blob: d550716fd5f59f0920e0b3ea2dcb8512e1dd8a46 [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 Kremenek0eb0afa2008-02-04 21:59:22 +0000171
172 // ParenExprs are no-ops.
173
174 case Stmt::ParenExprClass:
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000175 E = cast<ParenExpr>(E)->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000176 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 Kremenekddb3ef12008-02-12 18:50:32 +0000184 return GetValue(St, lval::DeclVal(cast<DeclRefExpr>(E)->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:
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000190 return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000191
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000192 case Stmt::CharacterLiteralClass: {
193 CharacterLiteral* C = cast<CharacterLiteral>(E);
194
195 return NonLValue::GetValue(ValMgr, C->getValue(), C->getType(),
196 C->getLoc());
197 }
198
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000199 // Casts where the source and target type are the same
200 // are no-ops. We blast through these to get the descendant
201 // subexpression that has a value.
202
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000203
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000204 case Stmt::ImplicitCastExprClass: {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000205 ImplicitCastExpr* C = cast<ImplicitCastExpr>(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000206 if (C->getType() == C->getSubExpr()->getType()) {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000207 E = C->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000208 continue;
209 }
210 break;
211 }
212
213 case Stmt::CastExprClass: {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000214 CastExpr* C = cast<CastExpr>(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000215 if (C->getType() == C->getSubExpr()->getType()) {
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000216 E = C->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000217 continue;
218 }
219 break;
220 }
221
222 // Handle all other Stmt* using a lookup.
223
224 default:
225 break;
226 };
227
228 break;
229 }
230
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000231 ValueState::ExprBindingsTy::TreeTy* T = St->SubExprBindings.SlimFind(E);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000232
233 if (T) {
234 if (hasVal) *hasVal = true;
235 return T->getValue().second;
236 }
237
Ted Kremenekddb3ef12008-02-12 18:50:32 +0000238 T = St->BlockExprBindings.SlimFind(E);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000239
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000240 if (T) {
241 if (hasVal) *hasVal = true;
242 return T->getValue().second;
243 }
244 else {
245 if (hasVal) *hasVal = false;
Ted Kremenekadec14b2008-02-08 02:57:34 +0000246 return UnknownVal();
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000247 }
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000248}
249
Ted Kremenek17c5f112008-02-11 19:21:59 +0000250LValue ValueStateManager::GetLValue(ValueState St, Expr* E) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000251
Ted Kremenek08cfd832008-02-08 21:10:02 +0000252 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
253 E = P->getSubExpr();
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000254
Ted Kremenek08cfd832008-02-08 21:10:02 +0000255 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E))
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000256 return lval::DeclVal(DR->getDecl());
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000257
Ted Kremenek08cfd832008-02-08 21:10:02 +0000258 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E))
Ted Kremeneke1f38b62008-02-07 01:08:27 +0000259 if (U->getOpcode() == UnaryOperator::Deref)
260 return cast<LValue>(GetValue(St, U->getSubExpr()));
261
Ted Kremenek08cfd832008-02-08 21:10:02 +0000262 return cast<LValue>(GetValue(St, E));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000263}
264
Ted Kremenek17c5f112008-02-11 19:21:59 +0000265ValueState
266ValueStateManager::SetValue(ValueState St, Expr* E, bool isBlkExpr,
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000267 const RValue& V) {
268
Ted Kremenek08cfd832008-02-08 21:10:02 +0000269 assert (E);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000270
271 if (V.isUnknown())
272 return St;
273
274 ValueStateImpl NewSt = *St;
275
276 if (isBlkExpr)
277 NewSt.BlockExprBindings = EXFactory.Add(NewSt.BlockExprBindings, E, V);
278 else
279 NewSt.SubExprBindings = EXFactory.Add(NewSt.SubExprBindings, E, V);
280
281 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000282}
283
Ted Kremenek17c5f112008-02-11 19:21:59 +0000284ValueState
285ValueStateManager::SetValue(ValueState St, const LValue& LV, const RValue& V) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000286
287 switch (LV.getSubKind()) {
Ted Kremenek1b63a3b2008-02-05 21:52:21 +0000288 case lval::DeclValKind:
Ted Kremenek08cfd832008-02-08 21:10:02 +0000289 return V.isKnown() // FIXME: Have DeclVal only contain VarDecl
Ted Kremenek17c5f112008-02-11 19:21:59 +0000290 ? BindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()), V)
291 : UnbindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()));
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000292
293 default:
294 assert ("SetValue for given LValue type not yet implemented.");
295 return St;
296 }
297}
298
Ted Kremenek17c5f112008-02-11 19:21:59 +0000299ValueState
300ValueStateManager::BindVar(ValueState St, VarDecl* D, const RValue& V) {
Ted Kremenek08cfd832008-02-08 21:10:02 +0000301
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000302 // Create a new state with the old binding removed.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000303 ValueStateImpl NewSt = *St;
304 NewSt.VarBindings = VBFactory.Add(NewSt.VarBindings, D, V);
Ted Kremenek08cfd832008-02-08 21:10:02 +0000305
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000306 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000307 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000308}
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000309
Ted Kremenek17c5f112008-02-11 19:21:59 +0000310ValueState
311ValueStateManager::UnbindVar(ValueState St, VarDecl* D) {
Ted Kremenek0eb0afa2008-02-04 21:59:22 +0000312
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000313 // Create a new state with the old binding removed.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000314 ValueStateImpl NewSt = *St;
315 NewSt.VarBindings = VBFactory.Remove(NewSt.VarBindings, D);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000316
317 // Get the persistent copy.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000318 return getPersistentState(NewSt);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000319}
320
Ted Kremenek17c5f112008-02-11 19:21:59 +0000321ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000322ValueStateManager::getInitialState() {
323
324 // Create a state with empty variable bindings.
Ted Kremenek08cfd832008-02-08 21:10:02 +0000325 ValueStateImpl StateImpl(EXFactory.GetEmptyMap(),
326 VBFactory.GetEmptyMap(),
Ted Kremenek13f31562008-02-06 00:54:14 +0000327 CNEFactory.GetEmptyMap(),
328 CEFactory.GetEmptyMap());
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000329
330 return getPersistentState(StateImpl);
331}
332
Ted Kremenek17c5f112008-02-11 19:21:59 +0000333ValueState
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000334ValueStateManager::getPersistentState(const ValueStateImpl &State) {
335
336 llvm::FoldingSetNodeID ID;
337 State.Profile(ID);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000338 void* InsertPos;
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000339
340 if (ValueStateImpl* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
341 return I;
342
Ted Kremenek9a4e8072008-02-06 02:45:20 +0000343 ValueStateImpl* I = (ValueStateImpl*) Alloc.Allocate<ValueStateImpl>();
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000344 new (I) ValueStateImpl(State);
345 StateSet.InsertNode(I, InsertPos);
346 return I;
347}
Ted Kremenek17c5f112008-02-11 19:21:59 +0000348
349void ValueState::printDOT(std::ostream& Out) const {
350 // Print Variable Bindings
351 Out << "Variables:\\l";
352
353 bool isFirst = true;
354
355 for (vb_iterator I=vb_begin(), E=vb_end(); I!=E; ++I) {
356
357 if (isFirst)
358 isFirst = false;
359 else
360 Out << "\\l";
361
362 Out << ' ' << I.getKey()->getName() << " : ";
363 I.getData().print(Out);
364 }
365
366 // Print Subexpression bindings.
367
368 isFirst = true;
369
370 for (seb_iterator I=seb_begin(), E=seb_end(); I != E;++I) {
371
372 if (isFirst) {
373 Out << "\\l\\lSub-Expressions:\\l";
374 isFirst = false;
375 }
376 else
377 Out << "\\l";
378
379 Out << " (" << (void*) I.getKey() << ") ";
380 I.getKey()->printPretty(Out);
381 Out << " : ";
382 I.getData().print(Out);
383 }
384
385 // Print block-expression bindings.
386
387 isFirst = true;
388
389 for (beb_iterator I=beb_begin(), E=beb_end(); I != E; ++I) {
390
391 if (isFirst) {
392 Out << "\\l\\lBlock-level Expressions:\\l";
393 isFirst = false;
394 }
395 else
396 Out << "\\l";
397
398 Out << " (" << (void*) I.getKey() << ") ";
399 I.getKey()->printPretty(Out);
400 Out << " : ";
401 I.getData().print(Out);
402 }
403
404 // Print equality constraints.
405
406 if (!Data->ConstantEq.isEmpty()) {
407
408 Out << "\\l\\|'==' constraints:";
409
410 for (ConstantEqTy::iterator I=Data->ConstantEq.begin(),
411 E=Data->ConstantEq.end(); I!=E;++I)
412 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
413 }
414
415
416 // Print != constraints.
417
418 if (!Data->ConstantNotEq.isEmpty()) {
419
420 Out << "\\l\\|'!=' constraints:";
421
422 for (ConstantNotEqTy::iterator I=Data->ConstantNotEq.begin(),
423 EI=Data->ConstantNotEq.end(); I != EI; ++I) {
424
425 Out << "\\l $" << I.getKey() << " : ";
426 isFirst = true;
427
428 IntSetTy::iterator J=I.getData().begin(), EJ=I.getData().end();
429
430 for ( ; J != EJ; ++J) {
431 if (isFirst) isFirst = false;
432 else Out << ", ";
433
434 Out << (*J)->toString();
435 }
436 }
437 }
438}