Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 1 | //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs ----------*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 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" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/Stmt.h" |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/AnalysisContext.h" |
| 19 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PostOrderIterator.h" |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame^] | 22 | #include "llvm/ADT/PriorityQueue.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <vector> |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 29 | namespace { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 30 | |
| 31 | class DataflowWorklist { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 32 | llvm::BitVector enqueuedBlocks; |
| 33 | PostOrderCFGView *POV; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame^] | 34 | llvm::PriorityQueue<const CFGBlock *, SmallVector<const CFGBlock *, 20>, |
| 35 | PostOrderCFGView::BlockOrderCompare> worklist; |
| 36 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 37 | public: |
| 38 | DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx) |
| 39 | : enqueuedBlocks(cfg.getNumBlockIDs()), |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame^] | 40 | POV(Ctx.getAnalysis<PostOrderCFGView>()), |
| 41 | worklist(POV->getComparator()) {} |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 42 | |
| 43 | void enqueueBlock(const CFGBlock *block); |
| 44 | void enqueuePredecessors(const CFGBlock *block); |
| 45 | |
| 46 | const CFGBlock *dequeue(); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 49 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 50 | |
| 51 | void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) { |
| 52 | if (block && !enqueuedBlocks[block->getBlockID()]) { |
| 53 | enqueuedBlocks[block->getBlockID()] = true; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame^] | 54 | worklist.push(block); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 59 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 60 | E = block->pred_end(); I != E; ++I) { |
| 61 | enqueueBlock(*I); |
| 62 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | const CFGBlock *DataflowWorklist::dequeue() { |
| 66 | if (worklist.empty()) |
| 67 | return nullptr; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame^] | 68 | const CFGBlock *b = worklist.top(); |
| 69 | worklist.pop(); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 70 | enqueuedBlocks[b->getBlockID()] = false; |
| 71 | return b; |
| 72 | } |
| 73 | |
| 74 | namespace { |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 75 | class LiveVariablesImpl { |
| 76 | public: |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 77 | AnalysisDeclContext &analysisContext; |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 78 | llvm::ImmutableSet<const Stmt *>::Factory SSetFact; |
| 79 | llvm::ImmutableSet<const VarDecl *>::Factory DSetFact; |
| 80 | llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksEndToLiveness; |
| 81 | llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues> blocksBeginToLiveness; |
| 82 | llvm::DenseMap<const Stmt *, LiveVariables::LivenessValues> stmtsToLiveness; |
| 83 | llvm::DenseMap<const DeclRefExpr *, unsigned> inAssignment; |
| 84 | const bool killAtAssign; |
| 85 | |
| 86 | LiveVariables::LivenessValues |
| 87 | merge(LiveVariables::LivenessValues valsA, |
| 88 | LiveVariables::LivenessValues valsB); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 89 | |
| 90 | LiveVariables::LivenessValues |
| 91 | runOnBlock(const CFGBlock *block, LiveVariables::LivenessValues val, |
| 92 | LiveVariables::Observer *obs = nullptr); |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 93 | |
| 94 | void dumpBlockLiveness(const SourceManager& M); |
| 95 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 96 | LiveVariablesImpl(AnalysisDeclContext &ac, bool KillAtAssign) |
Ted Kremenek | c8f008a | 2011-10-02 01:45:37 +0000 | [diff] [blame] | 97 | : analysisContext(ac), |
| 98 | SSetFact(false), // Do not canonicalize ImmutableSets by default. |
| 99 | DSetFact(false), // This is a *major* performance win. |
| 100 | killAtAssign(KillAtAssign) {} |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 101 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 102 | } |
Ted Kremenek | 82ff6d6 | 2008-04-15 18:35:30 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 104 | static LiveVariablesImpl &getImpl(void *x) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 105 | return *((LiveVariablesImpl *) x); |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 106 | } |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 107 | |
| 108 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 109 | // Operations and queries on LivenessValues. |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | bool LiveVariables::LivenessValues::isLive(const Stmt *S) const { |
| 113 | return liveStmts.contains(S); |
| 114 | } |
| 115 | |
| 116 | bool LiveVariables::LivenessValues::isLive(const VarDecl *D) const { |
| 117 | return liveDecls.contains(D); |
| 118 | } |
| 119 | |
| 120 | namespace { |
| 121 | template <typename SET> |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 122 | SET mergeSets(SET A, SET B) { |
| 123 | if (A.isEmpty()) |
| 124 | return B; |
| 125 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 126 | for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) { |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 127 | A = A.add(*it); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 128 | } |
| 129 | return A; |
| 130 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 131 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 132 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 133 | void LiveVariables::Observer::anchor() { } |
| 134 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 135 | LiveVariables::LivenessValues |
| 136 | LiveVariablesImpl::merge(LiveVariables::LivenessValues valsA, |
| 137 | LiveVariables::LivenessValues valsB) { |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 138 | |
| 139 | llvm::ImmutableSetRef<const Stmt *> |
| 140 | SSetRefA(valsA.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()), |
| 141 | SSetRefB(valsB.liveStmts.getRootWithoutRetain(), SSetFact.getTreeFactory()); |
| 142 | |
| 143 | |
| 144 | llvm::ImmutableSetRef<const VarDecl *> |
| 145 | DSetRefA(valsA.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()), |
| 146 | DSetRefB(valsB.liveDecls.getRootWithoutRetain(), DSetFact.getTreeFactory()); |
| 147 | |
| 148 | |
| 149 | SSetRefA = mergeSets(SSetRefA, SSetRefB); |
| 150 | DSetRefA = mergeSets(DSetRefA, DSetRefB); |
| 151 | |
Ted Kremenek | c8f008a | 2011-10-02 01:45:37 +0000 | [diff] [blame] | 152 | // asImmutableSet() canonicalizes the tree, allowing us to do an easy |
| 153 | // comparison afterwards. |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 154 | return LiveVariables::LivenessValues(SSetRefA.asImmutableSet(), |
| 155 | DSetRefA.asImmutableSet()); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool LiveVariables::LivenessValues::equals(const LivenessValues &V) const { |
| 159 | return liveStmts == V.liveStmts && liveDecls == V.liveDecls; |
| 160 | } |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // Query methods. |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | static bool isAlwaysAlive(const VarDecl *D) { |
| 167 | return D->hasGlobalStorage(); |
| 168 | } |
| 169 | |
| 170 | bool LiveVariables::isLive(const CFGBlock *B, const VarDecl *D) { |
| 171 | return isAlwaysAlive(D) || getImpl(impl).blocksEndToLiveness[B].isLive(D); |
| 172 | } |
| 173 | |
| 174 | bool LiveVariables::isLive(const Stmt *S, const VarDecl *D) { |
| 175 | return isAlwaysAlive(D) || getImpl(impl).stmtsToLiveness[S].isLive(D); |
| 176 | } |
| 177 | |
| 178 | bool LiveVariables::isLive(const Stmt *Loc, const Stmt *S) { |
| 179 | return getImpl(impl).stmtsToLiveness[Loc].isLive(S); |
| 180 | } |
| 181 | |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | // Dataflow computation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 185 | |
| 186 | namespace { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 187 | class TransferFunctions : public StmtVisitor<TransferFunctions> { |
| 188 | LiveVariablesImpl &LV; |
| 189 | LiveVariables::LivenessValues &val; |
| 190 | LiveVariables::Observer *observer; |
Ted Kremenek | 9865d7f | 2011-02-11 23:24:26 +0000 | [diff] [blame] | 191 | const CFGBlock *currentBlock; |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 192 | public: |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 193 | TransferFunctions(LiveVariablesImpl &im, |
| 194 | LiveVariables::LivenessValues &Val, |
| 195 | LiveVariables::Observer *Observer, |
| 196 | const CFGBlock *CurrentBlock) |
| 197 | : LV(im), val(Val), observer(Observer), currentBlock(CurrentBlock) {} |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 199 | void VisitBinaryOperator(BinaryOperator *BO); |
| 200 | void VisitBlockExpr(BlockExpr *BE); |
| 201 | void VisitDeclRefExpr(DeclRefExpr *DR); |
| 202 | void VisitDeclStmt(DeclStmt *DS); |
| 203 | void VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS); |
| 204 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE); |
| 205 | void VisitUnaryOperator(UnaryOperator *UO); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | void Visit(Stmt *S); |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 207 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 208 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 210 | static const VariableArrayType *FindVA(QualType Ty) { |
| 211 | const Type *ty = Ty.getTypePtr(); |
| 212 | while (const ArrayType *VT = dyn_cast<ArrayType>(ty)) { |
| 213 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(VT)) |
| 214 | if (VAT->getSizeExpr()) |
| 215 | return VAT; |
| 216 | |
| 217 | ty = VT->getElementType().getTypePtr(); |
| 218 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 219 | |
| 220 | return nullptr; |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Ted Kremenek | 5717049 | 2011-11-05 00:26:53 +0000 | [diff] [blame] | 223 | static const Stmt *LookThroughStmt(const Stmt *S) { |
Ted Kremenek | 977e30d | 2011-11-05 07:34:28 +0000 | [diff] [blame] | 224 | while (S) { |
| 225 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
| 226 | S = Ex->IgnoreParens(); |
John McCall | 29928fc | 2011-11-09 17:10:36 +0000 | [diff] [blame] | 227 | if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) { |
| 228 | S = EWC->getSubExpr(); |
| 229 | continue; |
| 230 | } |
Ted Kremenek | 977e30d | 2011-11-05 07:34:28 +0000 | [diff] [blame] | 231 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) { |
| 232 | S = OVE->getSourceExpr(); |
| 233 | continue; |
| 234 | } |
| 235 | break; |
| 236 | } |
Ted Kremenek | 5717049 | 2011-11-05 00:26:53 +0000 | [diff] [blame] | 237 | return S; |
| 238 | } |
| 239 | |
| 240 | static void AddLiveStmt(llvm::ImmutableSet<const Stmt *> &Set, |
| 241 | llvm::ImmutableSet<const Stmt *>::Factory &F, |
| 242 | const Stmt *S) { |
| 243 | Set = F.add(Set, LookThroughStmt(S)); |
| 244 | } |
| 245 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 246 | void TransferFunctions::Visit(Stmt *S) { |
| 247 | if (observer) |
| 248 | observer->observeStmt(S, currentBlock, val); |
Ted Kremenek | 9c951ab | 2009-12-24 02:40:30 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 250 | StmtVisitor<TransferFunctions>::Visit(S); |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 252 | if (isa<Expr>(S)) { |
| 253 | val.liveStmts = LV.SSetFact.remove(val.liveStmts, S); |
| 254 | } |
| 255 | |
| 256 | // Mark all children expressions live. |
| 257 | |
| 258 | switch (S->getStmtClass()) { |
| 259 | default: |
| 260 | break; |
| 261 | case Stmt::StmtExprClass: { |
| 262 | // For statement expressions, look through the compound statement. |
| 263 | S = cast<StmtExpr>(S)->getSubStmt(); |
| 264 | break; |
| 265 | } |
| 266 | case Stmt::CXXMemberCallExprClass: { |
| 267 | // Include the implicit "this" pointer as being live. |
| 268 | CXXMemberCallExpr *CE = cast<CXXMemberCallExpr>(S); |
Ted Kremenek | b7531d6 | 2011-10-06 20:53:28 +0000 | [diff] [blame] | 269 | if (Expr *ImplicitObj = CE->getImplicitObjectArgument()) { |
Ted Kremenek | 5717049 | 2011-11-05 00:26:53 +0000 | [diff] [blame] | 270 | AddLiveStmt(val.liveStmts, LV.SSetFact, ImplicitObj); |
Ted Kremenek | b7531d6 | 2011-10-06 20:53:28 +0000 | [diff] [blame] | 271 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
Anna Zaks | 23665a1 | 2012-08-14 00:36:20 +0000 | [diff] [blame] | 274 | case Stmt::ObjCMessageExprClass: { |
| 275 | // In calls to super, include the implicit "self" pointer as being live. |
| 276 | ObjCMessageExpr *CE = cast<ObjCMessageExpr>(S); |
| 277 | if (CE->getReceiverKind() == ObjCMessageExpr::SuperInstance) |
| 278 | val.liveDecls = LV.DSetFact.add(val.liveDecls, |
| 279 | LV.analysisContext.getSelfDecl()); |
| 280 | break; |
| 281 | } |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 282 | case Stmt::DeclStmtClass: { |
| 283 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 284 | if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) { |
| 285 | for (const VariableArrayType* VA = FindVA(VD->getType()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 286 | VA != nullptr; VA = FindVA(VA->getElementType())) { |
Ted Kremenek | 5717049 | 2011-11-05 00:26:53 +0000 | [diff] [blame] | 287 | AddLiveStmt(val.liveStmts, LV.SSetFact, VA->getSizeExpr()); |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | break; |
| 291 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 292 | case Stmt::PseudoObjectExprClass: { |
| 293 | // A pseudo-object operation only directly consumes its result |
| 294 | // expression. |
| 295 | Expr *child = cast<PseudoObjectExpr>(S)->getResultExpr(); |
| 296 | if (!child) return; |
| 297 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(child)) |
| 298 | child = OV->getSourceExpr(); |
| 299 | child = child->IgnoreParens(); |
| 300 | val.liveStmts = LV.SSetFact.add(val.liveStmts, child); |
| 301 | return; |
| 302 | } |
| 303 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 304 | // FIXME: These cases eventually shouldn't be needed. |
| 305 | case Stmt::ExprWithCleanupsClass: { |
| 306 | S = cast<ExprWithCleanups>(S)->getSubExpr(); |
| 307 | break; |
| 308 | } |
| 309 | case Stmt::CXXBindTemporaryExprClass: { |
| 310 | S = cast<CXXBindTemporaryExpr>(S)->getSubExpr(); |
| 311 | break; |
| 312 | } |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 313 | case Stmt::UnaryExprOrTypeTraitExprClass: { |
| 314 | // No need to unconditionally visit subexpressions. |
| 315 | return; |
| 316 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 317 | } |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame] | 318 | |
| 319 | for (Stmt *Child : S->children()) { |
| 320 | if (Child) |
| 321 | AddLiveStmt(val.liveStmts, LV.SSetFact, Child); |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 322 | } |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 323 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 325 | void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) { |
| 326 | if (B->isAssignmentOp()) { |
| 327 | if (!LV.killAtAssign) |
Ted Kremenek | fc419a0 | 2008-11-14 21:07:14 +0000 | [diff] [blame] | 328 | return; |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 329 | |
| 330 | // Assigning to a variable? |
| 331 | Expr *LHS = B->getLHS()->IgnoreParens(); |
| 332 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 333 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 334 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 335 | // Assignments to references don't kill the ref's address |
| 336 | if (VD->getType()->isReferenceType()) |
| 337 | return; |
Ted Kremenek | 3b4e1d5 | 2008-11-11 19:40:47 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 339 | if (!isAlwaysAlive(VD)) { |
| 340 | // The variable is now dead. |
| 341 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
| 342 | } |
Ted Kremenek | fbd2f40 | 2008-11-11 17:42:10 +0000 | [diff] [blame] | 343 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 344 | if (observer) |
| 345 | observer->observerKill(DR); |
Ted Kremenek | 20c9142 | 2008-02-22 00:34:10 +0000 | [diff] [blame] | 346 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 350 | void TransferFunctions::VisitBlockExpr(BlockExpr *BE) { |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 351 | for (const VarDecl *VD : |
| 352 | LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 353 | if (isAlwaysAlive(VD)) |
| 354 | continue; |
| 355 | val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 356 | } |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 359 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) { |
| 360 | if (const VarDecl *D = dyn_cast<VarDecl>(DR->getDecl())) |
| 361 | if (!isAlwaysAlive(D) && LV.inAssignment.find(DR) == LV.inAssignment.end()) |
| 362 | val.liveDecls = LV.DSetFact.add(val.liveDecls, D); |
| 363 | } |
| 364 | |
| 365 | void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 366 | for (const auto *DI : DS->decls()) |
| 367 | if (const auto *VD = dyn_cast<VarDecl>(DI)) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 368 | if (!isAlwaysAlive(VD)) |
| 369 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
Ted Kremenek | 7845b26 | 2008-02-25 22:28:54 +0000 | [diff] [blame] | 370 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 371 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 373 | void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) { |
| 374 | // Kill the iteration variable. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 375 | DeclRefExpr *DR = nullptr; |
| 376 | const VarDecl *VD = nullptr; |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 378 | Stmt *element = OS->getElement(); |
| 379 | if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) { |
| 380 | VD = cast<VarDecl>(DS->getSingleDecl()); |
| 381 | } |
| 382 | else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) { |
| 383 | VD = cast<VarDecl>(DR->getDecl()); |
| 384 | } |
| 385 | |
| 386 | if (VD) { |
| 387 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
| 388 | if (observer && DR) |
| 389 | observer->observerKill(DR); |
| 390 | } |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 391 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 393 | void TransferFunctions:: |
| 394 | VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE) |
| 395 | { |
| 396 | // While sizeof(var) doesn't technically extend the liveness of 'var', it |
| 397 | // does extent the liveness of metadata if 'var' is a VariableArrayType. |
| 398 | // We handle that special case here. |
| 399 | if (UE->getKind() != UETT_SizeOf || UE->isArgumentType()) |
| 400 | return; |
| 401 | |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 402 | const Expr *subEx = UE->getArgumentExpr(); |
| 403 | if (subEx->getType()->isVariableArrayType()) { |
| 404 | assert(subEx->isLValue()); |
| 405 | val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens()); |
| 406 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 409 | void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) { |
| 410 | // Treat ++/-- as a kill. |
| 411 | // Note we don't actually have to do anything if we don't have an observer, |
| 412 | // since a ++/-- acts as both a kill and a "use". |
| 413 | if (!observer) |
| 414 | return; |
| 415 | |
| 416 | switch (UO->getOpcode()) { |
| 417 | default: |
| 418 | return; |
| 419 | case UO_PostInc: |
| 420 | case UO_PostDec: |
| 421 | case UO_PreInc: |
| 422 | case UO_PreDec: |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) |
| 427 | if (isa<VarDecl>(DR->getDecl())) { |
| 428 | // Treat ++/-- as a kill. |
| 429 | observer->observerKill(DR); |
| 430 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 433 | LiveVariables::LivenessValues |
| 434 | LiveVariablesImpl::runOnBlock(const CFGBlock *block, |
| 435 | LiveVariables::LivenessValues val, |
| 436 | LiveVariables::Observer *obs) { |
| 437 | |
| 438 | TransferFunctions TF(*this, val, obs, block); |
| 439 | |
| 440 | // Visit the terminator (if any). |
| 441 | if (const Stmt *term = block->getTerminator()) |
| 442 | TF.Visit(const_cast<Stmt*>(term)); |
| 443 | |
| 444 | // Apply the transfer function for all Stmts in the block. |
| 445 | for (CFGBlock::const_reverse_iterator it = block->rbegin(), |
| 446 | ei = block->rend(); it != ei; ++it) { |
| 447 | const CFGElement &elem = *it; |
Jordan Rose | b324456 | 2012-07-26 20:04:08 +0000 | [diff] [blame] | 448 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 449 | if (Optional<CFGAutomaticObjDtor> Dtor = |
| 450 | elem.getAs<CFGAutomaticObjDtor>()) { |
| 451 | val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl()); |
Jordan Rose | b324456 | 2012-07-26 20:04:08 +0000 | [diff] [blame] | 452 | continue; |
| 453 | } |
| 454 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 455 | if (!elem.getAs<CFGStmt>()) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 456 | continue; |
| 457 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 458 | const Stmt *S = elem.castAs<CFGStmt>().getStmt(); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 459 | TF.Visit(const_cast<Stmt*>(S)); |
| 460 | stmtsToLiveness[S] = val; |
| 461 | } |
| 462 | return val; |
Ted Kremenek | 6dc7b11 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 465 | void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) { |
| 466 | const CFG *cfg = getImpl(impl).analysisContext.getCFG(); |
| 467 | for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) |
| 468 | getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 471 | LiveVariables::LiveVariables(void *im) : impl(im) {} |
| 472 | |
| 473 | LiveVariables::~LiveVariables() { |
| 474 | delete (LiveVariablesImpl*) impl; |
Ted Kremenek | 05ecfdd | 2008-01-18 00:40:21 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 477 | LiveVariables * |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 478 | LiveVariables::computeLiveness(AnalysisDeclContext &AC, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 479 | bool killAtAssign) { |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 481 | // No CFG? Bail out. |
| 482 | CFG *cfg = AC.getCFG(); |
| 483 | if (!cfg) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 484 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | de21a1c | 2012-07-02 20:21:52 +0000 | [diff] [blame] | 486 | // The analysis currently has scalability issues for very large CFGs. |
| 487 | // Bail out if it looks too large. |
| 488 | if (cfg->getNumBlockIDs() > 300000) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 489 | return nullptr; |
Ted Kremenek | de21a1c | 2012-07-02 20:21:52 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 491 | LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign); |
| 492 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 493 | // Construct the dataflow worklist. Enqueue the exit block as the |
| 494 | // start of the analysis. |
| 495 | DataflowWorklist worklist(*cfg, AC); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 496 | llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs()); |
| 497 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 498 | // FIXME: we should enqueue using post order. |
| 499 | for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) { |
| 500 | const CFGBlock *block = *it; |
| 501 | worklist.enqueueBlock(block); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 502 | |
| 503 | // FIXME: Scan for DeclRefExprs using in the LHS of an assignment. |
| 504 | // We need to do this because we lack context in the reverse analysis |
| 505 | // to determine if a DeclRefExpr appears in such a context, and thus |
| 506 | // doesn't constitute a "use". |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 507 | if (killAtAssign) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 508 | for (CFGBlock::const_iterator bi = block->begin(), be = block->end(); |
| 509 | bi != be; ++bi) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 510 | if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) { |
| 511 | if (const BinaryOperator *BO = |
| 512 | dyn_cast<BinaryOperator>(cs->getStmt())) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 513 | if (BO->getOpcode() == BO_Assign) { |
| 514 | if (const DeclRefExpr *DR = |
| 515 | dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) { |
| 516 | LV->inAssignment[DR] = 1; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 522 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 523 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 524 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 525 | // Determine if the block's end value has changed. If not, we |
| 526 | // have nothing left to do for this block. |
| 527 | LivenessValues &prevVal = LV->blocksEndToLiveness[block]; |
| 528 | |
| 529 | // Merge the values of all successor blocks. |
| 530 | LivenessValues val; |
| 531 | for (CFGBlock::const_succ_iterator it = block->succ_begin(), |
| 532 | ei = block->succ_end(); it != ei; ++it) { |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 533 | if (const CFGBlock *succ = *it) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 534 | val = LV->merge(val, LV->blocksBeginToLiveness[succ]); |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 535 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (!everAnalyzedBlock[block->getBlockID()]) |
| 539 | everAnalyzedBlock[block->getBlockID()] = true; |
| 540 | else if (prevVal.equals(val)) |
| 541 | continue; |
| 542 | |
| 543 | prevVal = val; |
| 544 | |
| 545 | // Update the dataflow value for the start of this block. |
| 546 | LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val); |
| 547 | |
| 548 | // Enqueue the value to the predecessors. |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 549 | worklist.enqueuePredecessors(block); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | return new LiveVariables(LV); |
| 553 | } |
| 554 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 555 | void LiveVariables::dumpBlockLiveness(const SourceManager &M) { |
| 556 | getImpl(impl).dumpBlockLiveness(M); |
| 557 | } |
| 558 | |
| 559 | void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) { |
| 560 | std::vector<const CFGBlock *> vec; |
| 561 | for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator |
| 562 | it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end(); |
| 563 | it != ei; ++it) { |
| 564 | vec.push_back(it->first); |
| 565 | } |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 566 | std::sort(vec.begin(), vec.end(), [](const CFGBlock *A, const CFGBlock *B) { |
| 567 | return A->getBlockID() < B->getBlockID(); |
| 568 | }); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 569 | |
| 570 | std::vector<const VarDecl*> declVec; |
| 571 | |
| 572 | for (std::vector<const CFGBlock *>::iterator |
| 573 | it = vec.begin(), ei = vec.end(); it != ei; ++it) { |
| 574 | llvm::errs() << "\n[ B" << (*it)->getBlockID() |
| 575 | << " (live variables at block exit) ]\n"; |
| 576 | |
| 577 | LiveVariables::LivenessValues vals = blocksEndToLiveness[*it]; |
| 578 | declVec.clear(); |
| 579 | |
| 580 | for (llvm::ImmutableSet<const VarDecl *>::iterator si = |
| 581 | vals.liveDecls.begin(), |
| 582 | se = vals.liveDecls.end(); si != se; ++si) { |
| 583 | declVec.push_back(*si); |
| 584 | } |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 585 | |
| 586 | std::sort(declVec.begin(), declVec.end(), [](const Decl *A, const Decl *B) { |
| 587 | return A->getLocStart() < B->getLocStart(); |
| 588 | }); |
| 589 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 590 | for (std::vector<const VarDecl*>::iterator di = declVec.begin(), |
| 591 | de = declVec.end(); di != de; ++di) { |
| 592 | llvm::errs() << " " << (*di)->getDeclName().getAsString() |
| 593 | << " <"; |
| 594 | (*di)->getLocation().dump(M); |
Daniel Dunbar | e81a553 | 2009-10-17 18:12:37 +0000 | [diff] [blame] | 595 | llvm::errs() << ">\n"; |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 596 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 597 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 598 | llvm::errs() << "\n"; |
Ted Kremenek | bd9cc5c | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 599 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 600 | |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 601 | const void *LiveVariables::getTag() { static int x; return &x; } |
| 602 | const void *RelaxedLiveVariables::getTag() { static int x; return &x; } |