blob: 399dacc96f7d0606938d853e9d598fa55876e6ab [file] [log] [blame]
Ted Kremenekd27f8162008-01-15 23:55:06 +00001//===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==//
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Constant Propagation via Graph Reachability
11//
12// This files defines a simple analysis that performs path-sensitive
13// constant propagation within a function. An example use of this analysis
14// is to perform simple checks for NULL dereferences.
15//
16//===----------------------------------------------------------------------===//
17
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000018#include "RValues.h"
19#include "ValueState.h"
20
Ted Kremenekd27f8162008-01-15 23:55:06 +000021#include "clang/Analysis/PathSensitive/GREngine.h"
22#include "clang/AST/Expr.h"
Ted Kremenek874d63f2008-01-24 02:02:54 +000023#include "clang/AST/ASTContext.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000024#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000025
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/DataTypes.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek3c6c6722008-01-16 17:56:25 +000031#include "llvm/ADT/SmallVector.h"
Ted Kremenekb38911f2008-01-30 23:03:39 +000032#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033#include "llvm/Support/Allocator.h"
Ted Kremenekd27f8162008-01-15 23:55:06 +000034#include "llvm/Support/Compiler.h"
Ted Kremenekab2b8c52008-01-23 19:59:44 +000035#include "llvm/Support/Streams.h"
36
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000037#include <functional>
38
Ted Kremenekaa66a322008-01-16 21:46:15 +000039#ifndef NDEBUG
40#include "llvm/Support/GraphWriter.h"
41#include <sstream>
42#endif
43
Ted Kremenekd27f8162008-01-15 23:55:06 +000044using namespace clang;
Ted Kremenekd27f8162008-01-15 23:55:06 +000045using llvm::dyn_cast;
46using llvm::cast;
Ted Kremenek5ee4ff82008-01-25 22:55:56 +000047using llvm::APSInt;
Ted Kremenekd27f8162008-01-15 23:55:06 +000048
49//===----------------------------------------------------------------------===//
Ted Kremenekbd03f1d2008-01-28 22:09:13 +000050// The Checker.
Ted Kremenekb38911f2008-01-30 23:03:39 +000051//
52// FIXME: This checker logic should be eventually broken into two components.
53// The first is the "meta"-level checking logic; the code that
54// does the Stmt visitation, fetching values from the map, etc.
55// The second part does the actual state manipulation. This way we
56// get more of a separate of concerns of these two pieces, with the
57// latter potentially being refactored back into the main checking
58// logic.
Ted Kremenekd27f8162008-01-15 23:55:06 +000059//===----------------------------------------------------------------------===//
60
61namespace {
Ted Kremenekd27f8162008-01-15 23:55:06 +000062
Ted Kremenekab2b8c52008-01-23 19:59:44 +000063class VISIBILITY_HIDDEN GRConstants {
Ted Kremenekd27f8162008-01-15 23:55:06 +000064
65public:
Ted Kremeneke070a1d2008-02-04 21:59:01 +000066 typedef ValueStateManager::StateTy StateTy;
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +000067 typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder;
68 typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder;
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000069 typedef ExplodedGraph<GRConstants> GraphTy;
70 typedef GraphTy::NodeTy NodeTy;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000071
72 class NodeSet {
73 typedef llvm::SmallVector<NodeTy*,3> ImplTy;
74 ImplTy Impl;
75 public:
76
77 NodeSet() {}
Ted Kremenekb38911f2008-01-30 23:03:39 +000078 NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000079
Ted Kremenekb38911f2008-01-30 23:03:39 +000080 void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000081
82 typedef ImplTy::iterator iterator;
83 typedef ImplTy::const_iterator const_iterator;
84
85 unsigned size() const { return Impl.size(); }
Ted Kremenek9de04c42008-01-24 20:55:43 +000086 bool empty() const { return Impl.empty(); }
Ted Kremenekab2b8c52008-01-23 19:59:44 +000087
88 iterator begin() { return Impl.begin(); }
89 iterator end() { return Impl.end(); }
90
91 const_iterator begin() const { return Impl.begin(); }
92 const_iterator end() const { return Impl.end(); }
93 };
Ted Kremenekcba2e432008-02-05 19:35:18 +000094
Ted Kremenekd27f8162008-01-15 23:55:06 +000095protected:
Ted Kremenekcb48b9c2008-01-29 00:33:40 +000096 /// G - the simulation graph.
97 GraphTy& G;
98
Ted Kremenekab2b8c52008-01-23 19:59:44 +000099 /// Liveness - live-variables information the ValueDecl* and block-level
100 /// Expr* in the CFG. Used to prune out dead state.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000101 LiveVariables Liveness;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000102
Ted Kremenekf4b7a692008-01-29 22:11:49 +0000103 /// Builder - The current GRStmtNodeBuilder which is used when building the nodes
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000104 /// for a given statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000105 StmtNodeBuilder* Builder;
106
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000107 /// StateMgr - Object that manages the data for all created states.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000108 ValueStateManager StateMgr;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000109
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000110 /// ValueMgr - Object that manages the data for all created RValues.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000111 ValueManager& ValMgr;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000112
Ted Kremenek68fd2572008-01-29 17:27:31 +0000113 /// SymMgr - Object that manages the symbol information.
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000114 SymbolManager& SymMgr;
Ted Kremenek68fd2572008-01-29 17:27:31 +0000115
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000116 /// StmtEntryNode - The immediate predecessor node.
117 NodeTy* StmtEntryNode;
118
119 /// CurrentStmt - The current block-level statement.
120 Stmt* CurrentStmt;
121
Ted Kremenekb38911f2008-01-30 23:03:39 +0000122 /// UninitBranches - Nodes in the ExplodedGraph that result from
123 /// taking a branch based on an uninitialized value.
124 typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy;
125 UninitBranchesTy UninitBranches;
126
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000127 bool StateCleaned;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000128
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000129 ASTContext& getContext() const { return G.getContext(); }
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000130
Ted Kremenekd27f8162008-01-15 23:55:06 +0000131public:
Ted Kremenekbffaa832008-01-29 05:13:23 +0000132 GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000133 Builder(NULL),
Ted Kremenek768ad162008-02-05 05:15:51 +0000134 StateMgr(G.getContext(), G.getAllocator()),
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000135 ValMgr(StateMgr.getValueManager()),
136 SymMgr(StateMgr.getSymbolManager()),
137 StmtEntryNode(NULL), CurrentStmt(NULL) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000138
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000139 // Compute liveness information.
Ted Kremenekbffaa832008-01-29 05:13:23 +0000140 Liveness.runOnCFG(G.getCFG());
141 Liveness.runOnAllBlocks(G.getCFG(), NULL, true);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000142 }
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000143
144 /// getCFG - Returns the CFG associated with this analysis.
145 CFG& getCFG() { return G.getCFG(); }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000146
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000147 /// getInitialState - Return the initial state used for the root vertex
148 /// in the ExplodedGraph.
Ted Kremenekd27f8162008-01-15 23:55:06 +0000149 StateTy getInitialState() {
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000150 StateTy St = StateMgr.getInitialState();
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000151
152 // Iterate the parameters.
153 FunctionDecl& F = G.getFunctionDecl();
154
155 for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end();
Ted Kremenek4150abf2008-01-31 00:09:56 +0000156 I!=E; ++I)
Ted Kremenek329f8542008-02-05 21:52:21 +0000157 St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I));
Ted Kremenekff6e3c52008-01-29 00:43:03 +0000158
Ted Kremenekcb48b9c2008-01-29 00:33:40 +0000159 return St;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000160 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +0000161
162 bool isUninitControlFlow(const NodeTy* N) const {
163 return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0;
164 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000165
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000166 /// ProcessStmt - Called by GREngine. Used to generate new successor
167 /// nodes by processing the 'effects' of a block-level statement.
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000168 void ProcessStmt(Stmt* S, StmtNodeBuilder& builder);
169
170 /// ProcessBranch - Called by GREngine. Used to generate successor
171 /// nodes by processing the 'effects' of a branch condition.
Ted Kremenekf233d482008-02-05 00:26:40 +0000172 void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000173
174 /// RemoveDeadBindings - Return a new state that is the same as 'M' except
175 /// that all subexpression mappings are removed and that any
176 /// block-level expressions that are not live at 'S' also have their
177 /// mappings removed.
178 StateTy RemoveDeadBindings(Stmt* S, StateTy M);
179
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000180 StateTy SetValue(StateTy St, Stmt* S, const RValue& V);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000181
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000182 StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000183 return SetValue(St, const_cast<Stmt*>(S), V);
184 }
185
Ted Kremenekcba2e432008-02-05 19:35:18 +0000186 /// SetValue - This version of SetValue is used to batch process a set
187 /// of different possible RValues and return a set of different states.
188 const StateTy::BufferTy& SetValue(StateTy St, Stmt* S,
189 const RValue::BufferTy& V,
190 StateTy::BufferTy& RetBuf);
191
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000192 StateTy SetValue(StateTy St, const LValue& LV, const RValue& V);
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000193
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194 inline RValue GetValue(const StateTy& St, Stmt* S) {
195 return StateMgr.GetValue(St, S);
196 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000197
198 inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) {
199 return StateMgr.GetValue(St, S, &hasVal);
200 }
201
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000202 inline RValue GetValue(const StateTy& St, const Stmt* S) {
Ted Kremenek9de04c42008-01-24 20:55:43 +0000203 return GetValue(St, const_cast<Stmt*>(S));
204 }
205
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000206 inline RValue GetValue(const StateTy& St, const LValue& LV) {
207 return StateMgr.GetValue(St, LV);
208 }
209
210 inline LValue GetLValue(const StateTy& St, Stmt* S) {
211 return StateMgr.GetLValue(St, S);
212 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000213
214 inline NonLValue GetRValueConstant(uint64_t X, Expr* E) {
215 return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart());
216 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000217
218 /// Assume - Create new state by assuming that a given expression
219 /// is true or false.
220 inline StateTy Assume(StateTy St, RValue Cond, bool Assumption,
221 bool& isFeasible) {
222 if (isa<LValue>(Cond))
223 return Assume(St, cast<LValue>(Cond), Assumption, isFeasible);
224 else
225 return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible);
226 }
227
228 StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible);
229 StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000230
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000231 StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V,
232 bool& isFeasible);
233
234 StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V,
235 bool& isFeasible);
236
Ted Kremenek08b66252008-02-06 04:31:33 +0000237 StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C,
238 bool& isFeasible);
239
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000240 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000241
Ted Kremenekcba2e432008-02-05 19:35:18 +0000242 /// Nodify - This version of Nodify is used to batch process a set of states.
243 /// The states are not guaranteed to be unique.
244 void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB);
245
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000246 /// Visit - Transfer function logic for all statements. Dispatches to
247 /// other functions that handle specific kinds of statements.
248 void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek874d63f2008-01-24 02:02:54 +0000249
250 /// VisitCast - Transfer function logic for all casts (implicit and explicit).
251 void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000252
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000253 /// VisitUnaryOperator - Transfer function logic for unary operators.
254 void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst);
255
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000256 /// VisitBinaryOperator - Transfer function logic for binary operators.
Ted Kremenek9de04c42008-01-24 20:55:43 +0000257 void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
258
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000259 void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000260
261 /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs.
262 void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst);
263
Ted Kremenek9de04c42008-01-24 20:55:43 +0000264 /// VisitDeclStmt - Transfer function logic for DeclStmts.
Ted Kremenekf233d482008-02-05 00:26:40 +0000265 void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst);
266
267 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose
268 void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
269 NodeTy* Pred, NodeSet& Dst);
270
271 /// VisitLogicalExpr - Transfer function logic for '&&', '||'
272 void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst);
Ted Kremenekd27f8162008-01-15 23:55:06 +0000273};
274} // end anonymous namespace
275
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000276
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000277GRConstants::StateTy
278GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000279
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000280 if (!StateCleaned) {
281 St = RemoveDeadBindings(CurrentStmt, St);
282 StateCleaned = true;
283 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000284
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000285 bool isBlkExpr = false;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000286
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000287 if (S == CurrentStmt) {
288 isBlkExpr = getCFG().isBlkExpr(S);
289
290 if (!isBlkExpr)
291 return St;
292 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000293
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000294 return StateMgr.SetValue(St, S, isBlkExpr, V);
295}
296
Ted Kremenekcba2e432008-02-05 19:35:18 +0000297const GRConstants::StateTy::BufferTy&
298GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB,
299 StateTy::BufferTy& RetBuf) {
300
301 assert (RetBuf.empty());
302
303 for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I)
304 RetBuf.push_back(SetValue(St, S, *I));
305
306 return RetBuf;
307}
308
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000309GRConstants::StateTy
310GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) {
311
312 if (!LV.isValid())
313 return St;
314
315 if (!StateCleaned) {
316 St = RemoveDeadBindings(CurrentStmt, St);
317 StateCleaned = true;
318 }
319
320 return StateMgr.SetValue(St, LV, V);
321}
322
Ted Kremenekf233d482008-02-05 00:26:40 +0000323void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term,
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000324 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000325
326 StateTy PrevState = builder.getState();
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000327
Ted Kremenekb38911f2008-01-30 23:03:39 +0000328 // Remove old bindings for subexpressions.
Ted Kremenekb80cbfe2008-02-05 18:19:15 +0000329 for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I)
Ted Kremenekb38911f2008-01-30 23:03:39 +0000330 if (I.getKey().isSubExpr())
331 PrevState = StateMgr.Remove(PrevState, I.getKey());
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000332
Ted Kremenekf233d482008-02-05 00:26:40 +0000333 // Remove terminator-specific bindings.
334 switch (Term->getStmtClass()) {
335 default: break;
336
337 case Stmt::BinaryOperatorClass: { // '&&', '||'
338 BinaryOperator* B = cast<BinaryOperator>(Term);
339 // FIXME: Liveness analysis should probably remove these automatically.
340 // Verify later when we converge to an 'optimization' stage.
341 PrevState = StateMgr.Remove(PrevState, B->getRHS());
342 break;
343 }
344
345 case Stmt::ConditionalOperatorClass: { // '?' operator
346 ConditionalOperator* C = cast<ConditionalOperator>(Term);
347 // FIXME: Liveness analysis should probably remove these automatically.
348 // Verify later when we converge to an 'optimization' stage.
349 if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L);
350 PrevState = StateMgr.Remove(PrevState, C->getRHS());
351 break;
352 }
353
354 case Stmt::ChooseExprClass: { // __builtin_choose_expr
355 ChooseExpr* C = cast<ChooseExpr>(Term);
356 // FIXME: Liveness analysis should probably remove these automatically.
357 // Verify later when we converge to an 'optimization' stage.
358 PrevState = StateMgr.Remove(PrevState, C->getRHS());
359 PrevState = StateMgr.Remove(PrevState, C->getRHS());
360 break;
361 }
362 }
363
Ted Kremenekb38911f2008-01-30 23:03:39 +0000364 RValue V = GetValue(PrevState, Condition);
365
366 switch (V.getBaseKind()) {
367 default:
368 break;
369
370 case RValue::InvalidKind:
371 builder.generateNode(PrevState, true);
372 builder.generateNode(PrevState, false);
373 return;
374
375 case RValue::UninitializedKind: {
376 NodeTy* N = builder.generateNode(PrevState, true);
377
378 if (N) {
379 N->markAsSink();
380 UninitBranches.insert(N);
381 }
382
383 builder.markInfeasible(false);
384 return;
385 }
386 }
387
388 // Process the true branch.
389 bool isFeasible = true;
Ted Kremenekf233d482008-02-05 00:26:40 +0000390
Ted Kremenekb38911f2008-01-30 23:03:39 +0000391 StateTy St = Assume(PrevState, V, true, isFeasible);
392
Ted Kremenekf233d482008-02-05 00:26:40 +0000393 if (isFeasible)
394 builder.generateNode(St, true);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000395 else {
396 builder.markInfeasible(true);
397 isFeasible = true;
398 }
399
400 // Process the false branch.
401 St = Assume(PrevState, V, false, isFeasible);
402
Ted Kremenekf233d482008-02-05 00:26:40 +0000403 if (isFeasible)
404 builder.generateNode(St, false);
405 else
406 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000407}
408
Ted Kremenekf233d482008-02-05 00:26:40 +0000409
410void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
411 NodeSet& Dst) {
412
413 bool hasR2;
414 StateTy PrevState = Pred->getState();
415
416 RValue R1 = GetValue(PrevState, B->getLHS());
417 RValue R2 = GetValue(PrevState, B->getRHS(), hasR2);
418
419 if (isa<InvalidValue>(R1) &&
420 (isa<InvalidValue>(R2) ||
421 isa<UninitializedValue>(R2))) {
422
423 Nodify(Dst, B, Pred, SetValue(PrevState, B, R2));
424 return;
425 }
426 else if (isa<UninitializedValue>(R1)) {
427 Nodify(Dst, B, Pred, SetValue(PrevState, B, R1));
428 return;
429 }
430
431 // R1 is an expression that can evaluate to either 'true' or 'false'.
432 if (B->getOpcode() == BinaryOperator::LAnd) {
433 // hasR2 == 'false' means that LHS evaluated to 'false' and that
434 // we short-circuited, leading to a value of '0' for the '&&' expression.
435 if (hasR2 == false) {
436 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
437 return;
438 }
439 }
440 else {
441 assert (B->getOpcode() == BinaryOperator::LOr);
442 // hasR2 == 'false' means that the LHS evaluate to 'true' and that
443 // we short-circuited, leading to a value of '1' for the '||' expression.
444 if (hasR2 == false) {
445 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
446 return;
447 }
448 }
449
450 // If we reach here we did not short-circuit. Assume R2 == true and
451 // R2 == false.
452
453 bool isFeasible;
454 StateTy St = Assume(PrevState, R2, true, isFeasible);
455
456 if (isFeasible)
457 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B)));
458
459 St = Assume(PrevState, R2, false, isFeasible);
460
461 if (isFeasible)
462 Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B)));
463}
464
465
466
Ted Kremenek7d7fe6d2008-01-29 22:56:11 +0000467void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
Ted Kremenekd27f8162008-01-15 23:55:06 +0000468 Builder = &builder;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000469
470 StmtEntryNode = builder.getLastNode();
471 CurrentStmt = S;
472 NodeSet Dst;
473 StateCleaned = false;
474
475 Visit(S, StmtEntryNode, Dst);
476
477 // If no nodes were generated, generate a new node that has all the
478 // dead mappings removed.
479 if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
480 StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
481 builder.generateNode(S, St, StmtEntryNode);
482 }
Ted Kremenekf84469b2008-01-18 00:41:32 +0000483
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000484 CurrentStmt = NULL;
485 StmtEntryNode = NULL;
486 Builder = NULL;
Ted Kremenekd27f8162008-01-15 23:55:06 +0000487}
488
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000489GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000490
491 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
492 // The roots are any Block-level exprs and Decls that our liveness algorithm
493 // tells us are live. We then see what Decls they may reference, and keep
494 // those around. This code more than likely can be made faster, and the
495 // frequency of which this method is called should be experimented with
496 // for optimum performance.
Ted Kremenekf84469b2008-01-18 00:41:32 +0000497
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000498 llvm::SmallVector<ValueDecl*, 10> WList;
Ted Kremenekf84469b2008-01-18 00:41:32 +0000499
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000500 for (StateTy::vb_iterator I = M.begin(), E = M.end();
501 I!=E && !I.getKey().isSymbol(); ++I) {
502
503 // Remove old bindings for subexpressions.
504 if (I.getKey().isSubExpr()) {
Ted Kremenek65cac132008-01-29 05:25:31 +0000505 M = StateMgr.Remove(M, I.getKey());
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000506 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000507 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000508
509 if (I.getKey().isBlkExpr()) {
510 if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) {
511 if (isa<lval::DeclVal>(I.getData())) {
512 lval::DeclVal LV = cast<lval::DeclVal>(I.getData());
513 WList.push_back(LV.getDecl());
514 }
515 }
516 else
517 M = StateMgr.Remove(M, I.getKey());
518
519 continue;
Ted Kremenek65cac132008-01-29 05:25:31 +0000520 }
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000521
522 assert (I.getKey().isDecl());
523
524 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
525 if (Liveness.isLive(Loc, V))
526 WList.push_back(V);
Ted Kremenek65cac132008-01-29 05:25:31 +0000527 }
Ted Kremenek565256e2008-01-24 22:44:24 +0000528
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000529 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
530
531 while (!WList.empty()) {
532 ValueDecl* V = WList.back();
533 WList.pop_back();
534
535 if (Marked.count(V))
536 continue;
537
538 Marked.insert(V);
539
540 if (V->getType()->isPointerType()) {
541 const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V)));
542
543 if (!isa<lval::DeclVal>(LV))
544 continue;
545
546 const lval::DeclVal& LVD = cast<lval::DeclVal>(LV);
547 WList.push_back(LVD.getDecl());
548 }
549 }
550
551 for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I)
552 if (I.getKey().isDecl())
553 if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey())))
554 if (!Marked.count(V))
555 M = StateMgr.Remove(M, V);
556
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000557 return M;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000558}
559
Ted Kremenekcba2e432008-02-05 19:35:18 +0000560void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000561
562 // If the state hasn't changed, don't generate a new node.
563 if (St == Pred->getState())
564 return;
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000565
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000566 Dst.Add(Builder->generateNode(S, St, Pred));
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000567}
Ted Kremenekd27f8162008-01-15 23:55:06 +0000568
Ted Kremenekcba2e432008-02-05 19:35:18 +0000569void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
570 const StateTy::BufferTy& SB) {
571
572 for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
573 Nodify(Dst, S, Pred, *I);
574}
575
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000576void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) {
577 if (D != CurrentStmt) {
578 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
579 return;
580 }
581
582 // If we are here, we are loading the value of the decl and binding
583 // it to the block-level expression.
584
585 StateTy St = Pred->getState();
586
587 Nodify(Dst, D, Pred,
588 SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl()))));
589}
590
Ted Kremenekcba2e432008-02-05 19:35:18 +0000591void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek874d63f2008-01-24 02:02:54 +0000592
593 QualType T = CastE->getType();
594
595 // Check for redundant casts.
596 if (E->getType() == T) {
597 Dst.Add(Pred);
598 return;
599 }
600
601 NodeSet S1;
602 Visit(E, Pred, S1);
603
604 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
605 NodeTy* N = *I1;
606 StateTy St = N->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000607 const RValue& V = GetValue(St, E);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000608 Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE)));
Ted Kremenek874d63f2008-01-24 02:02:54 +0000609 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000610}
611
612void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred,
613 GRConstants::NodeSet& Dst) {
614
615 StateTy St = Pred->getState();
616
617 for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
Ted Kremenek403c1812008-01-28 22:51:57 +0000618 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
619 const Expr* E = VD->getInit();
Ted Kremenek329f8542008-02-05 21:52:21 +0000620 St = SetValue(St, lval::DeclVal(VD),
Ted Kremenek403c1812008-01-28 22:51:57 +0000621 E ? GetValue(St, E) : UninitializedValue());
622 }
Ted Kremenek9de04c42008-01-24 20:55:43 +0000623
624 Nodify(Dst, DS, Pred, St);
625
626 if (Dst.empty())
627 Dst.Add(Pred);
628}
Ted Kremenek874d63f2008-01-24 02:02:54 +0000629
Ted Kremenekf233d482008-02-05 00:26:40 +0000630
631void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS,
632 NodeTy* Pred, NodeSet& Dst) {
633
634 StateTy St = Pred->getState();
635
636 RValue R = GetValue(St, LHS);
637 if (isa<InvalidValue>(R)) R = GetValue(St, RHS);
638
639 Nodify(Dst, S, Pred, SetValue(St, S, R));
640}
641
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000642void GRConstants::VisitUnaryOperator(UnaryOperator* U,
643 GRConstants::NodeTy* Pred,
644 GRConstants::NodeSet& Dst) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000645
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000646 NodeSet S1;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000647 UnaryOperator::Opcode Op = U->getOpcode();
648
649 // FIXME: This is a hack so that for '*' and '&' we don't recurse
650 // on visiting the subexpression if it is a DeclRefExpr. We should
651 // probably just handle AddrOf and Deref in their own methods to make
652 // this cleaner.
653 if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) &&
654 isa<DeclRefExpr>(U->getSubExpr()))
655 S1.Add(Pred);
656 else
657 Visit(U->getSubExpr(), Pred, S1);
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000658
659 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
660 NodeTy* N1 = *I1;
661 StateTy St = N1->getState();
662
663 switch (U->getOpcode()) {
664 case UnaryOperator::PostInc: {
665 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000666 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000667
668 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
669 GetRValueConstant(1U, U));
670
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000671 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
672 break;
673 }
674
675 case UnaryOperator::PostDec: {
676 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000677 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000678
679 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
680 GetRValueConstant(1U, U));
681
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000682 Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result));
683 break;
684 }
685
686 case UnaryOperator::PreInc: {
687 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000688 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000689
690 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add,
691 GetRValueConstant(1U, U));
692
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000693 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
694 break;
695 }
696
697 case UnaryOperator::PreDec: {
698 const LValue& L1 = GetLValue(St, U->getSubExpr());
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000699 NonLValue R1 = cast<NonLValue>(GetValue(St, L1));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000700
701 NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub,
702 GetRValueConstant(1U, U));
703
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000704 Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result));
705 break;
706 }
707
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000708 case UnaryOperator::Minus: {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000709 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000710 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U)));
Ted Kremenekdacbb4f2008-01-24 08:20:02 +0000711 break;
712 }
713
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000714 case UnaryOperator::Not: {
715 const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr()));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000716 Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr)));
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000717 break;
718 }
719
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000720 case UnaryOperator::LNot: {
721 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
722 //
723 // Note: technically we do "E == 0", but this is the same in the
724 // transfer functions as "0 == E".
725
726 RValue V1 = GetValue(St, U->getSubExpr());
727
728 if (isa<LValue>(V1)) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000729 const LValue& L1 = cast<LValue>(V1);
730 lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
731 Nodify(Dst, U, N1,
732 SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
733 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000734 }
735 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000736 const NonLValue& R1 = cast<NonLValue>(V1);
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000737 nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000738 Nodify(Dst, U, N1,
739 SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ,
740 V2)));
Ted Kremenekc60f0f72008-02-06 17:56:00 +0000741 }
742
743 break;
744 }
745
Ted Kremenek64924852008-01-31 02:35:41 +0000746 case UnaryOperator::AddrOf: {
747 const LValue& L1 = GetLValue(St, U->getSubExpr());
748 Nodify(Dst, U, N1, SetValue(St, U, L1));
749 break;
750 }
751
752 case UnaryOperator::Deref: {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000753 // FIXME: Stop when dereferencing an uninitialized value.
754 // FIXME: Bifurcate when dereferencing a symbolic with no constraints?
755
756 const RValue& V = GetValue(St, U->getSubExpr());
757 const LValue& L1 = cast<LValue>(V);
758
Ted Kremenek64924852008-01-31 02:35:41 +0000759 Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1)));
760 break;
761 }
762
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000763 default: ;
764 assert (false && "Not implemented.");
765 }
766 }
767}
768
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000769void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred,
770 GRConstants::NodeSet& Dst) {
771
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000772 if (isa<DeclRefExpr>(E)) {
773 Dst.Add(Pred);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000774 return;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000775 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000776
777 if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) {
778 if (U->getOpcode() == UnaryOperator::Deref) {
779 Visit(U->getSubExpr(), Pred, Dst);
780 return;
781 }
782 }
783
784 Visit(E, Pred, Dst);
785}
786
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000787void GRConstants::VisitBinaryOperator(BinaryOperator* B,
788 GRConstants::NodeTy* Pred,
789 GRConstants::NodeSet& Dst) {
790 NodeSet S1;
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000791
792 if (B->isAssignmentOp())
793 VisitAssignmentLHS(B->getLHS(), Pred, S1);
794 else
795 Visit(B->getLHS(), Pred, S1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000796
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000797 for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
798 NodeTy* N1 = *I1;
Ted Kremeneke00fe3f2008-01-17 00:52:48 +0000799
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000800 // When getting the value for the LHS, check if we are in an assignment.
801 // In such cases, we want to (initially) treat the LHS as an LValue,
802 // so we use GetLValue instead of GetValue so that DeclRefExpr's are
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000803 // evaluated to LValueDecl's instead of to an NonLValue.
804 const RValue& V1 =
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000805 B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS())
806 : GetValue(N1->getState(), B->getLHS());
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000807
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000808 NodeSet S2;
809 Visit(B->getRHS(), N1, S2);
810
811 for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000812
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000813 NodeTy* N2 = *I2;
814 StateTy St = N2->getState();
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000815 const RValue& V2 = GetValue(St, B->getRHS());
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000816
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000817 BinaryOperator::Opcode Op = B->getOpcode();
818
819 if (Op <= BinaryOperator::Or) {
820
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000821 if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) {
822 Nodify(Dst, B, N2, SetValue(St, B, V1));
823 continue;
824 }
825
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000826 if (isa<LValue>(V1)) {
827 // FIXME: Add support for RHS being a non-lvalue.
828 const LValue& L1 = cast<LValue>(V1);
829 const LValue& L2 = cast<LValue>(V2);
Ted Kremenek687af802008-01-29 19:43:15 +0000830
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000831 Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2)));
832 }
833 else {
Ted Kremenekbd03f1d2008-01-28 22:09:13 +0000834 const NonLValue& R1 = cast<NonLValue>(V1);
835 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000836
837 Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2)));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000838 }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000839
840 continue;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000841
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000842 }
843
844 switch (Op) {
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000845 case BinaryOperator::Assign: {
846 const LValue& L1 = cast<LValue>(V1);
Ted Kremenek3434b082008-02-06 04:41:14 +0000847 Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000848 break;
849 }
850
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000851 default: { // Compound assignment operators.
Ted Kremenek687af802008-01-29 19:43:15 +0000852
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000853 assert (B->isCompoundAssignmentOp());
854
855 const LValue& L1 = cast<LValue>(V1);
856 RValue Result = cast<NonLValue>(InvalidValue());
857
858 Op = (BinaryOperator::Opcode)
859 (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign));
860
861 if (isa<LValue>(V2)) {
862 // FIXME: Add support for Non-LValues on RHS.
Ted Kremenek687af802008-01-29 19:43:15 +0000863 const LValue& L2 = cast<LValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000864 Result = L1.EvalBinaryOp(ValMgr, Op, L2);
Ted Kremenek687af802008-01-29 19:43:15 +0000865 }
866 else {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000867 const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1));
Ted Kremenek687af802008-01-29 19:43:15 +0000868 const NonLValue& R2 = cast<NonLValue>(V2);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000869 Result = R1.EvalBinaryOp(ValMgr, Op, R2);
Ted Kremenek687af802008-01-29 19:43:15 +0000870 }
871
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000872 Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result));
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000873 break;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000874 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000875 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +0000876 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000877 }
Ted Kremenekd27f8162008-01-15 23:55:06 +0000878}
Ted Kremenekee985462008-01-16 18:18:48 +0000879
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000880
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000881void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred,
882 GRConstants::NodeSet& Dst) {
883
884 // FIXME: add metadata to the CFG so that we can disable
885 // this check when we KNOW that there is no block-level subexpression.
886 // The motivation is that this check requires a hashtable lookup.
887
888 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
889 Dst.Add(Pred);
890 return;
891 }
892
893 switch (S->getStmtClass()) {
894 case Stmt::BinaryOperatorClass:
Ted Kremenekf233d482008-02-05 00:26:40 +0000895
896 if (cast<BinaryOperator>(S)->isLogicalOp()) {
897 VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst);
898 break;
899 }
900
901 // Fall-through.
902
Ted Kremenekb4ae33f2008-01-23 23:38:00 +0000903 case Stmt::CompoundAssignOperatorClass:
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000904 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
905 break;
906
Ted Kremenek7b8009a2008-01-24 02:28:56 +0000907 case Stmt::UnaryOperatorClass:
908 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
909 break;
910
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000911 case Stmt::ParenExprClass:
912 Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
913 break;
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000914
915 case Stmt::DeclRefExprClass:
916 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
917 break;
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000918
Ted Kremenek874d63f2008-01-24 02:02:54 +0000919 case Stmt::ImplicitCastExprClass: {
920 ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
921 VisitCast(C, C->getSubExpr(), Pred, Dst);
922 break;
923 }
924
925 case Stmt::CastExprClass: {
926 CastExpr* C = cast<CastExpr>(S);
927 VisitCast(C, C->getSubExpr(), Pred, Dst);
928 break;
929 }
930
Ted Kremenekf233d482008-02-05 00:26:40 +0000931 case Stmt::ConditionalOperatorClass: { // '?' operator
932 ConditionalOperator* C = cast<ConditionalOperator>(S);
933 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
934 break;
935 }
936
937 case Stmt::ChooseExprClass: { // __builtin_choose_expr
938 ChooseExpr* C = cast<ChooseExpr>(S);
939 VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst);
940 break;
941 }
942
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +0000943 case Stmt::ReturnStmtClass:
944 if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
945 Visit(R, Pred, Dst);
946 else
947 Dst.Add(Pred);
948
949 break;
950
Ted Kremenek9de04c42008-01-24 20:55:43 +0000951 case Stmt::DeclStmtClass:
952 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
953 break;
954
Ted Kremenekab2b8c52008-01-23 19:59:44 +0000955 default:
956 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
957 break;
Ted Kremenek79649df2008-01-17 18:25:22 +0000958 }
Ted Kremenek1ccd31c2008-01-16 19:42:59 +0000959}
960
Ted Kremenekee985462008-01-16 18:18:48 +0000961//===----------------------------------------------------------------------===//
Ted Kremenekb38911f2008-01-30 23:03:39 +0000962// "Assume" logic.
963//===----------------------------------------------------------------------===//
964
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000965GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond,
966 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000967 bool& isFeasible) {
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000968
969 switch (Cond.getSubKind()) {
970 default:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000971 assert (false && "'Assume' not implemented for this LValue.");
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000972 return St;
973
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000974 case lval::SymbolValKind:
975 if (Assumption)
976 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
977 ValMgr.getZeroWithPtrWidth(), isFeasible);
978 else
979 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
980 ValMgr.getZeroWithPtrWidth(), isFeasible);
981
Ted Kremenek08b66252008-02-06 04:31:33 +0000982
Ted Kremenek329f8542008-02-05 21:52:21 +0000983 case lval::DeclValKind:
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000984 isFeasible = Assumption;
985 return St;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000986
Ted Kremenek329f8542008-02-05 21:52:21 +0000987 case lval::ConcreteIntKind: {
988 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000989 isFeasible = b ? Assumption : !Assumption;
990 return St;
991 }
992 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000993}
994
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000995GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond,
996 bool Assumption,
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000997 bool& isFeasible) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000998
999 switch (Cond.getSubKind()) {
1000 default:
1001 assert (false && "'Assume' not implemented for this NonLValue.");
1002 return St;
1003
Ted Kremenekfeb01f62008-02-06 17:32:17 +00001004
1005 case nonlval::SymbolValKind: {
1006 lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond);
1007 SymbolID sym = SV.getSymbol();
1008
1009 if (Assumption)
1010 return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1011 isFeasible);
1012 else
1013 return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
1014 isFeasible);
1015 }
1016
Ted Kremenek08b66252008-02-06 04:31:33 +00001017 case nonlval::SymIntConstraintValKind:
1018 return
1019 AssumeSymInt(St, Assumption,
1020 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
1021 isFeasible);
1022
Ted Kremenek329f8542008-02-05 21:52:21 +00001023 case nonlval::ConcreteIntKind: {
1024 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
Ted Kremenekb38911f2008-01-30 23:03:39 +00001025 isFeasible = b ? Assumption : !Assumption;
1026 return St;
1027 }
1028 }
1029}
1030
Ted Kremenek862d5bb2008-02-06 00:54:14 +00001031GRConstants::StateTy
1032GRConstants::AssumeSymNE(StateTy St, SymbolID sym,
1033 const llvm::APSInt& V, bool& isFeasible) {
1034
1035 // First, determine if sym == X, where X != V.
1036 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1037 isFeasible = *X != V;
1038 return St;
1039 }
1040
1041 // Second, determine if sym != V.
1042 if (St.isNotEqual(sym, V)) {
1043 isFeasible = true;
1044 return St;
1045 }
1046
1047 // If we reach here, sym is not a constant and we don't know if it is != V.
1048 // Make that assumption.
1049
1050 isFeasible = true;
1051 return StateMgr.AddNE(St, sym, V);
1052}
1053
1054GRConstants::StateTy
1055GRConstants::AssumeSymEQ(StateTy St, SymbolID sym,
1056 const llvm::APSInt& V, bool& isFeasible) {
1057
1058 // First, determine if sym == X, where X != V.
1059 if (const llvm::APSInt* X = St.getSymVal(sym)) {
1060 isFeasible = *X == V;
1061 return St;
1062 }
1063
1064 // Second, determine if sym != V.
1065 if (St.isNotEqual(sym, V)) {
1066 isFeasible = false;
1067 return St;
1068 }
1069
1070 // If we reach here, sym is not a constant and we don't know if it is == V.
1071 // Make that assumption.
1072
1073 isFeasible = true;
1074 return StateMgr.AddEQ(St, sym, V);
1075}
Ted Kremenekb38911f2008-01-30 23:03:39 +00001076
Ted Kremenek08b66252008-02-06 04:31:33 +00001077GRConstants::StateTy
1078GRConstants::AssumeSymInt(StateTy St, bool Assumption,
1079 const SymIntConstraint& C, bool& isFeasible) {
1080
1081 switch (C.getOpcode()) {
1082 default:
1083 // No logic yet for other operators.
1084 return St;
1085
1086 case BinaryOperator::EQ:
1087 if (Assumption)
1088 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1089 else
1090 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1091
1092 case BinaryOperator::NE:
1093 if (Assumption)
1094 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
1095 else
1096 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
1097 }
1098}
1099
Ted Kremenekb38911f2008-01-30 23:03:39 +00001100//===----------------------------------------------------------------------===//
Ted Kremenekee985462008-01-16 18:18:48 +00001101// Driver.
1102//===----------------------------------------------------------------------===//
1103
Ted Kremenekaa66a322008-01-16 21:46:15 +00001104#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001105static GRConstants* GraphPrintCheckerState;
1106
Ted Kremenekaa66a322008-01-16 21:46:15 +00001107namespace llvm {
1108template<>
1109struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> :
1110 public DefaultDOTGraphTraits {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001111
Ted Kremenek9153f732008-02-05 07:17:49 +00001112 static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) {
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001113 switch (kind) {
Ted Kremenek9153f732008-02-05 07:17:49 +00001114 case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break;
1115 case VarBindKey::IsDecl: Out << "Variables:\\l"; break;
1116 case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break;
1117 default: assert (false && "Unknown VarBindKey type.");
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001118 }
1119 }
1120
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001121 static void PrintKind(std::ostream& Out, GRConstants::StateTy M,
Ted Kremenek9153f732008-02-05 07:17:49 +00001122 VarBindKey::Kind kind, bool isFirstGroup = false) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001123 bool isFirst = true;
1124
Ted Kremenekb80cbfe2008-02-05 18:19:15 +00001125 for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) {
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001126 if (I.getKey().getKind() != kind)
1127 continue;
1128
1129 if (isFirst) {
1130 if (!isFirstGroup) Out << "\\l\\l";
1131 PrintKindLabel(Out, kind);
1132 isFirst = false;
1133 }
1134 else
1135 Out << "\\l";
1136
1137 Out << ' ';
1138
1139 if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey()))
1140 Out << V->getName();
1141 else {
1142 Stmt* E = cast<Stmt>(I.getKey());
1143 Out << " (" << (void*) E << ") ";
1144 E->printPretty(Out);
1145 }
1146
1147 Out << " : ";
1148 I.getData().print(Out);
1149 }
1150 }
1151
Ted Kremeneked4de312008-02-06 03:56:15 +00001152 static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) {
1153 ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq;
1154
1155 if (CE.isEmpty())
1156 return;
1157
1158 Out << "\\l\\|'==' constraints:";
1159
1160 for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
1161 Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
1162 }
1163
1164 static void PrintNE(std::ostream& Out, GRConstants::StateTy St) {
1165 ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq;
1166
1167 if (NE.isEmpty())
1168 return;
1169
1170 Out << "\\l\\|'!=' constraints:";
1171
1172 for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end();
1173 I != EI; ++I){
1174
1175 Out << "\\l $" << I.getKey() << " : ";
1176 bool isFirst = true;
1177
1178 ValueState::IntSetTy::iterator J=I.getData().begin(),
1179 EJ=I.getData().end();
1180 for ( ; J != EJ; ++J) {
1181 if (isFirst) isFirst = false;
1182 else Out << ", ";
1183
1184 Out << (*J)->toString();
1185 }
1186 }
1187 }
1188
Ted Kremenekaa66a322008-01-16 21:46:15 +00001189 static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) {
1190 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001191
1192 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00001193 ProgramPoint Loc = N->getLocation();
1194
1195 switch (Loc.getKind()) {
1196 case ProgramPoint::BlockEntranceKind:
1197 Out << "Block Entrance: B"
1198 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
1199 break;
1200
1201 case ProgramPoint::BlockExitKind:
1202 assert (false);
1203 break;
1204
1205 case ProgramPoint::PostStmtKind: {
1206 const PostStmt& L = cast<PostStmt>(Loc);
Ted Kremenek9ff731d2008-01-24 22:27:20 +00001207 Out << L.getStmt()->getStmtClassName() << ':'
1208 << (void*) L.getStmt() << ' ';
1209
Ted Kremenekaa66a322008-01-16 21:46:15 +00001210 L.getStmt()->printPretty(Out);
1211 break;
1212 }
1213
1214 default: {
1215 const BlockEdge& E = cast<BlockEdge>(Loc);
1216 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
1217 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00001218
1219 if (Stmt* T = E.getSrc()->getTerminator()) {
1220 Out << "\\|Terminator: ";
1221 E.getSrc()->printTerminator(Out);
1222
1223 if (isa<SwitchStmt>(T)) {
1224 // FIXME
1225 }
1226 else {
1227 Out << "\\lCondition: ";
1228 if (*E.getSrc()->succ_begin() == E.getDst())
1229 Out << "true";
1230 else
1231 Out << "false";
1232 }
1233
1234 Out << "\\l";
1235 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001236
1237 if (GraphPrintCheckerState->isUninitControlFlow(N)) {
1238 Out << "\\|Control-flow based on\\lUninitialized value.\\l";
1239 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00001240 }
1241 }
1242
Ted Kremenek9153f732008-02-05 07:17:49 +00001243 Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001244
Ted Kremenek9153f732008-02-05 07:17:49 +00001245 PrintKind(Out, N->getState(), VarBindKey::IsDecl, true);
1246 PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr);
1247 PrintKind(Out, N->getState(), VarBindKey::IsSubExpr);
Ted Kremeneked4de312008-02-06 03:56:15 +00001248
1249 PrintEQ(Out, N->getState());
1250 PrintNE(Out, N->getState());
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001251
Ted Kremenek803c9ed2008-01-23 22:30:44 +00001252 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00001253 return Out.str();
1254 }
1255};
1256} // end llvm namespace
1257#endif
1258
Ted Kremenekee985462008-01-16 18:18:48 +00001259namespace clang {
Ted Kremenekcb48b9c2008-01-29 00:33:40 +00001260void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) {
1261 GREngine<GRConstants> Engine(cfg, FD, Ctx);
Ted Kremenekee985462008-01-16 18:18:48 +00001262 Engine.ExecuteWorkList();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001263#ifndef NDEBUG
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001264 GraphPrintCheckerState = &Engine.getCheckerState();
Ted Kremenekaa66a322008-01-16 21:46:15 +00001265 llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants");
Ted Kremenek3b4f6702008-01-30 23:24:39 +00001266 GraphPrintCheckerState = NULL;
Ted Kremenekaa66a322008-01-16 21:46:15 +00001267#endif
Ted Kremenekee985462008-01-16 18:18:48 +00001268}
Ted Kremenekab2b8c52008-01-23 19:59:44 +00001269} // end clang namespace