blob: 17348cd028ea21a488e02d0656537aed683b0db8 [file] [log] [blame]
Chris Lattner10f2d132009-11-11 00:22:30 +00001//===- LazyValueInfo.cpp - Value constraint analysis ----------------------===//
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//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerb8c124c2009-11-12 01:22:16 +000015#define DEBUG_TYPE "lazy-value-info"
Chris Lattner10f2d132009-11-11 00:22:30 +000016#include "llvm/Analysis/LazyValueInfo.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000017#include "llvm/Analysis/ValueTracking.h"
Chris Lattnercc4d3b22009-11-11 02:08:33 +000018#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
20#include "llvm/Analysis/ConstantFolding.h"
21#include "llvm/Target/TargetData.h"
Chris Lattner16976522009-11-11 22:48:44 +000022#include "llvm/Support/CFG.h"
Owen Anderson5be2e782010-08-05 22:59:19 +000023#include "llvm/Support/ConstantRange.h"
Chris Lattnerb8c124c2009-11-12 01:22:16 +000024#include "llvm/Support/Debug.h"
Chris Lattner16976522009-11-11 22:48:44 +000025#include "llvm/Support/raw_ostream.h"
Owen Anderson7f9cb742010-07-30 23:59:40 +000026#include "llvm/Support/ValueHandle.h"
Chris Lattner16976522009-11-11 22:48:44 +000027#include "llvm/ADT/DenseMap.h"
Owen Anderson9a65dc92010-07-27 23:58:11 +000028#include "llvm/ADT/DenseSet.h"
Chris Lattnere5642812009-11-15 20:00:52 +000029#include "llvm/ADT/STLExtras.h"
Owen Anderson6bcd3a02010-09-07 19:16:25 +000030#include <map>
31#include <set>
Nick Lewycky90862ee2010-12-18 01:00:40 +000032#include <stack>
Chris Lattner10f2d132009-11-11 00:22:30 +000033using namespace llvm;
34
35char LazyValueInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000036INITIALIZE_PASS(LazyValueInfo, "lazy-value-info",
Owen Andersonce665bd2010-10-07 22:25:06 +000037 "Lazy Value Information Analysis", false, true)
Chris Lattner10f2d132009-11-11 00:22:30 +000038
39namespace llvm {
40 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
41}
42
Chris Lattnercc4d3b22009-11-11 02:08:33 +000043
44//===----------------------------------------------------------------------===//
45// LVILatticeVal
46//===----------------------------------------------------------------------===//
47
48/// LVILatticeVal - This is the information tracked by LazyValueInfo for each
49/// value.
50///
51/// FIXME: This is basically just for bringup, this can be made a lot more rich
52/// in the future.
53///
54namespace {
55class LVILatticeVal {
56 enum LatticeValueTy {
Nick Lewycky69bfdf52010-12-15 18:57:18 +000057 /// undefined - This Value has no known value yet.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000058 undefined,
Owen Anderson5be2e782010-08-05 22:59:19 +000059
Nick Lewycky69bfdf52010-12-15 18:57:18 +000060 /// constant - This Value has a specific constant value.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000061 constant,
Nick Lewycky69bfdf52010-12-15 18:57:18 +000062 /// notconstant - This Value is known to not have the specified value.
Chris Lattnerb52675b2009-11-12 04:36:58 +000063 notconstant,
64
Nick Lewycky69bfdf52010-12-15 18:57:18 +000065 /// constantrange - The Value falls within this range.
Owen Anderson5be2e782010-08-05 22:59:19 +000066 constantrange,
67
Nick Lewycky69bfdf52010-12-15 18:57:18 +000068 /// overdefined - This value is not known to be constant, and we know that
Chris Lattnercc4d3b22009-11-11 02:08:33 +000069 /// it has a value.
70 overdefined
71 };
72
73 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattnerb52675b2009-11-12 04:36:58 +000074 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersondb78d732010-08-05 22:10:46 +000075 LatticeValueTy Tag;
76 Constant *Val;
Owen Anderson5be2e782010-08-05 22:59:19 +000077 ConstantRange Range;
Chris Lattnercc4d3b22009-11-11 02:08:33 +000078
79public:
Owen Anderson5be2e782010-08-05 22:59:19 +000080 LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {}
Chris Lattnercc4d3b22009-11-11 02:08:33 +000081
Chris Lattner16976522009-11-11 22:48:44 +000082 static LVILatticeVal get(Constant *C) {
83 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000084 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000085 Res.markConstant(C);
Chris Lattner16976522009-11-11 22:48:44 +000086 return Res;
87 }
Chris Lattnerb52675b2009-11-12 04:36:58 +000088 static LVILatticeVal getNot(Constant *C) {
89 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000090 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000091 Res.markNotConstant(C);
Chris Lattnerb52675b2009-11-12 04:36:58 +000092 return Res;
93 }
Owen Anderson625051b2010-08-10 23:20:01 +000094 static LVILatticeVal getRange(ConstantRange CR) {
95 LVILatticeVal Res;
96 Res.markConstantRange(CR);
97 return Res;
98 }
Chris Lattner16976522009-11-11 22:48:44 +000099
Owen Anderson5be2e782010-08-05 22:59:19 +0000100 bool isUndefined() const { return Tag == undefined; }
101 bool isConstant() const { return Tag == constant; }
102 bool isNotConstant() const { return Tag == notconstant; }
103 bool isConstantRange() const { return Tag == constantrange; }
104 bool isOverdefined() const { return Tag == overdefined; }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000105
106 Constant *getConstant() const {
107 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000108 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000109 }
110
Chris Lattnerb52675b2009-11-12 04:36:58 +0000111 Constant *getNotConstant() const {
112 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000113 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000114 }
115
Owen Anderson5be2e782010-08-05 22:59:19 +0000116 ConstantRange getConstantRange() const {
117 assert(isConstantRange() &&
118 "Cannot get the constant-range of a non-constant-range!");
119 return Range;
120 }
121
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000122 /// markOverdefined - Return true if this is a change in status.
123 bool markOverdefined() {
124 if (isOverdefined())
125 return false;
Owen Andersondb78d732010-08-05 22:10:46 +0000126 Tag = overdefined;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000127 return true;
128 }
129
130 /// markConstant - Return true if this is a change in status.
131 bool markConstant(Constant *V) {
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000132 assert(V && "Marking constant with NULL");
133 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
134 return markConstantRange(ConstantRange(CI->getValue()));
135 if (isa<UndefValue>(V))
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000136 return false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000137
138 assert((!isConstant() || getConstant() == V) &&
139 "Marking constant with different value");
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000140 assert(isUndefined());
Owen Andersondb78d732010-08-05 22:10:46 +0000141 Tag = constant;
Owen Andersondb78d732010-08-05 22:10:46 +0000142 Val = V;
Chris Lattner16976522009-11-11 22:48:44 +0000143 return true;
144 }
145
Chris Lattnerb52675b2009-11-12 04:36:58 +0000146 /// markNotConstant - Return true if this is a change in status.
147 bool markNotConstant(Constant *V) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000148 assert(V && "Marking constant with NULL");
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000149 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
150 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
151 if (isa<UndefValue>(V))
152 return false;
153
154 assert((!isConstant() || getConstant() != V) &&
155 "Marking constant !constant with same value");
156 assert((!isNotConstant() || getNotConstant() == V) &&
157 "Marking !constant with different value");
158 assert(isUndefined() || isConstant());
159 Tag = notconstant;
Owen Andersondb78d732010-08-05 22:10:46 +0000160 Val = V;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000161 return true;
162 }
163
Owen Anderson5be2e782010-08-05 22:59:19 +0000164 /// markConstantRange - Return true if this is a change in status.
165 bool markConstantRange(const ConstantRange NewR) {
166 if (isConstantRange()) {
167 if (NewR.isEmptySet())
168 return markOverdefined();
169
Owen Anderson5be2e782010-08-05 22:59:19 +0000170 bool changed = Range == NewR;
171 Range = NewR;
172 return changed;
173 }
174
175 assert(isUndefined());
176 if (NewR.isEmptySet())
177 return markOverdefined();
Owen Anderson5be2e782010-08-05 22:59:19 +0000178
179 Tag = constantrange;
180 Range = NewR;
181 return true;
182 }
183
Chris Lattner16976522009-11-11 22:48:44 +0000184 /// mergeIn - Merge the specified lattice value into this one, updating this
185 /// one and returning true if anything changed.
186 bool mergeIn(const LVILatticeVal &RHS) {
187 if (RHS.isUndefined() || isOverdefined()) return false;
188 if (RHS.isOverdefined()) return markOverdefined();
189
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000190 if (isUndefined()) {
191 Tag = RHS.Tag;
192 Val = RHS.Val;
193 Range = RHS.Range;
194 return true;
Chris Lattnerf496e792009-11-12 04:57:13 +0000195 }
196
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000197 if (isConstant()) {
198 if (RHS.isConstant()) {
199 if (Val == RHS.Val)
200 return false;
201 return markOverdefined();
202 }
203
204 if (RHS.isNotConstant()) {
205 if (Val == RHS.Val)
206 return markOverdefined();
207
208 // Unless we can prove that the two Constants are different, we must
209 // move to overdefined.
210 // FIXME: use TargetData for smarter constant folding.
211 if (ConstantInt *Res = dyn_cast<ConstantInt>(
212 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
213 getConstant(),
214 RHS.getNotConstant())))
215 if (Res->isOne())
216 return markNotConstant(RHS.getNotConstant());
217
218 return markOverdefined();
219 }
220
221 // RHS is a ConstantRange, LHS is a non-integer Constant.
222
223 // FIXME: consider the case where RHS is a range [1, 0) and LHS is
224 // a function. The correct result is to pick up RHS.
225
Chris Lattner16976522009-11-11 22:48:44 +0000226 return markOverdefined();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000227 }
228
229 if (isNotConstant()) {
230 if (RHS.isConstant()) {
231 if (Val == RHS.Val)
232 return markOverdefined();
233
234 // Unless we can prove that the two Constants are different, we must
235 // move to overdefined.
236 // FIXME: use TargetData for smarter constant folding.
237 if (ConstantInt *Res = dyn_cast<ConstantInt>(
238 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
239 getNotConstant(),
240 RHS.getConstant())))
241 if (Res->isOne())
242 return false;
243
244 return markOverdefined();
245 }
246
247 if (RHS.isNotConstant()) {
248 if (Val == RHS.Val)
249 return false;
250 return markOverdefined();
251 }
252
253 return markOverdefined();
254 }
255
256 assert(isConstantRange() && "New LVILattice type?");
257 if (!RHS.isConstantRange())
258 return markOverdefined();
259
260 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
261 if (NewR.isFullSet())
262 return markOverdefined();
263 return markConstantRange(NewR);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000264 }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000265};
266
267} // end anonymous namespace.
268
Chris Lattner16976522009-11-11 22:48:44 +0000269namespace llvm {
270raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
271 if (Val.isUndefined())
272 return OS << "undefined";
273 if (Val.isOverdefined())
274 return OS << "overdefined";
Chris Lattnerb52675b2009-11-12 04:36:58 +0000275
276 if (Val.isNotConstant())
277 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson2f3ffb82010-08-09 20:50:46 +0000278 else if (Val.isConstantRange())
279 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
280 << Val.getConstantRange().getUpper() << '>';
Chris Lattner16976522009-11-11 22:48:44 +0000281 return OS << "constant<" << *Val.getConstant() << '>';
282}
283}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000284
285//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000286// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000287//===----------------------------------------------------------------------===//
288
Chris Lattner2c5adf82009-11-15 19:59:49 +0000289namespace {
Owen Anderson89778462011-01-05 21:15:29 +0000290 /// LVIValueHandle - A callback value handle update the cache when
291 /// values are erased.
292 class LazyValueInfoCache;
293 struct LVIValueHandle : public CallbackVH {
294 LazyValueInfoCache *Parent;
295
296 LVIValueHandle(Value *V, LazyValueInfoCache *P)
297 : CallbackVH(V), Parent(P) { }
298
299 void deleted();
300 void allUsesReplacedWith(Value *V) {
301 deleted();
302 }
303 };
304}
305
306namespace llvm {
307 template<>
308 struct DenseMapInfo<LVIValueHandle> {
309 typedef DenseMapInfo<Value*> PointerInfo;
310 static inline LVIValueHandle getEmptyKey() {
311 return LVIValueHandle(PointerInfo::getEmptyKey(),
312 static_cast<LazyValueInfoCache*>(0));
313 }
314 static inline LVIValueHandle getTombstoneKey() {
315 return LVIValueHandle(PointerInfo::getTombstoneKey(),
316 static_cast<LazyValueInfoCache*>(0));
317 }
318 static unsigned getHashValue(const LVIValueHandle &Val) {
319 return PointerInfo::getHashValue(Val);
320 }
321 static bool isEqual(const LVIValueHandle &LHS, const LVIValueHandle &RHS) {
322 return LHS == RHS;
323 }
324 };
325
326 template<>
327 struct DenseMapInfo<std::pair<AssertingVH<BasicBlock>, Value*> > {
328 typedef std::pair<AssertingVH<BasicBlock>, Value*> PairTy;
329 typedef DenseMapInfo<AssertingVH<BasicBlock> > APointerInfo;
330 typedef DenseMapInfo<Value*> BPointerInfo;
331 static inline PairTy getEmptyKey() {
332 return std::make_pair(APointerInfo::getEmptyKey(),
333 BPointerInfo::getEmptyKey());
334 }
335 static inline PairTy getTombstoneKey() {
336 return std::make_pair(APointerInfo::getTombstoneKey(),
337 BPointerInfo::getTombstoneKey());
338 }
339 static unsigned getHashValue( const PairTy &Val) {
340 return APointerInfo::getHashValue(Val.first) ^
341 BPointerInfo::getHashValue(Val.second);
342 }
343 static bool isEqual(const PairTy &LHS, const PairTy &RHS) {
344 return APointerInfo::isEqual(LHS.first, RHS.first) &&
345 BPointerInfo::isEqual(LHS.second, RHS.second);
346 }
347 };
348}
349
350namespace {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000351 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
352 /// maintains information about queries across the clients' queries.
353 class LazyValueInfoCache {
Owen Anderson81881bc2010-07-30 20:56:07 +0000354 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000355 /// ValueCacheEntryTy - This is all of the cached block information for
356 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
357 /// entries, allowing us to do a lookup with a binary search.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000358 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000359
Owen Anderson81881bc2010-07-30 20:56:07 +0000360 private:
Owen Anderson89778462011-01-05 21:15:29 +0000361 friend struct LVIValueHandle;
Owen Anderson87790ab2010-12-20 19:33:41 +0000362
363 /// OverDefinedCacheUpdater - A helper object that ensures that the
364 /// OverDefinedCache is updated whenever solveBlockValue returns.
365 struct OverDefinedCacheUpdater {
366 LazyValueInfoCache *Parent;
367 Value *Val;
368 BasicBlock *BB;
369 LVILatticeVal &BBLV;
370
371 OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV,
372 LazyValueInfoCache *P)
373 : Parent(P), Val(V), BB(B), BBLV(LV) { }
374
375 bool markResult(bool changed) {
376 if (changed && BBLV.isOverdefined())
377 Parent->OverDefinedCache.insert(std::make_pair(BB, Val));
378 return changed;
379 }
380 };
Owen Anderson7f9cb742010-07-30 23:59:40 +0000381
Chris Lattner2c5adf82009-11-15 19:59:49 +0000382 /// ValueCache - This is all of the cached information for all values,
383 /// mapped from Value* to key information.
Owen Anderson89778462011-01-05 21:15:29 +0000384 DenseMap<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000385
386 /// OverDefinedCache - This tracks, on a per-block basis, the set of
387 /// values that are over-defined at the end of that block. This is required
388 /// for cache updating.
Owen Anderson89778462011-01-05 21:15:29 +0000389 typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
390 DenseSet<OverDefinedPairTy> OverDefinedCache;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000391
Owen Andersonf33b3022010-12-09 06:14:58 +0000392 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000393 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
394 LVILatticeVal &Result);
395 bool hasBlockValue(Value *Val, BasicBlock *BB);
396
397 // These methods process one work item and may add more. A false value
398 // returned means that the work item was not completely processed and must
399 // be revisited after going through the new items.
400 bool solveBlockValue(Value *Val, BasicBlock *BB);
Owen Anderson61863942010-12-20 18:18:16 +0000401 bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
402 Value *Val, BasicBlock *BB);
403 bool solveBlockValuePHINode(LVILatticeVal &BBLV,
404 PHINode *PN, BasicBlock *BB);
405 bool solveBlockValueConstantRange(LVILatticeVal &BBLV,
406 Instruction *BBI, BasicBlock *BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000407
408 void solve();
Owen Andersonf33b3022010-12-09 06:14:58 +0000409
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000410 ValueCacheEntryTy &lookup(Value *V) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000411 return ValueCache[LVIValueHandle(V, this)];
412 }
413
Owen Anderson87790ab2010-12-20 19:33:41 +0000414 std::stack<std::pair<BasicBlock*, Value*> > block_value_stack;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000415
Chris Lattner2c5adf82009-11-15 19:59:49 +0000416 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000417 /// getValueInBlock - This is the query interface to determine the lattice
418 /// value for the specified Value* at the end of the specified block.
419 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
420
421 /// getValueOnEdge - This is the query interface to determine the lattice
422 /// value for the specified Value* that is true on the specified edge.
423 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000424
425 /// threadEdge - This is the update interface to inform the cache that an
426 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
427 /// NewSucc.
428 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000429
430 /// eraseBlock - This is part of the update interface to inform the cache
431 /// that a block has been deleted.
432 void eraseBlock(BasicBlock *BB);
433
434 /// clear - Empty the cache.
435 void clear() {
436 ValueCache.clear();
437 OverDefinedCache.clear();
438 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000439 };
440} // end anonymous namespace
441
Owen Anderson89778462011-01-05 21:15:29 +0000442void LVIValueHandle::deleted() {
443 typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
444
445 SmallVector<OverDefinedPairTy, 4> ToErase;
446 for (DenseSet<OverDefinedPairTy>::iterator
Owen Anderson7f9cb742010-07-30 23:59:40 +0000447 I = Parent->OverDefinedCache.begin(),
448 E = Parent->OverDefinedCache.end();
Owen Anderson89778462011-01-05 21:15:29 +0000449 I != E; ++I) {
450 if (I->second == getValPtr())
451 ToErase.push_back(*I);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000452 }
Owen Andersoncf6abd22010-08-11 22:36:04 +0000453
Owen Anderson89778462011-01-05 21:15:29 +0000454 for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(),
455 E = ToErase.end(); I != E; ++I)
456 Parent->OverDefinedCache.erase(*I);
457
Owen Andersoncf6abd22010-08-11 22:36:04 +0000458 // This erasure deallocates *this, so it MUST happen after we're done
459 // using any and all members of *this.
460 Parent->ValueCache.erase(*this);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000461}
462
Owen Anderson00ac77e2010-08-18 18:39:01 +0000463void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
Owen Anderson89778462011-01-05 21:15:29 +0000464 SmallVector<OverDefinedPairTy, 4> ToErase;
465 for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
466 E = OverDefinedCache.end(); I != E; ++I) {
467 if (I->first == BB)
468 ToErase.push_back(*I);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000469 }
Owen Anderson89778462011-01-05 21:15:29 +0000470
471 for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(),
472 E = ToErase.end(); I != E; ++I)
473 OverDefinedCache.erase(*I);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000474
Owen Anderson89778462011-01-05 21:15:29 +0000475 for (DenseMap<LVIValueHandle, ValueCacheEntryTy>::iterator
Owen Anderson00ac77e2010-08-18 18:39:01 +0000476 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
477 I->second.erase(BB);
478}
Owen Anderson7f9cb742010-07-30 23:59:40 +0000479
Nick Lewycky90862ee2010-12-18 01:00:40 +0000480void LazyValueInfoCache::solve() {
481 while (!block_value_stack.empty()) {
Owen Anderson87790ab2010-12-20 19:33:41 +0000482 std::pair<BasicBlock*, Value*> &e = block_value_stack.top();
483 if (solveBlockValue(e.second, e.first))
Nick Lewycky90862ee2010-12-18 01:00:40 +0000484 block_value_stack.pop();
485 }
486}
487
488bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
489 // If already a constant, there is nothing to compute.
490 if (isa<Constant>(Val))
491 return true;
492
Owen Anderson89778462011-01-05 21:15:29 +0000493 LVIValueHandle ValHandle(Val, this);
494 if (!ValueCache.count(ValHandle)) return false;
495 return ValueCache[ValHandle].count(BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000496}
497
Owen Andersonf33b3022010-12-09 06:14:58 +0000498LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000499 // If already a constant, there is nothing to compute.
500 if (Constant *VC = dyn_cast<Constant>(Val))
501 return LVILatticeVal::get(VC);
502
503 return lookup(Val)[BB];
504}
505
506bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
507 if (isa<Constant>(Val))
508 return true;
509
Owen Andersonf33b3022010-12-09 06:14:58 +0000510 ValueCacheEntryTy &Cache = lookup(Val);
511 LVILatticeVal &BBLV = Cache[BB];
Owen Anderson87790ab2010-12-20 19:33:41 +0000512
513 // OverDefinedCacheUpdater is a helper object that will update
514 // the OverDefinedCache for us when this method exits. Make sure to
515 // call markResult on it as we exist, passing a bool to indicate if the
516 // cache needs updating, i.e. if we have solve a new value or not.
517 OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000518
Chris Lattner2c5adf82009-11-15 19:59:49 +0000519 // If we've already computed this block's value, return it.
Chris Lattnere5642812009-11-15 20:00:52 +0000520 if (!BBLV.isUndefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000521 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
Owen Anderson87790ab2010-12-20 19:33:41 +0000522
523 // Since we're reusing a cached value here, we don't need to update the
524 // OverDefinedCahce. The cache will have been properly updated
525 // whenever the cached value was inserted.
526 ODCacheUpdater.markResult(false);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000527 return true;
Chris Lattnere5642812009-11-15 20:00:52 +0000528 }
529
Chris Lattner2c5adf82009-11-15 19:59:49 +0000530 // Otherwise, this is the first time we're seeing this block. Reset the
531 // lattice value to overdefined, so that cycles will terminate and be
532 // conservatively correct.
533 BBLV.markOverdefined();
534
Chris Lattner2c5adf82009-11-15 19:59:49 +0000535 Instruction *BBI = dyn_cast<Instruction>(Val);
536 if (BBI == 0 || BBI->getParent() != BB) {
Owen Anderson87790ab2010-12-20 19:33:41 +0000537 return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB));
Chris Lattner2c5adf82009-11-15 19:59:49 +0000538 }
Chris Lattnere5642812009-11-15 20:00:52 +0000539
Nick Lewycky90862ee2010-12-18 01:00:40 +0000540 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Owen Anderson87790ab2010-12-20 19:33:41 +0000541 return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000542 }
Owen Andersonb81fd622010-08-18 21:11:37 +0000543
544 // We can only analyze the definitions of certain classes of instructions
545 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000546 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000547 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
548 !BBI->getType()->isIntegerTy()) {
549 DEBUG(dbgs() << " compute BB '" << BB->getName()
550 << "' - overdefined because inst def found.\n");
Owen Anderson61863942010-12-20 18:18:16 +0000551 BBLV.markOverdefined();
Owen Anderson87790ab2010-12-20 19:33:41 +0000552 return ODCacheUpdater.markResult(true);
Owen Andersonb81fd622010-08-18 21:11:37 +0000553 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000554
Owen Andersonb81fd622010-08-18 21:11:37 +0000555 // FIXME: We're currently limited to binops with a constant RHS. This should
556 // be improved.
557 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
558 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
559 DEBUG(dbgs() << " compute BB '" << BB->getName()
560 << "' - overdefined because inst def found.\n");
561
Owen Anderson61863942010-12-20 18:18:16 +0000562 BBLV.markOverdefined();
Owen Anderson87790ab2010-12-20 19:33:41 +0000563 return ODCacheUpdater.markResult(true);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000564 }
Owen Andersonb81fd622010-08-18 21:11:37 +0000565
Owen Anderson87790ab2010-12-20 19:33:41 +0000566 return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000567}
568
569static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
570 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
571 return L->getPointerAddressSpace() == 0 &&
572 GetUnderlyingObject(L->getPointerOperand()) ==
573 GetUnderlyingObject(Ptr);
574 }
575 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
576 return S->getPointerAddressSpace() == 0 &&
577 GetUnderlyingObject(S->getPointerOperand()) ==
578 GetUnderlyingObject(Ptr);
579 }
580 // FIXME: llvm.memset, etc.
581 return false;
582}
583
Owen Anderson61863942010-12-20 18:18:16 +0000584bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
585 Value *Val, BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000586 LVILatticeVal Result; // Start Undefined.
587
588 // If this is a pointer, and there's a load from that pointer in this BB,
589 // then we know that the pointer can't be NULL.
590 bool NotNull = false;
591 if (Val->getType()->isPointerTy()) {
592 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){
593 if (InstructionDereferencesPointer(BI, Val)) {
594 NotNull = true;
595 break;
596 }
597 }
598 }
599
600 // If this is the entry block, we must be asking about an argument. The
601 // value is overdefined.
602 if (BB == &BB->getParent()->getEntryBlock()) {
603 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
604 if (NotNull) {
605 const PointerType *PTy = cast<PointerType>(Val->getType());
606 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
607 } else {
608 Result.markOverdefined();
609 }
Owen Anderson61863942010-12-20 18:18:16 +0000610 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000611 return true;
612 }
613
614 // Loop over all of our predecessors, merging what we know from them into
615 // result.
616 bool EdgesMissing = false;
617 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
618 LVILatticeVal EdgeResult;
619 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
620 if (EdgesMissing)
621 continue;
622
623 Result.mergeIn(EdgeResult);
624
625 // If we hit overdefined, exit early. The BlockVals entry is already set
626 // to overdefined.
627 if (Result.isOverdefined()) {
628 DEBUG(dbgs() << " compute BB '" << BB->getName()
629 << "' - overdefined because of pred.\n");
630 // If we previously determined that this is a pointer that can't be null
631 // then return that rather than giving up entirely.
632 if (NotNull) {
633 const PointerType *PTy = cast<PointerType>(Val->getType());
634 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
635 }
Owen Anderson61863942010-12-20 18:18:16 +0000636
637 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000638 return true;
639 }
640 }
641 if (EdgesMissing)
642 return false;
643
644 // Return the merged value, which is more precise than 'overdefined'.
645 assert(!Result.isOverdefined());
Owen Anderson61863942010-12-20 18:18:16 +0000646 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000647 return true;
648}
649
Owen Anderson61863942010-12-20 18:18:16 +0000650bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
651 PHINode *PN, BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000652 LVILatticeVal Result; // Start Undefined.
653
654 // Loop over all of our predecessors, merging what we know from them into
655 // result.
656 bool EdgesMissing = false;
657 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
658 BasicBlock *PhiBB = PN->getIncomingBlock(i);
659 Value *PhiVal = PN->getIncomingValue(i);
660 LVILatticeVal EdgeResult;
661 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult);
662 if (EdgesMissing)
663 continue;
664
665 Result.mergeIn(EdgeResult);
666
667 // If we hit overdefined, exit early. The BlockVals entry is already set
668 // to overdefined.
669 if (Result.isOverdefined()) {
670 DEBUG(dbgs() << " compute BB '" << BB->getName()
671 << "' - overdefined because of pred.\n");
Owen Anderson61863942010-12-20 18:18:16 +0000672
673 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000674 return true;
675 }
676 }
677 if (EdgesMissing)
678 return false;
679
680 // Return the merged value, which is more precise than 'overdefined'.
681 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson61863942010-12-20 18:18:16 +0000682 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000683 return true;
684}
685
Owen Anderson61863942010-12-20 18:18:16 +0000686bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
687 Instruction *BBI,
Nick Lewycky90862ee2010-12-18 01:00:40 +0000688 BasicBlock *BB) {
Owen Andersonb81fd622010-08-18 21:11:37 +0000689 // Figure out the range of the LHS. If that fails, bail.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000690 if (!hasBlockValue(BBI->getOperand(0), BB)) {
Owen Anderson87790ab2010-12-20 19:33:41 +0000691 block_value_stack.push(std::make_pair(BB, BBI->getOperand(0)));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000692 return false;
693 }
694
Nick Lewycky90862ee2010-12-18 01:00:40 +0000695 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Owen Andersonb81fd622010-08-18 21:11:37 +0000696 if (!LHSVal.isConstantRange()) {
Owen Anderson61863942010-12-20 18:18:16 +0000697 BBLV.markOverdefined();
Nick Lewycky90862ee2010-12-18 01:00:40 +0000698 return true;
Owen Andersonb81fd622010-08-18 21:11:37 +0000699 }
700
Owen Andersonb81fd622010-08-18 21:11:37 +0000701 ConstantRange LHSRange = LHSVal.getConstantRange();
702 ConstantRange RHSRange(1);
703 const IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
704 if (isa<BinaryOperator>(BBI)) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000705 if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) {
706 RHSRange = ConstantRange(RHS->getValue());
707 } else {
Owen Anderson61863942010-12-20 18:18:16 +0000708 BBLV.markOverdefined();
Nick Lewycky90862ee2010-12-18 01:00:40 +0000709 return true;
Owen Anderson59b06dc2010-08-24 07:55:44 +0000710 }
Owen Andersonb81fd622010-08-18 21:11:37 +0000711 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000712
Owen Andersonb81fd622010-08-18 21:11:37 +0000713 // NOTE: We're currently limited by the set of operations that ConstantRange
714 // can evaluate symbolically. Enhancing that set will allows us to analyze
715 // more definitions.
Owen Anderson61863942010-12-20 18:18:16 +0000716 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000717 switch (BBI->getOpcode()) {
718 case Instruction::Add:
719 Result.markConstantRange(LHSRange.add(RHSRange));
720 break;
721 case Instruction::Sub:
722 Result.markConstantRange(LHSRange.sub(RHSRange));
723 break;
724 case Instruction::Mul:
725 Result.markConstantRange(LHSRange.multiply(RHSRange));
726 break;
727 case Instruction::UDiv:
728 Result.markConstantRange(LHSRange.udiv(RHSRange));
729 break;
730 case Instruction::Shl:
731 Result.markConstantRange(LHSRange.shl(RHSRange));
732 break;
733 case Instruction::LShr:
734 Result.markConstantRange(LHSRange.lshr(RHSRange));
735 break;
736 case Instruction::Trunc:
737 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
738 break;
739 case Instruction::SExt:
740 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
741 break;
742 case Instruction::ZExt:
743 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
744 break;
745 case Instruction::BitCast:
746 Result.markConstantRange(LHSRange);
747 break;
Nick Lewycky198381e2010-09-07 05:39:02 +0000748 case Instruction::And:
749 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
750 break;
751 case Instruction::Or:
752 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
753 break;
Owen Andersonb81fd622010-08-18 21:11:37 +0000754
755 // Unhandled instructions are overdefined.
756 default:
757 DEBUG(dbgs() << " compute BB '" << BB->getName()
758 << "' - overdefined because inst def found.\n");
759 Result.markOverdefined();
760 break;
761 }
762
Owen Anderson61863942010-12-20 18:18:16 +0000763 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000764 return true;
Chris Lattner10f2d132009-11-11 00:22:30 +0000765}
766
Chris Lattner800c47e2009-11-15 20:02:12 +0000767/// getEdgeValue - This method attempts to infer more complex
Nick Lewycky90862ee2010-12-18 01:00:40 +0000768bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
769 BasicBlock *BBTo, LVILatticeVal &Result) {
770 // If already a constant, there is nothing to compute.
771 if (Constant *VC = dyn_cast<Constant>(Val)) {
772 Result = LVILatticeVal::get(VC);
773 return true;
774 }
775
Chris Lattner800c47e2009-11-15 20:02:12 +0000776 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
777 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +0000778 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
779 // If this is a conditional branch and only one successor goes to BBTo, then
780 // we maybe able to infer something from the condition.
781 if (BI->isConditional() &&
782 BI->getSuccessor(0) != BI->getSuccessor(1)) {
783 bool isTrueDest = BI->getSuccessor(0) == BBTo;
784 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
785 "BBTo isn't a successor of BBFrom");
786
787 // If V is the condition of the branch itself, then we know exactly what
788 // it is.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000789 if (BI->getCondition() == Val) {
790 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +0000791 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000792 return true;
793 }
Chris Lattner16976522009-11-11 22:48:44 +0000794
795 // If the condition of the branch is an equality comparison, we may be
796 // able to infer the value.
Owen Anderson2d0f2472010-08-11 04:24:25 +0000797 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
798 if (ICI && ICI->getOperand(0) == Val &&
799 isa<Constant>(ICI->getOperand(1))) {
800 if (ICI->isEquality()) {
801 // We know that V has the RHS constant if this is a true SETEQ or
802 // false SETNE.
803 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
Nick Lewycky90862ee2010-12-18 01:00:40 +0000804 Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
805 else
806 Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
807 return true;
Chris Lattner16976522009-11-11 22:48:44 +0000808 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000809
Owen Anderson2d0f2472010-08-11 04:24:25 +0000810 if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
811 // Calculate the range of values that would satisfy the comparison.
812 ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
813 ConstantRange TrueValues =
814 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000815
Owen Anderson2d0f2472010-08-11 04:24:25 +0000816 // If we're interested in the false dest, invert the condition.
817 if (!isTrueDest) TrueValues = TrueValues.inverse();
818
819 // Figure out the possible values of the query BEFORE this branch.
Owen Andersonbe419012011-01-05 21:37:18 +0000820 if (!hasBlockValue(Val, BBFrom)) {
821 block_value_stack.push(std::make_pair(BBFrom, Val));
822 return false;
823 }
824
Owen Andersonf33b3022010-12-09 06:14:58 +0000825 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000826 if (!InBlock.isConstantRange()) {
827 Result = LVILatticeVal::getRange(TrueValues);
828 return true;
829 }
830
Owen Anderson2d0f2472010-08-11 04:24:25 +0000831 // Find all potential values that satisfy both the input and output
832 // conditions.
833 ConstantRange PossibleValues =
834 TrueValues.intersectWith(InBlock.getConstantRange());
Nick Lewycky90862ee2010-12-18 01:00:40 +0000835
836 Result = LVILatticeVal::getRange(PossibleValues);
837 return true;
Owen Anderson2d0f2472010-08-11 04:24:25 +0000838 }
839 }
Chris Lattner16976522009-11-11 22:48:44 +0000840 }
841 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000842
843 // If the edge was formed by a switch on the value, then we may know exactly
844 // what it is.
845 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Owen Andersondae90c62010-08-24 21:59:42 +0000846 if (SI->getCondition() == Val) {
Owen Anderson4caef602010-09-02 22:16:52 +0000847 // We don't know anything in the default case.
Owen Andersondae90c62010-08-24 21:59:42 +0000848 if (SI->getDefaultDest() == BBTo) {
Owen Anderson4caef602010-09-02 22:16:52 +0000849 Result.markOverdefined();
Nick Lewycky90862ee2010-12-18 01:00:40 +0000850 return true;
Owen Andersondae90c62010-08-24 21:59:42 +0000851 }
852
Chris Lattner800c47e2009-11-15 20:02:12 +0000853 // We only know something if there is exactly one value that goes from
854 // BBFrom to BBTo.
855 unsigned NumEdges = 0;
856 ConstantInt *EdgeVal = 0;
857 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
858 if (SI->getSuccessor(i) != BBTo) continue;
859 if (NumEdges++) break;
860 EdgeVal = SI->getCaseValue(i);
861 }
862 assert(EdgeVal && "Missing successor?");
Nick Lewycky90862ee2010-12-18 01:00:40 +0000863 if (NumEdges == 1) {
864 Result = LVILatticeVal::get(EdgeVal);
865 return true;
866 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000867 }
868 }
Chris Lattner16976522009-11-11 22:48:44 +0000869
870 // Otherwise see if the value is known in the block.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000871 if (hasBlockValue(Val, BBFrom)) {
872 Result = getBlockValue(Val, BBFrom);
873 return true;
874 }
Owen Anderson87790ab2010-12-20 19:33:41 +0000875 block_value_stack.push(std::make_pair(BBFrom, Val));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000876 return false;
Chris Lattner16976522009-11-11 22:48:44 +0000877}
878
Chris Lattner2c5adf82009-11-15 19:59:49 +0000879LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
David Greene5d93a1f2009-12-23 20:43:58 +0000880 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000881 << BB->getName() << "'\n");
882
Owen Anderson87790ab2010-12-20 19:33:41 +0000883 block_value_stack.push(std::make_pair(BB, V));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000884 solve();
Owen Andersonf33b3022010-12-09 06:14:58 +0000885 LVILatticeVal Result = getBlockValue(V, BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000886
David Greene5d93a1f2009-12-23 20:43:58 +0000887 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000888 return Result;
889}
Chris Lattner16976522009-11-11 22:48:44 +0000890
Chris Lattner2c5adf82009-11-15 19:59:49 +0000891LVILatticeVal LazyValueInfoCache::
892getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
David Greene5d93a1f2009-12-23 20:43:58 +0000893 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000894 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000895
Nick Lewycky90862ee2010-12-18 01:00:40 +0000896 LVILatticeVal Result;
897 if (!getEdgeValue(V, FromBB, ToBB, Result)) {
898 solve();
899 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result);
900 (void)WasFastQuery;
901 assert(WasFastQuery && "More work to do after problem solved?");
902 }
903
David Greene5d93a1f2009-12-23 20:43:58 +0000904 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000905 return Result;
906}
907
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000908void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
909 BasicBlock *NewSucc) {
910 // When an edge in the graph has been threaded, values that we could not
911 // determine a value for before (i.e. were marked overdefined) may be possible
912 // to solve now. We do NOT try to proactively update these values. Instead,
913 // we clear their entries from the cache, and allow lazy updating to recompute
914 // them when needed.
915
916 // The updating process is fairly simple: we need to dropped cached info
917 // for all values that were marked overdefined in OldSucc, and for those same
918 // values in any successor of OldSucc (except NewSucc) in which they were
919 // also marked overdefined.
920 std::vector<BasicBlock*> worklist;
921 worklist.push_back(OldSucc);
922
Owen Anderson9a65dc92010-07-27 23:58:11 +0000923 DenseSet<Value*> ClearSet;
Owen Anderson89778462011-01-05 21:15:29 +0000924 for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
925 E = OverDefinedCache.end(); I != E; ++I) {
Owen Anderson9a65dc92010-07-27 23:58:11 +0000926 if (I->first == OldSucc)
927 ClearSet.insert(I->second);
928 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000929
930 // Use a worklist to perform a depth-first search of OldSucc's successors.
931 // NOTE: We do not need a visited list since any blocks we have already
932 // visited will have had their overdefined markers cleared already, and we
933 // thus won't loop to their successors.
934 while (!worklist.empty()) {
935 BasicBlock *ToUpdate = worklist.back();
936 worklist.pop_back();
937
938 // Skip blocks only accessible through NewSucc.
939 if (ToUpdate == NewSucc) continue;
940
941 bool changed = false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000942 for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000943 I != E; ++I) {
944 // If a value was marked overdefined in OldSucc, and is here too...
Owen Anderson89778462011-01-05 21:15:29 +0000945 DenseSet<OverDefinedPairTy>::iterator OI =
Owen Anderson9a65dc92010-07-27 23:58:11 +0000946 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
947 if (OI == OverDefinedCache.end()) continue;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000948
Owen Anderson9a65dc92010-07-27 23:58:11 +0000949 // Remove it from the caches.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000950 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
Owen Anderson9a65dc92010-07-27 23:58:11 +0000951 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000952
Owen Anderson9a65dc92010-07-27 23:58:11 +0000953 assert(CI != Entry.end() && "Couldn't find entry to update?");
954 Entry.erase(CI);
955 OverDefinedCache.erase(OI);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000956
Owen Anderson9a65dc92010-07-27 23:58:11 +0000957 // If we removed anything, then we potentially need to update
958 // blocks successors too.
959 changed = true;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000960 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000961
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000962 if (!changed) continue;
963
964 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
965 }
966}
967
Chris Lattner2c5adf82009-11-15 19:59:49 +0000968//===----------------------------------------------------------------------===//
969// LazyValueInfo Impl
970//===----------------------------------------------------------------------===//
971
Chris Lattner2c5adf82009-11-15 19:59:49 +0000972/// getCache - This lazily constructs the LazyValueInfoCache.
973static LazyValueInfoCache &getCache(void *&PImpl) {
974 if (!PImpl)
975 PImpl = new LazyValueInfoCache();
976 return *static_cast<LazyValueInfoCache*>(PImpl);
977}
978
Owen Anderson00ac77e2010-08-18 18:39:01 +0000979bool LazyValueInfo::runOnFunction(Function &F) {
980 if (PImpl)
981 getCache(PImpl).clear();
982
983 TD = getAnalysisIfAvailable<TargetData>();
984 // Fully lazy.
985 return false;
986}
987
Chris Lattner2c5adf82009-11-15 19:59:49 +0000988void LazyValueInfo::releaseMemory() {
989 // If the cache was allocated, free it.
990 if (PImpl) {
991 delete &getCache(PImpl);
992 PImpl = 0;
993 }
994}
995
996Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
997 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
998
Chris Lattner16976522009-11-11 22:48:44 +0000999 if (Result.isConstant())
1000 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001001 if (Result.isConstantRange()) {
Owen Andersonee61fcf2010-08-27 23:29:38 +00001002 ConstantRange CR = Result.getConstantRange();
1003 if (const APInt *SingleVal = CR.getSingleElement())
1004 return ConstantInt::get(V->getContext(), *SingleVal);
1005 }
Chris Lattner16976522009-11-11 22:48:44 +00001006 return 0;
1007}
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001008
Chris Lattner38392bb2009-11-12 01:29:10 +00001009/// getConstantOnEdge - Determine whether the specified value is known to be a
1010/// constant on the specified edge. Return null if not.
1011Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
1012 BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +00001013 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattner38392bb2009-11-12 01:29:10 +00001014
1015 if (Result.isConstant())
1016 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001017 if (Result.isConstantRange()) {
Owen Anderson9f014062010-08-10 20:03:09 +00001018 ConstantRange CR = Result.getConstantRange();
1019 if (const APInt *SingleVal = CR.getSingleElement())
1020 return ConstantInt::get(V->getContext(), *SingleVal);
1021 }
Chris Lattner38392bb2009-11-12 01:29:10 +00001022 return 0;
1023}
1024
Chris Lattnerb52675b2009-11-12 04:36:58 +00001025/// getPredicateOnEdge - Determine whether the specified value comparison
1026/// with a constant is known to be true or false on the specified CFG edge.
1027/// Pred is a CmpInst predicate.
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001028LazyValueInfo::Tristate
Chris Lattnerb52675b2009-11-12 04:36:58 +00001029LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1030 BasicBlock *FromBB, BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +00001031 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001032
Chris Lattnerb52675b2009-11-12 04:36:58 +00001033 // If we know the value is a constant, evaluate the conditional.
1034 Constant *Res = 0;
1035 if (Result.isConstant()) {
1036 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD);
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001037 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Chris Lattnerb52675b2009-11-12 04:36:58 +00001038 return ResCI->isZero() ? False : True;
Chris Lattner2c5adf82009-11-15 19:59:49 +00001039 return Unknown;
1040 }
1041
Owen Anderson9f014062010-08-10 20:03:09 +00001042 if (Result.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +00001043 ConstantInt *CI = dyn_cast<ConstantInt>(C);
1044 if (!CI) return Unknown;
1045
Owen Anderson9f014062010-08-10 20:03:09 +00001046 ConstantRange CR = Result.getConstantRange();
1047 if (Pred == ICmpInst::ICMP_EQ) {
1048 if (!CR.contains(CI->getValue()))
1049 return False;
1050
1051 if (CR.isSingleElement() && CR.contains(CI->getValue()))
1052 return True;
1053 } else if (Pred == ICmpInst::ICMP_NE) {
1054 if (!CR.contains(CI->getValue()))
1055 return True;
1056
1057 if (CR.isSingleElement() && CR.contains(CI->getValue()))
1058 return False;
1059 }
1060
1061 // Handle more complex predicates.
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001062 ConstantRange TrueValues =
1063 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1064 if (TrueValues.contains(CR))
Owen Anderson9f014062010-08-10 20:03:09 +00001065 return True;
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001066 if (TrueValues.inverse().contains(CR))
1067 return False;
Owen Anderson9f014062010-08-10 20:03:09 +00001068 return Unknown;
1069 }
1070
Chris Lattner2c5adf82009-11-15 19:59:49 +00001071 if (Result.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +00001072 // If this is an equality comparison, we can try to fold it knowing that
1073 // "V != C1".
1074 if (Pred == ICmpInst::ICMP_EQ) {
1075 // !C1 == C -> false iff C1 == C.
1076 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1077 Result.getNotConstant(), C, TD);
1078 if (Res->isNullValue())
1079 return False;
1080 } else if (Pred == ICmpInst::ICMP_NE) {
1081 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +00001082 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Chris Lattnerb52675b2009-11-12 04:36:58 +00001083 Result.getNotConstant(), C, TD);
1084 if (Res->isNullValue())
1085 return True;
1086 }
Chris Lattner2c5adf82009-11-15 19:59:49 +00001087 return Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +00001088 }
1089
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001090 return Unknown;
1091}
1092
Owen Andersoncfa7fb62010-07-26 18:48:03 +00001093void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001094 BasicBlock *NewSucc) {
Owen Anderson00ac77e2010-08-18 18:39:01 +00001095 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
1096}
1097
1098void LazyValueInfo::eraseBlock(BasicBlock *BB) {
1099 if (PImpl) getCache(PImpl).eraseBlock(BB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +00001100}