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