blob: f51175fdcac59477c4909faac3676205bdad4fe3 [file] [log] [blame]
Ted Kremenek2d8dce32008-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//
Ted Kremenek744a7862008-02-08 20:29:23 +000010// This files defines SymbolID, ExprBindKey, and ValueState.
Ted Kremenek2d8dce32008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek0eb0afa2008-02-04 21:59:22 +000014#include "ValueState.h"
15
16using namespace clang;
17
Ted Kremenek13f31562008-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
Ted Kremenekb8958d62008-02-08 19:17:19 +000034ValueState
35ValueStateManager::RemoveDeadBindings(ValueState St, Stmt* Loc,
36 const LiveVariables& Liveness) {
37
38 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
39 // The roots are any Block-level exprs and Decls that our liveness algorithm
40 // tells us are live. We then see what Decls they may reference, and keep
41 // those around. This code more than likely can be made faster, and the
42 // frequency of which this method is called should be experimented with
43 // for optimum performance.
44
45 llvm::SmallVector<ValueDecl*, 10> WList;
Ted Kremenek17c5f112008-02-11 19:21:59 +000046
47 ValueStateImpl NewSt = *St;
48
49 // Drop bindings for subexpressions.
50 NewSt.SubExprBindings = EXFactory.GetEmptyMap();
51
52 // Iterate over the block-expr bindings.
53 for (ValueState::beb_iterator I=St.beb_begin(), E=St.beb_end(); I!=E ; ++I) {
Ted Kremenek08cfd832008-02-08 21:10:02 +000054
Ted Kremenek17c5f112008-02-11 19:21:59 +000055 Expr* BlkExpr = I.getKey();
Ted Kremenekb8958d62008-02-08 19:17:19 +000056
Ted Kremenek17c5f112008-02-11 19:21:59 +000057 if (Liveness.isLive(Loc, BlkExpr)) {
Ted Kremenek08cfd832008-02-08 21:10:02 +000058 if (isa<lval::DeclVal>(I.getData())) {
59 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
60 WList.push_back(LV.getDecl());
Ted Kremenekb8958d62008-02-08 19:17:19 +000061 }
Ted Kremenekb8958d62008-02-08 19:17:19 +000062 }
Ted Kremenek08cfd832008-02-08 21:10:02 +000063 else
Ted Kremenek17c5f112008-02-11 19:21:59 +000064 NewSt.BlockExprBindings = Remove(NewSt, BlkExpr);
Ted Kremenekb8958d62008-02-08 19:17:19 +000065
Ted Kremenek08cfd832008-02-08 21:10:02 +000066 continue;
Ted Kremenekb8958d62008-02-08 19:17:19 +000067 }
Ted Kremenek08cfd832008-02-08 21:10:02 +000068
Ted Kremenek17c5f112008-02-11 19:21:59 +000069 // Iterate over the variable bindings.
70 for (ValueState::vb_iterator I = St.vb_begin(), E = St.vb_end(); I!=E ; ++I)
Ted Kremenek08cfd832008-02-08 21:10:02 +000071 if (Liveness.isLive(Loc, I.getKey()))
72 WList.push_back(I.getKey());
Ted Kremenekb8958d62008-02-08 19:17:19 +000073
Ted Kremenek15fa2f12008-02-11 23:12:59 +000074 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
Ted Kremenekb8958d62008-02-08 19:17:19 +000075
76 while (!WList.empty()) {
77 ValueDecl* V = WList.back();
78 WList.pop_back();
79
80 if (Marked.count(V))
81 continue;
82
83 Marked.insert(V);
84
85 if (V->getType()->isPointerType()) {
86 const LValue& LV = cast<LValue>(GetValue(St, lval::DeclVal(V)));
87
88 if (!isa<lval::DeclVal>(LV))
89 continue;
90
91 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
92 WList.push_back(LVD.getDecl());
93 }
94 }
95
Ted Kremenek17c5f112008-02-11 19:21:59 +000096 for (ValueState::vb_iterator I = St.vb_begin(), E = St.vb_end(); I!=E ; ++I)
Ted Kremenek08cfd832008-02-08 21:10:02 +000097 if (!Marked.count(I.getKey()))
Ted Kremenek17c5f112008-02-11 19:21:59 +000098 NewSt.VarBindings = Remove(NewSt, I.getKey());
Ted Kremenekb8958d62008-02-08 19:17:19 +000099
Ted Kremenek17c5f112008-02-11 19:21:59 +0000100 return getPersistentState(NewSt);
Ted Kremenekb8958d62008-02-08 19:17:19 +0000101}
Ted Kremenek13f31562008-02-06 00:54:14 +0000102
103
Ted Kremenek17c5f112008-02-11 19:21:59 +0000104RValue ValueStateManager::GetValue(ValueState St, const LValue& LV,
Ted Kremenek80d52d02008-02-07 05:48:01 +0000105 QualType* T) {
Ted Kremenekadec14b2008-02-08 02:57:34 +0000106 if (isa<UnknownVal>(LV))
107 return UnknownVal();
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000108
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000109 switch (LV.getSubKind()) {
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000110 case lval::DeclValKind: {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000111 ValueState::VarBindingsTy::TreeTy* T =
Ted Kremenek08cfd832008-02-08 21:10:02 +0000112 // FIXME: We should make lval::DeclVal only contain VarDecl
Ted Kremenek17c5f112008-02-11 19:21:59 +0000113 St->VarBindings.SlimFind(
Ted Kremenek08cfd832008-02-08 21:10:02 +0000114 cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()));
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000115
Ted Kremenekadec14b2008-02-08 02:57:34 +0000116 return T ? T->getValue().second : UnknownVal();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000117 }
Ted Kremenek80d52d02008-02-07 05:48:01 +0000118
119 // FIXME: We should bind how far a "ContentsOf" will go...
120
121 case lval::SymbolValKind: {
122 const lval::SymbolVal& SV = cast<lval::SymbolVal>(LV);
123 assert (T);
124
125 if (T->getTypePtr()->isPointerType())
126 return lval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol()));
127 else
128 return nonlval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol()));
129 }
130
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000131 default:
132 assert (false && "Invalid LValue.");
133 break;
134 }
135
Ted Kremenekadec14b2008-02-08 02:57:34 +0000136 return UnknownVal();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000137}
138
Ted Kremenek17c5f112008-02-11 19:21:59 +0000139ValueState
140ValueStateManager::AddNE(ValueState St, SymbolID sym, const llvm::APSInt& V) {
Ted Kremenek13f31562008-02-06 00:54:14 +0000141 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000142 ValueState::ConstantNotEqTy::TreeTy* T = St->ConstantNotEq.SlimFind(sym);
Ted Kremenek13f31562008-02-06 00:54:14 +0000143
144 ValueState::IntSetTy S = T ? T->getValue().second : ISetFactory.GetEmptySet();
145
146 // Now add V to the NE set.
147 S = ISetFactory.Add(S, &V);
148
149 // Create a new state with the old binding replaced.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000150 ValueStateImpl NewSt = *St;
151 NewSt.ConstantNotEq = CNEFactory.Add(NewSt.ConstantNotEq,
Ted Kremenek13f31562008-02-06 00:54:14 +0000152 sym, S);
153
154 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000155 return getPersistentState(NewSt);
Ted Kremenek13f31562008-02-06 00:54:14 +0000156}
157
Ted Kremenek17c5f112008-02-11 19:21:59 +0000158ValueState
159ValueStateManager::AddEQ(ValueState St, SymbolID sym, const llvm::APSInt& V) {
Ted Kremenek13f31562008-02-06 00:54:14 +0000160 // Create a new state with the old binding replaced.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000161 ValueStateImpl NewSt = *St;
162 NewSt.ConstantEq = CEFactory.Add(NewSt.ConstantEq, sym, &V);
Ted Kremenek13f31562008-02-06 00:54:14 +0000163
164 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000165 return getPersistentState(NewSt);
Ted Kremenek13f31562008-02-06 00:54:14 +0000166}
167
Ted Kremenek17c5f112008-02-11 19:21:59 +0000168RValue ValueStateManager::GetValue(ValueState St, Expr* S, bool* hasVal) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000169 for (;;) {
170 switch (S->getStmtClass()) {
171
172 // ParenExprs are no-ops.
173
174 case Stmt::ParenExprClass:
175 S = cast<ParenExpr>(S)->getSubExpr();
176 continue;
177
178 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
179 // (assuming an implicit "load") depending on the context. In this
180 // context we assume that we are retrieving the value contained
181 // within the referenced variables.
182
183 case Stmt::DeclRefExprClass:
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000184 return GetValue(St, lval::DeclVal(cast<DeclRefExpr>(S)->getDecl()));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000185
186 // Integer literals evaluate to an RValue. Simply retrieve the
187 // RValue for the literal.
188
189 case Stmt::IntegerLiteralClass:
190 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S));
191
192 // Casts where the source and target type are the same
193 // are no-ops. We blast through these to get the descendant
194 // subexpression that has a value.
195
196 case Stmt::ImplicitCastExprClass: {
197 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
198 if (C->getType() == C->getSubExpr()->getType()) {
199 S = C->getSubExpr();
200 continue;
201 }
202 break;
203 }
204
205 case Stmt::CastExprClass: {
206 CastExpr* C = cast<CastExpr>(S);
207 if (C->getType() == C->getSubExpr()->getType()) {
208 S = C->getSubExpr();
209 continue;
210 }
211 break;
212 }
213
214 // Handle all other Stmt* using a lookup.
215
216 default:
217 break;
218 };
219
220 break;
221 }
222
Ted Kremenek17c5f112008-02-11 19:21:59 +0000223 ValueState::ExprBindingsTy::TreeTy* T = St->SubExprBindings.SlimFind(S);
224
225 if (T) {
226 if (hasVal) *hasVal = true;
227 return T->getValue().second;
228 }
229
230 T = St->BlockExprBindings.SlimFind(S);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000231
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000232 if (T) {
233 if (hasVal) *hasVal = true;
234 return T->getValue().second;
235 }
236 else {
237 if (hasVal) *hasVal = false;
Ted Kremenekadec14b2008-02-08 02:57:34 +0000238 return UnknownVal();
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000239 }
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000240}
241
Ted Kremenek17c5f112008-02-11 19:21:59 +0000242LValue ValueStateManager::GetLValue(ValueState St, Expr* E) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000243
Ted Kremenek08cfd832008-02-08 21:10:02 +0000244 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
245 E = P->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000246
Ted Kremenek08cfd832008-02-08 21:10:02 +0000247 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000248 return lval::DeclVal(DR->getDecl());
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000249
Ted Kremenek08cfd832008-02-08 21:10:02 +0000250 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E))
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000251 if (U->getOpcode() == UnaryOperator::Deref)
252 return cast<LValue>(GetValue(St, U->getSubExpr()));
253
Ted Kremenek08cfd832008-02-08 21:10:02 +0000254 return cast<LValue>(GetValue(St, E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000255}
256
Ted Kremenek17c5f112008-02-11 19:21:59 +0000257ValueState
258ValueStateManager::SetValue(ValueState St, Expr* E, bool isBlkExpr,
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000259 const RValue& V) {
260
Ted Kremenek08cfd832008-02-08 21:10:02 +0000261 assert (E);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000262
263 if (V.isUnknown())
264 return St;
265
266 ValueStateImpl NewSt = *St;
267
268 if (isBlkExpr)
269 NewSt.BlockExprBindings = EXFactory.Add(NewSt.BlockExprBindings, E, V);
270 else
271 NewSt.SubExprBindings = EXFactory.Add(NewSt.SubExprBindings, E, V);
272
273 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000274}
275
Ted Kremenek17c5f112008-02-11 19:21:59 +0000276ValueState
277ValueStateManager::SetValue(ValueState St, const LValue& LV, const RValue& V) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000278
279 switch (LV.getSubKind()) {
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000280 case lval::DeclValKind:
Ted Kremenek08cfd832008-02-08 21:10:02 +0000281 return V.isKnown() // FIXME: Have DeclVal only contain VarDecl
Ted Kremenek17c5f112008-02-11 19:21:59 +0000282 ? BindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()), V)
283 : UnbindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000284
285 default:
286 assert ("SetValue for given LValue type not yet implemented.");
287 return St;
288 }
289}
290
Ted Kremenek17c5f112008-02-11 19:21:59 +0000291ValueState
292ValueStateManager::BindVar(ValueState St, VarDecl* D, const RValue& V) {
Ted Kremenek08cfd832008-02-08 21:10:02 +0000293
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000294 // Create a new state with the old binding removed.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000295 ValueStateImpl NewSt = *St;
296 NewSt.VarBindings = VBFactory.Add(NewSt.VarBindings, D, V);
Ted Kremenek08cfd832008-02-08 21:10:02 +0000297
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000298 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000299 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000300}
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000301
Ted Kremenek17c5f112008-02-11 19:21:59 +0000302ValueState
303ValueStateManager::UnbindVar(ValueState St, VarDecl* D) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000304
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000305 // Create a new state with the old binding removed.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000306 ValueStateImpl NewSt = *St;
307 NewSt.VarBindings = VBFactory.Remove(NewSt.VarBindings, D);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000308
309 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000310 return getPersistentState(NewSt);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000311}
312
Ted Kremenek17c5f112008-02-11 19:21:59 +0000313ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000314ValueStateManager::getInitialState() {
315
316 // Create a state with empty variable bindings.
Ted Kremenek08cfd832008-02-08 21:10:02 +0000317 ValueStateImpl StateImpl(EXFactory.GetEmptyMap(),
318 VBFactory.GetEmptyMap(),
Ted Kremenek13f31562008-02-06 00:54:14 +0000319 CNEFactory.GetEmptyMap(),
320 CEFactory.GetEmptyMap());
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000321
322 return getPersistentState(StateImpl);
323}
324
Ted Kremenek17c5f112008-02-11 19:21:59 +0000325ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000326ValueStateManager::getPersistentState(const ValueStateImpl &State) {
327
328 llvm::FoldingSetNodeID ID;
329 State.Profile(ID);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000330 void* InsertPos;
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000331
332 if (ValueStateImpl* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
333 return I;
334
Ted Kremenek9a4e8072008-02-06 02:45:20 +0000335 ValueStateImpl* I = (ValueStateImpl*) Alloc.Allocate<ValueStateImpl>();
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000336 new (I) ValueStateImpl(State);
337 StateSet.InsertNode(I, InsertPos);
338 return I;
339}
Ted Kremenek17c5f112008-02-11 19:21:59 +0000340
341void ValueState::printDOT(std::ostream& Out) const {
342 // Print Variable Bindings
343 Out << "Variables:\\l";
344
345 bool isFirst = true;
346
347 for (vb_iterator I=vb_begin(), E=vb_end(); I!=E; ++I) {
348
349 if (isFirst)
350 isFirst = false;
351 else
352 Out << "\\l";
353
354 Out << ' ' << I.getKey()->getName() << " : ";
355 I.getData().print(Out);
356 }
357
358 // Print Subexpression bindings.
359
360 isFirst = true;
361
362 for (seb_iterator I=seb_begin(), E=seb_end(); I != E;++I) {
363
364 if (isFirst) {
365 Out << "\\l\\lSub-Expressions:\\l";
366 isFirst = false;
367 }
368 else
369 Out << "\\l";
370
371 Out << " (" << (void*) I.getKey() << ") ";
372 I.getKey()->printPretty(Out);
373 Out << " : ";
374 I.getData().print(Out);
375 }
376
377 // Print block-expression bindings.
378
379 isFirst = true;
380
381 for (beb_iterator I=beb_begin(), E=beb_end(); I != E; ++I) {
382
383 if (isFirst) {
384 Out << "\\l\\lBlock-level Expressions:\\l";
385 isFirst = false;
386 }
387 else
388 Out << "\\l";
389
390 Out << " (" << (void*) I.getKey() << ") ";
391 I.getKey()->printPretty(Out);
392 Out << " : ";
393 I.getData().print(Out);
394 }
395
396 // Print equality constraints.
397
398 if (!Data->ConstantEq.isEmpty()) {
399
400 Out << "\\l\\|'==' constraints:";
401
402 for (ConstantEqTy::iterator I=Data->ConstantEq.begin(),
403 E=Data->ConstantEq.end(); I!=E;++I)
404 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
405 }
406
407
408 // Print != constraints.
409
410 if (!Data->ConstantNotEq.isEmpty()) {
411
412 Out << "\\l\\|'!=' constraints:";
413
414 for (ConstantNotEqTy::iterator I=Data->ConstantNotEq.begin(),
415 EI=Data->ConstantNotEq.end(); I != EI; ++I) {
416
417 Out << "\\l $" << I.getKey() << " : ";
418 isFirst = true;
419
420 IntSetTy::iterator J=I.getData().begin(), EJ=I.getData().end();
421
422 for ( ; J != EJ; ++J) {
423 if (isFirst) isFirst = false;
424 else Out << ", ";
425
426 Out << (*J)->toString();
427 }
428 }
429 }
430}