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 | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 7 | // details. |
Ted Kremenek | aa04c51 | 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 | cdf8e84 | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.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 | 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 | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 30 | // Dataflow initialization logic. |
| 31 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 32 | |
| 33 | namespace { |
Ted Kremenek | ec81835 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 34 | class VISIBILITY_HIDDEN RegisterDecls |
| 35 | : public CFGRecStmtDeclVisitor<RegisterDecls> { |
| 36 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 37 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 38 | public: |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 39 | RegisterDecls(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 40 | void VisitVarDecl(VarDecl* VD) { AD.Register(VD); } |
Ted Kremenek | 705386b | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 41 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 42 | }; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 43 | } // end anonymous namespace |
| 44 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 45 | void LiveVariables::InitializeValues(const CFG& cfg) { |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 46 | RegisterDecls R(getAnalysisData()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 47 | cfg.VisitBlockStmts(R); |
| 48 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 49 | |
| 50 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 51 | // Transfer functions. |
| 52 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 55 | |
| 56 | static const bool Alive = true; |
| 57 | static const bool Dead = false; |
| 58 | |
Ted Kremenek | ec81835 | 2008-01-08 18:19:08 +0000 | [diff] [blame] | 59 | class VISIBILITY_HIDDEN TransferFuncs : public CFGRecStmtVisitor<TransferFuncs>{ |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 60 | LiveVariables::AnalysisDataTy& AD; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 61 | LiveVariables::ValTy LiveState; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 62 | public: |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 63 | TransferFuncs(LiveVariables::AnalysisDataTy& ad) : AD(ad) {} |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 65 | LiveVariables::ValTy& getVal() { return LiveState; } |
Ted Kremenek | 705386b | 2007-11-20 03:01:58 +0000 | [diff] [blame] | 66 | CFG& getCFG() { return AD.getCFG(); } |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 68 | void VisitDeclRefExpr(DeclRefExpr* DR); |
| 69 | void VisitBinaryOperator(BinaryOperator* B); |
| 70 | void VisitAssign(BinaryOperator* B); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 71 | void VisitDeclStmt(DeclStmt* DS); |
| 72 | void VisitUnaryOperator(UnaryOperator* U); |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 73 | void Visit(Stmt *S); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 74 | }; |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 76 | void TransferFuncs::Visit(Stmt *S) { |
| 77 | if (AD.Observer) |
| 78 | AD.Observer->ObserveStmt(S,AD,LiveState); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 80 | static_cast<CFGStmtVisitor<TransferFuncs>*>(this)->Visit(S); |
| 81 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 83 | void TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) { |
| 84 | if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl())) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 85 | LiveState(V,AD) = Alive; |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 86 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 88 | void TransferFuncs::VisitBinaryOperator(BinaryOperator* B) { |
Ted Kremenek | 00b6327 | 2007-09-12 19:10:52 +0000 | [diff] [blame] | 89 | if (B->isAssignmentOp()) VisitAssign(B); |
| 90 | else VisitStmt(B); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 93 | void TransferFuncs::VisitUnaryOperator(UnaryOperator* U) { |
Ted Kremenek | 133befa | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 94 | Expr *E = U->getSubExpr(); |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 96 | switch (U->getOpcode()) { |
Ted Kremenek | e33d100 | 2007-12-13 04:47:15 +0000 | [diff] [blame] | 97 | case UnaryOperator::SizeOf: return; |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 98 | case UnaryOperator::PostInc: |
| 99 | case UnaryOperator::PostDec: |
| 100 | case UnaryOperator::PreInc: |
| 101 | case UnaryOperator::PreDec: |
| 102 | case UnaryOperator::AddrOf: |
| 103 | // Walk through the subexpressions, blasting through ParenExprs |
| 104 | // until we either find a DeclRefExpr or some non-DeclRefExpr |
| 105 | // expression. |
Ted Kremenek | 133befa | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 106 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 107 | // Treat the --/++/& operator as a kill. |
| 108 | LiveState(DR->getDecl(),AD) = Dead; |
| 109 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 110 | return VisitDeclRefExpr(DR); |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 111 | } |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 112 | |
| 113 | // Fall-through. |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 114 | |
| 115 | default: |
Ted Kremenek | 133befa | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 116 | return Visit(E); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 120 | void TransferFuncs::VisitAssign(BinaryOperator* B) { |
Ted Kremenek | 133befa | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 121 | Expr* LHS = B->getLHS(); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 123 | // Assigning to a variable? |
Ted Kremenek | 133befa | 2008-01-17 17:50:49 +0000 | [diff] [blame] | 124 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParens())) { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 125 | LiveState(DR->getDecl(),AD) = Dead; |
| 126 | if (AD.Observer) { AD.Observer->ObserverKill(DR); } |
| 127 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 128 | // Handle things like +=, etc., which also generate "uses" |
| 129 | // of a variable. Do this just by visiting the subexpression. |
Ted Kremenek | 83a6ba1 | 2007-09-28 21:29:33 +0000 | [diff] [blame] | 130 | if (B->getOpcode() != BinaryOperator::Assign) |
| 131 | VisitDeclRefExpr(DR); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 132 | } |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 133 | else // Not assigning to a variable. Process LHS as usual. |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 134 | Visit(LHS); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 136 | Visit(B->getRHS()); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 139 | void TransferFuncs::VisitDeclStmt(DeclStmt* DS) { |
| 140 | // Declarations effectively "kill" a variable since they cannot |
| 141 | // possibly be live before they are declared. |
Ted Kremenek | 8ce772b | 2007-10-01 20:33:52 +0000 | [diff] [blame] | 142 | for (ScopedDecl* D = DS->getDecl(); D != NULL; D = D->getNextDeclarator()) |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 143 | LiveState(D,AD) = Dead; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 144 | } |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 146 | } // end anonymous namespace |
| 147 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 148 | //===----------------------------------------------------------------------===// |
| 149 | // Merge operator: if something is live on any successor block, it is live |
| 150 | // in the current block (a set union). |
| 151 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 153 | namespace { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 154 | typedef DeclBitVector_Types::Union Merge; |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 155 | typedef DataflowSolver<LiveVariables,TransferFuncs,Merge> Solver; |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 156 | } // end anonymous namespace |
| 157 | |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | // External interface to run Liveness analysis. |
| 160 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 5ee98a7 | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 162 | void LiveVariables::runOnCFG(CFG& cfg) { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 163 | Solver S(*this); |
| 164 | S.runOnCFG(cfg); |
| 165 | } |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 167 | void LiveVariables::runOnAllBlocks(const CFG& cfg, |
| 168 | LiveVariables::ObserverTy& Obs) { |
| 169 | Solver S(*this); |
| 170 | ObserverTy* OldObserver = getAnalysisData().Observer; |
| 171 | getAnalysisData().Observer = &Obs; |
| 172 | S.runOnAllBlocks(cfg); |
| 173 | getAnalysisData().Observer = OldObserver; |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // liveness queries |
| 178 | // |
| 179 | |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 180 | bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 181 | return getBlockData(B)(D,getAnalysisData()); |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 184 | bool LiveVariables::isLive(const ValTy& Live, const VarDecl* D) const { |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 185 | return Live(D,getAnalysisData()); |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | e805c4a | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 189 | // printing liveness state for debugging |
| 190 | // |
| 191 | |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 192 | void LiveVariables::dumpLiveness(const ValTy& V, SourceManager& SM) const { |
| 193 | const AnalysisDataTy& AD = getAnalysisData(); |
| 194 | |
Ted Kremenek | eebf131 | 2007-09-28 20:38:59 +0000 | [diff] [blame] | 195 | for (AnalysisDataTy::decl_iterator I = AD.begin_decl(), |
| 196 | E = AD.end_decl(); I!=E; ++I) |
| 197 | if (V.getDeclBit(I->second)) { |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 198 | SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation()); |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 200 | fprintf(stderr, " %s <%s:%u:%u>\n", |
| 201 | I->first->getIdentifier()->getName(), |
| 202 | SM.getSourceName(PhysLoc), |
| 203 | SM.getLineNumber(PhysLoc), |
| 204 | SM.getColumnNumber(PhysLoc)); |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 205 | } |
Ted Kremenek | aa04c51 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 208 | void LiveVariables::dumpBlockLiveness(SourceManager& M) const { |
Ted Kremenek | d7a2f81 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 209 | for (BlockDataMapTy::iterator I = getBlockDataMap().begin(), |
| 210 | E = getBlockDataMap().end(); I!=E; ++I) { |
| 211 | fprintf(stderr, "\n[ B%d (live variables at block exit) ]\n", |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 212 | I->first->getBlockID()); |
| 213 | |
| 214 | dumpLiveness(I->second,M); |
| 215 | } |
Ted Kremenek | 6b2b4e3 | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 216 | |
| 217 | fprintf(stderr,"\n"); |
| 218 | } |