Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1 | //===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==// |
| 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 | // Constant Propagation via Graph Reachability |
| 11 | // |
| 12 | // This files defines a simple analysis that performs path-sensitive |
| 13 | // constant propagation within a function. An example use of this analysis |
| 14 | // is to perform simple checks for NULL dereferences. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | #include "clang/Analysis/Visitors/CFGStmtVisitor.h" |
| 22 | |
| 23 | #include "llvm/Support/Casting.h" |
| 24 | #include "llvm/Support/DataTypes.h" |
| 25 | #include "llvm/ADT/APSInt.h" |
| 26 | #include "llvm/ADT/FoldingSet.h" |
| 27 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
| 30 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 31 | #ifndef NDEBUG |
| 32 | #include "llvm/Support/GraphWriter.h" |
| 33 | #include <sstream> |
| 34 | #endif |
| 35 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using llvm::APInt; |
| 38 | using llvm::APFloat; |
| 39 | using llvm::dyn_cast; |
| 40 | using llvm::cast; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 43 | /// DSPtr - A variant smart pointer that wraps either a ValueDecl* or a |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | namespace { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 47 | class VISIBILITY_HIDDEN DSPtr { |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 48 | uintptr_t Raw; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 49 | public: |
Ted Kremenek | e3d7c24 | 2008-01-16 23:35:31 +0000 | [diff] [blame] | 50 | enum VariantKind { IsSubExp=0x0, IsValueDecl=0x1, IsBlkLvl=0x2, Flags=0x3 }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 51 | inline void* getPtr() const { return reinterpret_cast<void*>(Raw & ~Flags); } |
| 52 | inline VariantKind getKind() const { return (VariantKind) (Raw & Flags); } |
| 53 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 54 | DSPtr(ValueDecl* D) : Raw(reinterpret_cast<uintptr_t>(D) | IsValueDecl) {} |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 55 | DSPtr(Stmt* S, bool isBlkLvl) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 56 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkLvl ? IsBlkLvl : IsSubExp)) {} |
| 57 | |
| 58 | bool isSubExpr() const { return getKind() == IsSubExp; } |
| 59 | |
| 60 | inline void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 9849185 | 2008-01-16 05:51:13 +0000 | [diff] [blame] | 61 | ID.AddPointer(getPtr()); |
| 62 | ID.AddInteger((unsigned) getKind()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 63 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 64 | inline bool operator==(const DSPtr& X) const { return Raw == X.Raw; } |
| 65 | inline bool operator!=(const DSPtr& X) const { return Raw != X.Raw; } |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 66 | inline bool operator<(const DSPtr& X) const { |
| 67 | VariantKind k = getKind(), Xk = X.getKind(); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 68 | return k == Xk ? getPtr() < X.getPtr() : ((unsigned) k) < ((unsigned) Xk); |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 70 | }; |
| 71 | } // end anonymous namespace |
| 72 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 73 | // Machinery to get cast<> and dyn_cast<> working with DSPtr. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 74 | namespace llvm { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 75 | template<> inline bool isa<ValueDecl,DSPtr>(const DSPtr& V) { |
| 76 | return V.getKind() == DSPtr::IsValueDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 77 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 78 | template<> inline bool isa<Stmt,DSPtr>(const DSPtr& V) { |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 79 | return ((unsigned) V.getKind()) != DSPtr::IsValueDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 81 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,DSPtr> { |
| 82 | typedef const ValueDecl* ret_type; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 83 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 84 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 85 | typedef const Stmt* ret_type; |
| 86 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 87 | template<> struct VISIBILITY_HIDDEN simplify_type<DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 88 | typedef void* SimpleType; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 89 | static inline SimpleType getSimplifiedValue(const DSPtr &V) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 90 | return V.getPtr(); |
| 91 | } |
| 92 | }; |
| 93 | } // end llvm namespace |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // DeclStmtMapTy - A ImmutableMap type from Decl*/Stmt* to integers. |
| 97 | // |
| 98 | // FIXME: We may eventually use APSInt, or a mixture of APSInt and |
| 99 | // integer primitives to do this right; this will handle both |
| 100 | // different bit-widths and allow us to detect integer overflows, etc. |
| 101 | // |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 104 | typedef llvm::ImmutableMap<DSPtr,uint64_t> DeclStmtMapTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 105 | |
| 106 | namespace clang { |
| 107 | template<> |
| 108 | struct VISIBILITY_HIDDEN GRTrait<DeclStmtMapTy> { |
| 109 | static inline void* toPtr(DeclStmtMapTy M) { |
| 110 | return reinterpret_cast<void*>(M.getRoot()); |
| 111 | } |
| 112 | static inline DeclStmtMapTy toState(void* P) { |
| 113 | return DeclStmtMapTy(static_cast<DeclStmtMapTy::TreeTy*>(P)); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // The Checker! |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | namespace { |
| 123 | class VISIBILITY_HIDDEN ExprVariantTy { |
| 124 | const uint64_t val; |
| 125 | const bool isConstant; |
| 126 | public: |
| 127 | ExprVariantTy() : val(0), isConstant(false) {} |
| 128 | ExprVariantTy(uint64_t v) : val(v), isConstant(true) {} |
| 129 | |
| 130 | operator bool() const { return isConstant; } |
| 131 | uint64_t getVal() const { assert (isConstant); return val; } |
| 132 | |
| 133 | ExprVariantTy operator+(const ExprVariantTy& X) const { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 134 | return (isConstant && X.isConstant) ? ExprVariantTy(val + X.val) |
| 135 | : ExprVariantTy(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | ExprVariantTy operator-(const ExprVariantTy& X) const { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 139 | return (isConstant && X.isConstant) ? ExprVariantTy(val - X.val) |
| 140 | : ExprVariantTy(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | } // end anonymous namespace |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // The Checker! |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
| 149 | namespace { |
| 150 | class VISIBILITY_HIDDEN GRConstants : public CFGStmtVisitor<GRConstants> { |
| 151 | |
| 152 | public: |
| 153 | typedef DeclStmtMapTy StateTy; |
| 154 | typedef GRNodeBuilder<GRConstants> NodeBuilder; |
| 155 | typedef ExplodedNode<StateTy> NodeTy; |
| 156 | |
| 157 | protected: |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 158 | // Liveness - live-variables information the ValueDecl* and Expr* (block-level) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 159 | // in the CFG. Used to prune out dead state. |
| 160 | LiveVariables* Liveness; |
| 161 | |
| 162 | // Builder - The current GRNodeBuilder which is used when building the nodes |
| 163 | // for a given statement. |
| 164 | NodeBuilder* Builder; |
| 165 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 166 | DeclStmtMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 167 | |
| 168 | // cfg - the current CFG. |
| 169 | CFG* cfg; |
| 170 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 171 | typedef llvm::SmallVector<NodeTy*,8> NodeSetTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 172 | NodeSetTy NodeSetA; |
| 173 | NodeSetTy NodeSetB; |
| 174 | NodeSetTy* Nodes; |
| 175 | NodeSetTy* OldNodes; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 176 | StateTy CurrentState; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 177 | NodeTy* InitialPred; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 179 | public: |
| 180 | GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL), |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 181 | Nodes(&NodeSetA), OldNodes(&NodeSetB), |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 182 | CurrentState(StateMgr.GetEmptyMap()), InitialPred(NULL) {} |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 183 | |
| 184 | ~GRConstants() { delete Liveness; } |
| 185 | |
| 186 | CFG& getCFG() { assert (cfg); return *cfg; } |
| 187 | |
| 188 | void Initialize(CFG& c) { |
| 189 | cfg = &c; |
| 190 | Liveness = new LiveVariables(c); |
| 191 | Liveness->runOnCFG(c); |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 192 | Liveness->runOnAllBlocks(c, NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | StateTy getInitialState() { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 196 | return StateMgr.GetEmptyMap(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void ProcessStmt(Stmt* S, NodeBuilder& builder); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 200 | void SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 201 | void DoStmt(Stmt* S); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 202 | |
| 203 | StateTy RemoveDescendantMappings(Stmt* S, StateTy M, unsigned Levels=2); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 204 | StateTy RemoveDeadMappings(Stmt* S, StateTy M); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 205 | |
| 206 | void AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl = false); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 207 | void AddBinding(ValueDecl* D, ExprVariantTy V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 209 | ExprVariantTy GetBinding(Expr* E); |
| 210 | |
| 211 | void BlockStmt_VisitStmt(Stmt* S) { DoStmt(S); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 212 | |
| 213 | void VisitAssign(BinaryOperator* O); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 214 | void VisitBinAdd(BinaryOperator* O); |
| 215 | void VisitBinSub(BinaryOperator* O); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 216 | void VisitBinAssign(BinaryOperator* D); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 217 | }; |
| 218 | } // end anonymous namespace |
| 219 | |
| 220 | void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) { |
| 221 | Builder = &builder; |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 222 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 223 | Nodes->clear(); |
| 224 | OldNodes->clear(); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 225 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 226 | InitialPred = Builder->getLastNode(); |
| 227 | assert (InitialPred); |
| 228 | OldNodes->push_back(InitialPred); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 229 | |
| 230 | CurrentState = RemoveDeadMappings(S, InitialPred->getState()); |
| 231 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 232 | BlockStmt_Visit(S); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 233 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 234 | Builder = NULL; |
| 235 | } |
| 236 | |
| 237 | ExprVariantTy GRConstants::GetBinding(Expr* E) { |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 238 | DSPtr P(NULL); |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 239 | E = E->IgnoreParens(); |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 241 | switch (E->getStmtClass()) { |
| 242 | case Stmt::DeclRefExprClass: |
| 243 | P = DSPtr(cast<DeclRefExpr>(E)->getDecl()); |
| 244 | break; |
| 245 | |
| 246 | case Stmt::IntegerLiteralClass: |
| 247 | return cast<IntegerLiteral>(E)->getValue().getZExtValue(); |
| 248 | |
| 249 | default: |
| 250 | P = DSPtr(E, getCFG().isBlkExpr(E)); |
| 251 | break; |
| 252 | } |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 254 | StateTy::TreeTy* T = CurrentState.SlimFind(P); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 256 | if (!T) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 257 | return ExprVariantTy(); |
| 258 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 259 | return T->getValue().second; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void GRConstants::AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 263 | if (V) CurrentState = StateMgr.Add(CurrentState, DSPtr(E,isBlkLvl), V.getVal()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 266 | void GRConstants::AddBinding(ValueDecl* D, ExprVariantTy V) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 267 | if (V) CurrentState = StateMgr.Add(CurrentState, DSPtr(D), V.getVal()); |
| 268 | else CurrentState = StateMgr.Remove(CurrentState, DSPtr(D)); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 271 | void GRConstants::SwitchNodeSets() { |
| 272 | NodeSetTy* Tmp = OldNodes; |
| 273 | OldNodes = Nodes; |
| 274 | Nodes = Tmp; |
| 275 | Nodes->clear(); |
| 276 | } |
| 277 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 278 | GRConstants::StateTy GRConstants::RemoveDeadMappings(Stmt* Loc, StateTy M) { |
| 279 | #if 0 |
| 280 | return M; |
| 281 | #else |
| 282 | // Note: in the code below, we can assign a new map to M since the |
| 283 | // iterators are iterating over the tree of the *original* map. |
| 284 | |
| 285 | StateTy::iterator I = M.begin(), E = M.end(); |
| 286 | |
| 287 | // First remove mappings for subexpressions, since they are not needed. |
| 288 | for (; I!=E && I.getKey().getKind() == DSPtr::IsSubExp; ++I) |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 289 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 290 | |
| 291 | // Next, remove any decls or block-level expressions whose mappings are dead. |
| 292 | for (; I != E; ++I) { |
| 293 | if (ValueDecl* VD = dyn_cast<ValueDecl>(I.getKey())) { |
| 294 | if (VarDecl* V = dyn_cast<VarDecl>(VD)) { |
| 295 | if (!Liveness->isLive(Loc, V)) M = StateMgr.Remove(M, I.getKey()); |
| 296 | } |
| 297 | } |
| 298 | else { |
| 299 | Stmt* S = cast<Stmt>(I.getKey()); |
| 300 | assert (I.getKey().getKind() == DSPtr::IsBlkLvl); |
| 301 | if (!Liveness->isLive(Loc, S)) M = StateMgr.Remove(M, I.getKey()); |
| 302 | } |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | return M; |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 306 | #endif |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | GRConstants::StateTy |
| 311 | GRConstants::RemoveDescendantMappings(Stmt* S, GRConstants::StateTy State, |
| 312 | unsigned Levels) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 313 | typedef Stmt::child_iterator iterator; |
| 314 | |
| 315 | for (iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 316 | if (Stmt* C = *I) { |
| 317 | if (Levels == 1) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 318 | // Observe that this will only remove mappings to non-block level |
| 319 | // expressions. This is valid even if *CI is a block-level expression, |
| 320 | // since it simply won't be in the map in the first place. |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 321 | // Note: This should also work if 'C' is a block-level expression, |
| 322 | // although ideally we would want to skip processing C's children. |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 323 | State = StateMgr.Remove(State, DSPtr(C,false)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 325 | else { |
| 326 | if (ParenExpr* P = dyn_cast<ParenExpr>(C)) |
| 327 | State = RemoveDescendantMappings(P, State, Levels); |
| 328 | else |
| 329 | State = RemoveDescendantMappings(C, State, Levels-1); |
| 330 | } |
| 331 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 332 | |
| 333 | return State; |
| 334 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 335 | |
| 336 | void GRConstants::DoStmt(Stmt* S) { |
| 337 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 338 | if (*I) DoStmt(*I); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 339 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 340 | for (NodeSetTy::iterator I=OldNodes->begin(), E=OldNodes->end(); I!=E; ++I) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 341 | NodeTy* Pred = *I; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 342 | |
| 343 | if (Pred != InitialPred) |
| 344 | CurrentState = Pred->getState(); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 346 | StateTy OldState = CurrentState; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 347 | |
| 348 | if (Pred != InitialPred) |
| 349 | CurrentState = RemoveDescendantMappings(S, CurrentState); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 351 | Visit(S); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 353 | if (CurrentState != OldState) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 354 | NodeTy* N = Builder->generateNode(S, CurrentState, Pred); |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 355 | if (N) Nodes->push_back(N); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 356 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 357 | else Nodes->push_back(Pred); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 358 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 359 | |
| 360 | SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 363 | void GRConstants::VisitBinAdd(BinaryOperator* B) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 364 | AddBinding(B, GetBinding(B->getLHS()) + GetBinding(B->getRHS())); |
| 365 | } |
| 366 | |
| 367 | void GRConstants::VisitBinSub(BinaryOperator* B) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame^] | 368 | ExprVariantTy V1 = GetBinding(B->getLHS()); |
| 369 | ExprVariantTy V2 = GetBinding(B->getRHS()); |
| 370 | |
| 371 | AddBinding(B, V1 - V2 ); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 372 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 375 | void GRConstants::VisitBinAssign(BinaryOperator* B) { |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 376 | if (DeclRefExpr* D = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens())) { |
| 377 | ExprVariantTy V = GetBinding(B->getRHS()); |
| 378 | AddBinding(D->getDecl(), V); |
| 379 | AddBinding(B, V); |
| 380 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 383 | //===----------------------------------------------------------------------===// |
| 384 | // Driver. |
| 385 | //===----------------------------------------------------------------------===// |
| 386 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 387 | #ifndef NDEBUG |
| 388 | namespace llvm { |
| 389 | template<> |
| 390 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 391 | public DefaultDOTGraphTraits { |
| 392 | |
| 393 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 394 | std::ostringstream Out; |
| 395 | |
| 396 | Out << "Vertex: " << (void*) N << '\n'; |
| 397 | ProgramPoint Loc = N->getLocation(); |
| 398 | |
| 399 | switch (Loc.getKind()) { |
| 400 | case ProgramPoint::BlockEntranceKind: |
| 401 | Out << "Block Entrance: B" |
| 402 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 403 | break; |
| 404 | |
| 405 | case ProgramPoint::BlockExitKind: |
| 406 | assert (false); |
| 407 | break; |
| 408 | |
| 409 | case ProgramPoint::PostStmtKind: { |
| 410 | const PostStmt& L = cast<PostStmt>(Loc); |
| 411 | Out << "Stmt: " << (void*) L.getStmt() << '\n'; |
| 412 | L.getStmt()->printPretty(Out); |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | default: { |
| 417 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 418 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 419 | << E.getDst()->getBlockID() << ')'; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | Out << "\n{"; |
| 424 | |
| 425 | GRConstants::StateTy M = N->getState(); |
| 426 | bool isFirst = true; |
| 427 | |
| 428 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end(); I!=E; ++I) { |
| 429 | if (!isFirst) |
| 430 | Out << '\n'; |
| 431 | else |
| 432 | isFirst = false; |
| 433 | |
| 434 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) { |
| 435 | Out << "Decl: " << (void*) V << ", " << V->getName(); |
| 436 | } |
| 437 | else { |
| 438 | Stmt* E = cast<Stmt>(I.getKey()); |
| 439 | Out << "Stmt: " << (void*) E; |
| 440 | } |
| 441 | |
| 442 | Out << " => " << I.getData(); |
| 443 | } |
| 444 | |
| 445 | Out << " }"; |
| 446 | |
| 447 | return Out.str(); |
| 448 | } |
| 449 | }; |
| 450 | } // end llvm namespace |
| 451 | #endif |
| 452 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 453 | namespace clang { |
| 454 | void RunGRConstants(CFG& cfg) { |
| 455 | GREngine<GRConstants> Engine(cfg); |
| 456 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 457 | #ifndef NDEBUG |
| 458 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
| 459 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 460 | } |
| 461 | } |