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/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"
26 using namespace clang;
30 class DataflowWorklist {
31 SmallVector<const CFGBlock *, 20> worklist;
32 llvm::BitVector enqueuedBlocks;
33 PostOrderCFGView *POV;
35 DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx)
36 : enqueuedBlocks(cfg.getNumBlockIDs()),
37 POV(Ctx.getAnalysis<PostOrderCFGView>()) {}
39 void enqueueBlock(const CFGBlock *block);
40 void enqueuePredecessors(const CFGBlock *block);
42 const CFGBlock *dequeue();
49 void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) {
50 if (block && !enqueuedBlocks[block->getBlockID()]) {
51 enqueuedBlocks[block->getBlockID()] = true;
52 worklist.push_back(block);
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) {
63 if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
69 void DataflowWorklist::sortWorklist() {
70 std::sort(worklist.begin(), worklist.end(), POV->getComparator());
73 const CFGBlock *DataflowWorklist::dequeue() {
76 const CFGBlock *b = worklist.pop_back_val();
77 enqueuedBlocks[b->getBlockID()] = false;
82 class LiveVariablesImpl {
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;
94 LiveVariables::LivenessValues
95 merge(LiveVariables::LivenessValues valsA,
96 LiveVariables::LivenessValues valsB);
98 LiveVariables::LivenessValues
99 runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val,
100 LiveVariables::Observer *obs = nullptr);
102 void dumpBlockLiveness(const SourceManager& M);
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) {}
112 static LiveVariablesImpl &getImpl(void *x) {
113 return *((LiveVariablesImpl *) x);
116 //===----------------------------------------------------------------------===//
117 // Operations and queries on LivenessValues.
118 //===----------------------------------------------------------------------===//
120 bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
121 return liveStmts.contains(S);
124 bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
125 return liveDecls.contains(D);
129 template <typename SET>
130 SET mergeSets(SET A, SET B) {
134 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
141 void LiveVariables::Observer::anchor() { }
143 LiveVariables::LivenessValues
144 LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
145 LiveVariables::LivenessValues valsB) {
147 llvm::ImmutableSetRef<const Stmt *>
148 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
149 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
152 llvm::ImmutableSetRef<const VarDecl *>
153 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
154 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
157 SSetRefA = mergeSets(SSetRefA, SSetRefB);
158 DSetRefA = mergeSets(DSetRefA, DSetRefB);
160 // asImmutableSet() canonicalizes the tree, allowing us to do an easy
161 // comparison afterwards.
162 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
163 DSetRefA.asImmutableSet());
166 bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
167 return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
170 //===----------------------------------------------------------------------===//
172 //===----------------------------------------------------------------------===//
174 static bool isAlwaysAlive(const VarDecl *D) {
175 return D->hasGlobalStorage();
178 bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
179 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
182 bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
183 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
186 bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
187 return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
190 //===----------------------------------------------------------------------===//
191 // Dataflow computation.
192 //===----------------------------------------------------------------------===//
195 class TransferFunctions : public StmtVisitor<TransferFunctions> {
196 LiveVariablesImpl &LV;
197 LiveVariables::LivenessValues &val;
198 LiveVariables::Observer *observer;
199 const CFGBlock *currentBlock;
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) {}
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);
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())
225 ty = VT->getElementType().getTypePtr();
231 static const Stmt *LookThroughStmt(const Stmt *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();
239 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
240 S = OVE->getSourceExpr();
248 static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set,
249 llvm::ImmutableSet<const Stmt *>::Factory &F,
251 Set = F.add(Set, LookThroughStmt(S));
254 void TransferFunctions::Visit(Stmt *S) {
256 observer->observeStmt(S, currentBlock, val);
258 StmtVisitor<TransferFunctions>::Visit(S);
261 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
264 // Mark all children expressions live.
266 switch (S->getStmtClass()) {
269 case Stmt::StmtExprClass: {
270 // For statement expressions, look through the compound statement.
271 S = cast<StmtExpr>(S)->getSubStmt();
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);
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());
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());
300 case Stmt::PseudoObjectExprClass: {
301 // A pseudo-object operation only directly consumes its result
303 Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr();
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);
312 // FIXME: These cases eventually shouldn't be needed.
313 case Stmt::ExprWithCleanupsClass: {
314 S = cast<ExprWithCleanups>(S)->getSubExpr();
317 case Stmt::CXXBindTemporaryExprClass: {
318 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
321 case Stmt::UnaryExprOrTypeTraitExprClass: {
322 // No need to unconditionally visit subexpressions.
327 for (Stmt::child_iterator it = S->child_begin(), ei = S->child_end();
329 if (Stmt *child = *it)
330 AddLiveStmt(val.liveStmts, LV.SSetFact, child);
334 void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
335 if (B->isAssignmentOp()) {
336 if (!LV.killAtAssign)
339 // Assigning to a variable?
340 Expr *LHS = B->getLHS()->IgnoreParens();
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())
348 if (!isAlwaysAlive(VD)) {
349 // The variable is now dead.
350 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
354 observer->observerKill(DR);
359 void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
360 AnalysisDeclContext::referenced_decls_iterator I, E;
362 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl());
363 for ( ; I != E ; ++I) {
364 const VarDecl *VD = *I;
365 if (isAlwaysAlive(VD))
367 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
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);
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);
385 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
386 // Kill the iteration variable.
387 DeclRefExpr *DR = nullptr;
388 const VarDecl *VD = nullptr;
390 Stmt *element = OS->getElement();
391 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
392 VD = cast<VarDecl>(DS->getSingleDecl());
394 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
395 VD = cast<VarDecl>(DR->getDecl());
399 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
401 observer->observerKill(DR);
405 void TransferFunctions::
406 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
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())
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());
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".
428 switch (UO->getOpcode()) {
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);
445 LiveVariables::LivenessValues
446 LiveVariablesImpl::runOnBlock(const CFGBlock *block,
447 LiveVariables::LivenessValues val,
448 LiveVariables::Observer *obs) {
450 TransferFunctions TF(*this, val, obs, block);
452 // Visit the terminator (if any).
453 if (const Stmt *term = block->getTerminator())
454 TF.Visit(const_cast<Stmt*>(term));
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;
461 if (Optional<CFGAutomaticObjDtor> Dtor =
462 elem.getAs<CFGAutomaticObjDtor>()) {
463 val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl());
467 if (!elem.getAs<CFGStmt>())
470 const Stmt *S = elem.castAs<CFGStmt>().getStmt();
471 TF.Visit(const_cast<Stmt*>(S));
472 stmtsToLiveness[S] = val;
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);
483 LiveVariables::LiveVariables(void *im) : impl(im) {}
485 LiveVariables::~LiveVariables() {
486 delete (LiveVariablesImpl*) impl;
490 LiveVariables::computeLiveness(AnalysisDeclContext &AC,
494 CFG *cfg = AC.getCFG();
498 // The analysis currently has scalability issues for very large CFGs.
499 // Bail out if it looks too large.
500 if (cfg->getNumBlockIDs() > 300000)
503 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
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());
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);
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".
520 for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
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;
536 worklist.sortWorklist();
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];
543 // Merge the values of all successor blocks.
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]);
552 if (!everAnalyzedBlock[block->getBlockID()])
553 everAnalyzedBlock[block->getBlockID()] = true;
554 else if (prevVal.equals(val))
559 // Update the dataflow value for the start of this block.
560 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
562 // Enqueue the value to the predecessors.
563 worklist.enqueuePredecessors(block);
566 return new LiveVariables(LV);
569 void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
570 getImpl(impl).dumpBlockLiveness(M);
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();
578 vec.push_back(it->first);
580 std::sort(vec.begin(), vec.end(), [](const CFGBlock *A, const CFGBlock *B) {
581 return A->getBlockID() < B->getBlockID();
584 std::vector<const VarDecl*> declVec;
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";
591 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
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);
600 std::sort(declVec.begin(), declVec.end(), [](const Decl *A, const Decl *B) {
601 return A->getLocStart() < B->getLocStart();
604 for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
605 de = declVec.end(); di != de; ++di) {
606 llvm::errs() << " " << (*di)->getDeclName().getAsString()
608 (*di)->getLocation().dump(M);
609 llvm::errs() << ">\n";
612 llvm::errs() << "\n";
615 const void *LiveVariables::getTag() { static int x; return &x; }
616 const void *RelaxedLiveVariables::getTag() { static int x; return &x; }