blob: 8e6c6e0555c65124c860352a367da371e01f8afb [file] [log] [blame]
Ted Kremenekaa04c512007-09-06 00:17:54 +00001//==- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source 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/LiveVariables.h"
Ted Kremenek05334682007-09-06 21:26:58 +000015#include "clang/Basic/SourceManager.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/CFG.h"
Ted Kremenek00b63272007-09-12 19:10:52 +000018#include "clang/Analysis/DataflowStmtVisitor.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000019#include "clang/Lex/IdentifierTable.h"
20#include "llvm/ADT/SmallPtrSet.h"
21
Ted Kremenek05334682007-09-06 21:26:58 +000022#include <string.h>
23#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000024
25using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// RegisterDecls - Utility class to create VarInfo objects for all
29// Decls referenced in a function.
30//
31
32namespace {
33
34class RegisterDecls : public StmtVisitor<RegisterDecls,void> {
35 LiveVariables& L;
36 const CFG& cfg;
37public:
38 RegisterDecls(LiveVariables& l, const CFG& c)
39 : L(l), cfg(c) {}
40
41 void VisitStmt(Stmt* S);
42 void VisitDeclRefExpr(DeclRefExpr* DR);
Ted Kremenekf63bda52007-09-06 23:25:10 +000043 void VisitDeclStmt(DeclStmt* DS);
Ted Kremenekaa04c512007-09-06 00:17:54 +000044 void Register(Decl* D);
Ted Kremenekf63bda52007-09-06 23:25:10 +000045 void RegisterDeclChain(Decl* D);
Ted Kremenekaa04c512007-09-06 00:17:54 +000046 void RegisterUsedDecls();
47};
48
49void RegisterDecls::VisitStmt(Stmt* S) {
50 for (Stmt::child_iterator I = S->child_begin(),E = S->child_end(); I != E;++I)
51 Visit(*I);
52}
53
54void RegisterDecls::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenekf63bda52007-09-06 23:25:10 +000055 RegisterDeclChain(DR->getDecl());
56}
57
58void RegisterDecls::VisitDeclStmt(DeclStmt* DS) {
59 RegisterDeclChain(DS->getDecl());
60}
61
62void RegisterDecls::RegisterDeclChain(Decl* D) {
63 for (; D != NULL ; D = D->getNextDeclarator())
Ted Kremenekaa04c512007-09-06 00:17:54 +000064 Register(D);
65}
66
67void RegisterDecls::Register(Decl* D) {
Ted Kremenek6b2b4e32007-09-10 17:36:42 +000068 if (VarDecl* V = dyn_cast<VarDecl>(D)) {
69 LiveVariables::VPair& VP = L.getVarInfoMap()[V];
Ted Kremenekaa04c512007-09-06 00:17:54 +000070
Ted Kremenek6b2b4e32007-09-10 17:36:42 +000071 VP.V.AliveBlocks.resize(cfg.getNumBlockIDs());
72 VP.Idx = L.getNumDecls()++;
73 }
Ted Kremenekaa04c512007-09-06 00:17:54 +000074}
75
76void RegisterDecls::RegisterUsedDecls() {
77 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI)
78 for (CFGBlock::const_iterator SI=BI->begin(),SE = BI->end();SI != SE;++SI)
79 Visit(const_cast<Stmt*>(*SI));
80}
81
82
83} // end anonymous namespace
84
85//===----------------------------------------------------------------------===//
86// WorkList - Data structure representing the liveness algorithm worklist.
87//
88
89namespace {
90
91class WorkListTy {
92 typedef llvm::SmallPtrSet<const CFGBlock*,20> BlockSet;
93 BlockSet wlist;
94public:
95 void enqueue(const CFGBlock* B) { wlist.insert(B); }
96
97 const CFGBlock* dequeue() {
98 assert (!wlist.empty());
99 const CFGBlock* B = *wlist.begin();
100 wlist.erase(B);
101 return B;
102 }
103
104 bool isEmpty() const { return wlist.empty(); }
105};
106
107} // end anonymous namespace
108
109//===----------------------------------------------------------------------===//
110// TFuncs
111//
112
113namespace {
114
Ted Kremenek00b63272007-09-12 19:10:52 +0000115class LivenessTFuncs : public DataflowStmtVisitor<LivenessTFuncs,
116 dataflow::backward_analysis_tag> {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000117 LiveVariables& L;
118 llvm::BitVector Live;
Ted Kremenek05334682007-09-06 21:26:58 +0000119 llvm::BitVector KilledAtLeastOnce;
120 Stmt* CurrentStmt;
121 const CFGBlock* CurrentBlock;
122 bool blockPreviouslyProcessed;
Ted Kremenekd1d88262007-09-10 15:56:38 +0000123 LiveVariablesObserver* Observer;
Ted Kremenek00b63272007-09-12 19:10:52 +0000124
Ted Kremenekaa04c512007-09-06 00:17:54 +0000125public:
Ted Kremenekd1d88262007-09-10 15:56:38 +0000126 LivenessTFuncs(LiveVariables& l, LiveVariablesObserver* A = NULL)
Ted Kremenek05334682007-09-06 21:26:58 +0000127 : L(l), CurrentStmt(NULL), CurrentBlock(NULL),
Ted Kremenek00b63272007-09-12 19:10:52 +0000128 blockPreviouslyProcessed(false), Observer(A) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000129 Live.resize(l.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000130 KilledAtLeastOnce.resize(l.getNumDecls());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000131 }
Ted Kremenek00b63272007-09-12 19:10:52 +0000132
Ted Kremenekaa04c512007-09-06 00:17:54 +0000133 void VisitDeclRefExpr(DeclRefExpr* DR);
134 void VisitBinaryOperator(BinaryOperator* B);
135 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000136 void VisitDeclStmt(DeclStmt* DS);
137 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek00b63272007-09-12 19:10:52 +0000138 void ObserveStmt(Stmt* S);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000139
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000140 unsigned getIdx(const VarDecl* D) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000141 LiveVariables::VarInfoMap& V = L.getVarInfoMap();
142 LiveVariables::VarInfoMap::iterator I = V.find(D);
143 assert (I != V.end());
144 return I->second.Idx;
145 }
146
147 bool ProcessBlock(const CFGBlock* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000148 llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B);
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000149 LiveVariables::VarInfo& KillVar(VarDecl* D);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000150};
151
Ted Kremenek00b63272007-09-12 19:10:52 +0000152void LivenessTFuncs::ObserveStmt(Stmt* S) {
153 if (Observer) Observer->ObserveStmt(S,L,Live);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000154}
155
156void LivenessTFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
157 // Register a use of the variable.
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000158 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
159 Live.set(getIdx(V));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000160}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000161
Ted Kremenek00b63272007-09-12 19:10:52 +0000162void LivenessTFuncs::VisitBinaryOperator(BinaryOperator* B) {
163 if (B->isAssignmentOp()) VisitAssign(B);
164 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000165}
166
Ted Kremenek05334682007-09-06 21:26:58 +0000167void LivenessTFuncs::VisitUnaryOperator(UnaryOperator* U) {
168 switch (U->getOpcode()) {
169 case UnaryOperator::PostInc:
170 case UnaryOperator::PostDec:
171 case UnaryOperator::PreInc:
172 case UnaryOperator::PreDec:
173 case UnaryOperator::AddrOf:
174 // Walk through the subexpressions, blasting through ParenExprs until
175 // we either find a DeclRefExpr or some non-DeclRefExpr expression.
176 for (Stmt* S = U->getSubExpr() ; ; ) {
177 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
178 S = P->getSubExpr();
179 continue;
180 }
181 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
182 // Treat the --/++/& operator as a kill.
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000183 LiveVariables::VarInfo& V =
184 KillVar(cast<VarDecl>(DR->getDecl()));
Ted Kremenek05334682007-09-06 21:26:58 +0000185
186 if (!blockPreviouslyProcessed)
187 V.AddKill(CurrentStmt,DR);
188
189 VisitDeclRefExpr(DR);
190 }
191 else
192 Visit(S);
193
194 break;
195 }
196 break;
197
198 default:
Ted Kremenekcee54de2007-09-12 20:11:39 +0000199 Visit(U->getSubExpr());
Ted Kremenek05334682007-09-06 21:26:58 +0000200 break;
201 }
202}
203
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000204LiveVariables::VarInfo& LivenessTFuncs::KillVar(VarDecl* D) {
Ted Kremenek05334682007-09-06 21:26:58 +0000205 LiveVariables::VarInfoMap::iterator I = L.getVarInfoMap().find(D);
206
207 assert (I != L.getVarInfoMap().end() &&
208 "Declaration not managed by variable map in LiveVariables");
209
210 // Mark the variable dead, and remove the current block from
211 // the set of blocks where the variable may be alive the entire time.
212 Live.reset(I->second.Idx);
213 I->second.V.AliveBlocks.reset(CurrentBlock->getBlockID());
214
215 return I->second.V;
216}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000217
Ted Kremenek00b63272007-09-12 19:10:52 +0000218void LivenessTFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000219 // Check if we are assigning to a variable.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000220 Stmt* LHS = B->getLHS();
Ted Kremenek05334682007-09-06 21:26:58 +0000221
Ted Kremenekaa04c512007-09-06 00:17:54 +0000222 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000223 LiveVariables::VarInfo& V = KillVar(cast<VarDecl>(DR->getDecl()));
Ted Kremenek05334682007-09-06 21:26:58 +0000224
225 // We only need to register kills once, so we check if this block
226 // has been previously processed.
227 if (!blockPreviouslyProcessed)
Ted Kremenek42276bc2007-09-06 23:39:53 +0000228 V.AddKill(CurrentStmt,DR);
229
230 if (B->getOpcode() != BinaryOperator::Assign)
231 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000232 }
Ted Kremenek05334682007-09-06 21:26:58 +0000233 else
234 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000235
236 Visit(B->getRHS());
237}
238
Ted Kremenek05334682007-09-06 21:26:58 +0000239void LivenessTFuncs::VisitDeclStmt(DeclStmt* DS) {
Ted Kremenekd1d88262007-09-10 15:56:38 +0000240 if (Observer)
241 Observer->ObserveStmt(DS,L,Live);
Ted Kremenek05334682007-09-06 21:26:58 +0000242
243 // Declarations effectively "kill" a variable since they cannot possibly
244 // be live before they are declared. Declarations, however, are not kills
245 // in the sense that the value is obliterated, so we do not register
246 // DeclStmts as a "kill site" for a variable.
247 for (Decl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000248 KillVar(cast<VarDecl>(D));
Ted Kremenek05334682007-09-06 21:26:58 +0000249}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000250
Ted Kremenek05334682007-09-06 21:26:58 +0000251llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000252 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
253
254 LiveVariables::BlockLivenessMap::iterator I = BMap.find(B);
255 return (I == BMap.end()) ? NULL : &(I->second);
256}
257
Ted Kremenek00b63272007-09-12 19:10:52 +0000258
Ted Kremenekaa04c512007-09-06 00:17:54 +0000259bool LivenessTFuncs::ProcessBlock(const CFGBlock* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000260
261 CurrentBlock = B;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000262 Live.reset();
Ted Kremenek05334682007-09-06 21:26:58 +0000263 KilledAtLeastOnce.reset();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000264
Ted Kremenek05334682007-09-06 21:26:58 +0000265 // Check if this block has been previously processed.
266 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
267 LiveVariables::BlockLivenessMap::iterator BI = BMap.find(B);
268
269 blockPreviouslyProcessed = BI != BMap.end();
270
271 // Merge liveness information from all predecessors.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000272 for (CFGBlock::const_succ_iterator I=B->succ_begin(),E=B->succ_end();I!=E;++I)
Ted Kremenek05334682007-09-06 21:26:58 +0000273 if (llvm::BitVector* V = getBlockEntryLiveness(*I))
Ted Kremenekaa04c512007-09-06 00:17:54 +0000274 Live |= *V;
Ted Kremenek05334682007-09-06 21:26:58 +0000275
Ted Kremenekd1d88262007-09-10 15:56:38 +0000276 if (Observer)
Ted Kremenek00b63272007-09-12 19:10:52 +0000277 Observer->ObserveBlockExit(B,L,Live);
Ted Kremenek05334682007-09-06 21:26:58 +0000278
279 // Tentatively mark all variables alive at the end of the current block
280 // as being alive during the whole block. We then cull these out as
281 // we process the statements of this block.
282 for (LiveVariables::VarInfoMap::iterator
283 I=L.getVarInfoMap().begin(), E=L.getVarInfoMap().end(); I != E; ++I)
284 if (Live[I->second.Idx])
285 I->second.V.AliveBlocks.set(B->getBlockID());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000286
Ted Kremenek00b63272007-09-12 19:10:52 +0000287 // Visit the statements in reverse order;
288 VisitBlock(B);
289
Ted Kremenek05334682007-09-06 21:26:58 +0000290 // Compare the computed "Live" values with what we already have
291 // for the entry to this block.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000292 bool hasChanged = false;
293
Ted Kremenek05334682007-09-06 21:26:58 +0000294 if (!blockPreviouslyProcessed) {
295 // We have not previously calculated liveness information for this block.
296 // Lazily instantiate a bitvector, and copy the bits from Live.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000297 hasChanged = true;
298 llvm::BitVector& V = BMap[B];
299 V.resize(L.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000300 V = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000301 }
Ted Kremenek05334682007-09-06 21:26:58 +0000302 else if (BI->second != Live) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000303 hasChanged = true;
Ted Kremenek05334682007-09-06 21:26:58 +0000304 BI->second = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000305 }
306
307 return hasChanged;
308}
309
310} // end anonymous namespace
311
312//===----------------------------------------------------------------------===//
313// runOnCFG - Method to run the actual liveness computation.
314//
315
Ted Kremenekd1d88262007-09-10 15:56:38 +0000316void LiveVariables::runOnCFG(const CFG& cfg, LiveVariablesObserver* Observer) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000317 // Scan a CFG for DeclRefStmts. For each one, create a VarInfo object.
318 {
319 RegisterDecls R(*this,cfg);
320 R.RegisterUsedDecls();
321 }
322
323 // Create the worklist and enqueue the exit block.
324 WorkListTy WorkList;
325 WorkList.enqueue(&cfg.getExit());
326
327 // Create the state for transfer functions.
Ted Kremenekd1d88262007-09-10 15:56:38 +0000328 LivenessTFuncs TF(*this,Observer);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000329
330 // Process the worklist until it is empty.
331
332 while (!WorkList.isEmpty()) {
333 const CFGBlock* B = WorkList.dequeue();
334 if (TF.ProcessBlock(B))
335 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
336 I != E; ++I)
337 WorkList.enqueue(*I);
338 }
339
Ted Kremenek05334682007-09-06 21:26:58 +0000340 // Go through each block and reserve a bitvector. This is needed if
341 // a block was never visited by the worklist algorithm.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000342 for (CFG::const_iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
343 LiveAtBlockEntryMap[&(*I)].resize(NumDecls);
344}
345
Ted Kremenek05334682007-09-06 21:26:58 +0000346
Ted Kremenekd1d88262007-09-10 15:56:38 +0000347void LiveVariables::runOnBlock(const CFGBlock* B,
348 LiveVariablesObserver* Observer)
Ted Kremenek05334682007-09-06 21:26:58 +0000349{
Ted Kremenekd1d88262007-09-10 15:56:38 +0000350 LivenessTFuncs TF(*this,Observer);
Ted Kremenek05334682007-09-06 21:26:58 +0000351 TF.ProcessBlock(B);
352}
353
354//===----------------------------------------------------------------------===//
355// liveness queries
356//
357
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000358bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek05334682007-09-06 21:26:58 +0000359 BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B);
360 assert (I != LiveAtBlockEntryMap.end());
361
362 VarInfoMap::const_iterator VI = VarInfos.find(D);
363 assert (VI != VarInfos.end());
364
365 return I->second[VI->second.Idx];
366}
367
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000368bool LiveVariables::isLive(llvm::BitVector& Live, const VarDecl* D) const {
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000369 VarInfoMap::const_iterator VI = VarInfos.find(D);
370 assert (VI != VarInfos.end());
371 return Live[VI->second.Idx];
372}
373
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000374bool LiveVariables::KillsVar(const Stmt* S, const VarDecl* D) const {
Ted Kremenek05334682007-09-06 21:26:58 +0000375 VarInfoMap::const_iterator VI = VarInfos.find(D);
376 assert (VI != VarInfos.end());
377
378 for (VarInfo::KillsSet::const_iterator
379 I = VI->second.V.Kills.begin(), E = VI->second.V.Kills.end(); I!=E;++I)
380 if (I->first == S)
381 return true;
382
383 return false;
384}
385
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000386LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) {
Ted Kremenek05334682007-09-06 21:26:58 +0000387 VarInfoMap::iterator VI = VarInfos.find(D);
388 assert (VI != VarInfos.end());
389 return VI->second.V;
390}
391
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000392const LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) const{
Ted Kremenek05334682007-09-06 21:26:58 +0000393 return const_cast<LiveVariables*>(this)->getVarInfo(D);
394}
395
Ted Kremenekaa04c512007-09-06 00:17:54 +0000396//===----------------------------------------------------------------------===//
Ted Kremenekd1d88262007-09-10 15:56:38 +0000397// Defaults for LiveVariablesObserver
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000398
Ted Kremenekd1d88262007-09-10 15:56:38 +0000399void LiveVariablesObserver::ObserveStmt(Stmt* S, LiveVariables& L,
400 llvm::BitVector& V) {}
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000401
Ted Kremenekd1d88262007-09-10 15:56:38 +0000402void LiveVariablesObserver::ObserveBlockExit(const CFGBlock* B,
403 LiveVariables& L,
404 llvm::BitVector& V) {}
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000405
406//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000407// printing liveness state for debugging
408//
409
Ted Kremenek05334682007-09-06 21:26:58 +0000410void LiveVariables::dumpLiveness(const llvm::BitVector& V,
411 SourceManager& SM) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000412
413 for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) {
414 if (V[I->second.Idx]) {
Ted Kremenek05334682007-09-06 21:26:58 +0000415
416 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
417
418 fprintf(stderr, " %s <%s:%u:%u>\n",
419 I->first->getIdentifier()->getName(),
420 SM.getSourceName(PhysLoc),
421 SM.getLineNumber(PhysLoc),
422 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000423 }
424 }
425}
426
Ted Kremenek05334682007-09-06 21:26:58 +0000427void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000428 for (BlockLivenessMap::iterator I = LiveAtBlockEntryMap.begin(),
429 E = LiveAtBlockEntryMap.end();
430 I != E; ++I) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000431
Ted Kremenek05334682007-09-06 21:26:58 +0000432 fprintf(stderr,
433 "\n[ B%d (live variables at block entry) ]\n",
434 I->first->getBlockID());
435
436 dumpLiveness(I->second,M);
437 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000438
439 fprintf(stderr,"\n");
440}
441
442void LiveVariables::dumpVarLiveness(SourceManager& SM) const {
443
444 for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) {
445 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
446
447 fprintf(stderr, "[ %s <%s:%u:%u> ]\n",
448 I->first->getIdentifier()->getName(),
449 SM.getSourceName(PhysLoc),
450 SM.getLineNumber(PhysLoc),
451 SM.getColumnNumber(PhysLoc));
452
453 I->second.V.Dump(SM);
454 }
455}
456
457void LiveVariables::VarInfo::Dump(SourceManager& SM) const {
458 fprintf(stderr," Blocks Alive:");
459 for (unsigned i = 0; i < AliveBlocks.size(); ++i) {
460 if (i % 5 == 0)
461 fprintf(stderr,"\n ");
462
463 fprintf(stderr," B%d", i);
464 }
465
466 fprintf(stderr,"\n Kill Sites:\n");
467 for (KillsSet::const_iterator I = Kills.begin(), E = Kills.end(); I!=E; ++I) {
468 SourceLocation PhysLoc =
469 SM.getPhysicalLoc(I->second->getSourceRange().Begin());
470
471 fprintf(stderr, " <%s:%u:%u>\n",
472 SM.getSourceName(PhysLoc),
473 SM.getLineNumber(PhysLoc),
474 SM.getColumnNumber(PhysLoc));
475 }
476
477 fprintf(stderr,"\n");
Gabor Greif61ce98c2007-09-11 15:32:40 +0000478}