blob: feba80d695560b87b20f1c03fdc53e2858343ca3 [file] [log] [blame]
Ted Kremenek26e47462007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremenekaa04c512007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekd7a2f812007-09-25 04:31:27 +00007// details.
Ted Kremenekaa04c512007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekcdf8e842007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek05334682007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekd7a2f812007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek10d80462007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek9ea943f2008-04-15 18:35:30 +000022#include "llvm/ADT/SmallVector.h"
Ted Kremenekec818352008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremenekaa04c512007-09-06 00:17:54 +000024
Ted Kremenek05334682007-09-06 21:26:58 +000025#include <string.h>
26#include <stdio.h>
Ted Kremenekaa04c512007-09-06 00:17:54 +000027
28using namespace clang;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek9ea943f2008-04-15 18:35:30 +000031// Useful constants.
32//===----------------------------------------------------------------------===//
33
34static const bool Alive = true;
35static const bool Dead = false;
36
37//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +000038// Dataflow initialization logic.
39//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +000040
41namespace {
Ted Kremenekec818352008-01-08 18:19:08 +000042class VISIBILITY_HIDDEN RegisterDecls
43 : public CFGRecStmtDeclVisitor<RegisterDecls> {
44
Ted Kremenekd7a2f812007-09-25 04:31:27 +000045 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek9ea943f2008-04-15 18:35:30 +000046
47 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
48 AlwaysLiveTy AlwaysLive;
49
50
Ted Kremenekeebf1312007-09-28 20:38:59 +000051public:
Ted Kremenek9ea943f2008-04-15 18:35:30 +000052 RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
53
54 ~RegisterDecls() {
55
56 AD.AlwaysLive.resetValues(AD);
57
58 for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end();
59 I != E; ++ I)
60 AD.AlwaysLive(*I, AD) = Alive;
61 }
62
63 void VisitVarDecl(VarDecl* VD) {
64 // Register the VarDecl for tracking.
65 AD.Register(VD);
66
67 // Does the variable have global storage? If so, it is always live.
68 if (VD->hasGlobalStorage())
69 AlwaysLive.push_back(VD);
70 }
71
72 void VisitUnaryOperator(UnaryOperator* U) {
73 // Check for '&'. Any VarDecl whose value has its address-taken we
74 // treat as always being live (flow-insensitive).
75
76 Expr* E = U->getSubExpr()->IgnoreParenCasts();
77
78 if (U->getOpcode() == UnaryOperator::AddrOf)
79 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
80 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
81 AD.Register(VD);
82 AlwaysLive.push_back(VD);
83 return;
84 }
85
86 Visit(E);
87 }
88
Ted Kremenek705386b2007-11-20 03:01:58 +000089 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek8ce772b2007-10-01 20:33:52 +000090};
Ted Kremenekaa04c512007-09-06 00:17:54 +000091} // end anonymous namespace
92
Ted Kremenekd03aece2008-01-29 05:13:23 +000093
Ted Kremenekf41ac5f2008-03-13 16:55:07 +000094LiveVariables::LiveVariables(CFG& cfg) {
95 // Register all referenced VarDecls.
Ted Kremenekd03aece2008-01-29 05:13:23 +000096 getAnalysisData().setCFG(&cfg);
Ted Kremenek8ce772b2007-10-01 20:33:52 +000097 RegisterDecls R(getAnalysisData());
Ted Kremenekd7a2f812007-09-25 04:31:27 +000098 cfg.VisitBlockStmts(R);
99}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000100
101//===----------------------------------------------------------------------===//
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000102// Transfer functions.
103//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000104
105namespace {
Ted Kremenekeebf1312007-09-28 20:38:59 +0000106
Ted Kremenekec818352008-01-08 18:19:08 +0000107class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000108 LiveVariables::AnalysisDataTy& AD;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000109 LiveVariables::ValTy LiveState;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000110public:
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000111 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000112
Ted Kremenekeebf1312007-09-28 20:38:59 +0000113 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek705386b2007-11-20 03:01:58 +0000114 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek00b63272007-09-12 19:10:52 +0000115
Ted Kremenekaa04c512007-09-06 00:17:54 +0000116 void VisitDeclRefExpr(DeclRefExpr* DR);
117 void VisitBinaryOperator(BinaryOperator* B);
118 void VisitAssign(BinaryOperator* B);
Ted Kremenek05334682007-09-06 21:26:58 +0000119 void VisitDeclStmt(DeclStmt* DS);
120 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000121 void Visit(Stmt *S);
122 void VisitTerminator(Stmt* S);
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000123
124 void SetTopValue(LiveVariables::ValTy& V) {
125 V = AD.AlwaysLive;
126 }
127
Ted Kremenekaa04c512007-09-06 00:17:54 +0000128};
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000129
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000130void TransferFuncs::Visit(Stmt *S) {
131 if (AD.Observer)
132 AD.Observer->ObserveStmt(S,AD,LiveState);
Ted Kremenekeebf1312007-09-28 20:38:59 +0000133
Ted Kremenekab6c5902008-01-17 20:48:37 +0000134 if (S == getCurrentBlkStmt()) {
Ted Kremenek925b3a92008-03-05 19:26:46 +0000135 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek1796e132008-01-18 00:40:21 +0000136 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenekab6c5902008-01-17 20:48:37 +0000137 }
138 else if (!getCFG().isBlkExpr(S))
139 StmtVisitor<TransferFuncs,void>::Visit(S);
140 else
141 // For block-level expressions, mark that they are live.
142 LiveState(S,AD) = Alive;
Ted Kremenek8ce772b2007-10-01 20:33:52 +0000143}
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000144
145void TransferFuncs::VisitTerminator(Stmt* S) {
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000146
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000147 Expr* E = NULL;
148
149 switch (S->getStmtClass()) {
150 default:
151 return;
152
153 case Stmt::ForStmtClass:
154 E = cast<ForStmt>(S)->getCond();
155 break;
156
157 case Stmt::WhileStmtClass:
158 E = cast<WhileStmt>(S)->getCond();
159 break;
160
161 case Stmt::DoStmtClass:
162 E = cast<DoStmt>(S)->getCond();
163 break;
164
165 case Stmt::IfStmtClass:
166 E = cast<IfStmt>(S)->getCond();
167 break;
168
169 case Stmt::ChooseExprClass:
170 E = cast<ChooseExpr>(S)->getCond();
171 break;
172
173 case Stmt::IndirectGotoStmtClass:
174 E = cast<IndirectGotoStmt>(S)->getTarget();
175 break;
176
177 case Stmt::SwitchStmtClass:
178 E = cast<SwitchStmt>(S)->getCond();
179 break;
180
181 case Stmt::ConditionalOperatorClass:
182 E = cast<ConditionalOperator>(S)->getCond();
183 break;
184
185 case Stmt::BinaryOperatorClass: // '&&' and '||'
186 E = cast<BinaryOperator>(S)->getLHS();
187 break;
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000188 }
Ted Kremenek5ae4e832008-04-16 17:07:59 +0000189
190 if (!E)
191 return;
192
193 E = E->IgnoreParens();
194
195 assert (getCFG().isBlkExpr(E));
196 LiveState(E, AD) = Alive;
Ted Kremeneka0aa0b12008-04-15 04:39:08 +0000197}
198
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000199void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
200 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenekeebf1312007-09-28 20:38:59 +0000201 LiveState(V,AD) = Alive;
Ted Kremenekaa04c512007-09-06 00:17:54 +0000202}
Ted Kremenekaa04c512007-09-06 00:17:54 +0000203
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000204void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek00b63272007-09-12 19:10:52 +0000205 if (B->isAssignmentOp()) VisitAssign(B);
206 else VisitStmt(B);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000207}
208
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000209void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000210 Expr *E = U->getSubExpr();
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000211
Ted Kremenek05334682007-09-06 21:26:58 +0000212 switch (U->getOpcode()) {
Ted Kremeneke33d1002007-12-13 04:47:15 +0000213 case UnaryOperator::SizeOf: return;
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000214 case UnaryOperator::PostInc:
215 case UnaryOperator::PostDec:
216 case UnaryOperator::PreInc:
217 case UnaryOperator::PreDec:
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000218 // Walk through the subexpressions, blasting through ParenExprs
219 // until we either find a DeclRefExpr or some non-DeclRefExpr
220 // expression.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000221 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenek96715132008-04-15 04:08:54 +0000222 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
223 // Treat the --/++ operator as a kill.
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000224 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenek96715132008-04-15 04:08:54 +0000225 LiveState(VD, AD) = Alive;
Ted Kremenek2c3041c2008-02-22 00:34:10 +0000226 return VisitDeclRefExpr(DR);
227 }
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000228
229 // Fall-through.
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000230
231 default:
Ted Kremenek133befa2008-01-17 17:50:49 +0000232 return Visit(E);
Ted Kremenek05334682007-09-06 21:26:58 +0000233 }
234}
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000235
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000236void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenek133befa2008-01-17 17:50:49 +0000237 Expr* LHS = B->getLHS();
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000238
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000239 // Assigning to a variable?
Ted Kremenek133befa2008-01-17 17:50:49 +0000240 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000241
242 // Update liveness inforamtion.
243 unsigned bit = AD.getIdx(DR->getDecl());
244 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
245
Ted Kremenekeebf1312007-09-28 20:38:59 +0000246 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
247
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000248 // Handle things like +=, etc., which also generate "uses"
249 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek83a6ba12007-09-28 21:29:33 +0000250 if (B->getOpcode() != BinaryOperator::Assign)
251 VisitDeclRefExpr(DR);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000252 }
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000253 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek05334682007-09-06 21:26:58 +0000254 Visit(LHS);
Ted Kremenekaa04c512007-09-06 00:17:54 +0000255
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000256 Visit(B->getRHS());
Ted Kremenekaa04c512007-09-06 00:17:54 +0000257}
258
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000259void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
260 // Declarations effectively "kill" a variable since they cannot
261 // possibly be live before they are declared.
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000262 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
263 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek9ea943f2008-04-15 18:35:30 +0000264
265 // Update liveness information.
266 unsigned bit = AD.getIdx(VD);
267 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000268
Ted Kremenek0c5fe982008-02-07 02:38:55 +0000269 if (Expr* Init = VD->getInit())
270 Visit(Init);
Ted Kremenek43c51bd2008-02-25 22:28:54 +0000271 }
Ted Kremenek05334682007-09-06 21:26:58 +0000272}
Ted Kremenekeebf1312007-09-28 20:38:59 +0000273
Ted Kremenekaa04c512007-09-06 00:17:54 +0000274} // end anonymous namespace
275
Ted Kremenekeebf1312007-09-28 20:38:59 +0000276//===----------------------------------------------------------------------===//
277// Merge operator: if something is live on any successor block, it is live
278// in the current block (a set union).
279//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000280
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000281namespace {
Ted Kremenek9b9f2b92008-03-20 21:46:49 +0000282
283struct Merge {
284 typedef ExprDeclBitVector_Types::ValTy ValTy;
285
286 void operator()(ValTy& Dst, const ValTy& Src) {
287 Dst.OrDeclBits(Src);
288 Dst.AndExprBits(Src);
289 }
290};
291
292typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenekeebf1312007-09-28 20:38:59 +0000293} // end anonymous namespace
294
295//===----------------------------------------------------------------------===//
296// External interface to run Liveness analysis.
297//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000298
Ted Kremenek5ee98a72008-01-11 00:40:29 +0000299void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000300 Solver S(*this);
301 S.runOnCFG(cfg);
302}
Ted Kremenek05334682007-09-06 21:26:58 +0000303
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000304void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekab8ed952008-01-17 18:25:22 +0000305 LiveVariables::ObserverTy* Obs,
306 bool recordStmtValues) {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000307 Solver S(*this);
308 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekab8ed952008-01-17 18:25:22 +0000309 getAnalysisData().Observer = Obs;
310 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000311 getAnalysisData().Observer = OldObserver;
Ted Kremenek05334682007-09-06 21:26:58 +0000312}
313
314//===----------------------------------------------------------------------===//
315// liveness queries
316//
317
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000318bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000319 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
320 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek05334682007-09-06 21:26:58 +0000321}
322
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000323bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenekf41ac5f2008-03-13 16:55:07 +0000324 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
325 return i.isValid() ? Live.getBit(i) : false;
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000326}
327
Ted Kremenekab6c5902008-01-17 20:48:37 +0000328bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
329 return getStmtData(Loc)(StmtVal,getAnalysisData());
330}
331
Ted Kremenek1796e132008-01-18 00:40:21 +0000332bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
333 return getStmtData(Loc)(D,getAnalysisData());
334}
335
Ted Kremeneke805c4a2007-09-06 23:00:42 +0000336//===----------------------------------------------------------------------===//
Ted Kremenekaa04c512007-09-06 00:17:54 +0000337// printing liveness state for debugging
338//
339
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000340void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
341 const AnalysisDataTy& AD = getAnalysisData();
342
Ted Kremenekeebf1312007-09-28 20:38:59 +0000343 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
344 E = AD.end_decl(); I!=E; ++I)
345 if (V.getDeclBit(I->second)) {
Ted Kremenek05334682007-09-06 21:26:58 +0000346 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000347
Ted Kremenek05334682007-09-06 21:26:58 +0000348 fprintf(stderr, " %s <%s:%u:%u>\n",
349 I->first->getIdentifier()->getName(),
350 SM.getSourceName(PhysLoc),
351 SM.getLineNumber(PhysLoc),
352 SM.getColumnNumber(PhysLoc));
Ted Kremenekaa04c512007-09-06 00:17:54 +0000353 }
Ted Kremenekaa04c512007-09-06 00:17:54 +0000354}
355
Ted Kremenek05334682007-09-06 21:26:58 +0000356void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekd7a2f812007-09-25 04:31:27 +0000357 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
358 E = getBlockDataMap().end(); I!=E; ++I) {
359 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek05334682007-09-06 21:26:58 +0000360 I->first->getBlockID());
361
362 dumpLiveness(I->second,M);
363 }
Ted Kremenek6b2b4e32007-09-10 17:36:42 +0000364
365 fprintf(stderr,"\n");
366}