1 //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs ----------*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Live Variables analysis for source-level CFGs.
12 //===----------------------------------------------------------------------===//
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/AnalysisDeclContext.h"
19 #include "clang/Analysis/CFG.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PostOrderIterator.h"
22 #include "llvm/ADT/PriorityQueue.h"
23 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
31 class DataflowWorklist {
32 llvm::BitVector enqueuedBlocks;
33 PostOrderCFGView *POV;
34 llvm::PriorityQueue<const CFGBlock *, SmallVector<const CFGBlock *, 20>,
35 PostOrderCFGView::BlockOrderCompare> worklist;
38 DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx)
39 : enqueuedBlocks(cfg.getNumBlockIDs()),
40 POV(Ctx.getAnalysis<PostOrderCFGView>()),
41 worklist(POV->getComparator()) {}
43 void enqueueBlock(const CFGBlock *block);
44 void enqueuePredecessors(const CFGBlock *block);
46 const CFGBlock *dequeue();
51 void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) {
52 if (block && !enqueuedBlocks[block->getBlockID()]) {
53 enqueuedBlocks[block->getBlockID()] = true;
58 void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) {
59 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
60 E = block->pred_end(); I != E; ++I) {
65 const CFGBlock *DataflowWorklist::dequeue() {
68 const CFGBlock *b = worklist.top();
70 enqueuedBlocks[b->getBlockID()] = false;
75 class LiveVariablesImpl {
77 AnalysisDeclContext &analysisContext;
78 llvm::ImmutableSet<const Stmt *>::Factory SSetFact;
79 llvm::ImmutableSet<const VarDecl *>::Factory DSetFact;
80 llvm::ImmutableSet<const BindingDecl *>::Factory BSetFact;
81 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness;
82 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness;
83 llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness;
84 llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment;
85 const bool killAtAssign;
87 LiveVariables::LivenessValues
88 merge(LiveVariables::LivenessValues valsA,
89 LiveVariables::LivenessValues valsB);
91 LiveVariables::LivenessValues
92 runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val,
93 LiveVariables::Observer *obs = nullptr);
95 void dumpBlockLiveness(const SourceManager& M);
96 void dumpStmtLiveness(const SourceManager& M);
98 LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign)
99 : analysisContext(ac),
100 SSetFact(false), // Do not canonicalize ImmutableSets by default.
101 DSetFact(false), // This is a *major* performance win.
103 killAtAssign(KillAtAssign) {}
107 static LiveVariablesImpl &getImpl(void *x) {
108 return *((LiveVariablesImpl *) x);
111 //===----------------------------------------------------------------------===//
112 // Operations and queries on LivenessValues.
113 //===----------------------------------------------------------------------===//
115 bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
116 return liveStmts.contains(S);
119 bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
120 if (const auto *DD = dyn_cast<DecompositionDecl>(D)) {
122 for (const BindingDecl *BD : DD->bindings())
123 alive |= liveBindings.contains(BD);
126 return liveDecls.contains(D);
130 template <typename SET>
131 SET mergeSets(SET A, SET B) {
135 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
142 void LiveVariables::Observer::anchor() { }
144 LiveVariables::LivenessValues
145 LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
146 LiveVariables::LivenessValues valsB) {
148 llvm::ImmutableSetRef<const Stmt *>
149 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
150 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
153 llvm::ImmutableSetRef<const VarDecl *>
154 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
155 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
157 llvm::ImmutableSetRef<const BindingDecl *>
158 BSetRefA(valsA.liveBindings.getRootWithoutRetain(), BSetFact.getTreeFactory()),
159 BSetRefB(valsB.liveBindings.getRootWithoutRetain(), BSetFact.getTreeFactory());
161 SSetRefA = mergeSets(SSetRefA, SSetRefB);
162 DSetRefA = mergeSets(DSetRefA, DSetRefB);
163 BSetRefA = mergeSets(BSetRefA, BSetRefB);
165 // asImmutableSet() canonicalizes the tree, allowing us to do an easy
166 // comparison afterwards.
167 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
168 DSetRefA.asImmutableSet(),
169 BSetRefA.asImmutableSet());
172 bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
173 return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
176 //===----------------------------------------------------------------------===//
178 //===----------------------------------------------------------------------===//
180 static bool isAlwaysAlive(const VarDecl *D) {
181 return D->hasGlobalStorage();
184 bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
185 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
188 bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
189 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
192 bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
193 return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
196 //===----------------------------------------------------------------------===//
197 // Dataflow computation.
198 //===----------------------------------------------------------------------===//
201 class TransferFunctions : public StmtVisitor<TransferFunctions> {
202 LiveVariablesImpl &LV;
203 LiveVariables::LivenessValues &val;
204 LiveVariables::Observer *observer;
205 const CFGBlock *currentBlock;
207 TransferFunctions(LiveVariablesImpl &im,
208 LiveVariables::LivenessValues &Val,
209 LiveVariables::Observer *Observer,
210 const CFGBlock *CurrentBlock)
211 : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {}
213 void VisitBinaryOperator(BinaryOperator *BO);
214 void VisitBlockExpr(BlockExpr *BE);
215 void VisitDeclRefExpr(DeclRefExpr *DR);
216 void VisitDeclStmt(DeclStmt *DS);
217 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS);
218 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE);
219 void VisitUnaryOperator(UnaryOperator *UO);
224 static const VariableArrayType *FindVA(QualType Ty) {
225 const Type *ty = Ty.getTypePtr();
226 while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) {
227 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT))
228 if (VAT->getSizeExpr())
231 ty = VT->getElementType().getTypePtr();
237 static const Stmt *LookThroughStmt(const Stmt *S) {
239 if (const Expr *Ex = dyn_cast<Expr>(S))
240 S = Ex->IgnoreParens();
241 if (const FullExpr *FE = dyn_cast<FullExpr>(S)) {
242 S = FE->getSubExpr();
245 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
246 S = OVE->getSourceExpr();
254 static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set,
255 llvm::ImmutableSet<const Stmt *>::Factory &F,
257 Set = F.add(Set, LookThroughStmt(S));
260 void TransferFunctions::Visit(Stmt *S) {
262 observer->observeStmt(S, currentBlock, val);
264 StmtVisitor<TransferFunctions>::Visit(S);
267 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
270 // Mark all children expressions live.
272 switch (S->getStmtClass()) {
275 case Stmt::StmtExprClass: {
276 // For statement expressions, look through the compound statement.
277 S = cast<StmtExpr>(S)->getSubStmt();
280 case Stmt::CXXMemberCallExprClass: {
281 // Include the implicit "this" pointer as being live.
282 CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S);
283 if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) {
284 AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj);
288 case Stmt::ObjCMessageExprClass: {
289 // In calls to super, include the implicit "self" pointer as being live.
290 ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S);
291 if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance)
292 val.liveDecls = LV.DSetFact.add(val.liveDecls,
293 LV.analysisContext.getSelfDecl());
296 case Stmt::DeclStmtClass: {
297 const DeclStmt *DS = cast<DeclStmt>(S);
298 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) {
299 for (const VariableArrayType* VA = FindVA(VD->getType());
300 VA != nullptr; VA = FindVA(VA->getElementType())) {
301 AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr());
306 case Stmt::PseudoObjectExprClass: {
307 // A pseudo-object operation only directly consumes its result
309 Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr();
311 if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child))
312 child = OV->getSourceExpr();
313 child = child->IgnoreParens();
314 val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
318 // FIXME: These cases eventually shouldn't be needed.
319 case Stmt::ExprWithCleanupsClass: {
320 S = cast<ExprWithCleanups>(S)->getSubExpr();
323 case Stmt::CXXBindTemporaryExprClass: {
324 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
327 case Stmt::UnaryExprOrTypeTraitExprClass: {
328 // No need to unconditionally visit subexpressions.
331 case Stmt::IfStmtClass: {
332 // If one of the branches is an expression rather than a compound
333 // statement, it will be bad if we mark it as live at the terminator
334 // of the if-statement (i.e., immediately after the condition expression).
335 AddLiveStmt(val.liveStmts, LV.SSetFact, cast<IfStmt>(S)->getCond());
338 case Stmt::WhileStmtClass: {
339 // If the loop body is an expression rather than a compound statement,
340 // it will be bad if we mark it as live at the terminator of the loop
341 // (i.e., immediately after the condition expression).
342 AddLiveStmt(val.liveStmts, LV.SSetFact, cast<WhileStmt>(S)->getCond());
345 case Stmt::DoStmtClass: {
346 // If the loop body is an expression rather than a compound statement,
347 // it will be bad if we mark it as live at the terminator of the loop
348 // (i.e., immediately after the condition expression).
349 AddLiveStmt(val.liveStmts, LV.SSetFact, cast<DoStmt>(S)->getCond());
352 case Stmt::ForStmtClass: {
353 // If the loop body is an expression rather than a compound statement,
354 // it will be bad if we mark it as live at the terminator of the loop
355 // (i.e., immediately after the condition expression).
356 AddLiveStmt(val.liveStmts, LV.SSetFact, cast<ForStmt>(S)->getCond());
362 for (Stmt *Child : S->children()) {
364 AddLiveStmt(val.liveStmts, LV.SSetFact, Child);
368 static bool writeShouldKill(const VarDecl *VD) {
369 return VD && !VD->getType()->isReferenceType() &&
373 void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
374 if (B->isAssignmentOp()) {
375 if (!LV.killAtAssign)
378 // Assigning to a variable?
379 Expr *LHS = B->getLHS()->IgnoreParens();
381 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
382 const Decl* D = DR->getDecl();
385 if (const BindingDecl* BD = dyn_cast<BindingDecl>(D)) {
386 Killed = !BD->getType()->isReferenceType();
388 val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD);
389 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
390 Killed = writeShouldKill(VD);
392 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
396 if (Killed && observer)
397 observer->observerKill(DR);
402 void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
403 for (const VarDecl *VD :
404 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) {
405 if (isAlwaysAlive(VD))
407 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
411 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
412 const Decl* D = DR->getDecl();
413 bool InAssignment = LV.inAssignment[DR];
414 if (const auto *BD = dyn_cast<BindingDecl>(D)) {
416 val.liveBindings = LV.BSetFact.add(val.liveBindings, BD);
417 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
418 if (!InAssignment && !isAlwaysAlive(VD))
419 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
423 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
424 for (const auto *DI : DS->decls()) {
425 if (const auto *DD = dyn_cast<DecompositionDecl>(DI)) {
426 for (const auto *BD : DD->bindings())
427 val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD);
428 } else if (const auto *VD = dyn_cast<VarDecl>(DI)) {
429 if (!isAlwaysAlive(VD))
430 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
435 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
436 // Kill the iteration variable.
437 DeclRefExpr *DR = nullptr;
438 const VarDecl *VD = nullptr;
440 Stmt *element = OS->getElement();
441 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
442 VD = cast<VarDecl>(DS->getSingleDecl());
444 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
445 VD = cast<VarDecl>(DR->getDecl());
449 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
451 observer->observerKill(DR);
455 void TransferFunctions::
456 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
458 // While sizeof(var) doesn't technically extend the liveness of 'var', it
459 // does extent the liveness of metadata if 'var' is a VariableArrayType.
460 // We handle that special case here.
461 if (UE->getKind() != UETT_SizeOf || UE->isArgumentType())
464 const Expr *subEx = UE->getArgumentExpr();
465 if (subEx->getType()->isVariableArrayType()) {
466 assert(subEx->isLValue());
467 val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens());
471 void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) {
472 // Treat ++/-- as a kill.
473 // Note we don't actually have to do anything if we don't have an observer,
474 // since a ++/-- acts as both a kill and a "use".
478 switch (UO->getOpcode()) {
488 if (auto *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) {
489 const Decl *D = DR->getDecl();
490 if (isa<VarDecl>(D) || isa<BindingDecl>(D)) {
491 // Treat ++/-- as a kill.
492 observer->observerKill(DR);
497 LiveVariables::LivenessValues
498 LiveVariablesImpl::runOnBlock(const CFGBlock *block,
499 LiveVariables::LivenessValues val,
500 LiveVariables::Observer *obs) {
502 TransferFunctions TF(*this, val, obs, block);
504 // Visit the terminator (if any).
505 if (const Stmt *term = block->getTerminator())
506 TF.Visit(const_cast<Stmt*>(term));
508 // Apply the transfer function for all Stmts in the block.
509 for (CFGBlock::const_reverse_iterator it = block->rbegin(),
510 ei = block->rend(); it != ei; ++it) {
511 const CFGElement &elem = *it;
513 if (Optional<CFGAutomaticObjDtor> Dtor =
514 elem.getAs<CFGAutomaticObjDtor>()) {
515 val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl());
519 if (!elem.getAs<CFGStmt>())
522 const Stmt *S = elem.castAs<CFGStmt>().getStmt();
523 TF.Visit(const_cast<Stmt*>(S));
524 stmtsToLiveness[S] = val;
529 void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) {
530 const CFG *cfg = getImpl(impl).analysisContext.getCFG();
531 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it)
532 getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs);
535 LiveVariables::LiveVariables(void *im) : impl(im) {}
537 LiveVariables::~LiveVariables() {
538 delete (LiveVariablesImpl*) impl;
542 LiveVariables::computeLiveness(AnalysisDeclContext &AC,
546 CFG *cfg = AC.getCFG();
550 // The analysis currently has scalability issues for very large CFGs.
551 // Bail out if it looks too large.
552 if (cfg->getNumBlockIDs() > 300000)
555 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
557 // Construct the dataflow worklist. Enqueue the exit block as the
558 // start of the analysis.
559 DataflowWorklist worklist(*cfg, AC);
560 llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs());
562 // FIXME: we should enqueue using post order.
563 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
564 const CFGBlock *block = *it;
565 worklist.enqueueBlock(block);
567 // FIXME: Scan for DeclRefExprs using in the LHS of an assignment.
568 // We need to do this because we lack context in the reverse analysis
569 // to determine if a DeclRefExpr appears in such a context, and thus
570 // doesn't constitute a "use".
572 for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
574 if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) {
575 const Stmt* stmt = cs->getStmt();
576 if (const auto *BO = dyn_cast<BinaryOperator>(stmt)) {
577 if (BO->getOpcode() == BO_Assign) {
579 dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
580 LV->inAssignment[DR] = 1;
588 while (const CFGBlock *block = worklist.dequeue()) {
589 // Determine if the block's end value has changed. If not, we
590 // have nothing left to do for this block.
591 LivenessValues &prevVal = LV->blocksEndToLiveness[block];
593 // Merge the values of all successor blocks.
595 for (CFGBlock::const_succ_iterator it = block->succ_begin(),
596 ei = block->succ_end(); it != ei; ++it) {
597 if (const CFGBlock *succ = *it) {
598 val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
602 if (!everAnalyzedBlock[block->getBlockID()])
603 everAnalyzedBlock[block->getBlockID()] = true;
604 else if (prevVal.equals(val))
609 // Update the dataflow value for the start of this block.
610 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
612 // Enqueue the value to the predecessors.
613 worklist.enqueuePredecessors(block);
616 return new LiveVariables(LV);
619 void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
620 getImpl(impl).dumpBlockLiveness(M);
623 void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
624 std::vector<const CFGBlock *> vec;
625 for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator
626 it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end();
628 vec.push_back(it->first);
630 llvm::sort(vec, [](const CFGBlock *A, const CFGBlock *B) {
631 return A->getBlockID() < B->getBlockID();
634 std::vector<const VarDecl*> declVec;
636 for (std::vector<const CFGBlock *>::iterator
637 it = vec.begin(), ei = vec.end(); it != ei; ++it) {
638 llvm::errs() << "\n[ B" << (*it)->getBlockID()
639 << " (live variables at block exit) ]\n";
641 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
644 for (llvm::ImmutableSet<const VarDecl *>::iterator si =
645 vals.liveDecls.begin(),
646 se = vals.liveDecls.end(); si != se; ++si) {
647 declVec.push_back(*si);
650 llvm::sort(declVec, [](const Decl *A, const Decl *B) {
651 return A->getBeginLoc() < B->getBeginLoc();
654 for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
655 de = declVec.end(); di != de; ++di) {
656 llvm::errs() << " " << (*di)->getDeclName().getAsString()
658 (*di)->getLocation().print(llvm::errs(), M);
659 llvm::errs() << ">\n";
662 llvm::errs() << "\n";
665 void LiveVariables::dumpStmtLiveness(const SourceManager &M) {
666 getImpl(impl).dumpStmtLiveness(M);
669 void LiveVariablesImpl::dumpStmtLiveness(const SourceManager &M) {
670 // Don't iterate over blockEndsToLiveness directly because it's not sorted.
671 for (auto I : *analysisContext.getCFG()) {
673 llvm::errs() << "\n[ B" << I->getBlockID()
674 << " (live statements at block exit) ]\n";
675 for (auto S : blocksEndToLiveness[I].liveStmts) {
676 llvm::errs() << "\n";
679 llvm::errs() << "\n";
683 const void *LiveVariables::getTag() { static int x; return &x; }
684 const void *RelaxedLiveVariables::getTag() { static int x; return &x; }