blob: d73c504f14de51309f75cde5fccb7a031f1bc3ed [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"
Chris Lattnercc4d3b22009-11-11 02:08:33 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
19#include "llvm/Analysis/ConstantFolding.h"
20#include "llvm/Target/TargetData.h"
Chris Lattner16976522009-11-11 22:48:44 +000021#include "llvm/Support/CFG.h"
Owen Anderson5be2e782010-08-05 22:59:19 +000022#include "llvm/Support/ConstantRange.h"
Chris Lattnerb8c124c2009-11-12 01:22:16 +000023#include "llvm/Support/Debug.h"
Chris Lattner16976522009-11-11 22:48:44 +000024#include "llvm/Support/raw_ostream.h"
Owen Anderson7f9cb742010-07-30 23:59:40 +000025#include "llvm/Support/ValueHandle.h"
Chris Lattner16976522009-11-11 22:48:44 +000026#include "llvm/ADT/DenseMap.h"
Owen Anderson9a65dc92010-07-27 23:58:11 +000027#include "llvm/ADT/DenseSet.h"
Chris Lattnere5642812009-11-15 20:00:52 +000028#include "llvm/ADT/STLExtras.h"
Owen Anderson6bcd3a02010-09-07 19:16:25 +000029#include <map>
30#include <set>
Chris Lattner10f2d132009-11-11 00:22:30 +000031using namespace llvm;
32
33char LazyValueInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000034INITIALIZE_PASS(LazyValueInfo, "lazy-value-info",
Owen Andersonce665bd2010-10-07 22:25:06 +000035 "Lazy Value Information Analysis", false, true)
Chris Lattner10f2d132009-11-11 00:22:30 +000036
37namespace llvm {
38 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
39}
40
Chris Lattnercc4d3b22009-11-11 02:08:33 +000041
42//===----------------------------------------------------------------------===//
43// LVILatticeVal
44//===----------------------------------------------------------------------===//
45
46/// LVILatticeVal - This is the information tracked by LazyValueInfo for each
47/// value.
48///
49/// FIXME: This is basically just for bringup, this can be made a lot more rich
50/// in the future.
51///
52namespace {
53class LVILatticeVal {
54 enum LatticeValueTy {
Nick Lewycky69bfdf52010-12-15 18:57:18 +000055 /// undefined - This Value has no known value yet.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000056 undefined,
Owen Anderson5be2e782010-08-05 22:59:19 +000057
Nick Lewycky69bfdf52010-12-15 18:57:18 +000058 /// constant - This Value has a specific constant value.
Chris Lattnercc4d3b22009-11-11 02:08:33 +000059 constant,
Nick Lewycky69bfdf52010-12-15 18:57:18 +000060 /// notconstant - This Value is known to not have the specified value.
Chris Lattnerb52675b2009-11-12 04:36:58 +000061 notconstant,
62
Nick Lewycky69bfdf52010-12-15 18:57:18 +000063 /// constantrange - The Value falls within this range.
Owen Anderson5be2e782010-08-05 22:59:19 +000064 constantrange,
65
Nick Lewycky69bfdf52010-12-15 18:57:18 +000066 /// overdefined - This value is not known to be constant, and we know that
Chris Lattnercc4d3b22009-11-11 02:08:33 +000067 /// it has a value.
68 overdefined
69 };
70
71 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattnerb52675b2009-11-12 04:36:58 +000072 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersondb78d732010-08-05 22:10:46 +000073 LatticeValueTy Tag;
74 Constant *Val;
Owen Anderson5be2e782010-08-05 22:59:19 +000075 ConstantRange Range;
Chris Lattnercc4d3b22009-11-11 02:08:33 +000076
77public:
Owen Anderson5be2e782010-08-05 22:59:19 +000078 LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {}
Chris Lattnercc4d3b22009-11-11 02:08:33 +000079
Chris Lattner16976522009-11-11 22:48:44 +000080 static LVILatticeVal get(Constant *C) {
81 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000082 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000083 Res.markConstant(C);
Chris Lattner16976522009-11-11 22:48:44 +000084 return Res;
85 }
Chris Lattnerb52675b2009-11-12 04:36:58 +000086 static LVILatticeVal getNot(Constant *C) {
87 LVILatticeVal Res;
Nick Lewycky69bfdf52010-12-15 18:57:18 +000088 if (!isa<UndefValue>(C))
Owen Anderson9f014062010-08-10 20:03:09 +000089 Res.markNotConstant(C);
Chris Lattnerb52675b2009-11-12 04:36:58 +000090 return Res;
91 }
Owen Anderson625051b2010-08-10 23:20:01 +000092 static LVILatticeVal getRange(ConstantRange CR) {
93 LVILatticeVal Res;
94 Res.markConstantRange(CR);
95 return Res;
96 }
Chris Lattner16976522009-11-11 22:48:44 +000097
Owen Anderson5be2e782010-08-05 22:59:19 +000098 bool isUndefined() const { return Tag == undefined; }
99 bool isConstant() const { return Tag == constant; }
100 bool isNotConstant() const { return Tag == notconstant; }
101 bool isConstantRange() const { return Tag == constantrange; }
102 bool isOverdefined() const { return Tag == overdefined; }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000103
104 Constant *getConstant() const {
105 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000106 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000107 }
108
Chris Lattnerb52675b2009-11-12 04:36:58 +0000109 Constant *getNotConstant() const {
110 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000111 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000112 }
113
Owen Anderson5be2e782010-08-05 22:59:19 +0000114 ConstantRange getConstantRange() const {
115 assert(isConstantRange() &&
116 "Cannot get the constant-range of a non-constant-range!");
117 return Range;
118 }
119
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000120 /// markOverdefined - Return true if this is a change in status.
121 bool markOverdefined() {
122 if (isOverdefined())
123 return false;
Owen Andersondb78d732010-08-05 22:10:46 +0000124 Tag = overdefined;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000125 return true;
126 }
127
128 /// markConstant - Return true if this is a change in status.
129 bool markConstant(Constant *V) {
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000130 assert(V && "Marking constant with NULL");
131 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
132 return markConstantRange(ConstantRange(CI->getValue()));
133 if (isa<UndefValue>(V))
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000134 return false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000135
136 assert((!isConstant() || getConstant() == V) &&
137 "Marking constant with different value");
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000138 assert(isUndefined());
Owen Andersondb78d732010-08-05 22:10:46 +0000139 Tag = constant;
Owen Andersondb78d732010-08-05 22:10:46 +0000140 Val = V;
Chris Lattner16976522009-11-11 22:48:44 +0000141 return true;
142 }
143
Chris Lattnerb52675b2009-11-12 04:36:58 +0000144 /// markNotConstant - Return true if this is a change in status.
145 bool markNotConstant(Constant *V) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000146 assert(V && "Marking constant with NULL");
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000147 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
148 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
149 if (isa<UndefValue>(V))
150 return false;
151
152 assert((!isConstant() || getConstant() != V) &&
153 "Marking constant !constant with same value");
154 assert((!isNotConstant() || getNotConstant() == V) &&
155 "Marking !constant with different value");
156 assert(isUndefined() || isConstant());
157 Tag = notconstant;
Owen Andersondb78d732010-08-05 22:10:46 +0000158 Val = V;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000159 return true;
160 }
161
Owen Anderson5be2e782010-08-05 22:59:19 +0000162 /// markConstantRange - Return true if this is a change in status.
163 bool markConstantRange(const ConstantRange NewR) {
164 if (isConstantRange()) {
165 if (NewR.isEmptySet())
166 return markOverdefined();
167
Owen Anderson5be2e782010-08-05 22:59:19 +0000168 bool changed = Range == NewR;
169 Range = NewR;
170 return changed;
171 }
172
173 assert(isUndefined());
174 if (NewR.isEmptySet())
175 return markOverdefined();
Owen Anderson5be2e782010-08-05 22:59:19 +0000176
177 Tag = constantrange;
178 Range = NewR;
179 return true;
180 }
181
Chris Lattner16976522009-11-11 22:48:44 +0000182 /// mergeIn - Merge the specified lattice value into this one, updating this
183 /// one and returning true if anything changed.
184 bool mergeIn(const LVILatticeVal &RHS) {
185 if (RHS.isUndefined() || isOverdefined()) return false;
186 if (RHS.isOverdefined()) return markOverdefined();
187
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000188 if (isUndefined()) {
189 Tag = RHS.Tag;
190 Val = RHS.Val;
191 Range = RHS.Range;
192 return true;
Chris Lattnerf496e792009-11-12 04:57:13 +0000193 }
194
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000195 if (isConstant()) {
196 if (RHS.isConstant()) {
197 if (Val == RHS.Val)
198 return false;
199 return markOverdefined();
200 }
201
202 if (RHS.isNotConstant()) {
203 if (Val == RHS.Val)
204 return markOverdefined();
205
206 // Unless we can prove that the two Constants are different, we must
207 // move to overdefined.
208 // FIXME: use TargetData for smarter constant folding.
209 if (ConstantInt *Res = dyn_cast<ConstantInt>(
210 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
211 getConstant(),
212 RHS.getNotConstant())))
213 if (Res->isOne())
214 return markNotConstant(RHS.getNotConstant());
215
216 return markOverdefined();
217 }
218
219 // RHS is a ConstantRange, LHS is a non-integer Constant.
220
221 // FIXME: consider the case where RHS is a range [1, 0) and LHS is
222 // a function. The correct result is to pick up RHS.
223
Chris Lattner16976522009-11-11 22:48:44 +0000224 return markOverdefined();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000225 }
226
227 if (isNotConstant()) {
228 if (RHS.isConstant()) {
229 if (Val == RHS.Val)
230 return markOverdefined();
231
232 // Unless we can prove that the two Constants are different, we must
233 // move to overdefined.
234 // FIXME: use TargetData for smarter constant folding.
235 if (ConstantInt *Res = dyn_cast<ConstantInt>(
236 ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
237 getNotConstant(),
238 RHS.getConstant())))
239 if (Res->isOne())
240 return false;
241
242 return markOverdefined();
243 }
244
245 if (RHS.isNotConstant()) {
246 if (Val == RHS.Val)
247 return false;
248 return markOverdefined();
249 }
250
251 return markOverdefined();
252 }
253
254 assert(isConstantRange() && "New LVILattice type?");
255 if (!RHS.isConstantRange())
256 return markOverdefined();
257
258 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
259 if (NewR.isFullSet())
260 return markOverdefined();
261 return markConstantRange(NewR);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000262 }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000263};
264
265} // end anonymous namespace.
266
Chris Lattner16976522009-11-11 22:48:44 +0000267namespace llvm {
268raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
269 if (Val.isUndefined())
270 return OS << "undefined";
271 if (Val.isOverdefined())
272 return OS << "overdefined";
Chris Lattnerb52675b2009-11-12 04:36:58 +0000273
274 if (Val.isNotConstant())
275 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson2f3ffb82010-08-09 20:50:46 +0000276 else if (Val.isConstantRange())
277 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
278 << Val.getConstantRange().getUpper() << '>';
Chris Lattner16976522009-11-11 22:48:44 +0000279 return OS << "constant<" << *Val.getConstant() << '>';
280}
281}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000282
283//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000284// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000285//===----------------------------------------------------------------------===//
286
Chris Lattner2c5adf82009-11-15 19:59:49 +0000287namespace {
288 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
289 /// maintains information about queries across the clients' queries.
290 class LazyValueInfoCache {
Owen Anderson81881bc2010-07-30 20:56:07 +0000291 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000292 /// ValueCacheEntryTy - This is all of the cached block information for
293 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
294 /// entries, allowing us to do a lookup with a binary search.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000295 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000296
Owen Anderson81881bc2010-07-30 20:56:07 +0000297 private:
Owen Anderson7f9cb742010-07-30 23:59:40 +0000298 /// LVIValueHandle - A callback value handle update the cache when
299 /// values are erased.
300 struct LVIValueHandle : public CallbackVH {
301 LazyValueInfoCache *Parent;
302
303 LVIValueHandle(Value *V, LazyValueInfoCache *P)
304 : CallbackVH(V), Parent(P) { }
305
306 void deleted();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000307 void allUsesReplacedWith(Value *V) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000308 deleted();
309 }
Owen Anderson7f9cb742010-07-30 23:59:40 +0000310 };
311
Chris Lattner2c5adf82009-11-15 19:59:49 +0000312 /// ValueCache - This is all of the cached information for all values,
313 /// mapped from Value* to key information.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000314 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000315
316 /// OverDefinedCache - This tracks, on a per-block basis, the set of
317 /// values that are over-defined at the end of that block. This is required
318 /// for cache updating.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000319 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > OverDefinedCache;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000320
Owen Andersonf33b3022010-12-09 06:14:58 +0000321 LVILatticeVal &getCachedEntryForBlock(Value *Val, BasicBlock *BB);
322 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
323 LVILatticeVal getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T);
324
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000325 ValueCacheEntryTy &lookup(Value *V) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000326 return ValueCache[LVIValueHandle(V, this)];
327 }
328
329 LVILatticeVal setBlockValue(Value *V, BasicBlock *BB, LVILatticeVal L,
330 ValueCacheEntryTy &Cache) {
331 if (L.isOverdefined()) OverDefinedCache.insert(std::make_pair(BB, V));
332 return Cache[BB] = L;
333 }
334
Chris Lattner2c5adf82009-11-15 19:59:49 +0000335 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000336 /// getValueInBlock - This is the query interface to determine the lattice
337 /// value for the specified Value* at the end of the specified block.
338 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
339
340 /// getValueOnEdge - This is the query interface to determine the lattice
341 /// value for the specified Value* that is true on the specified edge.
342 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000343
344 /// threadEdge - This is the update interface to inform the cache that an
345 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
346 /// NewSucc.
347 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000348
349 /// eraseBlock - This is part of the update interface to inform the cache
350 /// that a block has been deleted.
351 void eraseBlock(BasicBlock *BB);
352
353 /// clear - Empty the cache.
354 void clear() {
355 ValueCache.clear();
356 OverDefinedCache.clear();
357 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000358 };
359} // end anonymous namespace
360
Owen Anderson7f9cb742010-07-30 23:59:40 +0000361void LazyValueInfoCache::LVIValueHandle::deleted() {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000362 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson7f9cb742010-07-30 23:59:40 +0000363 I = Parent->OverDefinedCache.begin(),
364 E = Parent->OverDefinedCache.end();
365 I != E; ) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000366 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000367 ++I;
368 if (tmp->second == getValPtr())
369 Parent->OverDefinedCache.erase(tmp);
370 }
Owen Andersoncf6abd22010-08-11 22:36:04 +0000371
372 // This erasure deallocates *this, so it MUST happen after we're done
373 // using any and all members of *this.
374 Parent->ValueCache.erase(*this);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000375}
376
Owen Anderson00ac77e2010-08-18 18:39:01 +0000377void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
378 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
379 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ) {
380 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
381 ++I;
382 if (tmp->first == BB)
383 OverDefinedCache.erase(tmp);
384 }
385
386 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
387 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
388 I->second.erase(BB);
389}
Owen Anderson7f9cb742010-07-30 23:59:40 +0000390
Owen Andersonf33b3022010-12-09 06:14:58 +0000391LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
392 ValueCacheEntryTy &Cache = lookup(Val);
393 LVILatticeVal &BBLV = Cache[BB];
Chris Lattner2c5adf82009-11-15 19:59:49 +0000394
395 // If we've already computed this block's value, return it.
Chris Lattnere5642812009-11-15 20:00:52 +0000396 if (!BBLV.isUndefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000397 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
Chris Lattner2c5adf82009-11-15 19:59:49 +0000398 return BBLV;
Chris Lattnere5642812009-11-15 20:00:52 +0000399 }
400
Chris Lattner2c5adf82009-11-15 19:59:49 +0000401 // Otherwise, this is the first time we're seeing this block. Reset the
402 // lattice value to overdefined, so that cycles will terminate and be
403 // conservatively correct.
404 BBLV.markOverdefined();
405
Chris Lattner2c5adf82009-11-15 19:59:49 +0000406 Instruction *BBI = dyn_cast<Instruction>(Val);
407 if (BBI == 0 || BBI->getParent() != BB) {
408 LVILatticeVal Result; // Start Undefined.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000409
Owen Andersonc8ef7502010-08-24 20:47:29 +0000410 // If this is a pointer, and there's a load from that pointer in this BB,
411 // then we know that the pointer can't be NULL.
Owen Anderson1593dd62010-09-03 19:08:37 +0000412 bool NotNull = false;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000413 if (Val->getType()->isPointerTy()) {
Owen Anderson6cd20752010-08-25 01:16:47 +0000414 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){
415 LoadInst *L = dyn_cast<LoadInst>(BI);
416 if (L && L->getPointerAddressSpace() == 0 &&
417 L->getPointerOperand()->getUnderlyingObject() ==
418 Val->getUnderlyingObject()) {
Owen Anderson1593dd62010-09-03 19:08:37 +0000419 NotNull = true;
420 break;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000421 }
422 }
423 }
424
425 unsigned NumPreds = 0;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000426 // Loop over all of our predecessors, merging what we know from them into
427 // result.
428 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000429 Result.mergeIn(getEdgeValue(Val, *PI, BB));
Chris Lattner2c5adf82009-11-15 19:59:49 +0000430
431 // If we hit overdefined, exit early. The BlockVals entry is already set
432 // to overdefined.
Chris Lattnere5642812009-11-15 20:00:52 +0000433 if (Result.isOverdefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000434 DEBUG(dbgs() << " compute BB '" << BB->getName()
Chris Lattnere5642812009-11-15 20:00:52 +0000435 << "' - overdefined because of pred.\n");
Owen Anderson1593dd62010-09-03 19:08:37 +0000436 // If we previously determined that this is a pointer that can't be null
437 // then return that rather than giving up entirely.
438 if (NotNull) {
439 const PointerType *PTy = cast<PointerType>(Val->getType());
440 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
441 }
442
Owen Andersonf33b3022010-12-09 06:14:58 +0000443 return setBlockValue(Val, BB, Result, Cache);
Chris Lattnere5642812009-11-15 20:00:52 +0000444 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000445 ++NumPreds;
446 }
447
Owen Anderson1593dd62010-09-03 19:08:37 +0000448
Chris Lattner2c5adf82009-11-15 19:59:49 +0000449 // If this is the entry block, we must be asking about an argument. The
450 // value is overdefined.
451 if (NumPreds == 0 && BB == &BB->getParent()->front()) {
452 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
453 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000454 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000455 }
456
457 // Return the merged value, which is more precise than 'overdefined'.
458 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000459 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000460 }
461
462 // If this value is defined by an instruction in this block, we have to
463 // process it here somehow or return overdefined.
464 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000465 LVILatticeVal Result; // Start Undefined.
466
467 // Loop over all of our predecessors, merging what we know from them into
468 // result.
469 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000470 Value *PhiVal = PN->getIncomingValueForBlock(*PI);
Owen Andersonf33b3022010-12-09 06:14:58 +0000471 Result.mergeIn(getValueOnEdge(PhiVal, *PI, BB));
Owen Anderson7f9cb742010-07-30 23:59:40 +0000472
473 // If we hit overdefined, exit early. The BlockVals entry is already set
474 // to overdefined.
475 if (Result.isOverdefined()) {
476 DEBUG(dbgs() << " compute BB '" << BB->getName()
477 << "' - overdefined because of pred.\n");
Owen Andersonf33b3022010-12-09 06:14:58 +0000478 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000479 }
480 }
481
482 // Return the merged value, which is more precise than 'overdefined'.
483 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000484 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000485 }
Chris Lattnere5642812009-11-15 20:00:52 +0000486
Owen Andersonf33b3022010-12-09 06:14:58 +0000487 assert(Cache[BB].isOverdefined() &&
488 "Recursive query changed our cache?");
Owen Andersonb81fd622010-08-18 21:11:37 +0000489
490 // We can only analyze the definitions of certain classes of instructions
491 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000492 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000493 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
494 !BBI->getType()->isIntegerTy()) {
495 DEBUG(dbgs() << " compute BB '" << BB->getName()
496 << "' - overdefined because inst def found.\n");
497 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000498 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000499 }
500
501 // FIXME: We're currently limited to binops with a constant RHS. This should
502 // be improved.
503 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
504 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
505 DEBUG(dbgs() << " compute BB '" << BB->getName()
506 << "' - overdefined because inst def found.\n");
507
508 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000509 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000510 }
511
512 // Figure out the range of the LHS. If that fails, bail.
Owen Andersonf33b3022010-12-09 06:14:58 +0000513 LVILatticeVal LHSVal = getValueInBlock(BBI->getOperand(0), BB);
Owen Andersonb81fd622010-08-18 21:11:37 +0000514 if (!LHSVal.isConstantRange()) {
515 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000516 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000517 }
518
519 ConstantInt *RHS = 0;
520 ConstantRange LHSRange = LHSVal.getConstantRange();
521 ConstantRange RHSRange(1);
522 const IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
523 if (isa<BinaryOperator>(BBI)) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000524 RHS = dyn_cast<ConstantInt>(BBI->getOperand(1));
525 if (!RHS) {
526 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000527 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson59b06dc2010-08-24 07:55:44 +0000528 }
529
Owen Andersonb81fd622010-08-18 21:11:37 +0000530 RHSRange = ConstantRange(RHS->getValue(), RHS->getValue()+1);
531 }
532
533 // NOTE: We're currently limited by the set of operations that ConstantRange
534 // can evaluate symbolically. Enhancing that set will allows us to analyze
535 // more definitions.
536 switch (BBI->getOpcode()) {
537 case Instruction::Add:
538 Result.markConstantRange(LHSRange.add(RHSRange));
539 break;
540 case Instruction::Sub:
541 Result.markConstantRange(LHSRange.sub(RHSRange));
542 break;
543 case Instruction::Mul:
544 Result.markConstantRange(LHSRange.multiply(RHSRange));
545 break;
546 case Instruction::UDiv:
547 Result.markConstantRange(LHSRange.udiv(RHSRange));
548 break;
549 case Instruction::Shl:
550 Result.markConstantRange(LHSRange.shl(RHSRange));
551 break;
552 case Instruction::LShr:
553 Result.markConstantRange(LHSRange.lshr(RHSRange));
554 break;
555 case Instruction::Trunc:
556 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
557 break;
558 case Instruction::SExt:
559 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
560 break;
561 case Instruction::ZExt:
562 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
563 break;
564 case Instruction::BitCast:
565 Result.markConstantRange(LHSRange);
566 break;
Nick Lewycky198381e2010-09-07 05:39:02 +0000567 case Instruction::And:
568 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
569 break;
570 case Instruction::Or:
571 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
572 break;
Owen Andersonb81fd622010-08-18 21:11:37 +0000573
574 // Unhandled instructions are overdefined.
575 default:
576 DEBUG(dbgs() << " compute BB '" << BB->getName()
577 << "' - overdefined because inst def found.\n");
578 Result.markOverdefined();
579 break;
580 }
581
Owen Andersonf33b3022010-12-09 06:14:58 +0000582 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner10f2d132009-11-11 00:22:30 +0000583}
584
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000585
Chris Lattner800c47e2009-11-15 20:02:12 +0000586/// getEdgeValue - This method attempts to infer more complex
Owen Andersonf33b3022010-12-09 06:14:58 +0000587LVILatticeVal LazyValueInfoCache::getEdgeValue(Value *Val,
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000588 BasicBlock *BBFrom,
589 BasicBlock *BBTo) {
Chris Lattner800c47e2009-11-15 20:02:12 +0000590 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
591 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +0000592 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
593 // If this is a conditional branch and only one successor goes to BBTo, then
594 // we maybe able to infer something from the condition.
595 if (BI->isConditional() &&
596 BI->getSuccessor(0) != BI->getSuccessor(1)) {
597 bool isTrueDest = BI->getSuccessor(0) == BBTo;
598 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
599 "BBTo isn't a successor of BBFrom");
600
601 // If V is the condition of the branch itself, then we know exactly what
602 // it is.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000603 if (BI->getCondition() == Val)
Chris Lattner16976522009-11-11 22:48:44 +0000604 return LVILatticeVal::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +0000605 Type::getInt1Ty(Val->getContext()), isTrueDest));
Chris Lattner16976522009-11-11 22:48:44 +0000606
607 // If the condition of the branch is an equality comparison, we may be
608 // able to infer the value.
Owen Anderson2d0f2472010-08-11 04:24:25 +0000609 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
610 if (ICI && ICI->getOperand(0) == Val &&
611 isa<Constant>(ICI->getOperand(1))) {
612 if (ICI->isEquality()) {
613 // We know that V has the RHS constant if this is a true SETEQ or
614 // false SETNE.
615 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
616 return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
617 return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
Chris Lattner16976522009-11-11 22:48:44 +0000618 }
Owen Anderson2d0f2472010-08-11 04:24:25 +0000619
620 if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
621 // Calculate the range of values that would satisfy the comparison.
622 ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
623 ConstantRange TrueValues =
624 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
625
626 // If we're interested in the false dest, invert the condition.
627 if (!isTrueDest) TrueValues = TrueValues.inverse();
628
629 // Figure out the possible values of the query BEFORE this branch.
Owen Andersonf33b3022010-12-09 06:14:58 +0000630 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Owen Anderson660cab32010-08-27 17:12:29 +0000631 if (!InBlock.isConstantRange())
632 return LVILatticeVal::getRange(TrueValues);
Owen Anderson2d0f2472010-08-11 04:24:25 +0000633
634 // Find all potential values that satisfy both the input and output
635 // conditions.
636 ConstantRange PossibleValues =
637 TrueValues.intersectWith(InBlock.getConstantRange());
638
639 return LVILatticeVal::getRange(PossibleValues);
640 }
641 }
Chris Lattner16976522009-11-11 22:48:44 +0000642 }
643 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000644
645 // If the edge was formed by a switch on the value, then we may know exactly
646 // what it is.
647 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Owen Andersondae90c62010-08-24 21:59:42 +0000648 if (SI->getCondition() == Val) {
Owen Anderson4caef602010-09-02 22:16:52 +0000649 // We don't know anything in the default case.
Owen Andersondae90c62010-08-24 21:59:42 +0000650 if (SI->getDefaultDest() == BBTo) {
Owen Andersondae90c62010-08-24 21:59:42 +0000651 LVILatticeVal Result;
Owen Anderson4caef602010-09-02 22:16:52 +0000652 Result.markOverdefined();
Owen Andersondae90c62010-08-24 21:59:42 +0000653 return Result;
654 }
655
Chris Lattner800c47e2009-11-15 20:02:12 +0000656 // We only know something if there is exactly one value that goes from
657 // BBFrom to BBTo.
658 unsigned NumEdges = 0;
659 ConstantInt *EdgeVal = 0;
660 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
661 if (SI->getSuccessor(i) != BBTo) continue;
662 if (NumEdges++) break;
663 EdgeVal = SI->getCaseValue(i);
664 }
665 assert(EdgeVal && "Missing successor?");
666 if (NumEdges == 1)
667 return LVILatticeVal::get(EdgeVal);
668 }
669 }
Chris Lattner16976522009-11-11 22:48:44 +0000670
671 // Otherwise see if the value is known in the block.
Owen Andersonf33b3022010-12-09 06:14:58 +0000672 return getBlockValue(Val, BBFrom);
Chris Lattner16976522009-11-11 22:48:44 +0000673}
674
Chris Lattner2c5adf82009-11-15 19:59:49 +0000675LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
676 // If already a constant, there is nothing to compute.
Chris Lattner16976522009-11-11 22:48:44 +0000677 if (Constant *VC = dyn_cast<Constant>(V))
Chris Lattner2c5adf82009-11-15 19:59:49 +0000678 return LVILatticeVal::get(VC);
Chris Lattner16976522009-11-11 22:48:44 +0000679
David Greene5d93a1f2009-12-23 20:43:58 +0000680 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000681 << BB->getName() << "'\n");
682
Owen Andersonf33b3022010-12-09 06:14:58 +0000683 LVILatticeVal Result = getBlockValue(V, BB);
Chris Lattner16976522009-11-11 22:48:44 +0000684
David Greene5d93a1f2009-12-23 20:43:58 +0000685 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000686 return Result;
687}
Chris Lattner16976522009-11-11 22:48:44 +0000688
Chris Lattner2c5adf82009-11-15 19:59:49 +0000689LVILatticeVal LazyValueInfoCache::
690getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
691 // If already a constant, there is nothing to compute.
692 if (Constant *VC = dyn_cast<Constant>(V))
693 return LVILatticeVal::get(VC);
694
David Greene5d93a1f2009-12-23 20:43:58 +0000695 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000696 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000697
Owen Andersonf33b3022010-12-09 06:14:58 +0000698 LVILatticeVal Result = getEdgeValue(V, FromBB, ToBB);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000699
David Greene5d93a1f2009-12-23 20:43:58 +0000700 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000701
702 return Result;
703}
704
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000705void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
706 BasicBlock *NewSucc) {
707 // When an edge in the graph has been threaded, values that we could not
708 // determine a value for before (i.e. were marked overdefined) may be possible
709 // to solve now. We do NOT try to proactively update these values. Instead,
710 // we clear their entries from the cache, and allow lazy updating to recompute
711 // them when needed.
712
713 // The updating process is fairly simple: we need to dropped cached info
714 // for all values that were marked overdefined in OldSucc, and for those same
715 // values in any successor of OldSucc (except NewSucc) in which they were
716 // also marked overdefined.
717 std::vector<BasicBlock*> worklist;
718 worklist.push_back(OldSucc);
719
Owen Anderson9a65dc92010-07-27 23:58:11 +0000720 DenseSet<Value*> ClearSet;
Owen Anderson00ac77e2010-08-18 18:39:01 +0000721 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson9a65dc92010-07-27 23:58:11 +0000722 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ++I) {
723 if (I->first == OldSucc)
724 ClearSet.insert(I->second);
725 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000726
727 // Use a worklist to perform a depth-first search of OldSucc's successors.
728 // NOTE: We do not need a visited list since any blocks we have already
729 // visited will have had their overdefined markers cleared already, and we
730 // thus won't loop to their successors.
731 while (!worklist.empty()) {
732 BasicBlock *ToUpdate = worklist.back();
733 worklist.pop_back();
734
735 // Skip blocks only accessible through NewSucc.
736 if (ToUpdate == NewSucc) continue;
737
738 bool changed = false;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000739 for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000740 I != E; ++I) {
741 // If a value was marked overdefined in OldSucc, and is here too...
Owen Anderson00ac77e2010-08-18 18:39:01 +0000742 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator OI =
Owen Anderson9a65dc92010-07-27 23:58:11 +0000743 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
744 if (OI == OverDefinedCache.end()) continue;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000745
Owen Anderson9a65dc92010-07-27 23:58:11 +0000746 // Remove it from the caches.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000747 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
Owen Anderson9a65dc92010-07-27 23:58:11 +0000748 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
749
750 assert(CI != Entry.end() && "Couldn't find entry to update?");
751 Entry.erase(CI);
752 OverDefinedCache.erase(OI);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000753
Owen Anderson9a65dc92010-07-27 23:58:11 +0000754 // If we removed anything, then we potentially need to update
755 // blocks successors too.
756 changed = true;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000757 }
758
759 if (!changed) continue;
760
761 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
762 }
763}
764
Chris Lattner2c5adf82009-11-15 19:59:49 +0000765//===----------------------------------------------------------------------===//
766// LazyValueInfo Impl
767//===----------------------------------------------------------------------===//
768
Chris Lattner2c5adf82009-11-15 19:59:49 +0000769/// getCache - This lazily constructs the LazyValueInfoCache.
770static LazyValueInfoCache &getCache(void *&PImpl) {
771 if (!PImpl)
772 PImpl = new LazyValueInfoCache();
773 return *static_cast<LazyValueInfoCache*>(PImpl);
774}
775
Owen Anderson00ac77e2010-08-18 18:39:01 +0000776bool LazyValueInfo::runOnFunction(Function &F) {
777 if (PImpl)
778 getCache(PImpl).clear();
779
780 TD = getAnalysisIfAvailable<TargetData>();
781 // Fully lazy.
782 return false;
783}
784
Chris Lattner2c5adf82009-11-15 19:59:49 +0000785void LazyValueInfo::releaseMemory() {
786 // If the cache was allocated, free it.
787 if (PImpl) {
788 delete &getCache(PImpl);
789 PImpl = 0;
790 }
791}
792
793Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
794 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
795
Chris Lattner16976522009-11-11 22:48:44 +0000796 if (Result.isConstant())
797 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000798 if (Result.isConstantRange()) {
Owen Andersonee61fcf2010-08-27 23:29:38 +0000799 ConstantRange CR = Result.getConstantRange();
800 if (const APInt *SingleVal = CR.getSingleElement())
801 return ConstantInt::get(V->getContext(), *SingleVal);
802 }
Chris Lattner16976522009-11-11 22:48:44 +0000803 return 0;
804}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000805
Chris Lattner38392bb2009-11-12 01:29:10 +0000806/// getConstantOnEdge - Determine whether the specified value is known to be a
807/// constant on the specified edge. Return null if not.
808Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
809 BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000810 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattner38392bb2009-11-12 01:29:10 +0000811
812 if (Result.isConstant())
813 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000814 if (Result.isConstantRange()) {
Owen Anderson9f014062010-08-10 20:03:09 +0000815 ConstantRange CR = Result.getConstantRange();
816 if (const APInt *SingleVal = CR.getSingleElement())
817 return ConstantInt::get(V->getContext(), *SingleVal);
818 }
Chris Lattner38392bb2009-11-12 01:29:10 +0000819 return 0;
820}
821
Chris Lattnerb52675b2009-11-12 04:36:58 +0000822/// getPredicateOnEdge - Determine whether the specified value comparison
823/// with a constant is known to be true or false on the specified CFG edge.
824/// Pred is a CmpInst predicate.
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000825LazyValueInfo::Tristate
Chris Lattnerb52675b2009-11-12 04:36:58 +0000826LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
827 BasicBlock *FromBB, BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000828 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000829
Chris Lattnerb52675b2009-11-12 04:36:58 +0000830 // If we know the value is a constant, evaluate the conditional.
831 Constant *Res = 0;
832 if (Result.isConstant()) {
833 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD);
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000834 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Chris Lattnerb52675b2009-11-12 04:36:58 +0000835 return ResCI->isZero() ? False : True;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000836 return Unknown;
837 }
838
Owen Anderson9f014062010-08-10 20:03:09 +0000839 if (Result.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000840 ConstantInt *CI = dyn_cast<ConstantInt>(C);
841 if (!CI) return Unknown;
842
Owen Anderson9f014062010-08-10 20:03:09 +0000843 ConstantRange CR = Result.getConstantRange();
844 if (Pred == ICmpInst::ICMP_EQ) {
845 if (!CR.contains(CI->getValue()))
846 return False;
847
848 if (CR.isSingleElement() && CR.contains(CI->getValue()))
849 return True;
850 } else if (Pred == ICmpInst::ICMP_NE) {
851 if (!CR.contains(CI->getValue()))
852 return True;
853
854 if (CR.isSingleElement() && CR.contains(CI->getValue()))
855 return False;
856 }
857
858 // Handle more complex predicates.
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000859 ConstantRange TrueValues =
860 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
861 if (TrueValues.contains(CR))
Owen Anderson9f014062010-08-10 20:03:09 +0000862 return True;
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000863 if (TrueValues.inverse().contains(CR))
864 return False;
Owen Anderson9f014062010-08-10 20:03:09 +0000865 return Unknown;
866 }
867
Chris Lattner2c5adf82009-11-15 19:59:49 +0000868 if (Result.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000869 // If this is an equality comparison, we can try to fold it knowing that
870 // "V != C1".
871 if (Pred == ICmpInst::ICMP_EQ) {
872 // !C1 == C -> false iff C1 == C.
873 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
874 Result.getNotConstant(), C, TD);
875 if (Res->isNullValue())
876 return False;
877 } else if (Pred == ICmpInst::ICMP_NE) {
878 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +0000879 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Chris Lattnerb52675b2009-11-12 04:36:58 +0000880 Result.getNotConstant(), C, TD);
881 if (Res->isNullValue())
882 return True;
883 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000884 return Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000885 }
886
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000887 return Unknown;
888}
889
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000890void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky69bfdf52010-12-15 18:57:18 +0000891 BasicBlock *NewSucc) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000892 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
893}
894
895void LazyValueInfo::eraseBlock(BasicBlock *BB) {
896 if (PImpl) getCache(PImpl).eraseBlock(BB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000897}