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