blob: 1cf695438bb8b2617d26c15ec08dfb878ce0b6b1 [file] [log] [blame]
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00001//= ValueState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
Ted Kremenek9153f732008-02-05 07:17:49 +00002//
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//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines SymbolID, ExprBindKey, and ValueState*
Ted Kremenek9153f732008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek0f5f0592008-02-27 06:07:00 +000014#include "clang/Analysis/PathSensitive/ValueState.h"
Ted Kremenek90e14812008-02-14 23:25:54 +000015#include "llvm/ADT/SmallSet.h"
Ted Kremenek729a9a22008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000017
18using namespace clang;
19
Ted Kremenek862d5bb2008-02-06 00:54:14 +000020bool ValueState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000021
22 // Retrieve the NE-set associated with the given symbol.
Ted Kremeneke8fdc832008-07-07 16:21:19 +000023 const ConstNotEqTy::data_type* T = ConstNotEq.lookup(sym);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000024
25 // See if V is present in the NE-set.
Ted Kremeneke8fdc832008-07-07 16:21:19 +000026 return T ? T->contains(&V) : false;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000027}
28
Ted Kremenek584def72008-07-22 00:46:16 +000029bool ValueState::isEqual(SymbolID sym, const llvm::APSInt& V) const {
30
31 // Retrieve the EQ-set associated with the given symbol.
32 const ConstEqTy::data_type* T = ConstEq.lookup(sym);
33
34 // See if V is present in the EQ-set.
35 return T ? **T == V : false;
36}
37
Ted Kremenek862d5bb2008-02-06 00:54:14 +000038const llvm::APSInt* ValueState::getSymVal(SymbolID sym) const {
Ted Kremeneke8fdc832008-07-07 16:21:19 +000039 ConstEqTy::data_type* T = ConstEq.lookup(sym);
40 return T ? *T : NULL;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000041}
42
Ted Kremenek4323a572008-07-10 22:03:41 +000043const ValueState*
44ValueStateManager::RemoveDeadBindings(const ValueState* St, Stmt* Loc,
Ted Kremenek77d7ef82008-04-24 18:31:42 +000045 const LiveVariables& Liveness,
Ted Kremenekf59bf482008-07-17 18:38:48 +000046 DeadSymbolsTy& DSymbols) {
Ted Kremenekb87d9092008-02-08 19:17:19 +000047
48 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
49 // The roots are any Block-level exprs and Decls that our liveness algorithm
50 // tells us are live. We then see what Decls they may reference, and keep
51 // those around. This code more than likely can be made faster, and the
52 // frequency of which this method is called should be experimented with
Ted Kremenekf59bf482008-07-17 18:38:48 +000053 // for optimum performance.
54 DRoots.clear();
55 StoreManager::LiveSymbolsTy LSymbols;
Ted Kremeneke7d22112008-02-11 19:21:59 +000056
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000057 ValueState NewSt = *St;
Ted Kremenekf59bf482008-07-17 18:38:48 +000058
59 // FIXME: Put this in environment.
60 // Clean up the environment.
Ted Kremeneke7d22112008-02-11 19:21:59 +000061
62 // Drop bindings for subexpressions.
Ted Kremenek8133a262008-07-08 21:46:56 +000063 NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env);
Ted Kremeneke7d22112008-02-11 19:21:59 +000064
65 // Iterate over the block-expr bindings.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000066
Ted Kremenekaed9b6a2008-02-28 10:21:43 +000067 for (ValueState::beb_iterator I = St->beb_begin(), E = St->beb_end();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000068 I!=E ; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +000069 Expr* BlkExpr = I.getKey();
Ted Kremenekb87d9092008-02-08 19:17:19 +000070
Ted Kremeneke7d22112008-02-11 19:21:59 +000071 if (Liveness.isLive(Loc, BlkExpr)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000072 RVal X = I.getData();
Ted Kremenek90e14812008-02-14 23:25:54 +000073
74 if (isa<lval::DeclVal>(X)) {
75 lval::DeclVal LV = cast<lval::DeclVal>(X);
Ted Kremenekf59bf482008-07-17 18:38:48 +000076 DRoots.push_back(LV.getDecl());
Ted Kremenekb87d9092008-02-08 19:17:19 +000077 }
Ted Kremenek90e14812008-02-14 23:25:54 +000078
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000079 for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
80 SI != SE; ++SI) {
Ted Kremenekf59bf482008-07-17 18:38:48 +000081 LSymbols.insert(*SI);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000082 }
Ted Kremenekb87d9092008-02-08 19:17:19 +000083 }
Ted Kremenek05a23782008-02-26 19:05:15 +000084 else {
85 RVal X = I.getData();
86
Ted Kremenek4a4e5242008-02-28 09:25:22 +000087 if (X.isUndef() && cast<UndefinedVal>(X).getData())
Ted Kremenek05a23782008-02-26 19:05:15 +000088 continue;
89
Ted Kremenek8133a262008-07-08 21:46:56 +000090 NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, BlkExpr);
Ted Kremenek05a23782008-02-26 19:05:15 +000091 }
Ted Kremenekb87d9092008-02-08 19:17:19 +000092 }
Ted Kremenek016f52f2008-02-08 21:10:02 +000093
Ted Kremenekf59bf482008-07-17 18:38:48 +000094 // Clean up the store.
95 DSymbols.clear();
96 NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness, DRoots,
97 LSymbols, DSymbols);
Ted Kremenekb87d9092008-02-08 19:17:19 +000098
Ted Kremenekf59bf482008-07-17 18:38:48 +000099 // Remove the dead symbols from the symbol tracker.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000100 for (ValueState::ce_iterator I = St->ce_begin(), E=St->ce_end(); I!=E; ++I) {
101
102 SymbolID sym = I.getKey();
103
Ted Kremenekf59bf482008-07-17 18:38:48 +0000104 if (!LSymbols.count(sym)) {
105 DSymbols.insert(sym);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000106 NewSt.ConstEq = CEFactory.Remove(NewSt.ConstEq, sym);
107 }
108 }
109
110 for (ValueState::cne_iterator I = St->cne_begin(), E=St->cne_end(); I!=E;++I){
111
112 SymbolID sym = I.getKey();
113
Ted Kremenekf59bf482008-07-17 18:38:48 +0000114 if (!LSymbols.count(sym)) {
115 DSymbols.insert(sym);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000116 NewSt.ConstNotEq = CNEFactory.Remove(NewSt.ConstNotEq, sym);
117 }
118 }
Ted Kremenek90e14812008-02-14 23:25:54 +0000119
Ted Kremeneke7d22112008-02-11 19:21:59 +0000120 return getPersistentState(NewSt);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000121}
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000122
Ted Kremenek4323a572008-07-10 22:03:41 +0000123const ValueState* ValueStateManager::SetRVal(const ValueState* St, LVal LV,
124 RVal V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000125
Ted Kremenek4323a572008-07-10 22:03:41 +0000126 Store OldStore = St->getStore();
127 Store NewStore = StMgr->SetRVal(OldStore, LV, V);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000128
Ted Kremenek4323a572008-07-10 22:03:41 +0000129 if (NewStore == OldStore)
130 return St;
Ted Kremenek692416c2008-02-18 22:57:02 +0000131
Ted Kremenek4323a572008-07-10 22:03:41 +0000132 ValueState NewSt = *St;
133 NewSt.St = NewStore;
134 return getPersistentState(NewSt);
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000135}
136
Ted Kremenek4323a572008-07-10 22:03:41 +0000137const ValueState* ValueStateManager::Unbind(const ValueState* St, LVal LV) {
138 Store OldStore = St->getStore();
139 Store NewStore = StMgr->Remove(OldStore, LV);
140
141 if (NewStore == OldStore)
142 return St;
143
144 ValueState NewSt = *St;
145 NewSt.St = NewStore;
146 return getPersistentState(NewSt);
147}
148
149
150const ValueState* ValueStateManager::AddNE(const ValueState* St, SymbolID sym,
151 const llvm::APSInt& V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000152
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000153 // First, retrieve the NE-set associated with the given symbol.
Ted Kremeneke8fdc832008-07-07 16:21:19 +0000154 ValueState::ConstNotEqTy::data_type* T = St->ConstNotEq.lookup(sym);
155 ValueState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000156
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000157 // Now add V to the NE set.
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000158 S = ISetFactory.Add(S, &V);
159
160 // Create a new state with the old binding replaced.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000161 ValueState NewSt = *St;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000162 NewSt.ConstNotEq = CNEFactory.Add(NewSt.ConstNotEq, sym, S);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000163
164 // Get the persistent copy.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000165 return getPersistentState(NewSt);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000166}
167
Ted Kremenek4323a572008-07-10 22:03:41 +0000168const ValueState* ValueStateManager::AddEQ(const ValueState* St, SymbolID sym,
169 const llvm::APSInt& V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000170
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000171 // Create a new state with the old binding replaced.
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000172 ValueState NewSt = *St;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000173 NewSt.ConstEq = CEFactory.Add(NewSt.ConstEq, sym, &V);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000174
175 // Get the persistent copy.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000176 return getPersistentState(NewSt);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000177}
178
Ted Kremenek4323a572008-07-10 22:03:41 +0000179const ValueState* ValueStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000180
Ted Kremenek8133a262008-07-08 21:46:56 +0000181 ValueState StateImpl(EnvMgr.getInitialEnvironment(),
Ted Kremenek4323a572008-07-10 22:03:41 +0000182 StMgr->getInitialStore(),
Ted Kremenek8133a262008-07-08 21:46:56 +0000183 CNEFactory.GetEmptyMap(),
184 CEFactory.GetEmptyMap());
Ted Kremenek9153f732008-02-05 07:17:49 +0000185
186 return getPersistentState(StateImpl);
187}
188
Ted Kremenek4323a572008-07-10 22:03:41 +0000189const ValueState* ValueStateManager::getPersistentState(ValueState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000190
191 llvm::FoldingSetNodeID ID;
192 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000193 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000194
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000195 if (ValueState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000196 return I;
197
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000198 ValueState* I = (ValueState*) Alloc.Allocate<ValueState>();
199 new (I) ValueState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000200 StateSet.InsertNode(I, InsertPos);
201 return I;
202}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000203
Ted Kremenek461f9772008-03-11 18:57:24 +0000204void ValueState::printDOT(std::ostream& Out, CheckerStatePrinter* P) const {
205 print(Out, P, "\\l", "\\|");
Ted Kremenek59894f92008-03-04 18:30:35 +0000206}
207
Ted Kremenek461f9772008-03-11 18:57:24 +0000208void ValueState::printStdErr(CheckerStatePrinter* P) const {
209 print(*llvm::cerr, P);
210}
211
212void ValueState::print(std::ostream& Out, CheckerStatePrinter* P,
213 const char* nl, const char* sep) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000214
Ted Kremeneke7d22112008-02-11 19:21:59 +0000215 // Print Variable Bindings
Ted Kremenek59894f92008-03-04 18:30:35 +0000216 Out << "Variables:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000217
218 bool isFirst = true;
219
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000220 for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000221
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000222 if (isFirst) isFirst = false;
Ted Kremenek59894f92008-03-04 18:30:35 +0000223 else Out << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000224
225 Out << ' ' << I.getKey()->getName() << " : ";
226 I.getData().print(Out);
227 }
228
229 // Print Subexpression bindings.
230
231 isFirst = true;
232
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000233 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000234
235 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000236 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000237 isFirst = false;
238 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000239 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000240
241 Out << " (" << (void*) I.getKey() << ") ";
242 I.getKey()->printPretty(Out);
243 Out << " : ";
244 I.getData().print(Out);
245 }
246
247 // Print block-expression bindings.
248
249 isFirst = true;
250
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000251 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000252
253 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000254 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000255 isFirst = false;
256 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000257 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000258
259 Out << " (" << (void*) I.getKey() << ") ";
260 I.getKey()->printPretty(Out);
261 Out << " : ";
262 I.getData().print(Out);
263 }
264
265 // Print equality constraints.
266
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000267 if (!ConstEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000268
Ted Kremenek59894f92008-03-04 18:30:35 +0000269 Out << nl << sep << "'==' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000270
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000271 for (ConstEqTy::iterator I = ConstEq.begin(),
272 E = ConstEq.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000273
Ted Kremenek59894f92008-03-04 18:30:35 +0000274 Out << nl << " $" << I.getKey()
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000275 << " : " << I.getData()->toString();
276 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000277 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000278
279 // Print != constraints.
280
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000281 if (!ConstNotEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000282
Ted Kremenek59894f92008-03-04 18:30:35 +0000283 Out << nl << sep << "'!=' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000284
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000285 for (ConstNotEqTy::iterator I = ConstNotEq.begin(),
286 EI = ConstNotEq.end(); I != EI; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000287
Ted Kremenek59894f92008-03-04 18:30:35 +0000288 Out << nl << " $" << I.getKey() << " : ";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000289 isFirst = true;
290
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000291 IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000292
293 for ( ; J != EJ; ++J) {
294 if (isFirst) isFirst = false;
295 else Out << ", ";
296
297 Out << (*J)->toString();
298 }
299 }
300 }
Ted Kremenek461f9772008-03-11 18:57:24 +0000301
302 // Print checker-specific data.
303
304 if (P && CheckerState)
305 P->PrintCheckerState(Out, CheckerState, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000306}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000307
Ted Kremenek584def72008-07-22 00:46:16 +0000308
309//===----------------------------------------------------------------------===//
310// Queries.
311//===----------------------------------------------------------------------===//
312
313bool ValueStateManager::isEqual(const ValueState* state, Expr* Ex,
314 const llvm::APSInt& Y) {
315 RVal V = GetRVal(state, Ex);
316
317 if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V))
318 return X->getValue() == Y;
319
320 if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V))
321 return X->getValue() == Y;
322
323 if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V))
324 return state->isEqual(X->getSymbol(), Y);
325
326 if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V))
327 return state->isEqual(X->getSymbol(), Y);
328
329 return false;
330}
331
332bool ValueStateManager::isEqual(const ValueState* state, Expr* Ex,
333 uint64_t x) {
334 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
335}
336
Ted Kremenek729a9a22008-07-17 23:15:45 +0000337//===----------------------------------------------------------------------===//
338// "Assume" logic.
339//===----------------------------------------------------------------------===//
340
341const ValueState* ValueStateManager::Assume(const ValueState* St, LVal Cond,
342 bool Assumption, bool& isFeasible) {
343
344 St = AssumeAux(St, Cond, Assumption, isFeasible);
345
346 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
347 : St;
348}
349
350const ValueState* ValueStateManager::AssumeAux(const ValueState* St, LVal Cond,
351 bool Assumption, bool& isFeasible) {
352
353 switch (Cond.getSubKind()) {
354 default:
355 assert (false && "'Assume' not implemented for this LVal.");
356 return St;
357
358 case lval::SymbolValKind:
359 if (Assumption)
360 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
361 BasicVals.getZeroWithPtrWidth(), isFeasible);
362 else
363 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
364 BasicVals.getZeroWithPtrWidth(), isFeasible);
365
366
367 case lval::DeclValKind:
368 case lval::FuncValKind:
369 case lval::GotoLabelKind:
370 case lval::StringLiteralValKind:
371 isFeasible = Assumption;
372 return St;
373
374 case lval::FieldOffsetKind:
375 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
376 Assumption, isFeasible);
377
378 case lval::ArrayOffsetKind:
379 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
380 Assumption, isFeasible);
381
382 case lval::ConcreteIntKind: {
383 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
384 isFeasible = b ? Assumption : !Assumption;
385 return St;
386 }
387 }
388}
389
390const ValueState* ValueStateManager::Assume(const ValueState* St, NonLVal Cond,
391 bool Assumption, bool& isFeasible) {
392
393 St = AssumeAux(St, Cond, Assumption, isFeasible);
394
395 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
396 : St;
397}
398
399const ValueState* ValueStateManager::AssumeAux(const ValueState* St, NonLVal Cond,
400 bool Assumption, bool& isFeasible) {
401 switch (Cond.getSubKind()) {
402 default:
403 assert (false && "'Assume' not implemented for this NonLVal.");
404 return St;
405
406
407 case nonlval::SymbolValKind: {
408 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
409 SymbolID sym = SV.getSymbol();
410
411 if (Assumption)
412 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
413 isFeasible);
414 else
415 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
416 isFeasible);
417 }
418
419 case nonlval::SymIntConstraintValKind:
420 return
421 AssumeSymInt(St, Assumption,
422 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
423 isFeasible);
424
425 case nonlval::ConcreteIntKind: {
426 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
427 isFeasible = b ? Assumption : !Assumption;
428 return St;
429 }
430
431 case nonlval::LValAsIntegerKind: {
432 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
433 Assumption, isFeasible);
434 }
435 }
436}
437
438const ValueState* ValueStateManager::AssumeSymNE(const ValueState* St,
439 SymbolID sym, const llvm::APSInt& V,
440 bool& isFeasible) {
441
442 // First, determine if sym == X, where X != V.
443 if (const llvm::APSInt* X = St->getSymVal(sym)) {
444 isFeasible = *X != V;
445 return St;
446 }
447
448 // Second, determine if sym != V.
449 if (St->isNotEqual(sym, V)) {
450 isFeasible = true;
451 return St;
452 }
453
454 // If we reach here, sym is not a constant and we don't know if it is != V.
455 // Make that assumption.
456
457 isFeasible = true;
458 return AddNE(St, sym, V);
459}
460
461const ValueState* ValueStateManager::AssumeSymEQ(const ValueState* St, SymbolID sym,
462 const llvm::APSInt& V, bool& isFeasible) {
463
464 // First, determine if sym == X, where X != V.
465 if (const llvm::APSInt* X = St->getSymVal(sym)) {
466 isFeasible = *X == V;
467 return St;
468 }
469
470 // Second, determine if sym != V.
471 if (St->isNotEqual(sym, V)) {
472 isFeasible = false;
473 return St;
474 }
475
476 // If we reach here, sym is not a constant and we don't know if it is == V.
477 // Make that assumption.
478
479 isFeasible = true;
480 return AddEQ(St, sym, V);
481}
482
483const ValueState* ValueStateManager::AssumeSymInt(const ValueState* St,
484 bool Assumption,
485 const SymIntConstraint& C,
486 bool& isFeasible) {
487
488 switch (C.getOpcode()) {
489 default:
490 // No logic yet for other operators.
491 isFeasible = true;
492 return St;
493
494 case BinaryOperator::EQ:
495 if (Assumption)
496 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
497 else
498 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
499
500 case BinaryOperator::NE:
501 if (Assumption)
502 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
503 else
504 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
505 }
506}