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 llvm::ImmutableSet<const Stmt *>::Factory SSetFact;
86 llvm::ImmutableSet<const VarDecl *>::Factory DSetFact;
87 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness;
88 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness;
89 llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness;
90 llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment;
91 const bool killAtAssign;
93 LiveVariables::LivenessValues
94 merge(LiveVariables::LivenessValues valsA,
95 LiveVariables::LivenessValues valsB);
97 LiveVariables::LivenessValues
98 runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val,
99 LiveVariables::Observer *obs = nullptr);
101 void dumpBlockLiveness(const SourceManager& M);
103 LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign)
104 : analysisContext(ac),
105 SSetFact(false), // Do not canonicalize ImmutableSets by default.
106 DSetFact(false), // This is a *major* performance win.
107 killAtAssign(KillAtAssign) {}
111 static LiveVariablesImpl &getImpl(void *x) {
112 return *((LiveVariablesImpl *) x);
115 //===----------------------------------------------------------------------===//
116 // Operations and queries on LivenessValues.
117 //===----------------------------------------------------------------------===//
119 bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
120 return liveStmts.contains(S);
123 bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
124 return liveDecls.contains(D);
128 template <typename SET>
129 SET mergeSets(SET A, SET B) {
133 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
140 void LiveVariables::Observer::anchor() { }
142 LiveVariables::LivenessValues
143 LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
144 LiveVariables::LivenessValues valsB) {
146 llvm::ImmutableSetRef<const Stmt *>
147 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
148 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
151 llvm::ImmutableSetRef<const VarDecl *>
152 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
153 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
156 SSetRefA = mergeSets(SSetRefA, SSetRefB);
157 DSetRefA = mergeSets(DSetRefA, DSetRefB);
159 // asImmutableSet() canonicalizes the tree, allowing us to do an easy
160 // comparison afterwards.
161 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
162 DSetRefA.asImmutableSet());
165 bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
166 return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
169 //===----------------------------------------------------------------------===//
171 //===----------------------------------------------------------------------===//
173 static bool isAlwaysAlive(const VarDecl *D) {
174 return D->hasGlobalStorage();
177 bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
178 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
181 bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
182 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
185 bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
186 return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
189 //===----------------------------------------------------------------------===//
190 // Dataflow computation.
191 //===----------------------------------------------------------------------===//
194 class TransferFunctions : public StmtVisitor<TransferFunctions> {
195 LiveVariablesImpl &LV;
196 LiveVariables::LivenessValues &val;
197 LiveVariables::Observer *observer;
198 const CFGBlock *currentBlock;
200 TransferFunctions(LiveVariablesImpl &im,
201 LiveVariables::LivenessValues &Val,
202 LiveVariables::Observer *Observer,
203 const CFGBlock *CurrentBlock)
204 : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {}
206 void VisitBinaryOperator(BinaryOperator *BO);
207 void VisitBlockExpr(BlockExpr *BE);
208 void VisitDeclRefExpr(DeclRefExpr *DR);
209 void VisitDeclStmt(DeclStmt *DS);
210 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS);
211 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE);
212 void VisitUnaryOperator(UnaryOperator *UO);
217 static const VariableArrayType *FindVA(QualType Ty) {
218 const Type *ty = Ty.getTypePtr();
219 while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) {
220 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT))
221 if (VAT->getSizeExpr())
224 ty = VT->getElementType().getTypePtr();
230 static const Stmt *LookThroughStmt(const Stmt *S) {
232 if (const Expr *Ex = dyn_cast<Expr>(S))
233 S = Ex->IgnoreParens();
234 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) {
235 S = EWC->getSubExpr();
238 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
239 S = OVE->getSourceExpr();
247 static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set,
248 llvm::ImmutableSet<const Stmt *>::Factory &F,
250 Set = F.add(Set, LookThroughStmt(S));
253 void TransferFunctions::Visit(Stmt *S) {
255 observer->observeStmt(S, currentBlock, val);
257 StmtVisitor<TransferFunctions>::Visit(S);
260 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
263 // Mark all children expressions live.
265 switch (S->getStmtClass()) {
268 case Stmt::StmtExprClass: {
269 // For statement expressions, look through the compound statement.
270 S = cast<StmtExpr>(S)->getSubStmt();
273 case Stmt::CXXMemberCallExprClass: {
274 // Include the implicit "this" pointer as being live.
275 CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S);
276 if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) {
277 AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj);
281 case Stmt::ObjCMessageExprClass: {
282 // In calls to super, include the implicit "self" pointer as being live.
283 ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S);
284 if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance)
285 val.liveDecls = LV.DSetFact.add(val.liveDecls,
286 LV.analysisContext.getSelfDecl());
289 case Stmt::DeclStmtClass: {
290 const DeclStmt *DS = cast<DeclStmt>(S);
291 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) {
292 for (const VariableArrayType* VA = FindVA(VD->getType());
293 VA != nullptr; VA = FindVA(VA->getElementType())) {
294 AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr());
299 case Stmt::PseudoObjectExprClass: {
300 // A pseudo-object operation only directly consumes its result
302 Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr();
304 if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child))
305 child = OV->getSourceExpr();
306 child = child->IgnoreParens();
307 val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
311 // FIXME: These cases eventually shouldn't be needed.
312 case Stmt::ExprWithCleanupsClass: {
313 S = cast<ExprWithCleanups>(S)->getSubExpr();
316 case Stmt::CXXBindTemporaryExprClass: {
317 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
320 case Stmt::UnaryExprOrTypeTraitExprClass: {
321 // No need to unconditionally visit subexpressions.
326 for (Stmt::child_iterator it = S->child_begin(), ei = S->child_end();
328 if (Stmt *child = *it)
329 AddLiveStmt(val.liveStmts, LV.SSetFact, child);
333 void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
334 if (B->isAssignmentOp()) {
335 if (!LV.killAtAssign)
338 // Assigning to a variable?
339 Expr *LHS = B->getLHS()->IgnoreParens();
341 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS))
342 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
343 // Assignments to references don't kill the ref's address
344 if (VD->getType()->isReferenceType())
347 if (!isAlwaysAlive(VD)) {
348 // The variable is now dead.
349 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
353 observer->observerKill(DR);
358 void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
359 for (const VarDecl *VD :
360 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) {
361 if (isAlwaysAlive(VD))
363 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
367 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
368 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
369 if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end())
370 val.liveDecls = LV.DSetFact.add(val.liveDecls, D);
373 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
374 for (const auto *DI : DS->decls())
375 if (const auto *VD = dyn_cast<VarDecl>(DI)) {
376 if (!isAlwaysAlive(VD))
377 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
381 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
382 // Kill the iteration variable.
383 DeclRefExpr *DR = nullptr;
384 const VarDecl *VD = nullptr;
386 Stmt *element = OS->getElement();
387 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
388 VD = cast<VarDecl>(DS->getSingleDecl());
390 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
391 VD = cast<VarDecl>(DR->getDecl());
395 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
397 observer->observerKill(DR);
401 void TransferFunctions::
402 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
404 // While sizeof(var) doesn't technically extend the liveness of 'var', it
405 // does extent the liveness of metadata if 'var' is a VariableArrayType.
406 // We handle that special case here.
407 if (UE->getKind() != UETT_SizeOf || UE->isArgumentType())
410 const Expr *subEx = UE->getArgumentExpr();
411 if (subEx->getType()->isVariableArrayType()) {
412 assert(subEx->isLValue());
413 val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens());
417 void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) {
418 // Treat ++/-- as a kill.
419 // Note we don't actually have to do anything if we don't have an observer,
420 // since a ++/-- acts as both a kill and a "use".
424 switch (UO->getOpcode()) {
434 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens()))
435 if (isa<VarDecl>(DR->getDecl())) {
436 // Treat ++/-- as a kill.
437 observer->observerKill(DR);
441 LiveVariables::LivenessValues
442 LiveVariablesImpl::runOnBlock(const CFGBlock *block,
443 LiveVariables::LivenessValues val,
444 LiveVariables::Observer *obs) {
446 TransferFunctions TF(*this, val, obs, block);
448 // Visit the terminator (if any).
449 if (const Stmt *term = block->getTerminator())
450 TF.Visit(const_cast<Stmt*>(term));
452 // Apply the transfer function for all Stmts in the block.
453 for (CFGBlock::const_reverse_iterator it = block->rbegin(),
454 ei = block->rend(); it != ei; ++it) {
455 const CFGElement &elem = *it;
457 if (Optional<CFGAutomaticObjDtor> Dtor =
458 elem.getAs<CFGAutomaticObjDtor>()) {
459 val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl());
463 if (!elem.getAs<CFGStmt>())
466 const Stmt *S = elem.castAs<CFGStmt>().getStmt();
467 TF.Visit(const_cast<Stmt*>(S));
468 stmtsToLiveness[S] = val;
473 void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) {
474 const CFG *cfg = getImpl(impl).analysisContext.getCFG();
475 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it)
476 getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs);
479 LiveVariables::LiveVariables(void *im) : impl(im) {}
481 LiveVariables::~LiveVariables() {
482 delete (LiveVariablesImpl*) impl;
486 LiveVariables::computeLiveness(AnalysisDeclContext &AC,
490 CFG *cfg = AC.getCFG();
494 // The analysis currently has scalability issues for very large CFGs.
495 // Bail out if it looks too large.
496 if (cfg->getNumBlockIDs() > 300000)
499 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
501 // Construct the dataflow worklist. Enqueue the exit block as the
502 // start of the analysis.
503 DataflowWorklist worklist(*cfg, AC);
504 llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs());
506 // FIXME: we should enqueue using post order.
507 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
508 const CFGBlock *block = *it;
509 worklist.enqueueBlock(block);
511 // FIXME: Scan for DeclRefExprs using in the LHS of an assignment.
512 // We need to do this because we lack context in the reverse analysis
513 // to determine if a DeclRefExpr appears in such a context, and thus
514 // doesn't constitute a "use".
516 for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
518 if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) {
519 if (const BinaryOperator *BO =
520 dyn_cast<BinaryOperator>(cs->getStmt())) {
521 if (BO->getOpcode() == BO_Assign) {
522 if (const DeclRefExpr *DR =
523 dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
524 LV->inAssignment[DR] = 1;
532 worklist.sortWorklist();
534 while (const CFGBlock *block = worklist.dequeue()) {
535 // Determine if the block's end value has changed. If not, we
536 // have nothing left to do for this block.
537 LivenessValues &prevVal = LV->blocksEndToLiveness[block];
539 // Merge the values of all successor blocks.
541 for (CFGBlock::const_succ_iterator it = block->succ_begin(),
542 ei = block->succ_end(); it != ei; ++it) {
543 if (const CFGBlock *succ = *it) {
544 val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
548 if (!everAnalyzedBlock[block->getBlockID()])
549 everAnalyzedBlock[block->getBlockID()] = true;
550 else if (prevVal.equals(val))
555 // Update the dataflow value for the start of this block.
556 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
558 // Enqueue the value to the predecessors.
559 worklist.enqueuePredecessors(block);
562 return new LiveVariables(LV);
565 void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
566 getImpl(impl).dumpBlockLiveness(M);
569 void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
570 std::vector<const CFGBlock *> vec;
571 for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator
572 it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end();
574 vec.push_back(it->first);
576 std::sort(vec.begin(), vec.end(), [](const CFGBlock *A, const CFGBlock *B) {
577 return A->getBlockID() < B->getBlockID();
580 std::vector<const VarDecl*> declVec;
582 for (std::vector<const CFGBlock *>::iterator
583 it = vec.begin(), ei = vec.end(); it != ei; ++it) {
584 llvm::errs() << "\n[ B" << (*it)->getBlockID()
585 << " (live variables at block exit) ]\n";
587 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
590 for (llvm::ImmutableSet<const VarDecl *>::iterator si =
591 vals.liveDecls.begin(),
592 se = vals.liveDecls.end(); si != se; ++si) {
593 declVec.push_back(*si);
596 std::sort(declVec.begin(), declVec.end(), [](const Decl *A, const Decl *B) {
597 return A->getLocStart() < B->getLocStart();
600 for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
601 de = declVec.end(); di != de; ++di) {
602 llvm::errs() << " " << (*di)->getDeclName().getAsString()
604 (*di)->getLocation().dump(M);
605 llvm::errs() << ">\n";
608 llvm::errs() << "\n";
611 const void *LiveVariables::getTag() { static int x; return &x; }
612 const void *RelaxedLiveVariables::getTag() { static int x; return &x; }