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