blob: e8cc69de551f66e417a516f51e5ebe1fb8c81da2 [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"
Chris Lattner10f2d132009-11-11 00:22:30 +000029using namespace llvm;
30
31char LazyValueInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000032INITIALIZE_PASS(LazyValueInfo, "lazy-value-info",
33 "Lazy Value Information Analysis", false, true);
Chris Lattner10f2d132009-11-11 00:22:30 +000034
35namespace llvm {
36 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
37}
38
Chris Lattnercc4d3b22009-11-11 02:08:33 +000039
40//===----------------------------------------------------------------------===//
41// LVILatticeVal
42//===----------------------------------------------------------------------===//
43
44/// LVILatticeVal - This is the information tracked by LazyValueInfo for each
45/// value.
46///
47/// FIXME: This is basically just for bringup, this can be made a lot more rich
48/// in the future.
49///
50namespace {
51class LVILatticeVal {
52 enum LatticeValueTy {
53 /// undefined - This LLVM Value has no known value yet.
54 undefined,
Owen Anderson5be2e782010-08-05 22:59:19 +000055
Chris Lattnercc4d3b22009-11-11 02:08:33 +000056 /// constant - This LLVM Value has a specific constant value.
57 constant,
Chris Lattnerb52675b2009-11-12 04:36:58 +000058 /// notconstant - This LLVM value is known to not have the specified value.
59 notconstant,
60
Owen Anderson5be2e782010-08-05 22:59:19 +000061 /// constantrange
62 constantrange,
63
Chris Lattnercc4d3b22009-11-11 02:08:33 +000064 /// overdefined - This instruction is not known to be constant, and we know
65 /// it has a value.
66 overdefined
67 };
68
69 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattnerb52675b2009-11-12 04:36:58 +000070 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersondb78d732010-08-05 22:10:46 +000071 LatticeValueTy Tag;
72 Constant *Val;
Owen Anderson5be2e782010-08-05 22:59:19 +000073 ConstantRange Range;
Chris Lattnercc4d3b22009-11-11 02:08:33 +000074
75public:
Owen Anderson5be2e782010-08-05 22:59:19 +000076 LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {}
Chris Lattnercc4d3b22009-11-11 02:08:33 +000077
Chris Lattner16976522009-11-11 22:48:44 +000078 static LVILatticeVal get(Constant *C) {
79 LVILatticeVal Res;
Owen Anderson9f014062010-08-10 20:03:09 +000080 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
81 Res.markConstantRange(ConstantRange(CI->getValue(), CI->getValue()+1));
82 else if (!isa<UndefValue>(C))
83 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;
Owen Anderson9f014062010-08-10 20:03:09 +000088 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
89 Res.markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
90 else
91 Res.markNotConstant(C);
Chris Lattnerb52675b2009-11-12 04:36:58 +000092 return Res;
93 }
Owen Anderson625051b2010-08-10 23:20:01 +000094 static LVILatticeVal getRange(ConstantRange CR) {
95 LVILatticeVal Res;
96 Res.markConstantRange(CR);
97 return Res;
98 }
Chris Lattner16976522009-11-11 22:48:44 +000099
Owen Anderson5be2e782010-08-05 22:59:19 +0000100 bool isUndefined() const { return Tag == undefined; }
101 bool isConstant() const { return Tag == constant; }
102 bool isNotConstant() const { return Tag == notconstant; }
103 bool isConstantRange() const { return Tag == constantrange; }
104 bool isOverdefined() const { return Tag == overdefined; }
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000105
106 Constant *getConstant() const {
107 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000108 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000109 }
110
Chris Lattnerb52675b2009-11-12 04:36:58 +0000111 Constant *getNotConstant() const {
112 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersondb78d732010-08-05 22:10:46 +0000113 return Val;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000114 }
115
Owen Anderson5be2e782010-08-05 22:59:19 +0000116 ConstantRange getConstantRange() const {
117 assert(isConstantRange() &&
118 "Cannot get the constant-range of a non-constant-range!");
119 return Range;
120 }
121
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000122 /// markOverdefined - Return true if this is a change in status.
123 bool markOverdefined() {
124 if (isOverdefined())
125 return false;
Owen Andersondb78d732010-08-05 22:10:46 +0000126 Tag = overdefined;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000127 return true;
128 }
129
130 /// markConstant - Return true if this is a change in status.
131 bool markConstant(Constant *V) {
132 if (isConstant()) {
133 assert(getConstant() == V && "Marking constant with different value");
134 return false;
135 }
136
137 assert(isUndefined());
Owen Andersondb78d732010-08-05 22:10:46 +0000138 Tag = constant;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000139 assert(V && "Marking constant with NULL");
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) {
146 if (isNotConstant()) {
147 assert(getNotConstant() == V && "Marking !constant with different value");
148 return false;
149 }
150
151 if (isConstant())
152 assert(getConstant() != V && "Marking not constant with different value");
153 else
154 assert(isUndefined());
155
Owen Andersondb78d732010-08-05 22:10:46 +0000156 Tag = notconstant;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000157 assert(V && "Marking constant with NULL");
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();
176 else if (NewR.isFullSet()) {
177 Tag = undefined;
178 return true;
179 }
180
181 Tag = constantrange;
182 Range = NewR;
183 return true;
184 }
185
Chris Lattner16976522009-11-11 22:48:44 +0000186 /// mergeIn - Merge the specified lattice value into this one, updating this
187 /// one and returning true if anything changed.
188 bool mergeIn(const LVILatticeVal &RHS) {
189 if (RHS.isUndefined() || isOverdefined()) return false;
190 if (RHS.isOverdefined()) return markOverdefined();
191
Chris Lattnerb52675b2009-11-12 04:36:58 +0000192 if (RHS.isNotConstant()) {
193 if (isNotConstant()) {
Chris Lattnerf496e792009-11-12 04:57:13 +0000194 if (getNotConstant() != RHS.getNotConstant() ||
195 isa<ConstantExpr>(getNotConstant()) ||
196 isa<ConstantExpr>(RHS.getNotConstant()))
Chris Lattnerb52675b2009-11-12 04:36:58 +0000197 return markOverdefined();
198 return false;
199 }
Chris Lattnerf496e792009-11-12 04:57:13 +0000200 if (isConstant()) {
201 if (getConstant() == RHS.getNotConstant() ||
202 isa<ConstantExpr>(RHS.getNotConstant()) ||
203 isa<ConstantExpr>(getConstant()))
204 return markOverdefined();
205 return markNotConstant(RHS.getNotConstant());
206 }
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
Chris Lattnerf496e792009-11-12 04:57:13 +0000227 // RHS must be a constant, we must be undef, constant, or notconstant.
Owen Anderson5be2e782010-08-05 22:59:19 +0000228 assert(!isConstantRange() &&
229 "Constant and ConstantRange cannot be merged.");
230
Chris Lattnerf496e792009-11-12 04:57:13 +0000231 if (isUndefined())
232 return markConstant(RHS.getConstant());
233
234 if (isConstant()) {
235 if (getConstant() != RHS.getConstant())
236 return markOverdefined();
237 return false;
238 }
239
240 // If we are known "!=4" and RHS is "==5", stay at "!=4".
241 if (getNotConstant() == RHS.getConstant() ||
242 isa<ConstantExpr>(getNotConstant()) ||
243 isa<ConstantExpr>(RHS.getConstant()))
Chris Lattner16976522009-11-11 22:48:44 +0000244 return markOverdefined();
Chris Lattnerf496e792009-11-12 04:57:13 +0000245 return false;
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000246 }
247
248};
249
250} // end anonymous namespace.
251
Chris Lattner16976522009-11-11 22:48:44 +0000252namespace llvm {
253raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
254 if (Val.isUndefined())
255 return OS << "undefined";
256 if (Val.isOverdefined())
257 return OS << "overdefined";
Chris Lattnerb52675b2009-11-12 04:36:58 +0000258
259 if (Val.isNotConstant())
260 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson2f3ffb82010-08-09 20:50:46 +0000261 else if (Val.isConstantRange())
262 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
263 << Val.getConstantRange().getUpper() << '>';
Chris Lattner16976522009-11-11 22:48:44 +0000264 return OS << "constant<" << *Val.getConstant() << '>';
265}
266}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000267
268//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000269// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000270//===----------------------------------------------------------------------===//
271
Chris Lattner2c5adf82009-11-15 19:59:49 +0000272namespace {
273 /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which
274 /// maintains information about queries across the clients' queries.
275 class LazyValueInfoCache {
Owen Anderson81881bc2010-07-30 20:56:07 +0000276 public:
Chris Lattner2c5adf82009-11-15 19:59:49 +0000277 /// BlockCacheEntryTy - This is a computed lattice value at the end of the
278 /// specified basic block for a Value* that depends on context.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000279 typedef std::pair<AssertingVH<BasicBlock>, LVILatticeVal> BlockCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000280
281 /// ValueCacheEntryTy - This is all of the cached block information for
282 /// exactly one Value*. The entries are sorted by the BasicBlock* of the
283 /// entries, allowing us to do a lookup with a binary search.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000284 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000285
Owen Anderson81881bc2010-07-30 20:56:07 +0000286 private:
Owen Anderson7f9cb742010-07-30 23:59:40 +0000287 /// LVIValueHandle - A callback value handle update the cache when
288 /// values are erased.
289 struct LVIValueHandle : public CallbackVH {
290 LazyValueInfoCache *Parent;
291
292 LVIValueHandle(Value *V, LazyValueInfoCache *P)
293 : CallbackVH(V), Parent(P) { }
294
295 void deleted();
296 void allUsesReplacedWith(Value* V) {
297 deleted();
298 }
299
300 LVIValueHandle &operator=(Value *V) {
301 return *this = LVIValueHandle(V, Parent);
302 }
303 };
304
Chris Lattner2c5adf82009-11-15 19:59:49 +0000305 /// ValueCache - This is all of the cached information for all values,
306 /// mapped from Value* to key information.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000307 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000308
309 /// OverDefinedCache - This tracks, on a per-block basis, the set of
310 /// values that are over-defined at the end of that block. This is required
311 /// for cache updating.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000312 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > OverDefinedCache;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000313
Chris Lattner2c5adf82009-11-15 19:59:49 +0000314 public:
315
316 /// getValueInBlock - This is the query interface to determine the lattice
317 /// value for the specified Value* at the end of the specified block.
318 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
319
320 /// getValueOnEdge - This is the query interface to determine the lattice
321 /// value for the specified Value* that is true on the specified edge.
322 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000323
324 /// threadEdge - This is the update interface to inform the cache that an
325 /// edge from PredBB to OldSucc has been threaded to be from PredBB to
326 /// NewSucc.
327 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Owen Anderson00ac77e2010-08-18 18:39:01 +0000328
329 /// eraseBlock - This is part of the update interface to inform the cache
330 /// that a block has been deleted.
331 void eraseBlock(BasicBlock *BB);
332
333 /// clear - Empty the cache.
334 void clear() {
335 ValueCache.clear();
336 OverDefinedCache.clear();
337 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000338 };
339} // end anonymous namespace
340
Owen Anderson81881bc2010-07-30 20:56:07 +0000341//===----------------------------------------------------------------------===//
342// LVIQuery Impl
343//===----------------------------------------------------------------------===//
344
345namespace {
346 /// LVIQuery - This is a transient object that exists while a query is
347 /// being performed.
348 ///
349 /// TODO: Reuse LVIQuery instead of recreating it for every query, this avoids
350 /// reallocation of the densemap on every query.
351 class LVIQuery {
352 typedef LazyValueInfoCache::BlockCacheEntryTy BlockCacheEntryTy;
353 typedef LazyValueInfoCache::ValueCacheEntryTy ValueCacheEntryTy;
354
355 /// This is the current value being queried for.
356 Value *Val;
357
Owen Anderson7f9cb742010-07-30 23:59:40 +0000358 /// This is a pointer to the owning cache, for recursive queries.
359 LazyValueInfoCache &Parent;
360
Owen Anderson81881bc2010-07-30 20:56:07 +0000361 /// This is all of the cached information about this value.
362 ValueCacheEntryTy &Cache;
363
364 /// This tracks, for each block, what values are overdefined.
Owen Anderson00ac77e2010-08-18 18:39:01 +0000365 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > &OverDefinedCache;
Owen Anderson81881bc2010-07-30 20:56:07 +0000366
367 /// NewBlocks - This is a mapping of the new BasicBlocks which have been
368 /// added to cache but that are not in sorted order.
369 DenseSet<BasicBlock*> NewBlockInfo;
370 public:
371
Owen Anderson7f9cb742010-07-30 23:59:40 +0000372 LVIQuery(Value *V, LazyValueInfoCache &P,
373 ValueCacheEntryTy &VC,
Owen Anderson00ac77e2010-08-18 18:39:01 +0000374 std::set<std::pair<AssertingVH<BasicBlock>, Value*> > &ODC)
Owen Anderson7f9cb742010-07-30 23:59:40 +0000375 : Val(V), Parent(P), Cache(VC), OverDefinedCache(ODC) {
Owen Anderson81881bc2010-07-30 20:56:07 +0000376 }
377
378 ~LVIQuery() {
379 // When the query is done, insert the newly discovered facts into the
380 // cache in sorted order.
381 if (NewBlockInfo.empty()) return;
382
383 for (DenseSet<BasicBlock*>::iterator I = NewBlockInfo.begin(),
384 E = NewBlockInfo.end(); I != E; ++I) {
385 if (Cache[*I].isOverdefined())
386 OverDefinedCache.insert(std::make_pair(*I, Val));
387 }
388 }
389
390 LVILatticeVal getBlockValue(BasicBlock *BB);
391 LVILatticeVal getEdgeValue(BasicBlock *FromBB, BasicBlock *ToBB);
392
393 private:
Owen Anderson4bb3eaf2010-08-16 23:42:33 +0000394 LVILatticeVal getCachedEntryForBlock(BasicBlock *BB);
Owen Anderson81881bc2010-07-30 20:56:07 +0000395 };
396} // end anonymous namespace
Chris Lattner2c5adf82009-11-15 19:59:49 +0000397
Owen Anderson7f9cb742010-07-30 23:59:40 +0000398void LazyValueInfoCache::LVIValueHandle::deleted() {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000399 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson7f9cb742010-07-30 23:59:40 +0000400 I = Parent->OverDefinedCache.begin(),
401 E = Parent->OverDefinedCache.end();
402 I != E; ) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000403 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
Owen Anderson7f9cb742010-07-30 23:59:40 +0000404 ++I;
405 if (tmp->second == getValPtr())
406 Parent->OverDefinedCache.erase(tmp);
407 }
Owen Andersoncf6abd22010-08-11 22:36:04 +0000408
409 // This erasure deallocates *this, so it MUST happen after we're done
410 // using any and all members of *this.
411 Parent->ValueCache.erase(*this);
Owen Anderson7f9cb742010-07-30 23:59:40 +0000412}
413
Owen Anderson00ac77e2010-08-18 18:39:01 +0000414void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
415 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
416 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ) {
417 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I;
418 ++I;
419 if (tmp->first == BB)
420 OverDefinedCache.erase(tmp);
421 }
422
423 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
424 I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
425 I->second.erase(BB);
426}
Owen Anderson7f9cb742010-07-30 23:59:40 +0000427
Chris Lattnere5642812009-11-15 20:00:52 +0000428/// getCachedEntryForBlock - See if we already have a value for this block. If
Owen Anderson9a65dc92010-07-27 23:58:11 +0000429/// so, return it, otherwise create a new entry in the Cache map to use.
Owen Anderson4bb3eaf2010-08-16 23:42:33 +0000430LVILatticeVal LVIQuery::getCachedEntryForBlock(BasicBlock *BB) {
Owen Anderson81881bc2010-07-30 20:56:07 +0000431 NewBlockInfo.insert(BB);
Owen Anderson9a65dc92010-07-27 23:58:11 +0000432 return Cache[BB];
Chris Lattnere5642812009-11-15 20:00:52 +0000433}
Chris Lattner2c5adf82009-11-15 19:59:49 +0000434
Owen Anderson81881bc2010-07-30 20:56:07 +0000435LVILatticeVal LVIQuery::getBlockValue(BasicBlock *BB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000436 // See if we already have a value for this block.
Owen Anderson4bb3eaf2010-08-16 23:42:33 +0000437 LVILatticeVal BBLV = getCachedEntryForBlock(BB);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000438
439 // If we've already computed this block's value, return it.
Chris Lattnere5642812009-11-15 20:00:52 +0000440 if (!BBLV.isUndefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000441 DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
Chris Lattner2c5adf82009-11-15 19:59:49 +0000442 return BBLV;
Chris Lattnere5642812009-11-15 20:00:52 +0000443 }
444
Chris Lattner2c5adf82009-11-15 19:59:49 +0000445 // Otherwise, this is the first time we're seeing this block. Reset the
446 // lattice value to overdefined, so that cycles will terminate and be
447 // conservatively correct.
448 BBLV.markOverdefined();
Owen Anderson4bb3eaf2010-08-16 23:42:33 +0000449 Cache[BB] = BBLV;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000450
451 // If V is live into BB, see if our predecessors know anything about it.
452 Instruction *BBI = dyn_cast<Instruction>(Val);
453 if (BBI == 0 || BBI->getParent() != BB) {
454 LVILatticeVal Result; // Start Undefined.
455 unsigned NumPreds = 0;
456
457 // Loop over all of our predecessors, merging what we know from them into
458 // result.
459 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Owen Anderson81881bc2010-07-30 20:56:07 +0000460 Result.mergeIn(getEdgeValue(*PI, BB));
Chris Lattner2c5adf82009-11-15 19:59:49 +0000461
462 // If we hit overdefined, exit early. The BlockVals entry is already set
463 // to overdefined.
Chris Lattnere5642812009-11-15 20:00:52 +0000464 if (Result.isOverdefined()) {
David Greene5d93a1f2009-12-23 20:43:58 +0000465 DEBUG(dbgs() << " compute BB '" << BB->getName()
Chris Lattnere5642812009-11-15 20:00:52 +0000466 << "' - overdefined because of pred.\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000467 return Result;
Chris Lattnere5642812009-11-15 20:00:52 +0000468 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000469 ++NumPreds;
470 }
471
472 // If this is the entry block, we must be asking about an argument. The
473 // value is overdefined.
474 if (NumPreds == 0 && BB == &BB->getParent()->front()) {
475 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
476 Result.markOverdefined();
477 return Result;
478 }
479
480 // Return the merged value, which is more precise than 'overdefined'.
481 assert(!Result.isOverdefined());
Owen Anderson4bb3eaf2010-08-16 23:42:33 +0000482 return Cache[BB] = Result;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000483 }
484
485 // If this value is defined by an instruction in this block, we have to
486 // process it here somehow or return overdefined.
487 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Owen Anderson7f9cb742010-07-30 23:59:40 +0000488 LVILatticeVal Result; // Start Undefined.
489
490 // Loop over all of our predecessors, merging what we know from them into
491 // result.
492 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
493 Value* PhiVal = PN->getIncomingValueForBlock(*PI);
494 Result.mergeIn(Parent.getValueOnEdge(PhiVal, *PI, BB));
495
496 // If we hit overdefined, exit early. The BlockVals entry is already set
497 // to overdefined.
498 if (Result.isOverdefined()) {
499 DEBUG(dbgs() << " compute BB '" << BB->getName()
500 << "' - overdefined because of pred.\n");
501 return Result;
502 }
503 }
504
505 // Return the merged value, which is more precise than 'overdefined'.
506 assert(!Result.isOverdefined());
Owen Anderson00ac77e2010-08-18 18:39:01 +0000507 return Cache[BB] = Result;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000508 }
Chris Lattnere5642812009-11-15 20:00:52 +0000509
Owen Andersonb81fd622010-08-18 21:11:37 +0000510 assert(Cache[BB].isOverdefined() && "Recursive query changed our cache?");
511
512 // We can only analyze the definitions of certain classes of instructions
513 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000514 LVILatticeVal Result;
Owen Andersonb81fd622010-08-18 21:11:37 +0000515 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
516 !BBI->getType()->isIntegerTy()) {
517 DEBUG(dbgs() << " compute BB '" << BB->getName()
518 << "' - overdefined because inst def found.\n");
519 Result.markOverdefined();
520 return Result;
521 }
522
523 // FIXME: We're currently limited to binops with a constant RHS. This should
524 // be improved.
525 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
526 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
527 DEBUG(dbgs() << " compute BB '" << BB->getName()
528 << "' - overdefined because inst def found.\n");
529
530 Result.markOverdefined();
531 return Result;
532 }
533
534 // Figure out the range of the LHS. If that fails, bail.
535 LVILatticeVal LHSVal = Parent.getValueInBlock(BBI->getOperand(0), BB);
536 if (!LHSVal.isConstantRange()) {
537 Result.markOverdefined();
538 return Result;
539 }
540
541 ConstantInt *RHS = 0;
542 ConstantRange LHSRange = LHSVal.getConstantRange();
543 ConstantRange RHSRange(1);
544 const IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
545 if (isa<BinaryOperator>(BBI)) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000546 RHS = dyn_cast<ConstantInt>(BBI->getOperand(1));
547 if (!RHS) {
548 Result.markOverdefined();
549 return Result;
550 }
551
Owen Andersonb81fd622010-08-18 21:11:37 +0000552 RHSRange = ConstantRange(RHS->getValue(), RHS->getValue()+1);
553 }
554
555 // NOTE: We're currently limited by the set of operations that ConstantRange
556 // can evaluate symbolically. Enhancing that set will allows us to analyze
557 // more definitions.
558 switch (BBI->getOpcode()) {
559 case Instruction::Add:
560 Result.markConstantRange(LHSRange.add(RHSRange));
561 break;
562 case Instruction::Sub:
563 Result.markConstantRange(LHSRange.sub(RHSRange));
564 break;
565 case Instruction::Mul:
566 Result.markConstantRange(LHSRange.multiply(RHSRange));
567 break;
568 case Instruction::UDiv:
569 Result.markConstantRange(LHSRange.udiv(RHSRange));
570 break;
571 case Instruction::Shl:
572 Result.markConstantRange(LHSRange.shl(RHSRange));
573 break;
574 case Instruction::LShr:
575 Result.markConstantRange(LHSRange.lshr(RHSRange));
576 break;
577 case Instruction::Trunc:
578 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
579 break;
580 case Instruction::SExt:
581 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
582 break;
583 case Instruction::ZExt:
584 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
585 break;
586 case Instruction::BitCast:
587 Result.markConstantRange(LHSRange);
588 break;
589
590 // Unhandled instructions are overdefined.
591 default:
592 DEBUG(dbgs() << " compute BB '" << BB->getName()
593 << "' - overdefined because inst def found.\n");
594 Result.markOverdefined();
595 break;
596 }
597
598 return Cache[BB] = Result;
Chris Lattner10f2d132009-11-11 00:22:30 +0000599}
600
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000601
Chris Lattner800c47e2009-11-15 20:02:12 +0000602/// getEdgeValue - This method attempts to infer more complex
Owen Anderson81881bc2010-07-30 20:56:07 +0000603LVILatticeVal LVIQuery::getEdgeValue(BasicBlock *BBFrom, BasicBlock *BBTo) {
Chris Lattner800c47e2009-11-15 20:02:12 +0000604 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
605 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +0000606 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
607 // If this is a conditional branch and only one successor goes to BBTo, then
608 // we maybe able to infer something from the condition.
609 if (BI->isConditional() &&
610 BI->getSuccessor(0) != BI->getSuccessor(1)) {
611 bool isTrueDest = BI->getSuccessor(0) == BBTo;
612 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
613 "BBTo isn't a successor of BBFrom");
614
615 // If V is the condition of the branch itself, then we know exactly what
616 // it is.
Chris Lattner2c5adf82009-11-15 19:59:49 +0000617 if (BI->getCondition() == Val)
Chris Lattner16976522009-11-11 22:48:44 +0000618 return LVILatticeVal::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +0000619 Type::getInt1Ty(Val->getContext()), isTrueDest));
Chris Lattner16976522009-11-11 22:48:44 +0000620
621 // If the condition of the branch is an equality comparison, we may be
622 // able to infer the value.
Owen Anderson2d0f2472010-08-11 04:24:25 +0000623 ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
624 if (ICI && ICI->getOperand(0) == Val &&
625 isa<Constant>(ICI->getOperand(1))) {
626 if (ICI->isEquality()) {
627 // We know that V has the RHS constant if this is a true SETEQ or
628 // false SETNE.
629 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
630 return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
631 return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
Chris Lattner16976522009-11-11 22:48:44 +0000632 }
Owen Anderson2d0f2472010-08-11 04:24:25 +0000633
634 if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
635 // Calculate the range of values that would satisfy the comparison.
636 ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
637 ConstantRange TrueValues =
638 ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
639
640 // If we're interested in the false dest, invert the condition.
641 if (!isTrueDest) TrueValues = TrueValues.inverse();
642
643 // Figure out the possible values of the query BEFORE this branch.
644 LVILatticeVal InBlock = getBlockValue(BBFrom);
645 if (!InBlock.isConstantRange()) return InBlock;
646
647 // Find all potential values that satisfy both the input and output
648 // conditions.
649 ConstantRange PossibleValues =
650 TrueValues.intersectWith(InBlock.getConstantRange());
651
652 return LVILatticeVal::getRange(PossibleValues);
653 }
654 }
Chris Lattner16976522009-11-11 22:48:44 +0000655 }
656 }
Chris Lattner800c47e2009-11-15 20:02:12 +0000657
658 // If the edge was formed by a switch on the value, then we may know exactly
659 // what it is.
660 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
661 // If BBTo is the default destination of the switch, we don't know anything.
662 // Given a more powerful range analysis we could know stuff.
663 if (SI->getCondition() == Val && SI->getDefaultDest() != BBTo) {
664 // We only know something if there is exactly one value that goes from
665 // BBFrom to BBTo.
666 unsigned NumEdges = 0;
667 ConstantInt *EdgeVal = 0;
668 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
669 if (SI->getSuccessor(i) != BBTo) continue;
670 if (NumEdges++) break;
671 EdgeVal = SI->getCaseValue(i);
672 }
673 assert(EdgeVal && "Missing successor?");
674 if (NumEdges == 1)
675 return LVILatticeVal::get(EdgeVal);
676 }
677 }
Chris Lattner16976522009-11-11 22:48:44 +0000678
679 // Otherwise see if the value is known in the block.
Owen Anderson81881bc2010-07-30 20:56:07 +0000680 return getBlockValue(BBFrom);
Chris Lattner16976522009-11-11 22:48:44 +0000681}
682
Owen Anderson81881bc2010-07-30 20:56:07 +0000683
684//===----------------------------------------------------------------------===//
685// LazyValueInfoCache Impl
686//===----------------------------------------------------------------------===//
687
Chris Lattner2c5adf82009-11-15 19:59:49 +0000688LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
689 // If already a constant, there is nothing to compute.
Chris Lattner16976522009-11-11 22:48:44 +0000690 if (Constant *VC = dyn_cast<Constant>(V))
Chris Lattner2c5adf82009-11-15 19:59:49 +0000691 return LVILatticeVal::get(VC);
Chris Lattner16976522009-11-11 22:48:44 +0000692
David Greene5d93a1f2009-12-23 20:43:58 +0000693 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000694 << BB->getName() << "'\n");
695
Owen Anderson7f9cb742010-07-30 23:59:40 +0000696 LVILatticeVal Result = LVIQuery(V, *this,
697 ValueCache[LVIValueHandle(V, this)],
Owen Anderson81881bc2010-07-30 20:56:07 +0000698 OverDefinedCache).getBlockValue(BB);
Chris Lattner16976522009-11-11 22:48:44 +0000699
David Greene5d93a1f2009-12-23 20:43:58 +0000700 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000701 return Result;
702}
Chris Lattner16976522009-11-11 22:48:44 +0000703
Chris Lattner2c5adf82009-11-15 19:59:49 +0000704LVILatticeVal LazyValueInfoCache::
705getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
706 // If already a constant, there is nothing to compute.
707 if (Constant *VC = dyn_cast<Constant>(V))
708 return LVILatticeVal::get(VC);
709
David Greene5d93a1f2009-12-23 20:43:58 +0000710 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattner2c5adf82009-11-15 19:59:49 +0000711 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000712
Owen Anderson81881bc2010-07-30 20:56:07 +0000713 LVILatticeVal Result =
Owen Anderson7f9cb742010-07-30 23:59:40 +0000714 LVIQuery(V, *this, ValueCache[LVIValueHandle(V, this)],
Owen Anderson81881bc2010-07-30 20:56:07 +0000715 OverDefinedCache).getEdgeValue(FromBB, ToBB);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000716
David Greene5d93a1f2009-12-23 20:43:58 +0000717 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +0000718
719 return Result;
720}
721
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000722void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
723 BasicBlock *NewSucc) {
724 // When an edge in the graph has been threaded, values that we could not
725 // determine a value for before (i.e. were marked overdefined) may be possible
726 // to solve now. We do NOT try to proactively update these values. Instead,
727 // we clear their entries from the cache, and allow lazy updating to recompute
728 // them when needed.
729
730 // The updating process is fairly simple: we need to dropped cached info
731 // for all values that were marked overdefined in OldSucc, and for those same
732 // values in any successor of OldSucc (except NewSucc) in which they were
733 // also marked overdefined.
734 std::vector<BasicBlock*> worklist;
735 worklist.push_back(OldSucc);
736
Owen Anderson9a65dc92010-07-27 23:58:11 +0000737 DenseSet<Value*> ClearSet;
Owen Anderson00ac77e2010-08-18 18:39:01 +0000738 for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator
Owen Anderson9a65dc92010-07-27 23:58:11 +0000739 I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ++I) {
740 if (I->first == OldSucc)
741 ClearSet.insert(I->second);
742 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000743
744 // Use a worklist to perform a depth-first search of OldSucc's successors.
745 // NOTE: We do not need a visited list since any blocks we have already
746 // visited will have had their overdefined markers cleared already, and we
747 // thus won't loop to their successors.
748 while (!worklist.empty()) {
749 BasicBlock *ToUpdate = worklist.back();
750 worklist.pop_back();
751
752 // Skip blocks only accessible through NewSucc.
753 if (ToUpdate == NewSucc) continue;
754
755 bool changed = false;
Owen Anderson9a65dc92010-07-27 23:58:11 +0000756 for (DenseSet<Value*>::iterator I = ClearSet.begin(),E = ClearSet.end();
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000757 I != E; ++I) {
758 // If a value was marked overdefined in OldSucc, and is here too...
Owen Anderson00ac77e2010-08-18 18:39:01 +0000759 std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator OI =
Owen Anderson9a65dc92010-07-27 23:58:11 +0000760 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
761 if (OI == OverDefinedCache.end()) continue;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000762
Owen Anderson9a65dc92010-07-27 23:58:11 +0000763 // Remove it from the caches.
Owen Anderson7f9cb742010-07-30 23:59:40 +0000764 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
Owen Anderson9a65dc92010-07-27 23:58:11 +0000765 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
766
767 assert(CI != Entry.end() && "Couldn't find entry to update?");
768 Entry.erase(CI);
769 OverDefinedCache.erase(OI);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000770
Owen Anderson9a65dc92010-07-27 23:58:11 +0000771 // If we removed anything, then we potentially need to update
772 // blocks successors too.
773 changed = true;
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000774 }
775
776 if (!changed) continue;
777
778 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
779 }
780}
781
Chris Lattner2c5adf82009-11-15 19:59:49 +0000782//===----------------------------------------------------------------------===//
783// LazyValueInfo Impl
784//===----------------------------------------------------------------------===//
785
Chris Lattner2c5adf82009-11-15 19:59:49 +0000786/// getCache - This lazily constructs the LazyValueInfoCache.
787static LazyValueInfoCache &getCache(void *&PImpl) {
788 if (!PImpl)
789 PImpl = new LazyValueInfoCache();
790 return *static_cast<LazyValueInfoCache*>(PImpl);
791}
792
Owen Anderson00ac77e2010-08-18 18:39:01 +0000793bool LazyValueInfo::runOnFunction(Function &F) {
794 if (PImpl)
795 getCache(PImpl).clear();
796
797 TD = getAnalysisIfAvailable<TargetData>();
798 // Fully lazy.
799 return false;
800}
801
Chris Lattner2c5adf82009-11-15 19:59:49 +0000802void LazyValueInfo::releaseMemory() {
803 // If the cache was allocated, free it.
804 if (PImpl) {
805 delete &getCache(PImpl);
806 PImpl = 0;
807 }
808}
809
810Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
811 LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
812
Chris Lattner16976522009-11-11 22:48:44 +0000813 if (Result.isConstant())
814 return Result.getConstant();
815 return 0;
816}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000817
Chris Lattner38392bb2009-11-12 01:29:10 +0000818/// getConstantOnEdge - Determine whether the specified value is known to be a
819/// constant on the specified edge. Return null if not.
820Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
821 BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000822 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattner38392bb2009-11-12 01:29:10 +0000823
824 if (Result.isConstant())
825 return Result.getConstant();
Owen Anderson9f014062010-08-10 20:03:09 +0000826 else if (Result.isConstantRange()) {
827 ConstantRange CR = Result.getConstantRange();
828 if (const APInt *SingleVal = CR.getSingleElement())
829 return ConstantInt::get(V->getContext(), *SingleVal);
830 }
Chris Lattner38392bb2009-11-12 01:29:10 +0000831 return 0;
832}
833
Chris Lattnerb52675b2009-11-12 04:36:58 +0000834/// getPredicateOnEdge - Determine whether the specified value comparison
835/// with a constant is known to be true or false on the specified CFG edge.
836/// Pred is a CmpInst predicate.
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000837LazyValueInfo::Tristate
Chris Lattnerb52675b2009-11-12 04:36:58 +0000838LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
839 BasicBlock *FromBB, BasicBlock *ToBB) {
Chris Lattner2c5adf82009-11-15 19:59:49 +0000840 LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000841
Chris Lattnerb52675b2009-11-12 04:36:58 +0000842 // If we know the value is a constant, evaluate the conditional.
843 Constant *Res = 0;
844 if (Result.isConstant()) {
845 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD);
846 if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res))
847 return ResCI->isZero() ? False : True;
Chris Lattner2c5adf82009-11-15 19:59:49 +0000848 return Unknown;
849 }
850
Owen Anderson9f014062010-08-10 20:03:09 +0000851 if (Result.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +0000852 ConstantInt *CI = dyn_cast<ConstantInt>(C);
853 if (!CI) return Unknown;
854
Owen Anderson9f014062010-08-10 20:03:09 +0000855 ConstantRange CR = Result.getConstantRange();
856 if (Pred == ICmpInst::ICMP_EQ) {
857 if (!CR.contains(CI->getValue()))
858 return False;
859
860 if (CR.isSingleElement() && CR.contains(CI->getValue()))
861 return True;
862 } else if (Pred == ICmpInst::ICMP_NE) {
863 if (!CR.contains(CI->getValue()))
864 return True;
865
866 if (CR.isSingleElement() && CR.contains(CI->getValue()))
867 return False;
868 }
869
870 // Handle more complex predicates.
871 ConstantRange RHS(CI->getValue(), CI->getValue()+1);
872 ConstantRange TrueValues = ConstantRange::makeICmpRegion(Pred, RHS);
873 if (CR.intersectWith(TrueValues).isEmptySet())
874 return False;
Owen Anderson625051b2010-08-10 23:20:01 +0000875 else if (TrueValues.contains(CR))
Owen Anderson9f014062010-08-10 20:03:09 +0000876 return True;
877
878 return Unknown;
879 }
880
Chris Lattner2c5adf82009-11-15 19:59:49 +0000881 if (Result.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +0000882 // If this is an equality comparison, we can try to fold it knowing that
883 // "V != C1".
884 if (Pred == ICmpInst::ICMP_EQ) {
885 // !C1 == C -> false iff C1 == C.
886 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
887 Result.getNotConstant(), C, TD);
888 if (Res->isNullValue())
889 return False;
890 } else if (Pred == ICmpInst::ICMP_NE) {
891 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +0000892 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Chris Lattnerb52675b2009-11-12 04:36:58 +0000893 Result.getNotConstant(), C, TD);
894 if (Res->isNullValue())
895 return True;
896 }
Chris Lattner2c5adf82009-11-15 19:59:49 +0000897 return Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +0000898 }
899
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000900 return Unknown;
901}
902
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000903void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
904 BasicBlock* NewSucc) {
Owen Anderson00ac77e2010-08-18 18:39:01 +0000905 if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
906}
907
908void LazyValueInfo::eraseBlock(BasicBlock *BB) {
909 if (PImpl) getCache(PImpl).eraseBlock(BB);
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000910}