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