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 | |
| 31 | using namespace clang; |
| 32 | using llvm::APInt; |
| 33 | using llvm::APFloat; |
| 34 | using llvm::dyn_cast; |
| 35 | using llvm::cast; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 38 | /// DSPtr - A variant smart pointer that wraps either a Decl* or a |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 39 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | namespace { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 42 | class VISIBILITY_HIDDEN DSPtr { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 43 | const uintptr_t Raw; |
| 44 | public: |
| 45 | enum VariantKind { IsDecl=0x1, IsBlkLvl=0x2, IsSubExp=0x3, Flags=0x3 }; |
| 46 | inline void* getPtr() const { return reinterpret_cast<void*>(Raw & ~Flags); } |
| 47 | inline VariantKind getKind() const { return (VariantKind) (Raw & Flags); } |
| 48 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 49 | DSPtr(Decl* D) : Raw(reinterpret_cast<uintptr_t>(D) | IsDecl) {} |
| 50 | DSPtr(Stmt* S, bool isBlkLvl) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 51 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkLvl ? IsBlkLvl : IsSubExp)) {} |
| 52 | |
| 53 | bool isSubExpr() const { return getKind() == IsSubExp; } |
| 54 | |
| 55 | inline void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 9849185 | 2008-01-16 05:51:13 +0000 | [diff] [blame] | 56 | ID.AddPointer(getPtr()); |
| 57 | ID.AddInteger((unsigned) getKind()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 58 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 59 | inline bool operator==(const DSPtr& X) const { return Raw == X.Raw; } |
| 60 | inline bool operator!=(const DSPtr& X) const { return Raw != X.Raw; } |
| 61 | inline bool operator<(const DSPtr& X) const { return Raw < X.Raw; } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 62 | }; |
| 63 | } // end anonymous namespace |
| 64 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 65 | // Machinery to get cast<> and dyn_cast<> working with DSPtr. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 66 | namespace llvm { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 67 | template<> inline bool isa<Decl,DSPtr>(const DSPtr& V) { |
| 68 | return V.getKind() == DSPtr::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 70 | template<> inline bool isa<Stmt,DSPtr>(const DSPtr& V) { |
| 71 | return ((unsigned) V.getKind()) > DSPtr::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 72 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 73 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Decl,DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 74 | typedef const Decl* ret_type; |
| 75 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 76 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 77 | typedef const Stmt* ret_type; |
| 78 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 79 | template<> struct VISIBILITY_HIDDEN simplify_type<DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 80 | typedef void* SimpleType; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 81 | static inline SimpleType getSimplifiedValue(const DSPtr &V) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 82 | return V.getPtr(); |
| 83 | } |
| 84 | }; |
| 85 | } // end llvm namespace |
| 86 | |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // DeclStmtMapTy - A ImmutableMap type from Decl*/Stmt* to integers. |
| 89 | // |
| 90 | // FIXME: We may eventually use APSInt, or a mixture of APSInt and |
| 91 | // integer primitives to do this right; this will handle both |
| 92 | // different bit-widths and allow us to detect integer overflows, etc. |
| 93 | // |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 96 | typedef llvm::ImmutableMap<DSPtr,uint64_t> DeclStmtMapTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 97 | |
| 98 | namespace clang { |
| 99 | template<> |
| 100 | struct VISIBILITY_HIDDEN GRTrait<DeclStmtMapTy> { |
| 101 | static inline void* toPtr(DeclStmtMapTy M) { |
| 102 | return reinterpret_cast<void*>(M.getRoot()); |
| 103 | } |
| 104 | static inline DeclStmtMapTy toState(void* P) { |
| 105 | return DeclStmtMapTy(static_cast<DeclStmtMapTy::TreeTy*>(P)); |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // The Checker! |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
| 114 | namespace { |
| 115 | class VISIBILITY_HIDDEN ExprVariantTy { |
| 116 | const uint64_t val; |
| 117 | const bool isConstant; |
| 118 | public: |
| 119 | ExprVariantTy() : val(0), isConstant(false) {} |
| 120 | ExprVariantTy(uint64_t v) : val(v), isConstant(true) {} |
| 121 | |
| 122 | operator bool() const { return isConstant; } |
| 123 | uint64_t getVal() const { assert (isConstant); return val; } |
| 124 | |
| 125 | ExprVariantTy operator+(const ExprVariantTy& X) const { |
| 126 | if (!isConstant || !X.isConstant) return ExprVariantTy(); |
| 127 | else return ExprVariantTy(val+X.val); |
| 128 | } |
| 129 | |
| 130 | ExprVariantTy operator-(const ExprVariantTy& X) const { |
| 131 | if (!isConstant || !X.isConstant) return ExprVariantTy(); |
| 132 | else return ExprVariantTy(val+X.val); |
| 133 | } |
| 134 | }; |
| 135 | } // end anonymous namespace |
| 136 | |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | // The Checker! |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
| 141 | namespace { |
| 142 | class VISIBILITY_HIDDEN GRConstants : public CFGStmtVisitor<GRConstants> { |
| 143 | |
| 144 | public: |
| 145 | typedef DeclStmtMapTy StateTy; |
| 146 | typedef GRNodeBuilder<GRConstants> NodeBuilder; |
| 147 | typedef ExplodedNode<StateTy> NodeTy; |
| 148 | |
| 149 | protected: |
| 150 | // Liveness - live-variables information the Decl* and Expr* (block-level) |
| 151 | // in the CFG. Used to prune out dead state. |
| 152 | LiveVariables* Liveness; |
| 153 | |
| 154 | // Builder - The current GRNodeBuilder which is used when building the nodes |
| 155 | // for a given statement. |
| 156 | NodeBuilder* Builder; |
| 157 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 158 | DeclStmtMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 159 | |
| 160 | // cfg - the current CFG. |
| 161 | CFG* cfg; |
| 162 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 163 | typedef llvm::SmallVector<NodeTy*,8> NodeSetTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 164 | NodeSetTy NodeSetA; |
| 165 | NodeSetTy NodeSetB; |
| 166 | NodeSetTy* Nodes; |
| 167 | NodeSetTy* OldNodes; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 168 | StateTy CurrentState; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 170 | public: |
| 171 | GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL), |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 172 | Nodes(&NodeSetA), OldNodes(&NodeSetB), |
| 173 | CurrentState(StateMgr.GetEmptyMap()) {} |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 174 | |
| 175 | ~GRConstants() { delete Liveness; } |
| 176 | |
| 177 | CFG& getCFG() { assert (cfg); return *cfg; } |
| 178 | |
| 179 | void Initialize(CFG& c) { |
| 180 | cfg = &c; |
| 181 | Liveness = new LiveVariables(c); |
| 182 | Liveness->runOnCFG(c); |
| 183 | } |
| 184 | |
| 185 | StateTy getInitialState() { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 186 | return StateMgr.GetEmptyMap(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void ProcessStmt(Stmt* S, NodeBuilder& builder); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 190 | void SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 191 | void DoStmt(Stmt* S); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 192 | StateTy RemoveGrandchildrenMappings(Stmt* S, StateTy M); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 193 | |
| 194 | void AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl = false); |
| 195 | ExprVariantTy GetBinding(Expr* E); |
| 196 | |
| 197 | void BlockStmt_VisitStmt(Stmt* S) { DoStmt(S); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 198 | |
| 199 | void VisitAssign(BinaryOperator* O); |
| 200 | void VisitIntegerLiteral(IntegerLiteral* L); |
| 201 | void VisitBinAdd(BinaryOperator* O); |
| 202 | void VisitBinSub(BinaryOperator* O); |
| 203 | }; |
| 204 | } // end anonymous namespace |
| 205 | |
| 206 | void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) { |
| 207 | Builder = &builder; |
| 208 | Nodes->clear(); |
| 209 | OldNodes->clear(); |
| 210 | NodeTy* N = Builder->getLastNode(); |
| 211 | assert (N); |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 212 | OldNodes->push_back(N); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 213 | BlockStmt_Visit(S); |
| 214 | Builder = NULL; |
| 215 | } |
| 216 | |
| 217 | ExprVariantTy GRConstants::GetBinding(Expr* E) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 218 | DSPtr P(E, getCFG().isBlkExpr(E)); |
| 219 | StateTy::iterator I = CurrentState.find(P); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 221 | if (I == CurrentState.end()) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 222 | return ExprVariantTy(); |
| 223 | |
| 224 | return (*I).second; |
| 225 | } |
| 226 | |
| 227 | void GRConstants::AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 228 | CurrentState = StateMgr.Add(CurrentState, DSPtr(E,isBlkLvl), V.getVal()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void GRConstants::SwitchNodeSets() { |
| 232 | NodeSetTy* Tmp = OldNodes; |
| 233 | OldNodes = Nodes; |
| 234 | Nodes = Tmp; |
| 235 | Nodes->clear(); |
| 236 | } |
| 237 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 238 | GRConstants::StateTy |
| 239 | GRConstants::RemoveGrandchildrenMappings(Stmt* S, GRConstants::StateTy State) { |
| 240 | |
| 241 | typedef Stmt::child_iterator iterator; |
| 242 | |
| 243 | for (iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
| 244 | if (Stmt* C = *I) |
| 245 | for (iterator CI=C->child_begin(), CE=C->child_end(); CI!=CE; ++CI) { |
| 246 | // Observe that this will only remove mappings to non-block level |
| 247 | // expressions. This is valid even if *CI is a block-level expression, |
| 248 | // 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] | 249 | // Note: This should also work if 'C' is a block-level expression, |
| 250 | // although ideally we would want to skip processing C's children. |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 251 | State = StateMgr.Remove(State, DSPtr(*CI,false)); |
| 252 | } |
| 253 | |
| 254 | return State; |
| 255 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 256 | |
| 257 | void GRConstants::DoStmt(Stmt* S) { |
| 258 | 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] | 259 | if (*I) DoStmt(*I); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 261 | for (NodeSetTy::iterator I=OldNodes->begin(), E=OldNodes->end(); I!=E; ++I) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 262 | NodeTy* Pred = *I; |
| 263 | CurrentState = Pred->getState(); |
| 264 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 265 | StateTy OldState = CurrentState; |
| 266 | CurrentState = RemoveGrandchildrenMappings(S, CurrentState); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 268 | Visit(S); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 270 | if (CurrentState != OldState) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 271 | NodeTy* N = Builder->generateNode(S, CurrentState, Pred); |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 272 | if (N) Nodes->push_back(N); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 273 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 274 | else Nodes->push_back(Pred); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 275 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 276 | |
| 277 | SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void GRConstants::VisitIntegerLiteral(IntegerLiteral* L) { |
| 281 | AddBinding(L, L->getValue().getZExtValue()); |
| 282 | } |
| 283 | |
| 284 | void GRConstants::VisitBinAdd(BinaryOperator* B) { |
| 285 | AddBinding(B, GetBinding(B->getLHS()) + GetBinding(B->getRHS())); |
| 286 | } |
| 287 | |
| 288 | void GRConstants::VisitBinSub(BinaryOperator* B) { |
| 289 | AddBinding(B, GetBinding(B->getLHS()) - GetBinding(B->getRHS())); |
| 290 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame^] | 291 | |
| 292 | //===----------------------------------------------------------------------===// |
| 293 | // Driver. |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | |
| 296 | namespace clang { |
| 297 | void RunGRConstants(CFG& cfg) { |
| 298 | GREngine<GRConstants> Engine(cfg); |
| 299 | Engine.ExecuteWorkList(); |
| 300 | } |
| 301 | } |