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/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::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness;
81 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness;
82 llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness;
83 llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment;
84 const bool killAtAssign;
86 LiveVariables::LivenessValues
87 merge(LiveVariables::LivenessValues valsA,
88 LiveVariables::LivenessValues valsB);
90 LiveVariables::LivenessValues
91 runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val,
92 LiveVariables::Observer *obs = nullptr);
94 void dumpBlockLiveness(const SourceManager& M);
96 LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign)
97 : analysisContext(ac),
98 SSetFact(false), // Do not canonicalize ImmutableSets by default.
99 DSetFact(false), // This is a *major* performance win.
100 killAtAssign(KillAtAssign) {}
104 static LiveVariablesImpl &getImpl(void *x) {
105 return *((LiveVariablesImpl *) x);
108 //===----------------------------------------------------------------------===//
109 // Operations and queries on LivenessValues.
110 //===----------------------------------------------------------------------===//
112 bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
113 return liveStmts.contains(S);
116 bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
117 return liveDecls.contains(D);
121 template <typename SET>
122 SET mergeSets(SET A, SET B) {
126 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
133 void LiveVariables::Observer::anchor() { }
135 LiveVariables::LivenessValues
136 LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
137 LiveVariables::LivenessValues valsB) {
139 llvm::ImmutableSetRef<const Stmt *>
140 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
141 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
144 llvm::ImmutableSetRef<const VarDecl *>
145 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
146 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
149 SSetRefA = mergeSets(SSetRefA, SSetRefB);
150 DSetRefA = mergeSets(DSetRefA, DSetRefB);
152 // asImmutableSet() canonicalizes the tree, allowing us to do an easy
153 // comparison afterwards.
154 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
155 DSetRefA.asImmutableSet());
158 bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
159 return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
162 //===----------------------------------------------------------------------===//
164 //===----------------------------------------------------------------------===//
166 static bool isAlwaysAlive(const VarDecl *D) {
167 return D->hasGlobalStorage();
170 bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
171 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
174 bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
175 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
178 bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
179 return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
182 //===----------------------------------------------------------------------===//
183 // Dataflow computation.
184 //===----------------------------------------------------------------------===//
187 class TransferFunctions : public StmtVisitor<TransferFunctions> {
188 LiveVariablesImpl &LV;
189 LiveVariables::LivenessValues &val;
190 LiveVariables::Observer *observer;
191 const CFGBlock *currentBlock;
193 TransferFunctions(LiveVariablesImpl &im,
194 LiveVariables::LivenessValues &Val,
195 LiveVariables::Observer *Observer,
196 const CFGBlock *CurrentBlock)
197 : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {}
199 void VisitBinaryOperator(BinaryOperator *BO);
200 void VisitBlockExpr(BlockExpr *BE);
201 void VisitDeclRefExpr(DeclRefExpr *DR);
202 void VisitDeclStmt(DeclStmt *DS);
203 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS);
204 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE);
205 void VisitUnaryOperator(UnaryOperator *UO);
210 static const VariableArrayType *FindVA(QualType Ty) {
211 const Type *ty = Ty.getTypePtr();
212 while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) {
213 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT))
214 if (VAT->getSizeExpr())
217 ty = VT->getElementType().getTypePtr();
223 static const Stmt *LookThroughStmt(const Stmt *S) {
225 if (const Expr *Ex = dyn_cast<Expr>(S))
226 S = Ex->IgnoreParens();
227 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) {
228 S = EWC->getSubExpr();
231 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
232 S = OVE->getSourceExpr();
240 static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set,
241 llvm::ImmutableSet<const Stmt *>::Factory &F,
243 Set = F.add(Set, LookThroughStmt(S));
246 void TransferFunctions::Visit(Stmt *S) {
248 observer->observeStmt(S, currentBlock, val);
250 StmtVisitor<TransferFunctions>::Visit(S);
253 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
256 // Mark all children expressions live.
258 switch (S->getStmtClass()) {
261 case Stmt::StmtExprClass: {
262 // For statement expressions, look through the compound statement.
263 S = cast<StmtExpr>(S)->getSubStmt();
266 case Stmt::CXXMemberCallExprClass: {
267 // Include the implicit "this" pointer as being live.
268 CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S);
269 if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) {
270 AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj);
274 case Stmt::ObjCMessageExprClass: {
275 // In calls to super, include the implicit "self" pointer as being live.
276 ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S);
277 if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance)
278 val.liveDecls = LV.DSetFact.add(val.liveDecls,
279 LV.analysisContext.getSelfDecl());
282 case Stmt::DeclStmtClass: {
283 const DeclStmt *DS = cast<DeclStmt>(S);
284 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) {
285 for (const VariableArrayType* VA = FindVA(VD->getType());
286 VA != nullptr; VA = FindVA(VA->getElementType())) {
287 AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr());
292 case Stmt::PseudoObjectExprClass: {
293 // A pseudo-object operation only directly consumes its result
295 Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr();
297 if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child))
298 child = OV->getSourceExpr();
299 child = child->IgnoreParens();
300 val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
304 // FIXME: These cases eventually shouldn't be needed.
305 case Stmt::ExprWithCleanupsClass: {
306 S = cast<ExprWithCleanups>(S)->getSubExpr();
309 case Stmt::CXXBindTemporaryExprClass: {
310 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
313 case Stmt::UnaryExprOrTypeTraitExprClass: {
314 // No need to unconditionally visit subexpressions.
319 for (Stmt *Child : S->children()) {
321 AddLiveStmt(val.liveStmts, LV.SSetFact, Child);
325 void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
326 if (B->isAssignmentOp()) {
327 if (!LV.killAtAssign)
330 // Assigning to a variable?
331 Expr *LHS = B->getLHS()->IgnoreParens();
333 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS))
334 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
335 // Assignments to references don't kill the ref's address
336 if (VD->getType()->isReferenceType())
339 if (!isAlwaysAlive(VD)) {
340 // The variable is now dead.
341 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
345 observer->observerKill(DR);
350 void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
351 for (const VarDecl *VD :
352 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) {
353 if (isAlwaysAlive(VD))
355 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
359 void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
360 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
361 if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end())
362 val.liveDecls = LV.DSetFact.add(val.liveDecls, D);
365 void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
366 for (const auto *DI : DS->decls())
367 if (const auto *VD = dyn_cast<VarDecl>(DI)) {
368 if (!isAlwaysAlive(VD))
369 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
373 void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
374 // Kill the iteration variable.
375 DeclRefExpr *DR = nullptr;
376 const VarDecl *VD = nullptr;
378 Stmt *element = OS->getElement();
379 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
380 VD = cast<VarDecl>(DS->getSingleDecl());
382 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
383 VD = cast<VarDecl>(DR->getDecl());
387 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
389 observer->observerKill(DR);
393 void TransferFunctions::
394 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
396 // While sizeof(var) doesn't technically extend the liveness of 'var', it
397 // does extent the liveness of metadata if 'var' is a VariableArrayType.
398 // We handle that special case here.
399 if (UE->getKind() != UETT_SizeOf || UE->isArgumentType())
402 const Expr *subEx = UE->getArgumentExpr();
403 if (subEx->getType()->isVariableArrayType()) {
404 assert(subEx->isLValue());
405 val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens());
409 void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) {
410 // Treat ++/-- as a kill.
411 // Note we don't actually have to do anything if we don't have an observer,
412 // since a ++/-- acts as both a kill and a "use".
416 switch (UO->getOpcode()) {
426 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens()))
427 if (isa<VarDecl>(DR->getDecl())) {
428 // Treat ++/-- as a kill.
429 observer->observerKill(DR);
433 LiveVariables::LivenessValues
434 LiveVariablesImpl::runOnBlock(const CFGBlock *block,
435 LiveVariables::LivenessValues val,
436 LiveVariables::Observer *obs) {
438 TransferFunctions TF(*this, val, obs, block);
440 // Visit the terminator (if any).
441 if (const Stmt *term = block->getTerminator())
442 TF.Visit(const_cast<Stmt*>(term));
444 // Apply the transfer function for all Stmts in the block.
445 for (CFGBlock::const_reverse_iterator it = block->rbegin(),
446 ei = block->rend(); it != ei; ++it) {
447 const CFGElement &elem = *it;
449 if (Optional<CFGAutomaticObjDtor> Dtor =
450 elem.getAs<CFGAutomaticObjDtor>()) {
451 val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl());
455 if (!elem.getAs<CFGStmt>())
458 const Stmt *S = elem.castAs<CFGStmt>().getStmt();
459 TF.Visit(const_cast<Stmt*>(S));
460 stmtsToLiveness[S] = val;
465 void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) {
466 const CFG *cfg = getImpl(impl).analysisContext.getCFG();
467 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it)
468 getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs);
471 LiveVariables::LiveVariables(void *im) : impl(im) {}
473 LiveVariables::~LiveVariables() {
474 delete (LiveVariablesImpl*) impl;
478 LiveVariables::computeLiveness(AnalysisDeclContext &AC,
482 CFG *cfg = AC.getCFG();
486 // The analysis currently has scalability issues for very large CFGs.
487 // Bail out if it looks too large.
488 if (cfg->getNumBlockIDs() > 300000)
491 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
493 // Construct the dataflow worklist. Enqueue the exit block as the
494 // start of the analysis.
495 DataflowWorklist worklist(*cfg, AC);
496 llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs());
498 // FIXME: we should enqueue using post order.
499 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
500 const CFGBlock *block = *it;
501 worklist.enqueueBlock(block);
503 // FIXME: Scan for DeclRefExprs using in the LHS of an assignment.
504 // We need to do this because we lack context in the reverse analysis
505 // to determine if a DeclRefExpr appears in such a context, and thus
506 // doesn't constitute a "use".
508 for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
510 if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) {
511 if (const BinaryOperator *BO =
512 dyn_cast<BinaryOperator>(cs->getStmt())) {
513 if (BO->getOpcode() == BO_Assign) {
514 if (const DeclRefExpr *DR =
515 dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
516 LV->inAssignment[DR] = 1;
524 while (const CFGBlock *block = worklist.dequeue()) {
525 // Determine if the block's end value has changed. If not, we
526 // have nothing left to do for this block.
527 LivenessValues &prevVal = LV->blocksEndToLiveness[block];
529 // Merge the values of all successor blocks.
531 for (CFGBlock::const_succ_iterator it = block->succ_begin(),
532 ei = block->succ_end(); it != ei; ++it) {
533 if (const CFGBlock *succ = *it) {
534 val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
538 if (!everAnalyzedBlock[block->getBlockID()])
539 everAnalyzedBlock[block->getBlockID()] = true;
540 else if (prevVal.equals(val))
545 // Update the dataflow value for the start of this block.
546 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
548 // Enqueue the value to the predecessors.
549 worklist.enqueuePredecessors(block);
552 return new LiveVariables(LV);
555 void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
556 getImpl(impl).dumpBlockLiveness(M);
559 void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
560 std::vector<const CFGBlock *> vec;
561 for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator
562 it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end();
564 vec.push_back(it->first);
566 std::sort(vec.begin(), vec.end(), [](const CFGBlock *A, const CFGBlock *B) {
567 return A->getBlockID() < B->getBlockID();
570 std::vector<const VarDecl*> declVec;
572 for (std::vector<const CFGBlock *>::iterator
573 it = vec.begin(), ei = vec.end(); it != ei; ++it) {
574 llvm::errs() << "\n[ B" << (*it)->getBlockID()
575 << " (live variables at block exit) ]\n";
577 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
580 for (llvm::ImmutableSet<const VarDecl *>::iterator si =
581 vals.liveDecls.begin(),
582 se = vals.liveDecls.end(); si != se; ++si) {
583 declVec.push_back(*si);
586 std::sort(declVec.begin(), declVec.end(), [](const Decl *A, const Decl *B) {
587 return A->getLocStart() < B->getLocStart();
590 for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
591 de = declVec.end(); di != de; ++di) {
592 llvm::errs() << " " << (*di)->getDeclName().getAsString()
594 (*di)->getLocation().dump(M);
595 llvm::errs() << ">\n";
598 llvm::errs() << "\n";
601 const void *LiveVariables::getTag() { static int x; return &x; }
602 const void *RelaxedLiveVariables::getTag() { static int x; return &x; }