blob: 6b25f9eee60f9d6e4f3183d006f4bccd0eabdd70 [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);
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) {
68 LiveVariables::VPair& VP = L.getVarInfoMap()[const_cast<const Decl*>(D)];
69
70 VP.V.AliveBlocks.reserve(cfg.getNumBlockIDs());
71 VP.Idx = L.getNumDecls()++;
72}
73
74void RegisterDecls::RegisterUsedDecls() {
75 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI)
76 for (CFGBlock::const_iterator SI=BI->begin(),SE = BI->end();SI != SE;++SI)
77 Visit(const_cast<Stmt*>(*SI));
78}
79
80
81} // end anonymous namespace
82
83//===----------------------------------------------------------------------===//
84// WorkList - Data structure representing the liveness algorithm worklist.
85//
86
87namespace {
88
89class WorkListTy {
90 typedef llvm::SmallPtrSet<const CFGBlock*,20> BlockSet;
91 BlockSet wlist;
92public:
93 void enqueue(const CFGBlock* B) { wlist.insert(B); }
94
95 const CFGBlock* dequeue() {
96 assert (!wlist.empty());
97 const CFGBlock* B = *wlist.begin();
98 wlist.erase(B);
99 return B;
100 }
101
102 bool isEmpty() const { return wlist.empty(); }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// TFuncs
109//
110
111namespace {
112
113class LivenessTFuncs : public StmtVisitor<LivenessTFuncs,void> {
114 LiveVariables& L;
115 llvm::BitVector Live;
Ted Kremenek05334682007-09-06 21:26:58 +0000116 llvm::BitVector KilledAtLeastOnce;
117 Stmt* CurrentStmt;
118 const CFGBlock* CurrentBlock;
119 bool blockPreviouslyProcessed;
120 LiveVariablesAuditor* Auditor;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000121public:
Ted Kremenek05334682007-09-06 21:26:58 +0000122 LivenessTFuncs(LiveVariables& l, LiveVariablesAuditor* A = NULL)
123 : L(l), CurrentStmt(NULL), CurrentBlock(NULL),
124 blockPreviouslyProcessed(false), Auditor(A)
125 {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000126 Live.resize(l.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000127 KilledAtLeastOnce.resize(l.getNumDecls());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000128 }
129
130 void VisitStmt(Stmt* S);
131 void VisitDeclRefExpr(DeclRefExpr* DR);
132 void VisitBinaryOperator(BinaryOperator* B);
133 void VisitAssign(BinaryOperator* B);
134 void VisitStmtExpr(StmtExpr* S);
Ted Kremenek05334682007-09-06 21:26:58 +0000135 void VisitDeclStmt(DeclStmt* DS);
136 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000137
138 unsigned getIdx(const Decl* D) {
139 LiveVariables::VarInfoMap& V = L.getVarInfoMap();
140 LiveVariables::VarInfoMap::iterator I = V.find(D);
141 assert (I != V.end());
142 return I->second.Idx;
143 }
144
145 bool ProcessBlock(const CFGBlock* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000146 llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B);
147 LiveVariables::VarInfo& KillVar(Decl* D);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000148};
149
150void LivenessTFuncs::VisitStmt(Stmt* S) {
Ted Kremenek05334682007-09-06 21:26:58 +0000151 if (Auditor)
152 Auditor->AuditStmt(S,L,Live);
153
Ted Kremenekaa04c512007-09-06 00:17:54 +0000154 // Evaluate the transfer functions for all subexpressions. Note that
155 // each invocation of "Visit" will have a side-effect: "Liveness" and "Kills"
Ted Kremenek05334682007-09-06 21:26:58 +0000156 // will be updated.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000157 for (Stmt::child_iterator I = S->child_begin(),E = S->child_end(); I != E;++I)
158 Visit(*I);
159}
160
161void LivenessTFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
Ted Kremenek05334682007-09-06 21:26:58 +0000162 if (Auditor)
163 Auditor->AuditStmt(DR,L,Live);
164
Ted Kremenekaa04c512007-09-06 00:17:54 +0000165 // Register a use of the variable.
166 Live.set(getIdx(DR->getDecl()));
167}
168
169void LivenessTFuncs::VisitStmtExpr(StmtExpr* S) {
170 // Do nothing. The substatements of S are segmented into separate
171 // statements in the CFG.
172}
173
174void LivenessTFuncs::VisitBinaryOperator(BinaryOperator* B) {
175 switch (B->getOpcode()) {
176 case BinaryOperator::LAnd:
177 case BinaryOperator::LOr:
178 case BinaryOperator::Comma:
179 // Do nothing. These operations are broken up into multiple
180 // statements in the CFG. All these expressions do is return
Ted Kremenek05334682007-09-06 21:26:58 +0000181 // the value of their subexpressions, but these subexpressions will
Ted Kremenekaa04c512007-09-06 00:17:54 +0000182 // be evalualated elsewhere in the CFG.
183 break;
184
185 // FIXME: handle '++' and '--'
Ted Kremenek05334682007-09-06 21:26:58 +0000186 default:
Ted Kremenekaa04c512007-09-06 00:17:54 +0000187 if (B->isAssignmentOp()) VisitAssign(B);
Ted Kremenek05334682007-09-06 21:26:58 +0000188 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000189 }
190}
191
Ted Kremenek05334682007-09-06 21:26:58 +0000192void LivenessTFuncs::VisitUnaryOperator(UnaryOperator* U) {
193 switch (U->getOpcode()) {
194 case UnaryOperator::PostInc:
195 case UnaryOperator::PostDec:
196 case UnaryOperator::PreInc:
197 case UnaryOperator::PreDec:
198 case UnaryOperator::AddrOf:
199 // Walk through the subexpressions, blasting through ParenExprs until
200 // we either find a DeclRefExpr or some non-DeclRefExpr expression.
201 for (Stmt* S = U->getSubExpr() ; ; ) {
202 if (ParenExpr* P = dyn_cast<ParenExpr>(S)) {
203 S = P->getSubExpr();
204 continue;
205 }
206 else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
207 // Treat the --/++/& operator as a kill.
208 LiveVariables::VarInfo& V = KillVar(DR->getDecl());
209
210 if (!blockPreviouslyProcessed)
211 V.AddKill(CurrentStmt,DR);
212
213 VisitDeclRefExpr(DR);
214 }
215 else
216 Visit(S);
217
218 break;
219 }
220 break;
221
222 default:
223 VisitStmt(U->getSubExpr());
224 break;
225 }
226}
227
228LiveVariables::VarInfo& LivenessTFuncs::KillVar(Decl* D) {
229 LiveVariables::VarInfoMap::iterator I = L.getVarInfoMap().find(D);
230
231 assert (I != L.getVarInfoMap().end() &&
232 "Declaration not managed by variable map in LiveVariables");
233
234 // Mark the variable dead, and remove the current block from
235 // the set of blocks where the variable may be alive the entire time.
236 Live.reset(I->second.Idx);
237 I->second.V.AliveBlocks.reset(CurrentBlock->getBlockID());
238
239 return I->second.V;
240}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000241
242void LivenessTFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000243 if (Auditor)
244 Auditor->AuditStmt(B,L,Live);
245
246 // Check if we are assigning to a variable.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000247 Stmt* LHS = B->getLHS();
Ted Kremenek05334682007-09-06 21:26:58 +0000248
Ted Kremenekaa04c512007-09-06 00:17:54 +0000249 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000250 LiveVariables::VarInfo& V = KillVar(DR->getDecl());
251
252 // We only need to register kills once, so we check if this block
253 // has been previously processed.
254 if (!blockPreviouslyProcessed)
255 V.AddKill(CurrentStmt,DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000256 }
Ted Kremenek05334682007-09-06 21:26:58 +0000257 else
258 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000259
260 Visit(B->getRHS());
261}
262
Ted Kremenek05334682007-09-06 21:26:58 +0000263void LivenessTFuncs::VisitDeclStmt(DeclStmt* DS) {
264 if (Auditor)
265 Auditor->AuditStmt(DS,L,Live);
266
267 // Declarations effectively "kill" a variable since they cannot possibly
268 // be live before they are declared. Declarations, however, are not kills
269 // in the sense that the value is obliterated, so we do not register
270 // DeclStmts as a "kill site" for a variable.
271 for (Decl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
272 KillVar(D);
273}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000274
Ted Kremenek05334682007-09-06 21:26:58 +0000275llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000276 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
277
278 LiveVariables::BlockLivenessMap::iterator I = BMap.find(B);
279 return (I == BMap.end()) ? NULL : &(I->second);
280}
281
282bool LivenessTFuncs::ProcessBlock(const CFGBlock* B) {
Ted Kremenek05334682007-09-06 21:26:58 +0000283
284 CurrentBlock = B;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000285 Live.reset();
Ted Kremenek05334682007-09-06 21:26:58 +0000286 KilledAtLeastOnce.reset();
Ted Kremenekaa04c512007-09-06 00:17:54 +0000287
Ted Kremenek05334682007-09-06 21:26:58 +0000288 // Check if this block has been previously processed.
289 LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap();
290 LiveVariables::BlockLivenessMap::iterator BI = BMap.find(B);
291
292 blockPreviouslyProcessed = BI != BMap.end();
293
294 // Merge liveness information from all predecessors.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000295 for (CFGBlock::const_succ_iterator I=B->succ_begin(),E=B->succ_end();I!=E;++I)
Ted Kremenek05334682007-09-06 21:26:58 +0000296 if (llvm::BitVector* V = getBlockEntryLiveness(*I))
Ted Kremenekaa04c512007-09-06 00:17:54 +0000297 Live |= *V;
Ted Kremenek05334682007-09-06 21:26:58 +0000298
299 if (Auditor)
300 Auditor->AuditBlockExit(B,L,Live);
301
302 // Tentatively mark all variables alive at the end of the current block
303 // as being alive during the whole block. We then cull these out as
304 // we process the statements of this block.
305 for (LiveVariables::VarInfoMap::iterator
306 I=L.getVarInfoMap().begin(), E=L.getVarInfoMap().end(); I != E; ++I)
307 if (Live[I->second.Idx])
308 I->second.V.AliveBlocks.set(B->getBlockID());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000309
Ted Kremenek05334682007-09-06 21:26:58 +0000310 // March up the statements and process the transfer functions.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000311 for (CFGBlock::const_reverse_iterator I=B->rbegin(), E=B->rend(); I!=E; ++I) {
Ted Kremenek05334682007-09-06 21:26:58 +0000312 CurrentStmt = *I;
313 Visit(CurrentStmt);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000314 }
315
Ted Kremenek05334682007-09-06 21:26:58 +0000316 // Compare the computed "Live" values with what we already have
317 // for the entry to this block.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000318 bool hasChanged = false;
Ted Kremenek05334682007-09-06 21:26:58 +0000319
Ted Kremenekaa04c512007-09-06 00:17:54 +0000320
Ted Kremenek05334682007-09-06 21:26:58 +0000321 if (!blockPreviouslyProcessed) {
322 // We have not previously calculated liveness information for this block.
323 // Lazily instantiate a bitvector, and copy the bits from Live.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000324 hasChanged = true;
325 llvm::BitVector& V = BMap[B];
326 V.resize(L.getNumDecls());
Ted Kremenek05334682007-09-06 21:26:58 +0000327 V = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000328 }
Ted Kremenek05334682007-09-06 21:26:58 +0000329 else if (BI->second != Live) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000330 hasChanged = true;
Ted Kremenek05334682007-09-06 21:26:58 +0000331 BI->second = Live;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000332 }
333
334 return hasChanged;
335}
336
337} // end anonymous namespace
338
339//===----------------------------------------------------------------------===//
340// runOnCFG - Method to run the actual liveness computation.
341//
342
Ted Kremenek05334682007-09-06 21:26:58 +0000343void LiveVariables::runOnCFG(const CFG& cfg, LiveVariablesAuditor* Auditor) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000344 // Scan a CFG for DeclRefStmts. For each one, create a VarInfo object.
345 {
346 RegisterDecls R(*this,cfg);
347 R.RegisterUsedDecls();
348 }
349
350 // Create the worklist and enqueue the exit block.
351 WorkListTy WorkList;
352 WorkList.enqueue(&cfg.getExit());
353
354 // Create the state for transfer functions.
Ted Kremenek05334682007-09-06 21:26:58 +0000355 LivenessTFuncs TF(*this,Auditor);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000356
357 // Process the worklist until it is empty.
358
359 while (!WorkList.isEmpty()) {
360 const CFGBlock* B = WorkList.dequeue();
361 if (TF.ProcessBlock(B))
362 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
363 I != E; ++I)
364 WorkList.enqueue(*I);
365 }
366
Ted Kremenek05334682007-09-06 21:26:58 +0000367 // Go through each block and reserve a bitvector. This is needed if
368 // a block was never visited by the worklist algorithm.
Ted Kremenekaa04c512007-09-06 00:17:54 +0000369 for (CFG::const_iterator I = cfg.begin(), E = cfg.end(); I != E; ++I)
370 LiveAtBlockEntryMap[&(*I)].resize(NumDecls);
371}
372
Ted Kremenek05334682007-09-06 21:26:58 +0000373
374void LiveVariables::runOnBlock(const CFGBlock* B, LiveVariablesAuditor* Auditor)
375{
Ted Kremenek05334682007-09-06 21:26:58 +0000376 LivenessTFuncs TF(*this,Auditor);
377 TF.ProcessBlock(B);
378}
379
380//===----------------------------------------------------------------------===//
381// liveness queries
382//
383
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000384bool LiveVariables::isLive(const CFGBlock* B, const Decl* D) const {
Ted Kremenek05334682007-09-06 21:26:58 +0000385 BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B);
386 assert (I != LiveAtBlockEntryMap.end());
387
388 VarInfoMap::const_iterator VI = VarInfos.find(D);
389 assert (VI != VarInfos.end());
390
391 return I->second[VI->second.Idx];
392}
393
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000394bool LiveVariables::isLive(llvm::BitVector& Live, const Decl* D) const {
395 VarInfoMap::const_iterator VI = VarInfos.find(D);
396 assert (VI != VarInfos.end());
397 return Live[VI->second.Idx];
398}
399
Ted Kremenek05334682007-09-06 21:26:58 +0000400bool LiveVariables::KillsVar(const Stmt* S, const Decl* D) const {
401 VarInfoMap::const_iterator VI = VarInfos.find(D);
402 assert (VI != VarInfos.end());
403
404 for (VarInfo::KillsSet::const_iterator
405 I = VI->second.V.Kills.begin(), E = VI->second.V.Kills.end(); I!=E;++I)
406 if (I->first == S)
407 return true;
408
409 return false;
410}
411
412LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) {
413 VarInfoMap::iterator VI = VarInfos.find(D);
414 assert (VI != VarInfos.end());
415 return VI->second.V;
416}
417
418const LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) const {
419 return const_cast<LiveVariables*>(this)->getVarInfo(D);
420}
421
Ted Kremenekaa04c512007-09-06 00:17:54 +0000422//===----------------------------------------------------------------------===//
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000423// Defaults for LiveVariablesAuditor
424
425void LiveVariablesAuditor::AuditStmt(Stmt* S, LiveVariables& L,
426 llvm::BitVector& V) {}
427
428void LiveVariablesAuditor::AuditBlockExit(const CFGBlock* B, LiveVariables& L,
429 llvm::BitVector& V) {}
430
431//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000432// printing liveness state for debugging
433//
434
Ted Kremenek05334682007-09-06 21:26:58 +0000435void LiveVariables::dumpLiveness(const llvm::BitVector& V,
436 SourceManager& SM) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000437
438 for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) {
439 if (V[I->second.Idx]) {
Ted Kremenek05334682007-09-06 21:26:58 +0000440
441 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
442
443 fprintf(stderr, " %s <%s:%u:%u>\n",
444 I->first->getIdentifier()->getName(),
445 SM.getSourceName(PhysLoc),
446 SM.getLineNumber(PhysLoc),
447 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000448 }
449 }
450}
451
Ted Kremenek05334682007-09-06 21:26:58 +0000452void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000453 for (BlockLivenessMap::iterator I = LiveAtBlockEntryMap.begin(),
454 E = LiveAtBlockEntryMap.end();
455 I != E; ++I) {
Ted Kremenekaa04c512007-09-06 00:17:54 +0000456
Ted Kremenek05334682007-09-06 21:26:58 +0000457 fprintf(stderr,
458 "\n[ B%d (live variables at block entry) ]\n",
459 I->first->getBlockID());
460
461 dumpLiveness(I->second,M);
462 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000463}