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