blob: eab4ca06f07a676525586164d6c62eb0882ca5e2 [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>
Chris Lattner10f2d132009-11-11 00:22:30 +000032using namespace llvm;
33
34char LazyValueInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000035INITIALIZE_PASS(LazyValueInfo, "lazy-value-info",
Owen Andersonce665bd2010-10-07 22:25:06 +000036 "Lazy Value Information Analysis", false, true)
Chris Lattner10f2d132009-11-11 00:22:30 +000037
38namespace llvm {
39 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
40}
41
Chris Lattnercc4d3b22009-11-11 02:08:33 +000042
43//===----------------------------------------------------------------------===//
44// LVILatticeVal
45//===----------------------------------------------------------------------===//
46
47/// LVILatticeVal - This is the information tracked by LazyValueInfo for each
48/// value.
49///
50/// FIXME: This is basically just for bringup, this can be made a lot more rich
51/// in the future.
52///
53namespace {
54class LVILatticeVal {
55 enum LatticeValueTy {
Nick Lewycky69bfdf52010-12-15 18:57:18 +000056 /// undefined - This Value has no known value yet.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000057 undefined,
Owen Anderson5be2e782010-08-05 22:59:19 +000058
Nick Lewycky69bfdf52010-12-15 18:57:18 +000059 /// constant - This Value has a specific constant value.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000060 constant,
Nick Lewycky69bfdf52010-12-15 18:57:18 +000061 /// notconstant - This Value is known to not have the specified value.
Chris Lattnerb52675b2009-11-12 04:36:58 +000062 notconstant,
63
Nick Lewycky69bfdf52010-12-15 18:57:18 +000064 /// constantrange - The Value falls within this range.
Owen Anderson5be2e782010-08-05 22:59:19 +000065 constantrange,
66
Nick Lewycky69bfdf52010-12-15 18:57:18 +000067 /// overdefined - This value is not known to be constant, and we know that
Chris Lattnercc4d3b22009-11-11 02:08:33 +000068 /// it has a value.
69 overdefined
70 };
71
72 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattnerb52675b2009-11-12 04:36:58 +000073 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersondb78d732010-08-05 22:10:46 +000074 LatticeValueTy Tag;
75 Constant *Val;
Owen Anderson5be2e782010-08-05 22:59:19 +000076 ConstantRange Range;
Chris Lattnercc4d3b22009-11-11 02:08:33 +000077
78public:
Owen Anderson5be2e782010-08-05 22:59:19 +000079 LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {}
Chris Lattnercc4d3b22009-11-11 02:08:33 +000080
Chris Lattner16976522009-11-11 22:48:44 +000081 static LVILatticeVal get(Constant *C) {
82 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000083 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000084 Res.markConstant(C);
Chris Lattner16976522009-11-11 22:48:44 +000085 return Res;
86 }
Chris Lattnerb52675b2009-11-12 04:36:58 +000087 static LVILatticeVal getNot(Constant *C) {
88 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000089 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000090 Res.markNotConstant(C);
Chris Lattnerb52675b2009-11-12 04:36:58 +000091 return Res;
92 }
Owen Anderson625051b2010-08-10 23:20:01 +000093 static LVILatticeVal getRange(ConstantRange CR) {
94 LVILatticeVal Res;
95 Res.markConstantRange(CR);
96 return Res;
97 }
Chris Lattner16976522009-11-11 22:48:44 +000098
Owen Anderson5be2e782010-08-05 22:59:19 +000099 bool isUndefined() const { return Tag == undefined; }
100 bool isConstant() const { return Tag == constant; }
101 bool isNotConstant() const { return Tag == notconstant; }
102 bool isConstantRange() const { return Tag == constantrange; }
103 bool isOverdefined() const { return Tag == overdefined; }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000104
105 Constant *getConstant() const {
106 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000107 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000108 }
109
Chris Lattnerb52675b2009-11-12 04:36:58 +0000110 Constant *getNotConstant() const {
111 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000112 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000113 }
114
Owen Anderson5be2e782010-08-05 22:59:19 +0000115 ConstantRange getConstantRange() const {
116 assert(isConstantRange() &&
117 "Cannot get the constant-range of a non-constant-range!");
118 return Range;
119 }
120
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000121 /// markOverdefined - Return true if this is a change in status.
122 bool markOverdefined() {
123 if (isOverdefined())
124 return false;
Owen Andersondb78d732010-08-05 22:10:46 +0000125 Tag = overdefined;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000126 return true;
127 }
128
129 /// markConstant - Return true if this is a change in status.
130 bool markConstant(Constant *V) {
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000131 assert(V && "Marking constant with NULL");
132 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
133 return markConstantRange(ConstantRange(CI->getValue()));
134 if (isa<UndefValue>(V))
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000135 return false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000136
137 assert((!isConstant() || getConstant() == V) &&
138 "Marking constant with different value");
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000139 assert(isUndefined());
Owen Andersondb78d732010-08-05 22:10:46 +0000140 Tag = constant;
Owen Andersondb78d732010-08-05 22:10:46 +0000141 Val = V;
Chris Lattner16976522009-11-11 22:48:44 +0000142 return true;
143 }
144
Chris Lattnerb52675b2009-11-12 04:36:58 +0000145 /// markNotConstant - Return true if this is a change in status.
146 bool markNotConstant(Constant *V) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000147 assert(V && "Marking constant with NULL");
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000148 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
149 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
150 if (isa<UndefValue>(V))
151 return false;
152
153 assert((!isConstant() || getConstant() != V) &&
154 "Marking constant !constant with same value");
155 assert((!isNotConstant() || getNotConstant() == V) &&
156 "Marking !constant with different value");
157 assert(isUndefined() || isConstant());
158 Tag = notconstant;
Owen Andersondb78d732010-08-05 22:10:46 +0000159 Val = V;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000160 return true;
161 }
162
Owen Anderson5be2e782010-08-05 22:59:19 +0000163 /// markConstantRange - Return true if this is a change in status.
164 bool markConstantRange(const ConstantRange NewR) {
165 if (isConstantRange()) {
166 if (NewR.isEmptySet())
167 return markOverdefined();
168
Owen Anderson5be2e782010-08-05 22:59:19 +0000169 bool changed = Range == NewR;
170 Range = NewR;
171 return changed;
172 }
173
174 assert(isUndefined());
175 if (NewR.isEmptySet())
176 return markOverdefined();
Owen Anderson5be2e782010-08-05 22:59:19 +0000177
178 Tag = constantrange;
179 Range = NewR;
180 return true;
181 }
182
Chris Lattner16976522009-11-11 22:48:44 +0000183 /// mergeIn - Merge the specified lattice value into this one, updating this
184 /// one and returning true if anything changed.
185 bool mergeIn(const LVILatticeVal &RHS) {
186 if (RHS.isUndefined() || isOverdefined()) return false;
187 if (RHS.isOverdefined()) return markOverdefined();
188
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000189 if (isUndefined()) {
190 Tag = RHS.Tag;
191 Val = RHS.Val;
192 Range = RHS.Range;
193 return true;
Chris Lattnerf496e792009-11-12 04:57:13 +0000194 }
195
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000196 if (isConstant()) {
197 if (RHS.isConstant()) {
198 if (Val == RHS.Val)
199 return false;
200 return markOverdefined();
201 }
202
203 if (RHS.isNotConstant()) {
204 if (Val == RHS.Val)
205 return markOverdefined();
206
207 // Unless we can prove that the two Constants are different, we must
208 // move to overdefined.
209 // FIXME: use TargetData for smarter constant folding.
210 if (ConstantInt *Res = dyn_cast<ConstantInt>(
211 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
212 getConstant(),
213 RHS.getNotConstant())))
214 if (Res->isOne())
215 return markNotConstant(RHS.getNotConstant());
216
217 return markOverdefined();
218 }
219
220 // RHS is a ConstantRange, LHS is a non-integer Constant.
221
222 // FIXME: consider the case where RHS is a range [1, 0) and LHS is
223 // a function. The correct result is to pick up RHS.
224
Chris Lattner16976522009-11-11 22:48:44 +0000225 return markOverdefined();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000226 }
227
228 if (isNotConstant()) {
229 if (RHS.isConstant()) {
230 if (Val == RHS.Val)
231 return markOverdefined();
232
233 // Unless we can prove that the two Constants are different, we must
234 // move to overdefined.
235 // FIXME: use TargetData for smarter constant folding.
236 if (ConstantInt *Res = dyn_cast<ConstantInt>(
237 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
238 getNotConstant(),
239 RHS.getConstant())))
240 if (Res->isOne())
241 return false;
242
243 return markOverdefined();
244 }
245
246 if (RHS.isNotConstant()) {
247 if (Val == RHS.Val)
248 return false;
249 return markOverdefined();
250 }
251
252 return markOverdefined();
253 }
254
255 assert(isConstantRange() && "New LVILattice type?");
256 if (!RHS.isConstantRange())
257 return markOverdefined();
258
259 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
260 if (NewR.isFullSet())
261 return markOverdefined();
262 return markConstantRange(NewR);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000263 }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000264};
265
266} // end anonymous namespace.
267
Chris Lattner16976522009-11-11 22:48:44 +0000268namespace llvm {
269raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
270 if (Val.isUndefined())
271 return OS << "undefined";
272 if (Val.isOverdefined())
273 return OS << "overdefined";
Chris Lattnerb52675b2009-11-12 04:36:58 +0000274
275 if (Val.isNotConstant())
276 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson2f3ffb82010-08-09 20:50:46 +0000277 else if (Val.isConstantRange())
278 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
279 << Val.getConstantRange().getUpper() << '>';
Chris Lattner16976522009-11-11 22:48:44 +0000280 return OS << "constant<" << *Val.getConstant() << '>';
281}
282}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000283
284//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000285// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000286//===----------------------------------------------------------------------===//
287
Chris Lattner2c5adf82009-11-15 19:59:49 +0000288namespace {
289 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
290 /// maintains information about queries across the clients' queries.
291 class LazyValueInfoCache {
Owen Anderson81881bc2010-07-30 20:56:07 +0000292 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000293 /// ValueCacheEntryTy - This is all of the cached block information for
294 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
295 /// entries, allowing us to do a lookup with a binary search.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000296 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000297
Owen Anderson81881bc2010-07-30 20:56:07 +0000298 private:
Owen Anderson7f9cb742010-07-30 23:59:40 +0000299 /// LVIValueHandle - A callback value handle update the cache when
300 /// values are erased.
301 struct LVIValueHandle : public CallbackVH {
302 LazyValueInfoCache *Parent;
303
304 LVIValueHandle(Value *V, LazyValueInfoCache *P)
305 : CallbackVH(V), Parent(P) { }
306
307 void deleted();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000308 void allUsesReplacedWith(Value *V) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000309 deleted();
310 }
Owen Anderson7f9cb742010-07-30 23:59:40 +0000311 };
312
Chris Lattner2c5adf82009-11-15 19:59:49 +0000313 /// ValueCache - This is all of the cached information for all values,
314 /// mapped from Value* to key information.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000315 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000316
317 /// OverDefinedCache - This tracks, on a per-block basis, the set of
318 /// values that are over-defined at the end of that block. This is required
319 /// for cache updating.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000320 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > OverDefinedCache;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000321
Owen Andersonf33b3022010-12-09 06:14:58 +0000322 LVILatticeVal &getCachedEntryForBlock(Value *Val, BasicBlock *BB);
323 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
324 LVILatticeVal getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T);
325
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000326 ValueCacheEntryTy &lookup(Value *V) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000327 return ValueCache[LVIValueHandle(V, this)];
328 }
329
330 LVILatticeVal setBlockValue(Value *V, BasicBlock *BB, LVILatticeVal L,
331 ValueCacheEntryTy &Cache) {
332 if (L.isOverdefined()) OverDefinedCache.insert(std::make_pair(BB, V));
333 return Cache[BB] = L;
334 }
335
Chris Lattner2c5adf82009-11-15 19:59:49 +0000336 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000337 /// getValueInBlock - This is the query interface to determine the lattice
338 /// value for the specified Value* at the end of the specified block.
339 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
340
341 /// getValueOnEdge - This is the query interface to determine the lattice
342 /// value for the specified Value* that is true on the specified edge.
343 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000344
345 /// threadEdge - This is the update interface to inform the cache that an
346 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
347 /// NewSucc.
348 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000349
350 /// eraseBlock - This is part of the update interface to inform the cache
351 /// that a block has been deleted.
352 void eraseBlock(BasicBlock *BB);
353
354 /// clear - Empty the cache.
355 void clear() {
356 ValueCache.clear();
357 OverDefinedCache.clear();
358 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000359 };
360} // end anonymous namespace
361
Owen Anderson7f9cb742010-07-30 23:59:40 +0000362void LazyValueInfoCache::LVIValueHandle::deleted() {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000363 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson7f9cb742010-07-30 23:59:40 +0000364 I = Parent->OverDefinedCache.begin(),
365 E = Parent->OverDefinedCache.end();
366 I != E; ) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000367 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000368 ++I;
369 if (tmp->second == getValPtr())
370 Parent->OverDefinedCache.erase(tmp);
371 }
Owen Andersoncf6abd22010-08-11 22:36:04 +0000372
373 // This erasure deallocates *this, so it MUST happen after we're done
374 // using any and all members of *this.
375 Parent->ValueCache.erase(*this);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000376}
377
Owen Anderson00ac77e2010-08-18 18:39:01 +0000378void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
379 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
380 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ) {
381 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
382 ++I;
383 if (tmp->first == BB)
384 OverDefinedCache.erase(tmp);
385 }
386
387 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
388 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
389 I->second.erase(BB);
390}
Owen Anderson7f9cb742010-07-30 23:59:40 +0000391
Owen Andersonf33b3022010-12-09 06:14:58 +0000392LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
393 ValueCacheEntryTy &Cache = lookup(Val);
394 LVILatticeVal &BBLV = Cache[BB];
Chris Lattner2c5adf82009-11-15 19:59:49 +0000395
396 // If we've already computed this block's value, return it.
Chris Lattnere5642812009-11-15 20:00:52 +0000397 if (!BBLV.isUndefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000398 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
Chris Lattner2c5adf82009-11-15 19:59:49 +0000399 return BBLV;
Chris Lattnere5642812009-11-15 20:00:52 +0000400 }
401
Chris Lattner2c5adf82009-11-15 19:59:49 +0000402 // Otherwise, this is the first time we're seeing this block. Reset the
403 // lattice value to overdefined, so that cycles will terminate and be
404 // conservatively correct.
405 BBLV.markOverdefined();
406
Chris Lattner2c5adf82009-11-15 19:59:49 +0000407 Instruction *BBI = dyn_cast<Instruction>(Val);
408 if (BBI == 0 || BBI->getParent() != BB) {
409 LVILatticeVal Result; // Start Undefined.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000410
Owen Andersonc8ef7502010-08-24 20:47:29 +0000411 // If this is a pointer, and there's a load from that pointer in this BB,
412 // then we know that the pointer can't be NULL.
Owen Anderson1593dd62010-09-03 19:08:37 +0000413 bool NotNull = false;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000414 if (Val->getType()->isPointerTy()) {
Owen Anderson6cd20752010-08-25 01:16:47 +0000415 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){
416 LoadInst *L = dyn_cast<LoadInst>(BI);
417 if (L && L->getPointerAddressSpace() == 0 &&
Dan Gohman5034dd32010-12-15 20:02:24 +0000418 GetUnderlyingObject(L->getPointerOperand()) ==
419 GetUnderlyingObject(Val)) {
Owen Anderson1593dd62010-09-03 19:08:37 +0000420 NotNull = true;
421 break;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000422 }
423 }
424 }
425
426 unsigned NumPreds = 0;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000427 // Loop over all of our predecessors, merging what we know from them into
428 // result.
429 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000430 Result.mergeIn(getEdgeValue(Val, *PI, BB));
Chris Lattner2c5adf82009-11-15 19:59:49 +0000431
432 // If we hit overdefined, exit early. The BlockVals entry is already set
433 // to overdefined.
Chris Lattnere5642812009-11-15 20:00:52 +0000434 if (Result.isOverdefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000435 DEBUG(dbgs() << " compute BB '" << BB->getName()
Chris Lattnere5642812009-11-15 20:00:52 +0000436 << "' - overdefined because of pred.\n");
Owen Anderson1593dd62010-09-03 19:08:37 +0000437 // If we previously determined that this is a pointer that can't be null
438 // then return that rather than giving up entirely.
439 if (NotNull) {
440 const PointerType *PTy = cast<PointerType>(Val->getType());
441 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
442 }
443
Owen Andersonf33b3022010-12-09 06:14:58 +0000444 return setBlockValue(Val, BB, Result, Cache);
Chris Lattnere5642812009-11-15 20:00:52 +0000445 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000446 ++NumPreds;
447 }
448
Owen Anderson1593dd62010-09-03 19:08:37 +0000449
Chris Lattner2c5adf82009-11-15 19:59:49 +0000450 // If this is the entry block, we must be asking about an argument. The
451 // value is overdefined.
452 if (NumPreds == 0 && BB == &BB->getParent()->front()) {
453 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
454 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000455 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000456 }
457
458 // Return the merged value, which is more precise than 'overdefined'.
459 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000460 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000461 }
462
463 // If this value is defined by an instruction in this block, we have to
464 // process it here somehow or return overdefined.
465 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000466 LVILatticeVal Result; // Start Undefined.
467
468 // Loop over all of our predecessors, merging what we know from them into
469 // result.
470 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000471 Value *PhiVal = PN->getIncomingValueForBlock(*PI);
Owen Andersonf33b3022010-12-09 06:14:58 +0000472 Result.mergeIn(getValueOnEdge(PhiVal, *PI, BB));
Owen Anderson7f9cb742010-07-30 23:59:40 +0000473
474 // If we hit overdefined, exit early. The BlockVals entry is already set
475 // to overdefined.
476 if (Result.isOverdefined()) {
477 DEBUG(dbgs() << " compute BB '" << BB->getName()
478 << "' - overdefined because of pred.\n");
Owen Andersonf33b3022010-12-09 06:14:58 +0000479 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000480 }
481 }
482
483 // Return the merged value, which is more precise than 'overdefined'.
484 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000485 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000486 }
Chris Lattnere5642812009-11-15 20:00:52 +0000487
Owen Andersonf33b3022010-12-09 06:14:58 +0000488 assert(Cache[BB].isOverdefined() &&
489 "Recursive query changed our cache?");
Owen Andersonb81fd622010-08-18 21:11:37 +0000490
491 // We can only analyze the definitions of certain classes of instructions
492 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000493 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000494 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
495 !BBI->getType()->isIntegerTy()) {
496 DEBUG(dbgs() << " compute BB '" << BB->getName()
497 << "' - overdefined because inst def found.\n");
498 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000499 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000500 }
501
502 // FIXME: We're currently limited to binops with a constant RHS. This should
503 // be improved.
504 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
505 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
506 DEBUG(dbgs() << " compute BB '" << BB->getName()
507 << "' - overdefined because inst def found.\n");
508
509 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000510 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000511 }
512
513 // Figure out the range of the LHS. If that fails, bail.
Owen Andersonf33b3022010-12-09 06:14:58 +0000514 LVILatticeVal LHSVal = getValueInBlock(BBI->getOperand(0), BB);
Owen Andersonb81fd622010-08-18 21:11:37 +0000515 if (!LHSVal.isConstantRange()) {
516 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000517 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000518 }
519
520 ConstantInt *RHS = 0;
521 ConstantRange LHSRange = LHSVal.getConstantRange();
522 ConstantRange RHSRange(1);
523 const IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
524 if (isa<BinaryOperator>(BBI)) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000525 RHS = dyn_cast<ConstantInt>(BBI->getOperand(1));
526 if (!RHS) {
527 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000528 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson59b06dc2010-08-24 07:55:44 +0000529 }
530
Owen Andersonb81fd622010-08-18 21:11:37 +0000531 RHSRange = ConstantRange(RHS->getValue(), RHS->getValue()+1);
532 }
533
534 // NOTE: We're currently limited by the set of operations that ConstantRange
535 // can evaluate symbolically. Enhancing that set will allows us to analyze
536 // more definitions.
537 switch (BBI->getOpcode()) {
538 case Instruction::Add:
539 Result.markConstantRange(LHSRange.add(RHSRange));
540 break;
541 case Instruction::Sub:
542 Result.markConstantRange(LHSRange.sub(RHSRange));
543 break;
544 case Instruction::Mul:
545 Result.markConstantRange(LHSRange.multiply(RHSRange));
546 break;
547 case Instruction::UDiv:
548 Result.markConstantRange(LHSRange.udiv(RHSRange));
549 break;
550 case Instruction::Shl:
551 Result.markConstantRange(LHSRange.shl(RHSRange));
552 break;
553 case Instruction::LShr:
554 Result.markConstantRange(LHSRange.lshr(RHSRange));
555 break;
556 case Instruction::Trunc:
557 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
558 break;
559 case Instruction::SExt:
560 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
561 break;
562 case Instruction::ZExt:
563 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
564 break;
565 case Instruction::BitCast:
566 Result.markConstantRange(LHSRange);
567 break;
Nick Lewycky198381e2010-09-07 05:39:02 +0000568 case Instruction::And:
569 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
570 break;
571 case Instruction::Or:
572 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
573 break;
Owen Andersonb81fd622010-08-18 21:11:37 +0000574
575 // Unhandled instructions are overdefined.
576 default:
577 DEBUG(dbgs() << " compute BB '" << BB->getName()
578 << "' - overdefined because inst def found.\n");
579 Result.markOverdefined();
580 break;
581 }
582
Owen Andersonf33b3022010-12-09 06:14:58 +0000583 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner10f2d132009-11-11 00:22:30 +0000584}
585
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000586
Chris Lattner800c47e2009-11-15 20:02:12 +0000587/// getEdgeValue - This method attempts to infer more complex
Owen Andersonf33b3022010-12-09 06:14:58 +0000588LVILatticeVal LazyValueInfoCache::getEdgeValue(Value *Val,
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000589 BasicBlock *BBFrom,
590 BasicBlock *BBTo) {
Chris Lattner800c47e2009-11-15 20:02:12 +0000591 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
592 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +0000593 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
594 // If this is a conditional branch and only one successor goes to BBTo, then
595 // we maybe able to infer something from the condition.
596 if (BI->isConditional() &&
597 BI->getSuccessor(0) != BI->getSuccessor(1)) {
598 bool isTrueDest = BI->getSuccessor(0) == BBTo;
599 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
600 "BBTo isn't a successor of BBFrom");
601
602 // If V is the condition of the branch itself, then we know exactly what
603 // it is.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000604 if (BI->getCondition() == Val)
Chris Lattner16976522009-11-11 22:48:44 +0000605 return LVILatticeVal::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +0000606 Type::getInt1Ty(Val->getContext()), isTrueDest));
Chris Lattner16976522009-11-11 22:48:44 +0000607
608 // If the condition of the branch is an equality comparison, we may be
609 // able to infer the value.
Owen Anderson2d0f2472010-08-11 04:24:25 +0000610 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
611 if (ICI && ICI->getOperand(0) == Val &&
612 isa<Constant>(ICI->getOperand(1))) {
613 if (ICI->isEquality()) {
614 // We know that V has the RHS constant if this is a true SETEQ or
615 // false SETNE.
616 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
617 return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
618 return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
Chris Lattner16976522009-11-11 22:48:44 +0000619 }
Owen Anderson2d0f2472010-08-11 04:24:25 +0000620
621 if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
622 // Calculate the range of values that would satisfy the comparison.
623 ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
624 ConstantRange TrueValues =
625 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
626
627 // If we're interested in the false dest, invert the condition.
628 if (!isTrueDest) TrueValues = TrueValues.inverse();
629
630 // Figure out the possible values of the query BEFORE this branch.
Owen Andersonf33b3022010-12-09 06:14:58 +0000631 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Owen Anderson660cab32010-08-27 17:12:29 +0000632 if (!InBlock.isConstantRange())
633 return LVILatticeVal::getRange(TrueValues);
Owen Anderson2d0f2472010-08-11 04:24:25 +0000634
635 // Find all potential values that satisfy both the input and output
636 // conditions.
637 ConstantRange PossibleValues =
638 TrueValues.intersectWith(InBlock.getConstantRange());
639
640 return LVILatticeVal::getRange(PossibleValues);
641 }
642 }
Chris Lattner16976522009-11-11 22:48:44 +0000643 }
644 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000645
646 // If the edge was formed by a switch on the value, then we may know exactly
647 // what it is.
648 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Owen Andersondae90c62010-08-24 21:59:42 +0000649 if (SI->getCondition() == Val) {
Owen Anderson4caef602010-09-02 22:16:52 +0000650 // We don't know anything in the default case.
Owen Andersondae90c62010-08-24 21:59:42 +0000651 if (SI->getDefaultDest() == BBTo) {
Owen Andersondae90c62010-08-24 21:59:42 +0000652 LVILatticeVal Result;
Owen Anderson4caef602010-09-02 22:16:52 +0000653 Result.markOverdefined();
Owen Andersondae90c62010-08-24 21:59:42 +0000654 return Result;
655 }
656
Chris Lattner800c47e2009-11-15 20:02:12 +0000657 // We only know something if there is exactly one value that goes from
658 // BBFrom to BBTo.
659 unsigned NumEdges = 0;
660 ConstantInt *EdgeVal = 0;
661 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
662 if (SI->getSuccessor(i) != BBTo) continue;
663 if (NumEdges++) break;
664 EdgeVal = SI->getCaseValue(i);
665 }
666 assert(EdgeVal && "Missing successor?");
667 if (NumEdges == 1)
668 return LVILatticeVal::get(EdgeVal);
669 }
670 }
Chris Lattner16976522009-11-11 22:48:44 +0000671
672 // Otherwise see if the value is known in the block.
Owen Andersonf33b3022010-12-09 06:14:58 +0000673 return getBlockValue(Val, BBFrom);
Chris Lattner16976522009-11-11 22:48:44 +0000674}
675
Chris Lattner2c5adf82009-11-15 19:59:49 +0000676LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
677 // If already a constant, there is nothing to compute.
Chris Lattner16976522009-11-11 22:48:44 +0000678 if (Constant *VC = dyn_cast<Constant>(V))
Chris Lattner2c5adf82009-11-15 19:59:49 +0000679 return LVILatticeVal::get(VC);
Chris Lattner16976522009-11-11 22:48:44 +0000680
David Greene5d93a1f2009-12-23 20:43:58 +0000681 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000682 << BB->getName() << "'\n");
683
Owen Andersonf33b3022010-12-09 06:14:58 +0000684 LVILatticeVal Result = getBlockValue(V, BB);
Chris Lattner16976522009-11-11 22:48:44 +0000685
David Greene5d93a1f2009-12-23 20:43:58 +0000686 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000687 return Result;
688}
Chris Lattner16976522009-11-11 22:48:44 +0000689
Chris Lattner2c5adf82009-11-15 19:59:49 +0000690LVILatticeVal LazyValueInfoCache::
691getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
692 // If already a constant, there is nothing to compute.
693 if (Constant *VC = dyn_cast<Constant>(V))
694 return LVILatticeVal::get(VC);
695
David Greene5d93a1f2009-12-23 20:43:58 +0000696 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000697 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000698
Owen Andersonf33b3022010-12-09 06:14:58 +0000699 LVILatticeVal Result = getEdgeValue(V, FromBB, ToBB);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000700
David Greene5d93a1f2009-12-23 20:43:58 +0000701 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000702
703 return Result;
704}
705
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000706void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
707 BasicBlock *NewSucc) {
708 // When an edge in the graph has been threaded, values that we could not
709 // determine a value for before (i.e. were marked overdefined) may be possible
710 // to solve now. We do NOT try to proactively update these values. Instead,
711 // we clear their entries from the cache, and allow lazy updating to recompute
712 // them when needed.
713
714 // The updating process is fairly simple: we need to dropped cached info
715 // for all values that were marked overdefined in OldSucc, and for those same
716 // values in any successor of OldSucc (except NewSucc) in which they were
717 // also marked overdefined.
718 std::vector<BasicBlock*> worklist;
719 worklist.push_back(OldSucc);
720
Owen Anderson9a65dc92010-07-27 23:58:11 +0000721 DenseSet<Value*> ClearSet;
Owen Anderson00ac77e2010-08-18 18:39:01 +0000722 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson9a65dc92010-07-27 23:58:11 +0000723 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ++I) {
724 if (I->first == OldSucc)
725 ClearSet.insert(I->second);
726 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000727
728 // Use a worklist to perform a depth-first search of OldSucc's successors.
729 // NOTE: We do not need a visited list since any blocks we have already
730 // visited will have had their overdefined markers cleared already, and we
731 // thus won't loop to their successors.
732 while (!worklist.empty()) {
733 BasicBlock *ToUpdate = worklist.back();
734 worklist.pop_back();
735
736 // Skip blocks only accessible through NewSucc.
737 if (ToUpdate == NewSucc) continue;
738
739 bool changed = false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000740 for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000741 I != E; ++I) {
742 // If a value was marked overdefined in OldSucc, and is here too...
Owen Anderson00ac77e2010-08-18 18:39:01 +0000743 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator OI =
Owen Anderson9a65dc92010-07-27 23:58:11 +0000744 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
745 if (OI == OverDefinedCache.end()) continue;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000746
Owen Anderson9a65dc92010-07-27 23:58:11 +0000747 // Remove it from the caches.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000748 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
Owen Anderson9a65dc92010-07-27 23:58:11 +0000749 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
750
751 assert(CI != Entry.end() && "Couldn't find entry to update?");
752 Entry.erase(CI);
753 OverDefinedCache.erase(OI);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000754
Owen Anderson9a65dc92010-07-27 23:58:11 +0000755 // If we removed anything, then we potentially need to update
756 // blocks successors too.
757 changed = true;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000758 }
759
760 if (!changed) continue;
761
762 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
763 }
764}
765
Chris Lattner2c5adf82009-11-15 19:59:49 +0000766//===----------------------------------------------------------------------===//
767// LazyValueInfo Impl
768//===----------------------------------------------------------------------===//
769
Chris Lattner2c5adf82009-11-15 19:59:49 +0000770/// getCache - This lazily constructs the LazyValueInfoCache.
771static LazyValueInfoCache &getCache(void *&PImpl) {
772 if (!PImpl)
773 PImpl = new LazyValueInfoCache();
774 return *static_cast<LazyValueInfoCache*>(PImpl);
775}
776
Owen Anderson00ac77e2010-08-18 18:39:01 +0000777bool LazyValueInfo::runOnFunction(Function &F) {
778 if (PImpl)
779 getCache(PImpl).clear();
780
781 TD = getAnalysisIfAvailable<TargetData>();
782 // Fully lazy.
783 return false;
784}
785
Chris Lattner2c5adf82009-11-15 19:59:49 +0000786void LazyValueInfo::releaseMemory() {
787 // If the cache was allocated, free it.
788 if (PImpl) {
789 delete &getCache(PImpl);
790 PImpl = 0;
791 }
792}
793
794Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
795 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
796
Chris Lattner16976522009-11-11 22:48:44 +0000797 if (Result.isConstant())
798 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000799 if (Result.isConstantRange()) {
Owen Andersonee61fcf2010-08-27 23:29:38 +0000800 ConstantRange CR = Result.getConstantRange();
801 if (const APInt *SingleVal = CR.getSingleElement())
802 return ConstantInt::get(V->getContext(), *SingleVal);
803 }
Chris Lattner16976522009-11-11 22:48:44 +0000804 return 0;
805}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000806
Chris Lattner38392bb2009-11-12 01:29:10 +0000807/// getConstantOnEdge - Determine whether the specified value is known to be a
808/// constant on the specified edge. Return null if not.
809Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
810 BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000811 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattner38392bb2009-11-12 01:29:10 +0000812
813 if (Result.isConstant())
814 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000815 if (Result.isConstantRange()) {
Owen Anderson9f014062010-08-10 20:03:09 +0000816 ConstantRange CR = Result.getConstantRange();
817 if (const APInt *SingleVal = CR.getSingleElement())
818 return ConstantInt::get(V->getContext(), *SingleVal);
819 }
Chris Lattner38392bb2009-11-12 01:29:10 +0000820 return 0;
821}
822
Chris Lattnerb52675b2009-11-12 04:36:58 +0000823/// getPredicateOnEdge - Determine whether the specified value comparison
824/// with a constant is known to be true or false on the specified CFG edge.
825/// Pred is a CmpInst predicate.
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000826LazyValueInfo::Tristate
Chris Lattnerb52675b2009-11-12 04:36:58 +0000827LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
828 BasicBlock *FromBB, BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000829 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000830
Chris Lattnerb52675b2009-11-12 04:36:58 +0000831 // If we know the value is a constant, evaluate the conditional.
832 Constant *Res = 0;
833 if (Result.isConstant()) {
834 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD);
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000835 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Chris Lattnerb52675b2009-11-12 04:36:58 +0000836 return ResCI->isZero() ? False : True;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000837 return Unknown;
838 }
839
Owen Anderson9f014062010-08-10 20:03:09 +0000840 if (Result.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000841 ConstantInt *CI = dyn_cast<ConstantInt>(C);
842 if (!CI) return Unknown;
843
Owen Anderson9f014062010-08-10 20:03:09 +0000844 ConstantRange CR = Result.getConstantRange();
845 if (Pred == ICmpInst::ICMP_EQ) {
846 if (!CR.contains(CI->getValue()))
847 return False;
848
849 if (CR.isSingleElement() && CR.contains(CI->getValue()))
850 return True;
851 } else if (Pred == ICmpInst::ICMP_NE) {
852 if (!CR.contains(CI->getValue()))
853 return True;
854
855 if (CR.isSingleElement() && CR.contains(CI->getValue()))
856 return False;
857 }
858
859 // Handle more complex predicates.
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000860 ConstantRange TrueValues =
861 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
862 if (TrueValues.contains(CR))
Owen Anderson9f014062010-08-10 20:03:09 +0000863 return True;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000864 if (TrueValues.inverse().contains(CR))
865 return False;
Owen Anderson9f014062010-08-10 20:03:09 +0000866 return Unknown;
867 }
868
Chris Lattner2c5adf82009-11-15 19:59:49 +0000869 if (Result.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000870 // If this is an equality comparison, we can try to fold it knowing that
871 // "V != C1".
872 if (Pred == ICmpInst::ICMP_EQ) {
873 // !C1 == C -> false iff C1 == C.
874 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
875 Result.getNotConstant(), C, TD);
876 if (Res->isNullValue())
877 return False;
878 } else if (Pred == ICmpInst::ICMP_NE) {
879 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +0000880 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Chris Lattnerb52675b2009-11-12 04:36:58 +0000881 Result.getNotConstant(), C, TD);
882 if (Res->isNullValue())
883 return True;
884 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000885 return Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000886 }
887
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000888 return Unknown;
889}
890
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000891void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000892 BasicBlock *NewSucc) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000893 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
894}
895
896void LazyValueInfo::eraseBlock(BasicBlock *BB) {
897 if (PImpl) getCache(PImpl).eraseBlock(BB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000898}