blob: c37839a6fa42d7a055d8579f3cd55f70d38a7873 [file] [log] [blame]
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner741c94c2009-11-11 00:22:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000022#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000023#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
Hal Finkel7e184492014-09-07 20:29:59 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +000029#include "llvm/IR/Intrinsics.h"
Philip Reameseb3e9da2015-10-29 03:57:17 +000030#include "llvm/IR/LLVMContext.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000031#include "llvm/IR/PatternMatch.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000032#include "llvm/IR/ValueHandle.h"
Chris Lattnerb584d1e2009-11-12 01:22:16 +000033#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/raw_ostream.h"
Bill Wendling4ec081a2012-01-11 23:43:34 +000035#include <map>
Nick Lewycky55a700b2010-12-18 01:00:40 +000036#include <stack>
Chris Lattner741c94c2009-11-11 00:22:30 +000037using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000038using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000039
Chandler Carruthf1221bd2014-04-22 02:48:03 +000040#define DEBUG_TYPE "lazy-value-info"
41
Sean Silva687019f2016-06-13 22:01:25 +000042char LazyValueInfoWrapperPass::ID = 0;
43INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosier43a33062011-12-02 01:26:24 +000044 "Lazy Value Information Analysis", false, true)
Chandler Carruth66b31302015-01-04 12:03:27 +000045INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000046INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva687019f2016-06-13 22:01:25 +000047INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000048 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000049
50namespace llvm {
Sean Silva687019f2016-06-13 22:01:25 +000051 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner741c94c2009-11-11 00:22:30 +000052}
53
Sean Silva687019f2016-06-13 22:01:25 +000054char LazyValueAnalysis::PassID;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000055
56//===----------------------------------------------------------------------===//
57// LVILatticeVal
58//===----------------------------------------------------------------------===//
59
Sanjay Patel2a385e22015-01-09 16:47:20 +000060/// This is the information tracked by LazyValueInfo for each value.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000061///
62/// FIXME: This is basically just for bringup, this can be made a lot more rich
63/// in the future.
64///
65namespace {
66class LVILatticeVal {
67 enum LatticeValueTy {
Philip Reames3bb28322016-04-25 18:48:43 +000068 /// This Value has no known value yet. As a result, this implies the
69 /// producing instruction is dead. Caution: We use this as the starting
70 /// state in our local meet rules. In this usage, it's taken to mean
NAKAMURA Takumif2529512016-07-04 01:26:27 +000071 /// "nothing known yet".
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000072 undefined,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000073
Philip Reames3bb28322016-04-25 18:48:43 +000074 /// This Value has a specific constant value. (For integers, constantrange
75 /// is used instead.)
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000076 constant,
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000077
Philip Reames3bb28322016-04-25 18:48:43 +000078 /// This Value is known to not have the specified value. (For integers,
79 /// constantrange is used instead.)
Chris Lattner565ee2f2009-11-12 04:36:58 +000080 notconstant,
Chad Rosier43a33062011-12-02 01:26:24 +000081
Philip Reames3bb28322016-04-25 18:48:43 +000082 /// The Value falls within this range. (Used only for integer typed values.)
Owen Anderson0f306a42010-08-05 22:59:19 +000083 constantrange,
Chad Rosier43a33062011-12-02 01:26:24 +000084
Philip Reames3bb28322016-04-25 18:48:43 +000085 /// We can not precisely model the dynamic values this value might take.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000086 overdefined
87 };
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000088
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000089 /// Val: This stores the current lattice value along with the Constant* for
Chris Lattner565ee2f2009-11-12 04:36:58 +000090 /// the constant if this is a 'constant' or 'notconstant' value.
Owen Andersonc3a14132010-08-05 22:10:46 +000091 LatticeValueTy Tag;
92 Constant *Val;
Owen Anderson0f306a42010-08-05 22:59:19 +000093 ConstantRange Range;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +000094
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000095public:
Craig Topper9f008862014-04-15 04:59:12 +000096 LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000097
Chris Lattner19019ea2009-11-11 22:48:44 +000098 static LVILatticeVal get(Constant *C) {
99 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000100 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000101 Res.markConstant(C);
Chris Lattner19019ea2009-11-11 22:48:44 +0000102 return Res;
103 }
Chris Lattner565ee2f2009-11-12 04:36:58 +0000104 static LVILatticeVal getNot(Constant *C) {
105 LVILatticeVal Res;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000106 if (!isa<UndefValue>(C))
Owen Anderson185fe002010-08-10 20:03:09 +0000107 Res.markNotConstant(C);
Chris Lattner565ee2f2009-11-12 04:36:58 +0000108 return Res;
109 }
Owen Anderson5f1dd092010-08-10 23:20:01 +0000110 static LVILatticeVal getRange(ConstantRange CR) {
111 LVILatticeVal Res;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000112 Res.markConstantRange(std::move(CR));
Owen Anderson5f1dd092010-08-10 23:20:01 +0000113 return Res;
114 }
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000115 static LVILatticeVal getOverdefined() {
116 LVILatticeVal Res;
117 Res.markOverdefined();
118 return Res;
119 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000120
Owen Anderson0f306a42010-08-05 22:59:19 +0000121 bool isUndefined() const { return Tag == undefined; }
122 bool isConstant() const { return Tag == constant; }
123 bool isNotConstant() const { return Tag == notconstant; }
124 bool isConstantRange() const { return Tag == constantrange; }
125 bool isOverdefined() const { return Tag == overdefined; }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000126
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000127 Constant *getConstant() const {
128 assert(isConstant() && "Cannot get the constant of a non-constant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000129 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000130 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000131
Chris Lattner565ee2f2009-11-12 04:36:58 +0000132 Constant *getNotConstant() const {
133 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
Owen Andersonc3a14132010-08-05 22:10:46 +0000134 return Val;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000135 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000136
Owen Anderson0f306a42010-08-05 22:59:19 +0000137 ConstantRange getConstantRange() const {
138 assert(isConstantRange() &&
139 "Cannot get the constant-range of a non-constant-range!");
140 return Range;
141 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000142
Sanjay Patel2a385e22015-01-09 16:47:20 +0000143 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000144 bool markOverdefined() {
145 if (isOverdefined())
146 return false;
Owen Andersonc3a14132010-08-05 22:10:46 +0000147 Tag = overdefined;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000148 return true;
149 }
150
Sanjay Patel2a385e22015-01-09 16:47:20 +0000151 /// Return true if this is a change in status.
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000152 bool markConstant(Constant *V) {
Nick Lewycky11678bd2010-12-15 18:57:18 +0000153 assert(V && "Marking constant with NULL");
154 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
155 return markConstantRange(ConstantRange(CI->getValue()));
156 if (isa<UndefValue>(V))
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000157 return false;
Nick Lewycky11678bd2010-12-15 18:57:18 +0000158
159 assert((!isConstant() || getConstant() == V) &&
160 "Marking constant with different value");
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000161 assert(isUndefined());
Owen Andersonc3a14132010-08-05 22:10:46 +0000162 Tag = constant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000163 Val = V;
Chris Lattner19019ea2009-11-11 22:48:44 +0000164 return true;
165 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000166
Sanjay Patel2a385e22015-01-09 16:47:20 +0000167 /// Return true if this is a change in status.
Chris Lattner565ee2f2009-11-12 04:36:58 +0000168 bool markNotConstant(Constant *V) {
Chris Lattner565ee2f2009-11-12 04:36:58 +0000169 assert(V && "Marking constant with NULL");
Nick Lewycky11678bd2010-12-15 18:57:18 +0000170 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
171 return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
172 if (isa<UndefValue>(V))
173 return false;
174
175 assert((!isConstant() || getConstant() != V) &&
176 "Marking constant !constant with same value");
177 assert((!isNotConstant() || getNotConstant() == V) &&
178 "Marking !constant with different value");
179 assert(isUndefined() || isConstant());
180 Tag = notconstant;
Owen Andersonc3a14132010-08-05 22:10:46 +0000181 Val = V;
Chris Lattner565ee2f2009-11-12 04:36:58 +0000182 return true;
183 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000184
Sanjay Patel2a385e22015-01-09 16:47:20 +0000185 /// Return true if this is a change in status.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000186 bool markConstantRange(ConstantRange NewR) {
Owen Anderson0f306a42010-08-05 22:59:19 +0000187 if (isConstantRange()) {
188 if (NewR.isEmptySet())
189 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000190
Nuno Lopese6e04902012-06-28 01:16:18 +0000191 bool changed = Range != NewR;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000192 Range = std::move(NewR);
Owen Anderson0f306a42010-08-05 22:59:19 +0000193 return changed;
194 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000195
Owen Anderson0f306a42010-08-05 22:59:19 +0000196 assert(isUndefined());
197 if (NewR.isEmptySet())
198 return markOverdefined();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000199
Owen Anderson0f306a42010-08-05 22:59:19 +0000200 Tag = constantrange;
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000201 Range = std::move(NewR);
Owen Anderson0f306a42010-08-05 22:59:19 +0000202 return true;
203 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000204
Sanjay Patel2a385e22015-01-09 16:47:20 +0000205 /// Merge the specified lattice value into this one, updating this
Chris Lattner19019ea2009-11-11 22:48:44 +0000206 /// one and returning true if anything changed.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000207 bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
Chris Lattner19019ea2009-11-11 22:48:44 +0000208 if (RHS.isUndefined() || isOverdefined()) return false;
209 if (RHS.isOverdefined()) return markOverdefined();
210
Nick Lewycky11678bd2010-12-15 18:57:18 +0000211 if (isUndefined()) {
212 Tag = RHS.Tag;
213 Val = RHS.Val;
214 Range = RHS.Range;
215 return true;
Chris Lattner22db4b52009-11-12 04:57:13 +0000216 }
217
Nick Lewycky11678bd2010-12-15 18:57:18 +0000218 if (isConstant()) {
219 if (RHS.isConstant()) {
220 if (Val == RHS.Val)
221 return false;
222 return markOverdefined();
223 }
224
225 if (RHS.isNotConstant()) {
226 if (Val == RHS.Val)
227 return markOverdefined();
228
229 // Unless we can prove that the two Constants are different, we must
230 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000231 if (ConstantInt *Res =
232 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
233 CmpInst::ICMP_NE, getConstant(), RHS.getNotConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000234 if (Res->isOne())
235 return markNotConstant(RHS.getNotConstant());
236
237 return markOverdefined();
238 }
239
Chris Lattner19019ea2009-11-11 22:48:44 +0000240 return markOverdefined();
Nick Lewycky11678bd2010-12-15 18:57:18 +0000241 }
242
243 if (isNotConstant()) {
244 if (RHS.isConstant()) {
245 if (Val == RHS.Val)
246 return markOverdefined();
247
248 // Unless we can prove that the two Constants are different, we must
249 // move to overdefined.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000250 if (ConstantInt *Res =
251 dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands(
252 CmpInst::ICMP_NE, getNotConstant(), RHS.getConstant(), DL)))
Nick Lewycky11678bd2010-12-15 18:57:18 +0000253 if (Res->isOne())
254 return false;
255
256 return markOverdefined();
257 }
258
259 if (RHS.isNotConstant()) {
260 if (Val == RHS.Val)
261 return false;
262 return markOverdefined();
263 }
264
265 return markOverdefined();
266 }
267
268 assert(isConstantRange() && "New LVILattice type?");
269 if (!RHS.isConstantRange())
270 return markOverdefined();
271
272 ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
273 if (NewR.isFullSet())
274 return markOverdefined();
275 return markConstantRange(NewR);
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000276 }
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000277};
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000278
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000279} // end anonymous namespace.
280
Chris Lattner19019ea2009-11-11 22:48:44 +0000281namespace llvm {
Chandler Carruth2b1ba482011-04-18 18:49:44 +0000282raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
283 LLVM_ATTRIBUTE_USED;
Chris Lattner19019ea2009-11-11 22:48:44 +0000284raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
285 if (Val.isUndefined())
286 return OS << "undefined";
287 if (Val.isOverdefined())
288 return OS << "overdefined";
Chris Lattner565ee2f2009-11-12 04:36:58 +0000289
290 if (Val.isNotConstant())
291 return OS << "notconstant<" << *Val.getNotConstant() << '>';
Davide Italianobd543d02016-05-25 22:29:34 +0000292 if (Val.isConstantRange())
Owen Anderson8afac042010-08-09 20:50:46 +0000293 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
294 << Val.getConstantRange().getUpper() << '>';
Chris Lattner19019ea2009-11-11 22:48:44 +0000295 return OS << "constant<" << *Val.getConstant() << '>';
296}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000297}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000298
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000299/// Returns true if this lattice value represents at most one possible value.
300/// This is as precise as any lattice value can get while still representing
301/// reachable code.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000302static bool hasSingleValue(const LVILatticeVal &Val) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000303 if (Val.isConstantRange() &&
304 Val.getConstantRange().isSingleElement())
305 // Integer constants are single element ranges
306 return true;
307 if (Val.isConstant())
308 // Non integer constants
309 return true;
310 return false;
311}
312
313/// Combine two sets of facts about the same value into a single set of
314/// facts. Note that this method is not suitable for merging facts along
315/// different paths in a CFG; that's what the mergeIn function is for. This
316/// is for merging facts gathered about the same value at the same location
317/// through two independent means.
318/// Notes:
319/// * This method does not promise to return the most precise possible lattice
320/// value implied by A and B. It is allowed to return any lattice element
321/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000322/// conflict, see below).
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000323/// * Due to unreachable code, the intersection of two lattice values could be
324/// contradictory. If this happens, we return some valid lattice value so as
325/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
326/// we do not make this guarantee. TODO: This would be a useful enhancement.
327static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) {
328 // Undefined is the strongest state. It means the value is known to be along
329 // an unreachable path.
330 if (A.isUndefined())
331 return A;
332 if (B.isUndefined())
333 return B;
334
335 // If we gave up for one, but got a useable fact from the other, use it.
336 if (A.isOverdefined())
337 return B;
338 if (B.isOverdefined())
339 return A;
340
341 // Can't get any more precise than constants.
342 if (hasSingleValue(A))
343 return A;
344 if (hasSingleValue(B))
345 return B;
346
347 // Could be either constant range or not constant here.
348 if (!A.isConstantRange() || !B.isConstantRange()) {
349 // TODO: Arbitrary choice, could be improved
350 return A;
351 }
352
353 // Intersect two constant ranges
354 ConstantRange Range =
355 A.getConstantRange().intersectWith(B.getConstantRange());
356 // Note: An empty range is implicitly converted to overdefined internally.
357 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000358 // and thus know this path must be unreachable.
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000359 return LVILatticeVal::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000360}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000361
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000362//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000363// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000364//===----------------------------------------------------------------------===//
365
Chris Lattneraf025d32009-11-15 19:59:49 +0000366namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000367 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000368 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000369 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000370 // Needs to access getValPtr(), which is protected.
371 friend struct DenseMapInfo<LVIValueHandle>;
372
Owen Anderson118ac802011-01-05 21:15:29 +0000373 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000374
Owen Anderson118ac802011-01-05 21:15:29 +0000375 LVIValueHandle(Value *V, LazyValueInfoCache *P)
376 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000377
378 void deleted() override;
379 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000380 deleted();
381 }
382 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000383} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000384
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000385namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000386 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000387 /// maintains information about queries across the clients' queries.
388 class LazyValueInfoCache {
Philip Reames92e5e1b2016-09-12 21:46:58 +0000389 protected:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000390 /// This is all of the cached block information for exactly one Value*.
391 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000392 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000393 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
394 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000395 struct ValueCacheEntryTy {
396 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
397 LVIValueHandle Handle;
398 SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4> BlockVals;
399 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000400
Sanjay Patel2a385e22015-01-09 16:47:20 +0000401 /// This is all of the cached information for all values,
Owen Anderson6f060af2011-01-05 23:26:22 +0000402 /// mapped from Value* to key information.
Justin Lebar58b377e2016-07-27 22:33:36 +0000403 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000404
Sanjay Patel2a385e22015-01-09 16:47:20 +0000405 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000406 /// over-defined at the end of that block.
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000407 typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>>
408 OverDefinedCacheTy;
409 OverDefinedCacheTy OverDefinedCache;
Benjamin Kramer36647082011-12-03 15:16:45 +0000410
Sanjay Patel2a385e22015-01-09 16:47:20 +0000411 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000412 /// don't spend time removing unused blocks from our caches.
413 DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
414
Hans Wennborg45172ac2014-11-25 17:23:05 +0000415 void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
416 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000417
418 // Insert over-defined values into their own cache to reduce memory
419 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000420 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000421 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-07-27 22:33:36 +0000422 else {
423 auto It = ValueCache.find_as(Val);
424 if (It == ValueCache.end()) {
425 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
426 It = ValueCache.find_as(Val);
427 assert(It != ValueCache.end() && "Val was just added to the map!");
428 }
429 It->second->BlockVals[BB] = Result;
430 }
Hans Wennborg45172ac2014-11-25 17:23:05 +0000431 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000432
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000433 bool isOverdefined(Value *V, BasicBlock *BB) const {
434 auto ODI = OverDefinedCache.find(BB);
435
436 if (ODI == OverDefinedCache.end())
437 return false;
438
439 return ODI->second.count(V);
440 }
441
442 bool hasCachedValueInfo(Value *V, BasicBlock *BB) {
443 if (isOverdefined(V, BB))
444 return true;
445
Justin Lebar58b377e2016-07-27 22:33:36 +0000446 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000447 if (I == ValueCache.end())
448 return false;
449
Justin Lebar58b377e2016-07-27 22:33:36 +0000450 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000451 }
452
453 LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) {
454 if (isOverdefined(V, BB))
455 return LVILatticeVal::getOverdefined();
456
Justin Lebar58b377e2016-07-27 22:33:36 +0000457 auto I = ValueCache.find_as(V);
458 if (I == ValueCache.end())
459 return LVILatticeVal();
460 auto BBI = I->second->BlockVals.find(BB);
461 if (BBI == I->second->BlockVals.end())
462 return LVILatticeVal();
463 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000464 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000465
Chris Lattneraf025d32009-11-15 19:59:49 +0000466 public:
Philip Reames92e5e1b2016-09-12 21:46:58 +0000467 /// clear - Empty the cache.
468 void clear() {
469 SeenBlocks.clear();
470 ValueCache.clear();
471 OverDefinedCache.clear();
472 }
473
474 friend struct LVIValueHandle;
475 };
476
477 // The actual implementation of the lazy analysis and update. Note that the
478 // inheritance from LazyValueInfoCache is intended to be temporary while
479 // splitting the code and then transitioning to a has-a relationship.
480 class LazyValueInfoImpl : public LazyValueInfoCache {
481
482 /// This stack holds the state of the value solver during a query.
483 /// It basically emulates the callstack of the naive
484 /// recursive value lookup process.
485 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
486
487 /// Keeps track of which block-value pairs are in BlockValueStack.
488 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
489
490 /// Push BV onto BlockValueStack unless it's already in there.
491 /// Returns true on success.
492 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
493 if (!BlockValueSet.insert(BV).second)
494 return false; // It's already in the stack.
495
496 DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
497 << "\n");
498 BlockValueStack.push(BV);
499 return true;
500 }
501
502 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
503 const DataLayout &DL; ///< A mandatory DataLayout
504 DominatorTree *DT; ///< An optional DT pointer.
505
506 LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
507 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
508 LVILatticeVal &Result, Instruction *CxtI = nullptr);
509 bool hasBlockValue(Value *Val, BasicBlock *BB);
510
511 // These methods process one work item and may add more. A false value
512 // returned means that the work item was not completely processed and must
513 // be revisited after going through the new items.
514 bool solveBlockValue(Value *Val, BasicBlock *BB);
515 bool solveBlockValueNonLocal(LVILatticeVal &BBLV, Value *Val, BasicBlock *BB);
516 bool solveBlockValuePHINode(LVILatticeVal &BBLV, PHINode *PN, BasicBlock *BB);
517 bool solveBlockValueSelect(LVILatticeVal &BBLV, SelectInst *S,
518 BasicBlock *BB);
519 bool solveBlockValueBinaryOp(LVILatticeVal &BBLV, Instruction *BBI,
520 BasicBlock *BB);
521 bool solveBlockValueCast(LVILatticeVal &BBLV, Instruction *BBI,
522 BasicBlock *BB);
523 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
524 LVILatticeVal &BBLV,
525 Instruction *BBI);
526
527 void solve();
528
529 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000530 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000531 /// value for the specified Value* at the end of the specified block.
Hal Finkel7e184492014-09-07 20:29:59 +0000532 LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
533 Instruction *CxtI = nullptr);
534
Sanjay Patel2a385e22015-01-09 16:47:20 +0000535 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000536 /// value for the specified Value* at the specified instruction (generally
537 /// from an assume intrinsic).
538 LVILatticeVal getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000539
Sanjay Patel2a385e22015-01-09 16:47:20 +0000540 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000541 /// value for the specified Value* that is true on the specified edge.
Hal Finkel7e184492014-09-07 20:29:59 +0000542 LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
543 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000544
Sanjay Patel2a385e22015-01-09 16:47:20 +0000545 /// This is the update interface to inform the cache that an edge from
546 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000547 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000548
Sanjay Patel2a385e22015-01-09 16:47:20 +0000549 /// This is part of the update interface to inform the cache
Owen Anderson208636f2010-08-18 18:39:01 +0000550 /// that a block has been deleted.
551 void eraseBlock(BasicBlock *BB);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000552
Philip Reames92e5e1b2016-09-12 21:46:58 +0000553 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
Chandler Carruth66b31302015-01-04 12:03:27 +0000554 DominatorTree *DT = nullptr)
555 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000556 };
557} // end anonymous namespace
558
Owen Anderson118ac802011-01-05 21:15:29 +0000559void LVIValueHandle::deleted() {
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000560 SmallVector<AssertingVH<BasicBlock>, 4> ToErase;
561 for (auto &I : Parent->OverDefinedCache) {
562 SmallPtrSetImpl<Value *> &ValueSet = I.second;
563 if (ValueSet.count(getValPtr()))
564 ValueSet.erase(getValPtr());
565 if (ValueSet.empty())
566 ToErase.push_back(I.first);
567 }
568 for (auto &BB : ToErase)
569 Parent->OverDefinedCache.erase(BB);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000570
Owen Anderson7b974a42010-08-11 22:36:04 +0000571 // This erasure deallocates *this, so it MUST happen after we're done
572 // using any and all members of *this.
573 Parent->ValueCache.erase(*this);
Owen Andersonc1561b82010-07-30 23:59:40 +0000574}
575
Philip Reames92e5e1b2016-09-12 21:46:58 +0000576void LazyValueInfoImpl::eraseBlock(BasicBlock *BB) {
Benjamin Kramer36647082011-12-03 15:16:45 +0000577 // Shortcut if we have never seen this block.
578 DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
579 if (I == SeenBlocks.end())
580 return;
581 SeenBlocks.erase(I);
582
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000583 auto ODI = OverDefinedCache.find(BB);
584 if (ODI != OverDefinedCache.end())
585 OverDefinedCache.erase(ODI);
Owen Anderson208636f2010-08-18 18:39:01 +0000586
Benjamin Krameraa209152016-06-26 17:27:42 +0000587 for (auto &I : ValueCache)
Justin Lebar58b377e2016-07-27 22:33:36 +0000588 I.second->BlockVals.erase(BB);
Owen Anderson208636f2010-08-18 18:39:01 +0000589}
Owen Andersonc1561b82010-07-30 23:59:40 +0000590
Philip Reames92e5e1b2016-09-12 21:46:58 +0000591void LazyValueInfoImpl::solve() {
Owen Anderson6f060af2011-01-05 23:26:22 +0000592 while (!BlockValueStack.empty()) {
593 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000594 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
595
Nuno Lopese6e04902012-06-28 01:16:18 +0000596 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000597 // The work item was completely processed.
598 assert(BlockValueStack.top() == e && "Nothing should have been pushed!");
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000599 assert(hasCachedValueInfo(e.second, e.first) &&
600 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000601
Philip Reames44456b82016-02-02 03:15:40 +0000602 DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000603 << " = " << getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000604
Owen Anderson6f060af2011-01-05 23:26:22 +0000605 BlockValueStack.pop();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000606 BlockValueSet.erase(e);
607 } else {
608 // More work needs to be done before revisiting.
609 assert(BlockValueStack.top() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000610 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000611 }
612}
613
Philip Reames92e5e1b2016-09-12 21:46:58 +0000614bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000615 // If already a constant, there is nothing to compute.
616 if (isa<Constant>(Val))
617 return true;
618
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000619 return hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000620}
621
Philip Reames92e5e1b2016-09-12 21:46:58 +0000622LVILatticeVal LazyValueInfoImpl::getBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000623 // If already a constant, there is nothing to compute.
624 if (Constant *VC = dyn_cast<Constant>(Val))
625 return LVILatticeVal::get(VC);
626
Benjamin Kramer36647082011-12-03 15:16:45 +0000627 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000628 return getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000629}
630
Philip Reameseb3e9da2015-10-29 03:57:17 +0000631static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
632 switch (BBI->getOpcode()) {
633 default: break;
634 case Instruction::Load:
635 case Instruction::Call:
636 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000637 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000638 if (isa<IntegerType>(BBI->getType())) {
Benjamin Kramer2337c1f2016-02-20 10:40:34 +0000639 return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000640 }
641 break;
642 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000643 // Nothing known - will be intersected with other facts
644 return LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000645}
646
Philip Reames92e5e1b2016-09-12 21:46:58 +0000647bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000648 if (isa<Constant>(Val))
649 return true;
650
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000651 if (hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000652 // If we have a cached value, use that.
653 DEBUG(dbgs() << " reuse BB '" << BB->getName()
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000654 << "' val=" << getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000655
Hans Wennborg45172ac2014-11-25 17:23:05 +0000656 // Since we're reusing a cached value, we don't need to update the
657 // OverDefinedCache. The cache will have been properly updated whenever the
658 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000659 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000660 }
661
Hans Wennborg45172ac2014-11-25 17:23:05 +0000662 // Hold off inserting this value into the Cache in case we have to return
663 // false and come back later.
664 LVILatticeVal Res;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000665
Chris Lattneraf025d32009-11-15 19:59:49 +0000666 Instruction *BBI = dyn_cast<Instruction>(Val);
Craig Topper9f008862014-04-15 04:59:12 +0000667 if (!BBI || BBI->getParent() != BB) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000668 if (!solveBlockValueNonLocal(Res, Val, BB))
669 return false;
670 insertResult(Val, BB, Res);
671 return true;
Chris Lattneraf025d32009-11-15 19:59:49 +0000672 }
Chris Lattner2c708562009-11-15 20:00:52 +0000673
Nick Lewycky55a700b2010-12-18 01:00:40 +0000674 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000675 if (!solveBlockValuePHINode(Res, PN, BB))
676 return false;
677 insertResult(Val, BB, Res);
678 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000679 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000680
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000681 if (auto *SI = dyn_cast<SelectInst>(BBI)) {
682 if (!solveBlockValueSelect(Res, SI, BB))
683 return false;
684 insertResult(Val, BB, Res);
685 return true;
686 }
687
Philip Reames2ab964e2016-04-27 01:02:25 +0000688 // If this value is a nonnull pointer, record it's range and bailout. Note
689 // that for all other pointer typed values, we terminate the search at the
690 // definition. We could easily extend this to look through geps, bitcasts,
691 // and the like to prove non-nullness, but it's not clear that's worth it
692 // compile time wise. The context-insensative value walk done inside
693 // isKnownNonNull gets most of the profitable cases at much less expense.
694 // This does mean that we have a sensativity to where the defining
695 // instruction is placed, even if it could legally be hoisted much higher.
696 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000697 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
698 if (PT && isKnownNonNull(BBI)) {
699 Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000700 insertResult(Val, BB, Res);
701 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000702 }
Davide Italianobd543d02016-05-25 22:29:34 +0000703 if (BBI->getType()->isIntegerTy()) {
Philip Reames2ab964e2016-04-27 01:02:25 +0000704 if (isa<CastInst>(BBI)) {
705 if (!solveBlockValueCast(Res, BBI, BB))
706 return false;
707 insertResult(Val, BB, Res);
708 return true;
709 }
710 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000711 if (BO && isa<ConstantInt>(BO->getOperand(1))) {
Philip Reames2ab964e2016-04-27 01:02:25 +0000712 if (!solveBlockValueBinaryOp(Res, BBI, BB))
713 return false;
714 insertResult(Val, BB, Res);
715 return true;
716 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000717 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000718
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000719 DEBUG(dbgs() << " compute BB '" << BB->getName()
720 << "' - unknown inst def found.\n");
721 Res = getFromRangeMetadata(BBI);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000722 insertResult(Val, BB, Res);
723 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000724}
725
726static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
727 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
728 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000729 GetUnderlyingObject(L->getPointerOperand(),
730 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000731 }
732 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
733 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000734 GetUnderlyingObject(S->getPointerOperand(),
735 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000736 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000737 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
738 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000739
740 // FIXME: check whether it has a valuerange that excludes zero?
741 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
742 if (!Len || Len->isZero()) return false;
743
Eli Friedman7a5fc692011-05-31 20:40:16 +0000744 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000745 if (GetUnderlyingObject(MI->getRawDest(),
746 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000747 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000748 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000749 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000750 if (GetUnderlyingObject(MTI->getRawSource(),
751 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000752 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000753 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000754 return false;
755}
756
Philip Reames3f83dbe2016-04-27 00:30:55 +0000757/// Return true if the allocation associated with Val is ever dereferenced
758/// within the given basic block. This establishes the fact Val is not null,
759/// but does not imply that the memory at Val is dereferenceable. (Val may
760/// point off the end of the dereferenceable part of the object.)
761static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
762 assert(Val->getType()->isPointerTy());
763
764 const DataLayout &DL = BB->getModule()->getDataLayout();
765 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
766 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
767 // inside InstructionDereferencesPointer either.
768 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
769 for (Instruction &I : *BB)
770 if (InstructionDereferencesPointer(&I, UnderlyingVal))
771 return true;
772 return false;
773}
774
Philip Reames92e5e1b2016-09-12 21:46:58 +0000775bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000776 Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000777 LVILatticeVal Result; // Start Undefined.
778
Nick Lewycky55a700b2010-12-18 01:00:40 +0000779 // If this is the entry block, we must be asking about an argument. The
780 // value is overdefined.
781 if (BB == &BB->getParent()->getEntryBlock()) {
782 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Philip Reames3f83dbe2016-04-27 00:30:55 +0000783 // Bofore giving up, see if we can prove the pointer non-null local to
784 // this particular block.
785 if (Val->getType()->isPointerTy() &&
786 (isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
Chris Lattner229907c2011-07-18 04:54:35 +0000787 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000788 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
789 } else {
790 Result.markOverdefined();
791 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000792 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000793 return true;
794 }
795
796 // Loop over all of our predecessors, merging what we know from them into
797 // result.
798 bool EdgesMissing = false;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000799 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000800 LVILatticeVal EdgeResult;
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000801 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000802 if (EdgesMissing)
803 continue;
804
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000805 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000806
807 // If we hit overdefined, exit early. The BlockVals entry is already set
808 // to overdefined.
809 if (Result.isOverdefined()) {
810 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000811 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000812 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000813 // this particular block.
814 if (Val->getType()->isPointerTy() &&
815 isObjectDereferencedInBlock(Val, BB)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000816 PointerType *PTy = cast<PointerType>(Val->getType());
Nick Lewycky55a700b2010-12-18 01:00:40 +0000817 Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
818 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000819
Owen Anderson64c2c572010-12-20 18:18:16 +0000820 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000821 return true;
822 }
823 }
824 if (EdgesMissing)
825 return false;
826
827 // Return the merged value, which is more precise than 'overdefined'.
828 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000829 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000830 return true;
831}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000832
Philip Reames92e5e1b2016-09-12 21:46:58 +0000833bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000834 PHINode *PN, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000835 LVILatticeVal Result; // Start Undefined.
836
837 // Loop over all of our predecessors, merging what we know from them into
838 // result.
839 bool EdgesMissing = false;
840 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
841 BasicBlock *PhiBB = PN->getIncomingBlock(i);
842 Value *PhiVal = PN->getIncomingValue(i);
843 LVILatticeVal EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000844 // Note that we can provide PN as the context value to getEdgeValue, even
845 // though the results will be cached, because PN is the value being used as
846 // the cache key in the caller.
Hal Finkel7e184492014-09-07 20:29:59 +0000847 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000848 if (EdgesMissing)
849 continue;
850
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000851 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000852
853 // If we hit overdefined, exit early. The BlockVals entry is already set
854 // to overdefined.
855 if (Result.isOverdefined()) {
856 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000857 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000858
Owen Anderson64c2c572010-12-20 18:18:16 +0000859 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000860 return true;
861 }
862 }
863 if (EdgesMissing)
864 return false;
865
866 // Return the merged value, which is more precise than 'overdefined'.
867 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000868 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000869 return true;
870}
871
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000872static LVILatticeVal getValueFromCondition(Value *Val, Value *Cond,
873 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000874
Philip Reamesd1f829d2016-02-02 21:57:37 +0000875// If we can determine a constraint on the value given conditions assumed by
876// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000877void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000878 Value *Val, LVILatticeVal &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000879 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
880 if (!BBI)
881 return;
882
Chandler Carruth66b31302015-01-04 12:03:27 +0000883 for (auto &AssumeVH : AC->assumptions()) {
884 if (!AssumeVH)
885 continue;
886 auto *I = cast<CallInst>(AssumeVH);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000887 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000888 continue;
889
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000890 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +0000891 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000892
893 // If guards are not used in the module, don't spend time looking for them
894 auto *GuardDecl = BBI->getModule()->getFunction(
895 Intrinsic::getName(Intrinsic::experimental_guard));
896 if (!GuardDecl || GuardDecl->use_empty())
897 return;
898
899 for (BasicBlock::iterator I = BBI->getIterator(),
900 E = BBI->getParent()->begin(); I != E; I--) {
901 Value *Cond = nullptr;
902 if (!match(&*I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
903 continue;
904 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
905 }
Hal Finkel7e184492014-09-07 20:29:59 +0000906}
907
Philip Reames92e5e1b2016-09-12 21:46:58 +0000908bool LazyValueInfoImpl::solveBlockValueSelect(LVILatticeVal &BBLV,
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000909 SelectInst *SI, BasicBlock *BB) {
910
911 // Recurse on our inputs if needed
912 if (!hasBlockValue(SI->getTrueValue(), BB)) {
913 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
914 return false;
915 BBLV.markOverdefined();
916 return true;
917 }
918 LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
919 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
920 // extra slots in the table if we can.
921 if (TrueVal.isOverdefined()) {
922 BBLV.markOverdefined();
923 return true;
924 }
925
926 if (!hasBlockValue(SI->getFalseValue(), BB)) {
927 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
928 return false;
929 BBLV.markOverdefined();
930 return true;
931 }
932 LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
933 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
934 // extra slots in the table if we can.
935 if (FalseVal.isOverdefined()) {
936 BBLV.markOverdefined();
937 return true;
938 }
939
Philip Reamesadf0e352016-02-26 22:53:59 +0000940 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
941 ConstantRange TrueCR = TrueVal.getConstantRange();
942 ConstantRange FalseCR = FalseVal.getConstantRange();
943 Value *LHS = nullptr;
944 Value *RHS = nullptr;
945 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
946 // Is this a min specifically of our two inputs? (Avoid the risk of
947 // ValueTracking getting smarter looking back past our immediate inputs.)
948 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
949 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
950 switch (SPR.Flavor) {
951 default:
952 llvm_unreachable("unexpected minmax type!");
953 case SPF_SMIN: /// Signed minimum
954 BBLV.markConstantRange(TrueCR.smin(FalseCR));
955 return true;
956 case SPF_UMIN: /// Unsigned minimum
957 BBLV.markConstantRange(TrueCR.umin(FalseCR));
958 return true;
959 case SPF_SMAX: /// Signed maximum
960 BBLV.markConstantRange(TrueCR.smax(FalseCR));
961 return true;
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000962 case SPF_UMAX: /// Unsigned maximum
Philip Reamesadf0e352016-02-26 22:53:59 +0000963 BBLV.markConstantRange(TrueCR.umax(FalseCR));
964 return true;
965 };
966 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000967
Philip Reamesadf0e352016-02-26 22:53:59 +0000968 // TODO: ABS, NABS from the SelectPatternResult
969 }
970
Philip Reames854a84c2016-02-12 00:09:18 +0000971 // Can we constrain the facts about the true and false values by using the
972 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
973 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000974 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000975 TrueVal = intersect(TrueVal,
976 getValueFromCondition(SI->getTrueValue(), Cond, true));
977 FalseVal = intersect(FalseVal,
978 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +0000979
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000980 // Handle clamp idioms such as:
981 // %24 = constantrange<0, 17>
982 // %39 = icmp eq i32 %24, 0
983 // %40 = add i32 %24, -1
984 // %siv.next = select i1 %39, i32 16, i32 %40
985 // %siv.next = constantrange<0, 17> not <-1, 17>
986 // In general, this can handle any clamp idiom which tests the edge
987 // condition via an equality or inequality.
988 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +0000989 ICmpInst::Predicate Pred = ICI->getPredicate();
990 Value *A = ICI->getOperand(0);
991 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
992 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
993 assert(A->getType() == B->getType());
994 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
995 };
996 // See if either input is A + C2, subject to the constraint from the
997 // condition that A != C when that input is used. We can assume that
998 // that input doesn't include C + C2.
999 ConstantInt *CIAdded;
1000 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +00001001 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +00001002 case ICmpInst::ICMP_EQ:
1003 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
1004 m_ConstantInt(CIAdded)))) {
1005 auto ResNot = addConstants(CIBase, CIAdded);
1006 FalseVal = intersect(FalseVal,
1007 LVILatticeVal::getNot(ResNot));
1008 }
1009 break;
1010 case ICmpInst::ICMP_NE:
1011 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
1012 m_ConstantInt(CIAdded)))) {
1013 auto ResNot = addConstants(CIBase, CIAdded);
1014 TrueVal = intersect(TrueVal,
1015 LVILatticeVal::getNot(ResNot));
1016 }
1017 break;
1018 };
1019 }
1020 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001021
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001022 LVILatticeVal Result; // Start Undefined.
1023 Result.mergeIn(TrueVal, DL);
1024 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +00001025 BBLV = Result;
1026 return true;
1027}
1028
Philip Reames92e5e1b2016-09-12 21:46:58 +00001029bool LazyValueInfoImpl::solveBlockValueCast(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001030 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001031 BasicBlock *BB) {
1032 if (!BBI->getOperand(0)->getType()->isSized()) {
1033 // Without knowing how wide the input is, we can't analyze it in any useful
1034 // way.
1035 BBLV.markOverdefined();
1036 return true;
1037 }
Philip Reamesf105db42016-04-26 23:27:33 +00001038
1039 // Filter out casts we don't know how to reason about before attempting to
1040 // recurse on our operand. This can cut a long search short if we know we're
1041 // not going to be able to get any useful information anways.
1042 switch (BBI->getOpcode()) {
1043 case Instruction::Trunc:
1044 case Instruction::SExt:
1045 case Instruction::ZExt:
1046 case Instruction::BitCast:
1047 break;
1048 default:
1049 // Unhandled instructions are overdefined.
1050 DEBUG(dbgs() << " compute BB '" << BB->getName()
1051 << "' - overdefined (unknown cast).\n");
1052 BBLV.markOverdefined();
1053 return true;
1054 }
1055
Philip Reames38c87c22016-04-26 21:48:16 +00001056 // Figure out the range of the LHS. If that fails, we still apply the
1057 // transfer rule on the full set since we may be able to locally infer
1058 // interesting facts.
1059 if (!hasBlockValue(BBI->getOperand(0), BB))
Hans Wennborg45172ac2014-11-25 17:23:05 +00001060 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
Philip Reames38c87c22016-04-26 21:48:16 +00001061 // More work to do before applying this transfer rule.
Hans Wennborg45172ac2014-11-25 17:23:05 +00001062 return false;
Philip Reames38c87c22016-04-26 21:48:16 +00001063
1064 const unsigned OperandBitWidth =
Philip Reamese5030e82016-04-26 22:52:30 +00001065 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
Philip Reames38c87c22016-04-26 21:48:16 +00001066 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1067 if (hasBlockValue(BBI->getOperand(0), BB)) {
1068 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001069 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1070 BBI);
Philip Reames38c87c22016-04-26 21:48:16 +00001071 if (LHSVal.isConstantRange())
1072 LHSRange = LHSVal.getConstantRange();
Nick Lewycky55a700b2010-12-18 01:00:40 +00001073 }
1074
Philip Reames38c87c22016-04-26 21:48:16 +00001075 const unsigned ResultBitWidth =
1076 cast<IntegerType>(BBI->getType())->getBitWidth();
Philip Reames66715772016-04-25 18:30:31 +00001077
1078 // NOTE: We're currently limited by the set of operations that ConstantRange
1079 // can evaluate symbolically. Enhancing that set will allows us to analyze
1080 // more definitions.
1081 LVILatticeVal Result;
1082 switch (BBI->getOpcode()) {
1083 case Instruction::Trunc:
Philip Reames38c87c22016-04-26 21:48:16 +00001084 Result.markConstantRange(LHSRange.truncate(ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001085 break;
1086 case Instruction::SExt:
Philip Reames38c87c22016-04-26 21:48:16 +00001087 Result.markConstantRange(LHSRange.signExtend(ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001088 break;
1089 case Instruction::ZExt:
Philip Reames38c87c22016-04-26 21:48:16 +00001090 Result.markConstantRange(LHSRange.zeroExtend(ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001091 break;
1092 case Instruction::BitCast:
1093 Result.markConstantRange(LHSRange);
1094 break;
Philip Reames66715772016-04-25 18:30:31 +00001095 default:
Philip Reamesf105db42016-04-26 23:27:33 +00001096 // Should be dead if the code above is correct
1097 llvm_unreachable("inconsistent with above");
Philip Reames66715772016-04-25 18:30:31 +00001098 break;
Owen Anderson80d19f02010-08-18 21:11:37 +00001099 }
Nick Lewycky55a700b2010-12-18 01:00:40 +00001100
Philip Reames66715772016-04-25 18:30:31 +00001101 BBLV = Result;
1102 return true;
1103}
1104
Philip Reames92e5e1b2016-09-12 21:46:58 +00001105bool LazyValueInfoImpl::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
Philip Reames66715772016-04-25 18:30:31 +00001106 Instruction *BBI,
Philip Reamese5030e82016-04-26 22:52:30 +00001107 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +00001108
Philip Reames053c2a62016-04-26 23:10:35 +00001109 assert(BBI->getOperand(0)->getType()->isSized() &&
1110 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +00001111
1112 // Filter out operators we don't know how to reason about before attempting to
1113 // recurse on our operand(s). This can cut a long search short if we know
1114 // we're not going to be able to get any useful information anways.
1115 switch (BBI->getOpcode()) {
1116 case Instruction::Add:
1117 case Instruction::Sub:
1118 case Instruction::Mul:
1119 case Instruction::UDiv:
1120 case Instruction::Shl:
1121 case Instruction::LShr:
1122 case Instruction::And:
1123 case Instruction::Or:
1124 // continue into the code below
1125 break;
1126 default:
1127 // Unhandled instructions are overdefined.
1128 DEBUG(dbgs() << " compute BB '" << BB->getName()
1129 << "' - overdefined (unknown binary operator).\n");
1130 BBLV.markOverdefined();
1131 return true;
1132 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001133
Philip Reames053c2a62016-04-26 23:10:35 +00001134 // Figure out the range of the LHS. If that fails, use a conservative range,
1135 // but apply the transfer rule anyways. This lets us pick up facts from
1136 // expressions like "and i32 (call i32 @foo()), 32"
1137 if (!hasBlockValue(BBI->getOperand(0), BB))
1138 if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
1139 // More work to do before applying this transfer rule.
1140 return false;
1141
1142 const unsigned OperandBitWidth =
1143 DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
1144 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
1145 if (hasBlockValue(BBI->getOperand(0), BB)) {
1146 LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001147 intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
1148 BBI);
Philip Reames053c2a62016-04-26 23:10:35 +00001149 if (LHSVal.isConstantRange())
1150 LHSRange = LHSVal.getConstantRange();
Philip Reames66715772016-04-25 18:30:31 +00001151 }
Philip Reames66715772016-04-25 18:30:31 +00001152
1153 ConstantInt *RHS = cast<ConstantInt>(BBI->getOperand(1));
1154 ConstantRange RHSRange = ConstantRange(RHS->getValue());
1155
Owen Anderson80d19f02010-08-18 21:11:37 +00001156 // NOTE: We're currently limited by the set of operations that ConstantRange
1157 // can evaluate symbolically. Enhancing that set will allows us to analyze
1158 // more definitions.
Owen Anderson64c2c572010-12-20 18:18:16 +00001159 LVILatticeVal Result;
Owen Anderson80d19f02010-08-18 21:11:37 +00001160 switch (BBI->getOpcode()) {
1161 case Instruction::Add:
1162 Result.markConstantRange(LHSRange.add(RHSRange));
1163 break;
1164 case Instruction::Sub:
1165 Result.markConstantRange(LHSRange.sub(RHSRange));
1166 break;
1167 case Instruction::Mul:
1168 Result.markConstantRange(LHSRange.multiply(RHSRange));
1169 break;
1170 case Instruction::UDiv:
1171 Result.markConstantRange(LHSRange.udiv(RHSRange));
1172 break;
1173 case Instruction::Shl:
1174 Result.markConstantRange(LHSRange.shl(RHSRange));
1175 break;
1176 case Instruction::LShr:
1177 Result.markConstantRange(LHSRange.lshr(RHSRange));
1178 break;
Nick Lewyckyad48e012010-09-07 05:39:02 +00001179 case Instruction::And:
1180 Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
1181 break;
1182 case Instruction::Or:
1183 Result.markConstantRange(LHSRange.binaryOr(RHSRange));
1184 break;
Owen Anderson80d19f02010-08-18 21:11:37 +00001185 default:
Philip Reamesf105db42016-04-26 23:27:33 +00001186 // Should be dead if the code above is correct
1187 llvm_unreachable("inconsistent with above");
Owen Anderson80d19f02010-08-18 21:11:37 +00001188 break;
1189 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001190
Owen Anderson64c2c572010-12-20 18:18:16 +00001191 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +00001192 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001193}
1194
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001195static LVILatticeVal getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1196 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001197 Value *LHS = ICI->getOperand(0);
1198 Value *RHS = ICI->getOperand(1);
1199 CmpInst::Predicate Predicate = ICI->getPredicate();
1200
1201 if (isa<Constant>(RHS)) {
1202 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001203 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001204 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001205 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001206 return LVILatticeVal::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001207 else
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001208 return LVILatticeVal::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001209 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001210 }
Hal Finkel7e184492014-09-07 20:29:59 +00001211
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001212 if (!Val->getType()->isIntegerTy())
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001213 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001214
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001215 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1216 // range of Val guaranteed by the condition. Recognize comparisons in the from
1217 // of:
1218 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001219 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001220 // The latter is the range checking idiom that InstCombine produces. Subtract
1221 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001222
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001223 // Val or (add Val, Offset) can be on either hand of the comparison
1224 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1225 std::swap(LHS, RHS);
1226 Predicate = CmpInst::getSwappedPredicate(Predicate);
1227 }
Hal Finkel7e184492014-09-07 20:29:59 +00001228
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001229 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001230 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001231 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001232
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001233 if (LHS == Val || Offset) {
1234 // Calculate the range of values that are allowed by the comparison
1235 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1236 /*isFullSet=*/true);
1237 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1238 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001239 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1240 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1241 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001242
1243 // If we're interested in the false dest, invert the condition
1244 CmpInst::Predicate Pred =
1245 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1246 ConstantRange TrueValues =
1247 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1248
1249 if (Offset) // Apply the offset from above.
1250 TrueValues = TrueValues.subtract(Offset->getValue());
1251
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001252 return LVILatticeVal::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001253 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001254
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001255 return LVILatticeVal::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001256}
1257
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001258static LVILatticeVal
1259getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1260 DenseMap<Value*, LVILatticeVal> &Visited);
1261
1262static LVILatticeVal
1263getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
1264 DenseMap<Value*, LVILatticeVal> &Visited) {
1265 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1266 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1267
1268 // Handle conditions in the form of (cond1 && cond2), we know that on the
1269 // true dest path both of the conditions hold.
1270 if (!isTrueDest)
1271 return LVILatticeVal::getOverdefined();
1272
1273 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
1274 if (!BO || BO->getOpcode() != BinaryOperator::And)
1275 return LVILatticeVal::getOverdefined();
1276
1277 auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
1278 auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
1279 return intersect(RHS, LHS);
1280}
1281
1282static LVILatticeVal
1283getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
1284 DenseMap<Value*, LVILatticeVal> &Visited) {
1285 auto I = Visited.find(Cond);
1286 if (I != Visited.end())
1287 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001288
1289 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1290 Visited[Cond] = Result;
1291 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001292}
1293
1294LVILatticeVal getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest) {
1295 assert(Cond && "precondition");
1296 DenseMap<Value*, LVILatticeVal> Visited;
1297 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1298}
1299
Nuno Lopese6e04902012-06-28 01:16:18 +00001300/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001301/// Val is not constrained on the edge. Result is unspecified if return value
1302/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001303static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
1304 BasicBlock *BBTo, LVILatticeVal &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001305 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001306 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001307 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1308 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001309 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001310 if (BI->isConditional() &&
1311 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1312 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1313 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1314 "BBTo isn't a successor of BBFrom");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001315
Chris Lattner19019ea2009-11-11 22:48:44 +00001316 // If V is the condition of the branch itself, then we know exactly what
1317 // it is.
Nick Lewycky55a700b2010-12-18 01:00:40 +00001318 if (BI->getCondition() == Val) {
1319 Result = LVILatticeVal::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001320 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001321 return true;
1322 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001323
Chris Lattner19019ea2009-11-11 22:48:44 +00001324 // If the condition of the branch is an equality comparison, we may be
1325 // able to infer the value.
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001326 Result = getValueFromCondition(Val, BI->getCondition(), isTrueDest);
1327 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001328 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001329 }
1330 }
Chris Lattner77358782009-11-15 20:02:12 +00001331
1332 // If the edge was formed by a switch on the value, then we may know exactly
1333 // what it is.
1334 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001335 if (SI->getCondition() != Val)
1336 return false;
1337
1338 bool DefaultCase = SI->getDefaultDest() == BBTo;
1339 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1340 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1341
Hans Wennborgcbb18e32014-11-21 19:07:46 +00001342 for (SwitchInst::CaseIt i : SI->cases()) {
Nuno Lopes8650fb82012-06-28 16:13:37 +00001343 ConstantRange EdgeVal(i.getCaseValue()->getValue());
Manman Renf3fedb62012-09-05 23:45:58 +00001344 if (DefaultCase) {
1345 // It is possible that the default destination is the destination of
1346 // some cases. There is no need to perform difference for those cases.
1347 if (i.getCaseSuccessor() != BBTo)
1348 EdgesVals = EdgesVals.difference(EdgeVal);
1349 } else if (i.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001350 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001351 }
Benjamin Kramer2337c1f2016-02-20 10:40:34 +00001352 Result = LVILatticeVal::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001353 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001354 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001355 return false;
1356}
1357
Sanjay Patel938e2792015-01-09 16:35:37 +00001358/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1359/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001360bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Hal Finkel7e184492014-09-07 20:29:59 +00001361 BasicBlock *BBTo, LVILatticeVal &Result,
1362 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001363 // If already a constant, there is nothing to compute.
1364 if (Constant *VC = dyn_cast<Constant>(Val)) {
1365 Result = LVILatticeVal::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001366 return true;
1367 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001368
Philip Reames44456b82016-02-02 03:15:40 +00001369 LVILatticeVal LocalResult;
1370 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1371 // If we couldn't constrain the value on the edge, LocalResult doesn't
1372 // provide any information.
1373 LocalResult.markOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001374
Philip Reames44456b82016-02-02 03:15:40 +00001375 if (hasSingleValue(LocalResult)) {
1376 // Can't get any more precise here
1377 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001378 return true;
1379 }
1380
1381 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001382 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1383 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001384 // No new information.
1385 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001386 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001387 }
1388
Philip Reames44456b82016-02-02 03:15:40 +00001389 // Try to intersect ranges of the BB and the constraint on the edge.
1390 LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001391 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1392 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001393 // We can use the context instruction (generically the ultimate instruction
1394 // the calling pass is trying to simplify) here, even though the result of
1395 // this function is generally cached when called from the solve* functions
1396 // (and that cached result might be used with queries using a different
1397 // context instruction), because when this function is called from the solve*
1398 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001399 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001400 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001401 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001402
1403 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001404 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001405}
1406
Philip Reames92e5e1b2016-09-12 21:46:58 +00001407LVILatticeVal LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
Hal Finkel7e184492014-09-07 20:29:59 +00001408 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001409 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001410 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001411
Hans Wennborg45172ac2014-11-25 17:23:05 +00001412 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001413 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001414 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001415 solve();
1416 }
Owen Andersonc7ed4dc2010-12-09 06:14:58 +00001417 LVILatticeVal Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001418 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001419
1420 DEBUG(dbgs() << " Result = " << Result << "\n");
1421 return Result;
1422}
1423
Philip Reames92e5e1b2016-09-12 21:46:58 +00001424LVILatticeVal LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001425 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1426 << CxtI->getName() << "'\n");
1427
Philip Reamesbb781b42016-02-10 21:46:32 +00001428 if (auto *C = dyn_cast<Constant>(V))
1429 return LVILatticeVal::get(C);
1430
Philip Reamesd1f829d2016-02-02 21:57:37 +00001431 LVILatticeVal Result = LVILatticeVal::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001432 if (auto *I = dyn_cast<Instruction>(V))
1433 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001434 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001435
David Greene37e98092009-12-23 20:43:58 +00001436 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001437 return Result;
1438}
Chris Lattner19019ea2009-11-11 22:48:44 +00001439
Philip Reames92e5e1b2016-09-12 21:46:58 +00001440LVILatticeVal LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001441getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1442 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001443 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001444 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001445
Nick Lewycky55a700b2010-12-18 01:00:40 +00001446 LVILatticeVal Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001447 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001448 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001449 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001450 (void)WasFastQuery;
1451 assert(WasFastQuery && "More work to do after problem solved?");
1452 }
1453
David Greene37e98092009-12-23 20:43:58 +00001454 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001455 return Result;
1456}
1457
Philip Reames92e5e1b2016-09-12 21:46:58 +00001458void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001459 BasicBlock *NewSucc) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001460 // When an edge in the graph has been threaded, values that we could not
1461 // determine a value for before (i.e. were marked overdefined) may be
1462 // possible to solve now. We do NOT try to proactively update these values.
1463 // Instead, we clear their entries from the cache, and allow lazy updating to
1464 // recompute them when needed.
1465
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001466 // The updating process is fairly simple: we need to drop cached info
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001467 // for all values that were marked overdefined in OldSucc, and for those same
1468 // values in any successor of OldSucc (except NewSucc) in which they were
1469 // also marked overdefined.
1470 std::vector<BasicBlock*> worklist;
1471 worklist.push_back(OldSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001472
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001473 auto I = OverDefinedCache.find(OldSucc);
1474 if (I == OverDefinedCache.end())
1475 return; // Nothing to process here.
Bruno Cardoso Lopes7a1483e2015-08-21 21:18:26 +00001476 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001477
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001478 // Use a worklist to perform a depth-first search of OldSucc's successors.
1479 // NOTE: We do not need a visited list since any blocks we have already
1480 // visited will have had their overdefined markers cleared already, and we
1481 // thus won't loop to their successors.
1482 while (!worklist.empty()) {
1483 BasicBlock *ToUpdate = worklist.back();
1484 worklist.pop_back();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001485
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001486 // Skip blocks only accessible through NewSucc.
1487 if (ToUpdate == NewSucc) continue;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001488
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001489 bool changed = false;
Bruno Cardoso Lopes7a1483e2015-08-21 21:18:26 +00001490 for (Value *V : ValsToClear) {
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001491 // If a value was marked overdefined in OldSucc, and is here too...
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001492 auto OI = OverDefinedCache.find(ToUpdate);
1493 if (OI == OverDefinedCache.end())
1494 continue;
1495 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
1496 if (!ValueSet.count(V))
1497 continue;
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001498
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +00001499 ValueSet.erase(V);
1500 if (ValueSet.empty())
1501 OverDefinedCache.erase(OI);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001502
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001503 // If we removed anything, then we potentially need to update
Owen Andersonaac5a722010-07-27 23:58:11 +00001504 // blocks successors too.
1505 changed = true;
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001506 }
Nick Lewycky55a700b2010-12-18 01:00:40 +00001507
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001508 if (!changed) continue;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001509
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001510 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
1511 }
1512}
1513
Chris Lattneraf025d32009-11-15 19:59:49 +00001514//===----------------------------------------------------------------------===//
1515// LazyValueInfo Impl
1516//===----------------------------------------------------------------------===//
1517
Philip Reames92e5e1b2016-09-12 21:46:58 +00001518/// This lazily constructs the LazyValueInfoImpl.
1519static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1520 const DataLayout *DL,
1521 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001522 if (!PImpl) {
1523 assert(DL && "getCache() called with a null DataLayout");
Philip Reames92e5e1b2016-09-12 21:46:58 +00001524 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001525 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001526 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001527}
1528
Sean Silva687019f2016-06-13 22:01:25 +00001529bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
1530 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001531 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001532
1533 DominatorTreeWrapperPass *DTWP =
1534 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001535 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1536 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001537
Sean Silva687019f2016-06-13 22:01:25 +00001538 if (Info.PImpl)
Philip Reames92e5e1b2016-09-12 21:46:58 +00001539 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001540
Owen Anderson208636f2010-08-18 18:39:01 +00001541 // Fully lazy.
1542 return false;
1543}
1544
Sean Silva687019f2016-06-13 22:01:25 +00001545void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001546 AU.setPreservesAll();
Chandler Carruth66b31302015-01-04 12:03:27 +00001547 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001548 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001549}
1550
Sean Silva687019f2016-06-13 22:01:25 +00001551LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1552
1553LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1554
Chris Lattneraf025d32009-11-15 19:59:49 +00001555void LazyValueInfo::releaseMemory() {
1556 // If the cache was allocated, free it.
1557 if (PImpl) {
Philip Reames92e5e1b2016-09-12 21:46:58 +00001558 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001559 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001560 }
1561}
1562
Sean Silva687019f2016-06-13 22:01:25 +00001563void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1564
1565LazyValueInfo LazyValueAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
1566 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
1567 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1568 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1569
1570 return LazyValueInfo(&AC, &TLI, DT);
1571}
1572
Hal Finkel7e184492014-09-07 20:29:59 +00001573Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1574 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001575 const DataLayout &DL = BB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001576 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001577 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001578
Chris Lattner19019ea2009-11-11 22:48:44 +00001579 if (Result.isConstant())
1580 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001581 if (Result.isConstantRange()) {
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001582 ConstantRange CR = Result.getConstantRange();
1583 if (const APInt *SingleVal = CR.getSingleElement())
1584 return ConstantInt::get(V->getContext(), *SingleVal);
1585 }
Craig Topper9f008862014-04-15 04:59:12 +00001586 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001587}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001588
John Regehre1c481d2016-05-02 19:58:00 +00001589ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001590 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001591 assert(V->getType()->isIntegerTy());
1592 unsigned Width = V->getType()->getIntegerBitWidth();
1593 const DataLayout &DL = BB->getModule()->getDataLayout();
1594 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001595 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001596 if (Result.isUndefined())
1597 return ConstantRange(Width, /*isFullSet=*/false);
1598 if (Result.isConstantRange())
1599 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001600 // We represent ConstantInt constants as constant ranges but other kinds
1601 // of integer constants, i.e. ConstantExpr will be tagged as constants
1602 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1603 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001604 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001605}
1606
Sanjay Patel2a385e22015-01-09 16:47:20 +00001607/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001608/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001609Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001610 BasicBlock *ToBB,
1611 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001612 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001613 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001614 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001615
Chris Lattnerd5e25432009-11-12 01:29:10 +00001616 if (Result.isConstant())
1617 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001618 if (Result.isConstantRange()) {
Owen Anderson185fe002010-08-10 20:03:09 +00001619 ConstantRange CR = Result.getConstantRange();
1620 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 Lattnerd5e25432009-11-12 01:29:10 +00001624}
1625
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001626static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
1627 LVILatticeVal &Result,
1628 const DataLayout &DL,
1629 TargetLibraryInfo *TLI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001630
Chris Lattner565ee2f2009-11-12 04:36:58 +00001631 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001632 Constant *Res = nullptr;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001633 if (Result.isConstant()) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001634 Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001635 TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001636 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001637 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1638 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001639 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001640
Owen Anderson185fe002010-08-10 20:03:09 +00001641 if (Result.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001642 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001643 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001644
Owen Anderson185fe002010-08-10 20:03:09 +00001645 ConstantRange CR = Result.getConstantRange();
1646 if (Pred == ICmpInst::ICMP_EQ) {
1647 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001648 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001649
Owen Anderson185fe002010-08-10 20:03:09 +00001650 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001651 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001652 } else if (Pred == ICmpInst::ICMP_NE) {
1653 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001654 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001655
Owen Anderson185fe002010-08-10 20:03:09 +00001656 if (CR.isSingleElement() && CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001657 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001658 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001659
Owen Anderson185fe002010-08-10 20:03:09 +00001660 // Handle more complex predicates.
Nick Lewycky11678bd2010-12-15 18:57:18 +00001661 ConstantRange TrueValues =
1662 ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
1663 if (TrueValues.contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001664 return LazyValueInfo::True;
Nick Lewycky11678bd2010-12-15 18:57:18 +00001665 if (TrueValues.inverse().contains(CR))
Hal Finkel7e184492014-09-07 20:29:59 +00001666 return LazyValueInfo::False;
1667 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001668 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001669
Chris Lattneraf025d32009-11-15 19:59:49 +00001670 if (Result.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001671 // If this is an equality comparison, we can try to fold it knowing that
1672 // "V != C1".
1673 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001674 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001675 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001676 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001677 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001678 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001679 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001680 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001681 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001682 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Rafael Espindola7c68beb2014-02-18 15:33:12 +00001683 Result.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001684 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001685 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001686 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001687 }
Hal Finkel7e184492014-09-07 20:29:59 +00001688 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001689 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001690
Hal Finkel7e184492014-09-07 20:29:59 +00001691 return LazyValueInfo::Unknown;
1692}
1693
Sanjay Patel2a385e22015-01-09 16:47:20 +00001694/// Determine whether the specified value comparison with a constant is known to
1695/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001696LazyValueInfo::Tristate
1697LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1698 BasicBlock *FromBB, BasicBlock *ToBB,
1699 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001700 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001701 LVILatticeVal Result =
Philip Reames92e5e1b2016-09-12 21:46:58 +00001702 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001703
1704 return getPredicateResult(Pred, C, Result, DL, TLI);
1705}
1706
1707LazyValueInfo::Tristate
1708LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1709 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001710 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001711 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001712 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1713 if (Ret != Unknown)
1714 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001715
Philip Reamesaeefae02015-11-04 01:47:04 +00001716 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1717 // LVI as a whole tries to compute a lattice value which is conservatively
1718 // correct at a given location. In this case, we have a predicate which we
1719 // weren't able to prove about the merged result, and we're pushing that
1720 // predicate back along each incoming edge to see if we can prove it
1721 // separately for each input. As a motivating example, consider:
1722 // bb1:
1723 // %v1 = ... ; constantrange<1, 5>
1724 // br label %merge
1725 // bb2:
1726 // %v2 = ... ; constantrange<10, 20>
1727 // br label %merge
1728 // merge:
1729 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1730 // %pred = icmp eq i32 %phi, 8
1731 // We can't tell from the lattice value for '%phi' that '%pred' is false
1732 // along each path, but by checking the predicate over each input separately,
1733 // we can.
1734 // We limit the search to one step backwards from the current BB and value.
1735 // We could consider extending this to search further backwards through the
1736 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001737 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001738 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001739 BasicBlock *BB = CxtI->getParent();
1740
1741 // Function entry or an unreachable block. Bail to avoid confusing
1742 // analysis below.
1743 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1744 if (PI == PE)
1745 return Unknown;
1746
1747 // If V is a PHI node in the same block as the context, we need to ask
1748 // questions about the predicate as applied to the incoming value along
1749 // each edge. This is useful for eliminating cases where the predicate is
1750 // known along all incoming edges.
1751 if (auto *PHI = dyn_cast<PHINode>(V))
1752 if (PHI->getParent() == BB) {
1753 Tristate Baseline = Unknown;
1754 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1755 Value *Incoming = PHI->getIncomingValue(i);
1756 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001757 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001758 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1759 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001760
Philip Reamesbb11d622015-08-31 18:31:48 +00001761 // Keep going as long as we've seen a consistent known result for
1762 // all inputs.
1763 Baseline = (i == 0) ? Result /* First iteration */
1764 : (Baseline == Result ? Baseline : Unknown); /* All others */
1765 if (Baseline == Unknown)
1766 break;
1767 }
1768 if (Baseline != Unknown)
1769 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001770 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001771
Philip Reames66ab0f02015-06-16 00:49:59 +00001772 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001773 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001774 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001775 if (!isa<Instruction>(V) ||
1776 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001777 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001778 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001779 // the value of the comparison in this block.
1780 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1781 if (Baseline != Unknown) {
1782 // Check that all remaining incoming values match the first one.
1783 while (++PI != PE) {
1784 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1785 if (Ret != Baseline) break;
1786 }
1787 // If we terminated early, then one of the values didn't match.
1788 if (PI == PE) {
1789 return Baseline;
1790 }
1791 }
1792 }
1793 }
1794 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001795}
1796
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001797void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001798 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001799 if (PImpl) {
1800 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001801 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001802 }
Owen Anderson208636f2010-08-18 18:39:01 +00001803}
1804
1805void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001806 if (PImpl) {
1807 const DataLayout &DL = BB->getModule()->getDataLayout();
Philip Reames92e5e1b2016-09-12 21:46:58 +00001808 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001809 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001810}