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