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