Zhongxing Xu | d19e21b | 2008-08-29 15:09:12 +0000 | [diff] [blame] | 1 | //== BasicConstraintManager.cpp - Manage basic 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 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file defines BasicConstraintManager, a class that tracks simple |
Zhongxing Xu | d19e21b | 2008-08-29 15:09:12 +0000 | [diff] [blame] | 11 | // equality and inequality constraints on symbolic values of GRState. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 15 | #include "SimpleConstraintManager.h" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 16 | #include "clang/GR/PathSensitive/GRState.h" |
| 17 | #include "clang/GR/PathSensitive/GRStateTrait.h" |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 18 | #include "clang/GR/PathSensitive/TransferFuncs.h" |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 22 | using namespace GR; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 24 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 25 | namespace { class ConstNotEq {}; } |
| 26 | namespace { class ConstEq {}; } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 28 | typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy; |
| 29 | typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 31 | static int ConstEqIndex = 0; |
| 32 | static int ConstNotEqIndex = 0; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 34 | namespace clang { |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 35 | namespace GR { |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 36 | template<> |
| 37 | struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | static inline void* GDMIndex() { return &ConstNotEqIndex; } |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | template<> |
| 42 | struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | static inline void* GDMIndex() { return &ConstEqIndex; } |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 44 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | } |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 46 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 48 | namespace { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 49 | // BasicConstraintManager only tracks equality and inequality constraints of |
| 50 | // constants and integer variables. |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 51 | class BasicConstraintManager |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 52 | : public SimpleConstraintManager { |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 53 | GRState::IntSetTy::Factory ISetFactory; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 54 | public: |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 55 | BasicConstraintManager(GRStateManager &statemgr, SubEngine &subengine) |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 56 | : SimpleConstraintManager(subengine), |
| 57 | ISetFactory(statemgr.getAllocator()) {} |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 59 | const GRState *assumeSymNE(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 60 | const llvm::APSInt& V, |
| 61 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 63 | const GRState *assumeSymEQ(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 64 | const llvm::APSInt& V, |
| 65 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 67 | const GRState *assumeSymLT(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 68 | const llvm::APSInt& V, |
| 69 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 71 | const GRState *assumeSymGT(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 72 | const llvm::APSInt& V, |
| 73 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 75 | const GRState *assumeSymGE(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 76 | const llvm::APSInt& V, |
| 77 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 79 | const GRState *assumeSymLE(const GRState* state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 80 | const llvm::APSInt& V, |
| 81 | const llvm::APSInt& Adjustment); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 83 | const GRState* AddEQ(const GRState* state, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 85 | const GRState* AddNE(const GRState* state, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 87 | const llvm::APSInt* getSymVal(const GRState* state, SymbolRef sym) const; |
| 88 | bool isNotEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V) |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 89 | const; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 90 | bool isEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V) |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 91 | const; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 93 | const GRState* RemoveDeadBindings(const GRState* state, SymbolReaper& SymReaper); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 94 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | void print(const GRState* state, llvm::raw_ostream& Out, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 96 | const char* nl, const char *sep); |
| 97 | }; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 98 | |
| 99 | } // end anonymous namespace |
| 100 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 101 | ConstraintManager* GR::CreateBasicConstraintManager(GRStateManager& statemgr, |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 102 | SubEngine &subengine) { |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 103 | return new BasicConstraintManager(statemgr, subengine); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 106 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 107 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 108 | BasicConstraintManager::assumeSymNE(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 109 | const llvm::APSInt &V, |
| 110 | const llvm::APSInt &Adjustment) { |
| 111 | // First, determine if sym == X, where X+Adjustment != V. |
| 112 | llvm::APSInt Adjusted = V-Adjustment; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 113 | if (const llvm::APSInt* X = getSymVal(state, sym)) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 114 | bool isFeasible = (*X != Adjusted); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 115 | return isFeasible ? state : NULL; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 118 | // Second, determine if sym+Adjustment != V. |
| 119 | if (isNotEqual(state, sym, Adjusted)) |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 120 | return state; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 121 | |
| 122 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 123 | // Make that assumption. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 124 | return AddNE(state, sym, Adjusted); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 127 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 128 | BasicConstraintManager::assumeSymEQ(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 129 | const llvm::APSInt &V, |
| 130 | const llvm::APSInt &Adjustment) { |
| 131 | // First, determine if sym == X, where X+Adjustment != V. |
| 132 | llvm::APSInt Adjusted = V-Adjustment; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 133 | if (const llvm::APSInt* X = getSymVal(state, sym)) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 134 | bool isFeasible = (*X == Adjusted); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 135 | return isFeasible ? state : NULL; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 138 | // Second, determine if sym+Adjustment != V. |
| 139 | if (isNotEqual(state, sym, Adjusted)) |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 140 | return NULL; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 141 | |
| 142 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 143 | // Make that assumption. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 144 | return AddEQ(state, sym, Adjusted); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 147 | // The logic for these will be handled in another ConstraintManager. |
| 148 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 149 | BasicConstraintManager::assumeSymLT(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 150 | const llvm::APSInt &V, |
| 151 | const llvm::APSInt &Adjustment) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 152 | // Is 'V' the smallest possible value? |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 153 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 154 | // sym cannot be any value less than 'V'. This path is infeasible. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 155 | return NULL; |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 156 | } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 157 | |
| 158 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 159 | return assumeSymNE(state, sym, V, Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 162 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 163 | BasicConstraintManager::assumeSymGT(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 164 | const llvm::APSInt &V, |
| 165 | const llvm::APSInt &Adjustment) { |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 166 | // Is 'V' the largest possible value? |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 167 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) { |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 168 | // sym cannot be any value greater than 'V'. This path is infeasible. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 169 | return NULL; |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 172 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 173 | return assumeSymNE(state, sym, V, Adjustment); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 176 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 177 | BasicConstraintManager::assumeSymGE(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 178 | const llvm::APSInt &V, |
| 179 | const llvm::APSInt &Adjustment) { |
| 180 | // Reject a path if the value of sym is a constant X and !(X+Adj >= V). |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 181 | if (const llvm::APSInt *X = getSymVal(state, sym)) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 182 | bool isFeasible = (*X >= V-Adjustment); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 183 | return isFeasible ? state : NULL; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 186 | // Sym is not a constant, but it is worth looking to see if V is the |
| 187 | // maximum integer value. |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 188 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 189 | llvm::APSInt Adjusted = V-Adjustment; |
| 190 | |
| 191 | // If we know that sym != V (after adjustment), then this condition |
| 192 | // is infeasible since there is no other value greater than V. |
| 193 | bool isFeasible = !isNotEqual(state, sym, Adjusted); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 195 | // If the path is still feasible then as a consequence we know that |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 196 | // 'sym+Adjustment == V' because there are no larger values. |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 197 | // Add this constraint. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 198 | return isFeasible ? AddEQ(state, sym, Adjusted) : NULL; |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 199 | } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 201 | return state; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | const GRState* |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 205 | BasicConstraintManager::assumeSymLE(const GRState *state, SymbolRef sym, |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 206 | const llvm::APSInt &V, |
| 207 | const llvm::APSInt &Adjustment) { |
| 208 | // Reject a path if the value of sym is a constant X and !(X+Adj <= V). |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 209 | if (const llvm::APSInt* X = getSymVal(state, sym)) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 210 | bool isFeasible = (*X <= V-Adjustment); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 211 | return isFeasible ? state : NULL; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 214 | // Sym is not a constant, but it is worth looking to see if V is the |
| 215 | // minimum integer value. |
Chris Lattner | 071e04e | 2009-01-30 01:58:33 +0000 | [diff] [blame] | 216 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) { |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 217 | llvm::APSInt Adjusted = V-Adjustment; |
| 218 | |
| 219 | // If we know that sym != V (after adjustment), then this condition |
| 220 | // is infeasible since there is no other value less than V. |
| 221 | bool isFeasible = !isNotEqual(state, sym, Adjusted); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 223 | // If the path is still feasible then as a consequence we know that |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 224 | // 'sym+Adjustment == V' because there are no smaller values. |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 225 | // Add this constraint. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 226 | return isFeasible ? AddEQ(state, sym, Adjusted) : NULL; |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 229 | return state; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 230 | } |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 232 | const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 233 | const llvm::APSInt& V) { |
| 234 | // Create a new state with the old binding replaced. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 235 | return state->set<ConstEq>(sym, &state->getBasicVals().getValue(V)); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 238 | const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 239 | const llvm::APSInt& V) { |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 240 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 241 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 242 | ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym); |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 243 | GRState::IntSetTy S = T ? *T : ISetFactory.getEmptySet(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 245 | // Now add V to the NE set. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 246 | S = ISetFactory.add(S, &state->getBasicVals().getValue(V)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 248 | // Create a new state with the old binding replaced. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 249 | return state->set<ConstNotEq>(sym, S); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 252 | const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* state, |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 253 | SymbolRef sym) const { |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 254 | const ConstEqTy::data_type* T = state->get<ConstEq>(sym); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 255 | return T ? *T : NULL; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | bool BasicConstraintManager::isNotEqual(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 259 | const llvm::APSInt& V) const { |
| 260 | |
| 261 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 262 | const ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 263 | |
| 264 | // See if V is present in the NE-set. |
Jordy Rose | ba0f61c | 2010-06-18 22:49:11 +0000 | [diff] [blame] | 265 | return T ? T->contains(&state->getBasicVals().getValue(V)) : false; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 268 | bool BasicConstraintManager::isEqual(const GRState* state, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 269 | const llvm::APSInt& V) const { |
| 270 | // Retrieve the EQ-set associated with the given symbol. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 271 | const ConstEqTy::data_type* T = state->get<ConstEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 272 | // See if V is present in the EQ-set. |
| 273 | return T ? **T == V : false; |
| 274 | } |
| 275 | |
Zhongxing Xu | 8fd9b35 | 2008-11-27 02:39:34 +0000 | [diff] [blame] | 276 | /// Scan all symbols referenced by the constraints. If the symbol is not alive |
| 277 | /// as marked in LSymbols, mark it as dead in DSymbols. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 278 | const GRState* |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 279 | BasicConstraintManager::RemoveDeadBindings(const GRState* state, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 280 | SymbolReaper& SymReaper) { |
| 281 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 282 | ConstEqTy CE = state->get<ConstEq>(); |
| 283 | ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 284 | |
| 285 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 286 | SymbolRef sym = I.getKey(); |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 287 | if (SymReaper.maybeDead(sym)) |
| 288 | CE = CEFactory.remove(CE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 290 | state = state->set<ConstEq>(CE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 292 | ConstNotEqTy CNE = state->get<ConstNotEq>(); |
| 293 | ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 294 | |
| 295 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | SymbolRef sym = I.getKey(); |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 297 | if (SymReaper.maybeDead(sym)) |
| 298 | CNE = CNEFactory.remove(CNE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 301 | return state->set<ConstNotEq>(CNE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | void BasicConstraintManager::print(const GRState* state, llvm::raw_ostream& Out, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 305 | const char* nl, const char *sep) { |
| 306 | // Print equality constraints. |
| 307 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 308 | ConstEqTy CE = state->get<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 309 | |
| 310 | if (!CE.isEmpty()) { |
| 311 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 312 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) |
| 313 | Out << nl << " $" << I.getKey() << " : " << *I.getData(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Print != constraints. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 318 | ConstNotEqTy CNE = state->get<ConstNotEq>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 320 | if (!CNE.isEmpty()) { |
| 321 | Out << nl << sep << "'!=' constraints:"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 323 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
| 324 | Out << nl << " $" << I.getKey() << " : "; |
| 325 | bool isFirst = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
| 327 | GRState::IntSetTy::iterator J = I.getData().begin(), |
| 328 | EJ = I.getData().end(); |
| 329 | |
| 330 | for ( ; J != EJ; ++J) { |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 331 | if (isFirst) isFirst = false; |
| 332 | else Out << ", "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Zhongxing Xu | 7d94e26 | 2008-11-10 05:00:06 +0000 | [diff] [blame] | 334 | Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
Daniel Dunbar | 0e194dd | 2008-08-30 02:06:22 +0000 | [diff] [blame] | 338 | } |