Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 1 | //=- LiveVariables.cpp - Live Variable Analysis for Source CFGs ----------*-==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements Live Variables analysis for source-level CFGs. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Ted Kremenek | bf593f8 | 2007-12-21 21:42:19 +0000 | [diff] [blame] | 13 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 14 | #include "clang/AST/Stmt.h" |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/StmtVisitor.h" |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
George Karpenkov | 50657f6 | 2017-09-06 21:45:03 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/AnalysisDeclContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/PostOrderIterator.h" |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PriorityQueue.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <vector> |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 28 | namespace { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 29 | |
| 30 | class DataflowWorklist { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 31 | llvm::BitVector enqueuedBlocks; |
| 32 | PostOrderCFGView *POV; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame] | 33 | llvm::PriorityQueue<const CFGBlock *, SmallVector<const CFGBlock *, 20>, |
| 34 | PostOrderCFGView::BlockOrderCompare> worklist; |
| 35 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 36 | public: |
| 37 | DataflowWorklist(const CFG &cfg, AnalysisDeclContext &Ctx) |
| 38 | : enqueuedBlocks(cfg.getNumBlockIDs()), |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame] | 39 | POV(Ctx.getAnalysis<PostOrderCFGView>()), |
| 40 | worklist(POV->getComparator()) {} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 41 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 42 | void enqueueBlock(const CFGBlock *block); |
| 43 | void enqueuePredecessors(const CFGBlock *block); |
| 44 | |
| 45 | const CFGBlock *dequeue(); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 48 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 49 | |
| 50 | void DataflowWorklist::enqueueBlock(const clang::CFGBlock *block) { |
| 51 | if (block && !enqueuedBlocks[block->getBlockID()]) { |
| 52 | enqueuedBlocks[block->getBlockID()] = true; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame] | 53 | worklist.push(block); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void DataflowWorklist::enqueuePredecessors(const clang::CFGBlock *block) { |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 58 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 59 | E = block->pred_end(); I != E; ++I) { |
| 60 | enqueueBlock(*I); |
| 61 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | const CFGBlock *DataflowWorklist::dequeue() { |
| 65 | if (worklist.empty()) |
| 66 | return nullptr; |
Alexander Shaposhnikov | fd905fc | 2016-10-13 21:31:46 +0000 | [diff] [blame] | 67 | const CFGBlock *b = worklist.top(); |
| 68 | worklist.pop(); |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 69 | enqueuedBlocks[b->getBlockID()] = false; |
| 70 | return b; |
| 71 | } |
| 72 | |
| 73 | namespace { |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 74 | class LiveVariablesImpl { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 75 | public: |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 76 | AnalysisDeclContext &analysisContext; |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 77 | llvm::ImmutableSet<const Stmt *>::Factory SSetFact; |
| 78 | llvm::ImmutableSet<const VarDecl *>::Factory DSetFact; |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 79 | llvm::ImmutableSet<const BindingDecl *>::Factory BSetFact; |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 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; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 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); |
Artem Dergachev | dda4216 | 2018-12-16 23:44:06 +0000 | [diff] [blame] | 95 | void dumpStmtLiveness(const SourceManager& M); |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 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(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 240 | if (const FullExpr *FE = dyn_cast<FullExpr>(S)) { |
| 241 | S = FE->getSubExpr(); |
John McCall | 29928fc | 2011-11-09 17:10:36 +0000 | [diff] [blame] | 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 | } |
Artem Dergachev | dda4216 | 2018-12-16 23:44:06 +0000 | [diff] [blame] | 330 | case Stmt::IfStmtClass: { |
| 331 | // If one of the branches is an expression rather than a compound |
| 332 | // statement, it will be bad if we mark it as live at the terminator |
| 333 | // of the if-statement (i.e., immediately after the condition expression). |
| 334 | AddLiveStmt(val.liveStmts, LV.SSetFact, cast<IfStmt>(S)->getCond()); |
| 335 | return; |
| 336 | } |
| 337 | case Stmt::WhileStmtClass: { |
| 338 | // If the loop body is an expression rather than a compound statement, |
| 339 | // it will be bad if we mark it as live at the terminator of the loop |
| 340 | // (i.e., immediately after the condition expression). |
| 341 | AddLiveStmt(val.liveStmts, LV.SSetFact, cast<WhileStmt>(S)->getCond()); |
| 342 | return; |
| 343 | } |
| 344 | case Stmt::DoStmtClass: { |
| 345 | // If the loop body is an expression rather than a compound statement, |
| 346 | // it will be bad if we mark it as live at the terminator of the loop |
| 347 | // (i.e., immediately after the condition expression). |
| 348 | AddLiveStmt(val.liveStmts, LV.SSetFact, cast<DoStmt>(S)->getCond()); |
| 349 | return; |
| 350 | } |
| 351 | case Stmt::ForStmtClass: { |
| 352 | // If the loop body is an expression rather than a compound statement, |
| 353 | // it will be bad if we mark it as live at the terminator of the loop |
| 354 | // (i.e., immediately after the condition expression). |
| 355 | AddLiveStmt(val.liveStmts, LV.SSetFact, cast<ForStmt>(S)->getCond()); |
| 356 | return; |
| 357 | } |
| 358 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 359 | } |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame] | 360 | |
| 361 | for (Stmt *Child : S->children()) { |
| 362 | if (Child) |
| 363 | AddLiveStmt(val.liveStmts, LV.SSetFact, Child); |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 365 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 367 | static bool writeShouldKill(const VarDecl *VD) { |
| 368 | return VD && !VD->getType()->isReferenceType() && |
| 369 | !isAlwaysAlive(VD); |
| 370 | } |
| 371 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 372 | void TransferFunctions::VisitBinaryOperator(BinaryOperator *B) { |
| 373 | if (B->isAssignmentOp()) { |
| 374 | if (!LV.killAtAssign) |
Ted Kremenek | fc419a0 | 2008-11-14 21:07:14 +0000 | [diff] [blame] | 375 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 377 | // Assigning to a variable? |
| 378 | Expr *LHS = B->getLHS()->IgnoreParens(); |
Ted Kremenek | 3b4e1d5 | 2008-11-11 19:40:47 +0000 | [diff] [blame] | 379 | |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 380 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 381 | const Decl* D = DR->getDecl(); |
| 382 | bool Killed = false; |
| 383 | |
| 384 | if (const BindingDecl* BD = dyn_cast<BindingDecl>(D)) { |
| 385 | Killed = !BD->getType()->isReferenceType(); |
| 386 | if (Killed) |
| 387 | val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD); |
| 388 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 389 | Killed = writeShouldKill(VD); |
| 390 | if (Killed) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 391 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
Ted Kremenek | fbd2f40 | 2008-11-11 17:42:10 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 20c9142 | 2008-02-22 00:34:10 +0000 | [diff] [blame] | 393 | } |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 394 | |
| 395 | if (Killed && observer) |
| 396 | observer->observerKill(DR); |
| 397 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 401 | void TransferFunctions::VisitBlockExpr(BlockExpr *BE) { |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 402 | for (const VarDecl *VD : |
| 403 | LV.analysisContext.getReferencedBlockVars(BE->getBlockDecl())) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 404 | if (isAlwaysAlive(VD)) |
| 405 | continue; |
| 406 | val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 407 | } |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 410 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) { |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 411 | const Decl* D = DR->getDecl(); |
| 412 | bool InAssignment = LV.inAssignment[DR]; |
George Karpenkov | dbddadd | 2018-04-06 19:14:05 +0000 | [diff] [blame] | 413 | if (const auto *BD = dyn_cast<BindingDecl>(D)) { |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 414 | if (!InAssignment) |
Andrea Di Biagio | d3144ea | 2018-04-02 12:04:37 +0000 | [diff] [blame] | 415 | val.liveBindings = LV.BSetFact.add(val.liveBindings, BD); |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 416 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 417 | if (!InAssignment && !isAlwaysAlive(VD)) |
| 418 | val.liveDecls = LV.DSetFact.add(val.liveDecls, VD); |
| 419 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 423 | for (const auto *DI : DS->decls()) { |
| 424 | if (const auto *DD = dyn_cast<DecompositionDecl>(DI)) { |
| 425 | for (const auto *BD : DD->bindings()) |
| 426 | val.liveBindings = LV.BSetFact.remove(val.liveBindings, BD); |
| 427 | } else if (const auto *VD = dyn_cast<VarDecl>(DI)) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 428 | if (!isAlwaysAlive(VD)) |
| 429 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
Ted Kremenek | 7845b26 | 2008-02-25 22:28:54 +0000 | [diff] [blame] | 430 | } |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 431 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 434 | void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *OS) { |
| 435 | // Kill the iteration variable. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 436 | DeclRefExpr *DR = nullptr; |
| 437 | const VarDecl *VD = nullptr; |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 439 | Stmt *element = OS->getElement(); |
| 440 | if (DeclStmt *DS = dyn_cast<DeclStmt>(element)) { |
| 441 | VD = cast<VarDecl>(DS->getSingleDecl()); |
| 442 | } |
| 443 | else if ((DR = dyn_cast<DeclRefExpr>(cast<Expr>(element)->IgnoreParens()))) { |
| 444 | VD = cast<VarDecl>(DR->getDecl()); |
| 445 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 447 | if (VD) { |
| 448 | val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD); |
| 449 | if (observer && DR) |
| 450 | observer->observerKill(DR); |
| 451 | } |
Ted Kremenek | ad8bce0 | 2007-09-25 04:31:27 +0000 | [diff] [blame] | 452 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 454 | void TransferFunctions:: |
| 455 | VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *UE) |
| 456 | { |
| 457 | // While sizeof(var) doesn't technically extend the liveness of 'var', it |
| 458 | // does extent the liveness of metadata if 'var' is a VariableArrayType. |
| 459 | // We handle that special case here. |
| 460 | if (UE->getKind() != UETT_SizeOf || UE->isArgumentType()) |
| 461 | return; |
| 462 | |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 463 | const Expr *subEx = UE->getArgumentExpr(); |
| 464 | if (subEx->getType()->isVariableArrayType()) { |
| 465 | assert(subEx->isLValue()); |
| 466 | val.liveStmts = LV.SSetFact.add(val.liveStmts, subEx->IgnoreParens()); |
| 467 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 470 | void TransferFunctions::VisitUnaryOperator(UnaryOperator *UO) { |
| 471 | // Treat ++/-- as a kill. |
| 472 | // Note we don't actually have to do anything if we don't have an observer, |
| 473 | // since a ++/-- acts as both a kill and a "use". |
| 474 | if (!observer) |
| 475 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 476 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 477 | switch (UO->getOpcode()) { |
| 478 | default: |
| 479 | return; |
| 480 | case UO_PostInc: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 481 | case UO_PostDec: |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 482 | case UO_PreInc: |
| 483 | case UO_PreDec: |
| 484 | break; |
| 485 | } |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 486 | |
| 487 | if (auto *DR = dyn_cast<DeclRefExpr>(UO->getSubExpr()->IgnoreParens())) { |
| 488 | const Decl *D = DR->getDecl(); |
| 489 | if (isa<VarDecl>(D) || isa<BindingDecl>(D)) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 490 | // Treat ++/-- as a kill. |
| 491 | observer->observerKill(DR); |
| 492 | } |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 493 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 496 | LiveVariables::LivenessValues |
| 497 | LiveVariablesImpl::runOnBlock(const CFGBlock *block, |
| 498 | LiveVariables::LivenessValues val, |
| 499 | LiveVariables::Observer *obs) { |
| 500 | |
| 501 | TransferFunctions TF(*this, val, obs, block); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 503 | // Visit the terminator (if any). |
| 504 | if (const Stmt *term = block->getTerminator()) |
| 505 | TF.Visit(const_cast<Stmt*>(term)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 507 | // Apply the transfer function for all Stmts in the block. |
| 508 | for (CFGBlock::const_reverse_iterator it = block->rbegin(), |
| 509 | ei = block->rend(); it != ei; ++it) { |
| 510 | const CFGElement &elem = *it; |
Jordan Rose | b324456 | 2012-07-26 20:04:08 +0000 | [diff] [blame] | 511 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 512 | if (Optional<CFGAutomaticObjDtor> Dtor = |
| 513 | elem.getAs<CFGAutomaticObjDtor>()) { |
| 514 | val.liveDecls = DSetFact.add(val.liveDecls, Dtor->getVarDecl()); |
Jordan Rose | b324456 | 2012-07-26 20:04:08 +0000 | [diff] [blame] | 515 | continue; |
| 516 | } |
| 517 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 518 | if (!elem.getAs<CFGStmt>()) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 519 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 520 | |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 521 | const Stmt *S = elem.castAs<CFGStmt>().getStmt(); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 522 | TF.Visit(const_cast<Stmt*>(S)); |
| 523 | stmtsToLiveness[S] = val; |
| 524 | } |
| 525 | return val; |
Ted Kremenek | 6dc7b11 | 2007-09-06 23:00:42 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 528 | void LiveVariables::runOnAllBlocks(LiveVariables::Observer &obs) { |
| 529 | const CFG *cfg = getImpl(impl).analysisContext.getCFG(); |
| 530 | 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] | 531 | getImpl(impl).runOnBlock(*it, getImpl(impl).blocksEndToLiveness[*it], &obs); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 534 | LiveVariables::LiveVariables(void *im) : impl(im) {} |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 535 | |
| 536 | LiveVariables::~LiveVariables() { |
| 537 | delete (LiveVariablesImpl*) impl; |
Ted Kremenek | 05ecfdd | 2008-01-18 00:40:21 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 540 | LiveVariables * |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 541 | LiveVariables::computeLiveness(AnalysisDeclContext &AC, |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 542 | bool killAtAssign) { |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 544 | // No CFG? Bail out. |
| 545 | CFG *cfg = AC.getCFG(); |
| 546 | if (!cfg) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 547 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | de21a1c | 2012-07-02 20:21:52 +0000 | [diff] [blame] | 549 | // The analysis currently has scalability issues for very large CFGs. |
| 550 | // Bail out if it looks too large. |
| 551 | if (cfg->getNumBlockIDs() > 300000) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 552 | return nullptr; |
Ted Kremenek | de21a1c | 2012-07-02 20:21:52 +0000 | [diff] [blame] | 553 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 554 | LiveVariablesImpl *LV = new LiveVariablesImpl(AC, killAtAssign); |
| 555 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 556 | // Construct the dataflow worklist. Enqueue the exit block as the |
| 557 | // start of the analysis. |
| 558 | DataflowWorklist worklist(*cfg, AC); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 559 | llvm::BitVector everAnalyzedBlock(cfg->getNumBlockIDs()); |
| 560 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 561 | // FIXME: we should enqueue using post order. |
| 562 | for (CFG::const_iterator it = cfg->begin(), ei = cfg->end(); it != ei; ++it) { |
| 563 | const CFGBlock *block = *it; |
| 564 | worklist.enqueueBlock(block); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 566 | // FIXME: Scan for DeclRefExprs using in the LHS of an assignment. |
| 567 | // We need to do this because we lack context in the reverse analysis |
| 568 | // to determine if a DeclRefExpr appears in such a context, and thus |
| 569 | // doesn't constitute a "use". |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 570 | if (killAtAssign) |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 571 | for (CFGBlock::const_iterator bi = block->begin(), be = block->end(); |
| 572 | bi != be; ++bi) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 573 | if (Optional<CFGStmt> cs = bi->getAs<CFGStmt>()) { |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 574 | const Stmt* stmt = cs->getStmt(); |
| 575 | if (const auto *BO = dyn_cast<BinaryOperator>(stmt)) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 576 | if (BO->getOpcode() == BO_Assign) { |
George Karpenkov | 137ca91 | 2018-03-31 01:20:06 +0000 | [diff] [blame] | 577 | if (const auto *DR = |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 578 | dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) { |
| 579 | LV->inAssignment[DR] = 1; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 585 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 586 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 587 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 588 | // Determine if the block's end value has changed. If not, we |
| 589 | // have nothing left to do for this block. |
| 590 | LivenessValues &prevVal = LV->blocksEndToLiveness[block]; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 591 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 592 | // Merge the values of all successor blocks. |
| 593 | LivenessValues val; |
| 594 | for (CFGBlock::const_succ_iterator it = block->succ_begin(), |
| 595 | ei = block->succ_end(); it != ei; ++it) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 596 | if (const CFGBlock *succ = *it) { |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 597 | val = LV->merge(val, LV->blocksBeginToLiveness[succ]); |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 598 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 599 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 600 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 601 | if (!everAnalyzedBlock[block->getBlockID()]) |
| 602 | everAnalyzedBlock[block->getBlockID()] = true; |
| 603 | else if (prevVal.equals(val)) |
| 604 | continue; |
| 605 | |
| 606 | prevVal = val; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 607 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 608 | // Update the dataflow value for the start of this block. |
| 609 | LV->blocksBeginToLiveness[block] = LV->runOnBlock(block, val); |
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 | // Enqueue the value to the predecessors. |
Ted Kremenek | 459597a | 2011-09-16 23:01:39 +0000 | [diff] [blame] | 612 | worklist.enqueuePredecessors(block); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 613 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 614 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 615 | return new LiveVariables(LV); |
| 616 | } |
| 617 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 618 | void LiveVariables::dumpBlockLiveness(const SourceManager &M) { |
| 619 | getImpl(impl).dumpBlockLiveness(M); |
| 620 | } |
| 621 | |
| 622 | void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) { |
| 623 | std::vector<const CFGBlock *> vec; |
| 624 | for (llvm::DenseMap<const CFGBlock *, LiveVariables::LivenessValues>::iterator |
| 625 | it = blocksEndToLiveness.begin(), ei = blocksEndToLiveness.end(); |
| 626 | it != ei; ++it) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 627 | vec.push_back(it->first); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 628 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 629 | llvm::sort(vec, [](const CFGBlock *A, const CFGBlock *B) { |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 630 | return A->getBlockID() < B->getBlockID(); |
| 631 | }); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 632 | |
| 633 | std::vector<const VarDecl*> declVec; |
| 634 | |
| 635 | for (std::vector<const CFGBlock *>::iterator |
| 636 | it = vec.begin(), ei = vec.end(); it != ei; ++it) { |
| 637 | llvm::errs() << "\n[ B" << (*it)->getBlockID() |
| 638 | << " (live variables at block exit) ]\n"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 640 | LiveVariables::LivenessValues vals = blocksEndToLiveness[*it]; |
| 641 | declVec.clear(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 643 | for (llvm::ImmutableSet<const VarDecl *>::iterator si = |
| 644 | vals.liveDecls.begin(), |
| 645 | se = vals.liveDecls.end(); si != se; ++si) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 646 | declVec.push_back(*si); |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 647 | } |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 648 | |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 649 | llvm::sort(declVec, [](const Decl *A, const Decl *B) { |
| 650 | return A->getBeginLoc() < B->getBeginLoc(); |
| 651 | }); |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 652 | |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 653 | for (std::vector<const VarDecl*>::iterator di = declVec.begin(), |
| 654 | de = declVec.end(); di != de; ++di) { |
| 655 | llvm::errs() << " " << (*di)->getDeclName().getAsString() |
| 656 | << " <"; |
Stephen Kelly | 3124ce7 | 2018-08-15 20:32:06 +0000 | [diff] [blame] | 657 | (*di)->getLocation().print(llvm::errs(), M); |
Daniel Dunbar | e81a553 | 2009-10-17 18:12:37 +0000 | [diff] [blame] | 658 | llvm::errs() << ">\n"; |
Ted Kremenek | b56a990 | 2007-09-06 00:17:54 +0000 | [diff] [blame] | 659 | } |
Ted Kremenek | 3f8ed26 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 660 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 661 | llvm::errs() << "\n"; |
Ted Kremenek | bd9cc5c | 2007-09-10 17:36:42 +0000 | [diff] [blame] | 662 | } |
Ted Kremenek | e9fda1e | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 663 | |
Artem Dergachev | dda4216 | 2018-12-16 23:44:06 +0000 | [diff] [blame] | 664 | void LiveVariables::dumpStmtLiveness(const SourceManager &M) { |
| 665 | getImpl(impl).dumpStmtLiveness(M); |
| 666 | } |
| 667 | |
| 668 | void LiveVariablesImpl::dumpStmtLiveness(const SourceManager &M) { |
| 669 | // Don't iterate over blockEndsToLiveness directly because it's not sorted. |
| 670 | for (auto I : *analysisContext.getCFG()) { |
| 671 | |
| 672 | llvm::errs() << "\n[ B" << I->getBlockID() |
| 673 | << " (live statements at block exit) ]\n"; |
| 674 | for (auto S : blocksEndToLiveness[I].liveStmts) { |
| 675 | llvm::errs() << "\n"; |
| 676 | S->dump(); |
| 677 | } |
| 678 | llvm::errs() << "\n"; |
| 679 | } |
| 680 | } |
| 681 | |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 682 | const void *LiveVariables::getTag() { static int x; return &x; } |
| 683 | const void *RelaxedLiveVariables::getTag() { static int x; return &x; } |