blob: 8f391dc56188481b6d361ecc14275c6bc32fc0db [file] [log] [blame]
Ted Kremenek24c62442007-09-20 21:42:55 +00001//=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==//
Ted Kremenekb56a9902007-09-06 00:17:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenekad8bce02007-09-25 04:31:27 +00007// details.
Ted Kremenekb56a9902007-09-06 00:17:54 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements Live Variables analysis for source-level CFGs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbf593f82007-12-21 21:42:19 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek3f8ed262007-09-06 21:26:58 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/CFG.h"
Ted Kremenekad8bce02007-09-25 04:31:27 +000019#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek39fc60f2007-09-25 21:00:24 +000020#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek82ff6d62008-04-15 18:35:30 +000022#include "llvm/ADT/SmallVector.h"
Ted Kremenek96b1ce42008-01-08 18:19:08 +000023#include "llvm/Support/Compiler.h"
Ted Kremenekb56a9902007-09-06 00:17:54 +000024
Ted Kremenek3f8ed262007-09-06 21:26:58 +000025#include <string.h>
26#include <stdio.h>
Ted Kremenekb56a9902007-09-06 00:17:54 +000027
28using namespace clang;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek82ff6d62008-04-15 18:35:30 +000031// Useful constants.
32//===----------------------------------------------------------------------===//
33
34static const bool Alive = true;
35static const bool Dead = false;
36
37//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +000038// Dataflow initialization logic.
39//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +000040
41namespace {
Ted Kremenek96b1ce42008-01-08 18:19:08 +000042class VISIBILITY_HIDDEN RegisterDecls
43 : public CFGRecStmtDeclVisitor<RegisterDecls> {
44
Ted Kremenekad8bce02007-09-25 04:31:27 +000045 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek82ff6d62008-04-15 18:35:30 +000046
47 typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy;
48 AlwaysLiveTy AlwaysLive;
49
50
Ted Kremenek0064ff42007-09-28 20:38:59 +000051public:
Ted Kremenek82ff6d62008-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
Chris Lattner5696e7b2008-06-17 18:05:57 +000063 void VisitImplicitParamDecl(ImplicitParamDecl* IPD) {
64 // Register the VarDecl for tracking.
65 AD.Register(IPD);
66 }
67
Ted Kremenek82ff6d62008-04-15 18:35:30 +000068 void VisitVarDecl(VarDecl* VD) {
69 // Register the VarDecl for tracking.
70 AD.Register(VD);
71
72 // Does the variable have global storage? If so, it is always live.
73 if (VD->hasGlobalStorage())
74 AlwaysLive.push_back(VD);
75 }
76
77 void VisitUnaryOperator(UnaryOperator* U) {
78 // Check for '&'. Any VarDecl whose value has its address-taken we
79 // treat as always being live (flow-insensitive).
80
81 Expr* E = U->getSubExpr()->IgnoreParenCasts();
82
83 if (U->getOpcode() == UnaryOperator::AddrOf)
84 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
85 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
86 AD.Register(VD);
87 AlwaysLive.push_back(VD);
88 return;
89 }
90
91 Visit(E);
92 }
93
Ted Kremenek9d0acca2007-11-20 03:01:58 +000094 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenekfb4750b2007-10-01 20:33:52 +000095};
Ted Kremenekb56a9902007-09-06 00:17:54 +000096} // end anonymous namespace
97
Ted Kremenek8ff7705a2008-01-29 05:13:23 +000098
Ted Kremenek1fdd0a42008-03-13 16:55:07 +000099LiveVariables::LiveVariables(CFG& cfg) {
100 // Register all referenced VarDecls.
Ted Kremenek8ff7705a2008-01-29 05:13:23 +0000101 getAnalysisData().setCFG(&cfg);
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000102 RegisterDecls R(getAnalysisData());
Ted Kremenekad8bce02007-09-25 04:31:27 +0000103 cfg.VisitBlockStmts(R);
104}
Ted Kremenekb56a9902007-09-06 00:17:54 +0000105
106//===----------------------------------------------------------------------===//
Ted Kremenekad8bce02007-09-25 04:31:27 +0000107// Transfer functions.
108//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000109
110namespace {
Ted Kremenek0064ff42007-09-28 20:38:59 +0000111
Ted Kremenek96b1ce42008-01-08 18:19:08 +0000112class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{
Ted Kremenekad8bce02007-09-25 04:31:27 +0000113 LiveVariables::AnalysisDataTy& AD;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000114 LiveVariables::ValTy LiveState;
Ted Kremenekb56a9902007-09-06 00:17:54 +0000115public:
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000116 TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {}
Ted Kremenekad8bce02007-09-25 04:31:27 +0000117
Ted Kremenek0064ff42007-09-28 20:38:59 +0000118 LiveVariables::ValTy& getVal() { return LiveState; }
Ted Kremenek9d0acca2007-11-20 03:01:58 +0000119 CFG& getCFG() { return AD.getCFG(); }
Ted Kremenek1147e362007-09-12 19:10:52 +0000120
Ted Kremenekb56a9902007-09-06 00:17:54 +0000121 void VisitDeclRefExpr(DeclRefExpr* DR);
122 void VisitBinaryOperator(BinaryOperator* B);
123 void VisitAssign(BinaryOperator* B);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000124 void VisitDeclStmt(DeclStmt* DS);
125 void VisitUnaryOperator(UnaryOperator* U);
Ted Kremenek8adeebb2008-04-15 04:39:08 +0000126 void Visit(Stmt *S);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000127 void VisitTerminator(CFGBlock* B);
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000128
129 void SetTopValue(LiveVariables::ValTy& V) {
130 V = AD.AlwaysLive;
131 }
132
Ted Kremenekb56a9902007-09-06 00:17:54 +0000133};
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000134
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000135void TransferFuncs::Visit(Stmt *S) {
Ted Kremenek0064ff42007-09-28 20:38:59 +0000136
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000137 if (S == getCurrentBlkStmt()) {
Ted Kremenek80440462008-07-03 22:25:27 +0000138
139 if (AD.Observer)
140 AD.Observer->ObserveStmt(S,AD,LiveState);
141
Ted Kremenek0c046062008-03-05 19:26:46 +0000142 if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead;
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000143 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000144 }
Ted Kremenek80440462008-07-03 22:25:27 +0000145 else if (!getCFG().isBlkExpr(S)) {
146
147 if (AD.Observer)
148 AD.Observer->ObserveStmt(S,AD,LiveState);
149
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000150 StmtVisitor<TransferFuncs,void>::Visit(S);
Ted Kremenek80440462008-07-03 22:25:27 +0000151
152 }
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000153 else
154 // For block-level expressions, mark that they are live.
155 LiveState(S,AD) = Alive;
Ted Kremenekfb4750b2007-10-01 20:33:52 +0000156}
Ted Kremenek8adeebb2008-04-15 04:39:08 +0000157
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000158void TransferFuncs::VisitTerminator(CFGBlock* B) {
159
160 const Expr* E = B->getTerminatorCondition();
161
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000162 if (!E)
163 return;
164
Ted Kremeneked30e8d2008-04-16 17:07:59 +0000165 assert (getCFG().isBlkExpr(E));
166 LiveState(E, AD) = Alive;
Ted Kremenek8adeebb2008-04-15 04:39:08 +0000167}
168
Ted Kremenekad8bce02007-09-25 04:31:27 +0000169void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
170 if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
Ted Kremenek0064ff42007-09-28 20:38:59 +0000171 LiveState(V,AD) = Alive;
Ted Kremenekb56a9902007-09-06 00:17:54 +0000172}
Ted Kremenekb56a9902007-09-06 00:17:54 +0000173
Ted Kremenekad8bce02007-09-25 04:31:27 +0000174void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
Ted Kremenek1147e362007-09-12 19:10:52 +0000175 if (B->isAssignmentOp()) VisitAssign(B);
176 else VisitStmt(B);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000177}
178
Ted Kremenekad8bce02007-09-25 04:31:27 +0000179void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000180 Expr *E = U->getSubExpr();
Ted Kremenek14851c32007-09-28 21:29:33 +0000181
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000182 switch (U->getOpcode()) {
Ted Kremenek78dcda62007-12-13 04:47:15 +0000183 case UnaryOperator::SizeOf: return;
Ted Kremenekad8bce02007-09-25 04:31:27 +0000184 case UnaryOperator::PostInc:
185 case UnaryOperator::PostDec:
186 case UnaryOperator::PreInc:
187 case UnaryOperator::PreDec:
Ted Kremenekad8bce02007-09-25 04:31:27 +0000188 // Walk through the subexpressions, blasting through ParenExprs
189 // until we either find a DeclRefExpr or some non-DeclRefExpr
190 // expression.
Ted Kremenek20c91422008-02-22 00:34:10 +0000191 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
Ted Kremenekcd76f952008-04-15 04:08:54 +0000192 if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) {
193 // Treat the --/++ operator as a kill.
Ted Kremenek20c91422008-02-22 00:34:10 +0000194 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
Ted Kremenekcd76f952008-04-15 04:08:54 +0000195 LiveState(VD, AD) = Alive;
Ted Kremenek20c91422008-02-22 00:34:10 +0000196 return VisitDeclRefExpr(DR);
197 }
Ted Kremenek14851c32007-09-28 21:29:33 +0000198
199 // Fall-through.
Ted Kremenekad8bce02007-09-25 04:31:27 +0000200
201 default:
Ted Kremenekf1dae232008-01-17 17:50:49 +0000202 return Visit(E);
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000203 }
204}
Ted Kremenek14851c32007-09-28 21:29:33 +0000205
Ted Kremenekad8bce02007-09-25 04:31:27 +0000206void TransferFuncs::VisitAssign(BinaryOperator* B) {
Ted Kremenekf1dae232008-01-17 17:50:49 +0000207 Expr* LHS = B->getLHS();
Ted Kremenekad8bce02007-09-25 04:31:27 +0000208
Ted Kremenek14851c32007-09-28 21:29:33 +0000209 // Assigning to a variable?
Ted Kremenekf1dae232008-01-17 17:50:49 +0000210 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) {
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000211
212 // Update liveness inforamtion.
213 unsigned bit = AD.getIdx(DR->getDecl());
214 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
215
Ted Kremenek0064ff42007-09-28 20:38:59 +0000216 if (AD.Observer) { AD.Observer->ObserverKill(DR); }
217
Ted Kremenekad8bce02007-09-25 04:31:27 +0000218 // Handle things like +=, etc., which also generate "uses"
219 // of a variable. Do this just by visiting the subexpression.
Ted Kremenek14851c32007-09-28 21:29:33 +0000220 if (B->getOpcode() != BinaryOperator::Assign)
221 VisitDeclRefExpr(DR);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000222 }
Ted Kremenekad8bce02007-09-25 04:31:27 +0000223 else // Not assigning to a variable. Process LHS as usual.
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000224 Visit(LHS);
Ted Kremenekb56a9902007-09-06 00:17:54 +0000225
Ted Kremenekad8bce02007-09-25 04:31:27 +0000226 Visit(B->getRHS());
Ted Kremenekb56a9902007-09-06 00:17:54 +0000227}
228
Ted Kremenekad8bce02007-09-25 04:31:27 +0000229void TransferFuncs::VisitDeclStmt(DeclStmt* DS) {
230 // Declarations effectively "kill" a variable since they cannot
231 // possibly be live before they are declared.
Ted Kremenek7845b262008-02-25 22:28:54 +0000232 for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator())
233 if (VarDecl* VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek82ff6d62008-04-15 18:35:30 +0000234
235 // Update liveness information.
236 unsigned bit = AD.getIdx(VD);
237 LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit);
Ted Kremenek7845b262008-02-25 22:28:54 +0000238
Ted Kremeneka56c08a2008-02-07 02:38:55 +0000239 if (Expr* Init = VD->getInit())
240 Visit(Init);
Ted Kremenek7845b262008-02-25 22:28:54 +0000241 }
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000242}
Ted Kremenek0064ff42007-09-28 20:38:59 +0000243
Ted Kremenekb56a9902007-09-06 00:17:54 +0000244} // end anonymous namespace
245
Ted Kremenek0064ff42007-09-28 20:38:59 +0000246//===----------------------------------------------------------------------===//
247// Merge operator: if something is live on any successor block, it is live
248// in the current block (a set union).
249//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000250
Ted Kremenekad8bce02007-09-25 04:31:27 +0000251namespace {
Ted Kremenekb7151c72008-03-20 21:46:49 +0000252
253struct Merge {
254 typedef ExprDeclBitVector_Types::ValTy ValTy;
255
256 void operator()(ValTy& Dst, const ValTy& Src) {
257 Dst.OrDeclBits(Src);
258 Dst.AndExprBits(Src);
259 }
260};
261
262typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver;
Ted Kremenek0064ff42007-09-28 20:38:59 +0000263} // end anonymous namespace
264
265//===----------------------------------------------------------------------===//
266// External interface to run Liveness analysis.
267//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000268
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +0000269void LiveVariables::runOnCFG(CFG& cfg) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000270 Solver S(*this);
271 S.runOnCFG(cfg);
272}
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000273
Ted Kremenekad8bce02007-09-25 04:31:27 +0000274void LiveVariables::runOnAllBlocks(const CFG& cfg,
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000275 LiveVariables::ObserverTy* Obs,
276 bool recordStmtValues) {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000277 Solver S(*this);
278 ObserverTy* OldObserver = getAnalysisData().Observer;
Ted Kremenekb4b65e62008-01-17 18:25:22 +0000279 getAnalysisData().Observer = Obs;
280 S.runOnAllBlocks(cfg, recordStmtValues);
Ted Kremenekad8bce02007-09-25 04:31:27 +0000281 getAnalysisData().Observer = OldObserver;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000282}
283
284//===----------------------------------------------------------------------===//
285// liveness queries
286//
287
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000288bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000289 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
290 return i.isValid() ? getBlockData(B).getBit(i) : false;
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000291}
292
Ted Kremenekad8bce02007-09-25 04:31:27 +0000293bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const {
Ted Kremenek1fdd0a42008-03-13 16:55:07 +0000294 DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D);
295 return i.isValid() ? Live.getBit(i) : false;
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000296}
297
Ted Kremenek85be7cf2008-01-17 20:48:37 +0000298bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const {
299 return getStmtData(Loc)(StmtVal,getAnalysisData());
300}
301
Ted Kremenek05ecfdd2008-01-18 00:40:21 +0000302bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const {
303 return getStmtData(Loc)(D,getAnalysisData());
304}
305
Ted Kremenek6dc7b112007-09-06 23:00:42 +0000306//===----------------------------------------------------------------------===//
Ted Kremenekb56a9902007-09-06 00:17:54 +0000307// printing liveness state for debugging
308//
309
Ted Kremenekad8bce02007-09-25 04:31:27 +0000310void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const {
311 const AnalysisDataTy& AD = getAnalysisData();
312
Ted Kremenek0064ff42007-09-28 20:38:59 +0000313 for (AnalysisDataTy::decl_iterator I = AD.begin_decl(),
314 E = AD.end_decl(); I!=E; ++I)
315 if (V.getDeclBit(I->second)) {
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000316 SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
Ted Kremenekad8bce02007-09-25 04:31:27 +0000317
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000318 fprintf(stderr, " %s <%s:%u:%u>\n",
319 I->first->getIdentifier()->getName(),
320 SM.getSourceName(PhysLoc),
321 SM.getLineNumber(PhysLoc),
322 SM.getColumnNumber(PhysLoc));
Ted Kremenekb56a9902007-09-06 00:17:54 +0000323 }
Ted Kremenekb56a9902007-09-06 00:17:54 +0000324}
325
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000326void LiveVariables::dumpBlockLiveness(SourceManager& M) const {
Ted Kremenekad8bce02007-09-25 04:31:27 +0000327 for (BlockDataMapTy::iterator I = getBlockDataMap().begin(),
328 E = getBlockDataMap().end(); I!=E; ++I) {
329 fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n",
Ted Kremenek3f8ed262007-09-06 21:26:58 +0000330 I->first->getBlockID());
331
332 dumpLiveness(I->second,M);
333 }
Ted Kremenekbd9cc5c2007-09-10 17:36:42 +0000334
335 fprintf(stderr,"\n");
336}