blob: 243cc26acb68ebd2665be3cade8d4ba9982c1a4b [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 {
55 /// undefined - This LLVM Value has no known value yet.
56 undefined,
Owen Anderson5be2e782010-08-05 22:59:19 +000057
Chris Lattnercc4d3b22009-11-11 02:08:33 +000058 /// constant - This LLVM Value has a specific constant value.
59 constant,
Chris Lattnerb52675b2009-11-12 04:36:58 +000060 /// notconstant - This LLVM value is known to not have the specified value.
61 notconstant,
62
Owen Anderson5be2e782010-08-05 22:59:19 +000063 /// constantrange
64 constantrange,
65
Chris Lattnercc4d3b22009-11-11 02:08:33 +000066 /// overdefined - This instruction is not known to be constant, and we know
67 /// 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;
Owen Anderson9f014062010-08-10 20:03:09 +000082 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
83 Res.markConstantRange(ConstantRange(CI->getValue(), CI->getValue()+1));
84 else if (!isa<UndefValue>(C))
85 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;
Owen Anderson9f014062010-08-10 20:03:09 +000090 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
91 Res.markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
92 else
93 Res.markNotConstant(C);
Chris Lattnerb52675b2009-11-12 04:36:58 +000094 return Res;
95 }
Owen Anderson625051b2010-08-10 23:20:01 +000096 static LVILatticeVal getRange(ConstantRange CR) {
97 LVILatticeVal Res;
98 Res.markConstantRange(CR);
99 return Res;
100 }
Chris Lattner16976522009-11-11 22:48:44 +0000101
Owen Anderson5be2e782010-08-05 22:59:19 +0000102 bool isUndefined() const { return Tag == undefined; }
103 bool isConstant() const { return Tag == constant; }
104 bool isNotConstant() const { return Tag == notconstant; }
105 bool isConstantRange() const { return Tag == constantrange; }
106 bool isOverdefined() const { return Tag == overdefined; }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000107
108 Constant *getConstant() const {
109 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000110 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000111 }
112
Chris Lattnerb52675b2009-11-12 04:36:58 +0000113 Constant *getNotConstant() const {
114 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000115 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000116 }
117
Owen Anderson5be2e782010-08-05 22:59:19 +0000118 ConstantRange getConstantRange() const {
119 assert(isConstantRange() &&
120 "Cannot get the constant-range of a non-constant-range!");
121 return Range;
122 }
123
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000124 /// markOverdefined - Return true if this is a change in status.
125 bool markOverdefined() {
126 if (isOverdefined())
127 return false;
Owen Andersondb78d732010-08-05 22:10:46 +0000128 Tag = overdefined;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000129 return true;
130 }
131
132 /// markConstant - Return true if this is a change in status.
133 bool markConstant(Constant *V) {
134 if (isConstant()) {
135 assert(getConstant() == V && "Marking constant with different value");
136 return false;
137 }
138
139 assert(isUndefined());
Owen Andersondb78d732010-08-05 22:10:46 +0000140 Tag = constant;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000141 assert(V && "Marking constant with NULL");
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) {
148 if (isNotConstant()) {
149 assert(getNotConstant() == V && "Marking !constant with different value");
150 return false;
151 }
152
153 if (isConstant())
154 assert(getConstant() != V && "Marking not constant with different value");
155 else
156 assert(isUndefined());
157
Owen Andersondb78d732010-08-05 22:10:46 +0000158 Tag = notconstant;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000159 assert(V && "Marking constant with NULL");
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
Chris Lattnerb52675b2009-11-12 04:36:58 +0000190 if (RHS.isNotConstant()) {
191 if (isNotConstant()) {
Chris Lattnerf496e792009-11-12 04:57:13 +0000192 if (getNotConstant() != RHS.getNotConstant() ||
193 isa<ConstantExpr>(getNotConstant()) ||
194 isa<ConstantExpr>(RHS.getNotConstant()))
Chris Lattnerb52675b2009-11-12 04:36:58 +0000195 return markOverdefined();
196 return false;
Owen Anderson39295322010-08-30 17:03:45 +0000197 } else if (isConstant()) {
Chris Lattnerf496e792009-11-12 04:57:13 +0000198 if (getConstant() == RHS.getNotConstant() ||
199 isa<ConstantExpr>(RHS.getNotConstant()) ||
200 isa<ConstantExpr>(getConstant()))
201 return markOverdefined();
202 return markNotConstant(RHS.getNotConstant());
Owen Anderson39295322010-08-30 17:03:45 +0000203 } else if (isConstantRange()) {
Owen Andersonc2ce21a2010-09-16 18:28:33 +0000204 // FIXME: This could be made more precise.
Owen Anderson39295322010-08-30 17:03:45 +0000205 return markOverdefined();
Chris Lattnerf496e792009-11-12 04:57:13 +0000206 }
207
208 assert(isUndefined() && "Unexpected lattice");
Chris Lattnerb52675b2009-11-12 04:36:58 +0000209 return markNotConstant(RHS.getNotConstant());
210 }
211
Owen Anderson5be2e782010-08-05 22:59:19 +0000212 if (RHS.isConstantRange()) {
213 if (isConstantRange()) {
Owen Anderson9f014062010-08-10 20:03:09 +0000214 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
215 if (NewR.isFullSet())
Owen Anderson5be2e782010-08-05 22:59:19 +0000216 return markOverdefined();
217 else
218 return markConstantRange(NewR);
Owen Anderson59b06dc2010-08-24 07:55:44 +0000219 } else if (!isUndefined()) {
220 return markOverdefined();
Owen Anderson5be2e782010-08-05 22:59:19 +0000221 }
222
223 assert(isUndefined() && "Unexpected lattice");
224 return markConstantRange(RHS.getConstantRange());
225 }
226
Owen Andersonc2ce21a2010-09-16 18:28:33 +0000227 // RHS must be a constant, we must be constantrange,
228 // undef, constant, or notconstant.
229 if (isConstantRange()) {
230 // FIXME: This could be made more precise.
231 return markOverdefined();
232 }
Owen Anderson5be2e782010-08-05 22:59:19 +0000233
Chris Lattnerf496e792009-11-12 04:57:13 +0000234 if (isUndefined())
235 return markConstant(RHS.getConstant());
236
237 if (isConstant()) {
238 if (getConstant() != RHS.getConstant())
239 return markOverdefined();
240 return false;
241 }
242
243 // If we are known "!=4" and RHS is "==5", stay at "!=4".
244 if (getNotConstant() == RHS.getConstant() ||
245 isa<ConstantExpr>(getNotConstant()) ||
246 isa<ConstantExpr>(RHS.getConstant()))
Chris Lattner16976522009-11-11 22:48:44 +0000247 return markOverdefined();
Chris Lattnerf496e792009-11-12 04:57:13 +0000248 return false;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000249 }
250
251};
252
253} // end anonymous namespace.
254
Chris Lattner16976522009-11-11 22:48:44 +0000255namespace llvm {
256raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
257 if (Val.isUndefined())
258 return OS << "undefined";
259 if (Val.isOverdefined())
260 return OS << "overdefined";
Chris Lattnerb52675b2009-11-12 04:36:58 +0000261
262 if (Val.isNotConstant())
263 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson2f3ffb82010-08-09 20:50:46 +0000264 else if (Val.isConstantRange())
265 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
266 << Val.getConstantRange().getUpper() << '>';
Chris Lattner16976522009-11-11 22:48:44 +0000267 return OS << "constant<" << *Val.getConstant() << '>';
268}
269}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000270
271//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000272// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000273//===----------------------------------------------------------------------===//
274
Chris Lattner2c5adf82009-11-15 19:59:49 +0000275namespace {
276 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
277 /// maintains information about queries across the clients' queries.
278 class LazyValueInfoCache {
Owen Anderson81881bc2010-07-30 20:56:07 +0000279 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000280 /// ValueCacheEntryTy - This is all of the cached block information for
281 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
282 /// entries, allowing us to do a lookup with a binary search.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000283 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000284
Owen Anderson81881bc2010-07-30 20:56:07 +0000285 private:
Owen Anderson7f9cb742010-07-30 23:59:40 +0000286 /// LVIValueHandle - A callback value handle update the cache when
287 /// values are erased.
288 struct LVIValueHandle : public CallbackVH {
289 LazyValueInfoCache *Parent;
290
291 LVIValueHandle(Value *V, LazyValueInfoCache *P)
292 : CallbackVH(V), Parent(P) { }
293
294 void deleted();
295 void allUsesReplacedWith(Value* V) {
296 deleted();
297 }
Owen Anderson7f9cb742010-07-30 23:59:40 +0000298 };
299
Chris Lattner2c5adf82009-11-15 19:59:49 +0000300 /// ValueCache - This is all of the cached information for all values,
301 /// mapped from Value* to key information.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000302 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000303
304 /// OverDefinedCache - This tracks, on a per-block basis, the set of
305 /// values that are over-defined at the end of that block. This is required
306 /// for cache updating.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000307 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > OverDefinedCache;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000308
Owen Andersonf33b3022010-12-09 06:14:58 +0000309 LVILatticeVal &getCachedEntryForBlock(Value *Val, BasicBlock *BB);
310 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
311 LVILatticeVal getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T);
312
313 ValueCacheEntryTy &lookup(Value *V) {
314 return ValueCache[LVIValueHandle(V, this)];
315 }
316
317 LVILatticeVal setBlockValue(Value *V, BasicBlock *BB, LVILatticeVal L,
318 ValueCacheEntryTy &Cache) {
319 if (L.isOverdefined()) OverDefinedCache.insert(std::make_pair(BB, V));
320 return Cache[BB] = L;
321 }
322
Chris Lattner2c5adf82009-11-15 19:59:49 +0000323 public:
324
325 /// getValueInBlock - This is the query interface to determine the lattice
326 /// value for the specified Value* at the end of the specified block.
327 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
328
329 /// getValueOnEdge - This is the query interface to determine the lattice
330 /// value for the specified Value* that is true on the specified edge.
331 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000332
333 /// threadEdge - This is the update interface to inform the cache that an
334 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
335 /// NewSucc.
336 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000337
338 /// eraseBlock - This is part of the update interface to inform the cache
339 /// that a block has been deleted.
340 void eraseBlock(BasicBlock *BB);
341
342 /// clear - Empty the cache.
343 void clear() {
344 ValueCache.clear();
345 OverDefinedCache.clear();
346 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000347 };
348} // end anonymous namespace
349
Owen Anderson7f9cb742010-07-30 23:59:40 +0000350void LazyValueInfoCache::LVIValueHandle::deleted() {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000351 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson7f9cb742010-07-30 23:59:40 +0000352 I = Parent->OverDefinedCache.begin(),
353 E = Parent->OverDefinedCache.end();
354 I != E; ) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000355 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000356 ++I;
357 if (tmp->second == getValPtr())
358 Parent->OverDefinedCache.erase(tmp);
359 }
Owen Andersoncf6abd22010-08-11 22:36:04 +0000360
361 // This erasure deallocates *this, so it MUST happen after we're done
362 // using any and all members of *this.
363 Parent->ValueCache.erase(*this);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000364}
365
Owen Anderson00ac77e2010-08-18 18:39:01 +0000366void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
367 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
368 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ) {
369 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
370 ++I;
371 if (tmp->first == BB)
372 OverDefinedCache.erase(tmp);
373 }
374
375 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
376 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
377 I->second.erase(BB);
378}
Owen Anderson7f9cb742010-07-30 23:59:40 +0000379
Owen Andersonf33b3022010-12-09 06:14:58 +0000380LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
381 ValueCacheEntryTy &Cache = lookup(Val);
382 LVILatticeVal &BBLV = Cache[BB];
Chris Lattner2c5adf82009-11-15 19:59:49 +0000383
384 // If we've already computed this block's value, return it.
Chris Lattnere5642812009-11-15 20:00:52 +0000385 if (!BBLV.isUndefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000386 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
Chris Lattner2c5adf82009-11-15 19:59:49 +0000387 return BBLV;
Chris Lattnere5642812009-11-15 20:00:52 +0000388 }
389
Chris Lattner2c5adf82009-11-15 19:59:49 +0000390 // Otherwise, this is the first time we're seeing this block. Reset the
391 // lattice value to overdefined, so that cycles will terminate and be
392 // conservatively correct.
393 BBLV.markOverdefined();
394
Chris Lattner2c5adf82009-11-15 19:59:49 +0000395 Instruction *BBI = dyn_cast<Instruction>(Val);
396 if (BBI == 0 || BBI->getParent() != BB) {
397 LVILatticeVal Result; // Start Undefined.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000398
Owen Andersonc8ef7502010-08-24 20:47:29 +0000399 // If this is a pointer, and there's a load from that pointer in this BB,
400 // then we know that the pointer can't be NULL.
Owen Anderson1593dd62010-09-03 19:08:37 +0000401 bool NotNull = false;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000402 if (Val->getType()->isPointerTy()) {
Owen Anderson6cd20752010-08-25 01:16:47 +0000403 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){
404 LoadInst *L = dyn_cast<LoadInst>(BI);
405 if (L && L->getPointerAddressSpace() == 0 &&
406 L->getPointerOperand()->getUnderlyingObject() ==
407 Val->getUnderlyingObject()) {
Owen Anderson1593dd62010-09-03 19:08:37 +0000408 NotNull = true;
409 break;
Owen Andersonc8ef7502010-08-24 20:47:29 +0000410 }
411 }
412 }
413
414 unsigned NumPreds = 0;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000415 // Loop over all of our predecessors, merging what we know from them into
416 // result.
417 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Owen Andersonf33b3022010-12-09 06:14:58 +0000418 Result.mergeIn(getEdgeValue(Val, *PI, BB));
Chris Lattner2c5adf82009-11-15 19:59:49 +0000419
420 // If we hit overdefined, exit early. The BlockVals entry is already set
421 // to overdefined.
Chris Lattnere5642812009-11-15 20:00:52 +0000422 if (Result.isOverdefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000423 DEBUG(dbgs() << " compute BB '" << BB->getName()
Chris Lattnere5642812009-11-15 20:00:52 +0000424 << "' - overdefined because of pred.\n");
Owen Anderson1593dd62010-09-03 19:08:37 +0000425 // If we previously determined that this is a pointer that can't be null
426 // then return that rather than giving up entirely.
427 if (NotNull) {
428 const PointerType *PTy = cast<PointerType>(Val->getType());
429 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
430 }
431
Owen Andersonf33b3022010-12-09 06:14:58 +0000432 return setBlockValue(Val, BB, Result, Cache);
Chris Lattnere5642812009-11-15 20:00:52 +0000433 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000434 ++NumPreds;
435 }
436
Owen Anderson1593dd62010-09-03 19:08:37 +0000437
Chris Lattner2c5adf82009-11-15 19:59:49 +0000438 // If this is the entry block, we must be asking about an argument. The
439 // value is overdefined.
440 if (NumPreds == 0 && BB == &BB->getParent()->front()) {
441 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
442 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000443 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000444 }
445
446 // Return the merged value, which is more precise than 'overdefined'.
447 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000448 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000449 }
450
451 // If this value is defined by an instruction in this block, we have to
452 // process it here somehow or return overdefined.
453 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000454 LVILatticeVal Result; // Start Undefined.
455
456 // Loop over all of our predecessors, merging what we know from them into
457 // result.
458 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
459 Value* PhiVal = PN->getIncomingValueForBlock(*PI);
Owen Andersonf33b3022010-12-09 06:14:58 +0000460 Result.mergeIn(getValueOnEdge(PhiVal, *PI, BB));
Owen Anderson7f9cb742010-07-30 23:59:40 +0000461
462 // If we hit overdefined, exit early. The BlockVals entry is already set
463 // to overdefined.
464 if (Result.isOverdefined()) {
465 DEBUG(dbgs() << " compute BB '" << BB->getName()
466 << "' - overdefined because of pred.\n");
Owen Andersonf33b3022010-12-09 06:14:58 +0000467 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000468 }
469 }
470
471 // Return the merged value, which is more precise than 'overdefined'.
472 assert(!Result.isOverdefined());
Owen Andersonf33b3022010-12-09 06:14:58 +0000473 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000474 }
Chris Lattnere5642812009-11-15 20:00:52 +0000475
Owen Andersonf33b3022010-12-09 06:14:58 +0000476 assert(Cache[BB].isOverdefined() &&
477 "Recursive query changed our cache?");
Owen Andersonb81fd622010-08-18 21:11:37 +0000478
479 // We can only analyze the definitions of certain classes of instructions
480 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000481 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000482 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
483 !BBI->getType()->isIntegerTy()) {
484 DEBUG(dbgs() << " compute BB '" << BB->getName()
485 << "' - overdefined because inst def found.\n");
486 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000487 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000488 }
489
490 // FIXME: We're currently limited to binops with a constant RHS. This should
491 // be improved.
492 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
493 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
494 DEBUG(dbgs() << " compute BB '" << BB->getName()
495 << "' - overdefined because inst def found.\n");
496
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 // Figure out the range of the LHS. If that fails, bail.
Owen Andersonf33b3022010-12-09 06:14:58 +0000502 LVILatticeVal LHSVal = getValueInBlock(BBI->getOperand(0), BB);
Owen Andersonb81fd622010-08-18 21:11:37 +0000503 if (!LHSVal.isConstantRange()) {
504 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000505 return setBlockValue(Val, BB, Result, Cache);
Owen Andersonb81fd622010-08-18 21:11:37 +0000506 }
507
508 ConstantInt *RHS = 0;
509 ConstantRange LHSRange = LHSVal.getConstantRange();
510 ConstantRange RHSRange(1);
511 const IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
512 if (isa<BinaryOperator>(BBI)) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000513 RHS = dyn_cast<ConstantInt>(BBI->getOperand(1));
514 if (!RHS) {
515 Result.markOverdefined();
Owen Andersonf33b3022010-12-09 06:14:58 +0000516 return setBlockValue(Val, BB, Result, Cache);
Owen Anderson59b06dc2010-08-24 07:55:44 +0000517 }
518
Owen Andersonb81fd622010-08-18 21:11:37 +0000519 RHSRange = ConstantRange(RHS->getValue(), RHS->getValue()+1);
520 }
521
522 // NOTE: We're currently limited by the set of operations that ConstantRange
523 // can evaluate symbolically. Enhancing that set will allows us to analyze
524 // more definitions.
525 switch (BBI->getOpcode()) {
526 case Instruction::Add:
527 Result.markConstantRange(LHSRange.add(RHSRange));
528 break;
529 case Instruction::Sub:
530 Result.markConstantRange(LHSRange.sub(RHSRange));
531 break;
532 case Instruction::Mul:
533 Result.markConstantRange(LHSRange.multiply(RHSRange));
534 break;
535 case Instruction::UDiv:
536 Result.markConstantRange(LHSRange.udiv(RHSRange));
537 break;
538 case Instruction::Shl:
539 Result.markConstantRange(LHSRange.shl(RHSRange));
540 break;
541 case Instruction::LShr:
542 Result.markConstantRange(LHSRange.lshr(RHSRange));
543 break;
544 case Instruction::Trunc:
545 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
546 break;
547 case Instruction::SExt:
548 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
549 break;
550 case Instruction::ZExt:
551 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
552 break;
553 case Instruction::BitCast:
554 Result.markConstantRange(LHSRange);
555 break;
Nick Lewycky198381e2010-09-07 05:39:02 +0000556 case Instruction::And:
557 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
558 break;
559 case Instruction::Or:
560 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
561 break;
Owen Andersonb81fd622010-08-18 21:11:37 +0000562
563 // Unhandled instructions are overdefined.
564 default:
565 DEBUG(dbgs() << " compute BB '" << BB->getName()
566 << "' - overdefined because inst def found.\n");
567 Result.markOverdefined();
568 break;
569 }
570
Owen Andersonf33b3022010-12-09 06:14:58 +0000571 return setBlockValue(Val, BB, Result, Cache);
Chris Lattner10f2d132009-11-11 00:22:30 +0000572}
573
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000574
Chris Lattner800c47e2009-11-15 20:02:12 +0000575/// getEdgeValue - This method attempts to infer more complex
Owen Andersonf33b3022010-12-09 06:14:58 +0000576LVILatticeVal LazyValueInfoCache::getEdgeValue(Value *Val,
577 BasicBlock *BBFrom,
578 BasicBlock *BBTo) {
Chris Lattner800c47e2009-11-15 20:02:12 +0000579 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
580 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +0000581 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
582 // If this is a conditional branch and only one successor goes to BBTo, then
583 // we maybe able to infer something from the condition.
584 if (BI->isConditional() &&
585 BI->getSuccessor(0) != BI->getSuccessor(1)) {
586 bool isTrueDest = BI->getSuccessor(0) == BBTo;
587 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
588 "BBTo isn't a successor of BBFrom");
589
590 // If V is the condition of the branch itself, then we know exactly what
591 // it is.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000592 if (BI->getCondition() == Val)
Chris Lattner16976522009-11-11 22:48:44 +0000593 return LVILatticeVal::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +0000594 Type::getInt1Ty(Val->getContext()), isTrueDest));
Chris Lattner16976522009-11-11 22:48:44 +0000595
596 // If the condition of the branch is an equality comparison, we may be
597 // able to infer the value.
Owen Anderson2d0f2472010-08-11 04:24:25 +0000598 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
599 if (ICI && ICI->getOperand(0) == Val &&
600 isa<Constant>(ICI->getOperand(1))) {
601 if (ICI->isEquality()) {
602 // We know that V has the RHS constant if this is a true SETEQ or
603 // false SETNE.
604 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
605 return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
606 return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
Chris Lattner16976522009-11-11 22:48:44 +0000607 }
Owen Anderson2d0f2472010-08-11 04:24:25 +0000608
609 if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
610 // Calculate the range of values that would satisfy the comparison.
611 ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
612 ConstantRange TrueValues =
613 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
614
615 // If we're interested in the false dest, invert the condition.
616 if (!isTrueDest) TrueValues = TrueValues.inverse();
617
618 // Figure out the possible values of the query BEFORE this branch.
Owen Andersonf33b3022010-12-09 06:14:58 +0000619 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Owen Anderson660cab32010-08-27 17:12:29 +0000620 if (!InBlock.isConstantRange())
621 return LVILatticeVal::getRange(TrueValues);
Owen Anderson2d0f2472010-08-11 04:24:25 +0000622
623 // Find all potential values that satisfy both the input and output
624 // conditions.
625 ConstantRange PossibleValues =
626 TrueValues.intersectWith(InBlock.getConstantRange());
627
628 return LVILatticeVal::getRange(PossibleValues);
629 }
630 }
Chris Lattner16976522009-11-11 22:48:44 +0000631 }
632 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000633
634 // If the edge was formed by a switch on the value, then we may know exactly
635 // what it is.
636 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Owen Andersondae90c62010-08-24 21:59:42 +0000637 if (SI->getCondition() == Val) {
Owen Anderson4caef602010-09-02 22:16:52 +0000638 // We don't know anything in the default case.
Owen Andersondae90c62010-08-24 21:59:42 +0000639 if (SI->getDefaultDest() == BBTo) {
Owen Andersondae90c62010-08-24 21:59:42 +0000640 LVILatticeVal Result;
Owen Anderson4caef602010-09-02 22:16:52 +0000641 Result.markOverdefined();
Owen Andersondae90c62010-08-24 21:59:42 +0000642 return Result;
643 }
644
Chris Lattner800c47e2009-11-15 20:02:12 +0000645 // We only know something if there is exactly one value that goes from
646 // BBFrom to BBTo.
647 unsigned NumEdges = 0;
648 ConstantInt *EdgeVal = 0;
649 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
650 if (SI->getSuccessor(i) != BBTo) continue;
651 if (NumEdges++) break;
652 EdgeVal = SI->getCaseValue(i);
653 }
654 assert(EdgeVal && "Missing successor?");
655 if (NumEdges == 1)
656 return LVILatticeVal::get(EdgeVal);
657 }
658 }
Chris Lattner16976522009-11-11 22:48:44 +0000659
660 // Otherwise see if the value is known in the block.
Owen Andersonf33b3022010-12-09 06:14:58 +0000661 return getBlockValue(Val, BBFrom);
Chris Lattner16976522009-11-11 22:48:44 +0000662}
663
Chris Lattner2c5adf82009-11-15 19:59:49 +0000664LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
665 // If already a constant, there is nothing to compute.
Chris Lattner16976522009-11-11 22:48:44 +0000666 if (Constant *VC = dyn_cast<Constant>(V))
Chris Lattner2c5adf82009-11-15 19:59:49 +0000667 return LVILatticeVal::get(VC);
Chris Lattner16976522009-11-11 22:48:44 +0000668
David Greene5d93a1f2009-12-23 20:43:58 +0000669 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000670 << BB->getName() << "'\n");
671
Owen Andersonf33b3022010-12-09 06:14:58 +0000672 LVILatticeVal Result = getBlockValue(V, BB);
Chris Lattner16976522009-11-11 22:48:44 +0000673
David Greene5d93a1f2009-12-23 20:43:58 +0000674 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000675 return Result;
676}
Chris Lattner16976522009-11-11 22:48:44 +0000677
Chris Lattner2c5adf82009-11-15 19:59:49 +0000678LVILatticeVal LazyValueInfoCache::
679getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
680 // If already a constant, there is nothing to compute.
681 if (Constant *VC = dyn_cast<Constant>(V))
682 return LVILatticeVal::get(VC);
683
David Greene5d93a1f2009-12-23 20:43:58 +0000684 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000685 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000686
Owen Andersonf33b3022010-12-09 06:14:58 +0000687 LVILatticeVal Result = getEdgeValue(V, FromBB, ToBB);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000688
David Greene5d93a1f2009-12-23 20:43:58 +0000689 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000690
691 return Result;
692}
693
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000694void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
695 BasicBlock *NewSucc) {
696 // When an edge in the graph has been threaded, values that we could not
697 // determine a value for before (i.e. were marked overdefined) may be possible
698 // to solve now. We do NOT try to proactively update these values. Instead,
699 // we clear their entries from the cache, and allow lazy updating to recompute
700 // them when needed.
701
702 // The updating process is fairly simple: we need to dropped cached info
703 // for all values that were marked overdefined in OldSucc, and for those same
704 // values in any successor of OldSucc (except NewSucc) in which they were
705 // also marked overdefined.
706 std::vector<BasicBlock*> worklist;
707 worklist.push_back(OldSucc);
708
Owen Anderson9a65dc92010-07-27 23:58:11 +0000709 DenseSet<Value*> ClearSet;
Owen Anderson00ac77e2010-08-18 18:39:01 +0000710 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson9a65dc92010-07-27 23:58:11 +0000711 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ++I) {
712 if (I->first == OldSucc)
713 ClearSet.insert(I->second);
714 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000715
716 // Use a worklist to perform a depth-first search of OldSucc's successors.
717 // NOTE: We do not need a visited list since any blocks we have already
718 // visited will have had their overdefined markers cleared already, and we
719 // thus won't loop to their successors.
720 while (!worklist.empty()) {
721 BasicBlock *ToUpdate = worklist.back();
722 worklist.pop_back();
723
724 // Skip blocks only accessible through NewSucc.
725 if (ToUpdate == NewSucc) continue;
726
727 bool changed = false;
Owen Anderson9a65dc92010-07-27 23:58:11 +0000728 for (DenseSet<Value*>::iterator I = ClearSet.begin(),E = ClearSet.end();
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000729 I != E; ++I) {
730 // If a value was marked overdefined in OldSucc, and is here too...
Owen Anderson00ac77e2010-08-18 18:39:01 +0000731 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator OI =
Owen Anderson9a65dc92010-07-27 23:58:11 +0000732 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
733 if (OI == OverDefinedCache.end()) continue;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000734
Owen Anderson9a65dc92010-07-27 23:58:11 +0000735 // Remove it from the caches.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000736 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
Owen Anderson9a65dc92010-07-27 23:58:11 +0000737 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
738
739 assert(CI != Entry.end() && "Couldn't find entry to update?");
740 Entry.erase(CI);
741 OverDefinedCache.erase(OI);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000742
Owen Anderson9a65dc92010-07-27 23:58:11 +0000743 // If we removed anything, then we potentially need to update
744 // blocks successors too.
745 changed = true;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000746 }
747
748 if (!changed) continue;
749
750 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
751 }
752}
753
Chris Lattner2c5adf82009-11-15 19:59:49 +0000754//===----------------------------------------------------------------------===//
755// LazyValueInfo Impl
756//===----------------------------------------------------------------------===//
757
Chris Lattner2c5adf82009-11-15 19:59:49 +0000758/// getCache - This lazily constructs the LazyValueInfoCache.
759static LazyValueInfoCache &getCache(void *&PImpl) {
760 if (!PImpl)
761 PImpl = new LazyValueInfoCache();
762 return *static_cast<LazyValueInfoCache*>(PImpl);
763}
764
Owen Anderson00ac77e2010-08-18 18:39:01 +0000765bool LazyValueInfo::runOnFunction(Function &F) {
766 if (PImpl)
767 getCache(PImpl).clear();
768
769 TD = getAnalysisIfAvailable<TargetData>();
770 // Fully lazy.
771 return false;
772}
773
Chris Lattner2c5adf82009-11-15 19:59:49 +0000774void LazyValueInfo::releaseMemory() {
775 // If the cache was allocated, free it.
776 if (PImpl) {
777 delete &getCache(PImpl);
778 PImpl = 0;
779 }
780}
781
782Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
783 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
784
Chris Lattner16976522009-11-11 22:48:44 +0000785 if (Result.isConstant())
786 return Result.getConstant();
Owen Andersonee61fcf2010-08-27 23:29:38 +0000787 else if (Result.isConstantRange()) {
788 ConstantRange CR = Result.getConstantRange();
789 if (const APInt *SingleVal = CR.getSingleElement())
790 return ConstantInt::get(V->getContext(), *SingleVal);
791 }
Chris Lattner16976522009-11-11 22:48:44 +0000792 return 0;
793}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000794
Chris Lattner38392bb2009-11-12 01:29:10 +0000795/// getConstantOnEdge - Determine whether the specified value is known to be a
796/// constant on the specified edge. Return null if not.
797Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
798 BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000799 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattner38392bb2009-11-12 01:29:10 +0000800
801 if (Result.isConstant())
802 return Result.getConstant();
Owen Anderson9f014062010-08-10 20:03:09 +0000803 else if (Result.isConstantRange()) {
804 ConstantRange CR = Result.getConstantRange();
805 if (const APInt *SingleVal = CR.getSingleElement())
806 return ConstantInt::get(V->getContext(), *SingleVal);
807 }
Chris Lattner38392bb2009-11-12 01:29:10 +0000808 return 0;
809}
810
Chris Lattnerb52675b2009-11-12 04:36:58 +0000811/// getPredicateOnEdge - Determine whether the specified value comparison
812/// with a constant is known to be true or false on the specified CFG edge.
813/// Pred is a CmpInst predicate.
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000814LazyValueInfo::Tristate
Chris Lattnerb52675b2009-11-12 04:36:58 +0000815LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
816 BasicBlock *FromBB, BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000817 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000818
Chris Lattnerb52675b2009-11-12 04:36:58 +0000819 // If we know the value is a constant, evaluate the conditional.
820 Constant *Res = 0;
821 if (Result.isConstant()) {
822 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD);
823 if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res))
824 return ResCI->isZero() ? False : True;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000825 return Unknown;
826 }
827
Owen Anderson9f014062010-08-10 20:03:09 +0000828 if (Result.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000829 ConstantInt *CI = dyn_cast<ConstantInt>(C);
830 if (!CI) return Unknown;
831
Owen Anderson9f014062010-08-10 20:03:09 +0000832 ConstantRange CR = Result.getConstantRange();
833 if (Pred == ICmpInst::ICMP_EQ) {
834 if (!CR.contains(CI->getValue()))
835 return False;
836
837 if (CR.isSingleElement() && CR.contains(CI->getValue()))
838 return True;
839 } else if (Pred == ICmpInst::ICMP_NE) {
840 if (!CR.contains(CI->getValue()))
841 return True;
842
843 if (CR.isSingleElement() && CR.contains(CI->getValue()))
844 return False;
845 }
846
847 // Handle more complex predicates.
848 ConstantRange RHS(CI->getValue(), CI->getValue()+1);
849 ConstantRange TrueValues = ConstantRange::makeICmpRegion(Pred, RHS);
850 if (CR.intersectWith(TrueValues).isEmptySet())
851 return False;
Owen Anderson625051b2010-08-10 23:20:01 +0000852 else if (TrueValues.contains(CR))
Owen Anderson9f014062010-08-10 20:03:09 +0000853 return True;
854
855 return Unknown;
856 }
857
Chris Lattner2c5adf82009-11-15 19:59:49 +0000858 if (Result.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000859 // If this is an equality comparison, we can try to fold it knowing that
860 // "V != C1".
861 if (Pred == ICmpInst::ICMP_EQ) {
862 // !C1 == C -> false iff C1 == C.
863 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
864 Result.getNotConstant(), C, TD);
865 if (Res->isNullValue())
866 return False;
867 } else if (Pred == ICmpInst::ICMP_NE) {
868 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +0000869 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Chris Lattnerb52675b2009-11-12 04:36:58 +0000870 Result.getNotConstant(), C, TD);
871 if (Res->isNullValue())
872 return True;
873 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000874 return Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000875 }
876
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000877 return Unknown;
878}
879
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000880void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
881 BasicBlock* NewSucc) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000882 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
883}
884
885void LazyValueInfo::eraseBlock(BasicBlock *BB) {
886 if (PImpl) getCache(PImpl).eraseBlock(BB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000887}