1 //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs ----------*-==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements Live Variables analysis for source-level CFGs. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Analysis/Analyses/LiveVariables.h" 15 #include "clang/AST/Stmt.h" 16 #include "clang/AST/StmtVisitor.h" 17 #include "clang/Analysis/Analyses/PostOrderCFGView.h" 18 #include "clang/Analysis/AnalysisContext.h" 19 #include "clang/Analysis/CFG.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/PostOrderIterator.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <algorithm> 24 #include <vector> 25 26 using namespace clang; 27 28 namespace { 29 30 class DataflowWorklist { 31 SmallVector<const CFGBlock *, 20> worklist; 32 llvm::BitVector enqueuedBlocks; 33 PostOrderCFGView *POV; 34 public: 35 DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx) 36 : enqueuedBlocks(cfg.getNumBlockIDs()), 37 POV(Ctx.getAnalysis<PostOrderCFGView>()) {} 38 39 void enqueueBlock(const CFGBlock *block); 40 void enqueuePredecessors(const CFGBlock *block); 41 42 const CFGBlock *dequeue(); 43 44 void sortWorklist(); 45 }; 46 47 } 48 49 void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) { 50 if (block && !enqueuedBlocks[block->getBlockID()]) { 51 enqueuedBlocks[block->getBlockID()] = true; 52 worklist.push_back(block); 53 } 54 } 55 56 void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) { 57 const unsigned OldWorklistSize = worklist.size(); 58 for (CFGBlock::const_pred_iterator I = block->pred_begin(), 59 E = block->pred_end(); I != E; ++I) { 60 enqueueBlock(*I); 61 } 62 63 if (OldWorklistSize == 0 || OldWorklistSize == worklist.size()) 64 return; 65 66 sortWorklist(); 67 } 68 69 void DataflowWorklist::sortWorklist() { 70 std::sort(worklist.begin(), worklist.end(), POV->getComparator()); 71 } 72 73 const CFGBlock *DataflowWorklist::dequeue() { 74 if (worklist.empty()) 75 return nullptr; 76 const CFGBlock *b = worklist.pop_back_val(); 77 enqueuedBlocks[b->getBlockID()] = false; 78 return b; 79 } 80 81 namespace { 82 class LiveVariablesImpl { 83 public: 84 AnalysisDeclContext &analysisContext; 85 std::vector<LiveVariables::LivenessValues> cfgBlockValues; 86 llvm::ImmutableSet<const Stmt *>::Factory SSetFact; 87 llvm::ImmutableSet<const VarDecl *>::Factory DSetFact; 88 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness; 89 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness; 90 llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness; 91 llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment; 92 const bool killAtAssign; 93 94 LiveVariables::LivenessValues 95 merge(LiveVariables::LivenessValues valsA, 96 LiveVariables::LivenessValues valsB); 97 98 LiveVariables::LivenessValues 99 runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val, 100 LiveVariables::Observer *obs = nullptr); 101 102 void dumpBlockLiveness(const SourceManager& M); 103 104 LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign) 105 : analysisContext(ac), 106 SSetFact(false), // Do not canonicalize ImmutableSets by default. 107 DSetFact(false), // This is a *major* performance win. 108 killAtAssign(KillAtAssign) {} 109 }; 110 } 111 112 static LiveVariablesImpl &getImpl(void *x) { 113 return *((LiveVariablesImpl *) x); 114 } 115 116 //===----------------------------------------------------------------------===// 117 // Operations and queries on LivenessValues. 118 //===----------------------------------------------------------------------===// 119 120 bool LiveVariables::LivenessValues::isLive(const Stmt *S) const { 121 return liveStmts.contains(S); 122 } 123 124 bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const { 125 return liveDecls.contains(D); 126 } 127 128 namespace { 129 template <typename SET> 130 SET mergeSets(SET A, SET B) { 131 if (A.isEmpty()) 132 return B; 133 134 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) { 135 A = A.add(*it); 136 } 137 return A; 138 } 139 } 140 141 void LiveVariables::Observer::anchor() { } 142 143 LiveVariables::LivenessValues 144 LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA, 145 LiveVariables::LivenessValues valsB) { 146 147 llvm::ImmutableSetRef<const Stmt *> 148 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()), 149 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()); 150 151 152 llvm::ImmutableSetRef<const VarDecl *> 153 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()), 154 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()); 155 156 157 SSetRefA = mergeSets(SSetRefA, SSetRefB); 158 DSetRefA = mergeSets(DSetRefA, DSetRefB); 159 160 // asImmutableSet() canonicalizes the tree, allowing us to do an easy 161 // comparison afterwards. 162 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(), 163 DSetRefA.asImmutableSet()); 164 } 165 166 bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const { 167 return liveStmts == V.liveStmts && liveDecls == V.liveDecls; 168 } 169 170 //===----------------------------------------------------------------------===// 171 // Query methods. 172 //===----------------------------------------------------------------------===// 173 174 static bool isAlwaysAlive(const VarDecl *D) { 175 return D->hasGlobalStorage(); 176 } 177 178 bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) { 179 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D); 180 } 181 182 bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) { 183 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D); 184 } 185 186 bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) { 187 return getImpl(impl).stmtsToLiveness[Loc].isLive(S); 188 } 189 190 //===----------------------------------------------------------------------===// 191 // Dataflow computation. 192 //===----------------------------------------------------------------------===// 193 194 namespace { 195 class TransferFunctions : public StmtVisitor<TransferFunctions> { 196 LiveVariablesImpl &LV; 197 LiveVariables::LivenessValues &val; 198 LiveVariables::Observer *observer; 199 const CFGBlock *currentBlock; 200 public: 201 TransferFunctions(LiveVariablesImpl &im, 202 LiveVariables::LivenessValues &Val, 203 LiveVariables::Observer *Observer, 204 const CFGBlock *CurrentBlock) 205 : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {} 206 207 void VisitBinaryOperator(BinaryOperator *BO); 208 void VisitBlockExpr(BlockExpr *BE); 209 void VisitDeclRefExpr(DeclRefExpr *DR); 210 void VisitDeclStmt(DeclStmt *DS); 211 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS); 212 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE); 213 void VisitUnaryOperator(UnaryOperator *UO); 214 void Visit(Stmt *S); 215 }; 216 } 217 218 static const VariableArrayType *FindVA(QualType Ty) { 219 const Type *ty = Ty.getTypePtr(); 220 while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) { 221 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT)) 222 if (VAT->getSizeExpr()) 223 return VAT; 224 225 ty = VT->getElementType().getTypePtr(); 226 } 227 228 return nullptr; 229 } 230 231 static const Stmt *LookThroughStmt(const Stmt *S) { 232 while (S) { 233 if (const Expr *Ex = dyn_cast<Expr>(S)) 234 S = Ex->IgnoreParens(); 235 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) { 236 S = EWC->getSubExpr(); 237 continue; 238 } 239 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) { 240 S = OVE->getSourceExpr(); 241 continue; 242 } 243 break; 244 } 245 return S; 246 } 247 248 static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set, 249 llvm::ImmutableSet<const Stmt *>::Factory &F, 250 const Stmt *S) { 251 Set = F.add(Set, LookThroughStmt(S)); 252 } 253 254 void TransferFunctions::Visit(Stmt *S) { 255 if (observer) 256 observer->observeStmt(S, currentBlock, val); 257 258 StmtVisitor<TransferFunctions>::Visit(S); 259 260 if (isa<Expr>(S)) { 261 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S); 262 } 263 264 // Mark all children expressions live. 265 266 switch (S->getStmtClass()) { 267 default: 268 break; 269 case Stmt::StmtExprClass: { 270 // For statement expressions, look through the compound statement. 271 S = cast<StmtExpr>(S)->getSubStmt(); 272 break; 273 } 274 case Stmt::CXXMemberCallExprClass: { 275 // Include the implicit "this" pointer as being live. 276 CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S); 277 if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) { 278 AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj); 279 } 280 break; 281 } 282 case Stmt::ObjCMessageExprClass: { 283 // In calls to super, include the implicit "self" pointer as being live. 284 ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S); 285 if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance) 286 val.liveDecls = LV.DSetFact.add(val.liveDecls, 287 LV.analysisContext.getSelfDecl()); 288 break; 289 } 290 case Stmt::DeclStmtClass: { 291 const DeclStmt *DS = cast<DeclStmt>(S); 292 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) { 293 for (const VariableArrayType* VA = FindVA(VD->getType()); 294 VA != nullptr; VA = FindVA(VA->getElementType())) { 295 AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr()); 296 } 297 } 298 break; 299 } 300 case Stmt::PseudoObjectExprClass: { 301 // A pseudo-object operation only directly consumes its result 302 // expression. 303 Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr(); 304 if (!child) return; 305 if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child)) 306 child = OV->getSourceExpr(); 307 child = child->IgnoreParens(); 308 val.liveStmts = LV.SSetFact.add(val.liveStmts, child); 309 return; 310 } 311 312 // FIXME: These cases eventually shouldn't be needed. 313 case Stmt::ExprWithCleanupsClass: { 314 S = cast<ExprWithCleanups>(S)->getSubExpr(); 315 break; 316 } 317 case Stmt::CXXBindTemporaryExprClass: { 318 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr(); 319 break; 320 } 321 case Stmt::UnaryExprOrTypeTraitExprClass: { 322 // No need to unconditionally visit subexpressions. 323 return; 324 } 325 } 326 327 for (Stmt::child_iterator it = S->child_begin(), ei = S->child_end(); 328 it != ei; ++it) { 329 if (Stmt *child = *it) 330 AddLiveStmt(val.liveStmts, LV.SSetFact, child); 331 } 332 } 333 334 void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) { 335 if (B->isAssignmentOp()) { 336 if (!LV.killAtAssign) 337 return; 338 339 // Assigning to a variable? 340 Expr *LHS = B->getLHS()->IgnoreParens(); 341 342 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) 343 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 344 // Assignments to references don't kill the ref's address 345 if (VD->getType()->isReferenceType()) 346 return; 347 348 if (!isAlwaysAlive(VD)) { 349 // The variable is now dead. 350 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 351 } 352 353 if (observer) 354 observer->observerKill(DR); 355 } 356 } 357 } 358 359 void TransferFunctions::VisitBlockExpr(BlockExpr *BE) { 360 AnalysisDeclContext::referenced_decls_iterator I, E; 361 std::tie(I, E) = 362 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl()); 363 for ( ; I != E ; ++I) { 364 const VarDecl *VD = *I; 365 if (isAlwaysAlive(VD)) 366 continue; 367 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); 368 } 369 } 370 371 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) { 372 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl())) 373 if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end()) 374 val.liveDecls = LV.DSetFact.add(val.liveDecls, D); 375 } 376 377 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { 378 for (const auto *DI : DS->decls()) 379 if (const auto *VD = dyn_cast<VarDecl>(DI)) { 380 if (!isAlwaysAlive(VD)) 381 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 382 } 383 } 384 385 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) { 386 // Kill the iteration variable. 387 DeclRefExpr *DR = nullptr; 388 const VarDecl *VD = nullptr; 389 390 Stmt *element = OS->getElement(); 391 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) { 392 VD = cast<VarDecl>(DS->getSingleDecl()); 393 } 394 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) { 395 VD = cast<VarDecl>(DR->getDecl()); 396 } 397 398 if (VD) { 399 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); 400 if (observer && DR) 401 observer->observerKill(DR); 402 } 403 } 404 405 void TransferFunctions:: 406 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE) 407 { 408 // While sizeof(var) doesn't technically extend the liveness of 'var', it 409 // does extent the liveness of metadata if 'var' is a VariableArrayType. 410 // We handle that special case here. 411 if (UE->getKind() != UETT_SizeOf || UE->isArgumentType()) 412 return; 413 414 const Expr *subEx = UE->getArgumentExpr(); 415 if (subEx->getType()->isVariableArrayType()) { 416 assert(subEx->isLValue()); 417 val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens()); 418 } 419 } 420 421 void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) { 422 // Treat ++/-- as a kill. 423 // Note we don't actually have to do anything if we don't have an observer, 424 // since a ++/-- acts as both a kill and a "use". 425 if (!observer) 426 return; 427 428 switch (UO->getOpcode()) { 429 default: 430 return; 431 case UO_PostInc: 432 case UO_PostDec: 433 case UO_PreInc: 434 case UO_PreDec: 435 break; 436 } 437 438 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) 439 if (isa<VarDecl>(DR->getDecl())) { 440 // Treat ++/-- as a kill. 441 observer->observerKill(DR); 442 } 443 } 444 445 LiveVariables::LivenessValues 446 LiveVariablesImpl::runOnBlock(const CFGBlock *block, 447 LiveVariables::LivenessValues val, 448 LiveVariables::Observer *obs) { 449 450 TransferFunctions TF(*this, val, obs, block); 451 452 // Visit the terminator (if any). 453 if (const Stmt *term = block->getTerminator()) 454 TF.Visit(const_cast<Stmt*>(term)); 455 456 // Apply the transfer function for all Stmts in the block. 457 for (CFGBlock::const_reverse_iterator it = block->rbegin(), 458 ei = block->rend(); it != ei; ++it) { 459 const CFGElement &elem = *it; 460 461 if (Optional<CFGAutomaticObjDtor> Dtor = 462 elem.getAs<CFGAutomaticObjDtor>()) { 463 val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl()); 464 continue; 465 } 466 467 if (!elem.getAs<CFGStmt>()) 468 continue; 469 470 const Stmt *S = elem.castAs<CFGStmt>().getStmt(); 471 TF.Visit(const_cast<Stmt*>(S)); 472 stmtsToLiveness[S] = val; 473 } 474 return val; 475 } 476 477 void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) { 478 const CFG *cfg = getImpl(impl).analysisContext.getCFG(); 479 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) 480 getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs); 481 } 482 483 LiveVariables::LiveVariables(void *im) : impl(im) {} 484 485 LiveVariables::~LiveVariables() { 486 delete (LiveVariablesImpl*) impl; 487 } 488 489 LiveVariables * 490 LiveVariables::computeLiveness(AnalysisDeclContext &AC, 491 bool killAtAssign) { 492 493 // No CFG? Bail out. 494 CFG *cfg = AC.getCFG(); 495 if (!cfg) 496 return nullptr; 497 498 // The analysis currently has scalability issues for very large CFGs. 499 // Bail out if it looks too large. 500 if (cfg->getNumBlockIDs() > 300000) 501 return nullptr; 502 503 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign); 504 505 // Construct the dataflow worklist. Enqueue the exit block as the 506 // start of the analysis. 507 DataflowWorklist worklist(*cfg, AC); 508 llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs()); 509 510 // FIXME: we should enqueue using post order. 511 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) { 512 const CFGBlock *block = *it; 513 worklist.enqueueBlock(block); 514 515 // FIXME: Scan for DeclRefExprs using in the LHS of an assignment. 516 // We need to do this because we lack context in the reverse analysis 517 // to determine if a DeclRefExpr appears in such a context, and thus 518 // doesn't constitute a "use". 519 if (killAtAssign) 520 for (CFGBlock::const_iterator bi = block->begin(), be = block->end(); 521 bi != be; ++bi) { 522 if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) { 523 if (const BinaryOperator *BO = 524 dyn_cast<BinaryOperator>(cs->getStmt())) { 525 if (BO->getOpcode() == BO_Assign) { 526 if (const DeclRefExpr *DR = 527 dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) { 528 LV->inAssignment[DR] = 1; 529 } 530 } 531 } 532 } 533 } 534 } 535 536 worklist.sortWorklist(); 537 538 while (const CFGBlock *block = worklist.dequeue()) { 539 // Determine if the block's end value has changed. If not, we 540 // have nothing left to do for this block. 541 LivenessValues &prevVal = LV->blocksEndToLiveness[block]; 542 543 // Merge the values of all successor blocks. 544 LivenessValues val; 545 for (CFGBlock::const_succ_iterator it = block->succ_begin(), 546 ei = block->succ_end(); it != ei; ++it) { 547 if (const CFGBlock *succ = *it) { 548 val = LV->merge(val, LV->blocksBeginToLiveness[succ]); 549 } 550 } 551 552 if (!everAnalyzedBlock[block->getBlockID()]) 553 everAnalyzedBlock[block->getBlockID()] = true; 554 else if (prevVal.equals(val)) 555 continue; 556 557 prevVal = val; 558 559 // Update the dataflow value for the start of this block. 560 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val); 561 562 // Enqueue the value to the predecessors. 563 worklist.enqueuePredecessors(block); 564 } 565 566 return new LiveVariables(LV); 567 } 568 569 void LiveVariables::dumpBlockLiveness(const SourceManager &M) { 570 getImpl(impl).dumpBlockLiveness(M); 571 } 572 573 void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) { 574 std::vector<const CFGBlock *> vec; 575 for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator 576 it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end(); 577 it != ei; ++it) { 578 vec.push_back(it->first); 579 } 580 std::sort(vec.begin(), vec.end(), [](const CFGBlock *A, const CFGBlock *B) { 581 return A->getBlockID() < B->getBlockID(); 582 }); 583 584 std::vector<const VarDecl*> declVec; 585 586 for (std::vector<const CFGBlock *>::iterator 587 it = vec.begin(), ei = vec.end(); it != ei; ++it) { 588 llvm::errs() << "\n[ B" << (*it)->getBlockID() 589 << " (live variables at block exit) ]\n"; 590 591 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it]; 592 declVec.clear(); 593 594 for (llvm::ImmutableSet<const VarDecl *>::iterator si = 595 vals.liveDecls.begin(), 596 se = vals.liveDecls.end(); si != se; ++si) { 597 declVec.push_back(*si); 598 } 599 600 std::sort(declVec.begin(), declVec.end(), [](const Decl *A, const Decl *B) { 601 return A->getLocStart() < B->getLocStart(); 602 }); 603 604 for (std::vector<const VarDecl*>::iterator di = declVec.begin(), 605 de = declVec.end(); di != de; ++di) { 606 llvm::errs() << " " << (*di)->getDeclName().getAsString() 607 << " <"; 608 (*di)->getLocation().dump(M); 609 llvm::errs() << ">\n"; 610 } 611 } 612 llvm::errs() << "\n"; 613 } 614 615 const void *LiveVariables::getTag() { static int x; return &x; } 616 const void *RelaxedLiveVariables::getTag() { static int x; return &x; } 617