LiveVariables.cpp 19 KB

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