blob: 418b06e725c3d60dec250681dfd4598ace7f68bb [file] [log] [blame]
Ted Kremenek9153f732008-02-05 07:17:49 +00001//= 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 Kremenekf66ea2cd2008-02-04 21:59:22 +000014#include "ValueState.h"
15
16using namespace clang;
17
Ted Kremenek862d5bb2008-02-06 00:54:14 +000018bool 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
29const 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 Kremenekf66ea2cd2008-02-04 21:59:22 +000036RValue ValueStateManager::GetValue(const StateTy& St, const LValue& LV) {
37 switch (LV.getSubKind()) {
Ted Kremenek329f8542008-02-05 21:52:21 +000038 case lval::DeclValKind: {
Ted Kremenek9153f732008-02-05 07:17:49 +000039 StateTy::VariableBindingsTy::TreeTy* T =
Ted Kremenek329f8542008-02-05 21:52:21 +000040 St.getImpl()->VariableBindings.SlimFind(cast<lval::DeclVal>(LV).getDecl());
Ted Kremenek9153f732008-02-05 07:17:49 +000041
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000042 return T ? T->getValue().second : InvalidValue();
43 }
44 default:
45 assert (false && "Invalid LValue.");
46 break;
47 }
48
49 return InvalidValue();
50}
51
Ted Kremenek862d5bb2008-02-06 00:54:14 +000052ValueStateManager::StateTy
53ValueStateManager::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
72ValueStateManager::StateTy
73ValueStateManager::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 Kremenekf233d482008-02-05 00:26:40 +000082RValue ValueStateManager::GetValue(const StateTy& St, Stmt* S, bool* hasVal) {
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000083 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 Kremenek329f8542008-02-05 21:52:21 +000098 return GetValue(St, lval::DeclVal(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000099
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 Kremenek9153f732008-02-05 07:17:49 +0000137 StateTy::VariableBindingsTy::TreeTy* T =
138 St.getImpl()->VariableBindings.SlimFind(S);
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000139
Ted Kremenekf233d482008-02-05 00:26:40 +0000140 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 Kremenekf66ea2cd2008-02-04 21:59:22 +0000148}
149
150LValue 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 Kremenek329f8542008-02-05 21:52:21 +0000156 return lval::DeclVal(DR->getDecl());
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000157
158 return cast<LValue>(GetValue(St, S));
159}
160
161
162ValueStateManager::StateTy
163ValueStateManager::SetValue(StateTy St, Stmt* S, bool isBlkExpr,
164 const RValue& V) {
165
166 assert (S);
Ted Kremenek9153f732008-02-05 07:17:49 +0000167 return V.isValid() ? Add(St, VarBindKey(S, isBlkExpr), V) : St;
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000168}
169
170ValueStateManager::StateTy
171ValueStateManager::SetValue(StateTy St, const LValue& LV, const RValue& V) {
172
173 switch (LV.getSubKind()) {
Ted Kremenek329f8542008-02-05 21:52:21 +0000174 case lval::DeclValKind:
175 return V.isValid() ? Add(St, cast<lval::DeclVal>(LV).getDecl(), V)
176 : Remove(St, cast<lval::DeclVal>(LV).getDecl());
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000177
178 default:
179 assert ("SetValue for given LValue type not yet implemented.");
180 return St;
181 }
182}
183
Ted Kremenek9153f732008-02-05 07:17:49 +0000184ValueStateManager::StateTy
185ValueStateManager::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 Kremenekf66ea2cd2008-02-04 21:59:22 +0000194}
Ted Kremenek9153f732008-02-05 07:17:49 +0000195
196ValueStateManager::StateTy
197ValueStateManager::Add(StateTy St, VarBindKey K, const RValue& V) {
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000198
Ted Kremenek9153f732008-02-05 07:17:49 +0000199 // 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
209ValueStateManager::StateTy
210ValueStateManager::getInitialState() {
211
212 // Create a state with empty variable bindings.
Ted Kremenek174aea42008-02-05 18:51:06 +0000213 ValueStateImpl StateImpl(VBFactory.GetEmptyMap(),
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000214 CNEFactory.GetEmptyMap(),
215 CEFactory.GetEmptyMap());
Ted Kremenek9153f732008-02-05 07:17:49 +0000216
217 return getPersistentState(StateImpl);
218}
219
220ValueStateManager::StateTy
221ValueStateManager::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}