Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1 | //= ValueState.cpp - Path-Sens. "State" for tracking valuues -----*- 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 files defines SymbolID, VarBindKey, and ValueState. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 14 | #include "ValueState.h" |
| 15 | |
| 16 | using namespace clang; |
| 17 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame^] | 18 | bool ValueState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
| 19 | // First, retrieve the NE-set associated with the given symbol. |
| 20 | ConstantNotEqTy::TreeTy* T = Data->ConstantNotEq.SlimFind(sym); |
| 21 | |
| 22 | if (!T) |
| 23 | return false; |
| 24 | |
| 25 | // Second, see if V is present in the NE-set. |
| 26 | return T->getValue().second.contains(&V); |
| 27 | } |
| 28 | |
| 29 | const llvm::APSInt* ValueState::getSymVal(SymbolID sym) const { |
| 30 | ConstantEqTy::TreeTy* T = Data->ConstantEq.SlimFind(sym); |
| 31 | return T ? T->getValue().second : NULL; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 36 | RValue ValueStateManager::GetValue(const StateTy& St, const LValue& LV) { |
| 37 | switch (LV.getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 38 | case lval::DeclValKind: { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 39 | StateTy::VariableBindingsTy::TreeTy* T = |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 40 | St.getImpl()->VariableBindings.SlimFind(cast<lval::DeclVal>(LV).getDecl()); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 42 | return T ? T->getValue().second : InvalidValue(); |
| 43 | } |
| 44 | default: |
| 45 | assert (false && "Invalid LValue."); |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | return InvalidValue(); |
| 50 | } |
| 51 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame^] | 52 | ValueStateManager::StateTy |
| 53 | ValueStateManager::AddNE(StateTy St, SymbolID sym, const llvm::APSInt& V) { |
| 54 | // First, retrieve the NE-set associated with the given symbol. |
| 55 | ValueState::ConstantNotEqTy::TreeTy* T = |
| 56 | St.getImpl()->ConstantNotEq.SlimFind(sym); |
| 57 | |
| 58 | ValueState::IntSetTy S = T ? T->getValue().second : ISetFactory.GetEmptySet(); |
| 59 | |
| 60 | // Now add V to the NE set. |
| 61 | S = ISetFactory.Add(S, &V); |
| 62 | |
| 63 | // Create a new state with the old binding replaced. |
| 64 | ValueStateImpl NewStateImpl = *St.getImpl(); |
| 65 | NewStateImpl.ConstantNotEq = CNEFactory.Add(NewStateImpl.ConstantNotEq, |
| 66 | sym, S); |
| 67 | |
| 68 | // Get the persistent copy. |
| 69 | return getPersistentState(NewStateImpl); |
| 70 | } |
| 71 | |
| 72 | ValueStateManager::StateTy |
| 73 | ValueStateManager::AddEQ(StateTy St, SymbolID sym, const llvm::APSInt& V) { |
| 74 | // Create a new state with the old binding replaced. |
| 75 | ValueStateImpl NewStateImpl = *St.getImpl(); |
| 76 | NewStateImpl.ConstantEq = CEFactory.Add(NewStateImpl.ConstantEq, sym, &V); |
| 77 | |
| 78 | // Get the persistent copy. |
| 79 | return getPersistentState(NewStateImpl); |
| 80 | } |
| 81 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 82 | RValue ValueStateManager::GetValue(const StateTy& St, Stmt* S, bool* hasVal) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 83 | for (;;) { |
| 84 | switch (S->getStmtClass()) { |
| 85 | |
| 86 | // ParenExprs are no-ops. |
| 87 | |
| 88 | case Stmt::ParenExprClass: |
| 89 | S = cast<ParenExpr>(S)->getSubExpr(); |
| 90 | continue; |
| 91 | |
| 92 | // DeclRefExprs can either evaluate to an LValue or a Non-LValue |
| 93 | // (assuming an implicit "load") depending on the context. In this |
| 94 | // context we assume that we are retrieving the value contained |
| 95 | // within the referenced variables. |
| 96 | |
| 97 | case Stmt::DeclRefExprClass: |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 98 | return GetValue(St, lval::DeclVal(cast<DeclRefExpr>(S)->getDecl())); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 99 | |
| 100 | // Integer literals evaluate to an RValue. Simply retrieve the |
| 101 | // RValue for the literal. |
| 102 | |
| 103 | case Stmt::IntegerLiteralClass: |
| 104 | return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S)); |
| 105 | |
| 106 | // Casts where the source and target type are the same |
| 107 | // are no-ops. We blast through these to get the descendant |
| 108 | // subexpression that has a value. |
| 109 | |
| 110 | case Stmt::ImplicitCastExprClass: { |
| 111 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 112 | if (C->getType() == C->getSubExpr()->getType()) { |
| 113 | S = C->getSubExpr(); |
| 114 | continue; |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | case Stmt::CastExprClass: { |
| 120 | CastExpr* C = cast<CastExpr>(S); |
| 121 | if (C->getType() == C->getSubExpr()->getType()) { |
| 122 | S = C->getSubExpr(); |
| 123 | continue; |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | // Handle all other Stmt* using a lookup. |
| 129 | |
| 130 | default: |
| 131 | break; |
| 132 | }; |
| 133 | |
| 134 | break; |
| 135 | } |
| 136 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 137 | StateTy::VariableBindingsTy::TreeTy* T = |
| 138 | St.getImpl()->VariableBindings.SlimFind(S); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 140 | if (T) { |
| 141 | if (hasVal) *hasVal = true; |
| 142 | return T->getValue().second; |
| 143 | } |
| 144 | else { |
| 145 | if (hasVal) *hasVal = false; |
| 146 | return InvalidValue(); |
| 147 | } |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | LValue ValueStateManager::GetLValue(const StateTy& St, Stmt* S) { |
| 151 | |
| 152 | while (ParenExpr* P = dyn_cast<ParenExpr>(S)) |
| 153 | S = P->getSubExpr(); |
| 154 | |
| 155 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 156 | return lval::DeclVal(DR->getDecl()); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 157 | |
| 158 | return cast<LValue>(GetValue(St, S)); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | ValueStateManager::StateTy |
| 163 | ValueStateManager::SetValue(StateTy St, Stmt* S, bool isBlkExpr, |
| 164 | const RValue& V) { |
| 165 | |
| 166 | assert (S); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 167 | return V.isValid() ? Add(St, VarBindKey(S, isBlkExpr), V) : St; |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ValueStateManager::StateTy |
| 171 | ValueStateManager::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 172 | |
| 173 | switch (LV.getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 174 | case lval::DeclValKind: |
| 175 | return V.isValid() ? Add(St, cast<lval::DeclVal>(LV).getDecl(), V) |
| 176 | : Remove(St, cast<lval::DeclVal>(LV).getDecl()); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 177 | |
| 178 | default: |
| 179 | assert ("SetValue for given LValue type not yet implemented."); |
| 180 | return St; |
| 181 | } |
| 182 | } |
| 183 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 184 | ValueStateManager::StateTy |
| 185 | ValueStateManager::Remove(StateTy St, VarBindKey K) { |
| 186 | |
| 187 | // Create a new state with the old binding removed. |
| 188 | ValueStateImpl NewStateImpl = *St.getImpl(); |
| 189 | NewStateImpl.VariableBindings = |
| 190 | VBFactory.Remove(NewStateImpl.VariableBindings, K); |
| 191 | |
| 192 | // Get the persistent copy. |
| 193 | return getPersistentState(NewStateImpl); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 194 | } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 195 | |
| 196 | ValueStateManager::StateTy |
| 197 | ValueStateManager::Add(StateTy St, VarBindKey K, const RValue& V) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 199 | // Create a new state with the old binding removed. |
| 200 | ValueStateImpl NewStateImpl = *St.getImpl(); |
| 201 | NewStateImpl.VariableBindings = |
| 202 | VBFactory.Add(NewStateImpl.VariableBindings, K, V); |
| 203 | |
| 204 | // Get the persistent copy. |
| 205 | return getPersistentState(NewStateImpl); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | ValueStateManager::StateTy |
| 210 | ValueStateManager::getInitialState() { |
| 211 | |
| 212 | // Create a state with empty variable bindings. |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 213 | ValueStateImpl StateImpl(VBFactory.GetEmptyMap(), |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame^] | 214 | CNEFactory.GetEmptyMap(), |
| 215 | CEFactory.GetEmptyMap()); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 216 | |
| 217 | return getPersistentState(StateImpl); |
| 218 | } |
| 219 | |
| 220 | ValueStateManager::StateTy |
| 221 | ValueStateManager::getPersistentState(const ValueStateImpl &State) { |
| 222 | |
| 223 | llvm::FoldingSetNodeID ID; |
| 224 | State.Profile(ID); |
| 225 | void* InsertPos; |
| 226 | |
| 227 | if (ValueStateImpl* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
| 228 | return I; |
| 229 | |
| 230 | ValueStateImpl* I = (ValueStateImpl*) Alloc.Allocate<ValueState>(); |
| 231 | new (I) ValueStateImpl(State); |
| 232 | StateSet.InsertNode(I, InsertPos); |
| 233 | return I; |
| 234 | } |