blob: 1d50e98c0fddbc00d2fd6f3cbfc8c57d2ce83357 [file] [log] [blame]
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner741c94c2009-11-11 00:22:30 +00002//
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
15#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000022#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000023#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
Hal Finkel7e184492014-09-07 20:29:59 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
Philip Reameseb3e9da2015-10-29 03:57:17 +000029#include "llvm/IR/LLVMContext.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000030#include "llvm/IR/PatternMatch.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000031#include "llvm/IR/ValueHandle.h"
Chris Lattnerb584d1e2009-11-12 01:22:16 +000032#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/raw_ostream.h"
Bill Wendling4ec081a2012-01-11 23:43:34 +000034#include <map>
Nick Lewycky55a700b2010-12-18 01:00:40 +000035#include <stack>
Chris Lattner741c94c2009-11-11 00:22:30 +000036using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000037using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000038
Chandler Carruthf1221bd2014-04-22 02:48:03 +000039#define DEBUG_TYPE "lazy-value-info"
40
Chris Lattner741c94c2009-11-11 00:22:30 +000041char LazyValueInfo::ID = 0;
Chad Rosier43a33062011-12-02 01:26:24 +000042INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info",
43 "Lazy Value Information Analysis", false, true)
Chandler Carruth66b31302015-01-04 12:03:27 +000044INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000045INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosier43a33062011-12-02 01:26:24 +000046INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000047 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000048
49namespace llvm {
50 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
51}
52
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000053
54//===----------------------------------------------------------------------===//
55// LVILatticeVal
56//===----------------------------------------------------------------------===//
57
Sanjay Patel2a385e22015-01-09 16:47:20 +000058/// This is the information tracked by LazyValueInfo for each value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000059///
60/// FIXME: This is basically just for bringup, this can be made a lot more rich
61/// in the future.
62///
63namespace {
64class LVILatticeVal {
65 enum LatticeValueTy {
Sanjay Patel2a385e22015-01-09 16:47:20 +000066 /// This Value has no known value yet.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000067 undefined,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000068
Sanjay Patel2a385e22015-01-09 16:47:20 +000069 /// This Value has a specific constant value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000070 constant,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000071
Sanjay Patel2a385e22015-01-09 16:47:20 +000072 /// This Value is known to not have the specified value.
Chris Lattner565ee2f2009-11-12 04:36:58 +000073 notconstant,
Chad Rosier43a33062011-12-02 01:26:24 +000074
Sanjay Patel2a385e22015-01-09 16:47:20 +000075 /// The Value falls within this range.
Owen Anderson0f306a42010-08-05 22:59:19 +000076 constantrange,
Chad Rosier43a33062011-12-02 01:26:24 +000077
Sanjay Patel2a385e22015-01-09 16:47:20 +000078 /// This value is not known to be constant, and we know that it has a value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000079 overdefined
80 };
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000081
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000082 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattner565ee2f2009-11-12 04:36:58 +000083 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersonc3a14132010-08-05 22:10:46 +000084 LatticeValueTy Tag;
85 Constant *Val;
Owen Anderson0f306a42010-08-05 22:59:19 +000086 ConstantRange Range;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000087
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000088public:
Craig Topper9f008862014-04-15 04:59:12 +000089 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000090
Chris Lattner19019ea2009-11-11 22:48:44 +000091 static LVILatticeVal get(Constant *C) {
92 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +000093 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +000094 Res.markConstant(C);
Chris Lattner19019ea2009-11-11 22:48:44 +000095 return Res;
96 }
Chris Lattner565ee2f2009-11-12 04:36:58 +000097 static LVILatticeVal getNot(Constant *C) {
98 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +000099 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000100 Res.markNotConstant(C);
Chris Lattner565ee2f2009-11-12 04:36:58 +0000101 return Res;
102 }
Owen Anderson5f1dd092010-08-10 23:20:01 +0000103 static LVILatticeVal getRange(ConstantRange CR) {
104 LVILatticeVal Res;
105 Res.markConstantRange(CR);
106 return Res;
107 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000108
Owen Anderson0f306a42010-08-05 22:59:19 +0000109 bool isUndefined() const { return Tag == undefined; }
110 bool isConstant() const { return Tag == constant; }
111 bool isNotConstant() const { return Tag == notconstant; }
112 bool isConstantRange() const { return Tag == constantrange; }
113 bool isOverdefined() const { return Tag == overdefined; }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000114
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000115 Constant *getConstant() const {
116 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000117 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000118 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000119
Chris Lattner565ee2f2009-11-12 04:36:58 +0000120 Constant *getNotConstant() const {
121 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000122 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000123 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000124
Owen Anderson0f306a42010-08-05 22:59:19 +0000125 ConstantRange getConstantRange() const {
126 assert(isConstantRange() &&
127 "Cannot get the constant-range of a non-constant-range!");
128 return Range;
129 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000130
Sanjay Patel2a385e22015-01-09 16:47:20 +0000131 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000132 bool markOverdefined() {
133 if (isOverdefined())
134 return false;
Owen Andersonc3a14132010-08-05 22:10:46 +0000135 Tag = overdefined;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000136 return true;
137 }
138
Sanjay Patel2a385e22015-01-09 16:47:20 +0000139 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000140 bool markConstant(Constant *V) {
Nick Lewycky11678bd2010-12-15 18:57:18 +0000141 assert(V && "Marking constant with NULL");
142 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
143 return markConstantRange(ConstantRange(CI->getValue()));
144 if (isa<UndefValue>(V))
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000145 return false;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000146
147 assert((!isConstant() || getConstant() == V) &&
148 "Marking constant with different value");
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000149 assert(isUndefined());
Owen Andersonc3a14132010-08-05 22:10:46 +0000150 Tag = constant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000151 Val = V;
Chris Lattner19019ea2009-11-11 22:48:44 +0000152 return true;
153 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000154
Sanjay Patel2a385e22015-01-09 16:47:20 +0000155 /// Return true if this is a change in status.
Chris Lattner565ee2f2009-11-12 04:36:58 +0000156 bool markNotConstant(Constant *V) {
Chris Lattner565ee2f2009-11-12 04:36:58 +0000157 assert(V && "Marking constant with NULL");
Nick Lewycky11678bd2010-12-15 18:57:18 +0000158 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
159 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
160 if (isa<UndefValue>(V))
161 return false;
162
163 assert((!isConstant() || getConstant() != V) &&
164 "Marking constant !constant with same value");
165 assert((!isNotConstant() || getNotConstant() == V) &&
166 "Marking !constant with different value");
167 assert(isUndefined() || isConstant());
168 Tag = notconstant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000169 Val = V;
Chris Lattner565ee2f2009-11-12 04:36:58 +0000170 return true;
171 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000172
Sanjay Patel2a385e22015-01-09 16:47:20 +0000173 /// Return true if this is a change in status.
Owen Anderson0f306a42010-08-05 22:59:19 +0000174 bool markConstantRange(const ConstantRange NewR) {
175 if (isConstantRange()) {
176 if (NewR.isEmptySet())
177 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000178
Nuno Lopese6e04902012-06-28 01:16:18 +0000179 bool changed = Range != NewR;
Owen Anderson0f306a42010-08-05 22:59:19 +0000180 Range = NewR;
181 return changed;
182 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000183
Owen Anderson0f306a42010-08-05 22:59:19 +0000184 assert(isUndefined());
185 if (NewR.isEmptySet())
186 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000187
Owen Anderson0f306a42010-08-05 22:59:19 +0000188 Tag = constantrange;
189 Range = NewR;
190 return true;
191 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000192
Sanjay Patel2a385e22015-01-09 16:47:20 +0000193 /// Merge the specified lattice value into this one, updating this
Chris Lattner19019ea2009-11-11 22:48:44 +0000194 /// one and returning true if anything changed.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000195 bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
Chris Lattner19019ea2009-11-11 22:48:44 +0000196 if (RHS.isUndefined() || isOverdefined()) return false;
197 if (RHS.isOverdefined()) return markOverdefined();
198
Nick Lewycky11678bd2010-12-15 18:57:18 +0000199 if (isUndefined()) {
200 Tag = RHS.Tag;
201 Val = RHS.Val;
202 Range = RHS.Range;
203 return true;
Chris Lattner22db4b52009-11-12 04:57:13 +0000204 }
205
Nick Lewycky11678bd2010-12-15 18:57:18 +0000206 if (isConstant()) {
207 if (RHS.isConstant()) {
208 if (Val == RHS.Val)
209 return false;
210 return markOverdefined();
211 }
212
213 if (RHS.isNotConstant()) {
214 if (Val == RHS.Val)
215 return markOverdefined();
216
217 // Unless we can prove that the two Constants are different, we must
218 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000219 if (ConstantInt *Res =
220 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
221 CmpInst::ICMP_NE, getConstant(), RHS.getNotConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000222 if (Res->isOne())
223 return markNotConstant(RHS.getNotConstant());
224
225 return markOverdefined();
226 }
227
228 // RHS is a ConstantRange, LHS is a non-integer Constant.
229
230 // FIXME: consider the case where RHS is a range [1, 0) and LHS is
231 // a function. The correct result is to pick up RHS.
232
Chris Lattner19019ea2009-11-11 22:48:44 +0000233 return markOverdefined();
Nick Lewycky11678bd2010-12-15 18:57:18 +0000234 }
235
236 if (isNotConstant()) {
237 if (RHS.isConstant()) {
238 if (Val == RHS.Val)
239 return markOverdefined();
240
241 // Unless we can prove that the two Constants are different, we must
242 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000243 if (ConstantInt *Res =
244 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
245 CmpInst::ICMP_NE, getNotConstant(), RHS.getConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000246 if (Res->isOne())
247 return false;
248
249 return markOverdefined();
250 }
251
252 if (RHS.isNotConstant()) {
253 if (Val == RHS.Val)
254 return false;
255 return markOverdefined();
256 }
257
258 return markOverdefined();
259 }
260
261 assert(isConstantRange() && "New LVILattice type?");
262 if (!RHS.isConstantRange())
263 return markOverdefined();
264
265 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
266 if (NewR.isFullSet())
267 return markOverdefined();
268 return markConstantRange(NewR);
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000269 }
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000270};
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000271
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000272} // end anonymous namespace.
273
Chris Lattner19019ea2009-11-11 22:48:44 +0000274namespace llvm {
Chandler Carruth2b1ba482011-04-18 18:49:44 +0000275raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
276 LLVM_ATTRIBUTE_USED;
Chris Lattner19019ea2009-11-11 22:48:44 +0000277raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
278 if (Val.isUndefined())
279 return OS << "undefined";
280 if (Val.isOverdefined())
281 return OS << "overdefined";
Chris Lattner565ee2f2009-11-12 04:36:58 +0000282
283 if (Val.isNotConstant())
284 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Owen Anderson8afac042010-08-09 20:50:46 +0000285 else if (Val.isConstantRange())
286 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
287 << Val.getConstantRange().getUpper() << '>';
Chris Lattner19019ea2009-11-11 22:48:44 +0000288 return OS << "constant<" << *Val.getConstant() << '>';
289}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000290}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000291
292//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000293// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000294//===----------------------------------------------------------------------===//
295
Chris Lattneraf025d32009-11-15 19:59:49 +0000296namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000297 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000298 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000299 struct LVIValueHandle final : public CallbackVH {
Owen Anderson118ac802011-01-05 21:15:29 +0000300 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000301
Owen Anderson118ac802011-01-05 21:15:29 +0000302 LVIValueHandle(Value *V, LazyValueInfoCache *P)
303 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000304
305 void deleted() override;
306 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000307 deleted();
308 }
309 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000310}
Owen Anderson118ac802011-01-05 21:15:29 +0000311
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000312namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000313 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000314 /// maintains information about queries across the clients' queries.
315 class LazyValueInfoCache {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000316 /// This is all of the cached block information for exactly one Value*.
317 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000318 /// entries, allowing us to do a lookup with a binary search.
Bruno Cardoso Lopes1846ea32015-08-18 16:54:36 +0000319 typedef SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4>
320 ValueCacheEntryTy;
Chris Lattneraf025d32009-11-15 19:59:49 +0000321
Sanjay Patel2a385e22015-01-09 16:47:20 +0000322 /// This is all of the cached information for all values,
Owen Anderson6f060af2011-01-05 23:26:22 +0000323 /// mapped from Value* to key information.
Bill Wendling58c75692012-01-12 01:41:03 +0000324 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000325
Sanjay Patel2a385e22015-01-09 16:47:20 +0000326 /// This tracks, on a per-block basis, the set of values that are
327 /// over-defined at the end of that block. This is required
Owen Anderson6f060af2011-01-05 23:26:22 +0000328 /// for cache updating.
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000329 typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>>
330 OverDefinedCacheTy;
331 OverDefinedCacheTy OverDefinedCache;
Benjamin Kramer36647082011-12-03 15:16:45 +0000332
Sanjay Patel2a385e22015-01-09 16:47:20 +0000333 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000334 /// don't spend time removing unused blocks from our caches.
335 DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
336
Sanjay Patel2a385e22015-01-09 16:47:20 +0000337 /// This stack holds the state of the value solver during a query.
338 /// It basically emulates the callstack of the naive
Owen Anderson6f060af2011-01-05 23:26:22 +0000339 /// recursive value lookup process.
340 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
Hal Finkel7e184492014-09-07 20:29:59 +0000341
Sanjay Patel2a385e22015-01-09 16:47:20 +0000342 /// Keeps track of which block-value pairs are in BlockValueStack.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000343 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
344
Sanjay Patel2a385e22015-01-09 16:47:20 +0000345 /// Push BV onto BlockValueStack unless it's already in there.
346 /// Returns true on success.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000347 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
Benjamin Kramer4e3b9032015-02-27 21:43:14 +0000348 if (!BlockValueSet.insert(BV).second)
Hans Wennborg45172ac2014-11-25 17:23:05 +0000349 return false; // It's already in the stack.
350
351 BlockValueStack.push(BV);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000352 return true;
353 }
354
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000355 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
356 const DataLayout &DL; ///< A mandatory DataLayout
357 DominatorTree *DT; ///< An optional DT pointer.
358
Owen Anderson118ac802011-01-05 21:15:29 +0000359 friend struct LVIValueHandle;
Owen Anderson6f060af2011-01-05 23:26:22 +0000360
Hans Wennborg45172ac2014-11-25 17:23:05 +0000361 void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
362 SeenBlocks.insert(BB);
363 lookup(Val)[BB] = Result;
364 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000365 OverDefinedCache[BB].insert(Val);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000366 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000367
Owen Andersonc7ed4dc2010-12-09 06:14:58 +0000368 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000369 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
Hal Finkel7e184492014-09-07 20:29:59 +0000370 LVILatticeVal &Result,
371 Instruction *CxtI = nullptr);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000372 bool hasBlockValue(Value *Val, BasicBlock *BB);
373
374 // These methods process one work item and may add more. A false value
375 // returned means that the work item was not completely processed and must
376 // be revisited after going through the new items.
377 bool solveBlockValue(Value *Val, BasicBlock *BB);
Owen Anderson64c2c572010-12-20 18:18:16 +0000378 bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
379 Value *Val, BasicBlock *BB);
380 bool solveBlockValuePHINode(LVILatticeVal &BBLV,
381 PHINode *PN, BasicBlock *BB);
382 bool solveBlockValueConstantRange(LVILatticeVal &BBLV,
383 Instruction *BBI, BasicBlock *BB);
Hal Finkel7e184492014-09-07 20:29:59 +0000384 void mergeAssumeBlockValueConstantRange(Value *Val, LVILatticeVal &BBLV,
385 Instruction *BBI);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000386
387 void solve();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000388
Nick Lewycky11678bd2010-12-15 18:57:18 +0000389 ValueCacheEntryTy &lookup(Value *V) {
Owen Andersonc7ed4dc2010-12-09 06:14:58 +0000390 return ValueCache[LVIValueHandle(V, this)];
391 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000392
Chris Lattneraf025d32009-11-15 19:59:49 +0000393 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000394 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000395 /// value for the specified Value* at the end of the specified block.
Hal Finkel7e184492014-09-07 20:29:59 +0000396 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
397 Instruction *CxtI = nullptr);
398
Sanjay Patel2a385e22015-01-09 16:47:20 +0000399 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000400 /// value for the specified Value* at the specified instruction (generally
401 /// from an assume intrinsic).
402 LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000403
Sanjay Patel2a385e22015-01-09 16:47:20 +0000404 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000405 /// value for the specified Value* that is true on the specified edge.
Hal Finkel7e184492014-09-07 20:29:59 +0000406 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
407 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000408
Sanjay Patel2a385e22015-01-09 16:47:20 +0000409 /// This is the update interface to inform the cache that an edge from
410 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000411 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000412
Sanjay Patel2a385e22015-01-09 16:47:20 +0000413 /// This is part of the update interface to inform the cache
Owen Anderson208636f2010-08-18 18:39:01 +0000414 /// that a block has been deleted.
415 void eraseBlock(BasicBlock *BB);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000416
Owen Anderson208636f2010-08-18 18:39:01 +0000417 /// clear - Empty the cache.
418 void clear() {
Benjamin Kramerbbf3c602011-12-03 15:19:55 +0000419 SeenBlocks.clear();
Owen Anderson208636f2010-08-18 18:39:01 +0000420 ValueCache.clear();
421 OverDefinedCache.clear();
422 }
Hal Finkel7e184492014-09-07 20:29:59 +0000423
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000424 LazyValueInfoCache(AssumptionCache *AC, const DataLayout &DL,
Chandler Carruth66b31302015-01-04 12:03:27 +0000425 DominatorTree *DT = nullptr)
426 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000427 };
428} // end anonymous namespace
429
Owen Anderson118ac802011-01-05 21:15:29 +0000430void LVIValueHandle::deleted() {
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000431 SmallVector<AssertingVH<BasicBlock>, 4> ToErase;
432 for (auto &I : Parent->OverDefinedCache) {
433 SmallPtrSetImpl<Value *> &ValueSet = I.second;
434 if (ValueSet.count(getValPtr()))
435 ValueSet.erase(getValPtr());
436 if (ValueSet.empty())
437 ToErase.push_back(I.first);
438 }
439 for (auto &BB : ToErase)
440 Parent->OverDefinedCache.erase(BB);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000441
Owen Anderson7b974a42010-08-11 22:36:04 +0000442 // This erasure deallocates *this, so it MUST happen after we're done
443 // using any and all members of *this.
444 Parent->ValueCache.erase(*this);
Owen Andersonc1561b82010-07-30 23:59:40 +0000445}
446
Owen Anderson208636f2010-08-18 18:39:01 +0000447void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
Benjamin Kramer36647082011-12-03 15:16:45 +0000448 // Shortcut if we have never seen this block.
449 DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
450 if (I == SeenBlocks.end())
451 return;
452 SeenBlocks.erase(I);
453
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000454 auto ODI = OverDefinedCache.find(BB);
455 if (ODI != OverDefinedCache.end())
456 OverDefinedCache.erase(ODI);
Owen Anderson208636f2010-08-18 18:39:01 +0000457
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000458 for (auto I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
Owen Anderson208636f2010-08-18 18:39:01 +0000459 I->second.erase(BB);
460}
Owen Andersonc1561b82010-07-30 23:59:40 +0000461
Nick Lewycky55a700b2010-12-18 01:00:40 +0000462void LazyValueInfoCache::solve() {
Owen Anderson6f060af2011-01-05 23:26:22 +0000463 while (!BlockValueStack.empty()) {
464 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000465 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
466
Nuno Lopese6e04902012-06-28 01:16:18 +0000467 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000468 // The work item was completely processed.
469 assert(BlockValueStack.top() == e && "Nothing should have been pushed!");
470 assert(lookup(e.second).count(e.first) && "Result should be in cache!");
471
Owen Anderson6f060af2011-01-05 23:26:22 +0000472 BlockValueStack.pop();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000473 BlockValueSet.erase(e);
474 } else {
475 // More work needs to be done before revisiting.
476 assert(BlockValueStack.top() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000477 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000478 }
479}
480
481bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
482 // If already a constant, there is nothing to compute.
483 if (isa<Constant>(Val))
484 return true;
485
Owen Anderson118ac802011-01-05 21:15:29 +0000486 LVIValueHandle ValHandle(Val, this);
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000487 auto I = ValueCache.find(ValHandle);
Benjamin Kramerf29db272012-08-22 15:37:57 +0000488 if (I == ValueCache.end()) return false;
489 return I->second.count(BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000490}
491
Owen Andersonc7ed4dc2010-12-09 06:14:58 +0000492LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000493 // If already a constant, there is nothing to compute.
494 if (Constant *VC = dyn_cast<Constant>(Val))
495 return LVILatticeVal::get(VC);
496
Benjamin Kramer36647082011-12-03 15:16:45 +0000497 SeenBlocks.insert(BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000498 return lookup(Val)[BB];
499}
500
Philip Reameseb3e9da2015-10-29 03:57:17 +0000501static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
502 switch (BBI->getOpcode()) {
503 default: break;
504 case Instruction::Load:
505 case Instruction::Call:
506 case Instruction::Invoke:
507 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000508 if (isa<IntegerType>(BBI->getType())) {
Philip Reameseb3e9da2015-10-29 03:57:17 +0000509 ConstantRange Result = getConstantRangeFromMetadata(*Ranges);
510 return LVILatticeVal::getRange(Result);
511 }
512 break;
513 };
514 // Nothing known - Note that we do not want overdefined here. We may know
515 // something else about the value and not having range metadata shouldn't
516 // cause us to throw away those facts.
517 return LVILatticeVal();
518}
519
Nick Lewycky55a700b2010-12-18 01:00:40 +0000520bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
521 if (isa<Constant>(Val))
522 return true;
523
Hans Wennborg45172ac2014-11-25 17:23:05 +0000524 if (lookup(Val).count(BB)) {
525 // If we have a cached value, use that.
526 DEBUG(dbgs() << " reuse BB '" << BB->getName()
527 << "' val=" << lookup(Val)[BB] << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000528
Hans Wennborg45172ac2014-11-25 17:23:05 +0000529 // Since we're reusing a cached value, we don't need to update the
530 // OverDefinedCache. The cache will have been properly updated whenever the
531 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000532 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000533 }
534
Hans Wennborg45172ac2014-11-25 17:23:05 +0000535 // Hold off inserting this value into the Cache in case we have to return
536 // false and come back later.
537 LVILatticeVal Res;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000538
Chris Lattneraf025d32009-11-15 19:59:49 +0000539 Instruction *BBI = dyn_cast<Instruction>(Val);
Craig Topper9f008862014-04-15 04:59:12 +0000540 if (!BBI || BBI->getParent() != BB) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000541 if (!solveBlockValueNonLocal(Res, Val, BB))
542 return false;
543 insertResult(Val, BB, Res);
544 return true;
Chris Lattneraf025d32009-11-15 19:59:49 +0000545 }
Chris Lattner2c708562009-11-15 20:00:52 +0000546
Nick Lewycky55a700b2010-12-18 01:00:40 +0000547 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000548 if (!solveBlockValuePHINode(Res, PN, BB))
549 return false;
550 insertResult(Val, BB, Res);
551 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000552 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000553
Igor Laevsky0fa48192015-09-18 13:01:48 +0000554 // If this value is a nonnull pointer, record it's range and bailout.
555 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
556 if (PT && isKnownNonNull(BBI)) {
557 Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000558 insertResult(Val, BB, Res);
559 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000560 }
561
Philip Reameseb3e9da2015-10-29 03:57:17 +0000562 // If this is an instruction which supports range metadata, return the
563 // implied range. TODO: This should be an intersection, not a union.
564 Res.mergeIn(getFromRangeMetadata(BBI), DL);
565
Owen Anderson80d19f02010-08-18 21:11:37 +0000566 // We can only analyze the definitions of certain classes of instructions
567 // (integral binops and casts at the moment), so bail if this isn't one.
Chris Lattneraf025d32009-11-15 19:59:49 +0000568 LVILatticeVal Result;
Owen Anderson80d19f02010-08-18 21:11:37 +0000569 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
570 !BBI->getType()->isIntegerTy()) {
571 DEBUG(dbgs() << " compute BB '" << BB->getName()
572 << "' - overdefined because inst def found.\n");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000573 Res.markOverdefined();
574 insertResult(Val, BB, Res);
575 return true;
Owen Anderson80d19f02010-08-18 21:11:37 +0000576 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000577
Owen Anderson80d19f02010-08-18 21:11:37 +0000578 // FIXME: We're currently limited to binops with a constant RHS. This should
579 // be improved.
580 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
581 if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
582 DEBUG(dbgs() << " compute BB '" << BB->getName()
583 << "' - overdefined because inst def found.\n");
584
Hans Wennborg45172ac2014-11-25 17:23:05 +0000585 Res.markOverdefined();
586 insertResult(Val, BB, Res);
587 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000588 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000589
Hans Wennborg45172ac2014-11-25 17:23:05 +0000590 if (!solveBlockValueConstantRange(Res, BBI, BB))
591 return false;
592 insertResult(Val, BB, Res);
593 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000594}
595
596static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
597 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
598 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000599 GetUnderlyingObject(L->getPointerOperand(),
600 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000601 }
602 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
603 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000604 GetUnderlyingObject(S->getPointerOperand(),
605 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000606 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000607 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
608 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000609
610 // FIXME: check whether it has a valuerange that excludes zero?
611 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
612 if (!Len || Len->isZero()) return false;
613
Eli Friedman7a5fc692011-05-31 20:40:16 +0000614 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000615 if (GetUnderlyingObject(MI->getRawDest(),
616 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000617 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000618 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000619 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000620 if (GetUnderlyingObject(MTI->getRawSource(),
621 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000622 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000623 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000624 return false;
625}
626
Owen Anderson64c2c572010-12-20 18:18:16 +0000627bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
628 Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000629 LVILatticeVal Result; // Start Undefined.
630
631 // If this is a pointer, and there's a load from that pointer in this BB,
632 // then we know that the pointer can't be NULL.
633 bool NotNull = false;
634 if (Val->getType()->isPointerTy()) {
Nick Lewyckyc86037f2012-10-26 04:43:47 +0000635 if (isKnownNonNull(Val)) {
Nick Lewycky367f98f2011-01-15 09:16:12 +0000636 NotNull = true;
637 } else {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000638 const DataLayout &DL = BB->getModule()->getDataLayout();
639 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
Nick Lewyckyc86037f2012-10-26 04:43:47 +0000640 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
641 // inside InstructionDereferencesPointer either.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000642 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1)) {
Hans Wennborgcbb18e32014-11-21 19:07:46 +0000643 for (Instruction &I : *BB) {
644 if (InstructionDereferencesPointer(&I, UnderlyingVal)) {
Nick Lewyckyc86037f2012-10-26 04:43:47 +0000645 NotNull = true;
646 break;
647 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000648 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000649 }
650 }
651 }
652
653 // If this is the entry block, we must be asking about an argument. The
654 // value is overdefined.
655 if (BB == &BB->getParent()->getEntryBlock()) {
656 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
657 if (NotNull) {
Chris Lattner229907c2011-07-18 04:54:35 +0000658 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000659 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
660 } else {
661 Result.markOverdefined();
662 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000663 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000664 return true;
665 }
666
667 // Loop over all of our predecessors, merging what we know from them into
668 // result.
669 bool EdgesMissing = false;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000670 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000671 LVILatticeVal EdgeResult;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000672 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000673 if (EdgesMissing)
674 continue;
675
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000676 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000677
678 // If we hit overdefined, exit early. The BlockVals entry is already set
679 // to overdefined.
680 if (Result.isOverdefined()) {
681 DEBUG(dbgs() << " compute BB '" << BB->getName()
682 << "' - overdefined because of pred.\n");
683 // If we previously determined that this is a pointer that can't be null
684 // then return that rather than giving up entirely.
685 if (NotNull) {
Chris Lattner229907c2011-07-18 04:54:35 +0000686 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000687 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
688 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000689
Owen Anderson64c2c572010-12-20 18:18:16 +0000690 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000691 return true;
692 }
693 }
694 if (EdgesMissing)
695 return false;
696
697 // Return the merged value, which is more precise than 'overdefined'.
698 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000699 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000700 return true;
701}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000702
Owen Anderson64c2c572010-12-20 18:18:16 +0000703bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
704 PHINode *PN, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000705 LVILatticeVal Result; // Start Undefined.
706
707 // Loop over all of our predecessors, merging what we know from them into
708 // result.
709 bool EdgesMissing = false;
710 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
711 BasicBlock *PhiBB = PN->getIncomingBlock(i);
712 Value *PhiVal = PN->getIncomingValue(i);
713 LVILatticeVal EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000714 // Note that we can provide PN as the context value to getEdgeValue, even
715 // though the results will be cached, because PN is the value being used as
716 // the cache key in the caller.
Hal Finkel7e184492014-09-07 20:29:59 +0000717 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000718 if (EdgesMissing)
719 continue;
720
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000721 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000722
723 // If we hit overdefined, exit early. The BlockVals entry is already set
724 // to overdefined.
725 if (Result.isOverdefined()) {
726 DEBUG(dbgs() << " compute BB '" << BB->getName()
727 << "' - overdefined because of pred.\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000728
Owen Anderson64c2c572010-12-20 18:18:16 +0000729 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000730 return true;
731 }
732 }
733 if (EdgesMissing)
734 return false;
735
736 // Return the merged value, which is more precise than 'overdefined'.
737 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000738 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000739 return true;
740}
741
Hal Finkel7e184492014-09-07 20:29:59 +0000742static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
743 LVILatticeVal &Result,
744 bool isTrueDest = true);
745
Hans Wennborgc5ec73d2014-11-21 18:58:23 +0000746// If we can determine a constant range for the value Val in the context
Hal Finkel7e184492014-09-07 20:29:59 +0000747// provided by the instruction BBI, then merge it into BBLV. If we did find a
748// constant range, return true.
Hans Wennborgc5ec73d2014-11-21 18:58:23 +0000749void LazyValueInfoCache::mergeAssumeBlockValueConstantRange(Value *Val,
750 LVILatticeVal &BBLV,
751 Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000752 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
753 if (!BBI)
754 return;
755
Chandler Carruth66b31302015-01-04 12:03:27 +0000756 for (auto &AssumeVH : AC->assumptions()) {
757 if (!AssumeVH)
758 continue;
759 auto *I = cast<CallInst>(AssumeVH);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000760 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000761 continue;
762
763 Value *C = I->getArgOperand(0);
764 if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) {
765 LVILatticeVal Result;
766 if (getValueFromFromCondition(Val, ICI, Result)) {
767 if (BBLV.isOverdefined())
768 BBLV = Result;
769 else
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000770 BBLV.mergeIn(Result, DL);
Hal Finkel7e184492014-09-07 20:29:59 +0000771 }
772 }
773 }
774}
775
Owen Anderson64c2c572010-12-20 18:18:16 +0000776bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
777 Instruction *BBI,
Nick Lewycky55a700b2010-12-18 01:00:40 +0000778 BasicBlock *BB) {
Owen Anderson80d19f02010-08-18 21:11:37 +0000779 // Figure out the range of the LHS. If that fails, bail.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000780 if (!hasBlockValue(BBI->getOperand(0), BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000781 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
782 return false;
783 BBLV.markOverdefined();
784 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000785 }
786
Nick Lewycky55a700b2010-12-18 01:00:40 +0000787 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Hal Finkel7e184492014-09-07 20:29:59 +0000788 mergeAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI);
Owen Anderson80d19f02010-08-18 21:11:37 +0000789 if (!LHSVal.isConstantRange()) {
Owen Anderson64c2c572010-12-20 18:18:16 +0000790 BBLV.markOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000791 return true;
Owen Anderson80d19f02010-08-18 21:11:37 +0000792 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000793
Owen Anderson80d19f02010-08-18 21:11:37 +0000794 ConstantRange LHSRange = LHSVal.getConstantRange();
795 ConstantRange RHSRange(1);
Chris Lattner229907c2011-07-18 04:54:35 +0000796 IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
Owen Anderson80d19f02010-08-18 21:11:37 +0000797 if (isa<BinaryOperator>(BBI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000798 if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) {
799 RHSRange = ConstantRange(RHS->getValue());
800 } else {
Owen Anderson64c2c572010-12-20 18:18:16 +0000801 BBLV.markOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000802 return true;
Owen Andersonc62f7042010-08-24 07:55:44 +0000803 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000804 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000805
Owen Anderson80d19f02010-08-18 21:11:37 +0000806 // NOTE: We're currently limited by the set of operations that ConstantRange
807 // can evaluate symbolically. Enhancing that set will allows us to analyze
808 // more definitions.
Owen Anderson64c2c572010-12-20 18:18:16 +0000809 LVILatticeVal Result;
Owen Anderson80d19f02010-08-18 21:11:37 +0000810 switch (BBI->getOpcode()) {
811 case Instruction::Add:
812 Result.markConstantRange(LHSRange.add(RHSRange));
813 break;
814 case Instruction::Sub:
815 Result.markConstantRange(LHSRange.sub(RHSRange));
816 break;
817 case Instruction::Mul:
818 Result.markConstantRange(LHSRange.multiply(RHSRange));
819 break;
820 case Instruction::UDiv:
821 Result.markConstantRange(LHSRange.udiv(RHSRange));
822 break;
823 case Instruction::Shl:
824 Result.markConstantRange(LHSRange.shl(RHSRange));
825 break;
826 case Instruction::LShr:
827 Result.markConstantRange(LHSRange.lshr(RHSRange));
828 break;
829 case Instruction::Trunc:
830 Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
831 break;
832 case Instruction::SExt:
833 Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
834 break;
835 case Instruction::ZExt:
836 Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
837 break;
838 case Instruction::BitCast:
839 Result.markConstantRange(LHSRange);
840 break;
Nick Lewyckyad48e012010-09-07 05:39:02 +0000841 case Instruction::And:
842 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
843 break;
844 case Instruction::Or:
845 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
846 break;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000847
Owen Anderson80d19f02010-08-18 21:11:37 +0000848 // Unhandled instructions are overdefined.
849 default:
850 DEBUG(dbgs() << " compute BB '" << BB->getName()
851 << "' - overdefined because inst def found.\n");
852 Result.markOverdefined();
853 break;
854 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000855
Owen Anderson64c2c572010-12-20 18:18:16 +0000856 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000857 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +0000858}
859
Hal Finkel7e184492014-09-07 20:29:59 +0000860bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
861 LVILatticeVal &Result, bool isTrueDest) {
862 if (ICI && isa<Constant>(ICI->getOperand(1))) {
863 if (ICI->isEquality() && ICI->getOperand(0) == Val) {
864 // We know that V has the RHS constant if this is a true SETEQ or
865 // false SETNE.
866 if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
867 Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
868 else
869 Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
870 return true;
871 }
872
873 // Recognize the range checking idiom that InstCombine produces.
874 // (X-C1) u< C2 --> [C1, C1+C2)
875 ConstantInt *NegOffset = nullptr;
876 if (ICI->getPredicate() == ICmpInst::ICMP_ULT)
877 match(ICI->getOperand(0), m_Add(m_Specific(Val),
878 m_ConstantInt(NegOffset)));
879
880 ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1));
881 if (CI && (ICI->getOperand(0) == Val || NegOffset)) {
Sanjoy Das7182d362015-03-18 00:41:24 +0000882 // Calculate the range of values that are allowed by the comparison
Hal Finkel7e184492014-09-07 20:29:59 +0000883 ConstantRange CmpRange(CI->getValue());
884 ConstantRange TrueValues =
Sanjoy Das7182d362015-03-18 00:41:24 +0000885 ConstantRange::makeAllowedICmpRegion(ICI->getPredicate(), CmpRange);
Hal Finkel7e184492014-09-07 20:29:59 +0000886
887 if (NegOffset) // Apply the offset from above.
888 TrueValues = TrueValues.subtract(NegOffset->getValue());
889
890 // If we're interested in the false dest, invert the condition.
891 if (!isTrueDest) TrueValues = TrueValues.inverse();
892
893 Result = LVILatticeVal::getRange(TrueValues);
894 return true;
895 }
896 }
897
898 return false;
899}
900
Nuno Lopese6e04902012-06-28 01:16:18 +0000901/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
902/// Val is not constrained on the edge.
903static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
904 BasicBlock *BBTo, LVILatticeVal &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000905 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +0000906 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +0000907 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
908 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +0000909 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +0000910 if (BI->isConditional() &&
911 BI->getSuccessor(0) != BI->getSuccessor(1)) {
912 bool isTrueDest = BI->getSuccessor(0) == BBTo;
913 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
914 "BBTo isn't a successor of BBFrom");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000915
Chris Lattner19019ea2009-11-11 22:48:44 +0000916 // If V is the condition of the branch itself, then we know exactly what
917 // it is.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000918 if (BI->getCondition() == Val) {
919 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +0000920 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +0000921 return true;
922 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000923
Chris Lattner19019ea2009-11-11 22:48:44 +0000924 // If the condition of the branch is an equality comparison, we may be
925 // able to infer the value.
Sanjay Pateld7291152015-01-09 16:28:15 +0000926 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
927 if (getValueFromFromCondition(Val, ICI, Result, isTrueDest))
928 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +0000929 }
930 }
Chris Lattner77358782009-11-15 20:02:12 +0000931
932 // If the edge was formed by a switch on the value, then we may know exactly
933 // what it is.
934 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Nuno Lopes8650fb82012-06-28 16:13:37 +0000935 if (SI->getCondition() != Val)
936 return false;
937
938 bool DefaultCase = SI->getDefaultDest() == BBTo;
939 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
940 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
941
Hans Wennborgcbb18e32014-11-21 19:07:46 +0000942 for (SwitchInst::CaseIt i : SI->cases()) {
Nuno Lopes8650fb82012-06-28 16:13:37 +0000943 ConstantRange EdgeVal(i.getCaseValue()->getValue());
Manman Renf3fedb62012-09-05 23:45:58 +0000944 if (DefaultCase) {
945 // It is possible that the default destination is the destination of
946 // some cases. There is no need to perform difference for those cases.
947 if (i.getCaseSuccessor() != BBTo)
948 EdgesVals = EdgesVals.difference(EdgeVal);
949 } else if (i.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +0000950 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +0000951 }
Nuno Lopes8650fb82012-06-28 16:13:37 +0000952 Result = LVILatticeVal::getRange(EdgesVals);
953 return true;
Chris Lattner77358782009-11-15 20:02:12 +0000954 }
Nuno Lopese6e04902012-06-28 01:16:18 +0000955 return false;
956}
957
Sanjay Patel938e2792015-01-09 16:35:37 +0000958/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
959/// the basic block if the edge does not constrain Val.
Nuno Lopese6e04902012-06-28 01:16:18 +0000960bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Hal Finkel7e184492014-09-07 20:29:59 +0000961 BasicBlock *BBTo, LVILatticeVal &Result,
962 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +0000963 // If already a constant, there is nothing to compute.
964 if (Constant *VC = dyn_cast<Constant>(Val)) {
965 Result = LVILatticeVal::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000966 return true;
967 }
Nuno Lopese6e04902012-06-28 01:16:18 +0000968
969 if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) {
970 if (!Result.isConstantRange() ||
Hans Wennborgc5ec73d2014-11-21 18:58:23 +0000971 Result.getConstantRange().getSingleElement())
Nuno Lopese6e04902012-06-28 01:16:18 +0000972 return true;
973
974 // FIXME: this check should be moved to the beginning of the function when
975 // LVI better supports recursive values. Even for the single value case, we
976 // can intersect to detect dead code (an empty range).
977 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000978 if (pushBlockValue(std::make_pair(BBFrom, Val)))
979 return false;
980 Result.markOverdefined();
981 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +0000982 }
983
984 // Try to intersect ranges of the BB and the constraint on the edge.
985 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Hal Finkela3f23e32014-10-14 16:04:49 +0000986 mergeAssumeBlockValueConstantRange(Val, InBlock, BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +0000987 // See note on the use of the CxtI with mergeAssumeBlockValueConstantRange,
988 // and caching, below.
Hal Finkel7e184492014-09-07 20:29:59 +0000989 mergeAssumeBlockValueConstantRange(Val, InBlock, CxtI);
Nuno Lopese6e04902012-06-28 01:16:18 +0000990 if (!InBlock.isConstantRange())
991 return true;
992
993 ConstantRange Range =
994 Result.getConstantRange().intersectWith(InBlock.getConstantRange());
995 Result = LVILatticeVal::getRange(Range);
996 return true;
997 }
998
999 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001000 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1001 return false;
1002 Result.markOverdefined();
1003 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001004 }
1005
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001006 // If we couldn't compute the value on the edge, use the value from the BB.
Nuno Lopese6e04902012-06-28 01:16:18 +00001007 Result = getBlockValue(Val, BBFrom);
Hal Finkela3f23e32014-10-14 16:04:49 +00001008 mergeAssumeBlockValueConstantRange(Val, Result, BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001009 // We can use the context instruction (generically the ultimate instruction
1010 // the calling pass is trying to simplify) here, even though the result of
1011 // this function is generally cached when called from the solve* functions
1012 // (and that cached result might be used with queries using a different
1013 // context instruction), because when this function is called from the solve*
1014 // functions, the context instruction is not provided. When called from
1015 // LazyValueInfoCache::getValueOnEdge, the context instruction is provided,
1016 // but then the result is not cached.
Hal Finkel7e184492014-09-07 20:29:59 +00001017 mergeAssumeBlockValueConstantRange(Val, Result, CxtI);
Nuno Lopese6e04902012-06-28 01:16:18 +00001018 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001019}
1020
Hal Finkel7e184492014-09-07 20:29:59 +00001021LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB,
1022 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001023 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001024 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001025
Hans Wennborg45172ac2014-11-25 17:23:05 +00001026 assert(BlockValueStack.empty() && BlockValueSet.empty());
1027 pushBlockValue(std::make_pair(BB, V));
1028
Nick Lewycky55a700b2010-12-18 01:00:40 +00001029 solve();
Owen Andersonc7ed4dc2010-12-09 06:14:58 +00001030 LVILatticeVal Result = getBlockValue(V, BB);
Hal Finkel7e184492014-09-07 20:29:59 +00001031 mergeAssumeBlockValueConstantRange(V, Result, CxtI);
1032
1033 DEBUG(dbgs() << " Result = " << Result << "\n");
1034 return Result;
1035}
1036
1037LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) {
1038 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1039 << CxtI->getName() << "'\n");
1040
1041 LVILatticeVal Result;
Philip Reameseb3e9da2015-10-29 03:57:17 +00001042 if (auto *I = dyn_cast<Instruction>(V))
1043 Result = getFromRangeMetadata(I);
Hal Finkel7e184492014-09-07 20:29:59 +00001044 mergeAssumeBlockValueConstantRange(V, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001045
David Greene37e98092009-12-23 20:43:58 +00001046 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001047 return Result;
1048}
Chris Lattner19019ea2009-11-11 22:48:44 +00001049
Chris Lattneraf025d32009-11-15 19:59:49 +00001050LVILatticeVal LazyValueInfoCache::
Hal Finkel7e184492014-09-07 20:29:59 +00001051getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1052 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001053 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001054 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001055
Nick Lewycky55a700b2010-12-18 01:00:40 +00001056 LVILatticeVal Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001057 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001058 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001059 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001060 (void)WasFastQuery;
1061 assert(WasFastQuery && "More work to do after problem solved?");
1062 }
1063
David Greene37e98092009-12-23 20:43:58 +00001064 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001065 return Result;
1066}
1067
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001068void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
1069 BasicBlock *NewSucc) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001070 // When an edge in the graph has been threaded, values that we could not
1071 // determine a value for before (i.e. were marked overdefined) may be
1072 // possible to solve now. We do NOT try to proactively update these values.
1073 // Instead, we clear their entries from the cache, and allow lazy updating to
1074 // recompute them when needed.
1075
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001076 // The updating process is fairly simple: we need to drop cached info
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001077 // for all values that were marked overdefined in OldSucc, and for those same
1078 // values in any successor of OldSucc (except NewSucc) in which they were
1079 // also marked overdefined.
1080 std::vector<BasicBlock*> worklist;
1081 worklist.push_back(OldSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001082
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001083 auto I = OverDefinedCache.find(OldSucc);
1084 if (I == OverDefinedCache.end())
1085 return; // Nothing to process here.
Bruno Cardoso Lopes7a1483e2015-08-21 21:18:26 +00001086 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001087
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001088 // Use a worklist to perform a depth-first search of OldSucc's successors.
1089 // NOTE: We do not need a visited list since any blocks we have already
1090 // visited will have had their overdefined markers cleared already, and we
1091 // thus won't loop to their successors.
1092 while (!worklist.empty()) {
1093 BasicBlock *ToUpdate = worklist.back();
1094 worklist.pop_back();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001095
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001096 // Skip blocks only accessible through NewSucc.
1097 if (ToUpdate == NewSucc) continue;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001098
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001099 bool changed = false;
Bruno Cardoso Lopes7a1483e2015-08-21 21:18:26 +00001100 for (Value *V : ValsToClear) {
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001101 // If a value was marked overdefined in OldSucc, and is here too...
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001102 auto OI = OverDefinedCache.find(ToUpdate);
1103 if (OI == OverDefinedCache.end())
1104 continue;
1105 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
1106 if (!ValueSet.count(V))
1107 continue;
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001108
Owen Andersonaac5a722010-07-27 23:58:11 +00001109 // Remove it from the caches.
Hans Wennborgcbb18e32014-11-21 19:07:46 +00001110 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(V, this)];
Owen Andersonaac5a722010-07-27 23:58:11 +00001111 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001112
Owen Andersonaac5a722010-07-27 23:58:11 +00001113 assert(CI != Entry.end() && "Couldn't find entry to update?");
1114 Entry.erase(CI);
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001115 ValueSet.erase(V);
1116 if (ValueSet.empty())
1117 OverDefinedCache.erase(OI);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001118
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001119 // If we removed anything, then we potentially need to update
Owen Andersonaac5a722010-07-27 23:58:11 +00001120 // blocks successors too.
1121 changed = true;
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001122 }
Nick Lewycky55a700b2010-12-18 01:00:40 +00001123
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001124 if (!changed) continue;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001125
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001126 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
1127 }
1128}
1129
Chris Lattneraf025d32009-11-15 19:59:49 +00001130//===----------------------------------------------------------------------===//
1131// LazyValueInfo Impl
1132//===----------------------------------------------------------------------===//
1133
Sanjay Patel2a385e22015-01-09 16:47:20 +00001134/// This lazily constructs the LazyValueInfoCache.
Chandler Carruth66b31302015-01-04 12:03:27 +00001135static LazyValueInfoCache &getCache(void *&PImpl, AssumptionCache *AC,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001136 const DataLayout *DL,
Hal Finkel7e184492014-09-07 20:29:59 +00001137 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001138 if (!PImpl) {
1139 assert(DL && "getCache() called with a null DataLayout");
1140 PImpl = new LazyValueInfoCache(AC, *DL, DT);
1141 }
Chris Lattneraf025d32009-11-15 19:59:49 +00001142 return *static_cast<LazyValueInfoCache*>(PImpl);
1143}
1144
Owen Anderson208636f2010-08-18 18:39:01 +00001145bool LazyValueInfo::runOnFunction(Function &F) {
Chandler Carruth66b31302015-01-04 12:03:27 +00001146 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001147 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001148
1149 DominatorTreeWrapperPass *DTWP =
1150 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1151 DT = DTWP ? &DTWP->getDomTree() : nullptr;
Chad Rosier43a33062011-12-02 01:26:24 +00001152
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001153 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001154
Hal Finkel7e184492014-09-07 20:29:59 +00001155 if (PImpl)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001156 getCache(PImpl, AC, &DL, DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001157
Owen Anderson208636f2010-08-18 18:39:01 +00001158 // Fully lazy.
1159 return false;
1160}
1161
Chad Rosier43a33062011-12-02 01:26:24 +00001162void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1163 AU.setPreservesAll();
Chandler Carruth66b31302015-01-04 12:03:27 +00001164 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001165 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001166}
1167
Chris Lattneraf025d32009-11-15 19:59:49 +00001168void LazyValueInfo::releaseMemory() {
1169 // If the cache was allocated, free it.
1170 if (PImpl) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001171 delete &getCache(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001172 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001173 }
1174}
1175
Hal Finkel7e184492014-09-07 20:29:59 +00001176Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1177 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001178 const DataLayout &DL = BB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001179 LVILatticeVal Result =
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001180 getCache(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001181
Chris Lattner19019ea2009-11-11 22:48:44 +00001182 if (Result.isConstant())
1183 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001184 if (Result.isConstantRange()) {
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001185 ConstantRange CR = Result.getConstantRange();
1186 if (const APInt *SingleVal = CR.getSingleElement())
1187 return ConstantInt::get(V->getContext(), *SingleVal);
1188 }
Craig Topper9f008862014-04-15 04:59:12 +00001189 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001190}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001191
Sanjay Patel2a385e22015-01-09 16:47:20 +00001192/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001193/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001194Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001195 BasicBlock *ToBB,
1196 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001197 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001198 LVILatticeVal Result =
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001199 getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001200
Chris Lattnerd5e25432009-11-12 01:29:10 +00001201 if (Result.isConstant())
1202 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001203 if (Result.isConstantRange()) {
Owen Anderson185fe002010-08-10 20:03:09 +00001204 ConstantRange CR = Result.getConstantRange();
1205 if (const APInt *SingleVal = CR.getSingleElement())
1206 return ConstantInt::get(V->getContext(), *SingleVal);
1207 }
Craig Topper9f008862014-04-15 04:59:12 +00001208 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001209}
1210
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001211static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
1212 LVILatticeVal &Result,
1213 const DataLayout &DL,
1214 TargetLibraryInfo *TLI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001215
Chris Lattner565ee2f2009-11-12 04:36:58 +00001216 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001217 Constant *Res = nullptr;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001218 if (Result.isConstant()) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001219 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001220 TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001221 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001222 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1223 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001224 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001225
Owen Anderson185fe002010-08-10 20:03:09 +00001226 if (Result.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001227 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001228 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001229
Owen Anderson185fe002010-08-10 20:03:09 +00001230 ConstantRange CR = Result.getConstantRange();
1231 if (Pred == ICmpInst::ICMP_EQ) {
1232 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001233 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001234
Owen Anderson185fe002010-08-10 20:03:09 +00001235 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001236 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001237 } else if (Pred == ICmpInst::ICMP_NE) {
1238 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001239 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001240
Owen Anderson185fe002010-08-10 20:03:09 +00001241 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001242 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001243 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001244
Owen Anderson185fe002010-08-10 20:03:09 +00001245 // Handle more complex predicates.
Nick Lewycky11678bd2010-12-15 18:57:18 +00001246 ConstantRange TrueValues =
1247 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1248 if (TrueValues.contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001249 return LazyValueInfo::True;
Nick Lewycky11678bd2010-12-15 18:57:18 +00001250 if (TrueValues.inverse().contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001251 return LazyValueInfo::False;
1252 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001253 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001254
Chris Lattneraf025d32009-11-15 19:59:49 +00001255 if (Result.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001256 // If this is an equality comparison, we can try to fold it knowing that
1257 // "V != C1".
1258 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001259 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001260 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001261 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001262 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001263 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001264 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001265 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001266 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001267 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001268 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001269 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001270 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001271 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001272 }
Hal Finkel7e184492014-09-07 20:29:59 +00001273 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001274 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001275
Hal Finkel7e184492014-09-07 20:29:59 +00001276 return LazyValueInfo::Unknown;
1277}
1278
Sanjay Patel2a385e22015-01-09 16:47:20 +00001279/// Determine whether the specified value comparison with a constant is known to
1280/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001281LazyValueInfo::Tristate
1282LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1283 BasicBlock *FromBB, BasicBlock *ToBB,
1284 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001285 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001286 LVILatticeVal Result =
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001287 getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001288
1289 return getPredicateResult(Pred, C, Result, DL, TLI);
1290}
1291
1292LazyValueInfo::Tristate
1293LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1294 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001295 const DataLayout &DL = CxtI->getModule()->getDataLayout();
1296 LVILatticeVal Result = getCache(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001297 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1298 if (Ret != Unknown)
1299 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001300
Philip Reamesaeefae02015-11-04 01:47:04 +00001301 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1302 // LVI as a whole tries to compute a lattice value which is conservatively
1303 // correct at a given location. In this case, we have a predicate which we
1304 // weren't able to prove about the merged result, and we're pushing that
1305 // predicate back along each incoming edge to see if we can prove it
1306 // separately for each input. As a motivating example, consider:
1307 // bb1:
1308 // %v1 = ... ; constantrange<1, 5>
1309 // br label %merge
1310 // bb2:
1311 // %v2 = ... ; constantrange<10, 20>
1312 // br label %merge
1313 // merge:
1314 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1315 // %pred = icmp eq i32 %phi, 8
1316 // We can't tell from the lattice value for '%phi' that '%pred' is false
1317 // along each path, but by checking the predicate over each input separately,
1318 // we can.
1319 // We limit the search to one step backwards from the current BB and value.
1320 // We could consider extending this to search further backwards through the
1321 // CFG and/or value graph, but there are non-obvious compile time vs quality
1322 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001323 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001324 BasicBlock *BB = CxtI->getParent();
1325
1326 // Function entry or an unreachable block. Bail to avoid confusing
1327 // analysis below.
1328 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1329 if (PI == PE)
1330 return Unknown;
1331
1332 // If V is a PHI node in the same block as the context, we need to ask
1333 // questions about the predicate as applied to the incoming value along
1334 // each edge. This is useful for eliminating cases where the predicate is
1335 // known along all incoming edges.
1336 if (auto *PHI = dyn_cast<PHINode>(V))
1337 if (PHI->getParent() == BB) {
1338 Tristate Baseline = Unknown;
1339 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1340 Value *Incoming = PHI->getIncomingValue(i);
1341 BasicBlock *PredBB = PHI->getIncomingBlock(i);
1342 // Note that PredBB may be BB itself.
1343 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1344 CxtI);
1345
1346 // Keep going as long as we've seen a consistent known result for
1347 // all inputs.
1348 Baseline = (i == 0) ? Result /* First iteration */
1349 : (Baseline == Result ? Baseline : Unknown); /* All others */
1350 if (Baseline == Unknown)
1351 break;
1352 }
1353 if (Baseline != Unknown)
1354 return Baseline;
1355 }
1356
Philip Reames66ab0f02015-06-16 00:49:59 +00001357 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001358 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001359 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001360 if (!isa<Instruction>(V) ||
1361 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001362 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001363 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001364 // the value of the comparison in this block.
1365 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1366 if (Baseline != Unknown) {
1367 // Check that all remaining incoming values match the first one.
1368 while (++PI != PE) {
1369 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1370 if (Ret != Baseline) break;
1371 }
1372 // If we terminated early, then one of the values didn't match.
1373 if (PI == PE) {
1374 return Baseline;
1375 }
1376 }
1377 }
1378 }
1379 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001380}
1381
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001382void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001383 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001384 if (PImpl) {
1385 const DataLayout &DL = PredBB->getModule()->getDataLayout();
1386 getCache(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
1387 }
Owen Anderson208636f2010-08-18 18:39:01 +00001388}
1389
1390void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001391 if (PImpl) {
1392 const DataLayout &DL = BB->getModule()->getDataLayout();
1393 getCache(PImpl, AC, &DL, DT).eraseBlock(BB);
1394 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001395}