blob: 102081e721ac6b053b5d059c04afd08227c105aa [file] [log] [blame]
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner741c94c2009-11-11 00:22:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000021#include "llvm/Analysis/ValueTracking.h"
Anna Thomase27b39a2017-03-22 19:27:12 +000022#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000023#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000024#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
Hal Finkel7e184492014-09-07 20:29:59 +000027#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.h"
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +000030#include "llvm/IR/Intrinsics.h"
Philip Reameseb3e9da2015-10-29 03:57:17 +000031#include "llvm/IR/LLVMContext.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000032#include "llvm/IR/PatternMatch.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000033#include "llvm/IR/ValueHandle.h"
Chris Lattnerb584d1e2009-11-12 01:22:16 +000034#include "llvm/Support/Debug.h"
Anna Thomase27b39a2017-03-22 19:27:12 +000035#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Support/raw_ostream.h"
Bill Wendling4ec081a2012-01-11 23:43:34 +000037#include <map>
Nick Lewycky55a700b2010-12-18 01:00:40 +000038#include <stack>
Chris Lattner741c94c2009-11-11 00:22:30 +000039using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000040using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000041
Chandler Carruthf1221bd2014-04-22 02:48:03 +000042#define DEBUG_TYPE "lazy-value-info"
43
Daniel Berlin9c92a462017-02-08 15:22:52 +000044// This is the number of worklist items we will process to try to discover an
45// answer for a given value.
46static const unsigned MaxProcessedPerValue = 500;
47
Sean Silva687019f2016-06-13 22:01:25 +000048char LazyValueInfoWrapperPass::ID = 0;
49INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosier43a33062011-12-02 01:26:24 +000050 "Lazy Value Information Analysis", false, true)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000051INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000052INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva687019f2016-06-13 22:01:25 +000053INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000054 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000055
56namespace llvm {
Sean Silva687019f2016-06-13 22:01:25 +000057 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner741c94c2009-11-11 00:22:30 +000058}
59
Chandler Carruthdab4eae2016-11-23 17:53:26 +000060AnalysisKey LazyValueAnalysis::Key;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000061
62//===----------------------------------------------------------------------===//
63// LVILatticeVal
64//===----------------------------------------------------------------------===//
65
Sanjay Patel2a385e22015-01-09 16:47:20 +000066/// This is the information tracked by LazyValueInfo for each value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000067///
68/// FIXME: This is basically just for bringup, this can be made a lot more rich
69/// in the future.
70///
71namespace {
72class LVILatticeVal {
73 enum LatticeValueTy {
Philip Reames3bb28322016-04-25 18:48:43 +000074 /// This Value has no known value yet. As a result, this implies the
75 /// producing instruction is dead. Caution: We use this as the starting
76 /// state in our local meet rules. In this usage, it's taken to mean
NAKAMURA Takumif2529512016-07-04 01:26:27 +000077 /// "nothing known yet".
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000078 undefined,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000079
Philip Reames02bb6a62016-12-07 04:48:50 +000080 /// This Value has a specific constant value. (For constant integers,
81 /// constantrange is used instead. Integer typed constantexprs can appear
82 /// as constant.)
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000083 constant,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000084
Philip Reames02bb6a62016-12-07 04:48:50 +000085 /// This Value is known to not have the specified value. (For constant
86 /// integers, constantrange is used instead. As above, integer typed
87 /// constantexprs can appear here.)
Chris Lattner565ee2f2009-11-12 04:36:58 +000088 notconstant,
Chad Rosier43a33062011-12-02 01:26:24 +000089
Philip Reames3bb28322016-04-25 18:48:43 +000090 /// The Value falls within this range. (Used only for integer typed values.)
Owen Anderson0f306a42010-08-05 22:59:19 +000091 constantrange,
Chad Rosier43a33062011-12-02 01:26:24 +000092
Philip Reames3bb28322016-04-25 18:48:43 +000093 /// We can not precisely model the dynamic values this value might take.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000094 overdefined
95 };
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000096
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000097 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattner565ee2f2009-11-12 04:36:58 +000098 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersonc3a14132010-08-05 22:10:46 +000099 LatticeValueTy Tag;
100 Constant *Val;
Owen Anderson0f306a42010-08-05 22:59:19 +0000101 ConstantRange Range;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000102
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000103public:
Craig Topper9f008862014-04-15 04:59:12 +0000104 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000105
Chris Lattner19019ea2009-11-11 22:48:44 +0000106 static LVILatticeVal get(Constant *C) {
107 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000108 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000109 Res.markConstant(C);
Chris Lattner19019ea2009-11-11 22:48:44 +0000110 return Res;
111 }
Chris Lattner565ee2f2009-11-12 04:36:58 +0000112 static LVILatticeVal getNot(Constant *C) {
113 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000114 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000115 Res.markNotConstant(C);
Chris Lattner565ee2f2009-11-12 04:36:58 +0000116 return Res;
117 }
Owen Anderson5f1dd092010-08-10 23:20:01 +0000118 static LVILatticeVal getRange(ConstantRange CR) {
119 LVILatticeVal Res;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000120 Res.markConstantRange(std::move(CR));
Owen Anderson5f1dd092010-08-10 23:20:01 +0000121 return Res;
122 }
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000123 static LVILatticeVal getOverdefined() {
124 LVILatticeVal Res;
125 Res.markOverdefined();
126 return Res;
127 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000128
Owen Anderson0f306a42010-08-05 22:59:19 +0000129 bool isUndefined() const { return Tag == undefined; }
130 bool isConstant() const { return Tag == constant; }
131 bool isNotConstant() const { return Tag == notconstant; }
132 bool isConstantRange() const { return Tag == constantrange; }
133 bool isOverdefined() const { return Tag == overdefined; }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000134
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000135 Constant *getConstant() const {
136 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000137 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000138 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000139
Chris Lattner565ee2f2009-11-12 04:36:58 +0000140 Constant *getNotConstant() const {
141 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000142 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000143 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000144
Craig Topper2b195fd2017-05-06 03:35:15 +0000145 const ConstantRange &getConstantRange() const {
Owen Anderson0f306a42010-08-05 22:59:19 +0000146 assert(isConstantRange() &&
147 "Cannot get the constant-range of a non-constant-range!");
148 return Range;
149 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000150
Philip Reames1baaef12016-12-06 03:01:08 +0000151private:
Philip Reames71a49672016-12-07 01:03:56 +0000152 void markOverdefined() {
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000153 if (isOverdefined())
Philip Reames71a49672016-12-07 01:03:56 +0000154 return;
Owen Andersonc3a14132010-08-05 22:10:46 +0000155 Tag = overdefined;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000156 }
157
Philip Reames71a49672016-12-07 01:03:56 +0000158 void markConstant(Constant *V) {
Nick Lewycky11678bd2010-12-15 18:57:18 +0000159 assert(V && "Marking constant with NULL");
Philip Reames71a49672016-12-07 01:03:56 +0000160 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
161 markConstantRange(ConstantRange(CI->getValue()));
162 return;
163 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000164 if (isa<UndefValue>(V))
Philip Reames71a49672016-12-07 01:03:56 +0000165 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000166
167 assert((!isConstant() || getConstant() == V) &&
168 "Marking constant with different value");
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000169 assert(isUndefined());
Owen Andersonc3a14132010-08-05 22:10:46 +0000170 Tag = constant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000171 Val = V;
Chris Lattner19019ea2009-11-11 22:48:44 +0000172 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000173
Philip Reames71a49672016-12-07 01:03:56 +0000174 void markNotConstant(Constant *V) {
Chris Lattner565ee2f2009-11-12 04:36:58 +0000175 assert(V && "Marking constant with NULL");
Philip Reames71a49672016-12-07 01:03:56 +0000176 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
177 markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
178 return;
179 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000180 if (isa<UndefValue>(V))
Philip Reames71a49672016-12-07 01:03:56 +0000181 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000182
183 assert((!isConstant() || getConstant() != V) &&
184 "Marking constant !constant with same value");
185 assert((!isNotConstant() || getNotConstant() == V) &&
186 "Marking !constant with different value");
187 assert(isUndefined() || isConstant());
188 Tag = notconstant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000189 Val = V;
Chris Lattner565ee2f2009-11-12 04:36:58 +0000190 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000191
Philip Reames71a49672016-12-07 01:03:56 +0000192 void markConstantRange(ConstantRange NewR) {
Owen Anderson0f306a42010-08-05 22:59:19 +0000193 if (isConstantRange()) {
194 if (NewR.isEmptySet())
Philip Reames71a49672016-12-07 01:03:56 +0000195 markOverdefined();
196 else {
Philip Reames71a49672016-12-07 01:03:56 +0000197 Range = std::move(NewR);
198 }
199 return;
Owen Anderson0f306a42010-08-05 22:59:19 +0000200 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000201
Owen Anderson0f306a42010-08-05 22:59:19 +0000202 assert(isUndefined());
203 if (NewR.isEmptySet())
Philip Reames71a49672016-12-07 01:03:56 +0000204 markOverdefined();
205 else {
206 Tag = constantrange;
207 Range = std::move(NewR);
208 }
Owen Anderson0f306a42010-08-05 22:59:19 +0000209 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000210
Philip Reamesb2949622016-12-06 02:54:16 +0000211public:
212
Sanjay Patel2a385e22015-01-09 16:47:20 +0000213 /// Merge the specified lattice value into this one, updating this
Chris Lattner19019ea2009-11-11 22:48:44 +0000214 /// one and returning true if anything changed.
Philip Reamesb47a7192016-12-07 00:54:21 +0000215 void mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
216 if (RHS.isUndefined() || isOverdefined())
217 return;
218 if (RHS.isOverdefined()) {
219 markOverdefined();
220 return;
221 }
Chris Lattner19019ea2009-11-11 22:48:44 +0000222
Nick Lewycky11678bd2010-12-15 18:57:18 +0000223 if (isUndefined()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000224 *this = RHS;
225 return;
Chris Lattner22db4b52009-11-12 04:57:13 +0000226 }
227
Nick Lewycky11678bd2010-12-15 18:57:18 +0000228 if (isConstant()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000229 if (RHS.isConstant() && Val == RHS.Val)
230 return;
231 markOverdefined();
232 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000233 }
234
235 if (isNotConstant()) {
Philip Reamesb47a7192016-12-07 00:54:21 +0000236 if (RHS.isNotConstant() && Val == RHS.Val)
237 return;
238 markOverdefined();
239 return;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000240 }
241
242 assert(isConstantRange() && "New LVILattice type?");
Philip Reames02bb6a62016-12-07 04:48:50 +0000243 if (!RHS.isConstantRange()) {
244 // We can get here if we've encountered a constantexpr of integer type
245 // and merge it with a constantrange.
246 markOverdefined();
247 return;
248 }
Nick Lewycky11678bd2010-12-15 18:57:18 +0000249 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
250 if (NewR.isFullSet())
Philip Reamesb47a7192016-12-07 00:54:21 +0000251 markOverdefined();
252 else
Craig Topper2b195fd2017-05-06 03:35:15 +0000253 markConstantRange(std::move(NewR));
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000254 }
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000255};
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000256
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000257} // end anonymous namespace.
258
Chris Lattner19019ea2009-11-11 22:48:44 +0000259namespace llvm {
Chandler Carruth2b1ba482011-04-18 18:49:44 +0000260raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
261 LLVM_ATTRIBUTE_USED;
Chris Lattner19019ea2009-11-11 22:48:44 +0000262raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
263 if (Val.isUndefined())
264 return OS << "undefined";
265 if (Val.isOverdefined())
266 return OS << "overdefined";
Chris Lattner565ee2f2009-11-12 04:36:58 +0000267
268 if (Val.isNotConstant())
269 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Davide Italianobd543d02016-05-25 22:29:34 +0000270 if (Val.isConstantRange())
Owen Anderson8afac042010-08-09 20:50:46 +0000271 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
272 << Val.getConstantRange().getUpper() << '>';
Chris Lattner19019ea2009-11-11 22:48:44 +0000273 return OS << "constant<" << *Val.getConstant() << '>';
274}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000275}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000276
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000277/// Returns true if this lattice value represents at most one possible value.
278/// This is as precise as any lattice value can get while still representing
279/// reachable code.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000280static bool hasSingleValue(const LVILatticeVal &Val) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000281 if (Val.isConstantRange() &&
282 Val.getConstantRange().isSingleElement())
283 // Integer constants are single element ranges
284 return true;
285 if (Val.isConstant())
286 // Non integer constants
287 return true;
288 return false;
289}
290
291/// Combine two sets of facts about the same value into a single set of
292/// facts. Note that this method is not suitable for merging facts along
293/// different paths in a CFG; that's what the mergeIn function is for. This
294/// is for merging facts gathered about the same value at the same location
295/// through two independent means.
296/// Notes:
297/// * This method does not promise to return the most precise possible lattice
298/// value implied by A and B. It is allowed to return any lattice element
299/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000300/// conflict, see below).
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000301/// * Due to unreachable code, the intersection of two lattice values could be
302/// contradictory. If this happens, we return some valid lattice value so as
303/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
304/// we do not make this guarantee. TODO: This would be a useful enhancement.
Craig Topperdb528092017-06-08 17:08:58 +0000305static LVILatticeVal intersect(const LVILatticeVal &A, const LVILatticeVal &B) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000306 // Undefined is the strongest state. It means the value is known to be along
307 // an unreachable path.
308 if (A.isUndefined())
309 return A;
310 if (B.isUndefined())
311 return B;
312
313 // If we gave up for one, but got a useable fact from the other, use it.
314 if (A.isOverdefined())
315 return B;
316 if (B.isOverdefined())
317 return A;
318
319 // Can't get any more precise than constants.
320 if (hasSingleValue(A))
321 return A;
322 if (hasSingleValue(B))
323 return B;
324
325 // Could be either constant range or not constant here.
326 if (!A.isConstantRange() || !B.isConstantRange()) {
327 // TODO: Arbitrary choice, could be improved
328 return A;
329 }
330
331 // Intersect two constant ranges
332 ConstantRange Range =
333 A.getConstantRange().intersectWith(B.getConstantRange());
334 // Note: An empty range is implicitly converted to overdefined internally.
335 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000336 // and thus know this path must be unreachable.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000337 return LVILatticeVal::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000338}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000339
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000340//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000341// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000342//===----------------------------------------------------------------------===//
343
Chris Lattneraf025d32009-11-15 19:59:49 +0000344namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000345 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000346 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000347 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000348 // Needs to access getValPtr(), which is protected.
349 friend struct DenseMapInfo<LVIValueHandle>;
350
Owen Anderson118ac802011-01-05 21:15:29 +0000351 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000352
Owen Anderson118ac802011-01-05 21:15:29 +0000353 LVIValueHandle(Value *V, LazyValueInfoCache *P)
354 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000355
356 void deleted() override;
357 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000358 deleted();
359 }
360 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000361} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000362
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000363namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000364 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000365 /// maintains information about queries across the clients' queries.
366 class LazyValueInfoCache {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000367 /// This is all of the cached block information for exactly one Value*.
368 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000369 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000370 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
371 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000372 struct ValueCacheEntryTy {
373 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
374 LVIValueHandle Handle;
Chandler Carruth6acdca72017-01-24 12:55:57 +0000375 SmallDenseMap<PoisoningVH<BasicBlock>, LVILatticeVal, 4> BlockVals;
Justin Lebar58b377e2016-07-27 22:33:36 +0000376 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000377
Sanjay Patel2a385e22015-01-09 16:47:20 +0000378 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000379 /// over-defined at the end of that block.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000380 typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000381 OverDefinedCacheTy;
Sanjay Patel2a385e22015-01-09 16:47:20 +0000382 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000383 /// don't spend time removing unused blocks from our caches.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000384 DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
Benjamin Kramer36647082011-12-03 15:16:45 +0000385
Anna Thomase27b39a2017-03-22 19:27:12 +0000386 /// This is all of the cached information for all values,
387 /// mapped from Value* to key information.
388 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
389 OverDefinedCacheTy OverDefinedCache;
390
391
Philip Reames9db79482016-09-12 22:38:44 +0000392 public:
Hans Wennborg45172ac2014-11-25 17:23:05 +0000393 void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
394 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000395
396 // Insert over-defined values into their own cache to reduce memory
397 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000398 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000399 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-07-27 22:33:36 +0000400 else {
401 auto It = ValueCache.find_as(Val);
402 if (It == ValueCache.end()) {
403 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
404 It = ValueCache.find_as(Val);
405 assert(It != ValueCache.end() && "Val was just added to the map!");
406 }
407 It->second->BlockVals[BB] = Result;
408 }
Hans Wennborg45172ac2014-11-25 17:23:05 +0000409 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000410
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000411 bool isOverdefined(Value *V, BasicBlock *BB) const {
412 auto ODI = OverDefinedCache.find(BB);
413
414 if (ODI == OverDefinedCache.end())
415 return false;
416
417 return ODI->second.count(V);
418 }
419
Philip Reames9db79482016-09-12 22:38:44 +0000420 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000421 if (isOverdefined(V, BB))
422 return true;
423
Justin Lebar58b377e2016-07-27 22:33:36 +0000424 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000425 if (I == ValueCache.end())
426 return false;
427
Justin Lebar58b377e2016-07-27 22:33:36 +0000428 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000429 }
430
Philip Reames9db79482016-09-12 22:38:44 +0000431 LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000432 if (isOverdefined(V, BB))
433 return LVILatticeVal::getOverdefined();
434
Justin Lebar58b377e2016-07-27 22:33:36 +0000435 auto I = ValueCache.find_as(V);
436 if (I == ValueCache.end())
437 return LVILatticeVal();
438 auto BBI = I->second->BlockVals.find(BB);
439 if (BBI == I->second->BlockVals.end())
440 return LVILatticeVal();
441 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000442 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000443
Philip Reames92e5e1b2016-09-12 21:46:58 +0000444 /// clear - Empty the cache.
445 void clear() {
446 SeenBlocks.clear();
447 ValueCache.clear();
448 OverDefinedCache.clear();
449 }
450
Philip Reamesb627aec2016-09-12 22:03:36 +0000451 /// Inform the cache that a given value has been deleted.
452 void eraseValue(Value *V);
453
454 /// This is part of the update interface to inform the cache
455 /// that a block has been deleted.
456 void eraseBlock(BasicBlock *BB);
457
Philip Reames9db79482016-09-12 22:38:44 +0000458 /// Updates the cache to remove any influence an overdefined value in
459 /// OldSucc might have (unless also overdefined in NewSucc). This just
460 /// flushes elements from the cache and does not add any.
461 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
462
Philip Reames92e5e1b2016-09-12 21:46:58 +0000463 friend struct LVIValueHandle;
464 };
Philip Reamesb627aec2016-09-12 22:03:36 +0000465}
Philip Reames92e5e1b2016-09-12 21:46:58 +0000466
Philip Reamesb627aec2016-09-12 22:03:36 +0000467void LazyValueInfoCache::eraseValue(Value *V) {
Chandler Carruth41421df2017-01-26 08:31:54 +0000468 for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) {
469 // Copy and increment the iterator immediately so we can erase behind
470 // ourselves.
471 auto Iter = I++;
472 SmallPtrSetImpl<Value *> &ValueSet = Iter->second;
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000473 ValueSet.erase(V);
Philip Reamesb627aec2016-09-12 22:03:36 +0000474 if (ValueSet.empty())
Chandler Carruth41421df2017-01-26 08:31:54 +0000475 OverDefinedCache.erase(Iter);
Philip Reamesb627aec2016-09-12 22:03:36 +0000476 }
Philip Reamesb627aec2016-09-12 22:03:36 +0000477
478 ValueCache.erase(V);
479}
480
481void LVIValueHandle::deleted() {
482 // This erasure deallocates *this, so it MUST happen after we're done
483 // using any and all members of *this.
484 Parent->eraseValue(*this);
485}
486
487void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
488 // Shortcut if we have never seen this block.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000489 DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
Philip Reamesb627aec2016-09-12 22:03:36 +0000490 if (I == SeenBlocks.end())
491 return;
492 SeenBlocks.erase(I);
493
494 auto ODI = OverDefinedCache.find(BB);
495 if (ODI != OverDefinedCache.end())
496 OverDefinedCache.erase(ODI);
497
498 for (auto &I : ValueCache)
499 I.second->BlockVals.erase(BB);
500}
501
Philip Reames9db79482016-09-12 22:38:44 +0000502void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
503 BasicBlock *NewSucc) {
504 // When an edge in the graph has been threaded, values that we could not
505 // determine a value for before (i.e. were marked overdefined) may be
506 // possible to solve now. We do NOT try to proactively update these values.
507 // Instead, we clear their entries from the cache, and allow lazy updating to
508 // recompute them when needed.
509
510 // The updating process is fairly simple: we need to drop cached info
511 // for all values that were marked overdefined in OldSucc, and for those same
512 // values in any successor of OldSucc (except NewSucc) in which they were
513 // also marked overdefined.
514 std::vector<BasicBlock*> worklist;
515 worklist.push_back(OldSucc);
516
517 auto I = OverDefinedCache.find(OldSucc);
518 if (I == OverDefinedCache.end())
519 return; // Nothing to process here.
520 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
521
522 // Use a worklist to perform a depth-first search of OldSucc's successors.
523 // NOTE: We do not need a visited list since any blocks we have already
524 // visited will have had their overdefined markers cleared already, and we
525 // thus won't loop to their successors.
526 while (!worklist.empty()) {
527 BasicBlock *ToUpdate = worklist.back();
528 worklist.pop_back();
529
530 // Skip blocks only accessible through NewSucc.
531 if (ToUpdate == NewSucc) continue;
532
Philip Reames1e48efc2016-12-30 17:56:47 +0000533 // If a value was marked overdefined in OldSucc, and is here too...
534 auto OI = OverDefinedCache.find(ToUpdate);
535 if (OI == OverDefinedCache.end())
536 continue;
537 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
538
Philip Reames9db79482016-09-12 22:38:44 +0000539 bool changed = false;
540 for (Value *V : ValsToClear) {
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000541 if (!ValueSet.erase(V))
Philip Reames9db79482016-09-12 22:38:44 +0000542 continue;
543
Philip Reames9db79482016-09-12 22:38:44 +0000544 // If we removed anything, then we potentially need to update
545 // blocks successors too.
546 changed = true;
Philip Reames1e48efc2016-12-30 17:56:47 +0000547
548 if (ValueSet.empty()) {
549 OverDefinedCache.erase(OI);
550 break;
551 }
Philip Reames9db79482016-09-12 22:38:44 +0000552 }
553
554 if (!changed) continue;
555
556 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
557 }
558}
559
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000560
561namespace {
562/// An assembly annotator class to print LazyValueCache information in
563/// comments.
564class LazyValueInfoImpl;
565class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {
566 LazyValueInfoImpl *LVIImpl;
567 // While analyzing which blocks we can solve values for, we need the dominator
568 // information. Since this is an optional parameter in LVI, we require this
569 // DomTreeAnalysis pass in the printer pass, and pass the dominator
570 // tree to the LazyValueInfoAnnotatedWriter.
571 DominatorTree &DT;
572
573public:
574 LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)
575 : LVIImpl(L), DT(DTree) {}
576
577 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
578 formatted_raw_ostream &OS);
579
580 virtual void emitInstructionAnnot(const Instruction *I,
581 formatted_raw_ostream &OS);
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.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000596 SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
Philip Reames92e5e1b2016-09-12 21:46:58 +0000597
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");
Daniel Berlin9c92a462017-02-08 15:22:52 +0000609 BlockValueStack.push_back(BV);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000610 return true;
611 }
612
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000613 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
Philip Reames92e5e1b2016-09-12 21:46:58 +0000614 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);
Philip Reames05c435e2016-12-06 03:22:03 +0000626 bool solveBlockValueImpl(LVILatticeVal &Res, Value *Val, BasicBlock *BB);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000627 bool solveBlockValueNonLocal(LVILatticeVal &BBLV, Value *Val, BasicBlock *BB);
628 bool solveBlockValuePHINode(LVILatticeVal &BBLV, PHINode *PN, BasicBlock *BB);
629 bool solveBlockValueSelect(LVILatticeVal &BBLV, SelectInst *S,
630 BasicBlock *BB);
Craig Topper3778c892017-06-02 16:33:13 +0000631 bool solveBlockValueBinaryOp(LVILatticeVal &BBLV, BinaryOperator *BBI,
Philip Reames92e5e1b2016-09-12 21:46:58 +0000632 BasicBlock *BB);
Craig Topper0e5f1092017-06-03 07:47:08 +0000633 bool solveBlockValueCast(LVILatticeVal &BBLV, CastInst *CI,
Philip Reames92e5e1b2016-09-12 21:46:58 +0000634 BasicBlock *BB);
635 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
636 LVILatticeVal &BBLV,
Craig Topper9277a862017-06-02 17:28:12 +0000637 Instruction *BBI);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000638
639 void solve();
640
641 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000642 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000643 /// value for the specified Value* at the end of the specified block.
Hal Finkel7e184492014-09-07 20:29:59 +0000644 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
645 Instruction *CxtI = nullptr);
646
Sanjay Patel2a385e22015-01-09 16:47:20 +0000647 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000648 /// value for the specified Value* at the specified instruction (generally
649 /// from an assume intrinsic).
650 LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000651
Sanjay Patel2a385e22015-01-09 16:47:20 +0000652 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000653 /// value for the specified Value* that is true on the specified edge.
Hal Finkel7e184492014-09-07 20:29:59 +0000654 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
655 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000656
Philip Reames9db79482016-09-12 22:38:44 +0000657 /// Complete flush all previously computed values
658 void clear() {
659 TheCache.clear();
660 }
661
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000662 /// Printing the LazyValueInfo Analysis.
663 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
664 LazyValueInfoAnnotatedWriter Writer(this, DTree);
665 F.print(OS, &Writer);
Anna Thomase27b39a2017-03-22 19:27:12 +0000666 }
667
Philip Reames9db79482016-09-12 22:38:44 +0000668 /// This is part of the update interface to inform the cache
669 /// that a block has been deleted.
670 void eraseBlock(BasicBlock *BB) {
671 TheCache.eraseBlock(BB);
672 }
673
Sanjay Patel2a385e22015-01-09 16:47:20 +0000674 /// This is the update interface to inform the cache that an edge from
675 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000676 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000677
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000678 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
679 DominatorTree *DT = nullptr)
680 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000681 };
682} // end anonymous namespace
683
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000684
Philip Reames92e5e1b2016-09-12 21:46:58 +0000685void LazyValueInfoImpl::solve() {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000686 SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
687 BlockValueStack.begin(), BlockValueStack.end());
688
689 unsigned processedCount = 0;
Owen Anderson6f060af2011-01-05 23:26:22 +0000690 while (!BlockValueStack.empty()) {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000691 processedCount++;
692 // Abort if we have to process too many values to get a result for this one.
693 // Because of the design of the overdefined cache currently being per-block
694 // to avoid naming-related issues (IE it wants to try to give different
695 // results for the same name in different blocks), overdefined results don't
696 // get cached globally, which in turn means we will often try to rediscover
697 // the same overdefined result again and again. Once something like
698 // PredicateInfo is used in LVI or CVP, we should be able to make the
699 // overdefined cache global, and remove this throttle.
700 if (processedCount > MaxProcessedPerValue) {
701 DEBUG(dbgs() << "Giving up on stack because we are getting too deep\n");
702 // Fill in the original values
703 while (!StartingStack.empty()) {
704 std::pair<BasicBlock *, Value *> &e = StartingStack.back();
705 TheCache.insertResult(e.second, e.first,
706 LVILatticeVal::getOverdefined());
707 StartingStack.pop_back();
708 }
709 BlockValueSet.clear();
710 BlockValueStack.clear();
711 return;
712 }
Vitaly Buka9987d982017-02-09 09:28:05 +0000713 std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000714 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
715
Nuno Lopese6e04902012-06-28 01:16:18 +0000716 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000717 // The work item was completely processed.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000718 assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
Philip Reames9db79482016-09-12 22:38:44 +0000719 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000720 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000721
Philip Reames44456b82016-02-02 03:15:40 +0000722 DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000723 << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000724
Daniel Berlin9c92a462017-02-08 15:22:52 +0000725 BlockValueStack.pop_back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000726 BlockValueSet.erase(e);
727 } else {
728 // More work needs to be done before revisiting.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000729 assert(BlockValueStack.back() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000730 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000731 }
732}
733
Philip Reames92e5e1b2016-09-12 21:46:58 +0000734bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000735 // If already a constant, there is nothing to compute.
736 if (isa<Constant>(Val))
737 return true;
738
Philip Reames9db79482016-09-12 22:38:44 +0000739 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000740}
741
Philip Reames92e5e1b2016-09-12 21:46:58 +0000742LVILatticeVal LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000743 // If already a constant, there is nothing to compute.
744 if (Constant *VC = dyn_cast<Constant>(Val))
745 return LVILatticeVal::get(VC);
746
Philip Reames9db79482016-09-12 22:38:44 +0000747 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000748}
749
Philip Reameseb3e9da2015-10-29 03:57:17 +0000750static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
751 switch (BBI->getOpcode()) {
752 default: break;
753 case Instruction::Load:
754 case Instruction::Call:
755 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000756 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000757 if (isa<IntegerType>(BBI->getType())) {
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000758 return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000759 }
760 break;
761 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000762 // Nothing known - will be intersected with other facts
763 return LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000764}
765
Philip Reames92e5e1b2016-09-12 21:46:58 +0000766bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000767 if (isa<Constant>(Val))
768 return true;
769
Philip Reames9db79482016-09-12 22:38:44 +0000770 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000771 // If we have a cached value, use that.
772 DEBUG(dbgs() << " reuse BB '" << BB->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000773 << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000774
Hans Wennborg45172ac2014-11-25 17:23:05 +0000775 // Since we're reusing a cached value, we don't need to update the
776 // OverDefinedCache. The cache will have been properly updated whenever the
777 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000778 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000779 }
780
Hans Wennborg45172ac2014-11-25 17:23:05 +0000781 // Hold off inserting this value into the Cache in case we have to return
782 // false and come back later.
783 LVILatticeVal Res;
Philip Reames05c435e2016-12-06 03:22:03 +0000784 if (!solveBlockValueImpl(Res, Val, BB))
785 // Work pushed, will revisit
786 return false;
787
788 TheCache.insertResult(Val, BB, Res);
789 return true;
790}
791
792bool LazyValueInfoImpl::solveBlockValueImpl(LVILatticeVal &Res,
793 Value *Val, BasicBlock *BB) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000794
Chris Lattneraf025d32009-11-15 19:59:49 +0000795 Instruction *BBI = dyn_cast<Instruction>(Val);
Philip Reames05c435e2016-12-06 03:22:03 +0000796 if (!BBI || BBI->getParent() != BB)
797 return solveBlockValueNonLocal(Res, Val, BB);
Chris Lattner2c708562009-11-15 20:00:52 +0000798
Philip Reames05c435e2016-12-06 03:22:03 +0000799 if (PHINode *PN = dyn_cast<PHINode>(BBI))
800 return solveBlockValuePHINode(Res, PN, BB);
Owen Anderson80d19f02010-08-18 21:11:37 +0000801
Philip Reames05c435e2016-12-06 03:22:03 +0000802 if (auto *SI = dyn_cast<SelectInst>(BBI))
803 return solveBlockValueSelect(Res, SI, BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000804
Philip Reames2ab964e2016-04-27 01:02:25 +0000805 // If this value is a nonnull pointer, record it's range and bailout. Note
806 // that for all other pointer typed values, we terminate the search at the
807 // definition. We could easily extend this to look through geps, bitcasts,
808 // and the like to prove non-nullness, but it's not clear that's worth it
Craig Topper7ad13f22017-06-09 21:21:17 +0000809 // compile time wise. The context-insensitive value walk done inside
Philip Reames2ab964e2016-04-27 01:02:25 +0000810 // isKnownNonNull gets most of the profitable cases at much less expense.
811 // This does mean that we have a sensativity to where the defining
812 // instruction is placed, even if it could legally be hoisted much higher.
813 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000814 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
815 if (PT && isKnownNonNull(BBI)) {
816 Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000817 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000818 }
Davide Italianobd543d02016-05-25 22:29:34 +0000819 if (BBI->getType()->isIntegerTy()) {
Craig Topper0e5f1092017-06-03 07:47:08 +0000820 if (auto *CI = dyn_cast<CastInst>(BBI))
821 return solveBlockValueCast(Res, CI, BB);
822
Philip Reames2ab964e2016-04-27 01:02:25 +0000823 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
Philip Reames05c435e2016-12-06 03:22:03 +0000824 if (BO && isa<ConstantInt>(BO->getOperand(1)))
Craig Topper3778c892017-06-02 16:33:13 +0000825 return solveBlockValueBinaryOp(Res, BO, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000826 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000827
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000828 DEBUG(dbgs() << " compute BB '" << BB->getName()
829 << "' - unknown inst def found.\n");
830 Res = getFromRangeMetadata(BBI);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000831 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000832}
833
834static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
835 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
836 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000837 GetUnderlyingObject(L->getPointerOperand(),
838 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000839 }
840 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
841 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000842 GetUnderlyingObject(S->getPointerOperand(),
843 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000844 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000845 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
846 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000847
848 // FIXME: check whether it has a valuerange that excludes zero?
849 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
850 if (!Len || Len->isZero()) return false;
851
Eli Friedman7a5fc692011-05-31 20:40:16 +0000852 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000853 if (GetUnderlyingObject(MI->getRawDest(),
854 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000855 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000856 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000857 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000858 if (GetUnderlyingObject(MTI->getRawSource(),
859 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000860 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000861 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000862 return false;
863}
864
Philip Reames3f83dbe2016-04-27 00:30:55 +0000865/// Return true if the allocation associated with Val is ever dereferenced
866/// within the given basic block. This establishes the fact Val is not null,
867/// but does not imply that the memory at Val is dereferenceable. (Val may
868/// point off the end of the dereferenceable part of the object.)
869static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
870 assert(Val->getType()->isPointerTy());
871
872 const DataLayout &DL = BB->getModule()->getDataLayout();
873 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
874 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
875 // inside InstructionDereferencesPointer either.
876 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
877 for (Instruction &I : *BB)
878 if (InstructionDereferencesPointer(&I, UnderlyingVal))
879 return true;
880 return false;
881}
882
Philip Reames92e5e1b2016-09-12 21:46:58 +0000883bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000884 Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000885 LVILatticeVal Result; // Start Undefined.
886
Nick Lewycky55a700b2010-12-18 01:00:40 +0000887 // If this is the entry block, we must be asking about an argument. The
888 // value is overdefined.
889 if (BB == &BB->getParent()->getEntryBlock()) {
890 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Craig Topper96d6ee852017-04-28 16:57:59 +0000891 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000892 // this particular block.
893 if (Val->getType()->isPointerTy() &&
894 (isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
Chris Lattner229907c2011-07-18 04:54:35 +0000895 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000896 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
897 } else {
Philip Reames1baaef12016-12-06 03:01:08 +0000898 Result = LVILatticeVal::getOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000899 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000900 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000901 return true;
902 }
903
904 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000905 // result. If we encounter an unexplored predecessor, we eagerly explore it
906 // in a depth first manner. In practice, this has the effect of discovering
907 // paths we can't analyze eagerly without spending compile times analyzing
908 // other paths. This heuristic benefits from the fact that predecessors are
909 // frequently arranged such that dominating ones come first and we quickly
910 // find a path to function entry. TODO: We should consider explicitly
911 // canonicalizing to make this true rather than relying on this happy
912 // accident.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000913 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000914 LVILatticeVal EdgeResult;
Philip Reamesc80bd042017-02-07 00:25:24 +0000915 if (!getEdgeValue(Val, *PI, BB, EdgeResult))
916 // Explore that input, then return here
917 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000918
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000919 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000920
921 // If we hit overdefined, exit early. The BlockVals entry is already set
922 // to overdefined.
923 if (Result.isOverdefined()) {
924 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000925 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000926 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000927 // this particular block.
928 if (Val->getType()->isPointerTy() &&
929 isObjectDereferencedInBlock(Val, BB)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000930 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000931 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
932 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000933
Owen Anderson64c2c572010-12-20 18:18:16 +0000934 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000935 return true;
936 }
937 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000938
939 // Return the merged value, which is more precise than 'overdefined'.
940 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000941 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000942 return true;
943}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000944
Philip Reames92e5e1b2016-09-12 21:46:58 +0000945bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000946 PHINode *PN, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000947 LVILatticeVal Result; // Start Undefined.
948
949 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000950 // result. See the comment about the chosen traversal order in
951 // solveBlockValueNonLocal; the same reasoning applies here.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000952 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
953 BasicBlock *PhiBB = PN->getIncomingBlock(i);
954 Value *PhiVal = PN->getIncomingValue(i);
955 LVILatticeVal EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000956 // Note that we can provide PN as the context value to getEdgeValue, even
957 // though the results will be cached, because PN is the value being used as
958 // the cache key in the caller.
Philip Reamesc80bd042017-02-07 00:25:24 +0000959 if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
960 // Explore that input, then return here
961 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000962
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000963 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000964
965 // If we hit overdefined, exit early. The BlockVals entry is already set
966 // to overdefined.
967 if (Result.isOverdefined()) {
968 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000969 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000970
Owen Anderson64c2c572010-12-20 18:18:16 +0000971 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000972 return true;
973 }
974 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000975
976 // Return the merged value, which is more precise than 'overdefined'.
977 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000978 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000979 return true;
980}
981
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000982static LVILatticeVal getValueFromCondition(Value *Val, Value *Cond,
983 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000984
Philip Reamesd1f829d2016-02-02 21:57:37 +0000985// If we can determine a constraint on the value given conditions assumed by
986// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000987void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000988 Value *Val, LVILatticeVal &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000989 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
990 if (!BBI)
991 return;
992
Hal Finkel8a9a7832017-01-11 13:24:24 +0000993 for (auto &AssumeVH : AC->assumptionsFor(Val)) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000994 if (!AssumeVH)
Chandler Carruth66b31302015-01-04 12:03:27 +0000995 continue;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000996 auto *I = cast<CallInst>(AssumeVH);
997 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000998 continue;
999
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001000 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +00001001 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001002
1003 // If guards are not used in the module, don't spend time looking for them
1004 auto *GuardDecl = BBI->getModule()->getFunction(
1005 Intrinsic::getName(Intrinsic::experimental_guard));
1006 if (!GuardDecl || GuardDecl->use_empty())
1007 return;
1008
Artur Pilipenko47dc0982016-10-21 15:02:21 +00001009 for (Instruction &I : make_range(BBI->getIterator().getReverse(),
1010 BBI->getParent()->rend())) {
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001011 Value *Cond = nullptr;
Artur Pilipenko47dc0982016-10-21 15:02:21 +00001012 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
1013 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001014 }
Hal Finkel7e184492014-09-07 20:29:59 +00001015}
1016
Philip Reames92e5e1b2016-09-12 21:46:58 +00001017bool LazyValueInfoImpl::solveBlockValueSelect(LVILatticeVal &BBLV,
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001018 SelectInst *SI, BasicBlock *BB) {
1019
1020 // Recurse on our inputs if needed
1021 if (!hasBlockValue(SI->getTrueValue(), BB)) {
1022 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
1023 return false;
Philip Reames1baaef12016-12-06 03:01:08 +00001024 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001025 return true;
1026 }
1027 LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
1028 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
1029 // extra slots in the table if we can.
1030 if (TrueVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +00001031 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001032 return true;
1033 }
1034
1035 if (!hasBlockValue(SI->getFalseValue(), BB)) {
1036 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
1037 return false;
Philip Reames1baaef12016-12-06 03:01:08 +00001038 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001039 return true;
1040 }
1041 LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
1042 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
1043 // extra slots in the table if we can.
1044 if (FalseVal.isOverdefined()) {
Philip Reames1baaef12016-12-06 03:01:08 +00001045 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001046 return true;
1047 }
1048
Philip Reamesadf0e352016-02-26 22:53:59 +00001049 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001050 const ConstantRange &TrueCR = TrueVal.getConstantRange();
1051 const ConstantRange &FalseCR = FalseVal.getConstantRange();
Philip Reamesadf0e352016-02-26 22:53:59 +00001052 Value *LHS = nullptr;
1053 Value *RHS = nullptr;
1054 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
1055 // Is this a min specifically of our two inputs? (Avoid the risk of
1056 // ValueTracking getting smarter looking back past our immediate inputs.)
1057 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
1058 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reamesb2949622016-12-06 02:54:16 +00001059 ConstantRange ResultCR = [&]() {
1060 switch (SPR.Flavor) {
1061 default:
1062 llvm_unreachable("unexpected minmax type!");
1063 case SPF_SMIN: /// Signed minimum
1064 return TrueCR.smin(FalseCR);
1065 case SPF_UMIN: /// Unsigned minimum
1066 return TrueCR.umin(FalseCR);
1067 case SPF_SMAX: /// Signed maximum
1068 return TrueCR.smax(FalseCR);
1069 case SPF_UMAX: /// Unsigned maximum
1070 return TrueCR.umax(FalseCR);
1071 };
1072 }();
1073 BBLV = LVILatticeVal::getRange(ResultCR);
1074 return true;
Philip Reamesadf0e352016-02-26 22:53:59 +00001075 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001076
Philip Reamesadf0e352016-02-26 22:53:59 +00001077 // TODO: ABS, NABS from the SelectPatternResult
1078 }
1079
Philip Reames854a84c2016-02-12 00:09:18 +00001080 // Can we constrain the facts about the true and false values by using the
1081 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
1082 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001083 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001084 TrueVal = intersect(TrueVal,
1085 getValueFromCondition(SI->getTrueValue(), Cond, true));
1086 FalseVal = intersect(FalseVal,
1087 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +00001088
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001089 // Handle clamp idioms such as:
1090 // %24 = constantrange<0, 17>
1091 // %39 = icmp eq i32 %24, 0
1092 // %40 = add i32 %24, -1
1093 // %siv.next = select i1 %39, i32 16, i32 %40
1094 // %siv.next = constantrange<0, 17> not <-1, 17>
1095 // In general, this can handle any clamp idiom which tests the edge
1096 // condition via an equality or inequality.
1097 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +00001098 ICmpInst::Predicate Pred = ICI->getPredicate();
1099 Value *A = ICI->getOperand(0);
1100 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
1101 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
1102 assert(A->getType() == B->getType());
1103 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
1104 };
1105 // See if either input is A + C2, subject to the constraint from the
1106 // condition that A != C when that input is used. We can assume that
1107 // that input doesn't include C + C2.
1108 ConstantInt *CIAdded;
1109 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +00001110 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +00001111 case ICmpInst::ICMP_EQ:
1112 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
1113 m_ConstantInt(CIAdded)))) {
1114 auto ResNot = addConstants(CIBase, CIAdded);
1115 FalseVal = intersect(FalseVal,
1116 LVILatticeVal::getNot(ResNot));
1117 }
1118 break;
1119 case ICmpInst::ICMP_NE:
1120 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
1121 m_ConstantInt(CIAdded)))) {
1122 auto ResNot = addConstants(CIBase, CIAdded);
1123 TrueVal = intersect(TrueVal,
1124 LVILatticeVal::getNot(ResNot));
1125 }
1126 break;
1127 };
1128 }
1129 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001130
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001131 LVILatticeVal Result; // Start Undefined.
1132 Result.mergeIn(TrueVal, DL);
1133 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001134 BBLV = Result;
1135 return true;
1136}
1137
Philip Reames92e5e1b2016-09-12 21:46:58 +00001138bool LazyValueInfoImpl::solveBlockValueCast(LVILatticeVal &BBLV,
Craig Topper0e5f1092017-06-03 07:47:08 +00001139 CastInst *CI,
1140 BasicBlock *BB) {
1141 if (!CI->getOperand(0)->getType()->isSized()) {
Philip Reamese5030e82016-04-26 22:52:30 +00001142 // Without knowing how wide the input is, we can't analyze it in any useful
1143 // way.
Philip Reames1baaef12016-12-06 03:01:08 +00001144 BBLV = LVILatticeVal::getOverdefined();
Philip Reamese5030e82016-04-26 22:52:30 +00001145 return true;
1146 }
Philip Reamesf105db42016-04-26 23:27:33 +00001147
1148 // Filter out casts we don't know how to reason about before attempting to
1149 // recurse on our operand. This can cut a long search short if we know we're
1150 // not going to be able to get any useful information anways.
Craig Topper0e5f1092017-06-03 07:47:08 +00001151 switch (CI->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +00001152 case Instruction::Trunc:
1153 case Instruction::SExt:
1154 case Instruction::ZExt:
1155 case Instruction::BitCast:
1156 break;
1157 default:
1158 // Unhandled instructions are overdefined.
1159 DEBUG(dbgs() << " compute BB '" << BB->getName()
1160 << "' - overdefined (unknown cast).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001161 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001162 return true;
1163 }
1164
Philip Reames38c87c22016-04-26 21:48:16 +00001165 // Figure out the range of the LHS. If that fails, we still apply the
1166 // transfer rule on the full set since we may be able to locally infer
1167 // interesting facts.
Craig Topper0e5f1092017-06-03 07:47:08 +00001168 if (!hasBlockValue(CI->getOperand(0), BB))
1169 if (pushBlockValue(std::make_pair(BB, CI->getOperand(0))))
Philip Reames38c87c22016-04-26 21:48:16 +00001170 // More work to do before applying this transfer rule.
Hans Wennborg45172ac2014-11-25 17:23:05 +00001171 return false;
Philip Reames38c87c22016-04-26 21:48:16 +00001172
1173 const unsigned OperandBitWidth =
Craig Topper0e5f1092017-06-03 07:47:08 +00001174 DL.getTypeSizeInBits(CI->getOperand(0)->getType());
Philip Reames38c87c22016-04-26 21:48:16 +00001175 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
Craig Topper0e5f1092017-06-03 07:47:08 +00001176 if (hasBlockValue(CI->getOperand(0), BB)) {
1177 LVILatticeVal LHSVal = getBlockValue(CI->getOperand(0), BB);
1178 intersectAssumeOrGuardBlockValueConstantRange(CI->getOperand(0), LHSVal,
1179 CI);
Philip Reames38c87c22016-04-26 21:48:16 +00001180 if (LHSVal.isConstantRange())
1181 LHSRange = LHSVal.getConstantRange();
Nick Lewycky55a700b2010-12-18 01:00:40 +00001182 }
1183
Craig Toppera803d5b2017-06-03 07:47:14 +00001184 const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
Philip Reames66715772016-04-25 18:30:31 +00001185
1186 // NOTE: We're currently limited by the set of operations that ConstantRange
1187 // can evaluate symbolically. Enhancing that set will allows us to analyze
1188 // more definitions.
Craig Topper0e5f1092017-06-03 07:47:08 +00001189 BBLV = LVILatticeVal::getRange(LHSRange.castOp(CI->getOpcode(),
1190 ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001191 return true;
1192}
1193
Philip Reames92e5e1b2016-09-12 21:46:58 +00001194bool LazyValueInfoImpl::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
Craig Topper3778c892017-06-02 16:33:13 +00001195 BinaryOperator *BO,
Philip Reamese5030e82016-04-26 22:52:30 +00001196 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +00001197
Craig Topper3778c892017-06-02 16:33:13 +00001198 assert(BO->getOperand(0)->getType()->isSized() &&
Philip Reames053c2a62016-04-26 23:10:35 +00001199 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +00001200
1201 // Filter out operators we don't know how to reason about before attempting to
1202 // recurse on our operand(s). This can cut a long search short if we know
Craig Topper84a9f162017-06-02 16:21:13 +00001203 // we're not going to be able to get any useful information anyways.
Craig Topper3778c892017-06-02 16:33:13 +00001204 switch (BO->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +00001205 case Instruction::Add:
1206 case Instruction::Sub:
1207 case Instruction::Mul:
1208 case Instruction::UDiv:
1209 case Instruction::Shl:
1210 case Instruction::LShr:
1211 case Instruction::And:
1212 case Instruction::Or:
1213 // continue into the code below
1214 break;
1215 default:
1216 // Unhandled instructions are overdefined.
1217 DEBUG(dbgs() << " compute BB '" << BB->getName()
1218 << "' - overdefined (unknown binary operator).\n");
Philip Reames1baaef12016-12-06 03:01:08 +00001219 BBLV = LVILatticeVal::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001220 return true;
1221 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001222
Philip Reames053c2a62016-04-26 23:10:35 +00001223 // Figure out the range of the LHS. If that fails, use a conservative range,
1224 // but apply the transfer rule anyways. This lets us pick up facts from
1225 // expressions like "and i32 (call i32 @foo()), 32"
Craig Topper3778c892017-06-02 16:33:13 +00001226 if (!hasBlockValue(BO->getOperand(0), BB))
1227 if (pushBlockValue(std::make_pair(BB, BO->getOperand(0))))
Philip Reames053c2a62016-04-26 23:10:35 +00001228 // More work to do before applying this transfer rule.
1229 return false;
1230
1231 const unsigned OperandBitWidth =
Craig Topper3778c892017-06-02 16:33:13 +00001232 DL.getTypeSizeInBits(BO->getOperand(0)->getType());
Philip Reames053c2a62016-04-26 23:10:35 +00001233 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
Craig Topper3778c892017-06-02 16:33:13 +00001234 if (hasBlockValue(BO->getOperand(0), BB)) {
1235 LVILatticeVal LHSVal = getBlockValue(BO->getOperand(0), BB);
1236 intersectAssumeOrGuardBlockValueConstantRange(BO->getOperand(0), LHSVal,
1237 BO);
Philip Reames053c2a62016-04-26 23:10:35 +00001238 if (LHSVal.isConstantRange())
1239 LHSRange = LHSVal.getConstantRange();
Philip Reames66715772016-04-25 18:30:31 +00001240 }
Philip Reames66715772016-04-25 18:30:31 +00001241
Craig Topper3778c892017-06-02 16:33:13 +00001242 ConstantInt *RHS = cast<ConstantInt>(BO->getOperand(1));
Philip Reames66715772016-04-25 18:30:31 +00001243 ConstantRange RHSRange = ConstantRange(RHS->getValue());
1244
Owen Anderson80d19f02010-08-18 21:11:37 +00001245 // NOTE: We're currently limited by the set of operations that ConstantRange
1246 // can evaluate symbolically. Enhancing that set will allows us to analyze
1247 // more definitions.
Craig Topper3778c892017-06-02 16:33:13 +00001248 Instruction::BinaryOps BinOp = BO->getOpcode();
Philip Reames0e613f72016-12-06 02:36:58 +00001249 BBLV = LVILatticeVal::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001250 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001251}
1252
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001253static LVILatticeVal getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1254 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001255 Value *LHS = ICI->getOperand(0);
1256 Value *RHS = ICI->getOperand(1);
1257 CmpInst::Predicate Predicate = ICI->getPredicate();
1258
1259 if (isa<Constant>(RHS)) {
1260 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001261 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001262 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001263 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001264 return LVILatticeVal::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001265 else
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001266 return LVILatticeVal::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001267 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001268 }
Hal Finkel7e184492014-09-07 20:29:59 +00001269
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001270 if (!Val->getType()->isIntegerTy())
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001271 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001272
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001273 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1274 // range of Val guaranteed by the condition. Recognize comparisons in the from
1275 // of:
1276 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001277 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001278 // The latter is the range checking idiom that InstCombine produces. Subtract
1279 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001280
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001281 // Val or (add Val, Offset) can be on either hand of the comparison
1282 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1283 std::swap(LHS, RHS);
1284 Predicate = CmpInst::getSwappedPredicate(Predicate);
1285 }
Hal Finkel7e184492014-09-07 20:29:59 +00001286
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001287 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001288 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001289 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001290
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001291 if (LHS == Val || Offset) {
1292 // Calculate the range of values that are allowed by the comparison
1293 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1294 /*isFullSet=*/true);
1295 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1296 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001297 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1298 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1299 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001300
1301 // If we're interested in the false dest, invert the condition
1302 CmpInst::Predicate Pred =
1303 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1304 ConstantRange TrueValues =
1305 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1306
1307 if (Offset) // Apply the offset from above.
1308 TrueValues = TrueValues.subtract(Offset->getValue());
1309
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001310 return LVILatticeVal::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001311 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001312
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001313 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001314}
1315
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001316static LVILatticeVal
1317getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1318 DenseMap<Value*, LVILatticeVal> &Visited);
1319
1320static LVILatticeVal
1321getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
1322 DenseMap<Value*, LVILatticeVal> &Visited) {
1323 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1324 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1325
1326 // Handle conditions in the form of (cond1 && cond2), we know that on the
Craig Topperb60f8662017-06-23 01:08:16 +00001327 // true dest path both of the conditions hold. Similarly for conditions of
1328 // the form (cond1 || cond2), we know that on the false dest path neither
1329 // condition holds.
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001330 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
Craig Topperb60f8662017-06-23 01:08:16 +00001331 if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
1332 (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001333 return LVILatticeVal::getOverdefined();
1334
1335 auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
1336 auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
1337 return intersect(RHS, LHS);
1338}
1339
1340static LVILatticeVal
1341getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1342 DenseMap<Value*, LVILatticeVal> &Visited) {
1343 auto I = Visited.find(Cond);
1344 if (I != Visited.end())
1345 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001346
1347 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1348 Visited[Cond] = Result;
1349 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001350}
1351
1352LVILatticeVal getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest) {
1353 assert(Cond && "precondition");
1354 DenseMap<Value*, LVILatticeVal> Visited;
1355 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1356}
1357
Nuno Lopese6e04902012-06-28 01:16:18 +00001358/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001359/// Val is not constrained on the edge. Result is unspecified if return value
1360/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001361static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1362 BasicBlock *BBTo, LVILatticeVal &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001363 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001364 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001365 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1366 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001367 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001368 if (BI->isConditional() &&
1369 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1370 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1371 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1372 "BBTo isn't a successor of BBFrom");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001373
Chris Lattner19019ea2009-11-11 22:48:44 +00001374 // If V is the condition of the branch itself, then we know exactly what
1375 // it is.
Daniel Jasper43cd2ef2017-08-01 05:30:49 +00001376 if (BI->getCondition() == Val) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001377 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001378 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001379 return true;
1380 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001381
Chris Lattner19019ea2009-11-11 22:48:44 +00001382 // If the condition of the branch is an equality comparison, we may be
1383 // able to infer the value.
Daniel Jasper43cd2ef2017-08-01 05:30:49 +00001384 Result = getValueFromCondition(Val, BI->getCondition(), isTrueDest);
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001385 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001386 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001387 }
1388 }
Chris Lattner77358782009-11-15 20:02:12 +00001389
1390 // If the edge was formed by a switch on the value, then we may know exactly
1391 // what it is.
1392 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Daniel Jasper43cd2ef2017-08-01 05:30:49 +00001393 if (SI->getCondition() != Val)
Nuno Lopes8650fb82012-06-28 16:13:37 +00001394 return false;
1395
1396 bool DefaultCase = SI->getDefaultDest() == BBTo;
1397 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1398 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1399
Chandler Carruth927d8e62017-04-12 07:27:28 +00001400 for (auto Case : SI->cases()) {
Daniel Jasper43cd2ef2017-08-01 05:30:49 +00001401 ConstantRange EdgeVal(Case.getCaseValue()->getValue());
Manman Renf3fedb62012-09-05 23:45:58 +00001402 if (DefaultCase) {
1403 // It is possible that the default destination is the destination of
1404 // some cases. There is no need to perform difference for those cases.
Chandler Carruth927d8e62017-04-12 07:27:28 +00001405 if (Case.getCaseSuccessor() != BBTo)
Manman Renf3fedb62012-09-05 23:45:58 +00001406 EdgesVals = EdgesVals.difference(EdgeVal);
Chandler Carruth927d8e62017-04-12 07:27:28 +00001407 } else if (Case.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001408 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001409 }
Benjamin Kramer2337c1f2016-02-20 10:40:34 +00001410 Result = LVILatticeVal::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001411 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001412 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001413 return false;
1414}
1415
Sanjay Patel938e2792015-01-09 16:35:37 +00001416/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1417/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001418bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Xin Tong68ea9aa2017-02-24 20:59:26 +00001419 BasicBlock *BBTo, LVILatticeVal &Result,
1420 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001421 // If already a constant, there is nothing to compute.
1422 if (Constant *VC = dyn_cast<Constant>(Val)) {
1423 Result = LVILatticeVal::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001424 return true;
1425 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001426
Philip Reames44456b82016-02-02 03:15:40 +00001427 LVILatticeVal LocalResult;
1428 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1429 // If we couldn't constrain the value on the edge, LocalResult doesn't
1430 // provide any information.
Philip Reames1baaef12016-12-06 03:01:08 +00001431 LocalResult = LVILatticeVal::getOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001432
Philip Reames44456b82016-02-02 03:15:40 +00001433 if (hasSingleValue(LocalResult)) {
1434 // Can't get any more precise here
1435 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001436 return true;
1437 }
1438
1439 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001440 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1441 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001442 // No new information.
1443 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001444 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001445 }
1446
Philip Reames44456b82016-02-02 03:15:40 +00001447 // Try to intersect ranges of the BB and the constraint on the edge.
1448 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001449 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1450 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001451 // We can use the context instruction (generically the ultimate instruction
1452 // the calling pass is trying to simplify) here, even though the result of
1453 // this function is generally cached when called from the solve* functions
1454 // (and that cached result might be used with queries using a different
1455 // context instruction), because when this function is called from the solve*
1456 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001457 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001458 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001459 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001460
1461 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001462 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001463}
1464
Philip Reames92e5e1b2016-09-12 21:46:58 +00001465LVILatticeVal LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
Hal Finkel7e184492014-09-07 20:29:59 +00001466 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001467 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001468 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001469
Hans Wennborg45172ac2014-11-25 17:23:05 +00001470 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001471 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001472 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001473 solve();
1474 }
Owen Andersonc7ed4dc2010-12-09 06:14:58 +00001475 LVILatticeVal Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001476 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001477
1478 DEBUG(dbgs() << " Result = " << Result << "\n");
1479 return Result;
1480}
1481
Philip Reames92e5e1b2016-09-12 21:46:58 +00001482LVILatticeVal LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001483 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1484 << CxtI->getName() << "'\n");
1485
Philip Reamesbb781b42016-02-10 21:46:32 +00001486 if (auto *C = dyn_cast<Constant>(V))
1487 return LVILatticeVal::get(C);
1488
Philip Reamesd1f829d2016-02-02 21:57:37 +00001489 LVILatticeVal Result = LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001490 if (auto *I = dyn_cast<Instruction>(V))
1491 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001492 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001493
David Greene37e98092009-12-23 20:43:58 +00001494 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001495 return Result;
1496}
Chris Lattner19019ea2009-11-11 22:48:44 +00001497
Philip Reames92e5e1b2016-09-12 21:46:58 +00001498LVILatticeVal LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001499getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1500 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001501 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001502 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001503
Nick Lewycky55a700b2010-12-18 01:00:40 +00001504 LVILatticeVal Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001505 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001506 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001507 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001508 (void)WasFastQuery;
1509 assert(WasFastQuery && "More work to do after problem solved?");
1510 }
1511
David Greene37e98092009-12-23 20:43:58 +00001512 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001513 return Result;
1514}
1515
Philip Reames92e5e1b2016-09-12 21:46:58 +00001516void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames9db79482016-09-12 22:38:44 +00001517 BasicBlock *NewSucc) {
1518 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001519}
1520
Chris Lattneraf025d32009-11-15 19:59:49 +00001521//===----------------------------------------------------------------------===//
1522// LazyValueInfo Impl
1523//===----------------------------------------------------------------------===//
1524
Philip Reames92e5e1b2016-09-12 21:46:58 +00001525/// This lazily constructs the LazyValueInfoImpl.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001526static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1527 const DataLayout *DL,
Philip Reames92e5e1b2016-09-12 21:46:58 +00001528 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001529 if (!PImpl) {
1530 assert(DL && "getCache() called with a null DataLayout");
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001531 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001532 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001533 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001534}
1535
Sean Silva687019f2016-06-13 22:01:25 +00001536bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001537 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001538 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001539
1540 DominatorTreeWrapperPass *DTWP =
1541 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001542 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1543 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001544
Sean Silva687019f2016-06-13 22:01:25 +00001545 if (Info.PImpl)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001546 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001547
Owen Anderson208636f2010-08-18 18:39:01 +00001548 // Fully lazy.
1549 return false;
1550}
1551
Sean Silva687019f2016-06-13 22:01:25 +00001552void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001553 AU.setPreservesAll();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001554 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001555 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001556}
1557
Sean Silva687019f2016-06-13 22:01:25 +00001558LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1559
1560LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1561
Chris Lattneraf025d32009-11-15 19:59:49 +00001562void LazyValueInfo::releaseMemory() {
1563 // If the cache was allocated, free it.
1564 if (PImpl) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001565 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001566 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001567 }
1568}
1569
Chandler Carrutha504f2b2017-01-23 06:35:12 +00001570bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1571 FunctionAnalysisManager::Invalidator &Inv) {
1572 // We need to invalidate if we have either failed to preserve this analyses
1573 // result directly or if any of its dependencies have been invalidated.
1574 auto PAC = PA.getChecker<LazyValueAnalysis>();
1575 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1576 (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1577 return true;
1578
1579 return false;
1580}
1581
Sean Silva687019f2016-06-13 22:01:25 +00001582void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1583
1584LazyValueInfo LazyValueAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001585 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
Sean Silva687019f2016-06-13 22:01:25 +00001586 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1587 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1588
Anna Thomasa10e3e42017-03-12 14:06:41 +00001589 return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
Sean Silva687019f2016-06-13 22:01:25 +00001590}
1591
Wei Mif160e342016-09-15 06:28:34 +00001592/// Returns true if we can statically tell that this value will never be a
1593/// "useful" constant. In practice, this means we've got something like an
1594/// alloca or a malloc call for which a comparison against a constant can
1595/// only be guarding dead code. Note that we are potentially giving up some
1596/// precision in dead code (a constant result) in favour of avoiding a
1597/// expensive search for a easily answered common query.
1598static bool isKnownNonConstant(Value *V) {
1599 V = V->stripPointerCasts();
1600 // The return val of alloc cannot be a Constant.
1601 if (isa<AllocaInst>(V))
1602 return true;
1603 return false;
1604}
1605
Hal Finkel7e184492014-09-07 20:29:59 +00001606Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1607 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001608 // Bail out early if V is known not to be a Constant.
1609 if (isKnownNonConstant(V))
1610 return nullptr;
1611
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001612 const DataLayout &DL = BB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001613 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001614 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001615
Chris Lattner19019ea2009-11-11 22:48:44 +00001616 if (Result.isConstant())
1617 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001618 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001619 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001620 if (const APInt *SingleVal = CR.getSingleElement())
1621 return ConstantInt::get(V->getContext(), *SingleVal);
1622 }
Craig Topper9f008862014-04-15 04:59:12 +00001623 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001624}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001625
John Regehre1c481d2016-05-02 19:58:00 +00001626ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001627 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001628 assert(V->getType()->isIntegerTy());
1629 unsigned Width = V->getType()->getIntegerBitWidth();
1630 const DataLayout &DL = BB->getModule()->getDataLayout();
1631 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001632 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001633 if (Result.isUndefined())
1634 return ConstantRange(Width, /*isFullSet=*/false);
1635 if (Result.isConstantRange())
1636 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001637 // We represent ConstantInt constants as constant ranges but other kinds
1638 // of integer constants, i.e. ConstantExpr will be tagged as constants
1639 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1640 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001641 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001642}
1643
Sanjay Patel2a385e22015-01-09 16:47:20 +00001644/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001645/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001646Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001647 BasicBlock *ToBB,
1648 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001649 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001650 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001651 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001652
Chris Lattnerd5e25432009-11-12 01:29:10 +00001653 if (Result.isConstant())
1654 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001655 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001656 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001657 if (const APInt *SingleVal = CR.getSingleElement())
1658 return ConstantInt::get(V->getContext(), *SingleVal);
1659 }
Craig Topper9f008862014-04-15 04:59:12 +00001660 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001661}
1662
Craig Topper2c20c422017-06-23 05:41:35 +00001663ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
1664 BasicBlock *FromBB,
1665 BasicBlock *ToBB,
1666 Instruction *CxtI) {
1667 unsigned Width = V->getType()->getIntegerBitWidth();
1668 const DataLayout &DL = FromBB->getModule()->getDataLayout();
1669 LVILatticeVal Result =
1670 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1671
1672 if (Result.isUndefined())
1673 return ConstantRange(Width, /*isFullSet=*/false);
1674 if (Result.isConstantRange())
1675 return Result.getConstantRange();
1676 // We represent ConstantInt constants as constant ranges but other kinds
1677 // of integer constants, i.e. ConstantExpr will be tagged as constants
1678 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1679 "ConstantInt value must be represented as constantrange");
1680 return ConstantRange(Width, /*isFullSet=*/true);
1681}
1682
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001683static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001684 const LVILatticeVal &Val,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001685 const DataLayout &DL,
1686 TargetLibraryInfo *TLI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001687
Chris Lattner565ee2f2009-11-12 04:36:58 +00001688 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001689 Constant *Res = nullptr;
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001690 if (Val.isConstant()) {
1691 Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001692 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001693 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1694 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001695 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001696
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001697 if (Val.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001698 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001699 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001700
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001701 const ConstantRange &CR = Val.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001702 if (Pred == ICmpInst::ICMP_EQ) {
1703 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001704 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001705
Craig Topper79452482017-06-07 00:58:09 +00001706 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001707 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001708 } else if (Pred == ICmpInst::ICMP_NE) {
1709 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001710 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001711
Craig Topper79452482017-06-07 00:58:09 +00001712 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001713 return LazyValueInfo::False;
Craig Topper31ce4ec2017-06-09 16:16:20 +00001714 } else {
1715 // Handle more complex predicates.
1716 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1717 (ICmpInst::Predicate)Pred, CI->getValue());
1718 if (TrueValues.contains(CR))
1719 return LazyValueInfo::True;
1720 if (TrueValues.inverse().contains(CR))
1721 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001722 }
Hal Finkel7e184492014-09-07 20:29:59 +00001723 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001724 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001725
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001726 if (Val.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001727 // If this is an equality comparison, we can try to fold it knowing that
1728 // "V != C1".
1729 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001730 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001731 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001732 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001733 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001734 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001735 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001736 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001737 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001738 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001739 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001740 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001741 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001742 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001743 }
Hal Finkel7e184492014-09-07 20:29:59 +00001744 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001745 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001746
Hal Finkel7e184492014-09-07 20:29:59 +00001747 return LazyValueInfo::Unknown;
1748}
1749
Sanjay Patel2a385e22015-01-09 16:47:20 +00001750/// Determine whether the specified value comparison with a constant is known to
1751/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001752LazyValueInfo::Tristate
1753LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1754 BasicBlock *FromBB, BasicBlock *ToBB,
1755 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001756 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001757 LVILatticeVal Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001758 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001759
1760 return getPredicateResult(Pred, C, Result, DL, TLI);
1761}
1762
1763LazyValueInfo::Tristate
1764LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1765 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001766 // Is or is not NonNull are common predicates being queried. If
1767 // isKnownNonNull can tell us the result of the predicate, we can
1768 // return it quickly. But this is only a fastpath, and falling
1769 // through would still be correct.
1770 if (V->getType()->isPointerTy() && C->isNullValue() &&
1771 isKnownNonNull(V->stripPointerCasts())) {
1772 if (Pred == ICmpInst::ICMP_EQ)
1773 return LazyValueInfo::False;
1774 else if (Pred == ICmpInst::ICMP_NE)
1775 return LazyValueInfo::True;
1776 }
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001777 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001778 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001779 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1780 if (Ret != Unknown)
1781 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001782
Philip Reamesaeefae02015-11-04 01:47:04 +00001783 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1784 // LVI as a whole tries to compute a lattice value which is conservatively
1785 // correct at a given location. In this case, we have a predicate which we
1786 // weren't able to prove about the merged result, and we're pushing that
1787 // predicate back along each incoming edge to see if we can prove it
1788 // separately for each input. As a motivating example, consider:
1789 // bb1:
1790 // %v1 = ... ; constantrange<1, 5>
1791 // br label %merge
1792 // bb2:
1793 // %v2 = ... ; constantrange<10, 20>
1794 // br label %merge
1795 // merge:
1796 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1797 // %pred = icmp eq i32 %phi, 8
1798 // We can't tell from the lattice value for '%phi' that '%pred' is false
1799 // along each path, but by checking the predicate over each input separately,
1800 // we can.
1801 // We limit the search to one step backwards from the current BB and value.
1802 // We could consider extending this to search further backwards through the
1803 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001804 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001805 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001806 BasicBlock *BB = CxtI->getParent();
1807
1808 // Function entry or an unreachable block. Bail to avoid confusing
1809 // analysis below.
1810 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1811 if (PI == PE)
1812 return Unknown;
1813
1814 // If V is a PHI node in the same block as the context, we need to ask
1815 // questions about the predicate as applied to the incoming value along
1816 // each edge. This is useful for eliminating cases where the predicate is
1817 // known along all incoming edges.
1818 if (auto *PHI = dyn_cast<PHINode>(V))
1819 if (PHI->getParent() == BB) {
1820 Tristate Baseline = Unknown;
1821 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1822 Value *Incoming = PHI->getIncomingValue(i);
1823 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001824 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001825 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1826 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001827
Philip Reamesbb11d622015-08-31 18:31:48 +00001828 // Keep going as long as we've seen a consistent known result for
1829 // all inputs.
1830 Baseline = (i == 0) ? Result /* First iteration */
1831 : (Baseline == Result ? Baseline : Unknown); /* All others */
1832 if (Baseline == Unknown)
1833 break;
1834 }
1835 if (Baseline != Unknown)
1836 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001837 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001838
Philip Reames66ab0f02015-06-16 00:49:59 +00001839 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001840 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001841 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001842 if (!isa<Instruction>(V) ||
1843 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001844 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001845 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001846 // the value of the comparison in this block.
1847 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1848 if (Baseline != Unknown) {
1849 // Check that all remaining incoming values match the first one.
1850 while (++PI != PE) {
1851 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1852 if (Ret != Baseline) break;
1853 }
1854 // If we terminated early, then one of the values didn't match.
1855 if (PI == PE) {
1856 return Baseline;
1857 }
1858 }
1859 }
1860 }
1861 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001862}
1863
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001864void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001865 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001866 if (PImpl) {
1867 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001868 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001869 }
Owen Anderson208636f2010-08-18 18:39:01 +00001870}
1871
1872void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001873 if (PImpl) {
1874 const DataLayout &DL = BB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001875 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001876 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001877}
Anna Thomase27b39a2017-03-22 19:27:12 +00001878
1879
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001880void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
Anna Thomase27b39a2017-03-22 19:27:12 +00001881 if (PImpl) {
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001882 getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
Anna Thomase27b39a2017-03-22 19:27:12 +00001883 }
1884}
1885
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001886// Print the LVI for the function arguments at the start of each basic block.
1887void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
1888 const BasicBlock *BB, formatted_raw_ostream &OS) {
1889 // Find if there are latticevalues defined for arguments of the function.
1890 auto *F = BB->getParent();
1891 for (auto &Arg : F->args()) {
1892 LVILatticeVal Result = LVIImpl->getValueInBlock(
1893 const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
1894 if (Result.isUndefined())
1895 continue;
1896 OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
1897 }
1898}
1899
1900// This function prints the LVI analysis for the instruction I at the beginning
1901// of various basic blocks. It relies on calculated values that are stored in
1902// the LazyValueInfoCache, and in the absence of cached values, recalculte the
1903// LazyValueInfo for `I`, and print that info.
1904void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
1905 const Instruction *I, formatted_raw_ostream &OS) {
1906
1907 auto *ParentBB = I->getParent();
1908 SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
1909 // We can generate (solve) LVI values only for blocks that are dominated by
1910 // the I's parent. However, to avoid generating LVI for all dominating blocks,
1911 // that contain redundant/uninteresting information, we print LVI for
1912 // blocks that may use this LVI information (such as immediate successor
1913 // blocks, and blocks that contain uses of `I`).
1914 auto printResult = [&](const BasicBlock *BB) {
1915 if (!BlocksContainingLVI.insert(BB).second)
1916 return;
1917 LVILatticeVal Result = LVIImpl->getValueInBlock(
1918 const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
1919 OS << "; LatticeVal for: '" << *I << "' in BB: '";
1920 BB->printAsOperand(OS, false);
1921 OS << "' is: " << Result << "\n";
1922 };
1923
1924 printResult(ParentBB);
1925 // Print the LVI analysis results for the the immediate successor blocks, that
1926 // are dominated by `ParentBB`.
1927 for (auto *BBSucc : successors(ParentBB))
1928 if (DT.dominates(ParentBB, BBSucc))
1929 printResult(BBSucc);
1930
1931 // Print LVI in blocks where `I` is used.
1932 for (auto *U : I->users())
1933 if (auto *UseI = dyn_cast<Instruction>(U))
1934 if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
1935 printResult(UseI->getParent());
1936
1937}
1938
Anna Thomase27b39a2017-03-22 19:27:12 +00001939namespace {
1940// Printer class for LazyValueInfo results.
1941class LazyValueInfoPrinter : public FunctionPass {
1942public:
1943 static char ID; // Pass identification, replacement for typeid
1944 LazyValueInfoPrinter() : FunctionPass(ID) {
1945 initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
1946 }
1947
1948 void getAnalysisUsage(AnalysisUsage &AU) const override {
1949 AU.setPreservesAll();
1950 AU.addRequired<LazyValueInfoWrapperPass>();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001951 AU.addRequired<DominatorTreeWrapperPass>();
Anna Thomase27b39a2017-03-22 19:27:12 +00001952 }
1953
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001954 // Get the mandatory dominator tree analysis and pass this in to the
1955 // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
Anna Thomase27b39a2017-03-22 19:27:12 +00001956 bool runOnFunction(Function &F) override {
1957 dbgs() << "LVI for function '" << F.getName() << "':\n";
1958 auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001959 auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1960 LVI.printLVI(F, DTree, dbgs());
Anna Thomase27b39a2017-03-22 19:27:12 +00001961 return false;
1962 }
1963};
1964}
1965
1966char LazyValueInfoPrinter::ID = 0;
1967INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
1968 "Lazy Value Info Printer Pass", false, false)
1969INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
1970INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
1971 "Lazy Value Info Printer Pass", false, false)