blob: dc22b8173a85ddda1c99b51befba5a1809f3c4a8 [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"
Daniel Jasperaec2fa32016-12-19 08:22:17 +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"
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +000029#include "llvm/IR/Intrinsics.h"
Philip Reameseb3e9da2015-10-29 03:57:17 +000030#include "llvm/IR/LLVMContext.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000031#include "llvm/IR/PatternMatch.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000032#include "llvm/IR/ValueHandle.h"
Chris Lattnerb584d1e2009-11-12 01:22:16 +000033#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/raw_ostream.h"
Bill Wendling4ec081a2012-01-11 23:43:34 +000035#include <map>
Nick Lewycky55a700b2010-12-18 01:00:40 +000036#include <stack>
Chris Lattner741c94c2009-11-11 00:22:30 +000037using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000038using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000039
Chandler Carruthf1221bd2014-04-22 02:48:03 +000040#define DEBUG_TYPE "lazy-value-info"
41
Sean Silva687019f2016-06-13 22:01:25 +000042char LazyValueInfoWrapperPass::ID = 0;
43INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosier43a33062011-12-02 01:26:24 +000044 "Lazy Value Information Analysis", false, true)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000045INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000046INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva687019f2016-06-13 22:01:25 +000047INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000048 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000049
50namespace llvm {
Sean Silva687019f2016-06-13 22:01:25 +000051 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner741c94c2009-11-11 00:22:30 +000052}
53
Chandler Carruthdab4eae2016-11-23 17:53:26 +000054AnalysisKey LazyValueAnalysis::Key;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000055
56//===----------------------------------------------------------------------===//
57// LVILatticeVal
58//===----------------------------------------------------------------------===//
59
Sanjay Patel2a385e22015-01-09 16:47:20 +000060/// This is the information tracked by LazyValueInfo for each value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000061///
62/// FIXME: This is basically just for bringup, this can be made a lot more rich
63/// in the future.
64///
65namespace {
66class LVILatticeVal {
67 enum LatticeValueTy {
Philip Reames3bb28322016-04-25 18:48:43 +000068 /// This Value has no known value yet. As a result, this implies the
69 /// producing instruction is dead. Caution: We use this as the starting
70 /// state in our local meet rules. In this usage, it's taken to mean
NAKAMURA Takumif2529512016-07-04 01:26:27 +000071 /// "nothing known yet".
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000072 undefined,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000073
Philip Reames02bb6a62016-12-07 04:48:50 +000074 /// This Value has a specific constant value. (For constant integers,
75 /// constantrange is used instead. Integer typed constantexprs can appear
76 /// as constant.)
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000077 constant,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000078
Philip Reames02bb6a62016-12-07 04:48:50 +000079 /// This Value is known to not have the specified value. (For constant
80 /// integers, constantrange is used instead. As above, integer typed
81 /// constantexprs can appear here.)
Chris Lattner565ee2f2009-11-12 04:36:58 +000082 notconstant,
Chad Rosier43a33062011-12-02 01:26:24 +000083
Philip Reames3bb28322016-04-25 18:48:43 +000084 /// The Value falls within this range. (Used only for integer typed values.)
Owen Anderson0f306a42010-08-05 22:59:19 +000085 constantrange,
Chad Rosier43a33062011-12-02 01:26:24 +000086
Philip Reames3bb28322016-04-25 18:48:43 +000087 /// We can not precisely model the dynamic values this value might take.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000088 overdefined
89 };
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000090
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000091 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattner565ee2f2009-11-12 04:36:58 +000092 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersonc3a14132010-08-05 22:10:46 +000093 LatticeValueTy Tag;
94 Constant *Val;
Owen Anderson0f306a42010-08-05 22:59:19 +000095 ConstantRange Range;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000096
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000097public:
Craig Topper9f008862014-04-15 04:59:12 +000098 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000099
Chris Lattner19019ea2009-11-11 22:48:44 +0000100 static LVILatticeVal get(Constant *C) {
101 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000102 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000103 Res.markConstant(C);
Chris Lattner19019ea2009-11-11 22:48:44 +0000104 return Res;
105 }
Chris Lattner565ee2f2009-11-12 04:36:58 +0000106 static LVILatticeVal getNot(Constant *C) {
107 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000108 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000109 Res.markNotConstant(C);
Chris Lattner565ee2f2009-11-12 04:36:58 +0000110 return Res;
111 }
Owen Anderson5f1dd092010-08-10 23:20:01 +0000112 static LVILatticeVal getRange(ConstantRange CR) {
113 LVILatticeVal Res;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000114 Res.markConstantRange(std::move(CR));
Owen Anderson5f1dd092010-08-10 23:20:01 +0000115 return Res;
116 }
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000117 static LVILatticeVal getOverdefined() {
118 LVILatticeVal Res;
119 Res.markOverdefined();
120 return Res;
121 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000122
Owen Anderson0f306a42010-08-05 22:59:19 +0000123 bool isUndefined() const { return Tag == undefined; }
124 bool isConstant() const { return Tag == constant; }
125 bool isNotConstant() const { return Tag == notconstant; }
126 bool isConstantRange() const { return Tag == constantrange; }
127 bool isOverdefined() const { return Tag == overdefined; }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000128
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000129 Constant *getConstant() const {
130 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000131 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000132 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000133
Chris Lattner565ee2f2009-11-12 04:36:58 +0000134 Constant *getNotConstant() const {
135 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000136 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000137 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000138
Owen Anderson0f306a42010-08-05 22:59:19 +0000139 ConstantRange getConstantRange() const {
140 assert(isConstantRange() &&
141 "Cannot get the constant-range of a non-constant-range!");
142 return Range;
143 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000144
Philip Reames1baaef12016-12-06 03:01:08 +0000145private:
Philip Reames71a49672016-12-07 01:03:56 +0000146 void markOverdefined() {
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000147 if (isOverdefined())
Philip Reames71a49672016-12-07 01:03:56 +0000148 return;
Owen Andersonc3a14132010-08-05 22:10:46 +0000149 Tag = overdefined;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000150 }
151
Philip Reames71a49672016-12-07 01:03:56 +0000152 void markConstant(Constant *V) {
Nick Lewycky11678bd2010-12-15 18:57:18 +0000153 assert(V && "Marking constant with NULL");
Philip Reames71a49672016-12-07 01:03:56 +0000154 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
155 markConstantRange(ConstantRange(CI->getValue()));
156 return;
157 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000158 if (isa<UndefValue>(V))
Philip Reames71a49672016-12-07 01:03:56 +0000159 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000160
161 assert((!isConstant() || getConstant() == V) &&
162 "Marking constant with different value");
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000163 assert(isUndefined());
Owen Andersonc3a14132010-08-05 22:10:46 +0000164 Tag = constant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000165 Val = V;
Chris Lattner19019ea2009-11-11 22:48:44 +0000166 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000167
Philip Reames71a49672016-12-07 01:03:56 +0000168 void markNotConstant(Constant *V) {
Chris Lattner565ee2f2009-11-12 04:36:58 +0000169 assert(V && "Marking constant with NULL");
Philip Reames71a49672016-12-07 01:03:56 +0000170 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
171 markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
172 return;
173 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000174 if (isa<UndefValue>(V))
Philip Reames71a49672016-12-07 01:03:56 +0000175 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000176
177 assert((!isConstant() || getConstant() != V) &&
178 "Marking constant !constant with same value");
179 assert((!isNotConstant() || getNotConstant() == V) &&
180 "Marking !constant with different value");
181 assert(isUndefined() || isConstant());
182 Tag = notconstant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000183 Val = V;
Chris Lattner565ee2f2009-11-12 04:36:58 +0000184 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000185
Philip Reames71a49672016-12-07 01:03:56 +0000186 void markConstantRange(ConstantRange NewR) {
Owen Anderson0f306a42010-08-05 22:59:19 +0000187 if (isConstantRange()) {
188 if (NewR.isEmptySet())
Philip Reames71a49672016-12-07 01:03:56 +0000189 markOverdefined();
190 else {
Philip Reames71a49672016-12-07 01:03:56 +0000191 Range = std::move(NewR);
192 }
193 return;
Owen Anderson0f306a42010-08-05 22:59:19 +0000194 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000195
Owen Anderson0f306a42010-08-05 22:59:19 +0000196 assert(isUndefined());
197 if (NewR.isEmptySet())
Philip Reames71a49672016-12-07 01:03:56 +0000198 markOverdefined();
199 else {
200 Tag = constantrange;
201 Range = std::move(NewR);
202 }
Owen Anderson0f306a42010-08-05 22:59:19 +0000203 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000204
Philip Reamesb2949622016-12-06 02:54:16 +0000205public:
206
Sanjay Patel2a385e22015-01-09 16:47:20 +0000207 /// Merge the specified lattice value into this one, updating this
Chris Lattner19019ea2009-11-11 22:48:44 +0000208 /// one and returning true if anything changed.
Philip Reamesb47a7192016-12-07 00:54:21 +0000209 void mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
210 if (RHS.isUndefined() || isOverdefined())
211 return;
212 if (RHS.isOverdefined()) {
213 markOverdefined();
214 return;
215 }
Chris Lattner19019ea2009-11-11 22:48:44 +0000216
Nick Lewycky11678bd2010-12-15 18:57:18 +0000217 if (isUndefined()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000218 *this = RHS;
219 return;
Chris Lattner22db4b52009-11-12 04:57:13 +0000220 }
221
Nick Lewycky11678bd2010-12-15 18:57:18 +0000222 if (isConstant()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000223 if (RHS.isConstant() && Val == RHS.Val)
224 return;
225 markOverdefined();
226 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000227 }
228
229 if (isNotConstant()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000230 if (RHS.isNotConstant() && Val == RHS.Val)
231 return;
232 markOverdefined();
233 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000234 }
235
236 assert(isConstantRange() && "New LVILattice type?");
Philip Reames02bb6a62016-12-07 04:48:50 +0000237 if (!RHS.isConstantRange()) {
238 // We can get here if we've encountered a constantexpr of integer type
239 // and merge it with a constantrange.
240 markOverdefined();
241 return;
242 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000243 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
244 if (NewR.isFullSet())
Philip Reamesb47a7192016-12-07 00:54:21 +0000245 markOverdefined();
246 else
247 markConstantRange(NewR);
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000248 }
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000249};
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000250
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000251} // end anonymous namespace.
252
Chris Lattner19019ea2009-11-11 22:48:44 +0000253namespace llvm {
Chandler Carruth2b1ba482011-04-18 18:49:44 +0000254raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
255 LLVM_ATTRIBUTE_USED;
Chris Lattner19019ea2009-11-11 22:48:44 +0000256raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
257 if (Val.isUndefined())
258 return OS << "undefined";
259 if (Val.isOverdefined())
260 return OS << "overdefined";
Chris Lattner565ee2f2009-11-12 04:36:58 +0000261
262 if (Val.isNotConstant())
263 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Davide Italianobd543d02016-05-25 22:29:34 +0000264 if (Val.isConstantRange())
Owen Anderson8afac042010-08-09 20:50:46 +0000265 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
266 << Val.getConstantRange().getUpper() << '>';
Chris Lattner19019ea2009-11-11 22:48:44 +0000267 return OS << "constant<" << *Val.getConstant() << '>';
268}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000269}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000270
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000271/// Returns true if this lattice value represents at most one possible value.
272/// This is as precise as any lattice value can get while still representing
273/// reachable code.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000274static bool hasSingleValue(const LVILatticeVal &Val) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000275 if (Val.isConstantRange() &&
276 Val.getConstantRange().isSingleElement())
277 // Integer constants are single element ranges
278 return true;
279 if (Val.isConstant())
280 // Non integer constants
281 return true;
282 return false;
283}
284
285/// Combine two sets of facts about the same value into a single set of
286/// facts. Note that this method is not suitable for merging facts along
287/// different paths in a CFG; that's what the mergeIn function is for. This
288/// is for merging facts gathered about the same value at the same location
289/// through two independent means.
290/// Notes:
291/// * This method does not promise to return the most precise possible lattice
292/// value implied by A and B. It is allowed to return any lattice element
293/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000294/// conflict, see below).
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000295/// * Due to unreachable code, the intersection of two lattice values could be
296/// contradictory. If this happens, we return some valid lattice value so as
297/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
298/// we do not make this guarantee. TODO: This would be a useful enhancement.
299static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) {
300 // Undefined is the strongest state. It means the value is known to be along
301 // an unreachable path.
302 if (A.isUndefined())
303 return A;
304 if (B.isUndefined())
305 return B;
306
307 // If we gave up for one, but got a useable fact from the other, use it.
308 if (A.isOverdefined())
309 return B;
310 if (B.isOverdefined())
311 return A;
312
313 // Can't get any more precise than constants.
314 if (hasSingleValue(A))
315 return A;
316 if (hasSingleValue(B))
317 return B;
318
319 // Could be either constant range or not constant here.
320 if (!A.isConstantRange() || !B.isConstantRange()) {
321 // TODO: Arbitrary choice, could be improved
322 return A;
323 }
324
325 // Intersect two constant ranges
326 ConstantRange Range =
327 A.getConstantRange().intersectWith(B.getConstantRange());
328 // Note: An empty range is implicitly converted to overdefined internally.
329 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000330 // and thus know this path must be unreachable.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000331 return LVILatticeVal::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000332}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000333
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000334//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000335// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000336//===----------------------------------------------------------------------===//
337
Chris Lattneraf025d32009-11-15 19:59:49 +0000338namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000339 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000340 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000341 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000342 // Needs to access getValPtr(), which is protected.
343 friend struct DenseMapInfo<LVIValueHandle>;
344
Owen Anderson118ac802011-01-05 21:15:29 +0000345 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000346
Owen Anderson118ac802011-01-05 21:15:29 +0000347 LVIValueHandle(Value *V, LazyValueInfoCache *P)
348 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000349
350 void deleted() override;
351 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000352 deleted();
353 }
354 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000355} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000356
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000357namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000358 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000359 /// maintains information about queries across the clients' queries.
360 class LazyValueInfoCache {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000361 /// This is all of the cached block information for exactly one Value*.
362 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000363 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000364 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
365 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000366 struct ValueCacheEntryTy {
367 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
368 LVIValueHandle Handle;
369 SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4> BlockVals;
370 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000371
Sanjay Patel2a385e22015-01-09 16:47:20 +0000372 /// This is all of the cached information for all values,
Owen Anderson6f060af2011-01-05 23:26:22 +0000373 /// mapped from Value* to key information.
Justin Lebar58b377e2016-07-27 22:33:36 +0000374 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000375
Sanjay Patel2a385e22015-01-09 16:47:20 +0000376 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000377 /// over-defined at the end of that block.
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000378 typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>>
379 OverDefinedCacheTy;
380 OverDefinedCacheTy OverDefinedCache;
Benjamin Kramer36647082011-12-03 15:16:45 +0000381
Sanjay Patel2a385e22015-01-09 16:47:20 +0000382 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000383 /// don't spend time removing unused blocks from our caches.
384 DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
385
Philip Reames9db79482016-09-12 22:38:44 +0000386 public:
Hans Wennborg45172ac2014-11-25 17:23:05 +0000387 void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
388 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000389
390 // Insert over-defined values into their own cache to reduce memory
391 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000392 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000393 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-07-27 22:33:36 +0000394 else {
395 auto It = ValueCache.find_as(Val);
396 if (It == ValueCache.end()) {
397 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
398 It = ValueCache.find_as(Val);
399 assert(It != ValueCache.end() && "Val was just added to the map!");
400 }
401 It->second->BlockVals[BB] = Result;
402 }
Hans Wennborg45172ac2014-11-25 17:23:05 +0000403 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000404
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000405 bool isOverdefined(Value *V, BasicBlock *BB) const {
406 auto ODI = OverDefinedCache.find(BB);
407
408 if (ODI == OverDefinedCache.end())
409 return false;
410
411 return ODI->second.count(V);
412 }
413
Philip Reames9db79482016-09-12 22:38:44 +0000414 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000415 if (isOverdefined(V, BB))
416 return true;
417
Justin Lebar58b377e2016-07-27 22:33:36 +0000418 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000419 if (I == ValueCache.end())
420 return false;
421
Justin Lebar58b377e2016-07-27 22:33:36 +0000422 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000423 }
424
Philip Reames9db79482016-09-12 22:38:44 +0000425 LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000426 if (isOverdefined(V, BB))
427 return LVILatticeVal::getOverdefined();
428
Justin Lebar58b377e2016-07-27 22:33:36 +0000429 auto I = ValueCache.find_as(V);
430 if (I == ValueCache.end())
431 return LVILatticeVal();
432 auto BBI = I->second->BlockVals.find(BB);
433 if (BBI == I->second->BlockVals.end())
434 return LVILatticeVal();
435 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000436 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000437
Philip Reames92e5e1b2016-09-12 21:46:58 +0000438 /// clear - Empty the cache.
439 void clear() {
440 SeenBlocks.clear();
441 ValueCache.clear();
442 OverDefinedCache.clear();
443 }
444
Philip Reamesb627aec2016-09-12 22:03:36 +0000445 /// Inform the cache that a given value has been deleted.
446 void eraseValue(Value *V);
447
448 /// This is part of the update interface to inform the cache
449 /// that a block has been deleted.
450 void eraseBlock(BasicBlock *BB);
451
Philip Reames9db79482016-09-12 22:38:44 +0000452 /// Updates the cache to remove any influence an overdefined value in
453 /// OldSucc might have (unless also overdefined in NewSucc). This just
454 /// flushes elements from the cache and does not add any.
455 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
456
Philip Reames92e5e1b2016-09-12 21:46:58 +0000457 friend struct LVIValueHandle;
458 };
Philip Reamesb627aec2016-09-12 22:03:36 +0000459}
Philip Reames92e5e1b2016-09-12 21:46:58 +0000460
Philip Reamesb627aec2016-09-12 22:03:36 +0000461void LazyValueInfoCache::eraseValue(Value *V) {
462 SmallVector<AssertingVH<BasicBlock>, 4> ToErase;
463 for (auto &I : OverDefinedCache) {
464 SmallPtrSetImpl<Value *> &ValueSet = I.second;
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000465 ValueSet.erase(V);
Philip Reamesb627aec2016-09-12 22:03:36 +0000466 if (ValueSet.empty())
467 ToErase.push_back(I.first);
468 }
469 for (auto &BB : ToErase)
470 OverDefinedCache.erase(BB);
471
472 ValueCache.erase(V);
473}
474
475void LVIValueHandle::deleted() {
476 // This erasure deallocates *this, so it MUST happen after we're done
477 // using any and all members of *this.
478 Parent->eraseValue(*this);
479}
480
481void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
482 // Shortcut if we have never seen this block.
483 DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
484 if (I == SeenBlocks.end())
485 return;
486 SeenBlocks.erase(I);
487
488 auto ODI = OverDefinedCache.find(BB);
489 if (ODI != OverDefinedCache.end())
490 OverDefinedCache.erase(ODI);
491
492 for (auto &I : ValueCache)
493 I.second->BlockVals.erase(BB);
494}
495
Philip Reames9db79482016-09-12 22:38:44 +0000496void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
497 BasicBlock *NewSucc) {
498 // When an edge in the graph has been threaded, values that we could not
499 // determine a value for before (i.e. were marked overdefined) may be
500 // possible to solve now. We do NOT try to proactively update these values.
501 // Instead, we clear their entries from the cache, and allow lazy updating to
502 // recompute them when needed.
503
504 // The updating process is fairly simple: we need to drop cached info
505 // for all values that were marked overdefined in OldSucc, and for those same
506 // values in any successor of OldSucc (except NewSucc) in which they were
507 // also marked overdefined.
508 std::vector<BasicBlock*> worklist;
509 worklist.push_back(OldSucc);
510
511 auto I = OverDefinedCache.find(OldSucc);
512 if (I == OverDefinedCache.end())
513 return; // Nothing to process here.
514 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
515
516 // Use a worklist to perform a depth-first search of OldSucc's successors.
517 // NOTE: We do not need a visited list since any blocks we have already
518 // visited will have had their overdefined markers cleared already, and we
519 // thus won't loop to their successors.
520 while (!worklist.empty()) {
521 BasicBlock *ToUpdate = worklist.back();
522 worklist.pop_back();
523
524 // Skip blocks only accessible through NewSucc.
525 if (ToUpdate == NewSucc) continue;
526
Philip Reames1e48efc2016-12-30 17:56:47 +0000527 // If a value was marked overdefined in OldSucc, and is here too...
528 auto OI = OverDefinedCache.find(ToUpdate);
529 if (OI == OverDefinedCache.end())
530 continue;
531 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
532
Philip Reames9db79482016-09-12 22:38:44 +0000533 bool changed = false;
534 for (Value *V : ValsToClear) {
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000535 if (!ValueSet.erase(V))
Philip Reames9db79482016-09-12 22:38:44 +0000536 continue;
537
Philip Reames9db79482016-09-12 22:38:44 +0000538 // If we removed anything, then we potentially need to update
539 // blocks successors too.
540 changed = true;
Philip Reames1e48efc2016-12-30 17:56:47 +0000541
542 if (ValueSet.empty()) {
543 OverDefinedCache.erase(OI);
544 break;
545 }
Philip Reames9db79482016-09-12 22:38:44 +0000546 }
547
548 if (!changed) continue;
549
550 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
551 }
552}
553
Philip Reamesb627aec2016-09-12 22:03:36 +0000554namespace {
Philip Reames92e5e1b2016-09-12 21:46:58 +0000555 // The actual implementation of the lazy analysis and update. Note that the
556 // inheritance from LazyValueInfoCache is intended to be temporary while
557 // splitting the code and then transitioning to a has-a relationship.
Philip Reames9db79482016-09-12 22:38:44 +0000558 class LazyValueInfoImpl {
559
560 /// Cached results from previous queries
561 LazyValueInfoCache TheCache;
Philip Reames92e5e1b2016-09-12 21:46:58 +0000562
563 /// This stack holds the state of the value solver during a query.
564 /// It basically emulates the callstack of the naive
565 /// recursive value lookup process.
566 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
567
568 /// Keeps track of which block-value pairs are in BlockValueStack.
569 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
570
571 /// Push BV onto BlockValueStack unless it's already in there.
572 /// Returns true on success.
573 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
574 if (!BlockValueSet.insert(BV).second)
575 return false; // It's already in the stack.
576
577 DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
578 << "\n");
579 BlockValueStack.push(BV);
580 return true;
581 }
582
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000583 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
Philip Reames92e5e1b2016-09-12 21:46:58 +0000584 const DataLayout &DL; ///< A mandatory DataLayout
585 DominatorTree *DT; ///< An optional DT pointer.
586
587 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
588 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
589 LVILatticeVal &Result, Instruction *CxtI = nullptr);
590 bool hasBlockValue(Value *Val, BasicBlock *BB);
591
592 // These methods process one work item and may add more. A false value
593 // returned means that the work item was not completely processed and must
594 // be revisited after going through the new items.
595 bool solveBlockValue(Value *Val, BasicBlock *BB);
Philip Reames05c435e2016-12-06 03:22:03 +0000596 bool solveBlockValueImpl(LVILatticeVal &Res, Value *Val, BasicBlock *BB);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000597 bool solveBlockValueNonLocal(LVILatticeVal &BBLV, Value *Val, BasicBlock *BB);
598 bool solveBlockValuePHINode(LVILatticeVal &BBLV, PHINode *PN, BasicBlock *BB);
599 bool solveBlockValueSelect(LVILatticeVal &BBLV, SelectInst *S,
600 BasicBlock *BB);
601 bool solveBlockValueBinaryOp(LVILatticeVal &BBLV, Instruction *BBI,
602 BasicBlock *BB);
603 bool solveBlockValueCast(LVILatticeVal &BBLV, Instruction *BBI,
604 BasicBlock *BB);
605 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
606 LVILatticeVal &BBLV,
607 Instruction *BBI);
608
609 void solve();
610
611 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000612 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000613 /// value for the specified Value* at the end of the specified block.
Hal Finkel7e184492014-09-07 20:29:59 +0000614 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
615 Instruction *CxtI = nullptr);
616
Sanjay Patel2a385e22015-01-09 16:47:20 +0000617 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000618 /// value for the specified Value* at the specified instruction (generally
619 /// from an assume intrinsic).
620 LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000621
Sanjay Patel2a385e22015-01-09 16:47:20 +0000622 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000623 /// value for the specified Value* that is true on the specified edge.
Hal Finkel7e184492014-09-07 20:29:59 +0000624 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
625 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000626
Philip Reames9db79482016-09-12 22:38:44 +0000627 /// Complete flush all previously computed values
628 void clear() {
629 TheCache.clear();
630 }
631
632 /// This is part of the update interface to inform the cache
633 /// that a block has been deleted.
634 void eraseBlock(BasicBlock *BB) {
635 TheCache.eraseBlock(BB);
636 }
637
Sanjay Patel2a385e22015-01-09 16:47:20 +0000638 /// This is the update interface to inform the cache that an edge from
639 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000640 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000641
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000642 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
643 DominatorTree *DT = nullptr)
644 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000645 };
646} // end anonymous namespace
647
Philip Reames92e5e1b2016-09-12 21:46:58 +0000648void LazyValueInfoImpl::solve() {
Owen Anderson6f060af2011-01-05 23:26:22 +0000649 while (!BlockValueStack.empty()) {
650 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000651 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
652
Nuno Lopese6e04902012-06-28 01:16:18 +0000653 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000654 // The work item was completely processed.
655 assert(BlockValueStack.top() == e && "Nothing should have been pushed!");
Philip Reames9db79482016-09-12 22:38:44 +0000656 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000657 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000658
Philip Reames44456b82016-02-02 03:15:40 +0000659 DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000660 << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000661
Owen Anderson6f060af2011-01-05 23:26:22 +0000662 BlockValueStack.pop();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000663 BlockValueSet.erase(e);
664 } else {
665 // More work needs to be done before revisiting.
666 assert(BlockValueStack.top() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000667 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000668 }
669}
670
Philip Reames92e5e1b2016-09-12 21:46:58 +0000671bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000672 // If already a constant, there is nothing to compute.
673 if (isa<Constant>(Val))
674 return true;
675
Philip Reames9db79482016-09-12 22:38:44 +0000676 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000677}
678
Philip Reames92e5e1b2016-09-12 21:46:58 +0000679LVILatticeVal LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000680 // If already a constant, there is nothing to compute.
681 if (Constant *VC = dyn_cast<Constant>(Val))
682 return LVILatticeVal::get(VC);
683
Philip Reames9db79482016-09-12 22:38:44 +0000684 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000685}
686
Philip Reameseb3e9da2015-10-29 03:57:17 +0000687static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
688 switch (BBI->getOpcode()) {
689 default: break;
690 case Instruction::Load:
691 case Instruction::Call:
692 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000693 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000694 if (isa<IntegerType>(BBI->getType())) {
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000695 return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000696 }
697 break;
698 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000699 // Nothing known - will be intersected with other facts
700 return LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000701}
702
Philip Reames92e5e1b2016-09-12 21:46:58 +0000703bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000704 if (isa<Constant>(Val))
705 return true;
706
Philip Reames9db79482016-09-12 22:38:44 +0000707 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000708 // If we have a cached value, use that.
709 DEBUG(dbgs() << " reuse BB '" << BB->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000710 << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000711
Hans Wennborg45172ac2014-11-25 17:23:05 +0000712 // Since we're reusing a cached value, we don't need to update the
713 // OverDefinedCache. The cache will have been properly updated whenever the
714 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000715 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000716 }
717
Hans Wennborg45172ac2014-11-25 17:23:05 +0000718 // Hold off inserting this value into the Cache in case we have to return
719 // false and come back later.
720 LVILatticeVal Res;
Philip Reames05c435e2016-12-06 03:22:03 +0000721 if (!solveBlockValueImpl(Res, Val, BB))
722 // Work pushed, will revisit
723 return false;
724
725 TheCache.insertResult(Val, BB, Res);
726 return true;
727}
728
729bool LazyValueInfoImpl::solveBlockValueImpl(LVILatticeVal &Res,
730 Value *Val, BasicBlock *BB) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000731
Chris Lattneraf025d32009-11-15 19:59:49 +0000732 Instruction *BBI = dyn_cast<Instruction>(Val);
Philip Reames05c435e2016-12-06 03:22:03 +0000733 if (!BBI || BBI->getParent() != BB)
734 return solveBlockValueNonLocal(Res, Val, BB);
Chris Lattner2c708562009-11-15 20:00:52 +0000735
Philip Reames05c435e2016-12-06 03:22:03 +0000736 if (PHINode *PN = dyn_cast<PHINode>(BBI))
737 return solveBlockValuePHINode(Res, PN, BB);
Owen Anderson80d19f02010-08-18 21:11:37 +0000738
Philip Reames05c435e2016-12-06 03:22:03 +0000739 if (auto *SI = dyn_cast<SelectInst>(BBI))
740 return solveBlockValueSelect(Res, SI, BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000741
Philip Reames2ab964e2016-04-27 01:02:25 +0000742 // If this value is a nonnull pointer, record it's range and bailout. Note
743 // that for all other pointer typed values, we terminate the search at the
744 // definition. We could easily extend this to look through geps, bitcasts,
745 // and the like to prove non-nullness, but it's not clear that's worth it
746 // compile time wise. The context-insensative value walk done inside
747 // isKnownNonNull gets most of the profitable cases at much less expense.
748 // This does mean that we have a sensativity to where the defining
749 // instruction is placed, even if it could legally be hoisted much higher.
750 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000751 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
752 if (PT && isKnownNonNull(BBI)) {
753 Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000754 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000755 }
Davide Italianobd543d02016-05-25 22:29:34 +0000756 if (BBI->getType()->isIntegerTy()) {
Philip Reames05c435e2016-12-06 03:22:03 +0000757 if (isa<CastInst>(BBI))
758 return solveBlockValueCast(Res, BBI, BB);
759
Philip Reames2ab964e2016-04-27 01:02:25 +0000760 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
Philip Reames05c435e2016-12-06 03:22:03 +0000761 if (BO && isa<ConstantInt>(BO->getOperand(1)))
762 return solveBlockValueBinaryOp(Res, BBI, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000763 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000764
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000765 DEBUG(dbgs() << " compute BB '" << BB->getName()
766 << "' - unknown inst def found.\n");
767 Res = getFromRangeMetadata(BBI);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000768 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000769}
770
771static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
772 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
773 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000774 GetUnderlyingObject(L->getPointerOperand(),
775 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000776 }
777 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
778 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000779 GetUnderlyingObject(S->getPointerOperand(),
780 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000781 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000782 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
783 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000784
785 // FIXME: check whether it has a valuerange that excludes zero?
786 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
787 if (!Len || Len->isZero()) return false;
788
Eli Friedman7a5fc692011-05-31 20:40:16 +0000789 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000790 if (GetUnderlyingObject(MI->getRawDest(),
791 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000792 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000793 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000794 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000795 if (GetUnderlyingObject(MTI->getRawSource(),
796 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000797 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000798 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000799 return false;
800}
801
Philip Reames3f83dbe2016-04-27 00:30:55 +0000802/// Return true if the allocation associated with Val is ever dereferenced
803/// within the given basic block. This establishes the fact Val is not null,
804/// but does not imply that the memory at Val is dereferenceable. (Val may
805/// point off the end of the dereferenceable part of the object.)
806static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
807 assert(Val->getType()->isPointerTy());
808
809 const DataLayout &DL = BB->getModule()->getDataLayout();
810 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
811 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
812 // inside InstructionDereferencesPointer either.
813 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
814 for (Instruction &I : *BB)
815 if (InstructionDereferencesPointer(&I, UnderlyingVal))
816 return true;
817 return false;
818}
819
Philip Reames92e5e1b2016-09-12 21:46:58 +0000820bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000821 Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000822 LVILatticeVal Result; // Start Undefined.
823
Nick Lewycky55a700b2010-12-18 01:00:40 +0000824 // If this is the entry block, we must be asking about an argument. The
825 // value is overdefined.
826 if (BB == &BB->getParent()->getEntryBlock()) {
827 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Philip Reames3f83dbe2016-04-27 00:30:55 +0000828 // Bofore giving up, see if we can prove the pointer non-null local to
829 // this particular block.
830 if (Val->getType()->isPointerTy() &&
831 (isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
Chris Lattner229907c2011-07-18 04:54:35 +0000832 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000833 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
834 } else {
Philip Reames1baaef12016-12-06 03:01:08 +0000835 Result = LVILatticeVal::getOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000836 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000837 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000838 return true;
839 }
840
841 // Loop over all of our predecessors, merging what we know from them into
842 // result.
843 bool EdgesMissing = false;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000844 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000845 LVILatticeVal EdgeResult;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000846 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000847 if (EdgesMissing)
848 continue;
849
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000850 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000851
852 // If we hit overdefined, exit early. The BlockVals entry is already set
853 // to overdefined.
854 if (Result.isOverdefined()) {
855 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000856 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000857 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000858 // this particular block.
859 if (Val->getType()->isPointerTy() &&
860 isObjectDereferencedInBlock(Val, BB)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000861 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000862 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
863 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000864
Owen Anderson64c2c572010-12-20 18:18:16 +0000865 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000866 return true;
867 }
868 }
869 if (EdgesMissing)
870 return false;
871
872 // Return the merged value, which is more precise than 'overdefined'.
873 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000874 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000875 return true;
876}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000877
Philip Reames92e5e1b2016-09-12 21:46:58 +0000878bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000879 PHINode *PN, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000880 LVILatticeVal Result; // Start Undefined.
881
882 // Loop over all of our predecessors, merging what we know from them into
883 // result.
884 bool EdgesMissing = false;
885 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
886 BasicBlock *PhiBB = PN->getIncomingBlock(i);
887 Value *PhiVal = PN->getIncomingValue(i);
888 LVILatticeVal EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000889 // Note that we can provide PN as the context value to getEdgeValue, even
890 // though the results will be cached, because PN is the value being used as
891 // the cache key in the caller.
Hal Finkel7e184492014-09-07 20:29:59 +0000892 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000893 if (EdgesMissing)
894 continue;
895
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000896 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000897
898 // If we hit overdefined, exit early. The BlockVals entry is already set
899 // to overdefined.
900 if (Result.isOverdefined()) {
901 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000902 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000903
Owen Anderson64c2c572010-12-20 18:18:16 +0000904 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000905 return true;
906 }
907 }
908 if (EdgesMissing)
909 return false;
910
911 // Return the merged value, which is more precise than 'overdefined'.
912 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000913 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000914 return true;
915}
916
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000917static LVILatticeVal getValueFromCondition(Value *Val, Value *Cond,
918 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000919
Philip Reamesd1f829d2016-02-02 21:57:37 +0000920// If we can determine a constraint on the value given conditions assumed by
921// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000922void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000923 Value *Val, LVILatticeVal &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000924 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
925 if (!BBI)
926 return;
927
Hal Finkel8a9a7832017-01-11 13:24:24 +0000928 for (auto &AssumeVH : AC->assumptionsFor(Val)) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000929 if (!AssumeVH)
Chandler Carruth66b31302015-01-04 12:03:27 +0000930 continue;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000931 auto *I = cast<CallInst>(AssumeVH);
932 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000933 continue;
934
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000935 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +0000936 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000937
938 // If guards are not used in the module, don't spend time looking for them
939 auto *GuardDecl = BBI->getModule()->getFunction(
940 Intrinsic::getName(Intrinsic::experimental_guard));
941 if (!GuardDecl || GuardDecl->use_empty())
942 return;
943
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000944 for (Instruction &I : make_range(BBI->getIterator().getReverse(),
945 BBI->getParent()->rend())) {
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000946 Value *Cond = nullptr;
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000947 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
948 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000949 }
Hal Finkel7e184492014-09-07 20:29:59 +0000950}
951
Philip Reames92e5e1b2016-09-12 21:46:58 +0000952bool LazyValueInfoImpl::solveBlockValueSelect(LVILatticeVal &BBLV,
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000953 SelectInst *SI, BasicBlock *BB) {
954
955 // Recurse on our inputs if needed
956 if (!hasBlockValue(SI->getTrueValue(), BB)) {
957 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
958 return false;
Philip Reames1baaef12016-12-06 03:01:08 +0000959 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000960 return true;
961 }
962 LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
963 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
964 // extra slots in the table if we can.
965 if (TrueVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +0000966 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000967 return true;
968 }
969
970 if (!hasBlockValue(SI->getFalseValue(), BB)) {
971 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
972 return false;
Philip Reames1baaef12016-12-06 03:01:08 +0000973 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000974 return true;
975 }
976 LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
977 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
978 // extra slots in the table if we can.
979 if (FalseVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +0000980 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000981 return true;
982 }
983
Philip Reamesadf0e352016-02-26 22:53:59 +0000984 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
985 ConstantRange TrueCR = TrueVal.getConstantRange();
986 ConstantRange FalseCR = FalseVal.getConstantRange();
987 Value *LHS = nullptr;
988 Value *RHS = nullptr;
989 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
990 // Is this a min specifically of our two inputs? (Avoid the risk of
991 // ValueTracking getting smarter looking back past our immediate inputs.)
992 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
993 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reamesb2949622016-12-06 02:54:16 +0000994 ConstantRange ResultCR = [&]() {
995 switch (SPR.Flavor) {
996 default:
997 llvm_unreachable("unexpected minmax type!");
998 case SPF_SMIN: /// Signed minimum
999 return TrueCR.smin(FalseCR);
1000 case SPF_UMIN: /// Unsigned minimum
1001 return TrueCR.umin(FalseCR);
1002 case SPF_SMAX: /// Signed maximum
1003 return TrueCR.smax(FalseCR);
1004 case SPF_UMAX: /// Unsigned maximum
1005 return TrueCR.umax(FalseCR);
1006 };
1007 }();
1008 BBLV = LVILatticeVal::getRange(ResultCR);
1009 return true;
Philip Reamesadf0e352016-02-26 22:53:59 +00001010 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001011
Philip Reamesadf0e352016-02-26 22:53:59 +00001012 // TODO: ABS, NABS from the SelectPatternResult
1013 }
1014
Philip Reames854a84c2016-02-12 00:09:18 +00001015 // Can we constrain the facts about the true and false values by using the
1016 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
1017 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001018 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001019 TrueVal = intersect(TrueVal,
1020 getValueFromCondition(SI->getTrueValue(), Cond, true));
1021 FalseVal = intersect(FalseVal,
1022 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +00001023
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001024 // Handle clamp idioms such as:
1025 // %24 = constantrange<0, 17>
1026 // %39 = icmp eq i32 %24, 0
1027 // %40 = add i32 %24, -1
1028 // %siv.next = select i1 %39, i32 16, i32 %40
1029 // %siv.next = constantrange<0, 17> not <-1, 17>
1030 // In general, this can handle any clamp idiom which tests the edge
1031 // condition via an equality or inequality.
1032 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +00001033 ICmpInst::Predicate Pred = ICI->getPredicate();
1034 Value *A = ICI->getOperand(0);
1035 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
1036 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
1037 assert(A->getType() == B->getType());
1038 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
1039 };
1040 // See if either input is A + C2, subject to the constraint from the
1041 // condition that A != C when that input is used. We can assume that
1042 // that input doesn't include C + C2.
1043 ConstantInt *CIAdded;
1044 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +00001045 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +00001046 case ICmpInst::ICMP_EQ:
1047 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
1048 m_ConstantInt(CIAdded)))) {
1049 auto ResNot = addConstants(CIBase, CIAdded);
1050 FalseVal = intersect(FalseVal,
1051 LVILatticeVal::getNot(ResNot));
1052 }
1053 break;
1054 case ICmpInst::ICMP_NE:
1055 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
1056 m_ConstantInt(CIAdded)))) {
1057 auto ResNot = addConstants(CIBase, CIAdded);
1058 TrueVal = intersect(TrueVal,
1059 LVILatticeVal::getNot(ResNot));
1060 }
1061 break;
1062 };
1063 }
1064 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001065
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001066 LVILatticeVal Result; // Start Undefined.
1067 Result.mergeIn(TrueVal, DL);
1068 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001069 BBLV = Result;
1070 return true;
1071}
1072
Philip Reames92e5e1b2016-09-12 21:46:58 +00001073bool LazyValueInfoImpl::solveBlockValueCast(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001074 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001075 BasicBlock *BB) {
1076 if (!BBI->getOperand(0)->getType()->isSized()) {
1077 // Without knowing how wide the input is, we can't analyze it in any useful
1078 // way.
Philip Reames1baaef12016-12-06 03:01:08 +00001079 BBLV = LVILatticeVal::getOverdefined();
Philip Reamese5030e82016-04-26 22:52:30 +00001080 return true;
1081 }
Philip Reamesf105db42016-04-26 23:27:33 +00001082
1083 // Filter out casts we don't know how to reason about before attempting to
1084 // recurse on our operand. This can cut a long search short if we know we're
1085 // not going to be able to get any useful information anways.
1086 switch (BBI->getOpcode()) {
1087 case Instruction::Trunc:
1088 case Instruction::SExt:
1089 case Instruction::ZExt:
1090 case Instruction::BitCast:
1091 break;
1092 default:
1093 // Unhandled instructions are overdefined.
1094 DEBUG(dbgs() << " compute BB '" << BB->getName()
1095 << "' - overdefined (unknown cast).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001096 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001097 return true;
1098 }
1099
Philip Reames38c87c22016-04-26 21:48:16 +00001100 // Figure out the range of the LHS. If that fails, we still apply the
1101 // transfer rule on the full set since we may be able to locally infer
1102 // interesting facts.
1103 if (!hasBlockValue(BBI->getOperand(0), BB))
Hans Wennborg45172ac2014-11-25 17:23:05 +00001104 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
Philip Reames38c87c22016-04-26 21:48:16 +00001105 // More work to do before applying this transfer rule.
Hans Wennborg45172ac2014-11-25 17:23:05 +00001106 return false;
Philip Reames38c87c22016-04-26 21:48:16 +00001107
1108 const unsigned OperandBitWidth =
Philip Reamese5030e82016-04-26 22:52:30 +00001109 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
Philip Reames38c87c22016-04-26 21:48:16 +00001110 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1111 if (hasBlockValue(BBI->getOperand(0), BB)) {
1112 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001113 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1114 BBI);
Philip Reames38c87c22016-04-26 21:48:16 +00001115 if (LHSVal.isConstantRange())
1116 LHSRange = LHSVal.getConstantRange();
Nick Lewycky55a700b2010-12-18 01:00:40 +00001117 }
1118
Philip Reames38c87c22016-04-26 21:48:16 +00001119 const unsigned ResultBitWidth =
1120 cast<IntegerType>(BBI->getType())->getBitWidth();
Philip Reames66715772016-04-25 18:30:31 +00001121
1122 // NOTE: We're currently limited by the set of operations that ConstantRange
1123 // can evaluate symbolically. Enhancing that set will allows us to analyze
1124 // more definitions.
Philip Reames4d00af12016-12-01 20:08:47 +00001125 auto CastOp = (Instruction::CastOps) BBI->getOpcode();
Philip Reames0e613f72016-12-06 02:36:58 +00001126 BBLV = LVILatticeVal::getRange(LHSRange.castOp(CastOp, ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001127 return true;
1128}
1129
Philip Reames92e5e1b2016-09-12 21:46:58 +00001130bool LazyValueInfoImpl::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001131 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001132 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +00001133
Philip Reames053c2a62016-04-26 23:10:35 +00001134 assert(BBI->getOperand(0)->getType()->isSized() &&
1135 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +00001136
1137 // Filter out operators we don't know how to reason about before attempting to
1138 // recurse on our operand(s). This can cut a long search short if we know
1139 // we're not going to be able to get any useful information anways.
1140 switch (BBI->getOpcode()) {
1141 case Instruction::Add:
1142 case Instruction::Sub:
1143 case Instruction::Mul:
1144 case Instruction::UDiv:
1145 case Instruction::Shl:
1146 case Instruction::LShr:
1147 case Instruction::And:
1148 case Instruction::Or:
1149 // continue into the code below
1150 break;
1151 default:
1152 // Unhandled instructions are overdefined.
1153 DEBUG(dbgs() << " compute BB '" << BB->getName()
1154 << "' - overdefined (unknown binary operator).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001155 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001156 return true;
1157 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001158
Philip Reames053c2a62016-04-26 23:10:35 +00001159 // Figure out the range of the LHS. If that fails, use a conservative range,
1160 // but apply the transfer rule anyways. This lets us pick up facts from
1161 // expressions like "and i32 (call i32 @foo()), 32"
1162 if (!hasBlockValue(BBI->getOperand(0), BB))
1163 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
1164 // More work to do before applying this transfer rule.
1165 return false;
1166
1167 const unsigned OperandBitWidth =
1168 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
1169 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1170 if (hasBlockValue(BBI->getOperand(0), BB)) {
1171 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001172 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1173 BBI);
Philip Reames053c2a62016-04-26 23:10:35 +00001174 if (LHSVal.isConstantRange())
1175 LHSRange = LHSVal.getConstantRange();
Philip Reames66715772016-04-25 18:30:31 +00001176 }
Philip Reames66715772016-04-25 18:30:31 +00001177
1178 ConstantInt *RHS = cast<ConstantInt>(BBI->getOperand(1));
1179 ConstantRange RHSRange = ConstantRange(RHS->getValue());
1180
Owen Anderson80d19f02010-08-18 21:11:37 +00001181 // NOTE: We're currently limited by the set of operations that ConstantRange
1182 // can evaluate symbolically. Enhancing that set will allows us to analyze
1183 // more definitions.
Philip Reames4d00af12016-12-01 20:08:47 +00001184 auto BinOp = (Instruction::BinaryOps) BBI->getOpcode();
Philip Reames0e613f72016-12-06 02:36:58 +00001185 BBLV = LVILatticeVal::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001186 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001187}
1188
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001189static LVILatticeVal getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1190 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001191 Value *LHS = ICI->getOperand(0);
1192 Value *RHS = ICI->getOperand(1);
1193 CmpInst::Predicate Predicate = ICI->getPredicate();
1194
1195 if (isa<Constant>(RHS)) {
1196 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001197 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001198 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001199 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001200 return LVILatticeVal::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001201 else
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001202 return LVILatticeVal::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001203 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001204 }
Hal Finkel7e184492014-09-07 20:29:59 +00001205
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001206 if (!Val->getType()->isIntegerTy())
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001207 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001208
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001209 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1210 // range of Val guaranteed by the condition. Recognize comparisons in the from
1211 // of:
1212 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001213 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001214 // The latter is the range checking idiom that InstCombine produces. Subtract
1215 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001216
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001217 // Val or (add Val, Offset) can be on either hand of the comparison
1218 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1219 std::swap(LHS, RHS);
1220 Predicate = CmpInst::getSwappedPredicate(Predicate);
1221 }
Hal Finkel7e184492014-09-07 20:29:59 +00001222
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001223 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001224 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001225 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001226
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001227 if (LHS == Val || Offset) {
1228 // Calculate the range of values that are allowed by the comparison
1229 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1230 /*isFullSet=*/true);
1231 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1232 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001233 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1234 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1235 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001236
1237 // If we're interested in the false dest, invert the condition
1238 CmpInst::Predicate Pred =
1239 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1240 ConstantRange TrueValues =
1241 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1242
1243 if (Offset) // Apply the offset from above.
1244 TrueValues = TrueValues.subtract(Offset->getValue());
1245
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001246 return LVILatticeVal::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001247 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001248
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001249 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001250}
1251
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001252static LVILatticeVal
1253getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1254 DenseMap<Value*, LVILatticeVal> &Visited);
1255
1256static LVILatticeVal
1257getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
1258 DenseMap<Value*, LVILatticeVal> &Visited) {
1259 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1260 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1261
1262 // Handle conditions in the form of (cond1 && cond2), we know that on the
1263 // true dest path both of the conditions hold.
1264 if (!isTrueDest)
1265 return LVILatticeVal::getOverdefined();
1266
1267 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
1268 if (!BO || BO->getOpcode() != BinaryOperator::And)
1269 return LVILatticeVal::getOverdefined();
1270
1271 auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
1272 auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
1273 return intersect(RHS, LHS);
1274}
1275
1276static LVILatticeVal
1277getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1278 DenseMap<Value*, LVILatticeVal> &Visited) {
1279 auto I = Visited.find(Cond);
1280 if (I != Visited.end())
1281 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001282
1283 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1284 Visited[Cond] = Result;
1285 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001286}
1287
1288LVILatticeVal getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest) {
1289 assert(Cond && "precondition");
1290 DenseMap<Value*, LVILatticeVal> Visited;
1291 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1292}
1293
Nuno Lopese6e04902012-06-28 01:16:18 +00001294/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001295/// Val is not constrained on the edge. Result is unspecified if return value
1296/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001297static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1298 BasicBlock *BBTo, LVILatticeVal &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001299 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001300 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001301 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1302 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001303 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001304 if (BI->isConditional() &&
1305 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1306 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1307 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1308 "BBTo isn't a successor of BBFrom");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001309
Chris Lattner19019ea2009-11-11 22:48:44 +00001310 // If V is the condition of the branch itself, then we know exactly what
1311 // it is.
Nick Lewycky55a700b2010-12-18 01:00:40 +00001312 if (BI->getCondition() == Val) {
1313 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001314 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001315 return true;
1316 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001317
Chris Lattner19019ea2009-11-11 22:48:44 +00001318 // If the condition of the branch is an equality comparison, we may be
1319 // able to infer the value.
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001320 Result = getValueFromCondition(Val, BI->getCondition(), isTrueDest);
1321 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001322 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001323 }
1324 }
Chris Lattner77358782009-11-15 20:02:12 +00001325
1326 // If the edge was formed by a switch on the value, then we may know exactly
1327 // what it is.
1328 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001329 if (SI->getCondition() != Val)
1330 return false;
1331
1332 bool DefaultCase = SI->getDefaultDest() == BBTo;
1333 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1334 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1335
Hans Wennborgcbb18e32014-11-21 19:07:46 +00001336 for (SwitchInst::CaseIt i : SI->cases()) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001337 ConstantRange EdgeVal(i.getCaseValue()->getValue());
Manman Renf3fedb62012-09-05 23:45:58 +00001338 if (DefaultCase) {
1339 // It is possible that the default destination is the destination of
1340 // some cases. There is no need to perform difference for those cases.
1341 if (i.getCaseSuccessor() != BBTo)
1342 EdgesVals = EdgesVals.difference(EdgeVal);
1343 } else if (i.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001344 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001345 }
Benjamin Kramer2337c1f2016-02-20 10:40:34 +00001346 Result = LVILatticeVal::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001347 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001348 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001349 return false;
1350}
1351
Sanjay Patel938e2792015-01-09 16:35:37 +00001352/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1353/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001354bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Hal Finkel7e184492014-09-07 20:29:59 +00001355 BasicBlock *BBTo, LVILatticeVal &Result,
1356 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001357 // If already a constant, there is nothing to compute.
1358 if (Constant *VC = dyn_cast<Constant>(Val)) {
1359 Result = LVILatticeVal::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001360 return true;
1361 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001362
Philip Reames44456b82016-02-02 03:15:40 +00001363 LVILatticeVal LocalResult;
1364 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1365 // If we couldn't constrain the value on the edge, LocalResult doesn't
1366 // provide any information.
Philip Reames1baaef12016-12-06 03:01:08 +00001367 LocalResult = LVILatticeVal::getOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001368
Philip Reames44456b82016-02-02 03:15:40 +00001369 if (hasSingleValue(LocalResult)) {
1370 // Can't get any more precise here
1371 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001372 return true;
1373 }
1374
1375 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001376 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1377 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001378 // No new information.
1379 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001380 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001381 }
1382
Philip Reames44456b82016-02-02 03:15:40 +00001383 // Try to intersect ranges of the BB and the constraint on the edge.
1384 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001385 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1386 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001387 // We can use the context instruction (generically the ultimate instruction
1388 // the calling pass is trying to simplify) here, even though the result of
1389 // this function is generally cached when called from the solve* functions
1390 // (and that cached result might be used with queries using a different
1391 // context instruction), because when this function is called from the solve*
1392 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001393 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001394 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001395 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001396
1397 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001398 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001399}
1400
Philip Reames92e5e1b2016-09-12 21:46:58 +00001401LVILatticeVal LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
Hal Finkel7e184492014-09-07 20:29:59 +00001402 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001403 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001404 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001405
Hans Wennborg45172ac2014-11-25 17:23:05 +00001406 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001407 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001408 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001409 solve();
1410 }
Owen Andersonc7ed4dc2010-12-09 06:14:58 +00001411 LVILatticeVal Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001412 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001413
1414 DEBUG(dbgs() << " Result = " << Result << "\n");
1415 return Result;
1416}
1417
Philip Reames92e5e1b2016-09-12 21:46:58 +00001418LVILatticeVal LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001419 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1420 << CxtI->getName() << "'\n");
1421
Philip Reamesbb781b42016-02-10 21:46:32 +00001422 if (auto *C = dyn_cast<Constant>(V))
1423 return LVILatticeVal::get(C);
1424
Philip Reamesd1f829d2016-02-02 21:57:37 +00001425 LVILatticeVal Result = LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001426 if (auto *I = dyn_cast<Instruction>(V))
1427 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001428 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001429
David Greene37e98092009-12-23 20:43:58 +00001430 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001431 return Result;
1432}
Chris Lattner19019ea2009-11-11 22:48:44 +00001433
Philip Reames92e5e1b2016-09-12 21:46:58 +00001434LVILatticeVal LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001435getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1436 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001437 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001438 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001439
Nick Lewycky55a700b2010-12-18 01:00:40 +00001440 LVILatticeVal Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001441 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001442 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001443 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001444 (void)WasFastQuery;
1445 assert(WasFastQuery && "More work to do after problem solved?");
1446 }
1447
David Greene37e98092009-12-23 20:43:58 +00001448 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001449 return Result;
1450}
1451
Philip Reames92e5e1b2016-09-12 21:46:58 +00001452void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames9db79482016-09-12 22:38:44 +00001453 BasicBlock *NewSucc) {
1454 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001455}
1456
Chris Lattneraf025d32009-11-15 19:59:49 +00001457//===----------------------------------------------------------------------===//
1458// LazyValueInfo Impl
1459//===----------------------------------------------------------------------===//
1460
Philip Reames92e5e1b2016-09-12 21:46:58 +00001461/// This lazily constructs the LazyValueInfoImpl.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001462static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1463 const DataLayout *DL,
Philip Reames92e5e1b2016-09-12 21:46:58 +00001464 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001465 if (!PImpl) {
1466 assert(DL && "getCache() called with a null DataLayout");
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001467 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001468 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001469 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001470}
1471
Sean Silva687019f2016-06-13 22:01:25 +00001472bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001473 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001474 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001475
1476 DominatorTreeWrapperPass *DTWP =
1477 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001478 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1479 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001480
Sean Silva687019f2016-06-13 22:01:25 +00001481 if (Info.PImpl)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001482 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001483
Owen Anderson208636f2010-08-18 18:39:01 +00001484 // Fully lazy.
1485 return false;
1486}
1487
Sean Silva687019f2016-06-13 22:01:25 +00001488void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001489 AU.setPreservesAll();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001490 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001491 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001492}
1493
Sean Silva687019f2016-06-13 22:01:25 +00001494LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1495
1496LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1497
Chris Lattneraf025d32009-11-15 19:59:49 +00001498void LazyValueInfo::releaseMemory() {
1499 // If the cache was allocated, free it.
1500 if (PImpl) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001501 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001502 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001503 }
1504}
1505
Chandler Carrutha504f2b2017-01-23 06:35:12 +00001506bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1507 FunctionAnalysisManager::Invalidator &Inv) {
1508 // We need to invalidate if we have either failed to preserve this analyses
1509 // result directly or if any of its dependencies have been invalidated.
1510 auto PAC = PA.getChecker<LazyValueAnalysis>();
1511 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1512 (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1513 return true;
1514
1515 return false;
1516}
1517
Sean Silva687019f2016-06-13 22:01:25 +00001518void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1519
1520LazyValueInfo LazyValueAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001521 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
Sean Silva687019f2016-06-13 22:01:25 +00001522 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1523 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1524
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001525 return LazyValueInfo(&AC, &TLI, DT);
Sean Silva687019f2016-06-13 22:01:25 +00001526}
1527
Wei Mif160e342016-09-15 06:28:34 +00001528/// Returns true if we can statically tell that this value will never be a
1529/// "useful" constant. In practice, this means we've got something like an
1530/// alloca or a malloc call for which a comparison against a constant can
1531/// only be guarding dead code. Note that we are potentially giving up some
1532/// precision in dead code (a constant result) in favour of avoiding a
1533/// expensive search for a easily answered common query.
1534static bool isKnownNonConstant(Value *V) {
1535 V = V->stripPointerCasts();
1536 // The return val of alloc cannot be a Constant.
1537 if (isa<AllocaInst>(V))
1538 return true;
1539 return false;
1540}
1541
Hal Finkel7e184492014-09-07 20:29:59 +00001542Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1543 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001544 // Bail out early if V is known not to be a Constant.
1545 if (isKnownNonConstant(V))
1546 return nullptr;
1547
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001548 const DataLayout &DL = BB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001549 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001550 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001551
Chris Lattner19019ea2009-11-11 22:48:44 +00001552 if (Result.isConstant())
1553 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001554 if (Result.isConstantRange()) {
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001555 ConstantRange CR = Result.getConstantRange();
1556 if (const APInt *SingleVal = CR.getSingleElement())
1557 return ConstantInt::get(V->getContext(), *SingleVal);
1558 }
Craig Topper9f008862014-04-15 04:59:12 +00001559 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001560}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001561
John Regehre1c481d2016-05-02 19:58:00 +00001562ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001563 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001564 assert(V->getType()->isIntegerTy());
1565 unsigned Width = V->getType()->getIntegerBitWidth();
1566 const DataLayout &DL = BB->getModule()->getDataLayout();
1567 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001568 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001569 if (Result.isUndefined())
1570 return ConstantRange(Width, /*isFullSet=*/false);
1571 if (Result.isConstantRange())
1572 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001573 // We represent ConstantInt constants as constant ranges but other kinds
1574 // of integer constants, i.e. ConstantExpr will be tagged as constants
1575 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1576 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001577 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001578}
1579
Sanjay Patel2a385e22015-01-09 16:47:20 +00001580/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001581/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001582Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001583 BasicBlock *ToBB,
1584 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001585 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001586 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001587 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001588
Chris Lattnerd5e25432009-11-12 01:29:10 +00001589 if (Result.isConstant())
1590 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001591 if (Result.isConstantRange()) {
Owen Anderson185fe002010-08-10 20:03:09 +00001592 ConstantRange CR = Result.getConstantRange();
1593 if (const APInt *SingleVal = CR.getSingleElement())
1594 return ConstantInt::get(V->getContext(), *SingleVal);
1595 }
Craig Topper9f008862014-04-15 04:59:12 +00001596 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001597}
1598
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001599static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
1600 LVILatticeVal &Result,
1601 const DataLayout &DL,
1602 TargetLibraryInfo *TLI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001603
Chris Lattner565ee2f2009-11-12 04:36:58 +00001604 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001605 Constant *Res = nullptr;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001606 if (Result.isConstant()) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001607 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001608 TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001609 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001610 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1611 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001612 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001613
Owen Anderson185fe002010-08-10 20:03:09 +00001614 if (Result.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001615 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001616 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001617
Owen Anderson185fe002010-08-10 20:03:09 +00001618 ConstantRange CR = Result.getConstantRange();
1619 if (Pred == ICmpInst::ICMP_EQ) {
1620 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001621 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001622
Owen Anderson185fe002010-08-10 20:03:09 +00001623 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001624 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001625 } else if (Pred == ICmpInst::ICMP_NE) {
1626 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001627 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001628
Owen Anderson185fe002010-08-10 20:03:09 +00001629 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001630 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001631 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001632
Owen Anderson185fe002010-08-10 20:03:09 +00001633 // Handle more complex predicates.
Sanjoy Das1f7b8132016-10-02 00:09:57 +00001634 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1635 (ICmpInst::Predicate)Pred, CI->getValue());
Nick Lewycky11678bd2010-12-15 18:57:18 +00001636 if (TrueValues.contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001637 return LazyValueInfo::True;
Nick Lewycky11678bd2010-12-15 18:57:18 +00001638 if (TrueValues.inverse().contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001639 return LazyValueInfo::False;
1640 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001641 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001642
Chris Lattneraf025d32009-11-15 19:59:49 +00001643 if (Result.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001644 // If this is an equality comparison, we can try to fold it knowing that
1645 // "V != C1".
1646 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001647 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001648 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001649 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001650 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001651 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001652 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001653 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001654 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001655 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001656 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001657 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001658 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001659 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001660 }
Hal Finkel7e184492014-09-07 20:29:59 +00001661 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001662 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001663
Hal Finkel7e184492014-09-07 20:29:59 +00001664 return LazyValueInfo::Unknown;
1665}
1666
Sanjay Patel2a385e22015-01-09 16:47:20 +00001667/// Determine whether the specified value comparison with a constant is known to
1668/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001669LazyValueInfo::Tristate
1670LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1671 BasicBlock *FromBB, BasicBlock *ToBB,
1672 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001673 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001674 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001675 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001676
1677 return getPredicateResult(Pred, C, Result, DL, TLI);
1678}
1679
1680LazyValueInfo::Tristate
1681LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1682 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001683 // Is or is not NonNull are common predicates being queried. If
1684 // isKnownNonNull can tell us the result of the predicate, we can
1685 // return it quickly. But this is only a fastpath, and falling
1686 // through would still be correct.
1687 if (V->getType()->isPointerTy() && C->isNullValue() &&
1688 isKnownNonNull(V->stripPointerCasts())) {
1689 if (Pred == ICmpInst::ICMP_EQ)
1690 return LazyValueInfo::False;
1691 else if (Pred == ICmpInst::ICMP_NE)
1692 return LazyValueInfo::True;
1693 }
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001694 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001695 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001696 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1697 if (Ret != Unknown)
1698 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001699
Philip Reamesaeefae02015-11-04 01:47:04 +00001700 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1701 // LVI as a whole tries to compute a lattice value which is conservatively
1702 // correct at a given location. In this case, we have a predicate which we
1703 // weren't able to prove about the merged result, and we're pushing that
1704 // predicate back along each incoming edge to see if we can prove it
1705 // separately for each input. As a motivating example, consider:
1706 // bb1:
1707 // %v1 = ... ; constantrange<1, 5>
1708 // br label %merge
1709 // bb2:
1710 // %v2 = ... ; constantrange<10, 20>
1711 // br label %merge
1712 // merge:
1713 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1714 // %pred = icmp eq i32 %phi, 8
1715 // We can't tell from the lattice value for '%phi' that '%pred' is false
1716 // along each path, but by checking the predicate over each input separately,
1717 // we can.
1718 // We limit the search to one step backwards from the current BB and value.
1719 // We could consider extending this to search further backwards through the
1720 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001721 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001722 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001723 BasicBlock *BB = CxtI->getParent();
1724
1725 // Function entry or an unreachable block. Bail to avoid confusing
1726 // analysis below.
1727 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1728 if (PI == PE)
1729 return Unknown;
1730
1731 // If V is a PHI node in the same block as the context, we need to ask
1732 // questions about the predicate as applied to the incoming value along
1733 // each edge. This is useful for eliminating cases where the predicate is
1734 // known along all incoming edges.
1735 if (auto *PHI = dyn_cast<PHINode>(V))
1736 if (PHI->getParent() == BB) {
1737 Tristate Baseline = Unknown;
1738 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1739 Value *Incoming = PHI->getIncomingValue(i);
1740 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001741 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001742 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1743 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001744
Philip Reamesbb11d622015-08-31 18:31:48 +00001745 // Keep going as long as we've seen a consistent known result for
1746 // all inputs.
1747 Baseline = (i == 0) ? Result /* First iteration */
1748 : (Baseline == Result ? Baseline : Unknown); /* All others */
1749 if (Baseline == Unknown)
1750 break;
1751 }
1752 if (Baseline != Unknown)
1753 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001754 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001755
Philip Reames66ab0f02015-06-16 00:49:59 +00001756 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001757 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001758 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001759 if (!isa<Instruction>(V) ||
1760 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001761 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001762 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001763 // the value of the comparison in this block.
1764 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1765 if (Baseline != Unknown) {
1766 // Check that all remaining incoming values match the first one.
1767 while (++PI != PE) {
1768 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1769 if (Ret != Baseline) break;
1770 }
1771 // If we terminated early, then one of the values didn't match.
1772 if (PI == PE) {
1773 return Baseline;
1774 }
1775 }
1776 }
1777 }
1778 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001779}
1780
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001781void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001782 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001783 if (PImpl) {
1784 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001785 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001786 }
Owen Anderson208636f2010-08-18 18:39:01 +00001787}
1788
1789void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001790 if (PImpl) {
1791 const DataLayout &DL = BB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001792 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001793 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001794}