blob: 5ea0e7ce9fb823e49fe5895bce347cc1a055a172 [file] [log] [blame]
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner741c94c2009-11-11 00:22:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000022#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000023#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
Hal Finkel7e184492014-09-07 20:29:59 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
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)
Chandler Carruth66b31302015-01-04 12:03:27 +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 Reames3bb28322016-04-25 18:48:43 +000074 /// This Value has a specific constant value. (For integers, constantrange
75 /// is used instead.)
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000076 constant,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000077
Philip Reames3bb28322016-04-25 18:48:43 +000078 /// This Value is known to not have the specified value. (For integers,
79 /// constantrange is used instead.)
Chris Lattner565ee2f2009-11-12 04:36:58 +000080 notconstant,
Chad Rosier43a33062011-12-02 01:26:24 +000081
Philip Reames3bb28322016-04-25 18:48:43 +000082 /// The Value falls within this range. (Used only for integer typed values.)
Owen Anderson0f306a42010-08-05 22:59:19 +000083 constantrange,
Chad Rosier43a33062011-12-02 01:26:24 +000084
Philip Reames3bb28322016-04-25 18:48:43 +000085 /// We can not precisely model the dynamic values this value might take.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000086 overdefined
87 };
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000088
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000089 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattner565ee2f2009-11-12 04:36:58 +000090 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersonc3a14132010-08-05 22:10:46 +000091 LatticeValueTy Tag;
92 Constant *Val;
Owen Anderson0f306a42010-08-05 22:59:19 +000093 ConstantRange Range;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000094
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000095public:
Craig Topper9f008862014-04-15 04:59:12 +000096 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000097
Chris Lattner19019ea2009-11-11 22:48:44 +000098 static LVILatticeVal get(Constant *C) {
99 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000100 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000101 Res.markConstant(C);
Chris Lattner19019ea2009-11-11 22:48:44 +0000102 return Res;
103 }
Chris Lattner565ee2f2009-11-12 04:36:58 +0000104 static LVILatticeVal getNot(Constant *C) {
105 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000106 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000107 Res.markNotConstant(C);
Chris Lattner565ee2f2009-11-12 04:36:58 +0000108 return Res;
109 }
Owen Anderson5f1dd092010-08-10 23:20:01 +0000110 static LVILatticeVal getRange(ConstantRange CR) {
111 LVILatticeVal Res;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000112 Res.markConstantRange(std::move(CR));
Owen Anderson5f1dd092010-08-10 23:20:01 +0000113 return Res;
114 }
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000115 static LVILatticeVal getOverdefined() {
116 LVILatticeVal Res;
117 Res.markOverdefined();
118 return Res;
119 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000120
Owen Anderson0f306a42010-08-05 22:59:19 +0000121 bool isUndefined() const { return Tag == undefined; }
122 bool isConstant() const { return Tag == constant; }
123 bool isNotConstant() const { return Tag == notconstant; }
124 bool isConstantRange() const { return Tag == constantrange; }
125 bool isOverdefined() const { return Tag == overdefined; }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000126
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000127 Constant *getConstant() const {
128 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000129 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000130 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000131
Chris Lattner565ee2f2009-11-12 04:36:58 +0000132 Constant *getNotConstant() const {
133 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000134 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000135 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000136
Owen Anderson0f306a42010-08-05 22:59:19 +0000137 ConstantRange getConstantRange() const {
138 assert(isConstantRange() &&
139 "Cannot get the constant-range of a non-constant-range!");
140 return Range;
141 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000142
Philip Reames1baaef12016-12-06 03:01:08 +0000143private:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000144 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000145 bool markOverdefined() {
146 if (isOverdefined())
147 return false;
Owen Andersonc3a14132010-08-05 22:10:46 +0000148 Tag = overdefined;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000149 return true;
150 }
151
Sanjay Patel2a385e22015-01-09 16:47:20 +0000152 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000153 bool markConstant(Constant *V) {
Nick Lewycky11678bd2010-12-15 18:57:18 +0000154 assert(V && "Marking constant with NULL");
155 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
156 return markConstantRange(ConstantRange(CI->getValue()));
157 if (isa<UndefValue>(V))
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000158 return false;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000159
160 assert((!isConstant() || getConstant() == V) &&
161 "Marking constant with different value");
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000162 assert(isUndefined());
Owen Andersonc3a14132010-08-05 22:10:46 +0000163 Tag = constant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000164 Val = V;
Chris Lattner19019ea2009-11-11 22:48:44 +0000165 return true;
166 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000167
Sanjay Patel2a385e22015-01-09 16:47:20 +0000168 /// Return true if this is a change in status.
Chris Lattner565ee2f2009-11-12 04:36:58 +0000169 bool markNotConstant(Constant *V) {
Chris Lattner565ee2f2009-11-12 04:36:58 +0000170 assert(V && "Marking constant with NULL");
Nick Lewycky11678bd2010-12-15 18:57:18 +0000171 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
172 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
173 if (isa<UndefValue>(V))
174 return false;
175
176 assert((!isConstant() || getConstant() != V) &&
177 "Marking constant !constant with same value");
178 assert((!isNotConstant() || getNotConstant() == V) &&
179 "Marking !constant with different value");
180 assert(isUndefined() || isConstant());
181 Tag = notconstant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000182 Val = V;
Chris Lattner565ee2f2009-11-12 04:36:58 +0000183 return true;
184 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000185
Sanjay Patel2a385e22015-01-09 16:47:20 +0000186 /// Return true if this is a change in status.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000187 bool markConstantRange(ConstantRange NewR) {
Owen Anderson0f306a42010-08-05 22:59:19 +0000188 if (isConstantRange()) {
189 if (NewR.isEmptySet())
190 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000191
Nuno Lopese6e04902012-06-28 01:16:18 +0000192 bool changed = Range != NewR;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000193 Range = std::move(NewR);
Owen Anderson0f306a42010-08-05 22:59:19 +0000194 return changed;
195 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000196
Owen Anderson0f306a42010-08-05 22:59:19 +0000197 assert(isUndefined());
198 if (NewR.isEmptySet())
199 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000200
Owen Anderson0f306a42010-08-05 22:59:19 +0000201 Tag = constantrange;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000202 Range = std::move(NewR);
Owen Anderson0f306a42010-08-05 22:59:19 +0000203 return true;
204 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000205
Philip Reamesb2949622016-12-06 02:54:16 +0000206public:
207
Sanjay Patel2a385e22015-01-09 16:47:20 +0000208 /// Merge the specified lattice value into this one, updating this
Chris Lattner19019ea2009-11-11 22:48:44 +0000209 /// one and returning true if anything changed.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000210 bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
Chris Lattner19019ea2009-11-11 22:48:44 +0000211 if (RHS.isUndefined() || isOverdefined()) return false;
212 if (RHS.isOverdefined()) return markOverdefined();
213
Nick Lewycky11678bd2010-12-15 18:57:18 +0000214 if (isUndefined()) {
215 Tag = RHS.Tag;
216 Val = RHS.Val;
217 Range = RHS.Range;
218 return true;
Chris Lattner22db4b52009-11-12 04:57:13 +0000219 }
220
Nick Lewycky11678bd2010-12-15 18:57:18 +0000221 if (isConstant()) {
222 if (RHS.isConstant()) {
223 if (Val == RHS.Val)
224 return false;
225 return markOverdefined();
226 }
227
228 if (RHS.isNotConstant()) {
229 if (Val == RHS.Val)
230 return markOverdefined();
231
232 // Unless we can prove that the two Constants are different, we must
233 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000234 if (ConstantInt *Res =
235 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
236 CmpInst::ICMP_NE, getConstant(), RHS.getNotConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000237 if (Res->isOne())
238 return markNotConstant(RHS.getNotConstant());
239
240 return markOverdefined();
241 }
242
Chris Lattner19019ea2009-11-11 22:48:44 +0000243 return markOverdefined();
Nick Lewycky11678bd2010-12-15 18:57:18 +0000244 }
245
246 if (isNotConstant()) {
247 if (RHS.isConstant()) {
248 if (Val == RHS.Val)
249 return markOverdefined();
250
251 // Unless we can prove that the two Constants are different, we must
252 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000253 if (ConstantInt *Res =
254 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
255 CmpInst::ICMP_NE, getNotConstant(), RHS.getConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000256 if (Res->isOne())
257 return false;
258
259 return markOverdefined();
260 }
261
262 if (RHS.isNotConstant()) {
263 if (Val == RHS.Val)
264 return false;
265 return markOverdefined();
266 }
267
268 return markOverdefined();
269 }
270
271 assert(isConstantRange() && "New LVILattice type?");
272 if (!RHS.isConstantRange())
273 return markOverdefined();
274
275 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
276 if (NewR.isFullSet())
277 return markOverdefined();
278 return markConstantRange(NewR);
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000279 }
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000280};
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000281
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000282} // end anonymous namespace.
283
Chris Lattner19019ea2009-11-11 22:48:44 +0000284namespace llvm {
Chandler Carruth2b1ba482011-04-18 18:49:44 +0000285raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
286 LLVM_ATTRIBUTE_USED;
Chris Lattner19019ea2009-11-11 22:48:44 +0000287raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
288 if (Val.isUndefined())
289 return OS << "undefined";
290 if (Val.isOverdefined())
291 return OS << "overdefined";
Chris Lattner565ee2f2009-11-12 04:36:58 +0000292
293 if (Val.isNotConstant())
294 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Davide Italianobd543d02016-05-25 22:29:34 +0000295 if (Val.isConstantRange())
Owen Anderson8afac042010-08-09 20:50:46 +0000296 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
297 << Val.getConstantRange().getUpper() << '>';
Chris Lattner19019ea2009-11-11 22:48:44 +0000298 return OS << "constant<" << *Val.getConstant() << '>';
299}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000300}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000301
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000302/// Returns true if this lattice value represents at most one possible value.
303/// This is as precise as any lattice value can get while still representing
304/// reachable code.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000305static bool hasSingleValue(const LVILatticeVal &Val) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000306 if (Val.isConstantRange() &&
307 Val.getConstantRange().isSingleElement())
308 // Integer constants are single element ranges
309 return true;
310 if (Val.isConstant())
311 // Non integer constants
312 return true;
313 return false;
314}
315
316/// Combine two sets of facts about the same value into a single set of
317/// facts. Note that this method is not suitable for merging facts along
318/// different paths in a CFG; that's what the mergeIn function is for. This
319/// is for merging facts gathered about the same value at the same location
320/// through two independent means.
321/// Notes:
322/// * This method does not promise to return the most precise possible lattice
323/// value implied by A and B. It is allowed to return any lattice element
324/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000325/// conflict, see below).
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000326/// * Due to unreachable code, the intersection of two lattice values could be
327/// contradictory. If this happens, we return some valid lattice value so as
328/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
329/// we do not make this guarantee. TODO: This would be a useful enhancement.
330static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) {
331 // Undefined is the strongest state. It means the value is known to be along
332 // an unreachable path.
333 if (A.isUndefined())
334 return A;
335 if (B.isUndefined())
336 return B;
337
338 // If we gave up for one, but got a useable fact from the other, use it.
339 if (A.isOverdefined())
340 return B;
341 if (B.isOverdefined())
342 return A;
343
344 // Can't get any more precise than constants.
345 if (hasSingleValue(A))
346 return A;
347 if (hasSingleValue(B))
348 return B;
349
350 // Could be either constant range or not constant here.
351 if (!A.isConstantRange() || !B.isConstantRange()) {
352 // TODO: Arbitrary choice, could be improved
353 return A;
354 }
355
356 // Intersect two constant ranges
357 ConstantRange Range =
358 A.getConstantRange().intersectWith(B.getConstantRange());
359 // Note: An empty range is implicitly converted to overdefined internally.
360 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000361 // and thus know this path must be unreachable.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000362 return LVILatticeVal::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000363}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000364
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000365//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000366// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000367//===----------------------------------------------------------------------===//
368
Chris Lattneraf025d32009-11-15 19:59:49 +0000369namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000370 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000371 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000372 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000373 // Needs to access getValPtr(), which is protected.
374 friend struct DenseMapInfo<LVIValueHandle>;
375
Owen Anderson118ac802011-01-05 21:15:29 +0000376 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000377
Owen Anderson118ac802011-01-05 21:15:29 +0000378 LVIValueHandle(Value *V, LazyValueInfoCache *P)
379 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000380
381 void deleted() override;
382 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000383 deleted();
384 }
385 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000386} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000387
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000388namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000389 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000390 /// maintains information about queries across the clients' queries.
391 class LazyValueInfoCache {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000392 /// This is all of the cached block information for exactly one Value*.
393 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000394 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000395 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
396 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000397 struct ValueCacheEntryTy {
398 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
399 LVIValueHandle Handle;
400 SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4> BlockVals;
401 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000402
Sanjay Patel2a385e22015-01-09 16:47:20 +0000403 /// This is all of the cached information for all values,
Owen Anderson6f060af2011-01-05 23:26:22 +0000404 /// mapped from Value* to key information.
Justin Lebar58b377e2016-07-27 22:33:36 +0000405 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000406
Sanjay Patel2a385e22015-01-09 16:47:20 +0000407 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000408 /// over-defined at the end of that block.
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000409 typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>>
410 OverDefinedCacheTy;
411 OverDefinedCacheTy OverDefinedCache;
Benjamin Kramer36647082011-12-03 15:16:45 +0000412
Sanjay Patel2a385e22015-01-09 16:47:20 +0000413 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000414 /// don't spend time removing unused blocks from our caches.
415 DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
416
Philip Reames9db79482016-09-12 22:38:44 +0000417 public:
Hans Wennborg45172ac2014-11-25 17:23:05 +0000418 void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
419 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000420
421 // Insert over-defined values into their own cache to reduce memory
422 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000423 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000424 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-07-27 22:33:36 +0000425 else {
426 auto It = ValueCache.find_as(Val);
427 if (It == ValueCache.end()) {
428 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
429 It = ValueCache.find_as(Val);
430 assert(It != ValueCache.end() && "Val was just added to the map!");
431 }
432 It->second->BlockVals[BB] = Result;
433 }
Hans Wennborg45172ac2014-11-25 17:23:05 +0000434 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000435
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000436 bool isOverdefined(Value *V, BasicBlock *BB) const {
437 auto ODI = OverDefinedCache.find(BB);
438
439 if (ODI == OverDefinedCache.end())
440 return false;
441
442 return ODI->second.count(V);
443 }
444
Philip Reames9db79482016-09-12 22:38:44 +0000445 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000446 if (isOverdefined(V, BB))
447 return true;
448
Justin Lebar58b377e2016-07-27 22:33:36 +0000449 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000450 if (I == ValueCache.end())
451 return false;
452
Justin Lebar58b377e2016-07-27 22:33:36 +0000453 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000454 }
455
Philip Reames9db79482016-09-12 22:38:44 +0000456 LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000457 if (isOverdefined(V, BB))
458 return LVILatticeVal::getOverdefined();
459
Justin Lebar58b377e2016-07-27 22:33:36 +0000460 auto I = ValueCache.find_as(V);
461 if (I == ValueCache.end())
462 return LVILatticeVal();
463 auto BBI = I->second->BlockVals.find(BB);
464 if (BBI == I->second->BlockVals.end())
465 return LVILatticeVal();
466 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000467 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000468
Philip Reames92e5e1b2016-09-12 21:46:58 +0000469 /// clear - Empty the cache.
470 void clear() {
471 SeenBlocks.clear();
472 ValueCache.clear();
473 OverDefinedCache.clear();
474 }
475
Philip Reamesb627aec2016-09-12 22:03:36 +0000476 /// Inform the cache that a given value has been deleted.
477 void eraseValue(Value *V);
478
479 /// This is part of the update interface to inform the cache
480 /// that a block has been deleted.
481 void eraseBlock(BasicBlock *BB);
482
Philip Reames9db79482016-09-12 22:38:44 +0000483 /// Updates the cache to remove any influence an overdefined value in
484 /// OldSucc might have (unless also overdefined in NewSucc). This just
485 /// flushes elements from the cache and does not add any.
486 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
487
Philip Reames92e5e1b2016-09-12 21:46:58 +0000488 friend struct LVIValueHandle;
489 };
Philip Reamesb627aec2016-09-12 22:03:36 +0000490}
Philip Reames92e5e1b2016-09-12 21:46:58 +0000491
Philip Reamesb627aec2016-09-12 22:03:36 +0000492void LazyValueInfoCache::eraseValue(Value *V) {
493 SmallVector<AssertingVH<BasicBlock>, 4> ToErase;
494 for (auto &I : OverDefinedCache) {
495 SmallPtrSetImpl<Value *> &ValueSet = I.second;
496 if (ValueSet.count(V))
497 ValueSet.erase(V);
498 if (ValueSet.empty())
499 ToErase.push_back(I.first);
500 }
501 for (auto &BB : ToErase)
502 OverDefinedCache.erase(BB);
503
504 ValueCache.erase(V);
505}
506
507void LVIValueHandle::deleted() {
508 // This erasure deallocates *this, so it MUST happen after we're done
509 // using any and all members of *this.
510 Parent->eraseValue(*this);
511}
512
513void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
514 // Shortcut if we have never seen this block.
515 DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
516 if (I == SeenBlocks.end())
517 return;
518 SeenBlocks.erase(I);
519
520 auto ODI = OverDefinedCache.find(BB);
521 if (ODI != OverDefinedCache.end())
522 OverDefinedCache.erase(ODI);
523
524 for (auto &I : ValueCache)
525 I.second->BlockVals.erase(BB);
526}
527
Philip Reames9db79482016-09-12 22:38:44 +0000528void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
529 BasicBlock *NewSucc) {
530 // When an edge in the graph has been threaded, values that we could not
531 // determine a value for before (i.e. were marked overdefined) may be
532 // possible to solve now. We do NOT try to proactively update these values.
533 // Instead, we clear their entries from the cache, and allow lazy updating to
534 // recompute them when needed.
535
536 // The updating process is fairly simple: we need to drop cached info
537 // for all values that were marked overdefined in OldSucc, and for those same
538 // values in any successor of OldSucc (except NewSucc) in which they were
539 // also marked overdefined.
540 std::vector<BasicBlock*> worklist;
541 worklist.push_back(OldSucc);
542
543 auto I = OverDefinedCache.find(OldSucc);
544 if (I == OverDefinedCache.end())
545 return; // Nothing to process here.
546 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
547
548 // Use a worklist to perform a depth-first search of OldSucc's successors.
549 // NOTE: We do not need a visited list since any blocks we have already
550 // visited will have had their overdefined markers cleared already, and we
551 // thus won't loop to their successors.
552 while (!worklist.empty()) {
553 BasicBlock *ToUpdate = worklist.back();
554 worklist.pop_back();
555
556 // Skip blocks only accessible through NewSucc.
557 if (ToUpdate == NewSucc) continue;
558
559 bool changed = false;
560 for (Value *V : ValsToClear) {
561 // If a value was marked overdefined in OldSucc, and is here too...
562 auto OI = OverDefinedCache.find(ToUpdate);
563 if (OI == OverDefinedCache.end())
564 continue;
565 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
566 if (!ValueSet.count(V))
567 continue;
568
569 ValueSet.erase(V);
570 if (ValueSet.empty())
571 OverDefinedCache.erase(OI);
572
573 // If we removed anything, then we potentially need to update
574 // blocks successors too.
575 changed = true;
576 }
577
578 if (!changed) continue;
579
580 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
581 }
582}
583
Philip Reamesb627aec2016-09-12 22:03:36 +0000584namespace {
Philip Reames92e5e1b2016-09-12 21:46:58 +0000585 // The actual implementation of the lazy analysis and update. Note that the
586 // inheritance from LazyValueInfoCache is intended to be temporary while
587 // splitting the code and then transitioning to a has-a relationship.
Philip Reames9db79482016-09-12 22:38:44 +0000588 class LazyValueInfoImpl {
589
590 /// Cached results from previous queries
591 LazyValueInfoCache TheCache;
Philip Reames92e5e1b2016-09-12 21:46:58 +0000592
593 /// This stack holds the state of the value solver during a query.
594 /// It basically emulates the callstack of the naive
595 /// recursive value lookup process.
596 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
597
598 /// Keeps track of which block-value pairs are in BlockValueStack.
599 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
600
601 /// Push BV onto BlockValueStack unless it's already in there.
602 /// Returns true on success.
603 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
604 if (!BlockValueSet.insert(BV).second)
605 return false; // It's already in the stack.
606
607 DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
608 << "\n");
609 BlockValueStack.push(BV);
610 return true;
611 }
612
613 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
614 const DataLayout &DL; ///< A mandatory DataLayout
615 DominatorTree *DT; ///< An optional DT pointer.
616
617 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
618 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
619 LVILatticeVal &Result, Instruction *CxtI = nullptr);
620 bool hasBlockValue(Value *Val, BasicBlock *BB);
621
622 // These methods process one work item and may add more. A false value
623 // returned means that the work item was not completely processed and must
624 // be revisited after going through the new items.
625 bool solveBlockValue(Value *Val, BasicBlock *BB);
626 bool solveBlockValueNonLocal(LVILatticeVal &BBLV, Value *Val, BasicBlock *BB);
627 bool solveBlockValuePHINode(LVILatticeVal &BBLV, PHINode *PN, BasicBlock *BB);
628 bool solveBlockValueSelect(LVILatticeVal &BBLV, SelectInst *S,
629 BasicBlock *BB);
630 bool solveBlockValueBinaryOp(LVILatticeVal &BBLV, Instruction *BBI,
631 BasicBlock *BB);
632 bool solveBlockValueCast(LVILatticeVal &BBLV, Instruction *BBI,
633 BasicBlock *BB);
634 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
635 LVILatticeVal &BBLV,
636 Instruction *BBI);
637
638 void solve();
639
640 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000641 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000642 /// value for the specified Value* at the end of the specified block.
Hal Finkel7e184492014-09-07 20:29:59 +0000643 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
644 Instruction *CxtI = nullptr);
645
Sanjay Patel2a385e22015-01-09 16:47:20 +0000646 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000647 /// value for the specified Value* at the specified instruction (generally
648 /// from an assume intrinsic).
649 LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000650
Sanjay Patel2a385e22015-01-09 16:47:20 +0000651 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000652 /// value for the specified Value* that is true on the specified edge.
Hal Finkel7e184492014-09-07 20:29:59 +0000653 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
654 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000655
Philip Reames9db79482016-09-12 22:38:44 +0000656 /// Complete flush all previously computed values
657 void clear() {
658 TheCache.clear();
659 }
660
661 /// This is part of the update interface to inform the cache
662 /// that a block has been deleted.
663 void eraseBlock(BasicBlock *BB) {
664 TheCache.eraseBlock(BB);
665 }
666
Sanjay Patel2a385e22015-01-09 16:47:20 +0000667 /// This is the update interface to inform the cache that an edge from
668 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000669 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000670
Philip Reames92e5e1b2016-09-12 21:46:58 +0000671 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
Chandler Carruth66b31302015-01-04 12:03:27 +0000672 DominatorTree *DT = nullptr)
673 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000674 };
675} // end anonymous namespace
676
Philip Reames92e5e1b2016-09-12 21:46:58 +0000677void LazyValueInfoImpl::solve() {
Owen Anderson6f060af2011-01-05 23:26:22 +0000678 while (!BlockValueStack.empty()) {
679 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000680 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
681
Nuno Lopese6e04902012-06-28 01:16:18 +0000682 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000683 // The work item was completely processed.
684 assert(BlockValueStack.top() == e && "Nothing should have been pushed!");
Philip Reames9db79482016-09-12 22:38:44 +0000685 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000686 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000687
Philip Reames44456b82016-02-02 03:15:40 +0000688 DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000689 << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000690
Owen Anderson6f060af2011-01-05 23:26:22 +0000691 BlockValueStack.pop();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000692 BlockValueSet.erase(e);
693 } else {
694 // More work needs to be done before revisiting.
695 assert(BlockValueStack.top() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000696 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000697 }
698}
699
Philip Reames92e5e1b2016-09-12 21:46:58 +0000700bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000701 // If already a constant, there is nothing to compute.
702 if (isa<Constant>(Val))
703 return true;
704
Philip Reames9db79482016-09-12 22:38:44 +0000705 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000706}
707
Philip Reames92e5e1b2016-09-12 21:46:58 +0000708LVILatticeVal LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000709 // If already a constant, there is nothing to compute.
710 if (Constant *VC = dyn_cast<Constant>(Val))
711 return LVILatticeVal::get(VC);
712
Philip Reames9db79482016-09-12 22:38:44 +0000713 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000714}
715
Philip Reameseb3e9da2015-10-29 03:57:17 +0000716static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
717 switch (BBI->getOpcode()) {
718 default: break;
719 case Instruction::Load:
720 case Instruction::Call:
721 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000722 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000723 if (isa<IntegerType>(BBI->getType())) {
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000724 return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000725 }
726 break;
727 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000728 // Nothing known - will be intersected with other facts
729 return LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000730}
731
Philip Reames92e5e1b2016-09-12 21:46:58 +0000732bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000733 if (isa<Constant>(Val))
734 return true;
735
Philip Reames9db79482016-09-12 22:38:44 +0000736 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000737 // If we have a cached value, use that.
738 DEBUG(dbgs() << " reuse BB '" << BB->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000739 << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000740
Hans Wennborg45172ac2014-11-25 17:23:05 +0000741 // Since we're reusing a cached value, we don't need to update the
742 // OverDefinedCache. The cache will have been properly updated whenever the
743 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000744 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000745 }
746
Hans Wennborg45172ac2014-11-25 17:23:05 +0000747 // Hold off inserting this value into the Cache in case we have to return
748 // false and come back later.
749 LVILatticeVal Res;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000750
Chris Lattneraf025d32009-11-15 19:59:49 +0000751 Instruction *BBI = dyn_cast<Instruction>(Val);
Craig Topper9f008862014-04-15 04:59:12 +0000752 if (!BBI || BBI->getParent() != BB) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000753 if (!solveBlockValueNonLocal(Res, Val, BB))
754 return false;
Philip Reames9db79482016-09-12 22:38:44 +0000755 TheCache.insertResult(Val, BB, Res);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000756 return true;
Chris Lattneraf025d32009-11-15 19:59:49 +0000757 }
Chris Lattner2c708562009-11-15 20:00:52 +0000758
Nick Lewycky55a700b2010-12-18 01:00:40 +0000759 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000760 if (!solveBlockValuePHINode(Res, PN, BB))
761 return false;
Philip Reames9db79482016-09-12 22:38:44 +0000762 TheCache.insertResult(Val, BB, Res);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000763 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000764 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000765
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000766 if (auto *SI = dyn_cast<SelectInst>(BBI)) {
767 if (!solveBlockValueSelect(Res, SI, BB))
768 return false;
Philip Reames9db79482016-09-12 22:38:44 +0000769 TheCache.insertResult(Val, BB, Res);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000770 return true;
771 }
772
Philip Reames2ab964e2016-04-27 01:02:25 +0000773 // If this value is a nonnull pointer, record it's range and bailout. Note
774 // that for all other pointer typed values, we terminate the search at the
775 // definition. We could easily extend this to look through geps, bitcasts,
776 // and the like to prove non-nullness, but it's not clear that's worth it
777 // compile time wise. The context-insensative value walk done inside
778 // isKnownNonNull gets most of the profitable cases at much less expense.
779 // This does mean that we have a sensativity to where the defining
780 // instruction is placed, even if it could legally be hoisted much higher.
781 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000782 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
783 if (PT && isKnownNonNull(BBI)) {
784 Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
Philip Reames9db79482016-09-12 22:38:44 +0000785 TheCache.insertResult(Val, BB, Res);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000786 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000787 }
Davide Italianobd543d02016-05-25 22:29:34 +0000788 if (BBI->getType()->isIntegerTy()) {
Philip Reames2ab964e2016-04-27 01:02:25 +0000789 if (isa<CastInst>(BBI)) {
790 if (!solveBlockValueCast(Res, BBI, BB))
791 return false;
Philip Reames9db79482016-09-12 22:38:44 +0000792 TheCache.insertResult(Val, BB, Res);
Philip Reames2ab964e2016-04-27 01:02:25 +0000793 return true;
794 }
795 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000796 if (BO && isa<ConstantInt>(BO->getOperand(1))) {
Philip Reames2ab964e2016-04-27 01:02:25 +0000797 if (!solveBlockValueBinaryOp(Res, BBI, BB))
798 return false;
Philip Reames9db79482016-09-12 22:38:44 +0000799 TheCache.insertResult(Val, BB, Res);
Philip Reames2ab964e2016-04-27 01:02:25 +0000800 return true;
801 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000802 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000803
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000804 DEBUG(dbgs() << " compute BB '" << BB->getName()
805 << "' - unknown inst def found.\n");
806 Res = getFromRangeMetadata(BBI);
Philip Reames9db79482016-09-12 22:38:44 +0000807 TheCache.insertResult(Val, BB, Res);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000808 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000809}
810
811static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
812 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
813 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000814 GetUnderlyingObject(L->getPointerOperand(),
815 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000816 }
817 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
818 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000819 GetUnderlyingObject(S->getPointerOperand(),
820 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000821 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000822 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
823 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000824
825 // FIXME: check whether it has a valuerange that excludes zero?
826 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
827 if (!Len || Len->isZero()) return false;
828
Eli Friedman7a5fc692011-05-31 20:40:16 +0000829 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000830 if (GetUnderlyingObject(MI->getRawDest(),
831 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000832 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000833 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000834 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000835 if (GetUnderlyingObject(MTI->getRawSource(),
836 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000837 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000838 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000839 return false;
840}
841
Philip Reames3f83dbe2016-04-27 00:30:55 +0000842/// Return true if the allocation associated with Val is ever dereferenced
843/// within the given basic block. This establishes the fact Val is not null,
844/// but does not imply that the memory at Val is dereferenceable. (Val may
845/// point off the end of the dereferenceable part of the object.)
846static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
847 assert(Val->getType()->isPointerTy());
848
849 const DataLayout &DL = BB->getModule()->getDataLayout();
850 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
851 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
852 // inside InstructionDereferencesPointer either.
853 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
854 for (Instruction &I : *BB)
855 if (InstructionDereferencesPointer(&I, UnderlyingVal))
856 return true;
857 return false;
858}
859
Philip Reames92e5e1b2016-09-12 21:46:58 +0000860bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000861 Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000862 LVILatticeVal Result; // Start Undefined.
863
Nick Lewycky55a700b2010-12-18 01:00:40 +0000864 // If this is the entry block, we must be asking about an argument. The
865 // value is overdefined.
866 if (BB == &BB->getParent()->getEntryBlock()) {
867 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Philip Reames3f83dbe2016-04-27 00:30:55 +0000868 // Bofore giving up, see if we can prove the pointer non-null local to
869 // this particular block.
870 if (Val->getType()->isPointerTy() &&
871 (isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
Chris Lattner229907c2011-07-18 04:54:35 +0000872 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000873 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
874 } else {
Philip Reames1baaef12016-12-06 03:01:08 +0000875 Result = LVILatticeVal::getOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000876 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000877 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000878 return true;
879 }
880
881 // Loop over all of our predecessors, merging what we know from them into
882 // result.
883 bool EdgesMissing = false;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000884 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000885 LVILatticeVal EdgeResult;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000886 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000887 if (EdgesMissing)
888 continue;
889
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000890 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000891
892 // If we hit overdefined, exit early. The BlockVals entry is already set
893 // to overdefined.
894 if (Result.isOverdefined()) {
895 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000896 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000897 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000898 // this particular block.
899 if (Val->getType()->isPointerTy() &&
900 isObjectDereferencedInBlock(Val, BB)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000901 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000902 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
903 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000904
Owen Anderson64c2c572010-12-20 18:18:16 +0000905 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000906 return true;
907 }
908 }
909 if (EdgesMissing)
910 return false;
911
912 // Return the merged value, which is more precise than 'overdefined'.
913 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000914 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000915 return true;
916}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000917
Philip Reames92e5e1b2016-09-12 21:46:58 +0000918bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000919 PHINode *PN, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000920 LVILatticeVal Result; // Start Undefined.
921
922 // Loop over all of our predecessors, merging what we know from them into
923 // result.
924 bool EdgesMissing = false;
925 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
926 BasicBlock *PhiBB = PN->getIncomingBlock(i);
927 Value *PhiVal = PN->getIncomingValue(i);
928 LVILatticeVal EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000929 // Note that we can provide PN as the context value to getEdgeValue, even
930 // though the results will be cached, because PN is the value being used as
931 // the cache key in the caller.
Hal Finkel7e184492014-09-07 20:29:59 +0000932 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000933 if (EdgesMissing)
934 continue;
935
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000936 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000937
938 // If we hit overdefined, exit early. The BlockVals entry is already set
939 // to overdefined.
940 if (Result.isOverdefined()) {
941 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000942 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000943
Owen Anderson64c2c572010-12-20 18:18:16 +0000944 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000945 return true;
946 }
947 }
948 if (EdgesMissing)
949 return false;
950
951 // Return the merged value, which is more precise than 'overdefined'.
952 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000953 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000954 return true;
955}
956
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000957static LVILatticeVal getValueFromCondition(Value *Val, Value *Cond,
958 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000959
Philip Reamesd1f829d2016-02-02 21:57:37 +0000960// If we can determine a constraint on the value given conditions assumed by
961// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000962void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000963 Value *Val, LVILatticeVal &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000964 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
965 if (!BBI)
966 return;
967
Chandler Carruth66b31302015-01-04 12:03:27 +0000968 for (auto &AssumeVH : AC->assumptions()) {
969 if (!AssumeVH)
970 continue;
971 auto *I = cast<CallInst>(AssumeVH);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000972 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000973 continue;
974
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000975 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +0000976 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000977
978 // If guards are not used in the module, don't spend time looking for them
979 auto *GuardDecl = BBI->getModule()->getFunction(
980 Intrinsic::getName(Intrinsic::experimental_guard));
981 if (!GuardDecl || GuardDecl->use_empty())
982 return;
983
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000984 for (Instruction &I : make_range(BBI->getIterator().getReverse(),
985 BBI->getParent()->rend())) {
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000986 Value *Cond = nullptr;
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000987 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
988 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000989 }
Hal Finkel7e184492014-09-07 20:29:59 +0000990}
991
Philip Reames92e5e1b2016-09-12 21:46:58 +0000992bool LazyValueInfoImpl::solveBlockValueSelect(LVILatticeVal &BBLV,
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000993 SelectInst *SI, BasicBlock *BB) {
994
995 // Recurse on our inputs if needed
996 if (!hasBlockValue(SI->getTrueValue(), BB)) {
997 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
998 return false;
Philip Reames1baaef12016-12-06 03:01:08 +0000999 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001000 return true;
1001 }
1002 LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
1003 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
1004 // extra slots in the table if we can.
1005 if (TrueVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +00001006 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001007 return true;
1008 }
1009
1010 if (!hasBlockValue(SI->getFalseValue(), BB)) {
1011 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
1012 return false;
Philip Reames1baaef12016-12-06 03:01:08 +00001013 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001014 return true;
1015 }
1016 LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
1017 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
1018 // extra slots in the table if we can.
1019 if (FalseVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +00001020 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001021 return true;
1022 }
1023
Philip Reamesadf0e352016-02-26 22:53:59 +00001024 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
1025 ConstantRange TrueCR = TrueVal.getConstantRange();
1026 ConstantRange FalseCR = FalseVal.getConstantRange();
1027 Value *LHS = nullptr;
1028 Value *RHS = nullptr;
1029 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
1030 // Is this a min specifically of our two inputs? (Avoid the risk of
1031 // ValueTracking getting smarter looking back past our immediate inputs.)
1032 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
1033 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reamesb2949622016-12-06 02:54:16 +00001034 ConstantRange ResultCR = [&]() {
1035 switch (SPR.Flavor) {
1036 default:
1037 llvm_unreachable("unexpected minmax type!");
1038 case SPF_SMIN: /// Signed minimum
1039 return TrueCR.smin(FalseCR);
1040 case SPF_UMIN: /// Unsigned minimum
1041 return TrueCR.umin(FalseCR);
1042 case SPF_SMAX: /// Signed maximum
1043 return TrueCR.smax(FalseCR);
1044 case SPF_UMAX: /// Unsigned maximum
1045 return TrueCR.umax(FalseCR);
1046 };
1047 }();
1048 BBLV = LVILatticeVal::getRange(ResultCR);
1049 return true;
Philip Reamesadf0e352016-02-26 22:53:59 +00001050 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001051
Philip Reamesadf0e352016-02-26 22:53:59 +00001052 // TODO: ABS, NABS from the SelectPatternResult
1053 }
1054
Philip Reames854a84c2016-02-12 00:09:18 +00001055 // Can we constrain the facts about the true and false values by using the
1056 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
1057 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001058 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001059 TrueVal = intersect(TrueVal,
1060 getValueFromCondition(SI->getTrueValue(), Cond, true));
1061 FalseVal = intersect(FalseVal,
1062 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +00001063
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001064 // Handle clamp idioms such as:
1065 // %24 = constantrange<0, 17>
1066 // %39 = icmp eq i32 %24, 0
1067 // %40 = add i32 %24, -1
1068 // %siv.next = select i1 %39, i32 16, i32 %40
1069 // %siv.next = constantrange<0, 17> not <-1, 17>
1070 // In general, this can handle any clamp idiom which tests the edge
1071 // condition via an equality or inequality.
1072 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +00001073 ICmpInst::Predicate Pred = ICI->getPredicate();
1074 Value *A = ICI->getOperand(0);
1075 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
1076 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
1077 assert(A->getType() == B->getType());
1078 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
1079 };
1080 // See if either input is A + C2, subject to the constraint from the
1081 // condition that A != C when that input is used. We can assume that
1082 // that input doesn't include C + C2.
1083 ConstantInt *CIAdded;
1084 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +00001085 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +00001086 case ICmpInst::ICMP_EQ:
1087 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
1088 m_ConstantInt(CIAdded)))) {
1089 auto ResNot = addConstants(CIBase, CIAdded);
1090 FalseVal = intersect(FalseVal,
1091 LVILatticeVal::getNot(ResNot));
1092 }
1093 break;
1094 case ICmpInst::ICMP_NE:
1095 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
1096 m_ConstantInt(CIAdded)))) {
1097 auto ResNot = addConstants(CIBase, CIAdded);
1098 TrueVal = intersect(TrueVal,
1099 LVILatticeVal::getNot(ResNot));
1100 }
1101 break;
1102 };
1103 }
1104 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001105
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001106 LVILatticeVal Result; // Start Undefined.
1107 Result.mergeIn(TrueVal, DL);
1108 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001109 BBLV = Result;
1110 return true;
1111}
1112
Philip Reames92e5e1b2016-09-12 21:46:58 +00001113bool LazyValueInfoImpl::solveBlockValueCast(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001114 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001115 BasicBlock *BB) {
1116 if (!BBI->getOperand(0)->getType()->isSized()) {
1117 // Without knowing how wide the input is, we can't analyze it in any useful
1118 // way.
Philip Reames1baaef12016-12-06 03:01:08 +00001119 BBLV = LVILatticeVal::getOverdefined();
Philip Reamese5030e82016-04-26 22:52:30 +00001120 return true;
1121 }
Philip Reamesf105db42016-04-26 23:27:33 +00001122
1123 // Filter out casts we don't know how to reason about before attempting to
1124 // recurse on our operand. This can cut a long search short if we know we're
1125 // not going to be able to get any useful information anways.
1126 switch (BBI->getOpcode()) {
1127 case Instruction::Trunc:
1128 case Instruction::SExt:
1129 case Instruction::ZExt:
1130 case Instruction::BitCast:
1131 break;
1132 default:
1133 // Unhandled instructions are overdefined.
1134 DEBUG(dbgs() << " compute BB '" << BB->getName()
1135 << "' - overdefined (unknown cast).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001136 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001137 return true;
1138 }
1139
Philip Reames38c87c22016-04-26 21:48:16 +00001140 // Figure out the range of the LHS. If that fails, we still apply the
1141 // transfer rule on the full set since we may be able to locally infer
1142 // interesting facts.
1143 if (!hasBlockValue(BBI->getOperand(0), BB))
Hans Wennborg45172ac2014-11-25 17:23:05 +00001144 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
Philip Reames38c87c22016-04-26 21:48:16 +00001145 // More work to do before applying this transfer rule.
Hans Wennborg45172ac2014-11-25 17:23:05 +00001146 return false;
Philip Reames38c87c22016-04-26 21:48:16 +00001147
1148 const unsigned OperandBitWidth =
Philip Reamese5030e82016-04-26 22:52:30 +00001149 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
Philip Reames38c87c22016-04-26 21:48:16 +00001150 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1151 if (hasBlockValue(BBI->getOperand(0), BB)) {
1152 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001153 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1154 BBI);
Philip Reames38c87c22016-04-26 21:48:16 +00001155 if (LHSVal.isConstantRange())
1156 LHSRange = LHSVal.getConstantRange();
Nick Lewycky55a700b2010-12-18 01:00:40 +00001157 }
1158
Philip Reames38c87c22016-04-26 21:48:16 +00001159 const unsigned ResultBitWidth =
1160 cast<IntegerType>(BBI->getType())->getBitWidth();
Philip Reames66715772016-04-25 18:30:31 +00001161
1162 // NOTE: We're currently limited by the set of operations that ConstantRange
1163 // can evaluate symbolically. Enhancing that set will allows us to analyze
1164 // more definitions.
Philip Reames4d00af12016-12-01 20:08:47 +00001165 auto CastOp = (Instruction::CastOps) BBI->getOpcode();
Philip Reames0e613f72016-12-06 02:36:58 +00001166 BBLV = LVILatticeVal::getRange(LHSRange.castOp(CastOp, ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001167 return true;
1168}
1169
Philip Reames92e5e1b2016-09-12 21:46:58 +00001170bool LazyValueInfoImpl::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001171 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001172 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +00001173
Philip Reames053c2a62016-04-26 23:10:35 +00001174 assert(BBI->getOperand(0)->getType()->isSized() &&
1175 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +00001176
1177 // Filter out operators we don't know how to reason about before attempting to
1178 // recurse on our operand(s). This can cut a long search short if we know
1179 // we're not going to be able to get any useful information anways.
1180 switch (BBI->getOpcode()) {
1181 case Instruction::Add:
1182 case Instruction::Sub:
1183 case Instruction::Mul:
1184 case Instruction::UDiv:
1185 case Instruction::Shl:
1186 case Instruction::LShr:
1187 case Instruction::And:
1188 case Instruction::Or:
1189 // continue into the code below
1190 break;
1191 default:
1192 // Unhandled instructions are overdefined.
1193 DEBUG(dbgs() << " compute BB '" << BB->getName()
1194 << "' - overdefined (unknown binary operator).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001195 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001196 return true;
1197 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001198
Philip Reames053c2a62016-04-26 23:10:35 +00001199 // Figure out the range of the LHS. If that fails, use a conservative range,
1200 // but apply the transfer rule anyways. This lets us pick up facts from
1201 // expressions like "and i32 (call i32 @foo()), 32"
1202 if (!hasBlockValue(BBI->getOperand(0), BB))
1203 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
1204 // More work to do before applying this transfer rule.
1205 return false;
1206
1207 const unsigned OperandBitWidth =
1208 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
1209 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1210 if (hasBlockValue(BBI->getOperand(0), BB)) {
1211 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001212 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1213 BBI);
Philip Reames053c2a62016-04-26 23:10:35 +00001214 if (LHSVal.isConstantRange())
1215 LHSRange = LHSVal.getConstantRange();
Philip Reames66715772016-04-25 18:30:31 +00001216 }
Philip Reames66715772016-04-25 18:30:31 +00001217
1218 ConstantInt *RHS = cast<ConstantInt>(BBI->getOperand(1));
1219 ConstantRange RHSRange = ConstantRange(RHS->getValue());
1220
Owen Anderson80d19f02010-08-18 21:11:37 +00001221 // NOTE: We're currently limited by the set of operations that ConstantRange
1222 // can evaluate symbolically. Enhancing that set will allows us to analyze
1223 // more definitions.
Philip Reames4d00af12016-12-01 20:08:47 +00001224 auto BinOp = (Instruction::BinaryOps) BBI->getOpcode();
Philip Reames0e613f72016-12-06 02:36:58 +00001225 BBLV = LVILatticeVal::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001226 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001227}
1228
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001229static LVILatticeVal getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1230 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001231 Value *LHS = ICI->getOperand(0);
1232 Value *RHS = ICI->getOperand(1);
1233 CmpInst::Predicate Predicate = ICI->getPredicate();
1234
1235 if (isa<Constant>(RHS)) {
1236 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001237 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001238 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001239 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001240 return LVILatticeVal::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001241 else
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001242 return LVILatticeVal::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001243 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001244 }
Hal Finkel7e184492014-09-07 20:29:59 +00001245
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001246 if (!Val->getType()->isIntegerTy())
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001247 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001248
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001249 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1250 // range of Val guaranteed by the condition. Recognize comparisons in the from
1251 // of:
1252 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001253 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001254 // The latter is the range checking idiom that InstCombine produces. Subtract
1255 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001256
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001257 // Val or (add Val, Offset) can be on either hand of the comparison
1258 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1259 std::swap(LHS, RHS);
1260 Predicate = CmpInst::getSwappedPredicate(Predicate);
1261 }
Hal Finkel7e184492014-09-07 20:29:59 +00001262
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001263 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001264 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001265 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001266
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001267 if (LHS == Val || Offset) {
1268 // Calculate the range of values that are allowed by the comparison
1269 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1270 /*isFullSet=*/true);
1271 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1272 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001273 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1274 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1275 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001276
1277 // If we're interested in the false dest, invert the condition
1278 CmpInst::Predicate Pred =
1279 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1280 ConstantRange TrueValues =
1281 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1282
1283 if (Offset) // Apply the offset from above.
1284 TrueValues = TrueValues.subtract(Offset->getValue());
1285
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001286 return LVILatticeVal::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001287 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001288
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001289 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001290}
1291
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001292static LVILatticeVal
1293getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1294 DenseMap<Value*, LVILatticeVal> &Visited);
1295
1296static LVILatticeVal
1297getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
1298 DenseMap<Value*, LVILatticeVal> &Visited) {
1299 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1300 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1301
1302 // Handle conditions in the form of (cond1 && cond2), we know that on the
1303 // true dest path both of the conditions hold.
1304 if (!isTrueDest)
1305 return LVILatticeVal::getOverdefined();
1306
1307 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
1308 if (!BO || BO->getOpcode() != BinaryOperator::And)
1309 return LVILatticeVal::getOverdefined();
1310
1311 auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
1312 auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
1313 return intersect(RHS, LHS);
1314}
1315
1316static LVILatticeVal
1317getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1318 DenseMap<Value*, LVILatticeVal> &Visited) {
1319 auto I = Visited.find(Cond);
1320 if (I != Visited.end())
1321 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001322
1323 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1324 Visited[Cond] = Result;
1325 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001326}
1327
1328LVILatticeVal getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest) {
1329 assert(Cond && "precondition");
1330 DenseMap<Value*, LVILatticeVal> Visited;
1331 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1332}
1333
Nuno Lopese6e04902012-06-28 01:16:18 +00001334/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001335/// Val is not constrained on the edge. Result is unspecified if return value
1336/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001337static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1338 BasicBlock *BBTo, LVILatticeVal &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001339 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001340 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001341 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1342 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001343 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001344 if (BI->isConditional() &&
1345 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1346 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1347 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1348 "BBTo isn't a successor of BBFrom");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001349
Chris Lattner19019ea2009-11-11 22:48:44 +00001350 // If V is the condition of the branch itself, then we know exactly what
1351 // it is.
Nick Lewycky55a700b2010-12-18 01:00:40 +00001352 if (BI->getCondition() == Val) {
1353 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001354 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001355 return true;
1356 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001357
Chris Lattner19019ea2009-11-11 22:48:44 +00001358 // If the condition of the branch is an equality comparison, we may be
1359 // able to infer the value.
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001360 Result = getValueFromCondition(Val, BI->getCondition(), isTrueDest);
1361 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001362 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001363 }
1364 }
Chris Lattner77358782009-11-15 20:02:12 +00001365
1366 // If the edge was formed by a switch on the value, then we may know exactly
1367 // what it is.
1368 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001369 if (SI->getCondition() != Val)
1370 return false;
1371
1372 bool DefaultCase = SI->getDefaultDest() == BBTo;
1373 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1374 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1375
Hans Wennborgcbb18e32014-11-21 19:07:46 +00001376 for (SwitchInst::CaseIt i : SI->cases()) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001377 ConstantRange EdgeVal(i.getCaseValue()->getValue());
Manman Renf3fedb62012-09-05 23:45:58 +00001378 if (DefaultCase) {
1379 // It is possible that the default destination is the destination of
1380 // some cases. There is no need to perform difference for those cases.
1381 if (i.getCaseSuccessor() != BBTo)
1382 EdgesVals = EdgesVals.difference(EdgeVal);
1383 } else if (i.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001384 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001385 }
Benjamin Kramer2337c1f2016-02-20 10:40:34 +00001386 Result = LVILatticeVal::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001387 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001388 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001389 return false;
1390}
1391
Sanjay Patel938e2792015-01-09 16:35:37 +00001392/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1393/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001394bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Hal Finkel7e184492014-09-07 20:29:59 +00001395 BasicBlock *BBTo, LVILatticeVal &Result,
1396 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001397 // If already a constant, there is nothing to compute.
1398 if (Constant *VC = dyn_cast<Constant>(Val)) {
1399 Result = LVILatticeVal::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001400 return true;
1401 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001402
Philip Reames44456b82016-02-02 03:15:40 +00001403 LVILatticeVal LocalResult;
1404 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1405 // If we couldn't constrain the value on the edge, LocalResult doesn't
1406 // provide any information.
Philip Reames1baaef12016-12-06 03:01:08 +00001407 LocalResult = LVILatticeVal::getOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001408
Philip Reames44456b82016-02-02 03:15:40 +00001409 if (hasSingleValue(LocalResult)) {
1410 // Can't get any more precise here
1411 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001412 return true;
1413 }
1414
1415 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001416 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1417 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001418 // No new information.
1419 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001420 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001421 }
1422
Philip Reames44456b82016-02-02 03:15:40 +00001423 // Try to intersect ranges of the BB and the constraint on the edge.
1424 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001425 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1426 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001427 // We can use the context instruction (generically the ultimate instruction
1428 // the calling pass is trying to simplify) here, even though the result of
1429 // this function is generally cached when called from the solve* functions
1430 // (and that cached result might be used with queries using a different
1431 // context instruction), because when this function is called from the solve*
1432 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001433 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001434 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001435 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001436
1437 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001438 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001439}
1440
Philip Reames92e5e1b2016-09-12 21:46:58 +00001441LVILatticeVal LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
Hal Finkel7e184492014-09-07 20:29:59 +00001442 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001443 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001444 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001445
Hans Wennborg45172ac2014-11-25 17:23:05 +00001446 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001447 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001448 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001449 solve();
1450 }
Owen Andersonc7ed4dc2010-12-09 06:14:58 +00001451 LVILatticeVal Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001452 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001453
1454 DEBUG(dbgs() << " Result = " << Result << "\n");
1455 return Result;
1456}
1457
Philip Reames92e5e1b2016-09-12 21:46:58 +00001458LVILatticeVal LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001459 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1460 << CxtI->getName() << "'\n");
1461
Philip Reamesbb781b42016-02-10 21:46:32 +00001462 if (auto *C = dyn_cast<Constant>(V))
1463 return LVILatticeVal::get(C);
1464
Philip Reamesd1f829d2016-02-02 21:57:37 +00001465 LVILatticeVal Result = LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001466 if (auto *I = dyn_cast<Instruction>(V))
1467 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001468 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001469
David Greene37e98092009-12-23 20:43:58 +00001470 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001471 return Result;
1472}
Chris Lattner19019ea2009-11-11 22:48:44 +00001473
Philip Reames92e5e1b2016-09-12 21:46:58 +00001474LVILatticeVal LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001475getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1476 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001477 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001478 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001479
Nick Lewycky55a700b2010-12-18 01:00:40 +00001480 LVILatticeVal Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001481 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001482 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001483 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001484 (void)WasFastQuery;
1485 assert(WasFastQuery && "More work to do after problem solved?");
1486 }
1487
David Greene37e98092009-12-23 20:43:58 +00001488 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001489 return Result;
1490}
1491
Philip Reames92e5e1b2016-09-12 21:46:58 +00001492void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames9db79482016-09-12 22:38:44 +00001493 BasicBlock *NewSucc) {
1494 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001495}
1496
Chris Lattneraf025d32009-11-15 19:59:49 +00001497//===----------------------------------------------------------------------===//
1498// LazyValueInfo Impl
1499//===----------------------------------------------------------------------===//
1500
Philip Reames92e5e1b2016-09-12 21:46:58 +00001501/// This lazily constructs the LazyValueInfoImpl.
1502static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1503 const DataLayout *DL,
1504 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001505 if (!PImpl) {
1506 assert(DL && "getCache() called with a null DataLayout");
Philip Reames92e5e1b2016-09-12 21:46:58 +00001507 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001508 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001509 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001510}
1511
Sean Silva687019f2016-06-13 22:01:25 +00001512bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
1513 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001514 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001515
1516 DominatorTreeWrapperPass *DTWP =
1517 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001518 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1519 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001520
Sean Silva687019f2016-06-13 22:01:25 +00001521 if (Info.PImpl)
Philip Reames92e5e1b2016-09-12 21:46:58 +00001522 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001523
Owen Anderson208636f2010-08-18 18:39:01 +00001524 // Fully lazy.
1525 return false;
1526}
1527
Sean Silva687019f2016-06-13 22:01:25 +00001528void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001529 AU.setPreservesAll();
Chandler Carruth66b31302015-01-04 12:03:27 +00001530 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001531 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001532}
1533
Sean Silva687019f2016-06-13 22:01:25 +00001534LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1535
1536LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1537
Chris Lattneraf025d32009-11-15 19:59:49 +00001538void LazyValueInfo::releaseMemory() {
1539 // If the cache was allocated, free it.
1540 if (PImpl) {
Philip Reames92e5e1b2016-09-12 21:46:58 +00001541 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001542 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001543 }
1544}
1545
Sean Silva687019f2016-06-13 22:01:25 +00001546void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1547
1548LazyValueInfo LazyValueAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
1549 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
1550 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1551 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1552
1553 return LazyValueInfo(&AC, &TLI, DT);
1554}
1555
Wei Mif160e342016-09-15 06:28:34 +00001556/// Returns true if we can statically tell that this value will never be a
1557/// "useful" constant. In practice, this means we've got something like an
1558/// alloca or a malloc call for which a comparison against a constant can
1559/// only be guarding dead code. Note that we are potentially giving up some
1560/// precision in dead code (a constant result) in favour of avoiding a
1561/// expensive search for a easily answered common query.
1562static bool isKnownNonConstant(Value *V) {
1563 V = V->stripPointerCasts();
1564 // The return val of alloc cannot be a Constant.
1565 if (isa<AllocaInst>(V))
1566 return true;
1567 return false;
1568}
1569
Hal Finkel7e184492014-09-07 20:29:59 +00001570Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1571 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001572 // Bail out early if V is known not to be a Constant.
1573 if (isKnownNonConstant(V))
1574 return nullptr;
1575
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001576 const DataLayout &DL = BB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001577 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001578 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001579
Chris Lattner19019ea2009-11-11 22:48:44 +00001580 if (Result.isConstant())
1581 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001582 if (Result.isConstantRange()) {
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001583 ConstantRange CR = Result.getConstantRange();
1584 if (const APInt *SingleVal = CR.getSingleElement())
1585 return ConstantInt::get(V->getContext(), *SingleVal);
1586 }
Craig Topper9f008862014-04-15 04:59:12 +00001587 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001588}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001589
John Regehre1c481d2016-05-02 19:58:00 +00001590ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001591 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001592 assert(V->getType()->isIntegerTy());
1593 unsigned Width = V->getType()->getIntegerBitWidth();
1594 const DataLayout &DL = BB->getModule()->getDataLayout();
1595 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001596 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001597 if (Result.isUndefined())
1598 return ConstantRange(Width, /*isFullSet=*/false);
1599 if (Result.isConstantRange())
1600 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001601 // We represent ConstantInt constants as constant ranges but other kinds
1602 // of integer constants, i.e. ConstantExpr will be tagged as constants
1603 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1604 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001605 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001606}
1607
Sanjay Patel2a385e22015-01-09 16:47:20 +00001608/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001609/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001610Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001611 BasicBlock *ToBB,
1612 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001613 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001614 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001615 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001616
Chris Lattnerd5e25432009-11-12 01:29:10 +00001617 if (Result.isConstant())
1618 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001619 if (Result.isConstantRange()) {
Owen Anderson185fe002010-08-10 20:03:09 +00001620 ConstantRange CR = Result.getConstantRange();
1621 if (const APInt *SingleVal = CR.getSingleElement())
1622 return ConstantInt::get(V->getContext(), *SingleVal);
1623 }
Craig Topper9f008862014-04-15 04:59:12 +00001624 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001625}
1626
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001627static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
1628 LVILatticeVal &Result,
1629 const DataLayout &DL,
1630 TargetLibraryInfo *TLI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001631
Chris Lattner565ee2f2009-11-12 04:36:58 +00001632 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001633 Constant *Res = nullptr;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001634 if (Result.isConstant()) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001635 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001636 TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001637 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001638 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1639 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001640 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001641
Owen Anderson185fe002010-08-10 20:03:09 +00001642 if (Result.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001643 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001644 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001645
Owen Anderson185fe002010-08-10 20:03:09 +00001646 ConstantRange CR = Result.getConstantRange();
1647 if (Pred == ICmpInst::ICMP_EQ) {
1648 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001649 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001650
Owen Anderson185fe002010-08-10 20:03:09 +00001651 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001652 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001653 } else if (Pred == ICmpInst::ICMP_NE) {
1654 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001655 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001656
Owen Anderson185fe002010-08-10 20:03:09 +00001657 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001658 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001659 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001660
Owen Anderson185fe002010-08-10 20:03:09 +00001661 // Handle more complex predicates.
Sanjoy Das1f7b8132016-10-02 00:09:57 +00001662 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1663 (ICmpInst::Predicate)Pred, CI->getValue());
Nick Lewycky11678bd2010-12-15 18:57:18 +00001664 if (TrueValues.contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001665 return LazyValueInfo::True;
Nick Lewycky11678bd2010-12-15 18:57:18 +00001666 if (TrueValues.inverse().contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001667 return LazyValueInfo::False;
1668 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001669 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001670
Chris Lattneraf025d32009-11-15 19:59:49 +00001671 if (Result.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001672 // If this is an equality comparison, we can try to fold it knowing that
1673 // "V != C1".
1674 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001675 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001676 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001677 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001678 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001679 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001680 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001681 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001682 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001683 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001684 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001685 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001686 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001687 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001688 }
Hal Finkel7e184492014-09-07 20:29:59 +00001689 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001690 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001691
Hal Finkel7e184492014-09-07 20:29:59 +00001692 return LazyValueInfo::Unknown;
1693}
1694
Sanjay Patel2a385e22015-01-09 16:47:20 +00001695/// Determine whether the specified value comparison with a constant is known to
1696/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001697LazyValueInfo::Tristate
1698LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1699 BasicBlock *FromBB, BasicBlock *ToBB,
1700 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001701 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001702 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001703 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001704
1705 return getPredicateResult(Pred, C, Result, DL, TLI);
1706}
1707
1708LazyValueInfo::Tristate
1709LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1710 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001711 // Is or is not NonNull are common predicates being queried. If
1712 // isKnownNonNull can tell us the result of the predicate, we can
1713 // return it quickly. But this is only a fastpath, and falling
1714 // through would still be correct.
1715 if (V->getType()->isPointerTy() && C->isNullValue() &&
1716 isKnownNonNull(V->stripPointerCasts())) {
1717 if (Pred == ICmpInst::ICMP_EQ)
1718 return LazyValueInfo::False;
1719 else if (Pred == ICmpInst::ICMP_NE)
1720 return LazyValueInfo::True;
1721 }
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001722 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001723 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001724 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1725 if (Ret != Unknown)
1726 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001727
Philip Reamesaeefae02015-11-04 01:47:04 +00001728 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1729 // LVI as a whole tries to compute a lattice value which is conservatively
1730 // correct at a given location. In this case, we have a predicate which we
1731 // weren't able to prove about the merged result, and we're pushing that
1732 // predicate back along each incoming edge to see if we can prove it
1733 // separately for each input. As a motivating example, consider:
1734 // bb1:
1735 // %v1 = ... ; constantrange<1, 5>
1736 // br label %merge
1737 // bb2:
1738 // %v2 = ... ; constantrange<10, 20>
1739 // br label %merge
1740 // merge:
1741 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1742 // %pred = icmp eq i32 %phi, 8
1743 // We can't tell from the lattice value for '%phi' that '%pred' is false
1744 // along each path, but by checking the predicate over each input separately,
1745 // we can.
1746 // We limit the search to one step backwards from the current BB and value.
1747 // We could consider extending this to search further backwards through the
1748 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001749 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001750 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001751 BasicBlock *BB = CxtI->getParent();
1752
1753 // Function entry or an unreachable block. Bail to avoid confusing
1754 // analysis below.
1755 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1756 if (PI == PE)
1757 return Unknown;
1758
1759 // If V is a PHI node in the same block as the context, we need to ask
1760 // questions about the predicate as applied to the incoming value along
1761 // each edge. This is useful for eliminating cases where the predicate is
1762 // known along all incoming edges.
1763 if (auto *PHI = dyn_cast<PHINode>(V))
1764 if (PHI->getParent() == BB) {
1765 Tristate Baseline = Unknown;
1766 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1767 Value *Incoming = PHI->getIncomingValue(i);
1768 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001769 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001770 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1771 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001772
Philip Reamesbb11d622015-08-31 18:31:48 +00001773 // Keep going as long as we've seen a consistent known result for
1774 // all inputs.
1775 Baseline = (i == 0) ? Result /* First iteration */
1776 : (Baseline == Result ? Baseline : Unknown); /* All others */
1777 if (Baseline == Unknown)
1778 break;
1779 }
1780 if (Baseline != Unknown)
1781 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001782 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001783
Philip Reames66ab0f02015-06-16 00:49:59 +00001784 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001785 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001786 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001787 if (!isa<Instruction>(V) ||
1788 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001789 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001790 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001791 // the value of the comparison in this block.
1792 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1793 if (Baseline != Unknown) {
1794 // Check that all remaining incoming values match the first one.
1795 while (++PI != PE) {
1796 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1797 if (Ret != Baseline) break;
1798 }
1799 // If we terminated early, then one of the values didn't match.
1800 if (PI == PE) {
1801 return Baseline;
1802 }
1803 }
1804 }
1805 }
1806 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001807}
1808
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001809void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001810 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001811 if (PImpl) {
1812 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001813 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001814 }
Owen Anderson208636f2010-08-18 18:39:01 +00001815}
1816
1817void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001818 if (PImpl) {
1819 const DataLayout &DL = BB->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001820 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001821 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001822}