blob: 629065190223aa95b52ce558967cfaa29c8a2749 [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"
18#include "clang/AST/StmtVisitor.h"
19#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);
43 void Register(Decl* D);
44 void RegisterUsedDecls();
45};
46
47void RegisterDecls::VisitStmt(Stmt* S) {
48 for (Stmt::child_iterator I = S->child_begin(),E = S->child_end(); I != E;++I)
49 Visit(*I);
50}
51
52void RegisterDecls::VisitDeclRefExpr(DeclRefExpr* DR) {
53 for (Decl* D = DR->getDecl() ; D != NULL ; D = D->getNextDeclarator())
54 Register(D);
55}
56
57void RegisterDecls::Register(Decl* D) {
58 LiveVariables::VPair& VP = L.getVarInfoMap()[const_cast<const Decl*>(D)];
59
60 VP.V.AliveBlocks.reserve(cfg.getNumBlockIDs());
61 VP.Idx = L.getNumDecls()++;
62}
63
64void RegisterDecls::RegisterUsedDecls() {
65 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI)
66 for (CFGBlock::const_iterator SI=BI->begin(),SE = BI->end();SI != SE;++SI)
67 Visit(const_cast<Stmt*>(*SI));
68}
69
70
71} // end anonymous namespace
72
73//===----------------------------------------------------------------------===//
74// WorkList - Data structure representing the liveness algorithm worklist.
75//
76
77namespace {
78
79class WorkListTy {
80 typedef llvm::SmallPtrSet<const CFGBlock*,20> BlockSet;
81 BlockSet wlist;
82public:
83 void enqueue(const CFGBlock* B) { wlist.insert(B); }
84
85 const CFGBlock* dequeue() {
86 assert (!wlist.empty());
87 const CFGBlock* B = *wlist.begin();
88 wlist.erase(B);
89 return B;
90 }
91
92 bool isEmpty() const { return wlist.empty(); }
93};
94
95} // end anonymous namespace
96
97//===----------------------------------------------------------------------===//
98// TFuncs
99//
100
101namespace {
102
103class LivenessTFuncs : public StmtVisitor<LivenessTFuncs,void> {
104 LiveVariables& L;
105 llvm::BitVector Live;
Ted Kremenek05334682007-09-06 21:26:58 +0000106 llvm::BitVector KilledAtLeastOnce;
107 Stmt* CurrentStmt;
108 const CFGBlock* CurrentBlock;
109 bool blockPreviouslyProcessed;
110 LiveVariablesAuditor* Auditor;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000111public:
Ted Kremenek05334682007-09-06 21:26:58 +0000112 LivenessTFuncs(LiveVariables& l, LiveVariablesAuditor* A = NULL)
113 : L(l), CurrentStmt(NULL), CurrentBlock(NULL),
114 blockPreviouslyProcessed(false), Auditor(A)
115 {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000116 Live.resize(l.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000117 KilledAtLeastOnce.resize(l.getNumDecls());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000118 }
119
120 void VisitStmt(Stmt* S);
121 void VisitDeclRefExpr(DeclRefExpr* DR);
122 void VisitBinaryOperator(BinaryOperator* B);
123 void VisitAssign(BinaryOperator* B);
124 void VisitStmtExpr(StmtExpr* S);
Ted Kremenek05334682007-09-06 21:26:58 +0000125 void VisitDeclStmt(DeclStmt* DS);
126 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000127
128 unsigned getIdx(const Decl* D) {
129 LiveVariables::VarInfoMap& V = L.getVarInfoMap();
130 LiveVariables::VarInfoMap::iterator I = V.find(D);
131 assert (I != V.end());
132 return I->second.Idx;
133 }
134
135 bool ProcessBlock(const CFGBlock* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000136 llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B);
137 LiveVariables::VarInfo& KillVar(Decl* D);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000138};
139
140void LivenessTFuncs::VisitStmt(Stmt* S) {
Ted Kremenek05334682007-09-06 21:26:58 +0000141 if (Auditor)
142 Auditor->AuditStmt(S,L,Live);
143
Ted Kremenekaa04c512007-09-06 00:17:54 +0000144 // Evaluate the transfer functions for all subexpressions. Note that
145 // each invocation of "Visit" will have a side-effect: "Liveness" and "Kills"
Ted Kremenek05334682007-09-06 21:26:58 +0000146 // will be updated.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000147 for (Stmt::child_iterator I = S->child_begin(),E = S->child_end(); I != E;++I)
148 Visit(*I);
149}
150
151void LivenessTFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek05334682007-09-06 21:26:58 +0000152 if (Auditor)
153 Auditor->AuditStmt(DR,L,Live);
154
Ted Kremenekaa04c512007-09-06 00:17:54 +0000155 // Register a use of the variable.
156 Live.set(getIdx(DR->getDecl()));
157}
158
159void LivenessTFuncs::VisitStmtExpr(StmtExpr* S) {
160 // Do nothing. The substatements of S are segmented into separate
161 // statements in the CFG.
162}
163
164void LivenessTFuncs::VisitBinaryOperator(BinaryOperator* B) {
165 switch (B->getOpcode()) {
166 case BinaryOperator::LAnd:
167 case BinaryOperator::LOr:
168 case BinaryOperator::Comma:
169 // Do nothing. These operations are broken up into multiple
170 // statements in the CFG. All these expressions do is return
Ted Kremenek05334682007-09-06 21:26:58 +0000171 // the value of their subexpressions, but these subexpressions will
Ted Kremenekaa04c512007-09-06 00:17:54 +0000172 // be evalualated elsewhere in the CFG.
173 break;
174
175 // FIXME: handle '++' and '--'
Ted Kremenek05334682007-09-06 21:26:58 +0000176 default:
Ted Kremenekaa04c512007-09-06 00:17:54 +0000177 if (B->isAssignmentOp()) VisitAssign(B);
Ted Kremenek05334682007-09-06 21:26:58 +0000178 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000179 }
180}
181
Ted Kremenek05334682007-09-06 21:26:58 +0000182void LivenessTFuncs::VisitUnaryOperator(UnaryOperator* U) {
183 switch (U->getOpcode()) {
184 case UnaryOperator::PostInc:
185 case UnaryOperator::PostDec:
186 case UnaryOperator::PreInc:
187 case UnaryOperator::PreDec:
188 case UnaryOperator::AddrOf:
189 // Walk through the subexpressions, blasting through ParenExprs until
190 // we either find a DeclRefExpr or some non-DeclRefExpr expression.
191 for (Stmt* S = U->getSubExpr() ; ; ) {
192 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
193 S = P->getSubExpr();
194 continue;
195 }
196 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
197 // Treat the --/++/& operator as a kill.
198 LiveVariables::VarInfo& V = KillVar(DR->getDecl());
199
200 if (!blockPreviouslyProcessed)
201 V.AddKill(CurrentStmt,DR);
202
203 VisitDeclRefExpr(DR);
204 }
205 else
206 Visit(S);
207
208 break;
209 }
210 break;
211
212 default:
213 VisitStmt(U->getSubExpr());
214 break;
215 }
216}
217
218LiveVariables::VarInfo& LivenessTFuncs::KillVar(Decl* D) {
219 LiveVariables::VarInfoMap::iterator I = L.getVarInfoMap().find(D);
220
221 assert (I != L.getVarInfoMap().end() &&
222 "Declaration not managed by variable map in LiveVariables");
223
224 // Mark the variable dead, and remove the current block from
225 // the set of blocks where the variable may be alive the entire time.
226 Live.reset(I->second.Idx);
227 I->second.V.AliveBlocks.reset(CurrentBlock->getBlockID());
228
229 return I->second.V;
230}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000231
232void LivenessTFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000233 if (Auditor)
234 Auditor->AuditStmt(B,L,Live);
235
236 // Check if we are assigning to a variable.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000237 Stmt* LHS = B->getLHS();
Ted Kremenek05334682007-09-06 21:26:58 +0000238
Ted Kremenekaa04c512007-09-06 00:17:54 +0000239 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000240 LiveVariables::VarInfo& V = KillVar(DR->getDecl());
241
242 // We only need to register kills once, so we check if this block
243 // has been previously processed.
244 if (!blockPreviouslyProcessed)
245 V.AddKill(CurrentStmt,DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000246 }
Ted Kremenek05334682007-09-06 21:26:58 +0000247 else
248 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000249
250 Visit(B->getRHS());
251}
252
Ted Kremenek05334682007-09-06 21:26:58 +0000253void LivenessTFuncs::VisitDeclStmt(DeclStmt* DS) {
254 if (Auditor)
255 Auditor->AuditStmt(DS,L,Live);
256
257 // Declarations effectively "kill" a variable since they cannot possibly
258 // be live before they are declared. Declarations, however, are not kills
259 // in the sense that the value is obliterated, so we do not register
260 // DeclStmts as a "kill site" for a variable.
261 for (Decl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
262 KillVar(D);
263}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000264
Ted Kremenek05334682007-09-06 21:26:58 +0000265llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000266 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
267
268 LiveVariables::BlockLivenessMap::iterator I = BMap.find(B);
269 return (I == BMap.end()) ? NULL : &(I->second);
270}
271
272bool LivenessTFuncs::ProcessBlock(const CFGBlock* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000273
274 CurrentBlock = B;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000275 Live.reset();
Ted Kremenek05334682007-09-06 21:26:58 +0000276 KilledAtLeastOnce.reset();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000277
Ted Kremenek05334682007-09-06 21:26:58 +0000278 // Check if this block has been previously processed.
279 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
280 LiveVariables::BlockLivenessMap::iterator BI = BMap.find(B);
281
282 blockPreviouslyProcessed = BI != BMap.end();
283
284 // Merge liveness information from all predecessors.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000285 for (CFGBlock::const_succ_iterator I=B->succ_begin(),E=B->succ_end();I!=E;++I)
Ted Kremenek05334682007-09-06 21:26:58 +0000286 if (llvm::BitVector* V = getBlockEntryLiveness(*I))
Ted Kremenekaa04c512007-09-06 00:17:54 +0000287 Live |= *V;
Ted Kremenek05334682007-09-06 21:26:58 +0000288
289 if (Auditor)
290 Auditor->AuditBlockExit(B,L,Live);
291
292 // Tentatively mark all variables alive at the end of the current block
293 // as being alive during the whole block. We then cull these out as
294 // we process the statements of this block.
295 for (LiveVariables::VarInfoMap::iterator
296 I=L.getVarInfoMap().begin(), E=L.getVarInfoMap().end(); I != E; ++I)
297 if (Live[I->second.Idx])
298 I->second.V.AliveBlocks.set(B->getBlockID());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000299
Ted Kremenek05334682007-09-06 21:26:58 +0000300 // March up the statements and process the transfer functions.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000301 for (CFGBlock::const_reverse_iterator I=B->rbegin(), E=B->rend(); I!=E; ++I) {
Ted Kremenek05334682007-09-06 21:26:58 +0000302 CurrentStmt = *I;
303 Visit(CurrentStmt);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000304 }
305
Ted Kremenek05334682007-09-06 21:26:58 +0000306 // Compare the computed "Live" values with what we already have
307 // for the entry to this block.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000308 bool hasChanged = false;
Ted Kremenek05334682007-09-06 21:26:58 +0000309
Ted Kremenekaa04c512007-09-06 00:17:54 +0000310
Ted Kremenek05334682007-09-06 21:26:58 +0000311 if (!blockPreviouslyProcessed) {
312 // We have not previously calculated liveness information for this block.
313 // Lazily instantiate a bitvector, and copy the bits from Live.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000314 hasChanged = true;
315 llvm::BitVector& V = BMap[B];
316 V.resize(L.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000317 V = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000318 }
Ted Kremenek05334682007-09-06 21:26:58 +0000319 else if (BI->second != Live) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000320 hasChanged = true;
Ted Kremenek05334682007-09-06 21:26:58 +0000321 BI->second = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000322 }
323
324 return hasChanged;
325}
326
327} // end anonymous namespace
328
329//===----------------------------------------------------------------------===//
330// runOnCFG - Method to run the actual liveness computation.
331//
332
Ted Kremenek05334682007-09-06 21:26:58 +0000333void LiveVariables::runOnCFG(const CFG& cfg, LiveVariablesAuditor* Auditor) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000334 // Scan a CFG for DeclRefStmts. For each one, create a VarInfo object.
335 {
336 RegisterDecls R(*this,cfg);
337 R.RegisterUsedDecls();
338 }
339
340 // Create the worklist and enqueue the exit block.
341 WorkListTy WorkList;
342 WorkList.enqueue(&cfg.getExit());
343
344 // Create the state for transfer functions.
Ted Kremenek05334682007-09-06 21:26:58 +0000345 LivenessTFuncs TF(*this,Auditor);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000346
347 // Process the worklist until it is empty.
348
349 while (!WorkList.isEmpty()) {
350 const CFGBlock* B = WorkList.dequeue();
351 if (TF.ProcessBlock(B))
352 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
353 I != E; ++I)
354 WorkList.enqueue(*I);
355 }
356
Ted Kremenek05334682007-09-06 21:26:58 +0000357 // Go through each block and reserve a bitvector. This is needed if
358 // a block was never visited by the worklist algorithm.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000359 for (CFG::const_iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
360 LiveAtBlockEntryMap[&(*I)].resize(NumDecls);
361}
362
Ted Kremenek05334682007-09-06 21:26:58 +0000363
364void LiveVariables::runOnBlock(const CFGBlock* B, LiveVariablesAuditor* Auditor)
365{
366 assert (NumDecls && "You must use runOnCFG before using runOnBlock.");
367 LivenessTFuncs TF(*this,Auditor);
368 TF.ProcessBlock(B);
369}
370
371//===----------------------------------------------------------------------===//
372// liveness queries
373//
374
375bool LiveVariables::IsLive(const CFGBlock* B, const Decl* D) const {
376 BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B);
377 assert (I != LiveAtBlockEntryMap.end());
378
379 VarInfoMap::const_iterator VI = VarInfos.find(D);
380 assert (VI != VarInfos.end());
381
382 return I->second[VI->second.Idx];
383}
384
385bool LiveVariables::KillsVar(const Stmt* S, const Decl* D) const {
386 VarInfoMap::const_iterator VI = VarInfos.find(D);
387 assert (VI != VarInfos.end());
388
389 for (VarInfo::KillsSet::const_iterator
390 I = VI->second.V.Kills.begin(), E = VI->second.V.Kills.end(); I!=E;++I)
391 if (I->first == S)
392 return true;
393
394 return false;
395}
396
397LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) {
398 VarInfoMap::iterator VI = VarInfos.find(D);
399 assert (VI != VarInfos.end());
400 return VI->second.V;
401}
402
403const LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) const {
404 return const_cast<LiveVariables*>(this)->getVarInfo(D);
405}
406
Ted Kremenekaa04c512007-09-06 00:17:54 +0000407//===----------------------------------------------------------------------===//
408// printing liveness state for debugging
409//
410
Ted Kremenek05334682007-09-06 21:26:58 +0000411void LiveVariables::dumpLiveness(const llvm::BitVector& V,
412 SourceManager& SM) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000413
414 for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) {
415 if (V[I->second.Idx]) {
Ted Kremenek05334682007-09-06 21:26:58 +0000416
417 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
418
419 fprintf(stderr, " %s <%s:%u:%u>\n",
420 I->first->getIdentifier()->getName(),
421 SM.getSourceName(PhysLoc),
422 SM.getLineNumber(PhysLoc),
423 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000424 }
425 }
426}
427
Ted Kremenek05334682007-09-06 21:26:58 +0000428void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000429 for (BlockLivenessMap::iterator I = LiveAtBlockEntryMap.begin(),
430 E = LiveAtBlockEntryMap.end();
431 I != E; ++I) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000432
Ted Kremenek05334682007-09-06 21:26:58 +0000433 fprintf(stderr,
434 "\n[ B%d (live variables at block entry) ]\n",
435 I->first->getBlockID());
436
437 dumpLiveness(I->second,M);
438 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000439}