Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | //== RangeConstraintManager.cpp - Manage range constraints.------*- 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 | // This file defines RangeConstraintManager, a class that tracks simple |
| 11 | // equality and inequality constraints on symbolic values of GRState. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SimpleConstraintManager.h" |
| 16 | #include "clang/Checker/PathSensitive/GRState.h" |
| 17 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
| 18 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
| 19 | #include "clang/Checker/ManagerRegistry.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/ADT/FoldingSet.h" |
| 22 | #include "llvm/ADT/ImmutableSet.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { class ConstraintRange {}; } |
| 28 | static int ConstraintRangeIndex = 0; |
| 29 | |
| 30 | /// A Range represents the closed range [from, to]. The caller must |
| 31 | /// guarantee that from <= to. Note that Range is immutable, so as not |
| 32 | /// to subvert RangeSet's immutability. |
| 33 | namespace { |
| 34 | class Range : public std::pair<const llvm::APSInt*, |
| 35 | const llvm::APSInt*> { |
| 36 | public: |
| 37 | Range(const llvm::APSInt &from, const llvm::APSInt &to) |
| 38 | : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) { |
| 39 | assert(from <= to); |
| 40 | } |
| 41 | bool Includes(const llvm::APSInt &v) const { |
| 42 | return *first <= v && v <= *second; |
| 43 | } |
| 44 | const llvm::APSInt &From() const { |
| 45 | return *first; |
| 46 | } |
| 47 | const llvm::APSInt &To() const { |
| 48 | return *second; |
| 49 | } |
| 50 | const llvm::APSInt *getConcreteValue() const { |
| 51 | return &From() == &To() ? &From() : NULL; |
| 52 | } |
| 53 | |
| 54 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 55 | ID.AddPointer(&From()); |
| 56 | ID.AddPointer(&To()); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | class RangeTrait : public llvm::ImutContainerInfo<Range> { |
| 62 | public: |
| 63 | // When comparing if one Range is less than another, we should compare |
| 64 | // the actual APSInt values instead of their pointers. This keeps the order |
| 65 | // consistent (instead of comparing by pointer values) and can potentially |
| 66 | // be used to speed up some of the operations in RangeSet. |
| 67 | static inline bool isLess(key_type_ref lhs, key_type_ref rhs) { |
| 68 | return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) && |
| 69 | *lhs.second < *rhs.second); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | /// RangeSet contains a set of ranges. If the set is empty, then |
| 74 | /// there the value of a symbol is overly constrained and there are no |
| 75 | /// possible values for that symbol. |
| 76 | class RangeSet { |
| 77 | typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet; |
| 78 | PrimRangeSet ranges; // no need to make const, since it is an |
| 79 | // ImmutableSet - this allows default operator= |
| 80 | // to work. |
| 81 | public: |
| 82 | typedef PrimRangeSet::Factory Factory; |
| 83 | typedef PrimRangeSet::iterator iterator; |
| 84 | |
| 85 | RangeSet(PrimRangeSet RS) : ranges(RS) {} |
| 86 | RangeSet(Factory& F) : ranges(F.GetEmptySet()) {} |
| 87 | |
| 88 | iterator begin() const { return ranges.begin(); } |
| 89 | iterator end() const { return ranges.end(); } |
| 90 | |
| 91 | bool isEmpty() const { return ranges.isEmpty(); } |
| 92 | |
| 93 | /// Construct a new RangeSet representing '{ [from, to] }'. |
| 94 | RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to) |
| 95 | : ranges(F.Add(F.GetEmptySet(), Range(from, to))) {} |
| 96 | |
| 97 | /// Profile - Generates a hash profile of this RangeSet for use |
| 98 | /// by FoldingSet. |
| 99 | void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); } |
| 100 | |
| 101 | /// getConcreteValue - If a symbol is contrained to equal a specific integer |
| 102 | /// constant then this method returns that value. Otherwise, it returns |
| 103 | /// NULL. |
| 104 | const llvm::APSInt* getConcreteValue() const { |
| 105 | return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0; |
| 106 | } |
| 107 | |
| 108 | /// AddEQ - Create a new RangeSet with the additional constraint that the |
| 109 | /// value be equal to V. |
| 110 | RangeSet AddEQ(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 111 | // Search for a range that includes 'V'. If so, return a new RangeSet |
| 112 | // representing { [V, V] }. |
| 113 | for (PrimRangeSet::iterator i = begin(), e = end(); i!=e; ++i) |
| 114 | if (i->Includes(V)) |
| 115 | return RangeSet(F, V, V); |
| 116 | |
| 117 | return RangeSet(F); |
| 118 | } |
| 119 | |
| 120 | /// AddNE - Create a new RangeSet with the additional constraint that the |
| 121 | /// value be not be equal to V. |
| 122 | RangeSet AddNE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 123 | PrimRangeSet newRanges = ranges; |
| 124 | |
| 125 | // FIXME: We can perhaps enhance ImmutableSet to do this search for us |
| 126 | // in log(N) time using the sorted property of the internal AVL tree. |
| 127 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 128 | if (i->Includes(V)) { |
| 129 | // Remove the old range. |
| 130 | newRanges = F.Remove(newRanges, *i); |
| 131 | // Split the old range into possibly one or two ranges. |
| 132 | if (V != i->From()) |
| 133 | newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V))); |
| 134 | if (V != i->To()) |
| 135 | newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To())); |
| 136 | // All of the ranges are non-overlapping, so we can stop. |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return newRanges; |
| 142 | } |
| 143 | |
| 144 | /// AddNE - Create a new RangeSet with the additional constraint that the |
| 145 | /// value be less than V. |
| 146 | RangeSet AddLT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 147 | PrimRangeSet newRanges = F.GetEmptySet(); |
| 148 | |
| 149 | for (iterator i = begin(), e = end() ; i != e ; ++i) { |
| 150 | if (i->Includes(V) && i->From() < V) |
| 151 | newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V))); |
| 152 | else if (i->To() < V) |
| 153 | newRanges = F.Add(newRanges, *i); |
| 154 | } |
| 155 | |
| 156 | return newRanges; |
| 157 | } |
| 158 | |
| 159 | RangeSet AddLE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 160 | PrimRangeSet newRanges = F.GetEmptySet(); |
| 161 | |
| 162 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 163 | // Strictly we should test for includes *V + 1, but no harm is |
| 164 | // done by this formulation |
| 165 | if (i->Includes(V)) |
| 166 | newRanges = F.Add(newRanges, Range(i->From(), V)); |
| 167 | else if (i->To() <= V) |
| 168 | newRanges = F.Add(newRanges, *i); |
| 169 | } |
| 170 | |
| 171 | return newRanges; |
| 172 | } |
| 173 | |
| 174 | RangeSet AddGT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 175 | PrimRangeSet newRanges = F.GetEmptySet(); |
| 176 | |
| 177 | for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) { |
| 178 | if (i->Includes(V) && i->To() > V) |
| 179 | newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To())); |
| 180 | else if (i->From() > V) |
| 181 | newRanges = F.Add(newRanges, *i); |
| 182 | } |
| 183 | |
| 184 | return newRanges; |
| 185 | } |
| 186 | |
| 187 | RangeSet AddGE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 188 | PrimRangeSet newRanges = F.GetEmptySet(); |
| 189 | |
| 190 | for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) { |
| 191 | // Strictly we should test for includes *V - 1, but no harm is |
| 192 | // done by this formulation |
| 193 | if (i->Includes(V)) |
| 194 | newRanges = F.Add(newRanges, Range(V, i->To())); |
| 195 | else if (i->From() >= V) |
| 196 | newRanges = F.Add(newRanges, *i); |
| 197 | } |
| 198 | |
| 199 | return newRanges; |
| 200 | } |
| 201 | |
| 202 | void print(llvm::raw_ostream &os) const { |
| 203 | bool isFirst = true; |
| 204 | os << "{ "; |
| 205 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 206 | if (isFirst) |
| 207 | isFirst = false; |
| 208 | else |
| 209 | os << ", "; |
| 210 | |
| 211 | os << '[' << i->From().toString(10) << ", " << i->To().toString(10) |
| 212 | << ']'; |
| 213 | } |
| 214 | os << " }"; |
| 215 | } |
| 216 | |
| 217 | bool operator==(const RangeSet &other) const { |
| 218 | return ranges == other.ranges; |
| 219 | } |
| 220 | }; |
| 221 | } // end anonymous namespace |
| 222 | |
| 223 | typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy; |
| 224 | |
| 225 | namespace clang { |
| 226 | template<> |
| 227 | struct GRStateTrait<ConstraintRange> |
| 228 | : public GRStatePartialTrait<ConstraintRangeTy> { |
| 229 | static inline void* GDMIndex() { return &ConstraintRangeIndex; } |
| 230 | }; |
| 231 | } |
| 232 | |
| 233 | namespace { |
| 234 | class RangeConstraintManager : public SimpleConstraintManager{ |
| 235 | RangeSet GetRange(const GRState *state, SymbolRef sym); |
| 236 | public: |
| 237 | RangeConstraintManager(GRSubEngine &subengine) |
| 238 | : SimpleConstraintManager(subengine) {} |
| 239 | |
| 240 | const GRState* AssumeSymNE(const GRState* St, SymbolRef sym, |
| 241 | const llvm::APSInt& V); |
| 242 | |
| 243 | const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym, |
| 244 | const llvm::APSInt& V); |
| 245 | |
| 246 | const GRState* AssumeSymLT(const GRState* St, SymbolRef sym, |
| 247 | const llvm::APSInt& V); |
| 248 | |
| 249 | const GRState* AssumeSymGT(const GRState* St, SymbolRef sym, |
| 250 | const llvm::APSInt& V); |
| 251 | |
| 252 | const GRState* AssumeSymGE(const GRState* St, SymbolRef sym, |
| 253 | const llvm::APSInt& V); |
| 254 | |
| 255 | const GRState* AssumeSymLE(const GRState* St, SymbolRef sym, |
| 256 | const llvm::APSInt& V); |
| 257 | |
| 258 | const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const; |
| 259 | |
| 260 | // FIXME: Refactor into SimpleConstraintManager? |
| 261 | bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const { |
| 262 | const llvm::APSInt *i = getSymVal(St, sym); |
| 263 | return i ? *i == V : false; |
| 264 | } |
| 265 | |
| 266 | const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper); |
| 267 | |
| 268 | void print(const GRState* St, llvm::raw_ostream& Out, |
| 269 | const char* nl, const char *sep); |
| 270 | |
| 271 | private: |
| 272 | RangeSet::Factory F; |
| 273 | }; |
| 274 | |
| 275 | } // end anonymous namespace |
| 276 | |
| 277 | ConstraintManager* clang::CreateRangeConstraintManager(GRStateManager&, |
| 278 | GRSubEngine &subeng) { |
| 279 | return new RangeConstraintManager(subeng); |
| 280 | } |
| 281 | |
| 282 | const llvm::APSInt* RangeConstraintManager::getSymVal(const GRState* St, |
| 283 | SymbolRef sym) const { |
| 284 | const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym); |
| 285 | return T ? T->getConcreteValue() : NULL; |
| 286 | } |
| 287 | |
| 288 | /// Scan all symbols referenced by the constraints. If the symbol is not alive |
| 289 | /// as marked in LSymbols, mark it as dead in DSymbols. |
| 290 | const GRState* |
| 291 | RangeConstraintManager::RemoveDeadBindings(const GRState* state, |
| 292 | SymbolReaper& SymReaper) { |
| 293 | |
| 294 | ConstraintRangeTy CR = state->get<ConstraintRange>(); |
| 295 | ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>(); |
| 296 | |
| 297 | for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) { |
| 298 | SymbolRef sym = I.getKey(); |
| 299 | if (SymReaper.maybeDead(sym)) |
| 300 | CR = CRFactory.Remove(CR, sym); |
| 301 | } |
| 302 | |
| 303 | return state->set<ConstraintRange>(CR); |
| 304 | } |
| 305 | |
| 306 | //===------------------------------------------------------------------------=== |
| 307 | // AssumeSymX methods: public interface for RangeConstraintManager. |
| 308 | //===------------------------------------------------------------------------===/ |
| 309 | |
| 310 | RangeSet |
| 311 | RangeConstraintManager::GetRange(const GRState *state, SymbolRef sym) { |
| 312 | if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym)) |
| 313 | return *V; |
| 314 | |
| 315 | // Lazily generate a new RangeSet representing all possible values for the |
| 316 | // given symbol type. |
| 317 | QualType T = state->getSymbolManager().getType(sym); |
| 318 | BasicValueFactory& BV = state->getBasicVals(); |
| 319 | return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T)); |
| 320 | } |
| 321 | |
| 322 | //===------------------------------------------------------------------------=== |
| 323 | // AssumeSymX methods: public interface for RangeConstraintManager. |
| 324 | //===------------------------------------------------------------------------===/ |
| 325 | |
| 326 | #define AssumeX(OP)\ |
| 327 | const GRState*\ |
| 328 | RangeConstraintManager::AssumeSym ## OP(const GRState* state, SymbolRef sym,\ |
| 329 | const llvm::APSInt& V){\ |
| 330 | const RangeSet& R = GetRange(state, sym).Add##OP(state->getBasicVals(), F, V);\ |
| 331 | return !R.isEmpty() ? state->set<ConstraintRange>(sym, R) : NULL;\ |
| 332 | } |
| 333 | |
| 334 | AssumeX(EQ) |
| 335 | AssumeX(NE) |
| 336 | AssumeX(LT) |
| 337 | AssumeX(GT) |
| 338 | AssumeX(LE) |
| 339 | AssumeX(GE) |
| 340 | |
| 341 | //===------------------------------------------------------------------------=== |
| 342 | // Pretty-printing. |
| 343 | //===------------------------------------------------------------------------===/ |
| 344 | |
| 345 | void RangeConstraintManager::print(const GRState* St, llvm::raw_ostream& Out, |
| 346 | const char* nl, const char *sep) { |
| 347 | |
| 348 | ConstraintRangeTy Ranges = St->get<ConstraintRange>(); |
| 349 | |
| 350 | if (Ranges.isEmpty()) |
| 351 | return; |
| 352 | |
| 353 | Out << nl << sep << "ranges of symbol values:"; |
| 354 | |
| 355 | for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){ |
| 356 | Out << nl << ' ' << I.getKey() << " : "; |
| 357 | I.getData().print(Out); |
| 358 | } |
| 359 | } |