Ted Kremenek | 11de5cb | 2007-09-20 21:42:55 +0000 | [diff] [blame] | 1 | //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs -*- C++ --*-==// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 7 | // details. |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This file implements Live Variables analysis for source-level CFGs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | cf6e41b | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/CFG.h" |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 1de632b | 2007-09-25 21:00:24 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/FlowSensitive/DataflowSolver.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 24 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdio.h> |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 31 | // Useful constants. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | static const bool Alive = true; |
| 35 | static const bool Dead = false; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 38 | // Dataflow initialization logic. |
| 39 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 42 | class VISIBILITY_HIDDEN RegisterDecls |
| 43 | : public CFGRecStmtDeclVisitor<RegisterDecls> { |
| 44 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 45 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 46 | |
| 47 | typedef llvm::SmallVector<VarDecl*, 20> AlwaysLiveTy; |
| 48 | AlwaysLiveTy AlwaysLive; |
| 49 | |
| 50 | |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 51 | public: |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 52 | RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
| 53 | |
| 54 | ~RegisterDecls() { |
| 55 | |
| 56 | AD.AlwaysLive.resetValues(AD); |
| 57 | |
| 58 | for (AlwaysLiveTy::iterator I = AlwaysLive.begin(), E = AlwaysLive.end(); |
| 59 | I != E; ++ I) |
| 60 | AD.AlwaysLive(*I, AD) = Alive; |
| 61 | } |
| 62 | |
| 63 | void VisitVarDecl(VarDecl* VD) { |
| 64 | // Register the VarDecl for tracking. |
| 65 | AD.Register(VD); |
| 66 | |
| 67 | // Does the variable have global storage? If so, it is always live. |
| 68 | if (VD->hasGlobalStorage()) |
| 69 | AlwaysLive.push_back(VD); |
| 70 | } |
| 71 | |
| 72 | void VisitUnaryOperator(UnaryOperator* U) { |
| 73 | // Check for '&'. Any VarDecl whose value has its address-taken we |
| 74 | // treat as always being live (flow-insensitive). |
| 75 | |
| 76 | Expr* E = U->getSubExpr()->IgnoreParenCasts(); |
| 77 | |
| 78 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 79 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E)) |
| 80 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 81 | AD.Register(VD); |
| 82 | AlwaysLive.push_back(VD); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | Visit(E); |
| 87 | } |
| 88 | |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 89 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 90 | }; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 91 | } // end anonymous namespace |
| 92 | |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 94 | LiveVariables::LiveVariables(CFG& cfg) { |
| 95 | // Register all referenced VarDecls. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 96 | getAnalysisData().setCFG(&cfg); |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 97 | RegisterDecls R(getAnalysisData()); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 98 | cfg.VisitBlockStmts(R); |
| 99 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 100 | |
| 101 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 102 | // Transfer functions. |
| 103 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 104 | |
| 105 | namespace { |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | c2b51d8 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 107 | class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{ |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 108 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 109 | LiveVariables::ValTy LiveState; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 110 | public: |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 111 | TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 113 | LiveVariables::ValTy& getVal() { return LiveState; } |
Ted Kremenek | 9f9141c | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 114 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 116 | void VisitDeclRefExpr(DeclRefExpr* DR); |
| 117 | void VisitBinaryOperator(BinaryOperator* B); |
| 118 | void VisitAssign(BinaryOperator* B); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 119 | void VisitDeclStmt(DeclStmt* DS); |
| 120 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | 3762208 | 2008-04-15 04:39:08 +0000 | [diff] [blame] | 121 | void Visit(Stmt *S); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 122 | void VisitTerminator(CFGBlock* B); |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 123 | |
| 124 | void SetTopValue(LiveVariables::ValTy& V) { |
| 125 | V = AD.AlwaysLive; |
| 126 | } |
| 127 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 128 | }; |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 130 | void TransferFuncs::Visit(Stmt *S) { |
| 131 | if (AD.Observer) |
| 132 | AD.Observer->ObserveStmt(S,AD,LiveState); |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 134 | if (S == getCurrentBlkStmt()) { |
Ted Kremenek | 1aa9a58 | 2008-03-05 19:26:46 +0000 | [diff] [blame] | 135 | if (getCFG().isBlkExpr(S)) LiveState(S,AD) = Dead; |
Ted Kremenek | 2a9da9c | 2008-01-18 00:40:21 +0000 | [diff] [blame] | 136 | StmtVisitor<TransferFuncs,void>::Visit(S); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 137 | } |
| 138 | else if (!getCFG().isBlkExpr(S)) |
| 139 | StmtVisitor<TransferFuncs,void>::Visit(S); |
| 140 | else |
| 141 | // For block-level expressions, mark that they are live. |
| 142 | LiveState(S,AD) = Alive; |
Ted Kremenek | 11e7218 | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | 3762208 | 2008-04-15 04:39:08 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 145 | void TransferFuncs::VisitTerminator(CFGBlock* B) { |
| 146 | |
| 147 | const Expr* E = B->getTerminatorCondition(); |
| 148 | |
Ted Kremenek | 5cd2438 | 2008-04-16 17:07:59 +0000 | [diff] [blame] | 149 | if (!E) |
| 150 | return; |
| 151 | |
Ted Kremenek | 5cd2438 | 2008-04-16 17:07:59 +0000 | [diff] [blame] | 152 | assert (getCFG().isBlkExpr(E)); |
| 153 | LiveState(E, AD) = Alive; |
Ted Kremenek | 3762208 | 2008-04-15 04:39:08 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 156 | void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 157 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 158 | LiveState(V,AD) = Alive; |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 159 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 161 | void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | f175805 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 162 | if (B->isAssignmentOp()) VisitAssign(B); |
| 163 | else VisitStmt(B); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 166 | void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 5e2b609 | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 167 | Expr *E = U->getSubExpr(); |
Ted Kremenek | bcb07d5 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 169 | switch (U->getOpcode()) { |
Ted Kremenek | 8d9ebae | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 170 | case UnaryOperator::SizeOf: return; |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 171 | case UnaryOperator::PostInc: |
| 172 | case UnaryOperator::PostDec: |
| 173 | case UnaryOperator::PreInc: |
| 174 | case UnaryOperator::PreDec: |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 175 | // Walk through the subexpressions, blasting through ParenExprs |
| 176 | // until we either find a DeclRefExpr or some non-DeclRefExpr |
| 177 | // expression. |
Ted Kremenek | 5620631 | 2008-02-22 00:34:10 +0000 | [diff] [blame] | 178 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens())) |
Ted Kremenek | dace4c9 | 2008-04-15 04:08:54 +0000 | [diff] [blame] | 179 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 180 | // Treat the --/++ operator as a kill. |
Ted Kremenek | 5620631 | 2008-02-22 00:34:10 +0000 | [diff] [blame] | 181 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
Ted Kremenek | dace4c9 | 2008-04-15 04:08:54 +0000 | [diff] [blame] | 182 | LiveState(VD, AD) = Alive; |
Ted Kremenek | 5620631 | 2008-02-22 00:34:10 +0000 | [diff] [blame] | 183 | return VisitDeclRefExpr(DR); |
| 184 | } |
Ted Kremenek | bcb07d5 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 185 | |
| 186 | // Fall-through. |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 187 | |
| 188 | default: |
Ted Kremenek | 5e2b609 | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 189 | return Visit(E); |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
Ted Kremenek | bcb07d5 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 193 | void TransferFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | 5e2b609 | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 194 | Expr* LHS = B->getLHS(); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | bcb07d5 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 196 | // Assigning to a variable? |
Ted Kremenek | 5e2b609 | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 197 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) { |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 198 | |
| 199 | // Update liveness inforamtion. |
| 200 | unsigned bit = AD.getIdx(DR->getDecl()); |
| 201 | LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit); |
| 202 | |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 203 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 204 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 205 | // Handle things like +=, etc., which also generate "uses" |
| 206 | // of a variable. Do this just by visiting the subexpression. |
Ted Kremenek | bcb07d5 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 207 | if (B->getOpcode() != BinaryOperator::Assign) |
| 208 | VisitDeclRefExpr(DR); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 209 | } |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 210 | else // Not assigning to a variable. Process LHS as usual. |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 211 | Visit(LHS); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 213 | Visit(B->getRHS()); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 216 | void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { |
| 217 | // Declarations effectively "kill" a variable since they cannot |
| 218 | // possibly be live before they are declared. |
Ted Kremenek | dcc4810 | 2008-02-25 22:28:54 +0000 | [diff] [blame] | 219 | for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) |
| 220 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | 7deed0c | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 221 | |
| 222 | // Update liveness information. |
| 223 | unsigned bit = AD.getIdx(VD); |
| 224 | LiveState.getDeclBit(bit) = Dead | AD.AlwaysLive.getDeclBit(bit); |
Ted Kremenek | dcc4810 | 2008-02-25 22:28:54 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 2bca5e4 | 2008-02-07 02:38:55 +0000 | [diff] [blame] | 226 | if (Expr* Init = VD->getInit()) |
| 227 | Visit(Init); |
Ted Kremenek | dcc4810 | 2008-02-25 22:28:54 +0000 | [diff] [blame] | 228 | } |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 229 | } |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 231 | } // end anonymous namespace |
| 232 | |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | // Merge operator: if something is live on any successor block, it is live |
| 235 | // in the current block (a set union). |
| 236 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 238 | namespace { |
Ted Kremenek | fa59f1f | 2008-03-20 21:46:49 +0000 | [diff] [blame] | 239 | |
| 240 | struct Merge { |
| 241 | typedef ExprDeclBitVector_Types::ValTy ValTy; |
| 242 | |
| 243 | void operator()(ValTy& Dst, const ValTy& Src) { |
| 244 | Dst.OrDeclBits(Src); |
| 245 | Dst.AndExprBits(Src); |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | typedef DataflowSolver<LiveVariables, TransferFuncs, Merge> Solver; |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 250 | } // end anonymous namespace |
| 251 | |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | // External interface to run Liveness analysis. |
| 254 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 256 | void LiveVariables::runOnCFG(CFG& cfg) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 257 | Solver S(*this); |
| 258 | S.runOnCFG(cfg); |
| 259 | } |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 261 | void LiveVariables::runOnAllBlocks(const CFG& cfg, |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 262 | LiveVariables::ObserverTy* Obs, |
| 263 | bool recordStmtValues) { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 264 | Solver S(*this); |
| 265 | ObserverTy* OldObserver = getAnalysisData().Observer; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 266 | getAnalysisData().Observer = Obs; |
| 267 | S.runOnAllBlocks(cfg, recordStmtValues); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 268 | getAnalysisData().Observer = OldObserver; |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | // liveness queries |
| 273 | // |
| 274 | |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 275 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 276 | DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D); |
| 277 | return i.isValid() ? getBlockData(B).getBit(i) : false; |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 280 | bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const { |
Ted Kremenek | 7cb1593 | 2008-03-13 16:55:07 +0000 | [diff] [blame] | 281 | DeclBitVector_Types::Idx i = getAnalysisData().getIdx(D); |
| 282 | return i.isValid() ? Live.getBit(i) : false; |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 285 | bool LiveVariables::isLive(const Stmt* Loc, const Stmt* StmtVal) const { |
| 286 | return getStmtData(Loc)(StmtVal,getAnalysisData()); |
| 287 | } |
| 288 | |
Ted Kremenek | 2a9da9c | 2008-01-18 00:40:21 +0000 | [diff] [blame] | 289 | bool LiveVariables::isLive(const Stmt* Loc, const VarDecl* D) const { |
| 290 | return getStmtData(Loc)(D,getAnalysisData()); |
| 291 | } |
| 292 | |
Ted Kremenek | 055c275 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 293 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 294 | // printing liveness state for debugging |
| 295 | // |
| 296 | |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 297 | void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const { |
| 298 | const AnalysisDataTy& AD = getAnalysisData(); |
| 299 | |
Ted Kremenek | f63aa45 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 300 | for (AnalysisDataTy::decl_iterator I = AD.begin_decl(), |
| 301 | E = AD.end_decl(); I!=E; ++I) |
| 302 | if (V.getDeclBit(I->second)) { |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 303 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 305 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 306 | I->first->getIdentifier()->getName(), |
| 307 | SM.getSourceName(PhysLoc), |
| 308 | SM.getLineNumber(PhysLoc), |
| 309 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 310 | } |
Ted Kremenek | e4e6334 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 313 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | fdd225e | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 314 | for (BlockDataMapTy::iterator I = getBlockDataMap().begin(), |
| 315 | E = getBlockDataMap().end(); I!=E; ++I) { |
| 316 | fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 317 | I->first->getBlockID()); |
| 318 | |
| 319 | dumpLiveness(I->second,M); |
| 320 | } |
Ted Kremenek | c0576ca | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 321 | |
| 322 | fprintf(stderr,"\n"); |
| 323 | } |