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