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