blob: c0912b02272e5414b3dbaf56494377de810b5852 [file] [log] [blame]
Ted Kremenekcf6e41b2007-12-21 21:42:19 +00001#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekedb18632011-10-22 02:14:23 +00002#include "clang/Analysis/Analyses/PostOrderCFGView.h"
3
Ted Kremenek88299892011-07-28 23:07:59 +00004#include "clang/AST/Stmt.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +00005#include "clang/Analysis/CFG.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +00006#include "clang/Analysis/AnalysisContext.h"
Ted Kremenek88299892011-07-28 23:07:59 +00007#include "clang/AST/StmtVisitor.h"
8
Ted Kremenek87aa1252011-09-16 23:01:39 +00009#include "llvm/ADT/PostOrderIterator.h"
10#include "llvm/ADT/DenseMap.h"
11
Ted Kremenek88299892011-07-28 23:07:59 +000012#include <deque>
13#include <algorithm>
14#include <vector>
Ted Kremeneke4e63342007-09-06 00:17:54 +000015
16using namespace clang;
17
Ted Kremeneke4e63342007-09-06 00:17:54 +000018namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000019
Ted Kremenek87aa1252011-09-16 23:01:39 +000020class DataflowWorklist {
21 SmallVector<const CFGBlock *, 20> worklist;
22 llvm::BitVector enqueuedBlocks;
Ted Kremenekedb18632011-10-22 02:14:23 +000023 PostOrderCFGView *POV;
Ted Kremenek87aa1252011-09-16 23:01:39 +000024public:
Ted Kremenekedb18632011-10-22 02:14:23 +000025 DataflowWorklist(const CFG &cfg, AnalysisContext &Ctx)
Ted Kremenek87aa1252011-09-16 23:01:39 +000026 : enqueuedBlocks(cfg.getNumBlockIDs()),
Ted Kremenekedb18632011-10-22 02:14:23 +000027 POV(Ctx.getAnalysis<PostOrderCFGView>()) {}
Ted Kremenek87aa1252011-09-16 23:01:39 +000028
29 void enqueueBlock(const CFGBlock *block);
30 void enqueueSuccessors(const CFGBlock *block);
31 void enqueuePredecessors(const CFGBlock *block);
32
33 const CFGBlock *dequeue();
34
35 void sortWorklist();
36};
37
38}
39
40void 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
47void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
48 const unsigned OldWorklistSize = worklist.size();
49 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
50 E = block->succ_end(); I != E; ++I) {
51 enqueueBlock(*I);
52 }
53
54 if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
55 return;
56
57 sortWorklist();
58}
59
60void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) {
61 const unsigned OldWorklistSize = worklist.size();
62 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
63 E = block->pred_end(); I != E; ++I) {
64 enqueueBlock(*I);
65 }
66
67 if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
68 return;
69
70 sortWorklist();
71}
72
73void DataflowWorklist::sortWorklist() {
Ted Kremenekedb18632011-10-22 02:14:23 +000074 std::sort(worklist.begin(), worklist.end(), POV->getComparator());
Ted Kremenek87aa1252011-09-16 23:01:39 +000075}
76
Ted Kremenek87aa1252011-09-16 23:01:39 +000077const CFGBlock *DataflowWorklist::dequeue() {
78 if (worklist.empty())
79 return 0;
80 const CFGBlock *b = worklist.back();
81 worklist.pop_back();
82 enqueuedBlocks[b->getBlockID()] = false;
83 return b;
84}
85
86namespace {
87class LiveVariablesImpl {
88public:
89 AnalysisContext &analysisContext;
90 std::vector<LiveVariables::LivenessValues> cfgBlockValues;
91 llvm::ImmutableSet<const Stmt *>::Factory SSetFact;
92 llvm::ImmutableSet<const VarDecl *>::Factory DSetFact;
93 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness;
94 llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness;
95 llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness;
96 llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment;
97 const bool killAtAssign;
98
99 LiveVariables::LivenessValues
100 merge(LiveVariables::LivenessValues valsA,
101 LiveVariables::LivenessValues valsB);
102
103 LiveVariables::LivenessValues runOnBlock(const CFGBlock *block,
104 LiveVariables::LivenessValues val,
105 LiveVariables::Observer *obs = 0);
106
107 void dumpBlockLiveness(const SourceManager& M);
108
109 LiveVariablesImpl(AnalysisContext &ac, bool KillAtAssign)
Ted Kremenek3c2b5f72011-10-02 01:45:37 +0000110 : analysisContext(ac),
111 SSetFact(false), // Do not canonicalize ImmutableSets by default.
112 DSetFact(false), // This is a *major* performance win.
113 killAtAssign(KillAtAssign) {}
Ted Kremenek87aa1252011-09-16 23:01:39 +0000114};
Ted Kremenek88299892011-07-28 23:07:59 +0000115}
Ted Kremenek7deed0c2008-04-15 18:35:30 +0000116
Ted Kremenek9c378f72011-08-12 23:37:29 +0000117static LiveVariablesImpl &getImpl(void *x) {
Ted Kremenek88299892011-07-28 23:07:59 +0000118 return *((LiveVariablesImpl *) x);
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000119}
Ted Kremeneke4e63342007-09-06 00:17:54 +0000120
121//===----------------------------------------------------------------------===//
Ted Kremenek88299892011-07-28 23:07:59 +0000122// Operations and queries on LivenessValues.
123//===----------------------------------------------------------------------===//
124
125bool LiveVariables::LivenessValues::isLive(const Stmt *S) const {
126 return liveStmts.contains(S);
127}
128
129bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const {
130 return liveDecls.contains(D);
131}
132
133namespace {
134 template <typename SET>
Ted Kremenek87aa1252011-09-16 23:01:39 +0000135 SET mergeSets(SET A, SET B) {
136 if (A.isEmpty())
137 return B;
138
Ted Kremenek88299892011-07-28 23:07:59 +0000139 for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
Ted Kremenek87aa1252011-09-16 23:01:39 +0000140 A = A.add(*it);
Ted Kremenek88299892011-07-28 23:07:59 +0000141 }
142 return A;
143 }
144}
145
146LiveVariables::LivenessValues
147LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA,
148 LiveVariables::LivenessValues valsB) {
Ted Kremenek87aa1252011-09-16 23:01:39 +0000149
150 llvm::ImmutableSetRef<const Stmt *>
151 SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()),
152 SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory());
153
154
155 llvm::ImmutableSetRef<const VarDecl *>
156 DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()),
157 DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory());
158
159
160 SSetRefA = mergeSets(SSetRefA, SSetRefB);
161 DSetRefA = mergeSets(DSetRefA, DSetRefB);
162
Ted Kremenek3c2b5f72011-10-02 01:45:37 +0000163 // asImmutableSet() canonicalizes the tree, allowing us to do an easy
164 // comparison afterwards.
Ted Kremenek87aa1252011-09-16 23:01:39 +0000165 return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(),
166 DSetRefA.asImmutableSet());
Ted Kremenek88299892011-07-28 23:07:59 +0000167}
168
169bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const {
170 return liveStmts == V.liveStmts && liveDecls == V.liveDecls;
171}
172
173//===----------------------------------------------------------------------===//
174// Query methods.
175//===----------------------------------------------------------------------===//
176
177static bool isAlwaysAlive(const VarDecl *D) {
178 return D->hasGlobalStorage();
179}
180
181bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) {
182 return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D);
183}
184
185bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) {
186 return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D);
187}
188
189bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) {
190 return getImpl(impl).stmtsToLiveness[Loc].isLive(S);
191}
192
193//===----------------------------------------------------------------------===//
194// Dataflow computation.
Mike Stump1eb44332009-09-09 15:08:12 +0000195//===----------------------------------------------------------------------===//
Ted Kremeneke4e63342007-09-06 00:17:54 +0000196
197namespace {
Ted Kremenek88299892011-07-28 23:07:59 +0000198class TransferFunctions : public StmtVisitor<TransferFunctions> {
199 LiveVariablesImpl &LV;
200 LiveVariables::LivenessValues &val;
201 LiveVariables::Observer *observer;
Ted Kremenek848ec832011-02-11 23:24:26 +0000202 const CFGBlock *currentBlock;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000203public:
Ted Kremenek88299892011-07-28 23:07:59 +0000204 TransferFunctions(LiveVariablesImpl &im,
205 LiveVariables::LivenessValues &Val,
206 LiveVariables::Observer *Observer,
207 const CFGBlock *CurrentBlock)
208 : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {}
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000209
Ted Kremenek88299892011-07-28 23:07:59 +0000210 void VisitBinaryOperator(BinaryOperator *BO);
211 void VisitBlockExpr(BlockExpr *BE);
212 void VisitDeclRefExpr(DeclRefExpr *DR);
213 void VisitDeclStmt(DeclStmt *DS);
214 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS);
215 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE);
216 void VisitUnaryOperator(UnaryOperator *UO);
Mike Stump1eb44332009-09-09 15:08:12 +0000217 void Visit(Stmt *S);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000218};
Ted Kremenek11e72182007-10-01 20:33:52 +0000219}
Ted Kremenek88299892011-07-28 23:07:59 +0000220
Ted Kremenekf91a5b02011-08-06 00:30:00 +0000221static const VariableArrayType *FindVA(QualType Ty) {
222 const Type *ty = Ty.getTypePtr();
223 while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) {
224 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT))
225 if (VAT->getSizeExpr())
226 return VAT;
227
228 ty = VT->getElementType().getTypePtr();
229 }
230
231 return 0;
232}
233
Ted Kremenek88299892011-07-28 23:07:59 +0000234void TransferFunctions::Visit(Stmt *S) {
235 if (observer)
236 observer->observeStmt(S, currentBlock, val);
Ted Kremenekbfbcefb2009-12-24 02:40:30 +0000237
Ted Kremenek88299892011-07-28 23:07:59 +0000238 StmtVisitor<TransferFunctions>::Visit(S);
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000239
Ted Kremenek88299892011-07-28 23:07:59 +0000240 if (isa<Expr>(S)) {
241 val.liveStmts = LV.SSetFact.remove(val.liveStmts, S);
242 }
243
244 // Mark all children expressions live.
245
246 switch (S->getStmtClass()) {
247 default:
248 break;
249 case Stmt::StmtExprClass: {
250 // For statement expressions, look through the compound statement.
251 S = cast<StmtExpr>(S)->getSubStmt();
252 break;
253 }
254 case Stmt::CXXMemberCallExprClass: {
255 // Include the implicit "this" pointer as being live.
256 CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S);
Ted Kremenekc8085032011-10-06 20:53:28 +0000257 if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) {
258 ImplicitObj = ImplicitObj->IgnoreParens();
259 val.liveStmts = LV.SSetFact.add(val.liveStmts, ImplicitObj);
260 }
Ted Kremenek88299892011-07-28 23:07:59 +0000261 break;
262 }
Ted Kremenekf91a5b02011-08-06 00:30:00 +0000263 case Stmt::DeclStmtClass: {
264 const DeclStmt *DS = cast<DeclStmt>(S);
265 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) {
266 for (const VariableArrayType* VA = FindVA(VD->getType());
267 VA != 0; VA = FindVA(VA->getElementType())) {
268 val.liveStmts = LV.SSetFact.add(val.liveStmts,
269 VA->getSizeExpr()->IgnoreParens());
270 }
271 }
272 break;
273 }
Ted Kremenek88299892011-07-28 23:07:59 +0000274 // FIXME: These cases eventually shouldn't be needed.
275 case Stmt::ExprWithCleanupsClass: {
276 S = cast<ExprWithCleanups>(S)->getSubExpr();
277 break;
278 }
279 case Stmt::CXXBindTemporaryExprClass: {
280 S = cast<CXXBindTemporaryExpr>(S)->getSubExpr();
281 break;
282 }
Ted Kremenekf91a5b02011-08-06 00:30:00 +0000283 case Stmt::UnaryExprOrTypeTraitExprClass: {
284 // No need to unconditionally visit subexpressions.
285 return;
286 }
Ted Kremenek88299892011-07-28 23:07:59 +0000287 }
288
289 for (Stmt::child_iterator it = S->child_begin(), ei = S->child_end();
290 it != ei; ++it) {
291 if (Stmt *child = *it) {
292 if (Expr *Ex = dyn_cast<Expr>(child))
293 child = Ex->IgnoreParens();
294
295 val.liveStmts = LV.SSetFact.add(val.liveStmts, child);
296 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000297 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000298}
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek88299892011-07-28 23:07:59 +0000300void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) {
301 if (B->isAssignmentOp()) {
302 if (!LV.killAtAssign)
Ted Kremenek06529ae2008-11-14 21:07:14 +0000303 return;
Ted Kremenek88299892011-07-28 23:07:59 +0000304
305 // Assigning to a variable?
306 Expr *LHS = B->getLHS()->IgnoreParens();
307
Ted Kremenek9c378f72011-08-12 23:37:29 +0000308 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS))
Ted Kremenek88299892011-07-28 23:07:59 +0000309 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
310 // Assignments to references don't kill the ref's address
311 if (VD->getType()->isReferenceType())
312 return;
Ted Kremeneke97d9db2008-11-11 19:40:47 +0000313
Ted Kremenek88299892011-07-28 23:07:59 +0000314 if (!isAlwaysAlive(VD)) {
315 // The variable is now dead.
316 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
317 }
Ted Kremenek8f5aab62008-11-11 17:42:10 +0000318
Ted Kremenek88299892011-07-28 23:07:59 +0000319 if (observer)
320 observer->observerKill(DR);
Ted Kremenek56206312008-02-22 00:34:10 +0000321 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000322 }
323}
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek88299892011-07-28 23:07:59 +0000325void TransferFunctions::VisitBlockExpr(BlockExpr *BE) {
326 AnalysisContext::referenced_decls_iterator I, E;
327 llvm::tie(I, E) =
328 LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl());
329 for ( ; I != E ; ++I) {
330 const VarDecl *VD = *I;
331 if (isAlwaysAlive(VD))
332 continue;
333 val.liveDecls = LV.DSetFact.add(val.liveDecls, VD);
Ted Kremeneke4e63342007-09-06 00:17:54 +0000334 }
Ted Kremeneke4e63342007-09-06 00:17:54 +0000335}
336
Ted Kremenek88299892011-07-28 23:07:59 +0000337void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
338 if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl()))
339 if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end())
340 val.liveDecls = LV.DSetFact.add(val.liveDecls, D);
341}
342
343void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000344 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
345 DI != DE; ++DI)
Ted Kremenek9c378f72011-08-12 23:37:29 +0000346 if (VarDecl *VD = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek88299892011-07-28 23:07:59 +0000347 if (!isAlwaysAlive(VD))
348 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
Ted Kremenekdcc48102008-02-25 22:28:54 +0000349 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000350}
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek88299892011-07-28 23:07:59 +0000352void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) {
353 // Kill the iteration variable.
354 DeclRefExpr *DR = 0;
355 const VarDecl *VD = 0;
Ted Kremeneke4e63342007-09-06 00:17:54 +0000356
Ted Kremenek88299892011-07-28 23:07:59 +0000357 Stmt *element = OS->getElement();
358 if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) {
359 VD = cast<VarDecl>(DS->getSingleDecl());
360 }
361 else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) {
362 VD = cast<VarDecl>(DR->getDecl());
363 }
364
365 if (VD) {
366 val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
367 if (observer && DR)
368 observer->observerKill(DR);
369 }
Ted Kremenekfdd225e2007-09-25 04:31:27 +0000370}
Ted Kremenek27b07c52007-09-06 21:26:58 +0000371
Ted Kremenek88299892011-07-28 23:07:59 +0000372void TransferFunctions::
373VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE)
374{
375 // While sizeof(var) doesn't technically extend the liveness of 'var', it
376 // does extent the liveness of metadata if 'var' is a VariableArrayType.
377 // We handle that special case here.
378 if (UE->getKind() != UETT_SizeOf || UE->isArgumentType())
379 return;
380
Ted Kremenekf91a5b02011-08-06 00:30:00 +0000381 const Expr *subEx = UE->getArgumentExpr();
382 if (subEx->getType()->isVariableArrayType()) {
383 assert(subEx->isLValue());
384 val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens());
385 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000386}
387
Ted Kremenek88299892011-07-28 23:07:59 +0000388void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) {
389 // Treat ++/-- as a kill.
390 // Note we don't actually have to do anything if we don't have an observer,
391 // since a ++/-- acts as both a kill and a "use".
392 if (!observer)
393 return;
394
395 switch (UO->getOpcode()) {
396 default:
397 return;
398 case UO_PostInc:
399 case UO_PostDec:
400 case UO_PreInc:
401 case UO_PreDec:
402 break;
403 }
404
405 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens()))
406 if (isa<VarDecl>(DR->getDecl())) {
407 // Treat ++/-- as a kill.
408 observer->observerKill(DR);
409 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000410}
411
Ted Kremenek88299892011-07-28 23:07:59 +0000412LiveVariables::LivenessValues
413LiveVariablesImpl::runOnBlock(const CFGBlock *block,
414 LiveVariables::LivenessValues val,
415 LiveVariables::Observer *obs) {
416
417 TransferFunctions TF(*this, val, obs, block);
418
419 // Visit the terminator (if any).
420 if (const Stmt *term = block->getTerminator())
421 TF.Visit(const_cast<Stmt*>(term));
422
423 // Apply the transfer function for all Stmts in the block.
424 for (CFGBlock::const_reverse_iterator it = block->rbegin(),
425 ei = block->rend(); it != ei; ++it) {
426 const CFGElement &elem = *it;
427 if (!isa<CFGStmt>(elem))
428 continue;
429
430 const Stmt *S = cast<CFGStmt>(elem).getStmt();
431 TF.Visit(const_cast<Stmt*>(S));
432 stmtsToLiveness[S] = val;
433 }
434 return val;
Ted Kremenek055c2752007-09-06 23:00:42 +0000435}
436
Ted Kremenek88299892011-07-28 23:07:59 +0000437void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) {
438 const CFG *cfg = getImpl(impl).analysisContext.getCFG();
439 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it)
440 getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs);
Ted Kremenek86946742008-01-17 20:48:37 +0000441}
442
Ted Kremenek88299892011-07-28 23:07:59 +0000443LiveVariables::LiveVariables(void *im) : impl(im) {}
444
445LiveVariables::~LiveVariables() {
446 delete (LiveVariablesImpl*) impl;
Ted Kremenek2a9da9c2008-01-18 00:40:21 +0000447}
448
Ted Kremenek88299892011-07-28 23:07:59 +0000449LiveVariables *
450LiveVariables::computeLiveness(AnalysisContext &AC,
451 bool killAtAssign) {
Ted Kremeneke4e63342007-09-06 00:17:54 +0000452
Ted Kremenek88299892011-07-28 23:07:59 +0000453 // No CFG? Bail out.
454 CFG *cfg = AC.getCFG();
455 if (!cfg)
456 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek88299892011-07-28 23:07:59 +0000458 LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign);
459
460 // Construct the dataflow worklist. Enqueue the exit block as the
461 // start of the analysis.
Ted Kremenekedb18632011-10-22 02:14:23 +0000462 DataflowWorklist worklist(*cfg, AC);
Ted Kremenek88299892011-07-28 23:07:59 +0000463 llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs());
464
465 // FIXME: we should enqueue using post order.
466 for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) {
467 const CFGBlock *block = *it;
468 worklist.enqueueBlock(block);
469
470 // FIXME: Scan for DeclRefExprs using in the LHS of an assignment.
471 // We need to do this because we lack context in the reverse analysis
472 // to determine if a DeclRefExpr appears in such a context, and thus
473 // doesn't constitute a "use".
474 if (killAtAssign)
475 for (CFGBlock::const_iterator bi = block->begin(), be = block->end();
476 bi != be; ++bi) {
477 if (const CFGStmt *cs = bi->getAs<CFGStmt>()) {
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000478 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(cs->getStmt())) {
Ted Kremenek88299892011-07-28 23:07:59 +0000479 if (BO->getOpcode() == BO_Assign) {
480 if (const DeclRefExpr *DR =
481 dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) {
482 LV->inAssignment[DR] = 1;
483 }
484 }
485 }
486 }
487 }
488 }
489
Ted Kremenek87aa1252011-09-16 23:01:39 +0000490 worklist.sortWorklist();
491
492 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek88299892011-07-28 23:07:59 +0000493 // Determine if the block's end value has changed. If not, we
494 // have nothing left to do for this block.
495 LivenessValues &prevVal = LV->blocksEndToLiveness[block];
496
497 // Merge the values of all successor blocks.
498 LivenessValues val;
499 for (CFGBlock::const_succ_iterator it = block->succ_begin(),
500 ei = block->succ_end(); it != ei; ++it) {
Ted Kremenek87aa1252011-09-16 23:01:39 +0000501 if (const CFGBlock *succ = *it) {
Ted Kremenek88299892011-07-28 23:07:59 +0000502 val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
Ted Kremenek87aa1252011-09-16 23:01:39 +0000503 }
Ted Kremenek88299892011-07-28 23:07:59 +0000504 }
505
506 if (!everAnalyzedBlock[block->getBlockID()])
507 everAnalyzedBlock[block->getBlockID()] = true;
508 else if (prevVal.equals(val))
509 continue;
510
511 prevVal = val;
512
513 // Update the dataflow value for the start of this block.
514 LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val);
515
516 // Enqueue the value to the predecessors.
Ted Kremenek87aa1252011-09-16 23:01:39 +0000517 worklist.enqueuePredecessors(block);
Ted Kremenek88299892011-07-28 23:07:59 +0000518 }
519
520 return new LiveVariables(LV);
521}
522
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000523static bool compare_entries(const CFGBlock *A, const CFGBlock *B) {
Ted Kremenek88299892011-07-28 23:07:59 +0000524 return A->getBlockID() < B->getBlockID();
525}
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000526
527static bool compare_vd_entries(const Decl *A, const Decl *B) {
Ted Kremenek88299892011-07-28 23:07:59 +0000528 SourceLocation ALoc = A->getLocStart();
529 SourceLocation BLoc = B->getLocStart();
530 return ALoc.getRawEncoding() < BLoc.getRawEncoding();
531}
532
533void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
534 getImpl(impl).dumpBlockLiveness(M);
535}
536
537void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
538 std::vector<const CFGBlock *> vec;
539 for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator
540 it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end();
541 it != ei; ++it) {
542 vec.push_back(it->first);
543 }
544 std::sort(vec.begin(), vec.end(), compare_entries);
545
546 std::vector<const VarDecl*> declVec;
547
548 for (std::vector<const CFGBlock *>::iterator
549 it = vec.begin(), ei = vec.end(); it != ei; ++it) {
550 llvm::errs() << "\n[ B" << (*it)->getBlockID()
551 << " (live variables at block exit) ]\n";
552
553 LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
554 declVec.clear();
555
556 for (llvm::ImmutableSet<const VarDecl *>::iterator si =
557 vals.liveDecls.begin(),
558 se = vals.liveDecls.end(); si != se; ++si) {
559 declVec.push_back(*si);
560 }
561
562 std::sort(declVec.begin(), declVec.end(), compare_vd_entries);
563
564 for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
565 de = declVec.end(); di != de; ++di) {
566 llvm::errs() << " " << (*di)->getDeclName().getAsString()
567 << " <";
568 (*di)->getLocation().dump(M);
Daniel Dunbarc0d56722009-10-17 18:12:37 +0000569 llvm::errs() << ">\n";
Ted Kremeneke4e63342007-09-06 00:17:54 +0000570 }
Ted Kremenek27b07c52007-09-06 21:26:58 +0000571 }
Ted Kremenek88299892011-07-28 23:07:59 +0000572 llvm::errs() << "\n";
Ted Kremenekc0576ca2007-09-10 17:36:42 +0000573}
Ted Kremenek88299892011-07-28 23:07:59 +0000574
Ted Kremeneka5937bb2011-10-07 22:21:02 +0000575const void *LiveVariables::getTag() { static int x; return &x; }
576const void *RelaxedLiveVariables::getTag() { static int x; return &x; }