Ted Kremenek | aa04c51 | 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 | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/CFG.h" |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 18 | #include "clang/Analysis/DataflowStmtVisitor.h" |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 19 | #include "clang/Lex/IdentifierTable.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <stdio.h> |
Ted Kremenek | aa04c51 | 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 | f63bda5 | 2007-09-06 23:25:10 +0000 | [diff] [blame] | 43 | void VisitDeclStmt(DeclStmt* DS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 44 | void Register(Decl* D); |
Ted Kremenek | f63bda5 | 2007-09-06 23:25:10 +0000 | [diff] [blame] | 45 | void RegisterDeclChain(Decl* D); |
Ted Kremenek | aa04c51 | 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 | f63bda5 | 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 | |
| 62 | void RegisterDecls::RegisterDeclChain(Decl* D) { |
| 63 | for (; D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 64 | Register(D); |
| 65 | } |
| 66 | |
| 67 | void RegisterDecls::Register(Decl* D) { |
Ted Kremenek | 6b2b4e3 | 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 | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 71 | VP.V.AliveBlocks.resize(cfg.getNumBlockIDs()); |
| 72 | VP.Idx = L.getNumDecls()++; |
| 73 | } |
Ted Kremenek | aa04c51 | 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 | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 115 | class LivenessTFuncs : public DataflowStmtVisitor<LivenessTFuncs, |
| 116 | dataflow::backward_analysis_tag> { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 117 | LiveVariables& L; |
| 118 | llvm::BitVector Live; |
Ted Kremenek | 0533468 | 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 | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 123 | LiveVariablesObserver* Observer; |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 124 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 125 | public: |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 126 | LivenessTFuncs(LiveVariables& l, LiveVariablesObserver* A = NULL) |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 127 | : L(l), CurrentStmt(NULL), CurrentBlock(NULL), |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 128 | blockPreviouslyProcessed(false), Observer(A) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 129 | Live.resize(l.getNumDecls()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 130 | KilledAtLeastOnce.resize(l.getNumDecls()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 131 | } |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 132 | |
Ted Kremenek | aa04c51 | 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 | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 136 | void VisitDeclStmt(DeclStmt* DS); |
| 137 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 138 | void ObserveStmt(Stmt* S); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 140 | unsigned getIdx(const VarDecl* D) { |
Ted Kremenek | aa04c51 | 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 | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 148 | llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B); |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 149 | LiveVariables::VarInfo& KillVar(VarDecl* D); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Ted Kremenek | 00b6327 | 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 | aa04c51 | 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 | 6b2b4e3 | 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 | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 00b6327 | 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 | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ted Kremenek | 0533468 | 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: |
| 173 | case UnaryOperator::AddrOf: |
| 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 | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 183 | LiveVariables::VarInfo& V = |
| 184 | KillVar(cast<VarDecl>(DR->getDecl())); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 185 | |
| 186 | if (!blockPreviouslyProcessed) |
| 187 | V.AddKill(CurrentStmt,DR); |
| 188 | |
| 189 | VisitDeclRefExpr(DR); |
| 190 | } |
| 191 | else |
| 192 | Visit(S); |
| 193 | |
| 194 | break; |
| 195 | } |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | VisitStmt(U->getSubExpr()); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 204 | LiveVariables::VarInfo& LivenessTFuncs::KillVar(VarDecl* D) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 205 | LiveVariables::VarInfoMap::iterator I = L.getVarInfoMap().find(D); |
| 206 | |
| 207 | assert (I != L.getVarInfoMap().end() && |
| 208 | "Declaration not managed by variable map in LiveVariables"); |
| 209 | |
| 210 | // Mark the variable dead, and remove the current block from |
| 211 | // the set of blocks where the variable may be alive the entire time. |
| 212 | Live.reset(I->second.Idx); |
| 213 | I->second.V.AliveBlocks.reset(CurrentBlock->getBlockID()); |
| 214 | |
| 215 | return I->second.V; |
| 216 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 218 | void LivenessTFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 219 | // Check if we are assigning to a variable. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 220 | Stmt* LHS = B->getLHS(); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 222 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 223 | LiveVariables::VarInfo& V = KillVar(cast<VarDecl>(DR->getDecl())); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 224 | |
| 225 | // We only need to register kills once, so we check if this block |
| 226 | // has been previously processed. |
| 227 | if (!blockPreviouslyProcessed) |
Ted Kremenek | 42276bc | 2007-09-06 23:39:53 +0000 | [diff] [blame] | 228 | V.AddKill(CurrentStmt,DR); |
| 229 | |
| 230 | if (B->getOpcode() != BinaryOperator::Assign) |
| 231 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 232 | } |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 233 | else |
| 234 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 235 | |
| 236 | Visit(B->getRHS()); |
| 237 | } |
| 238 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 239 | void LivenessTFuncs::VisitDeclStmt(DeclStmt* DS) { |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 240 | if (Observer) |
| 241 | Observer->ObserveStmt(DS,L,Live); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 242 | |
| 243 | // Declarations effectively "kill" a variable since they cannot possibly |
| 244 | // be live before they are declared. Declarations, however, are not kills |
| 245 | // in the sense that the value is obliterated, so we do not register |
| 246 | // DeclStmts as a "kill site" for a variable. |
| 247 | for (Decl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator()) |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 248 | KillVar(cast<VarDecl>(D)); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 249 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 251 | llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 252 | LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap(); |
| 253 | |
| 254 | LiveVariables::BlockLivenessMap::iterator I = BMap.find(B); |
| 255 | return (I == BMap.end()) ? NULL : &(I->second); |
| 256 | } |
| 257 | |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 258 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 259 | bool LivenessTFuncs::ProcessBlock(const CFGBlock* B) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 260 | |
| 261 | CurrentBlock = B; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 262 | Live.reset(); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 263 | KilledAtLeastOnce.reset(); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 265 | // Check if this block has been previously processed. |
| 266 | LiveVariables::BlockLivenessMap& BMap = L.getLiveAtBlockEntryMap(); |
| 267 | LiveVariables::BlockLivenessMap::iterator BI = BMap.find(B); |
| 268 | |
| 269 | blockPreviouslyProcessed = BI != BMap.end(); |
| 270 | |
| 271 | // Merge liveness information from all predecessors. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 272 | for (CFGBlock::const_succ_iterator I=B->succ_begin(),E=B->succ_end();I!=E;++I) |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 273 | if (llvm::BitVector* V = getBlockEntryLiveness(*I)) |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 274 | Live |= *V; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 276 | if (Observer) |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 277 | Observer->ObserveBlockExit(B,L,Live); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 278 | |
| 279 | // Tentatively mark all variables alive at the end of the current block |
| 280 | // as being alive during the whole block. We then cull these out as |
| 281 | // we process the statements of this block. |
| 282 | for (LiveVariables::VarInfoMap::iterator |
| 283 | I=L.getVarInfoMap().begin(), E=L.getVarInfoMap().end(); I != E; ++I) |
| 284 | if (Live[I->second.Idx]) |
| 285 | I->second.V.AliveBlocks.set(B->getBlockID()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame^] | 287 | // Visit the statements in reverse order; |
| 288 | VisitBlock(B); |
| 289 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 290 | // Compare the computed "Live" values with what we already have |
| 291 | // for the entry to this block. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 292 | bool hasChanged = false; |
| 293 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 294 | if (!blockPreviouslyProcessed) { |
| 295 | // We have not previously calculated liveness information for this block. |
| 296 | // Lazily instantiate a bitvector, and copy the bits from Live. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 297 | hasChanged = true; |
| 298 | llvm::BitVector& V = BMap[B]; |
| 299 | V.resize(L.getNumDecls()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 300 | V = Live; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 301 | } |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 302 | else if (BI->second != Live) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 303 | hasChanged = true; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 304 | BI->second = Live; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return hasChanged; |
| 308 | } |
| 309 | |
| 310 | } // end anonymous namespace |
| 311 | |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | // runOnCFG - Method to run the actual liveness computation. |
| 314 | // |
| 315 | |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 316 | void LiveVariables::runOnCFG(const CFG& cfg, LiveVariablesObserver* Observer) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 317 | // Scan a CFG for DeclRefStmts. For each one, create a VarInfo object. |
| 318 | { |
| 319 | RegisterDecls R(*this,cfg); |
| 320 | R.RegisterUsedDecls(); |
| 321 | } |
| 322 | |
| 323 | // Create the worklist and enqueue the exit block. |
| 324 | WorkListTy WorkList; |
| 325 | WorkList.enqueue(&cfg.getExit()); |
| 326 | |
| 327 | // Create the state for transfer functions. |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 328 | LivenessTFuncs TF(*this,Observer); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 329 | |
| 330 | // Process the worklist until it is empty. |
| 331 | |
| 332 | while (!WorkList.isEmpty()) { |
| 333 | const CFGBlock* B = WorkList.dequeue(); |
| 334 | if (TF.ProcessBlock(B)) |
| 335 | for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); |
| 336 | I != E; ++I) |
| 337 | WorkList.enqueue(*I); |
| 338 | } |
| 339 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 340 | // Go through each block and reserve a bitvector. This is needed if |
| 341 | // a block was never visited by the worklist algorithm. |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 342 | for (CFG::const_iterator I = cfg.begin(), E = cfg.end(); I != E; ++I) |
| 343 | LiveAtBlockEntryMap[&(*I)].resize(NumDecls); |
| 344 | } |
| 345 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 347 | void LiveVariables::runOnBlock(const CFGBlock* B, |
| 348 | LiveVariablesObserver* Observer) |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 349 | { |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 350 | LivenessTFuncs TF(*this,Observer); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 351 | TF.ProcessBlock(B); |
| 352 | } |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | // liveness queries |
| 356 | // |
| 357 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 358 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 359 | BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B); |
| 360 | assert (I != LiveAtBlockEntryMap.end()); |
| 361 | |
| 362 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 363 | assert (VI != VarInfos.end()); |
| 364 | |
| 365 | return I->second[VI->second.Idx]; |
| 366 | } |
| 367 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 368 | bool LiveVariables::isLive(llvm::BitVector& Live, const VarDecl* D) const { |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 369 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 370 | assert (VI != VarInfos.end()); |
| 371 | return Live[VI->second.Idx]; |
| 372 | } |
| 373 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 374 | bool LiveVariables::KillsVar(const Stmt* S, const VarDecl* D) const { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 375 | VarInfoMap::const_iterator VI = VarInfos.find(D); |
| 376 | assert (VI != VarInfos.end()); |
| 377 | |
| 378 | for (VarInfo::KillsSet::const_iterator |
| 379 | I = VI->second.V.Kills.begin(), E = VI->second.V.Kills.end(); I!=E;++I) |
| 380 | if (I->first == S) |
| 381 | return true; |
| 382 | |
| 383 | return false; |
| 384 | } |
| 385 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 386 | LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 387 | VarInfoMap::iterator VI = VarInfos.find(D); |
| 388 | assert (VI != VarInfos.end()); |
| 389 | return VI->second.V; |
| 390 | } |
| 391 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 392 | const LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) const{ |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 393 | return const_cast<LiveVariables*>(this)->getVarInfo(D); |
| 394 | } |
| 395 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 396 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 397 | // Defaults for LiveVariablesObserver |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 399 | void LiveVariablesObserver::ObserveStmt(Stmt* S, LiveVariables& L, |
| 400 | llvm::BitVector& V) {} |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | d1d8826 | 2007-09-10 15:56:38 +0000 | [diff] [blame] | 402 | void LiveVariablesObserver::ObserveBlockExit(const CFGBlock* B, |
| 403 | LiveVariables& L, |
| 404 | llvm::BitVector& V) {} |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 405 | |
| 406 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 407 | // printing liveness state for debugging |
| 408 | // |
| 409 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 410 | void LiveVariables::dumpLiveness(const llvm::BitVector& V, |
| 411 | SourceManager& SM) const { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 412 | |
| 413 | for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) { |
| 414 | if (V[I->second.Idx]) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 415 | |
| 416 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
| 417 | |
| 418 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 419 | I->first->getIdentifier()->getName(), |
| 420 | SM.getSourceName(PhysLoc), |
| 421 | SM.getLineNumber(PhysLoc), |
| 422 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 427 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 428 | for (BlockLivenessMap::iterator I = LiveAtBlockEntryMap.begin(), |
| 429 | E = LiveAtBlockEntryMap.end(); |
| 430 | I != E; ++I) { |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 432 | fprintf(stderr, |
| 433 | "\n[ B%d (live variables at block entry) ]\n", |
| 434 | I->first->getBlockID()); |
| 435 | |
| 436 | dumpLiveness(I->second,M); |
| 437 | } |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 438 | |
| 439 | fprintf(stderr,"\n"); |
| 440 | } |
| 441 | |
| 442 | void LiveVariables::dumpVarLiveness(SourceManager& SM) const { |
| 443 | |
| 444 | for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) { |
| 445 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
| 446 | |
| 447 | fprintf(stderr, "[ %s <%s:%u:%u> ]\n", |
| 448 | I->first->getIdentifier()->getName(), |
| 449 | SM.getSourceName(PhysLoc), |
| 450 | SM.getLineNumber(PhysLoc), |
| 451 | SM.getColumnNumber(PhysLoc)); |
| 452 | |
| 453 | I->second.V.Dump(SM); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | void LiveVariables::VarInfo::Dump(SourceManager& SM) const { |
| 458 | fprintf(stderr," Blocks Alive:"); |
| 459 | for (unsigned i = 0; i < AliveBlocks.size(); ++i) { |
| 460 | if (i % 5 == 0) |
| 461 | fprintf(stderr,"\n "); |
| 462 | |
| 463 | fprintf(stderr," B%d", i); |
| 464 | } |
| 465 | |
| 466 | fprintf(stderr,"\n Kill Sites:\n"); |
| 467 | for (KillsSet::const_iterator I = Kills.begin(), E = Kills.end(); I!=E; ++I) { |
| 468 | SourceLocation PhysLoc = |
| 469 | SM.getPhysicalLoc(I->second->getSourceRange().Begin()); |
| 470 | |
| 471 | fprintf(stderr, " <%s:%u:%u>\n", |
| 472 | SM.getSourceName(PhysLoc), |
| 473 | SM.getLineNumber(PhysLoc), |
| 474 | SM.getColumnNumber(PhysLoc)); |
| 475 | } |
| 476 | |
| 477 | fprintf(stderr,"\n"); |
Gabor Greif | 61ce98c | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 478 | } |