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