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