blob: c74c876b5121fd718390ddd0a411d97ee0cc36cc [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 Kremenekddb3ef12008-02-12 18:50:32 +0000168RValue ValueStateManager::GetValue(ValueState St, Expr* E, bool* hasVal) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000169 for (;;) {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000170 switch (E->getStmtClass()) {
Ted Kremenekad884682008-02-12 21:37:56 +0000171
172 case Stmt::AddrLabelExprClass:
173 return LValue::GetValue(cast<AddrLabelExpr>(E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000174
175 // ParenExprs are no-ops.
176
177 case Stmt::ParenExprClass:
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000178 E = cast<ParenExpr>(E)->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000179 continue;
180
181 // DeclRefExprs can either evaluate to an LValue or a Non-LValue
182 // (assuming an implicit "load") depending on the context. In this
183 // context we assume that we are retrieving the value contained
184 // within the referenced variables.
185
186 case Stmt::DeclRefExprClass:
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000187 return GetValue(St, lval::DeclVal(cast<DeclRefExpr>(E)->getDecl()));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000188
189 // Integer literals evaluate to an RValue. Simply retrieve the
190 // RValue for the literal.
191
192 case Stmt::IntegerLiteralClass:
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000193 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000194
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000195 case Stmt::CharacterLiteralClass: {
196 CharacterLiteral* C = cast<CharacterLiteral>(E);
197
198 return NonLValue::GetValue(ValMgr, C->getValue(), C->getType(),
199 C->getLoc());
200 }
201
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000202 // Casts where the source and target type are the same
203 // are no-ops. We blast through these to get the descendant
204 // subexpression that has a value.
205
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000206
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000207 case Stmt::ImplicitCastExprClass: {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000208 ImplicitCastExpr* C = cast<ImplicitCastExpr>(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000209 if (C->getType() == C->getSubExpr()->getType()) {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000210 E = C->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000211 continue;
212 }
213 break;
214 }
215
216 case Stmt::CastExprClass: {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000217 CastExpr* C = cast<CastExpr>(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000218 if (C->getType() == C->getSubExpr()->getType()) {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000219 E = C->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000220 continue;
221 }
222 break;
223 }
224
225 // Handle all other Stmt* using a lookup.
226
227 default:
228 break;
229 };
230
231 break;
232 }
233
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000234 ValueState::ExprBindingsTy::TreeTy* T = St->SubExprBindings.SlimFind(E);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000235
236 if (T) {
237 if (hasVal) *hasVal = true;
238 return T->getValue().second;
239 }
240
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000241 T = St->BlockExprBindings.SlimFind(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000242
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000243 if (T) {
244 if (hasVal) *hasVal = true;
245 return T->getValue().second;
246 }
247 else {
248 if (hasVal) *hasVal = false;
Ted Kremenekadec14b2008-02-08 02:57:34 +0000249 return UnknownVal();
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000250 }
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000251}
252
Ted Kremenek17c5f112008-02-11 19:21:59 +0000253LValue ValueStateManager::GetLValue(ValueState St, Expr* E) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000254
Ted Kremenek08cfd832008-02-08 21:10:02 +0000255 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
256 E = P->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000257
Ted Kremenek08cfd832008-02-08 21:10:02 +0000258 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000259 return lval::DeclVal(DR->getDecl());
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000260
Ted Kremenek08cfd832008-02-08 21:10:02 +0000261 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E))
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000262 if (U->getOpcode() == UnaryOperator::Deref)
263 return cast<LValue>(GetValue(St, U->getSubExpr()));
264
Ted Kremenek08cfd832008-02-08 21:10:02 +0000265 return cast<LValue>(GetValue(St, E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000266}
267
Ted Kremenek17c5f112008-02-11 19:21:59 +0000268ValueState
269ValueStateManager::SetValue(ValueState St, Expr* E, bool isBlkExpr,
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000270 const RValue& V) {
271
Ted Kremenek08cfd832008-02-08 21:10:02 +0000272 assert (E);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000273
274 if (V.isUnknown())
275 return St;
276
277 ValueStateImpl NewSt = *St;
278
279 if (isBlkExpr)
280 NewSt.BlockExprBindings = EXFactory.Add(NewSt.BlockExprBindings, E, V);
281 else
282 NewSt.SubExprBindings = EXFactory.Add(NewSt.SubExprBindings, E, V);
283
284 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000285}
286
Ted Kremenek17c5f112008-02-11 19:21:59 +0000287ValueState
288ValueStateManager::SetValue(ValueState St, const LValue& LV, const RValue& V) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000289
290 switch (LV.getSubKind()) {
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000291 case lval::DeclValKind:
Ted Kremenek08cfd832008-02-08 21:10:02 +0000292 return V.isKnown() // FIXME: Have DeclVal only contain VarDecl
Ted Kremenek17c5f112008-02-11 19:21:59 +0000293 ? BindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()), V)
294 : UnbindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000295
296 default:
297 assert ("SetValue for given LValue type not yet implemented.");
298 return St;
299 }
300}
301
Ted Kremenek17c5f112008-02-11 19:21:59 +0000302ValueState
303ValueStateManager::BindVar(ValueState St, VarDecl* D, const RValue& V) {
Ted Kremenek08cfd832008-02-08 21:10:02 +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.Add(NewSt.VarBindings, D, V);
Ted Kremenek08cfd832008-02-08 21:10:02 +0000308
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000309 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000310 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000311}
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000312
Ted Kremenek17c5f112008-02-11 19:21:59 +0000313ValueState
314ValueStateManager::UnbindVar(ValueState St, VarDecl* D) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000315
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000316 // Create a new state with the old binding removed.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000317 ValueStateImpl NewSt = *St;
318 NewSt.VarBindings = VBFactory.Remove(NewSt.VarBindings, D);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000319
320 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000321 return getPersistentState(NewSt);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000322}
323
Ted Kremenek17c5f112008-02-11 19:21:59 +0000324ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000325ValueStateManager::getInitialState() {
326
327 // Create a state with empty variable bindings.
Ted Kremenek08cfd832008-02-08 21:10:02 +0000328 ValueStateImpl StateImpl(EXFactory.GetEmptyMap(),
329 VBFactory.GetEmptyMap(),
Ted Kremenek13f31562008-02-06 00:54:14 +0000330 CNEFactory.GetEmptyMap(),
331 CEFactory.GetEmptyMap());
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000332
333 return getPersistentState(StateImpl);
334}
335
Ted Kremenek17c5f112008-02-11 19:21:59 +0000336ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000337ValueStateManager::getPersistentState(const ValueStateImpl &State) {
338
339 llvm::FoldingSetNodeID ID;
340 State.Profile(ID);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000341 void* InsertPos;
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000342
343 if (ValueStateImpl* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
344 return I;
345
Ted Kremenek9a4e8072008-02-06 02:45:20 +0000346 ValueStateImpl* I = (ValueStateImpl*) Alloc.Allocate<ValueStateImpl>();
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000347 new (I) ValueStateImpl(State);
348 StateSet.InsertNode(I, InsertPos);
349 return I;
350}
Ted Kremenek17c5f112008-02-11 19:21:59 +0000351
352void ValueState::printDOT(std::ostream& Out) const {
353 // Print Variable Bindings
354 Out << "Variables:\\l";
355
356 bool isFirst = true;
357
358 for (vb_iterator I=vb_begin(), E=vb_end(); I!=E; ++I) {
359
360 if (isFirst)
361 isFirst = false;
362 else
363 Out << "\\l";
364
365 Out << ' ' << I.getKey()->getName() << " : ";
366 I.getData().print(Out);
367 }
368
369 // Print Subexpression bindings.
370
371 isFirst = true;
372
373 for (seb_iterator I=seb_begin(), E=seb_end(); I != E;++I) {
374
375 if (isFirst) {
376 Out << "\\l\\lSub-Expressions:\\l";
377 isFirst = false;
378 }
379 else
380 Out << "\\l";
381
382 Out << " (" << (void*) I.getKey() << ") ";
383 I.getKey()->printPretty(Out);
384 Out << " : ";
385 I.getData().print(Out);
386 }
387
388 // Print block-expression bindings.
389
390 isFirst = true;
391
392 for (beb_iterator I=beb_begin(), E=beb_end(); I != E; ++I) {
393
394 if (isFirst) {
395 Out << "\\l\\lBlock-level Expressions:\\l";
396 isFirst = false;
397 }
398 else
399 Out << "\\l";
400
401 Out << " (" << (void*) I.getKey() << ") ";
402 I.getKey()->printPretty(Out);
403 Out << " : ";
404 I.getData().print(Out);
405 }
406
407 // Print equality constraints.
408
409 if (!Data->ConstantEq.isEmpty()) {
410
411 Out << "\\l\\|'==' constraints:";
412
413 for (ConstantEqTy::iterator I=Data->ConstantEq.begin(),
414 E=Data->ConstantEq.end(); I!=E;++I)
415 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
416 }
417
418
419 // Print != constraints.
420
421 if (!Data->ConstantNotEq.isEmpty()) {
422
423 Out << "\\l\\|'!=' constraints:";
424
425 for (ConstantNotEqTy::iterator I=Data->ConstantNotEq.begin(),
426 EI=Data->ConstantNotEq.end(); I != EI; ++I) {
427
428 Out << "\\l $" << I.getKey() << " : ";
429 isFirst = true;
430
431 IntSetTy::iterator J=I.getData().begin(), EJ=I.getData().end();
432
433 for ( ; J != EJ; ++J) {
434 if (isFirst) isFirst = false;
435 else Out << ", ";
436
437 Out << (*J)->toString();
438 }
439 }
440 }
441}