Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 1 | //==- 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 Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/CFG.h" |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/DataflowStmtVisitor.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 19 | #include "clang/Lex/IdentifierTable.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <stdio.h> |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // RegisterDecls - Utility class to create VarInfo objects for all |
| 29 | // Decls referenced in a function. |
| 30 | // |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class RegisterDecls : public StmtVisitor<RegisterDecls,void> { |
| 35 | LiveVariables& L; |
| 36 | const CFG& cfg; |
| 37 | public: |
| 38 | RegisterDecls(LiveVariables& l, const CFG& c) |
| 39 | : L(l), cfg(c) {} |
| 40 | |
| 41 | void VisitStmt(Stmt* S); |
| 42 | void VisitDeclRefExpr(DeclRefExpr* DR); |
Ted Kremenek | e2ca142 | 2007-09-06 23:25:10 +0000 | [diff] [blame] | 43 | void VisitDeclStmt(DeclStmt* DS); |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame^] | 44 | void Register(ScopedDecl* D); |
| 45 | void RegisterDeclChain(ScopedDecl* D); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 46 | void RegisterUsedDecls(); |
| 47 | }; |
| 48 | |
| 49 | void 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 | |
| 54 | void RegisterDecls::VisitDeclRefExpr(DeclRefExpr* DR) { |
Ted Kremenek | e2ca142 | 2007-09-06 23:25:10 +0000 | [diff] [blame] | 55 | RegisterDeclChain(DR->getDecl()); |
| 56 | } |
| 57 | |
| 58 | void RegisterDecls::VisitDeclStmt(DeclStmt* DS) { |
| 59 | RegisterDeclChain(DS->getDecl()); |
| 60 | } |
| 61 | |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame^] | 62 | void RegisterDecls::RegisterDeclChain(ScopedDecl* D) { |
Ted Kremenek | e2ca142 | 2007-09-06 23:25:10 +0000 | [diff] [blame] | 63 | for (; D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 64 | Register(D); |
| 65 | } |
| 66 | |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame^] | 67 | void RegisterDecls::Register(ScopedDecl* D) { |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 68 | if (VarDecl* V = dyn_cast<VarDecl>(D)) { |
| 69 | LiveVariables::VPair& VP = L.getVarInfoMap()[V]; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 71 | VP.V.AliveBlocks.resize(cfg.getNumBlockIDs()); |
| 72 | VP.Idx = L.getNumDecls()++; |
| 73 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void RegisterDecls::RegisterUsedDecls() { |
| 77 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) |
| 78 | for (CFGBlock::const_iterator SI=BI->begin(),SE = BI->end();SI != SE;++SI) |
| 79 | Visit(const_cast<Stmt*>(*SI)); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | } // end anonymous namespace |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // WorkList - Data structure representing the liveness algorithm worklist. |
| 87 | // |
| 88 | |
| 89 | namespace { |
| 90 | |
| 91 | class WorkListTy { |
| 92 | typedef llvm::SmallPtrSet<const CFGBlock*,20> BlockSet; |
| 93 | BlockSet wlist; |
| 94 | public: |
| 95 | void enqueue(const CFGBlock* B) { wlist.insert(B); } |
| 96 | |
| 97 | const CFGBlock* dequeue() { |
| 98 | assert (!wlist.empty()); |
| 99 | const CFGBlock* B = *wlist.begin(); |
| 100 | wlist.erase(B); |
| 101 | return B; |
| 102 | } |
| 103 | |
| 104 | bool isEmpty() const { return wlist.empty(); } |
| 105 | }; |
| 106 | |
| 107 | } // end anonymous namespace |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // TFuncs |
| 111 | // |
| 112 | |
| 113 | namespace { |
| 114 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 115 | class LivenessTFuncs : public DataflowStmtVisitor<LivenessTFuncs, |
| 116 | dataflow::backward_analysis_tag> { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 117 | LiveVariables& L; |
| 118 | llvm::BitVector Live; |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 119 | llvm::BitVector KilledAtLeastOnce; |
| 120 | Stmt* CurrentStmt; |
| 121 | const CFGBlock* CurrentBlock; |
| 122 | bool blockPreviouslyProcessed; |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 123 | LiveVariablesObserver* Observer; |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 125 | public: |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 126 | LivenessTFuncs(LiveVariables& l, LiveVariablesObserver* A = NULL) |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 127 | : L(l), CurrentStmt(NULL), CurrentBlock(NULL), |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 128 | blockPreviouslyProcessed(false), Observer(A) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 129 | Live.resize(l.getNumDecls()); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 130 | KilledAtLeastOnce.resize(l.getNumDecls()); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 131 | } |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 133 | void VisitDeclRefExpr(DeclRefExpr* DR); |
| 134 | void VisitBinaryOperator(BinaryOperator* B); |
| 135 | void VisitAssign(BinaryOperator* B); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 136 | void VisitDeclStmt(DeclStmt* DS); |
| 137 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 138 | void ObserveStmt(Stmt* S); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 140 | unsigned getIdx(const VarDecl* D) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 141 | LiveVariables::VarInfoMap& V = L.getVarInfoMap(); |
| 142 | LiveVariables::VarInfoMap::iterator I = V.find(D); |
| 143 | assert (I != V.end()); |
| 144 | return I->second.Idx; |
| 145 | } |
| 146 | |
| 147 | bool ProcessBlock(const CFGBlock* B); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 148 | llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B); |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 149 | LiveVariables::VarInfo& KillVar(VarDecl* D); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 152 | void LivenessTFuncs::ObserveStmt(Stmt* S) { |
| 153 | if (Observer) Observer->ObserveStmt(S,L,Live); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void LivenessTFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 157 | // Register a use of the variable. |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 158 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
| 159 | Live.set(getIdx(V)); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 162 | void LivenessTFuncs::VisitBinaryOperator(BinaryOperator* B) { |
| 163 | if (B->isAssignmentOp()) VisitAssign(B); |
| 164 | else VisitStmt(B); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 167 | void LivenessTFuncs::VisitUnaryOperator(UnaryOperator* U) { |
| 168 | switch (U->getOpcode()) { |
| 169 | case UnaryOperator::PostInc: |
| 170 | case UnaryOperator::PostDec: |
| 171 | case UnaryOperator::PreInc: |
| 172 | case UnaryOperator::PreDec: |
Ted Kremenek | 97487a0 | 2007-09-12 20:28:48 +0000 | [diff] [blame] | 173 | case UnaryOperator::AddrOf: |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 174 | // Walk through the subexpressions, blasting through ParenExprs until |
| 175 | // we either find a DeclRefExpr or some non-DeclRefExpr expression. |
| 176 | for (Stmt* S = U->getSubExpr() ; ; ) { |
| 177 | if (ParenExpr* P = dyn_cast<ParenExpr>(S)) { |
| 178 | S = P->getSubExpr(); |
| 179 | continue; |
| 180 | } |
| 181 | else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) { |
| 182 | // Treat the --/++/& operator as a kill. |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 183 | LiveVariables::VarInfo& V = |
| 184 | KillVar(cast<VarDecl>(DR->getDecl())); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 185 | |
| 186 | if (!blockPreviouslyProcessed) |
| 187 | V.AddKill(CurrentStmt,DR); |
| 188 | |
| 189 | VisitDeclRefExpr(DR); |
| 190 | } |
Ted Kremenek | 97487a0 | 2007-09-12 20:28:48 +0000 | [diff] [blame] | 191 | else Visit(S); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 192 | |
| 193 | break; |
| 194 | } |
| 195 | break; |
| 196 | |
| 197 | default: |
Ted Kremenek | 37f9701 | 2007-09-12 20:11:39 +0000 | [diff] [blame] | 198 | Visit(U->getSubExpr()); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 203 | LiveVariables::VarInfo& LivenessTFuncs::KillVar(VarDecl* D) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 204 | LiveVariables::VarInfoMap::iterator I = L.getVarInfoMap().find(D); |
| 205 | |
| 206 | assert (I != L.getVarInfoMap().end() && |
| 207 | "Declaration not managed by variable map in LiveVariables"); |
| 208 | |
| 209 | // Mark the variable dead, and remove the current block from |
| 210 | // the set of blocks where the variable may be alive the entire time. |
| 211 | Live.reset(I->second.Idx); |
| 212 | I->second.V.AliveBlocks.reset(CurrentBlock->getBlockID()); |
| 213 | |
| 214 | return I->second.V; |
| 215 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 217 | void LivenessTFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 218 | // Check if we are assigning to a variable. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 219 | Stmt* LHS = B->getLHS(); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 221 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 222 | LiveVariables::VarInfo& V = KillVar(cast<VarDecl>(DR->getDecl())); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 223 | |
| 224 | // We only need to register kills once, so we check if this block |
| 225 | // has been previously processed. |
| 226 | if (!blockPreviouslyProcessed) |
Ted Kremenek | 83522a3 | 2007-09-06 23:39:53 +0000 | [diff] [blame] | 227 | V.AddKill(CurrentStmt,DR); |
| 228 | |
| 229 | if (B->getOpcode() != BinaryOperator::Assign) |
| 230 | Visit(LHS); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 231 | } |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 232 | else |
| 233 | Visit(LHS); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 234 | |
| 235 | Visit(B->getRHS()); |
| 236 | } |
| 237 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 238 | void LivenessTFuncs::VisitDeclStmt(DeclStmt* DS) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 239 | // Declarations effectively "kill" a variable since they cannot possibly |
| 240 | // be live before they are declared. Declarations, however, are not kills |
| 241 | // in the sense that the value is obliterated, so we do not register |
| 242 | // DeclStmts as a "kill site" for a variable. |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame^] | 243 | for (ScopedDecl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 244 | KillVar(cast<VarDecl>(D)); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 245 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 247 | llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 248 | LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap(); |
| 249 | |
| 250 | LiveVariables::BlockLivenessMap::iterator I = BMap.find(B); |
| 251 | return (I == BMap.end()) ? NULL : &(I->second); |
| 252 | } |
| 253 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 255 | bool LivenessTFuncs::ProcessBlock(const CFGBlock* B) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 256 | |
| 257 | CurrentBlock = B; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 258 | Live.reset(); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 259 | KilledAtLeastOnce.reset(); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 261 | // Check if this block has been previously processed. |
| 262 | LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap(); |
| 263 | LiveVariables::BlockLivenessMap::iterator BI = BMap.find(B); |
| 264 | |
| 265 | blockPreviouslyProcessed = BI != BMap.end(); |
| 266 | |
| 267 | // Merge liveness information from all predecessors. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 268 | for (CFGBlock::const_succ_iterator I=B->succ_begin(),E=B->succ_end();I!=E;++I) |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 269 | if (llvm::BitVector* V = getBlockEntryLiveness(*I)) |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 270 | Live |= *V; |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 272 | if (Observer) |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 273 | Observer->ObserveBlockExit(B,L,Live); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 274 | |
| 275 | // Tentatively mark all variables alive at the end of the current block |
| 276 | // as being alive during the whole block. We then cull these out as |
| 277 | // we process the statements of this block. |
| 278 | for (LiveVariables::VarInfoMap::iterator |
| 279 | I=L.getVarInfoMap().begin(), E=L.getVarInfoMap().end(); I != E; ++I) |
| 280 | if (Live[I->second.Idx]) |
| 281 | I->second.V.AliveBlocks.set(B->getBlockID()); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 283 | // Visit the statements in reverse order; |
| 284 | VisitBlock(B); |
| 285 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 286 | // Compare the computed "Live" values with what we already have |
| 287 | // for the entry to this block. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 288 | bool hasChanged = false; |
| 289 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 290 | if (!blockPreviouslyProcessed) { |
| 291 | // We have not previously calculated liveness information for this block. |
| 292 | // Lazily instantiate a bitvector, and copy the bits from Live. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 293 | hasChanged = true; |
| 294 | llvm::BitVector& V = BMap[B]; |
| 295 | V.resize(L.getNumDecls()); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 296 | V = Live; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 297 | } |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 298 | else if (BI->second != Live) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 299 | hasChanged = true; |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 300 | BI->second = Live; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return hasChanged; |
| 304 | } |
| 305 | |
| 306 | } // end anonymous namespace |
| 307 | |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | // runOnCFG - Method to run the actual liveness computation. |
| 310 | // |
| 311 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 312 | void LiveVariables::runOnCFG(const CFG& cfg, LiveVariablesObserver* Observer) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 313 | // Scan a CFG for DeclRefStmts. For each one, create a VarInfo object. |
| 314 | { |
| 315 | RegisterDecls R(*this,cfg); |
| 316 | R.RegisterUsedDecls(); |
| 317 | } |
| 318 | |
| 319 | // Create the worklist and enqueue the exit block. |
| 320 | WorkListTy WorkList; |
| 321 | WorkList.enqueue(&cfg.getExit()); |
| 322 | |
| 323 | // Create the state for transfer functions. |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 324 | LivenessTFuncs TF(*this,Observer); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 325 | |
| 326 | // Process the worklist until it is empty. |
| 327 | |
| 328 | while (!WorkList.isEmpty()) { |
| 329 | const CFGBlock* B = WorkList.dequeue(); |
| 330 | if (TF.ProcessBlock(B)) |
| 331 | for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); |
| 332 | I != E; ++I) |
| 333 | WorkList.enqueue(*I); |
| 334 | } |
| 335 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 336 | // Go through each block and reserve a bitvector. This is needed if |
| 337 | // a block was never visited by the worklist algorithm. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 338 | for (CFG::const_iterator I = cfg.begin(), E = cfg.end(); I != E; ++I) |
| 339 | LiveAtBlockEntryMap[&(*I)].resize(NumDecls); |
| 340 | } |
| 341 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 343 | void LiveVariables::runOnBlock(const CFGBlock* B, |
| 344 | LiveVariablesObserver* Observer) |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 345 | { |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 346 | LivenessTFuncs TF(*this,Observer); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 347 | TF.ProcessBlock(B); |
| 348 | } |
| 349 | |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | // liveness queries |
| 352 | // |
| 353 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 354 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 355 | BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B); |
| 356 | assert (I != LiveAtBlockEntryMap.end()); |
| 357 | |
| 358 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 359 | assert (VI != VarInfos.end()); |
| 360 | |
| 361 | return I->second[VI->second.Idx]; |
| 362 | } |
| 363 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 364 | bool LiveVariables::isLive(llvm::BitVector& Live, const VarDecl* D) const { |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 365 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 366 | assert (VI != VarInfos.end()); |
| 367 | return Live[VI->second.Idx]; |
| 368 | } |
| 369 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 370 | bool LiveVariables::KillsVar(const Stmt* S, const VarDecl* D) const { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 371 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 372 | assert (VI != VarInfos.end()); |
| 373 | |
| 374 | for (VarInfo::KillsSet::const_iterator |
| 375 | I = VI->second.V.Kills.begin(), E = VI->second.V.Kills.end(); I!=E;++I) |
| 376 | if (I->first == S) |
| 377 | return true; |
| 378 | |
| 379 | return false; |
| 380 | } |
| 381 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 382 | LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 383 | VarInfoMap::iterator VI = VarInfos.find(D); |
| 384 | assert (VI != VarInfos.end()); |
| 385 | return VI->second.V; |
| 386 | } |
| 387 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 388 | const LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) const{ |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 389 | return const_cast<LiveVariables*>(this)->getVarInfo(D); |
| 390 | } |
| 391 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 392 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 393 | // Defaults for LiveVariablesObserver |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 395 | void LiveVariablesObserver::ObserveStmt(Stmt* S, LiveVariables& L, |
| 396 | llvm::BitVector& V) {} |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | b00c95e | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 398 | void LiveVariablesObserver::ObserveBlockExit(const CFGBlock* B, |
| 399 | LiveVariables& L, |
| 400 | llvm::BitVector& V) {} |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 401 | |
| 402 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 403 | // printing liveness state for debugging |
| 404 | // |
| 405 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 406 | void LiveVariables::dumpLiveness(const llvm::BitVector& V, |
| 407 | SourceManager& SM) const { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 408 | |
| 409 | for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) { |
| 410 | if (V[I->second.Idx]) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 411 | |
| 412 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
| 413 | |
| 414 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 415 | I->first->getIdentifier()->getName(), |
| 416 | SM.getSourceName(PhysLoc), |
| 417 | SM.getLineNumber(PhysLoc), |
| 418 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 423 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 424 | for (BlockLivenessMap::iterator I = LiveAtBlockEntryMap.begin(), |
| 425 | E = LiveAtBlockEntryMap.end(); |
| 426 | I != E; ++I) { |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 428 | fprintf(stderr, |
| 429 | "\n[ B%d (live variables at block entry) ]\n", |
| 430 | I->first->getBlockID()); |
| 431 | |
| 432 | dumpLiveness(I->second,M); |
| 433 | } |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 434 | |
| 435 | fprintf(stderr,"\n"); |
| 436 | } |
| 437 | |
| 438 | void LiveVariables::dumpVarLiveness(SourceManager& SM) const { |
| 439 | |
| 440 | for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) { |
| 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)); |
| 448 | |
| 449 | I->second.V.Dump(SM); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void LiveVariables::VarInfo::Dump(SourceManager& SM) const { |
| 454 | fprintf(stderr," Blocks Alive:"); |
| 455 | for (unsigned i = 0; i < AliveBlocks.size(); ++i) { |
| 456 | if (i % 5 == 0) |
| 457 | fprintf(stderr,"\n "); |
| 458 | |
| 459 | fprintf(stderr," B%d", i); |
| 460 | } |
| 461 | |
| 462 | fprintf(stderr,"\n Kill Sites:\n"); |
| 463 | for (KillsSet::const_iterator I = Kills.begin(), E = Kills.end(); I!=E; ++I) { |
| 464 | SourceLocation PhysLoc = |
| 465 | SM.getPhysicalLoc(I->second->getSourceRange().Begin()); |
| 466 | |
| 467 | fprintf(stderr, " <%s:%u:%u>\n", |
| 468 | SM.getSourceName(PhysLoc), |
| 469 | SM.getLineNumber(PhysLoc), |
| 470 | SM.getColumnNumber(PhysLoc)); |
| 471 | } |
| 472 | |
| 473 | fprintf(stderr,"\n"); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 474 | } |