blob: 110c085d3f353224dac76da6543b969797a6b2f5 [file] [log] [blame]
Hans Wennborg36c3fc52014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner10f2d132009-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 Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
John Regehr21e89f92018-11-21 05:24:12 +000017#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
Daniel Jasper8de3a542016-12-19 08:22:17 +000019#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/Analysis/ConstantFolding.h"
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +000021#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruthbda13492015-01-15 02:16:27 +000022#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000023#include "llvm/Analysis/ValueTracking.h"
Florian Hahn5133f6f2017-09-28 11:09:22 +000024#include "llvm/Analysis/ValueLattice.h"
Anna Thomas6cde8772017-03-22 19:27:12 +000025#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth03e36d72014-03-04 11:45:46 +000026#include "llvm/IR/CFG.h"
Chandler Carruth19d764f2014-03-04 12:24:34 +000027#include "llvm/IR/ConstantRange.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
Hal Finkel3ef1aae2014-09-07 20:29:59 +000030#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/Instructions.h"
32#include "llvm/IR/IntrinsicInst.h"
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +000033#include "llvm/IR/Intrinsics.h"
Philip Reames45ef74f2015-10-29 03:57:17 +000034#include "llvm/IR/LLVMContext.h"
Chandler Carruthdf3d8e82014-03-04 11:08:18 +000035#include "llvm/IR/PatternMatch.h"
Chandler Carrutheb3d76d2014-03-04 11:17:44 +000036#include "llvm/IR/ValueHandle.h"
Chris Lattnerb8c124c2009-11-12 01:22:16 +000037#include "llvm/Support/Debug.h"
Anna Thomas6cde8772017-03-22 19:27:12 +000038#include "llvm/Support/FormattedStream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000039#include "llvm/Support/raw_ostream.h"
Bill Wendlingaa8b9942012-01-11 23:43:34 +000040#include <map>
Chris Lattner10f2d132009-11-11 00:22:30 +000041using namespace llvm;
Benjamin Kramer8979e5f2012-03-02 15:34:43 +000042using namespace PatternMatch;
Chris Lattner10f2d132009-11-11 00:22:30 +000043
Chandler Carruth4da25372014-04-22 02:48:03 +000044#define DEBUG_TYPE "lazy-value-info"
45
Daniel Berlinf48edbb2017-02-08 15:22:52 +000046// This is the number of worklist items we will process to try to discover an
47// answer for a given value.
48static const unsigned MaxProcessedPerValue = 500;
49
Sean Silva9ce41c32016-06-13 22:01:25 +000050char LazyValueInfoWrapperPass::ID = 0;
51INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosieraab8e282011-12-02 01:26:24 +000052 "Lazy Value Information Analysis", false, true)
Daniel Jasper8de3a542016-12-19 08:22:17 +000053INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carrutheeeec3c2015-01-15 10:41:28 +000054INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva9ce41c32016-06-13 22:01:25 +000055INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersonce665bd2010-10-07 22:25:06 +000056 "Lazy Value Information Analysis", false, true)
Chris Lattner10f2d132009-11-11 00:22:30 +000057
58namespace llvm {
Sean Silva9ce41c32016-06-13 22:01:25 +000059 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner10f2d132009-11-11 00:22:30 +000060}
61
Chandler Carruth33d56812016-11-23 17:53:26 +000062AnalysisKey LazyValueAnalysis::Key;
Chris Lattnercc4d3b22009-11-11 02:08:33 +000063
Philip Reames524fa2e2016-02-02 22:03:19 +000064/// Returns true if this lattice value represents at most one possible value.
65/// This is as precise as any lattice value can get while still representing
66/// reachable code.
Florian Hahn5133f6f2017-09-28 11:09:22 +000067static bool hasSingleValue(const ValueLatticeElement &Val) {
Philip Reames524fa2e2016-02-02 22:03:19 +000068 if (Val.isConstantRange() &&
69 Val.getConstantRange().isSingleElement())
70 // Integer constants are single element ranges
71 return true;
72 if (Val.isConstant())
73 // Non integer constants
74 return true;
75 return false;
76}
77
78/// Combine two sets of facts about the same value into a single set of
79/// facts. Note that this method is not suitable for merging facts along
80/// different paths in a CFG; that's what the mergeIn function is for. This
81/// is for merging facts gathered about the same value at the same location
82/// through two independent means.
83/// Notes:
84/// * This method does not promise to return the most precise possible lattice
85/// value implied by A and B. It is allowed to return any lattice element
86/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumi6f439632016-07-04 01:26:27 +000087/// conflict, see below).
Philip Reames524fa2e2016-02-02 22:03:19 +000088/// * Due to unreachable code, the intersection of two lattice values could be
89/// contradictory. If this happens, we return some valid lattice value so as
90/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
91/// we do not make this guarantee. TODO: This would be a useful enhancement.
Florian Hahn5133f6f2017-09-28 11:09:22 +000092static ValueLatticeElement intersect(const ValueLatticeElement &A,
93 const ValueLatticeElement &B) {
Philip Reames524fa2e2016-02-02 22:03:19 +000094 // Undefined is the strongest state. It means the value is known to be along
95 // an unreachable path.
96 if (A.isUndefined())
97 return A;
98 if (B.isUndefined())
99 return B;
100
101 // If we gave up for one, but got a useable fact from the other, use it.
102 if (A.isOverdefined())
103 return B;
104 if (B.isOverdefined())
105 return A;
106
107 // Can't get any more precise than constants.
108 if (hasSingleValue(A))
109 return A;
110 if (hasSingleValue(B))
111 return B;
112
113 // Could be either constant range or not constant here.
114 if (!A.isConstantRange() || !B.isConstantRange()) {
115 // TODO: Arbitrary choice, could be improved
116 return A;
117 }
118
119 // Intersect two constant ranges
120 ConstantRange Range =
121 A.getConstantRange().intersectWith(B.getConstantRange());
122 // Note: An empty range is implicitly converted to overdefined internally.
123 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumi6f439632016-07-04 01:26:27 +0000124 // and thus know this path must be unreachable.
Florian Hahn5133f6f2017-09-28 11:09:22 +0000125 return ValueLatticeElement::getRange(std::move(Range));
Philip Reames524fa2e2016-02-02 22:03:19 +0000126}
Philip Reamesdc27a092016-02-02 21:57:37 +0000127
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000128//===----------------------------------------------------------------------===//
Chris Lattner2c5adf82009-11-15 19:59:49 +0000129// LazyValueInfoCache Decl
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000130//===----------------------------------------------------------------------===//
131
Chris Lattner2c5adf82009-11-15 19:59:49 +0000132namespace {
Sanjay Patel61478e32015-01-09 16:47:20 +0000133 /// A callback value handle updates the cache when values are erased.
Owen Anderson89778462011-01-05 21:15:29 +0000134 class LazyValueInfoCache;
David Blaikiec4423f02015-08-03 22:30:24 +0000135 struct LVIValueHandle final : public CallbackVH {
Justin Lebarb3f897d2016-07-27 22:33:36 +0000136 // Needs to access getValPtr(), which is protected.
137 friend struct DenseMapInfo<LVIValueHandle>;
138
Owen Anderson89778462011-01-05 21:15:29 +0000139 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000140
Owen Anderson89778462011-01-05 21:15:29 +0000141 LVIValueHandle(Value *V, LazyValueInfoCache *P)
142 : CallbackVH(V), Parent(P) { }
Craig Topperc37e6c02014-03-05 07:30:04 +0000143
144 void deleted() override;
145 void allUsesReplacedWith(Value *V) override {
Owen Anderson89778462011-01-05 21:15:29 +0000146 deleted();
147 }
148 };
Justin Lebarb3f897d2016-07-27 22:33:36 +0000149} // end anonymous namespace
Owen Anderson89778462011-01-05 21:15:29 +0000150
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000151namespace {
Sanjay Patel61478e32015-01-09 16:47:20 +0000152 /// This is the cache kept by LazyValueInfo which
Chris Lattner2c5adf82009-11-15 19:59:49 +0000153 /// maintains information about queries across the clients' queries.
154 class LazyValueInfoCache {
Sanjay Patel61478e32015-01-09 16:47:20 +0000155 /// This is all of the cached block information for exactly one Value*.
156 /// The entries are sorted by the BasicBlock* of the
Chris Lattner2c5adf82009-11-15 19:59:49 +0000157 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000158 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
159 /// memory overhead.
Justin Lebarb3f897d2016-07-27 22:33:36 +0000160 struct ValueCacheEntryTy {
161 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
162 LVIValueHandle Handle;
Florian Hahn5133f6f2017-09-28 11:09:22 +0000163 SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals;
Justin Lebarb3f897d2016-07-27 22:33:36 +0000164 };
Chris Lattner2c5adf82009-11-15 19:59:49 +0000165
Sanjay Patel61478e32015-01-09 16:47:20 +0000166 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000167 /// over-defined at the end of that block.
Chandler Carruth55856262017-01-24 12:55:57 +0000168 typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
Bruno Cardoso Lopesbfd49ab2015-08-18 16:34:27 +0000169 OverDefinedCacheTy;
Sanjay Patel61478e32015-01-09 16:47:20 +0000170 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramerfeb9b4b2011-12-03 15:16:45 +0000171 /// don't spend time removing unused blocks from our caches.
Chandler Carruth55856262017-01-24 12:55:57 +0000172 DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
Benjamin Kramerfeb9b4b2011-12-03 15:16:45 +0000173
Anna Thomas6cde8772017-03-22 19:27:12 +0000174 /// This is all of the cached information for all values,
175 /// mapped from Value* to key information.
176 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
177 OverDefinedCacheTy OverDefinedCache;
178
179
Philip Reames35517182016-09-12 22:38:44 +0000180 public:
Florian Hahn5133f6f2017-09-28 11:09:22 +0000181 void insertResult(Value *Val, BasicBlock *BB,
182 const ValueLatticeElement &Result) {
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000183 SeenBlocks.insert(BB);
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000184
185 // Insert over-defined values into their own cache to reduce memory
186 // overhead.
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000187 if (Result.isOverdefined())
Bruno Cardoso Lopesbfd49ab2015-08-18 16:34:27 +0000188 OverDefinedCache[BB].insert(Val);
Justin Lebarb3f897d2016-07-27 22:33:36 +0000189 else {
190 auto It = ValueCache.find_as(Val);
191 if (It == ValueCache.end()) {
192 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
193 It = ValueCache.find_as(Val);
194 assert(It != ValueCache.end() && "Val was just added to the map!");
195 }
196 It->second->BlockVals[BB] = Result;
197 }
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000198 }
Owen Anderson7f9cb742010-07-30 23:59:40 +0000199
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000200 bool isOverdefined(Value *V, BasicBlock *BB) const {
201 auto ODI = OverDefinedCache.find(BB);
202
203 if (ODI == OverDefinedCache.end())
204 return false;
205
206 return ODI->second.count(V);
207 }
208
Philip Reames35517182016-09-12 22:38:44 +0000209 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000210 if (isOverdefined(V, BB))
211 return true;
212
Justin Lebarb3f897d2016-07-27 22:33:36 +0000213 auto I = ValueCache.find_as(V);
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000214 if (I == ValueCache.end())
215 return false;
216
Justin Lebarb3f897d2016-07-27 22:33:36 +0000217 return I->second->BlockVals.count(BB);
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000218 }
219
Florian Hahn5133f6f2017-09-28 11:09:22 +0000220 ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000221 if (isOverdefined(V, BB))
Florian Hahn5133f6f2017-09-28 11:09:22 +0000222 return ValueLatticeElement::getOverdefined();
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000223
Justin Lebarb3f897d2016-07-27 22:33:36 +0000224 auto I = ValueCache.find_as(V);
225 if (I == ValueCache.end())
Florian Hahn5133f6f2017-09-28 11:09:22 +0000226 return ValueLatticeElement();
Justin Lebarb3f897d2016-07-27 22:33:36 +0000227 auto BBI = I->second->BlockVals.find(BB);
228 if (BBI == I->second->BlockVals.end())
Florian Hahn5133f6f2017-09-28 11:09:22 +0000229 return ValueLatticeElement();
Justin Lebarb3f897d2016-07-27 22:33:36 +0000230 return BBI->second;
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000231 }
NAKAMURA Takumi283ee812016-07-04 01:26:33 +0000232
Philip Reames8efea572016-09-12 21:46:58 +0000233 /// clear - Empty the cache.
234 void clear() {
235 SeenBlocks.clear();
236 ValueCache.clear();
237 OverDefinedCache.clear();
238 }
239
Philip Reamesac22fbe2016-09-12 22:03:36 +0000240 /// Inform the cache that a given value has been deleted.
241 void eraseValue(Value *V);
242
243 /// This is part of the update interface to inform the cache
244 /// that a block has been deleted.
245 void eraseBlock(BasicBlock *BB);
246
Philip Reames35517182016-09-12 22:38:44 +0000247 /// Updates the cache to remove any influence an overdefined value in
248 /// OldSucc might have (unless also overdefined in NewSucc). This just
249 /// flushes elements from the cache and does not add any.
250 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
251
Philip Reames8efea572016-09-12 21:46:58 +0000252 friend struct LVIValueHandle;
253 };
Philip Reamesac22fbe2016-09-12 22:03:36 +0000254}
Philip Reames8efea572016-09-12 21:46:58 +0000255
Philip Reamesac22fbe2016-09-12 22:03:36 +0000256void LazyValueInfoCache::eraseValue(Value *V) {
Chandler Carruthc5374d22017-01-26 08:31:54 +0000257 for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) {
258 // Copy and increment the iterator immediately so we can erase behind
259 // ourselves.
260 auto Iter = I++;
261 SmallPtrSetImpl<Value *> &ValueSet = Iter->second;
Philip Reamesef2b74a2016-12-30 22:09:10 +0000262 ValueSet.erase(V);
Philip Reamesac22fbe2016-09-12 22:03:36 +0000263 if (ValueSet.empty())
Chandler Carruthc5374d22017-01-26 08:31:54 +0000264 OverDefinedCache.erase(Iter);
Philip Reamesac22fbe2016-09-12 22:03:36 +0000265 }
Philip Reamesac22fbe2016-09-12 22:03:36 +0000266
267 ValueCache.erase(V);
268}
269
270void LVIValueHandle::deleted() {
271 // This erasure deallocates *this, so it MUST happen after we're done
272 // using any and all members of *this.
273 Parent->eraseValue(*this);
274}
275
276void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
277 // Shortcut if we have never seen this block.
Chandler Carruth55856262017-01-24 12:55:57 +0000278 DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
Philip Reamesac22fbe2016-09-12 22:03:36 +0000279 if (I == SeenBlocks.end())
280 return;
281 SeenBlocks.erase(I);
282
283 auto ODI = OverDefinedCache.find(BB);
284 if (ODI != OverDefinedCache.end())
285 OverDefinedCache.erase(ODI);
286
287 for (auto &I : ValueCache)
288 I.second->BlockVals.erase(BB);
289}
290
Philip Reames35517182016-09-12 22:38:44 +0000291void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
292 BasicBlock *NewSucc) {
293 // When an edge in the graph has been threaded, values that we could not
294 // determine a value for before (i.e. were marked overdefined) may be
295 // possible to solve now. We do NOT try to proactively update these values.
296 // Instead, we clear their entries from the cache, and allow lazy updating to
297 // recompute them when needed.
298
299 // The updating process is fairly simple: we need to drop cached info
300 // for all values that were marked overdefined in OldSucc, and for those same
301 // values in any successor of OldSucc (except NewSucc) in which they were
302 // also marked overdefined.
303 std::vector<BasicBlock*> worklist;
304 worklist.push_back(OldSucc);
305
306 auto I = OverDefinedCache.find(OldSucc);
307 if (I == OverDefinedCache.end())
308 return; // Nothing to process here.
309 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
310
311 // Use a worklist to perform a depth-first search of OldSucc's successors.
312 // NOTE: We do not need a visited list since any blocks we have already
313 // visited will have had their overdefined markers cleared already, and we
314 // thus won't loop to their successors.
315 while (!worklist.empty()) {
316 BasicBlock *ToUpdate = worklist.back();
317 worklist.pop_back();
318
319 // Skip blocks only accessible through NewSucc.
320 if (ToUpdate == NewSucc) continue;
321
Philip Reamese0083002016-12-30 17:56:47 +0000322 // If a value was marked overdefined in OldSucc, and is here too...
323 auto OI = OverDefinedCache.find(ToUpdate);
324 if (OI == OverDefinedCache.end())
325 continue;
326 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
327
Philip Reames35517182016-09-12 22:38:44 +0000328 bool changed = false;
329 for (Value *V : ValsToClear) {
Philip Reamesef2b74a2016-12-30 22:09:10 +0000330 if (!ValueSet.erase(V))
Philip Reames35517182016-09-12 22:38:44 +0000331 continue;
332
Philip Reames35517182016-09-12 22:38:44 +0000333 // If we removed anything, then we potentially need to update
334 // blocks successors too.
335 changed = true;
Philip Reamese0083002016-12-30 17:56:47 +0000336
337 if (ValueSet.empty()) {
338 OverDefinedCache.erase(OI);
339 break;
340 }
Philip Reames35517182016-09-12 22:38:44 +0000341 }
342
343 if (!changed) continue;
344
345 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
346 }
347}
348
Anna Thomas46747f12017-06-06 19:25:31 +0000349
350namespace {
351/// An assembly annotator class to print LazyValueCache information in
352/// comments.
353class LazyValueInfoImpl;
354class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {
355 LazyValueInfoImpl *LVIImpl;
356 // While analyzing which blocks we can solve values for, we need the dominator
357 // information. Since this is an optional parameter in LVI, we require this
358 // DomTreeAnalysis pass in the printer pass, and pass the dominator
359 // tree to the LazyValueInfoAnnotatedWriter.
360 DominatorTree &DT;
361
362public:
363 LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)
364 : LVIImpl(L), DT(DTree) {}
365
366 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
367 formatted_raw_ostream &OS);
368
369 virtual void emitInstructionAnnot(const Instruction *I,
370 formatted_raw_ostream &OS);
371};
372}
Philip Reamesac22fbe2016-09-12 22:03:36 +0000373namespace {
Philip Reames8efea572016-09-12 21:46:58 +0000374 // The actual implementation of the lazy analysis and update. Note that the
375 // inheritance from LazyValueInfoCache is intended to be temporary while
376 // splitting the code and then transitioning to a has-a relationship.
Philip Reames35517182016-09-12 22:38:44 +0000377 class LazyValueInfoImpl {
378
379 /// Cached results from previous queries
380 LazyValueInfoCache TheCache;
Philip Reames8efea572016-09-12 21:46:58 +0000381
382 /// This stack holds the state of the value solver during a query.
383 /// It basically emulates the callstack of the naive
384 /// recursive value lookup process.
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000385 SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
Philip Reames8efea572016-09-12 21:46:58 +0000386
387 /// Keeps track of which block-value pairs are in BlockValueStack.
388 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
389
390 /// Push BV onto BlockValueStack unless it's already in there.
391 /// Returns true on success.
392 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
393 if (!BlockValueSet.insert(BV).second)
394 return false; // It's already in the stack.
395
Nicola Zaghen0818e782018-05-14 12:53:11 +0000396 LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in "
397 << BV.first->getName() << "\n");
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000398 BlockValueStack.push_back(BV);
Philip Reames8efea572016-09-12 21:46:58 +0000399 return true;
400 }
401
Daniel Jasper8de3a542016-12-19 08:22:17 +0000402 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
Philip Reames8efea572016-09-12 21:46:58 +0000403 const DataLayout &DL; ///< A mandatory DataLayout
404 DominatorTree *DT; ///< An optional DT pointer.
Brian M. Rzycki55da8a32018-02-16 16:35:17 +0000405 DominatorTree *DisabledDT; ///< Stores DT if it's disabled.
Philip Reames8efea572016-09-12 21:46:58 +0000406
Florian Hahn5133f6f2017-09-28 11:09:22 +0000407 ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB);
Philip Reames8efea572016-09-12 21:46:58 +0000408 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
Florian Hahn5133f6f2017-09-28 11:09:22 +0000409 ValueLatticeElement &Result, Instruction *CxtI = nullptr);
Philip Reames8efea572016-09-12 21:46:58 +0000410 bool hasBlockValue(Value *Val, BasicBlock *BB);
411
412 // These methods process one work item and may add more. A false value
413 // returned means that the work item was not completely processed and must
414 // be revisited after going through the new items.
415 bool solveBlockValue(Value *Val, BasicBlock *BB);
Florian Hahn5133f6f2017-09-28 11:09:22 +0000416 bool solveBlockValueImpl(ValueLatticeElement &Res, Value *Val,
417 BasicBlock *BB);
418 bool solveBlockValueNonLocal(ValueLatticeElement &BBLV, Value *Val,
Philip Reames8efea572016-09-12 21:46:58 +0000419 BasicBlock *BB);
Florian Hahn5133f6f2017-09-28 11:09:22 +0000420 bool solveBlockValuePHINode(ValueLatticeElement &BBLV, PHINode *PN,
421 BasicBlock *BB);
422 bool solveBlockValueSelect(ValueLatticeElement &BBLV, SelectInst *S,
423 BasicBlock *BB);
John Regehr21e89f92018-11-21 05:24:12 +0000424 Optional<ConstantRange> getRangeForOperand(unsigned Op, Instruction *I,
425 BasicBlock *BB);
Florian Hahn5133f6f2017-09-28 11:09:22 +0000426 bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
427 BasicBlock *BB);
428 bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
Philip Reames8efea572016-09-12 21:46:58 +0000429 BasicBlock *BB);
430 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
Florian Hahn5133f6f2017-09-28 11:09:22 +0000431 ValueLatticeElement &BBLV,
Craig Topper23873bb2017-06-02 17:28:12 +0000432 Instruction *BBI);
Philip Reames8efea572016-09-12 21:46:58 +0000433
434 void solve();
435
436 public:
Sanjay Patel61478e32015-01-09 16:47:20 +0000437 /// This is the query interface to determine the lattice
Chris Lattner2c5adf82009-11-15 19:59:49 +0000438 /// value for the specified Value* at the end of the specified block.
Florian Hahn5133f6f2017-09-28 11:09:22 +0000439 ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
440 Instruction *CxtI = nullptr);
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000441
Sanjay Patel61478e32015-01-09 16:47:20 +0000442 /// This is the query interface to determine the lattice
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000443 /// value for the specified Value* at the specified instruction (generally
444 /// from an assume intrinsic).
Florian Hahn5133f6f2017-09-28 11:09:22 +0000445 ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
Chris Lattner2c5adf82009-11-15 19:59:49 +0000446
Sanjay Patel61478e32015-01-09 16:47:20 +0000447 /// This is the query interface to determine the lattice
Chris Lattner2c5adf82009-11-15 19:59:49 +0000448 /// value for the specified Value* that is true on the specified edge.
Florian Hahn5133f6f2017-09-28 11:09:22 +0000449 ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
450 BasicBlock *ToBB,
451 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000452
Philip Reames35517182016-09-12 22:38:44 +0000453 /// Complete flush all previously computed values
454 void clear() {
455 TheCache.clear();
456 }
457
Anna Thomas46747f12017-06-06 19:25:31 +0000458 /// Printing the LazyValueInfo Analysis.
459 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
460 LazyValueInfoAnnotatedWriter Writer(this, DTree);
461 F.print(OS, &Writer);
Anna Thomas6cde8772017-03-22 19:27:12 +0000462 }
463
Philip Reames35517182016-09-12 22:38:44 +0000464 /// This is part of the update interface to inform the cache
465 /// that a block has been deleted.
466 void eraseBlock(BasicBlock *BB) {
467 TheCache.eraseBlock(BB);
468 }
469
Brian M. Rzycki55da8a32018-02-16 16:35:17 +0000470 /// Disables use of the DominatorTree within LVI.
471 void disableDT() {
472 if (DT) {
473 assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
474 std::swap(DT, DisabledDT);
475 }
476 }
477
478 /// Enables use of the DominatorTree within LVI. Does nothing if the class
479 /// instance was initialized without a DT pointer.
480 void enableDT() {
481 if (DisabledDT) {
482 assert(!DT && "Both DT and DisabledDT are not nullptr!");
483 std::swap(DT, DisabledDT);
484 }
485 }
486
Sanjay Patel61478e32015-01-09 16:47:20 +0000487 /// This is the update interface to inform the cache that an edge from
488 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersoncfa7fb62010-07-26 18:48:03 +0000489 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000490
Daniel Jasper8de3a542016-12-19 08:22:17 +0000491 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
492 DominatorTree *DT = nullptr)
Brian M. Rzycki55da8a32018-02-16 16:35:17 +0000493 : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
Chris Lattner2c5adf82009-11-15 19:59:49 +0000494 };
495} // end anonymous namespace
496
Anna Thomas46747f12017-06-06 19:25:31 +0000497
Philip Reames8efea572016-09-12 21:46:58 +0000498void LazyValueInfoImpl::solve() {
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000499 SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
500 BlockValueStack.begin(), BlockValueStack.end());
501
502 unsigned processedCount = 0;
Owen Andersone68713a2011-01-05 23:26:22 +0000503 while (!BlockValueStack.empty()) {
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000504 processedCount++;
505 // Abort if we have to process too many values to get a result for this one.
506 // Because of the design of the overdefined cache currently being per-block
507 // to avoid naming-related issues (IE it wants to try to give different
508 // results for the same name in different blocks), overdefined results don't
509 // get cached globally, which in turn means we will often try to rediscover
510 // the same overdefined result again and again. Once something like
511 // PredicateInfo is used in LVI or CVP, we should be able to make the
512 // overdefined cache global, and remove this throttle.
513 if (processedCount > MaxProcessedPerValue) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000514 LLVM_DEBUG(
515 dbgs() << "Giving up on stack because we are getting too deep\n");
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000516 // Fill in the original values
517 while (!StartingStack.empty()) {
518 std::pair<BasicBlock *, Value *> &e = StartingStack.back();
519 TheCache.insertResult(e.second, e.first,
Florian Hahn5133f6f2017-09-28 11:09:22 +0000520 ValueLatticeElement::getOverdefined());
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000521 StartingStack.pop_back();
522 }
523 BlockValueSet.clear();
524 BlockValueStack.clear();
525 return;
526 }
Vitaly Buka82afb7e2017-02-09 09:28:05 +0000527 std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000528 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
529
Nuno Lopese4413942012-06-28 01:16:18 +0000530 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000531 // The work item was completely processed.
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000532 assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
Philip Reames35517182016-09-12 22:38:44 +0000533 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka283d7eb2015-12-11 00:49:47 +0000534 "Result should be in cache!");
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000535
Nicola Zaghen0818e782018-05-14 12:53:11 +0000536 LLVM_DEBUG(
537 dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "
538 << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reamesbf09bb72016-02-02 03:15:40 +0000539
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000540 BlockValueStack.pop_back();
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000541 BlockValueSet.erase(e);
542 } else {
543 // More work needs to be done before revisiting.
Daniel Berlinf48edbb2017-02-08 15:22:52 +0000544 assert(BlockValueStack.back() != e && "Stack should have been pushed!");
Nuno Lopese4413942012-06-28 01:16:18 +0000545 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000546 }
547}
548
Philip Reames8efea572016-09-12 21:46:58 +0000549bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000550 // If already a constant, there is nothing to compute.
551 if (isa<Constant>(Val))
552 return true;
553
Philip Reames35517182016-09-12 22:38:44 +0000554 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000555}
556
Florian Hahn5133f6f2017-09-28 11:09:22 +0000557ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
558 BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000559 // If already a constant, there is nothing to compute.
560 if (Constant *VC = dyn_cast<Constant>(Val))
Florian Hahn5133f6f2017-09-28 11:09:22 +0000561 return ValueLatticeElement::get(VC);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000562
Philip Reames35517182016-09-12 22:38:44 +0000563 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000564}
565
Florian Hahn5133f6f2017-09-28 11:09:22 +0000566static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
Philip Reames45ef74f2015-10-29 03:57:17 +0000567 switch (BBI->getOpcode()) {
568 default: break;
569 case Instruction::Load:
570 case Instruction::Call:
571 case Instruction::Invoke:
NAKAMURA Takumib78624d2016-07-25 00:59:46 +0000572 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reamesebc19462015-10-29 04:21:49 +0000573 if (isa<IntegerType>(BBI->getType())) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000574 return ValueLatticeElement::getRange(
575 getConstantRangeFromMetadata(*Ranges));
Philip Reames45ef74f2015-10-29 03:57:17 +0000576 }
577 break;
578 };
Philip Reamesdc27a092016-02-02 21:57:37 +0000579 // Nothing known - will be intersected with other facts
Florian Hahn5133f6f2017-09-28 11:09:22 +0000580 return ValueLatticeElement::getOverdefined();
Philip Reames45ef74f2015-10-29 03:57:17 +0000581}
582
Philip Reames8efea572016-09-12 21:46:58 +0000583bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky90862ee2010-12-18 01:00:40 +0000584 if (isa<Constant>(Val))
585 return true;
586
Philip Reames35517182016-09-12 22:38:44 +0000587 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000588 // If we have a cached value, use that.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000589 LLVM_DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val="
590 << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky90862ee2010-12-18 01:00:40 +0000591
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000592 // Since we're reusing a cached value, we don't need to update the
593 // OverDefinedCache. The cache will have been properly updated whenever the
594 // cached value was inserted.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000595 return true;
Chris Lattnere5642812009-11-15 20:00:52 +0000596 }
597
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000598 // Hold off inserting this value into the Cache in case we have to return
599 // false and come back later.
Florian Hahn5133f6f2017-09-28 11:09:22 +0000600 ValueLatticeElement Res;
Philip Reames19937b02016-12-06 03:22:03 +0000601 if (!solveBlockValueImpl(Res, Val, BB))
602 // Work pushed, will revisit
603 return false;
604
605 TheCache.insertResult(Val, BB, Res);
606 return true;
607}
608
Florian Hahn5133f6f2017-09-28 11:09:22 +0000609bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
Philip Reames19937b02016-12-06 03:22:03 +0000610 Value *Val, BasicBlock *BB) {
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000611
Chris Lattner2c5adf82009-11-15 19:59:49 +0000612 Instruction *BBI = dyn_cast<Instruction>(Val);
Philip Reames19937b02016-12-06 03:22:03 +0000613 if (!BBI || BBI->getParent() != BB)
614 return solveBlockValueNonLocal(Res, Val, BB);
Chris Lattnere5642812009-11-15 20:00:52 +0000615
Philip Reames19937b02016-12-06 03:22:03 +0000616 if (PHINode *PN = dyn_cast<PHINode>(BBI))
617 return solveBlockValuePHINode(Res, PN, BB);
Owen Andersonb81fd622010-08-18 21:11:37 +0000618
Philip Reames19937b02016-12-06 03:22:03 +0000619 if (auto *SI = dyn_cast<SelectInst>(BBI))
620 return solveBlockValueSelect(Res, SI, BB);
Philip Reames2b06e732016-02-01 22:57:53 +0000621
Philip Reames6d1c8552016-04-27 01:02:25 +0000622 // If this value is a nonnull pointer, record it's range and bailout. Note
623 // that for all other pointer typed values, we terminate the search at the
624 // definition. We could easily extend this to look through geps, bitcasts,
625 // and the like to prove non-nullness, but it's not clear that's worth it
Craig Topper24ed17e2017-06-09 21:21:17 +0000626 // compile time wise. The context-insensitive value walk done inside
Nuno Lopesfe353a02017-09-09 18:23:11 +0000627 // isKnownNonZero gets most of the profitable cases at much less expense.
Philip Reames6d1c8552016-04-27 01:02:25 +0000628 // This does mean that we have a sensativity to where the defining
629 // instruction is placed, even if it could legally be hoisted much higher.
630 // That is unfortunate.
Igor Laevskyef40e272015-09-18 13:01:48 +0000631 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
Nuno Lopesfe353a02017-09-09 18:23:11 +0000632 if (PT && isKnownNonZero(BBI, DL)) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000633 Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000634 return true;
Nick Lewycky786c7cd2011-01-15 09:16:12 +0000635 }
Davide Italiano088a7732016-05-25 22:29:34 +0000636 if (BBI->getType()->isIntegerTy()) {
Craig Topper09226ec2017-06-03 07:47:08 +0000637 if (auto *CI = dyn_cast<CastInst>(BBI))
638 return solveBlockValueCast(Res, CI, BB);
639
John Regehr21e89f92018-11-21 05:24:12 +0000640 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))
Craig Topperfd6a4a52017-06-02 16:33:13 +0000641 return solveBlockValueBinaryOp(Res, BO, BB);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000642 }
Owen Andersonb81fd622010-08-18 21:11:37 +0000643
Nicola Zaghen0818e782018-05-14 12:53:11 +0000644 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
645 << "' - unknown inst def found.\n");
Philip Reames1758dd62016-03-04 22:27:39 +0000646 Res = getFromRangeMetadata(BBI);
Hans Wennborg4d48c3f12014-11-25 17:23:05 +0000647 return true;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000648}
649
650static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
651 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
652 return L->getPointerAddressSpace() == 0 &&
Mehdi Amini529919f2015-03-10 02:37:25 +0000653 GetUnderlyingObject(L->getPointerOperand(),
654 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000655 }
656 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
657 return S->getPointerAddressSpace() == 0 &&
Mehdi Amini529919f2015-03-10 02:37:25 +0000658 GetUnderlyingObject(S->getPointerOperand(),
659 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000660 }
Nick Lewycky786c7cd2011-01-15 09:16:12 +0000661 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
662 if (MI->isVolatile()) return false;
Nick Lewycky786c7cd2011-01-15 09:16:12 +0000663
664 // FIXME: check whether it has a valuerange that excludes zero?
665 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
666 if (!Len || Len->isZero()) return false;
667
Eli Friedman69388e52011-05-31 20:40:16 +0000668 if (MI->getDestAddressSpace() == 0)
Mehdi Amini529919f2015-03-10 02:37:25 +0000669 if (GetUnderlyingObject(MI->getRawDest(),
670 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman69388e52011-05-31 20:40:16 +0000671 return true;
Nick Lewycky786c7cd2011-01-15 09:16:12 +0000672 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman69388e52011-05-31 20:40:16 +0000673 if (MTI->getSourceAddressSpace() == 0)
Mehdi Amini529919f2015-03-10 02:37:25 +0000674 if (GetUnderlyingObject(MTI->getRawSource(),
675 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman69388e52011-05-31 20:40:16 +0000676 return true;
Nick Lewycky786c7cd2011-01-15 09:16:12 +0000677 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000678 return false;
679}
680
Philip Reames82d78c22016-04-27 00:30:55 +0000681/// Return true if the allocation associated with Val is ever dereferenced
682/// within the given basic block. This establishes the fact Val is not null,
683/// but does not imply that the memory at Val is dereferenceable. (Val may
684/// point off the end of the dereferenceable part of the object.)
685static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
686 assert(Val->getType()->isPointerTy());
687
688 const DataLayout &DL = BB->getModule()->getDataLayout();
689 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
690 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
691 // inside InstructionDereferencesPointer either.
692 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
693 for (Instruction &I : *BB)
694 if (InstructionDereferencesPointer(&I, UnderlyingVal))
695 return true;
696 return false;
697}
698
Florian Hahn5133f6f2017-09-28 11:09:22 +0000699bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
Owen Anderson61863942010-12-20 18:18:16 +0000700 Value *Val, BasicBlock *BB) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000701 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000702
Nick Lewycky90862ee2010-12-18 01:00:40 +0000703 // If this is the entry block, we must be asking about an argument. The
704 // value is overdefined.
705 if (BB == &BB->getParent()->getEntryBlock()) {
706 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Craig Topperf7434472017-04-28 16:57:59 +0000707 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames82d78c22016-04-27 00:30:55 +0000708 // this particular block.
Manoj Guptac6da6862018-07-09 22:27:23 +0000709 PointerType *PTy = dyn_cast<PointerType>(Val->getType());
710 if (PTy &&
711 (isKnownNonZero(Val, DL) ||
712 (isObjectDereferencedInBlock(Val, BB) &&
713 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000714 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000715 } else {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000716 Result = ValueLatticeElement::getOverdefined();
Nick Lewycky90862ee2010-12-18 01:00:40 +0000717 }
Owen Anderson61863942010-12-20 18:18:16 +0000718 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000719 return true;
720 }
721
722 // Loop over all of our predecessors, merging what we know from them into
Philip Reames85df5172017-02-07 00:25:24 +0000723 // result. If we encounter an unexplored predecessor, we eagerly explore it
724 // in a depth first manner. In practice, this has the effect of discovering
725 // paths we can't analyze eagerly without spending compile times analyzing
726 // other paths. This heuristic benefits from the fact that predecessors are
727 // frequently arranged such that dominating ones come first and we quickly
728 // find a path to function entry. TODO: We should consider explicitly
729 // canonicalizing to make this true rather than relying on this happy
Fangrui Songaf7b1832018-07-30 19:41:25 +0000730 // accident.
Duncan P. N. Exon Smithfacdfc62014-07-21 17:06:51 +0000731 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000732 ValueLatticeElement EdgeResult;
Philip Reames85df5172017-02-07 00:25:24 +0000733 if (!getEdgeValue(Val, *PI, BB, EdgeResult))
734 // Explore that input, then return here
735 return false;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000736
Mehdi Amini529919f2015-03-10 02:37:25 +0000737 Result.mergeIn(EdgeResult, DL);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000738
739 // If we hit overdefined, exit early. The BlockVals entry is already set
740 // to overdefined.
741 if (Result.isOverdefined()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000742 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
743 << "' - overdefined because of pred (non local).\n");
Artur Pilipenko3d7517b2016-08-09 09:14:29 +0000744 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames82d78c22016-04-27 00:30:55 +0000745 // this particular block.
Manoj Guptac6da6862018-07-09 22:27:23 +0000746 PointerType *PTy = dyn_cast<PointerType>(Val->getType());
747 if (PTy && isObjectDereferencedInBlock(Val, BB) &&
748 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000749 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky90862ee2010-12-18 01:00:40 +0000750 }
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000751
Owen Anderson61863942010-12-20 18:18:16 +0000752 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000753 return true;
754 }
755 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000756
757 // Return the merged value, which is more precise than 'overdefined'.
758 assert(!Result.isOverdefined());
Owen Anderson61863942010-12-20 18:18:16 +0000759 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000760 return true;
761}
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000762
Florian Hahn5133f6f2017-09-28 11:09:22 +0000763bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
764 PHINode *PN, BasicBlock *BB) {
765 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000766
767 // Loop over all of our predecessors, merging what we know from them into
Philip Reames85df5172017-02-07 00:25:24 +0000768 // result. See the comment about the chosen traversal order in
769 // solveBlockValueNonLocal; the same reasoning applies here.
Nick Lewycky90862ee2010-12-18 01:00:40 +0000770 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
771 BasicBlock *PhiBB = PN->getIncomingBlock(i);
772 Value *PhiVal = PN->getIncomingValue(i);
Florian Hahn5133f6f2017-09-28 11:09:22 +0000773 ValueLatticeElement EdgeResult;
Hal Finkel61c65b22014-10-16 00:40:05 +0000774 // Note that we can provide PN as the context value to getEdgeValue, even
775 // though the results will be cached, because PN is the value being used as
776 // the cache key in the caller.
Philip Reames85df5172017-02-07 00:25:24 +0000777 if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
778 // Explore that input, then return here
779 return false;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000780
Mehdi Amini529919f2015-03-10 02:37:25 +0000781 Result.mergeIn(EdgeResult, DL);
Nick Lewycky90862ee2010-12-18 01:00:40 +0000782
783 // If we hit overdefined, exit early. The BlockVals entry is already set
784 // to overdefined.
785 if (Result.isOverdefined()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000786 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
787 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +0000788
Owen Anderson61863942010-12-20 18:18:16 +0000789 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000790 return true;
791 }
792 }
Nick Lewycky90862ee2010-12-18 01:00:40 +0000793
794 // Return the merged value, which is more precise than 'overdefined'.
795 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson61863942010-12-20 18:18:16 +0000796 BBLV = Result;
Nick Lewycky90862ee2010-12-18 01:00:40 +0000797 return true;
798}
799
Florian Hahn5133f6f2017-09-28 11:09:22 +0000800static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
801 bool isTrueDest = true);
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000802
Philip Reamesdc27a092016-02-02 21:57:37 +0000803// If we can determine a constraint on the value given conditions assumed by
804// the program, intersect those constraints with BBLV
Philip Reames8efea572016-09-12 21:46:58 +0000805void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Florian Hahn5133f6f2017-09-28 11:09:22 +0000806 Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000807 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
808 if (!BBI)
809 return;
810
Hal Finkela6e44bd2017-01-11 13:24:24 +0000811 for (auto &AssumeVH : AC->assumptionsFor(Val)) {
Daniel Jasper8de3a542016-12-19 08:22:17 +0000812 if (!AssumeVH)
Chandler Carruth5a9cd4d2015-01-04 12:03:27 +0000813 continue;
Daniel Jasper8de3a542016-12-19 08:22:17 +0000814 auto *I = cast<CallInst>(AssumeVH);
815 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000816 continue;
817
Daniel Jasper8de3a542016-12-19 08:22:17 +0000818 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000819 }
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +0000820
821 // If guards are not used in the module, don't spend time looking for them
822 auto *GuardDecl = BBI->getModule()->getFunction(
823 Intrinsic::getName(Intrinsic::experimental_guard));
824 if (!GuardDecl || GuardDecl->use_empty())
825 return;
826
Artur Pilipenkod193fb52016-10-21 15:02:21 +0000827 for (Instruction &I : make_range(BBI->getIterator().getReverse(),
828 BBI->getParent()->rend())) {
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +0000829 Value *Cond = nullptr;
Artur Pilipenkod193fb52016-10-21 15:02:21 +0000830 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
831 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +0000832 }
Hal Finkel3ef1aae2014-09-07 20:29:59 +0000833}
834
Florian Hahn5133f6f2017-09-28 11:09:22 +0000835bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
836 SelectInst *SI, BasicBlock *BB) {
Philip Reames2b06e732016-02-01 22:57:53 +0000837
838 // Recurse on our inputs if needed
839 if (!hasBlockValue(SI->getTrueValue(), BB)) {
840 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
841 return false;
Florian Hahn5133f6f2017-09-28 11:09:22 +0000842 BBLV = ValueLatticeElement::getOverdefined();
Philip Reames2b06e732016-02-01 22:57:53 +0000843 return true;
844 }
Florian Hahn5133f6f2017-09-28 11:09:22 +0000845 ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
Philip Reames2b06e732016-02-01 22:57:53 +0000846 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
847 // extra slots in the table if we can.
848 if (TrueVal.isOverdefined()) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000849 BBLV = ValueLatticeElement::getOverdefined();
Philip Reames2b06e732016-02-01 22:57:53 +0000850 return true;
851 }
852
853 if (!hasBlockValue(SI->getFalseValue(), BB)) {
854 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
855 return false;
Florian Hahn5133f6f2017-09-28 11:09:22 +0000856 BBLV = ValueLatticeElement::getOverdefined();
Philip Reames2b06e732016-02-01 22:57:53 +0000857 return true;
858 }
Florian Hahn5133f6f2017-09-28 11:09:22 +0000859 ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
Philip Reames2b06e732016-02-01 22:57:53 +0000860 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
861 // extra slots in the table if we can.
862 if (FalseVal.isOverdefined()) {
Florian Hahn5133f6f2017-09-28 11:09:22 +0000863 BBLV = ValueLatticeElement::getOverdefined();
Philip Reames2b06e732016-02-01 22:57:53 +0000864 return true;
865 }
866
Philip Reamesedac10e2016-02-26 22:53:59 +0000867 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
Craig Topper583a2512017-05-06 03:35:15 +0000868 const ConstantRange &TrueCR = TrueVal.getConstantRange();
869 const ConstantRange &FalseCR = FalseVal.getConstantRange();
Philip Reamesedac10e2016-02-26 22:53:59 +0000870 Value *LHS = nullptr;
871 Value *RHS = nullptr;
872 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
873 // Is this a min specifically of our two inputs? (Avoid the risk of
874 // ValueTracking getting smarter looking back past our immediate inputs.)
875 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
876 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reames36c7a942016-12-06 02:54:16 +0000877 ConstantRange ResultCR = [&]() {
878 switch (SPR.Flavor) {
879 default:
880 llvm_unreachable("unexpected minmax type!");
881 case SPF_SMIN: /// Signed minimum
882 return TrueCR.smin(FalseCR);
883 case SPF_UMIN: /// Unsigned minimum
884 return TrueCR.umin(FalseCR);
885 case SPF_SMAX: /// Signed maximum
886 return TrueCR.smax(FalseCR);
887 case SPF_UMAX: /// Unsigned maximum
888 return TrueCR.umax(FalseCR);
889 };
890 }();
Florian Hahn5133f6f2017-09-28 11:09:22 +0000891 BBLV = ValueLatticeElement::getRange(ResultCR);
Philip Reames36c7a942016-12-06 02:54:16 +0000892 return true;
Philip Reamesedac10e2016-02-26 22:53:59 +0000893 }
NAKAMURA Takumi283ee812016-07-04 01:26:33 +0000894
Philip Reamesedac10e2016-02-26 22:53:59 +0000895 // TODO: ABS, NABS from the SelectPatternResult
896 }
897
Philip Reames29f3f952016-02-12 00:09:18 +0000898 // Can we constrain the facts about the true and false values by using the
899 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
900 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenkoe2e5c072016-08-02 16:20:48 +0000901 Value *Cond = SI->getCondition();
Artur Pilipenko5e9462a2016-08-10 13:38:07 +0000902 TrueVal = intersect(TrueVal,
903 getValueFromCondition(SI->getTrueValue(), Cond, true));
904 FalseVal = intersect(FalseVal,
905 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames29f3f952016-02-12 00:09:18 +0000906
Artur Pilipenkoe2e5c072016-08-02 16:20:48 +0000907 // Handle clamp idioms such as:
908 // %24 = constantrange<0, 17>
909 // %39 = icmp eq i32 %24, 0
910 // %40 = add i32 %24, -1
911 // %siv.next = select i1 %39, i32 16, i32 %40
912 // %siv.next = constantrange<0, 17> not <-1, 17>
913 // In general, this can handle any clamp idiom which tests the edge
914 // condition via an equality or inequality.
915 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesedac10e2016-02-26 22:53:59 +0000916 ICmpInst::Predicate Pred = ICI->getPredicate();
917 Value *A = ICI->getOperand(0);
918 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
919 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
920 assert(A->getType() == B->getType());
921 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
922 };
923 // See if either input is A + C2, subject to the constraint from the
924 // condition that A != C when that input is used. We can assume that
925 // that input doesn't include C + C2.
926 ConstantInt *CIAdded;
927 switch (Pred) {
Philip Reames81737172016-02-27 05:18:30 +0000928 default: break;
Philip Reamesedac10e2016-02-26 22:53:59 +0000929 case ICmpInst::ICMP_EQ:
930 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
931 m_ConstantInt(CIAdded)))) {
932 auto ResNot = addConstants(CIBase, CIAdded);
933 FalseVal = intersect(FalseVal,
Florian Hahn5133f6f2017-09-28 11:09:22 +0000934 ValueLatticeElement::getNot(ResNot));
Philip Reamesedac10e2016-02-26 22:53:59 +0000935 }
936 break;
937 case ICmpInst::ICMP_NE:
938 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
939 m_ConstantInt(CIAdded)))) {
940 auto ResNot = addConstants(CIBase, CIAdded);
941 TrueVal = intersect(TrueVal,
Florian Hahn5133f6f2017-09-28 11:09:22 +0000942 ValueLatticeElement::getNot(ResNot));
Philip Reamesedac10e2016-02-26 22:53:59 +0000943 }
944 break;
945 };
946 }
947 }
NAKAMURA Takumi283ee812016-07-04 01:26:33 +0000948
Florian Hahn5133f6f2017-09-28 11:09:22 +0000949 ValueLatticeElement Result; // Start Undefined.
Philip Reames2b06e732016-02-01 22:57:53 +0000950 Result.mergeIn(TrueVal, DL);
951 Result.mergeIn(FalseVal, DL);
Philip Reames2b06e732016-02-01 22:57:53 +0000952 BBLV = Result;
953 return true;
954}
955
John Regehr21e89f92018-11-21 05:24:12 +0000956Optional<ConstantRange> LazyValueInfoImpl::getRangeForOperand(unsigned Op,
957 Instruction *I,
958 BasicBlock *BB) {
959 if (!hasBlockValue(I->getOperand(Op), BB))
960 if (pushBlockValue(std::make_pair(BB, I->getOperand(Op))))
961 return None;
962
963 const unsigned OperandBitWidth =
964 DL.getTypeSizeInBits(I->getOperand(Op)->getType());
965 ConstantRange Range = ConstantRange(OperandBitWidth);
966 if (hasBlockValue(I->getOperand(Op), BB)) {
967 ValueLatticeElement Val = getBlockValue(I->getOperand(Op), BB);
968 intersectAssumeOrGuardBlockValueConstantRange(I->getOperand(Op), Val, I);
969 if (Val.isConstantRange())
970 Range = Val.getConstantRange();
971 }
972 return Range;
973}
974
Florian Hahn5133f6f2017-09-28 11:09:22 +0000975bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
Craig Topper09226ec2017-06-03 07:47:08 +0000976 CastInst *CI,
977 BasicBlock *BB) {
978 if (!CI->getOperand(0)->getType()->isSized()) {
Philip Reames171bf702016-04-26 22:52:30 +0000979 // Without knowing how wide the input is, we can't analyze it in any useful
980 // way.
Florian Hahn5133f6f2017-09-28 11:09:22 +0000981 BBLV = ValueLatticeElement::getOverdefined();
Philip Reames171bf702016-04-26 22:52:30 +0000982 return true;
983 }
Philip Reamesaa06cc12016-04-26 23:27:33 +0000984
985 // Filter out casts we don't know how to reason about before attempting to
986 // recurse on our operand. This can cut a long search short if we know we're
987 // not going to be able to get any useful information anways.
Craig Topper09226ec2017-06-03 07:47:08 +0000988 switch (CI->getOpcode()) {
Philip Reamesaa06cc12016-04-26 23:27:33 +0000989 case Instruction::Trunc:
990 case Instruction::SExt:
991 case Instruction::ZExt:
992 case Instruction::BitCast:
993 break;
994 default:
995 // Unhandled instructions are overdefined.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000996 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
997 << "' - overdefined (unknown cast).\n");
Florian Hahn5133f6f2017-09-28 11:09:22 +0000998 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesaa06cc12016-04-26 23:27:33 +0000999 return true;
1000 }
1001
Philip Reames7f3fd5d2016-04-26 21:48:16 +00001002 // Figure out the range of the LHS. If that fails, we still apply the
1003 // transfer rule on the full set since we may be able to locally infer
1004 // interesting facts.
John Regehr21e89f92018-11-21 05:24:12 +00001005 Optional<ConstantRange> LHSRes = getRangeForOperand(0, CI, BB);
1006 if (!LHSRes.hasValue())
1007 // More work to do before applying this transfer rule.
1008 return false;
1009 ConstantRange LHSRange = LHSRes.getValue();
Nick Lewycky90862ee2010-12-18 01:00:40 +00001010
Craig Topper079f8402017-06-03 07:47:14 +00001011 const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
Philip Reames5fad6b42016-04-25 18:30:31 +00001012
1013 // NOTE: We're currently limited by the set of operations that ConstantRange
1014 // can evaluate symbolically. Enhancing that set will allows us to analyze
1015 // more definitions.
Florian Hahn5133f6f2017-09-28 11:09:22 +00001016 BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
1017 ResultBitWidth));
Philip Reames5fad6b42016-04-25 18:30:31 +00001018 return true;
1019}
1020
Florian Hahn5133f6f2017-09-28 11:09:22 +00001021bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
1022 BinaryOperator *BO,
1023 BasicBlock *BB) {
Philip Reames5fad6b42016-04-25 18:30:31 +00001024
Craig Topperfd6a4a52017-06-02 16:33:13 +00001025 assert(BO->getOperand(0)->getType()->isSized() &&
Philip Reames5fcdfef2016-04-26 23:10:35 +00001026 "all operands to binary operators are sized");
Philip Reamesaa06cc12016-04-26 23:27:33 +00001027
1028 // Filter out operators we don't know how to reason about before attempting to
1029 // recurse on our operand(s). This can cut a long search short if we know
Craig Topper1c790ae2017-06-02 16:21:13 +00001030 // we're not going to be able to get any useful information anyways.
Craig Topperfd6a4a52017-06-02 16:33:13 +00001031 switch (BO->getOpcode()) {
Philip Reamesaa06cc12016-04-26 23:27:33 +00001032 case Instruction::Add:
1033 case Instruction::Sub:
1034 case Instruction::Mul:
1035 case Instruction::UDiv:
1036 case Instruction::Shl:
1037 case Instruction::LShr:
Max Kazantsev7a7c05d2017-12-18 14:23:30 +00001038 case Instruction::AShr:
Philip Reamesaa06cc12016-04-26 23:27:33 +00001039 case Instruction::And:
1040 case Instruction::Or:
1041 // continue into the code below
1042 break;
1043 default:
1044 // Unhandled instructions are overdefined.
Nicola Zaghen0818e782018-05-14 12:53:11 +00001045 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1046 << "' - overdefined (unknown binary operator).\n");
Florian Hahn5133f6f2017-09-28 11:09:22 +00001047 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesaa06cc12016-04-26 23:27:33 +00001048 return true;
1049 };
NAKAMURA Takumi283ee812016-07-04 01:26:33 +00001050
John Regehr21e89f92018-11-21 05:24:12 +00001051 // Figure out the ranges of the operands. If that fails, use a
1052 // conservative range, but apply the transfer rule anyways. This
1053 // lets us pick up facts from expressions like "and i32 (call i32
1054 // @foo()), 32"
1055 Optional<ConstantRange> LHSRes = getRangeForOperand(0, BO, BB);
1056 Optional<ConstantRange> RHSRes = getRangeForOperand(1, BO, BB);
Philip Reames5fcdfef2016-04-26 23:10:35 +00001057
John Regehr21e89f92018-11-21 05:24:12 +00001058 if (!LHSRes.hasValue() || !RHSRes.hasValue())
1059 // More work to do before applying this transfer rule.
1060 return false;
Philip Reames5fad6b42016-04-25 18:30:31 +00001061
John Regehr21e89f92018-11-21 05:24:12 +00001062 ConstantRange LHSRange = LHSRes.getValue();
1063 ConstantRange RHSRange = RHSRes.getValue();
Philip Reames5fad6b42016-04-25 18:30:31 +00001064
Owen Andersonb81fd622010-08-18 21:11:37 +00001065 // NOTE: We're currently limited by the set of operations that ConstantRange
1066 // can evaluate symbolically. Enhancing that set will allows us to analyze
1067 // more definitions.
Craig Topperfd6a4a52017-06-02 16:33:13 +00001068 Instruction::BinaryOps BinOp = BO->getOpcode();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001069 BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky90862ee2010-12-18 01:00:40 +00001070 return true;
Chris Lattner10f2d132009-11-11 00:22:30 +00001071}
1072
Florian Hahn5133f6f2017-09-28 11:09:22 +00001073static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1074 bool isTrueDest) {
Artur Pilipenkoc14eb9a2016-08-08 14:08:37 +00001075 Value *LHS = ICI->getOperand(0);
1076 Value *RHS = ICI->getOperand(1);
1077 CmpInst::Predicate Predicate = ICI->getPredicate();
1078
1079 if (isa<Constant>(RHS)) {
1080 if (ICI->isEquality() && LHS == Val) {
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001081 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumi6f439632016-07-04 01:26:27 +00001082 // false SETNE.
Artur Pilipenkoc14eb9a2016-08-08 14:08:37 +00001083 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Florian Hahn5133f6f2017-09-28 11:09:22 +00001084 return ValueLatticeElement::get(cast<Constant>(RHS));
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001085 else
Florian Hahn5133f6f2017-09-28 11:09:22 +00001086 return ValueLatticeElement::getNot(cast<Constant>(RHS));
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001087 }
Artur Pilipenko861388b2016-08-09 14:50:08 +00001088 }
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001089
Artur Pilipenko861388b2016-08-09 14:50:08 +00001090 if (!Val->getType()->isIntegerTy())
Florian Hahn5133f6f2017-09-28 11:09:22 +00001091 return ValueLatticeElement::getOverdefined();
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001092
Artur Pilipenko861388b2016-08-09 14:50:08 +00001093 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1094 // range of Val guaranteed by the condition. Recognize comparisons in the from
1095 // of:
1096 // icmp <pred> Val, ...
Artur Pilipenkoa6da8aa2016-08-12 10:05:11 +00001097 // icmp <pred> (add Val, Offset), ...
Artur Pilipenko861388b2016-08-09 14:50:08 +00001098 // The latter is the range checking idiom that InstCombine produces. Subtract
1099 // the offset from the allowed range for RHS in this case.
Artur Pilipenkofe11e982016-08-08 14:33:11 +00001100
Artur Pilipenko861388b2016-08-09 14:50:08 +00001101 // Val or (add Val, Offset) can be on either hand of the comparison
1102 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1103 std::swap(LHS, RHS);
1104 Predicate = CmpInst::getSwappedPredicate(Predicate);
1105 }
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001106
Artur Pilipenko861388b2016-08-09 14:50:08 +00001107 ConstantInt *Offset = nullptr;
Artur Pilipenkoa6da8aa2016-08-12 10:05:11 +00001108 if (LHS != Val)
Artur Pilipenko861388b2016-08-09 14:50:08 +00001109 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001110
Artur Pilipenko861388b2016-08-09 14:50:08 +00001111 if (LHS == Val || Offset) {
1112 // Calculate the range of values that are allowed by the comparison
1113 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1114 /*isFullSet=*/true);
1115 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1116 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko38a5bdc2016-08-12 10:14:11 +00001117 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1118 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1119 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenko861388b2016-08-09 14:50:08 +00001120
1121 // If we're interested in the false dest, invert the condition
1122 CmpInst::Predicate Pred =
1123 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1124 ConstantRange TrueValues =
1125 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1126
1127 if (Offset) // Apply the offset from above.
1128 TrueValues = TrueValues.subtract(Offset->getValue());
1129
Florian Hahn5133f6f2017-09-28 11:09:22 +00001130 return ValueLatticeElement::getRange(std::move(TrueValues));
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001131 }
NAKAMURA Takumi283ee812016-07-04 01:26:33 +00001132
Florian Hahn5133f6f2017-09-28 11:09:22 +00001133 return ValueLatticeElement::getOverdefined();
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001134}
1135
Florian Hahn5133f6f2017-09-28 11:09:22 +00001136static ValueLatticeElement
Artur Pilipenko72349b22016-08-10 15:13:15 +00001137getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn5133f6f2017-09-28 11:09:22 +00001138 DenseMap<Value*, ValueLatticeElement> &Visited);
Artur Pilipenko72349b22016-08-10 15:13:15 +00001139
Florian Hahn5133f6f2017-09-28 11:09:22 +00001140static ValueLatticeElement
Artur Pilipenko72349b22016-08-10 15:13:15 +00001141getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn5133f6f2017-09-28 11:09:22 +00001142 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenko72349b22016-08-10 15:13:15 +00001143 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1144 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1145
1146 // Handle conditions in the form of (cond1 && cond2), we know that on the
Craig Topper9c13e872017-06-23 01:08:16 +00001147 // true dest path both of the conditions hold. Similarly for conditions of
1148 // the form (cond1 || cond2), we know that on the false dest path neither
1149 // condition holds.
Artur Pilipenko72349b22016-08-10 15:13:15 +00001150 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
Craig Topper9c13e872017-06-23 01:08:16 +00001151 if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
1152 (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
Florian Hahn5133f6f2017-09-28 11:09:22 +00001153 return ValueLatticeElement::getOverdefined();
Artur Pilipenko72349b22016-08-10 15:13:15 +00001154
Brian M. Rzyckib2e91342018-03-13 18:14:10 +00001155 // Prevent infinite recursion if Cond references itself as in this example:
1156 // Cond: "%tmp4 = and i1 %tmp4, undef"
1157 // BL: "%tmp4 = and i1 %tmp4, undef"
1158 // BR: "i1 undef"
1159 Value *BL = BO->getOperand(0);
1160 Value *BR = BO->getOperand(1);
1161 if (BL == Cond || BR == Cond)
1162 return ValueLatticeElement::getOverdefined();
1163
1164 return intersect(getValueFromCondition(Val, BL, isTrueDest, Visited),
1165 getValueFromCondition(Val, BR, isTrueDest, Visited));
Artur Pilipenko72349b22016-08-10 15:13:15 +00001166}
1167
Florian Hahn5133f6f2017-09-28 11:09:22 +00001168static ValueLatticeElement
Artur Pilipenko72349b22016-08-10 15:13:15 +00001169getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn5133f6f2017-09-28 11:09:22 +00001170 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenko72349b22016-08-10 15:13:15 +00001171 auto I = Visited.find(Cond);
1172 if (I != Visited.end())
1173 return I->second;
Artur Pilipenkoe18f64c2016-08-12 15:08:15 +00001174
1175 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1176 Visited[Cond] = Result;
1177 return Result;
Artur Pilipenko72349b22016-08-10 15:13:15 +00001178}
1179
Florian Hahn5133f6f2017-09-28 11:09:22 +00001180ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
1181 bool isTrueDest) {
Artur Pilipenko72349b22016-08-10 15:13:15 +00001182 assert(Cond && "precondition");
Florian Hahn5133f6f2017-09-28 11:09:22 +00001183 DenseMap<Value*, ValueLatticeElement> Visited;
Artur Pilipenko72349b22016-08-10 15:13:15 +00001184 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1185}
1186
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001187// Return true if Usr has Op as an operand, otherwise false.
1188static bool usesOperand(User *Usr, Value *Op) {
1189 return find(Usr->operands(), Op) != Usr->op_end();
1190}
1191
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001192// Return true if the instruction type of Val is supported by
1193// constantFoldUser(). Currently CastInst and BinaryOperator only. Call this
1194// before calling constantFoldUser() to find out if it's even worth attempting
1195// to call it.
1196static bool isOperationFoldable(User *Usr) {
1197 return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
1198}
1199
1200// Check if Usr can be simplified to an integer constant when the value of one
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001201// of its operands Op is an integer constant OpConstVal. If so, return it as an
1202// lattice value range with a single element or otherwise return an overdefined
1203// lattice value.
Florian Hahn5133f6f2017-09-28 11:09:22 +00001204static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
1205 const APInt &OpConstVal,
1206 const DataLayout &DL) {
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001207 assert(isOperationFoldable(Usr) && "Precondition");
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001208 Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001209 // Check if Usr can be simplified to a constant.
1210 if (auto *CI = dyn_cast<CastInst>(Usr)) {
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001211 assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
1212 if (auto *C = dyn_cast_or_null<ConstantInt>(
1213 SimplifyCastInst(CI->getOpcode(), OpConst,
1214 CI->getDestTy(), DL))) {
Florian Hahn5133f6f2017-09-28 11:09:22 +00001215 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001216 }
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001217 } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001218 bool Op0Match = BO->getOperand(0) == Op;
1219 bool Op1Match = BO->getOperand(1) == Op;
1220 assert((Op0Match || Op1Match) &&
1221 "Operand 0 nor Operand 1 isn't a match");
1222 Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
1223 Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
1224 if (auto *C = dyn_cast_or_null<ConstantInt>(
1225 SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
Florian Hahn5133f6f2017-09-28 11:09:22 +00001226 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001227 }
1228 }
Florian Hahn5133f6f2017-09-28 11:09:22 +00001229 return ValueLatticeElement::getOverdefined();
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001230}
1231
Adrian Prantl26b584c2018-05-01 15:54:18 +00001232/// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames9d1fe7a2016-02-01 23:21:11 +00001233/// Val is not constrained on the edge. Result is unspecified if return value
1234/// is false.
Nuno Lopese4413942012-06-28 01:16:18 +00001235static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
Florian Hahn5133f6f2017-09-28 11:09:22 +00001236 BasicBlock *BBTo, ValueLatticeElement &Result) {
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001237 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner800c47e2009-11-15 20:02:12 +00001238 // know that v != 0.
Chris Lattner16976522009-11-11 22:48:44 +00001239 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1240 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patela9877422015-01-09 16:35:37 +00001241 // we may be able to infer something from the condition.
Chris Lattner16976522009-11-11 22:48:44 +00001242 if (BI->isConditional() &&
1243 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1244 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1245 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1246 "BBTo isn't a successor of BBFrom");
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001247 Value *Condition = BI->getCondition();
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001248
Chris Lattner16976522009-11-11 22:48:44 +00001249 // If V is the condition of the branch itself, then we know exactly what
1250 // it is.
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001251 if (Condition == Val) {
Florian Hahn5133f6f2017-09-28 11:09:22 +00001252 Result = ValueLatticeElement::get(ConstantInt::get(
Owen Anderson9f014062010-08-10 20:03:09 +00001253 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky90862ee2010-12-18 01:00:40 +00001254 return true;
1255 }
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001256
Chris Lattner16976522009-11-11 22:48:44 +00001257 // If the condition of the branch is an equality comparison, we may be
1258 // able to infer the value.
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001259 Result = getValueFromCondition(Val, Condition, isTrueDest);
1260 if (!Result.isOverdefined())
1261 return true;
1262
1263 if (User *Usr = dyn_cast<User>(Val)) {
1264 assert(Result.isOverdefined() && "Result isn't overdefined");
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001265 // Check with isOperationFoldable() first to avoid linearly iterating
1266 // over the operands unnecessarily which can be expensive for
1267 // instructions with many operands.
1268 if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001269 const DataLayout &DL = BBTo->getModule()->getDataLayout();
1270 if (usesOperand(Usr, Condition)) {
1271 // If Val has Condition as an operand and Val can be folded into a
1272 // constant with either Condition == true or Condition == false,
1273 // propagate the constant.
1274 // eg.
1275 // ; %Val is true on the edge to %then.
1276 // %Val = and i1 %Condition, true.
1277 // br %Condition, label %then, label %else
1278 APInt ConditionVal(1, isTrueDest ? 1 : 0);
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001279 Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001280 } else {
1281 // If one of Val's operand has an inferred value, we may be able to
1282 // infer the value of Val.
1283 // eg.
1284 // ; %Val is 94 on the edge to %then.
1285 // %Val = add i8 %Op, 1
1286 // %Condition = icmp eq i8 %Op, 93
1287 // br i1 %Condition, label %then, label %else
1288 for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
1289 Value *Op = Usr->getOperand(i);
Florian Hahn5133f6f2017-09-28 11:09:22 +00001290 ValueLatticeElement OpLatticeVal =
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001291 getValueFromCondition(Op, Condition, isTrueDest);
1292 if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001293 Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001294 break;
1295 }
1296 }
1297 }
1298 }
1299 }
Artur Pilipenko5e9462a2016-08-10 13:38:07 +00001300 if (!Result.isOverdefined())
Artur Pilipenkoe2e5c072016-08-02 16:20:48 +00001301 return true;
Chris Lattner16976522009-11-11 22:48:44 +00001302 }
1303 }
Chris Lattner800c47e2009-11-15 20:02:12 +00001304
1305 // If the edge was formed by a switch on the value, then we may know exactly
1306 // what it is.
1307 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001308 Value *Condition = SI->getCondition();
1309 if (!isa<IntegerType>(Val->getType()))
Nuno Lopese5048772012-06-28 16:13:37 +00001310 return false;
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001311 bool ValUsesConditionAndMayBeFoldable = false;
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001312 if (Condition != Val) {
1313 // Check if Val has Condition as an operand.
1314 if (User *Usr = dyn_cast<User>(Val))
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001315 ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
1316 usesOperand(Usr, Condition);
1317 if (!ValUsesConditionAndMayBeFoldable)
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001318 return false;
1319 }
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001320 assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001321 "Condition != Val nor Val doesn't use Condition");
Nuno Lopese5048772012-06-28 16:13:37 +00001322
1323 bool DefaultCase = SI->getDefaultDest() == BBTo;
1324 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1325 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1326
Chandler Carruthddfada22017-04-12 07:27:28 +00001327 for (auto Case : SI->cases()) {
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001328 APInt CaseValue = Case.getCaseValue()->getValue();
1329 ConstantRange EdgeVal(CaseValue);
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001330 if (ValUsesConditionAndMayBeFoldable) {
1331 User *Usr = cast<User>(Val);
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001332 const DataLayout &DL = BBTo->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001333 ValueLatticeElement EdgeLatticeVal =
Hiroshi Yamauchi8d76d002017-08-10 02:23:14 +00001334 constantFoldUser(Usr, Condition, CaseValue, DL);
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001335 if (EdgeLatticeVal.isOverdefined())
1336 return false;
1337 EdgeVal = EdgeLatticeVal.getConstantRange();
1338 }
Manman Ren408853e2012-09-05 23:45:58 +00001339 if (DefaultCase) {
1340 // It is possible that the default destination is the destination of
Hiroshi Yamauchia75318a2017-08-03 21:11:30 +00001341 // some cases. We cannot perform difference for those cases.
1342 // We know Condition != CaseValue in BBTo. In some cases we can use
1343 // this to infer Val == f(Condition) is != f(CaseValue). For now, we
1344 // only do this when f is identity (i.e. Val == Condition), but we
1345 // should be able to do this for any injective f.
1346 if (Case.getCaseSuccessor() != BBTo && Condition == Val)
Manman Ren408853e2012-09-05 23:45:58 +00001347 EdgesVals = EdgesVals.difference(EdgeVal);
Chandler Carruthddfada22017-04-12 07:27:28 +00001348 } else if (Case.getCaseSuccessor() == BBTo)
Nuno Lopes90255a82012-05-18 21:02:10 +00001349 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner800c47e2009-11-15 20:02:12 +00001350 }
Florian Hahn5133f6f2017-09-28 11:09:22 +00001351 Result = ValueLatticeElement::getRange(std::move(EdgesVals));
Nuno Lopese5048772012-06-28 16:13:37 +00001352 return true;
Chris Lattner800c47e2009-11-15 20:02:12 +00001353 }
Nuno Lopese4413942012-06-28 01:16:18 +00001354 return false;
1355}
1356
Adrian Prantl26b584c2018-05-01 15:54:18 +00001357/// Compute the value of Val on the edge BBFrom -> BBTo or the value at
Sanjay Patela9877422015-01-09 16:35:37 +00001358/// the basic block if the edge does not constrain Val.
Philip Reames8efea572016-09-12 21:46:58 +00001359bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Florian Hahn5133f6f2017-09-28 11:09:22 +00001360 BasicBlock *BBTo,
1361 ValueLatticeElement &Result,
Xin Tong8ff56502017-02-24 20:59:26 +00001362 Instruction *CxtI) {
Nuno Lopese4413942012-06-28 01:16:18 +00001363 // If already a constant, there is nothing to compute.
1364 if (Constant *VC = dyn_cast<Constant>(Val)) {
Florian Hahn5133f6f2017-09-28 11:09:22 +00001365 Result = ValueLatticeElement::get(VC);
Nick Lewycky90862ee2010-12-18 01:00:40 +00001366 return true;
1367 }
Nuno Lopese4413942012-06-28 01:16:18 +00001368
Florian Hahn5133f6f2017-09-28 11:09:22 +00001369 ValueLatticeElement LocalResult;
Philip Reamesbf09bb72016-02-02 03:15:40 +00001370 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1371 // If we couldn't constrain the value on the edge, LocalResult doesn't
1372 // provide any information.
Florian Hahn5133f6f2017-09-28 11:09:22 +00001373 LocalResult = ValueLatticeElement::getOverdefined();
NAKAMURA Takumi283ee812016-07-04 01:26:33 +00001374
Philip Reamesbf09bb72016-02-02 03:15:40 +00001375 if (hasSingleValue(LocalResult)) {
1376 // Can't get any more precise here
1377 Result = LocalResult;
Nuno Lopese4413942012-06-28 01:16:18 +00001378 return true;
1379 }
1380
1381 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg4d48c3f12014-11-25 17:23:05 +00001382 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1383 return false;
Philip Reamesbf09bb72016-02-02 03:15:40 +00001384 // No new information.
1385 Result = LocalResult;
Hans Wennborg4d48c3f12014-11-25 17:23:05 +00001386 return true;
Nuno Lopese4413942012-06-28 01:16:18 +00001387 }
1388
Philip Reamesbf09bb72016-02-02 03:15:40 +00001389 // Try to intersect ranges of the BB and the constraint on the edge.
Florian Hahn5133f6f2017-09-28 11:09:22 +00001390 ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +00001391 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1392 BBFrom->getTerminator());
Hal Finkel61c65b22014-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 Reames8efea572016-09-12 21:46:58 +00001399 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel61c65b22014-10-16 00:40:05 +00001400 // but then the result is not cached.
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +00001401 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reamesbf09bb72016-02-02 03:15:40 +00001402
1403 Result = intersect(LocalResult, InBlock);
Nuno Lopese4413942012-06-28 01:16:18 +00001404 return true;
Chris Lattner16976522009-11-11 22:48:44 +00001405}
1406
Florian Hahn5133f6f2017-09-28 11:09:22 +00001407ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
1408 Instruction *CxtI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001409 LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
1410 << BB->getName() << "'\n");
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001411
Hans Wennborg4d48c3f12014-11-25 17:23:05 +00001412 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reames1d9f7a92016-02-10 21:46:32 +00001413 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumib78624d2016-07-25 00:59:46 +00001414 pushBlockValue(std::make_pair(BB, V));
Philip Reames1d9f7a92016-02-10 21:46:32 +00001415 solve();
1416 }
Florian Hahn5133f6f2017-09-28 11:09:22 +00001417 ValueLatticeElement Result = getBlockValue(V, BB);
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +00001418 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001419
Nicola Zaghen0818e782018-05-14 12:53:11 +00001420 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001421 return Result;
1422}
1423
Florian Hahn5133f6f2017-09-28 11:09:22 +00001424ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001425 LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()
1426 << "'\n");
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001427
Philip Reames1d9f7a92016-02-10 21:46:32 +00001428 if (auto *C = dyn_cast<Constant>(V))
Florian Hahn5133f6f2017-09-28 11:09:22 +00001429 return ValueLatticeElement::get(C);
Philip Reames1d9f7a92016-02-10 21:46:32 +00001430
Florian Hahn5133f6f2017-09-28 11:09:22 +00001431 ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
Philip Reames45ef74f2015-10-29 03:57:17 +00001432 if (auto *I = dyn_cast<Instruction>(V))
1433 Result = getFromRangeMetadata(I);
Artur Pilipenkoe5ae8392016-08-12 15:52:23 +00001434 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reamesb4e775c2016-02-02 00:45:30 +00001435
Nicola Zaghen0818e782018-05-14 12:53:11 +00001436 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +00001437 return Result;
1438}
Chris Lattner16976522009-11-11 22:48:44 +00001439
Florian Hahn5133f6f2017-09-28 11:09:22 +00001440ValueLatticeElement LazyValueInfoImpl::
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001441getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1442 Instruction *CxtI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001443 LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
1444 << FromBB->getName() << "' to '" << ToBB->getName()
1445 << "'\n");
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001446
Florian Hahn5133f6f2017-09-28 11:09:22 +00001447 ValueLatticeElement Result;
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001448 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky90862ee2010-12-18 01:00:40 +00001449 solve();
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001450 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky90862ee2010-12-18 01:00:40 +00001451 (void)WasFastQuery;
1452 assert(WasFastQuery && "More work to do after problem solved?");
1453 }
1454
Nicola Zaghen0818e782018-05-14 12:53:11 +00001455 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattner2c5adf82009-11-15 19:59:49 +00001456 return Result;
1457}
1458
Philip Reames8efea572016-09-12 21:46:58 +00001459void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames35517182016-09-12 22:38:44 +00001460 BasicBlock *NewSucc) {
1461 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersoncfa7fb62010-07-26 18:48:03 +00001462}
1463
Chris Lattner2c5adf82009-11-15 19:59:49 +00001464//===----------------------------------------------------------------------===//
1465// LazyValueInfo Impl
1466//===----------------------------------------------------------------------===//
1467
Philip Reames8efea572016-09-12 21:46:58 +00001468/// This lazily constructs the LazyValueInfoImpl.
Daniel Jasper8de3a542016-12-19 08:22:17 +00001469static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1470 const DataLayout *DL,
Philip Reames8efea572016-09-12 21:46:58 +00001471 DominatorTree *DT = nullptr) {
Mehdi Amini529919f2015-03-10 02:37:25 +00001472 if (!PImpl) {
1473 assert(DL && "getCache() called with a null DataLayout");
Daniel Jasper8de3a542016-12-19 08:22:17 +00001474 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Amini529919f2015-03-10 02:37:25 +00001475 }
Philip Reames8efea572016-09-12 21:46:58 +00001476 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattner2c5adf82009-11-15 19:59:49 +00001477}
1478
Sean Silva9ce41c32016-06-13 22:01:25 +00001479bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
Daniel Jasper8de3a542016-12-19 08:22:17 +00001480 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Amini529919f2015-03-10 02:37:25 +00001481 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001482
1483 DominatorTreeWrapperPass *DTWP =
1484 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva9ce41c32016-06-13 22:01:25 +00001485 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1486 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosieraab8e282011-12-02 01:26:24 +00001487
Sean Silva9ce41c32016-06-13 22:01:25 +00001488 if (Info.PImpl)
Daniel Jasper8de3a542016-12-19 08:22:17 +00001489 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001490
Owen Anderson00ac77e2010-08-18 18:39:01 +00001491 // Fully lazy.
1492 return false;
1493}
1494
Sean Silva9ce41c32016-06-13 22:01:25 +00001495void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosieraab8e282011-12-02 01:26:24 +00001496 AU.setPreservesAll();
Daniel Jasper8de3a542016-12-19 08:22:17 +00001497 AU.addRequired<AssumptionCacheTracker>();
Chandler Carrutheeeec3c2015-01-15 10:41:28 +00001498 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosieraab8e282011-12-02 01:26:24 +00001499}
1500
Sean Silva9ce41c32016-06-13 22:01:25 +00001501LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1502
1503LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1504
Chris Lattner2c5adf82009-11-15 19:59:49 +00001505void LazyValueInfo::releaseMemory() {
1506 // If the cache was allocated, free it.
1507 if (PImpl) {
Daniel Jasper8de3a542016-12-19 08:22:17 +00001508 delete &getImpl(PImpl, AC, nullptr);
Craig Topper570e52c2014-04-15 04:59:12 +00001509 PImpl = nullptr;
Chris Lattner2c5adf82009-11-15 19:59:49 +00001510 }
1511}
1512
Chandler Carruthd894e4c2017-01-23 06:35:12 +00001513bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1514 FunctionAnalysisManager::Invalidator &Inv) {
1515 // We need to invalidate if we have either failed to preserve this analyses
1516 // result directly or if any of its dependencies have been invalidated.
1517 auto PAC = PA.getChecker<LazyValueAnalysis>();
1518 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1519 (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1520 return true;
1521
1522 return false;
1523}
1524
Sean Silva9ce41c32016-06-13 22:01:25 +00001525void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1526
Florian Hahn5133f6f2017-09-28 11:09:22 +00001527LazyValueInfo LazyValueAnalysis::run(Function &F,
1528 FunctionAnalysisManager &FAM) {
Daniel Jasper8de3a542016-12-19 08:22:17 +00001529 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
Sean Silva9ce41c32016-06-13 22:01:25 +00001530 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1531 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1532
Anna Thomas5596b412017-03-12 14:06:41 +00001533 return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
Sean Silva9ce41c32016-06-13 22:01:25 +00001534}
1535
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001536/// Returns true if we can statically tell that this value will never be a
1537/// "useful" constant. In practice, this means we've got something like an
1538/// alloca or a malloc call for which a comparison against a constant can
1539/// only be guarding dead code. Note that we are potentially giving up some
1540/// precision in dead code (a constant result) in favour of avoiding a
1541/// expensive search for a easily answered common query.
1542static bool isKnownNonConstant(Value *V) {
1543 V = V->stripPointerCasts();
1544 // The return val of alloc cannot be a Constant.
1545 if (isa<AllocaInst>(V))
1546 return true;
1547 return false;
1548}
1549
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001550Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1551 Instruction *CxtI) {
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001552 // Bail out early if V is known not to be a Constant.
1553 if (isKnownNonConstant(V))
1554 return nullptr;
1555
Mehdi Amini529919f2015-03-10 02:37:25 +00001556 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001557 ValueLatticeElement Result =
Daniel Jasper8de3a542016-12-19 08:22:17 +00001558 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth5a9cd4d2015-01-04 12:03:27 +00001559
Chris Lattner16976522009-11-11 22:48:44 +00001560 if (Result.isConstant())
1561 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001562 if (Result.isConstantRange()) {
Craig Topper583a2512017-05-06 03:35:15 +00001563 const ConstantRange &CR = Result.getConstantRange();
Owen Andersonee61fcf2010-08-27 23:29:38 +00001564 if (const APInt *SingleVal = CR.getSingleElement())
1565 return ConstantInt::get(V->getContext(), *SingleVal);
1566 }
Craig Topper570e52c2014-04-15 04:59:12 +00001567 return nullptr;
Chris Lattner16976522009-11-11 22:48:44 +00001568}
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001569
John Regehr98d359a2016-05-02 19:58:00 +00001570ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumief2b8e72016-07-04 01:26:21 +00001571 Instruction *CxtI) {
John Regehr98d359a2016-05-02 19:58:00 +00001572 assert(V->getType()->isIntegerTy());
1573 unsigned Width = V->getType()->getIntegerBitWidth();
1574 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001575 ValueLatticeElement Result =
Daniel Jasper8de3a542016-12-19 08:22:17 +00001576 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehr98d359a2016-05-02 19:58:00 +00001577 if (Result.isUndefined())
1578 return ConstantRange(Width, /*isFullSet=*/false);
1579 if (Result.isConstantRange())
1580 return Result.getConstantRange();
Artur Pilipenko06ab33e2016-08-10 12:54:54 +00001581 // We represent ConstantInt constants as constant ranges but other kinds
1582 // of integer constants, i.e. ConstantExpr will be tagged as constants
1583 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1584 "ConstantInt value must be represented as constantrange");
Davide Italiano088a7732016-05-25 22:29:34 +00001585 return ConstantRange(Width, /*isFullSet=*/true);
John Regehr98d359a2016-05-02 19:58:00 +00001586}
1587
Sanjay Patel61478e32015-01-09 16:47:20 +00001588/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001589/// constant on the specified edge. Return null if not.
Chris Lattner38392bb2009-11-12 01:29:10 +00001590Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001591 BasicBlock *ToBB,
1592 Instruction *CxtI) {
Mehdi Amini529919f2015-03-10 02:37:25 +00001593 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001594 ValueLatticeElement Result =
Daniel Jasper8de3a542016-12-19 08:22:17 +00001595 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth5a9cd4d2015-01-04 12:03:27 +00001596
Chris Lattner38392bb2009-11-12 01:29:10 +00001597 if (Result.isConstant())
1598 return Result.getConstant();
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001599 if (Result.isConstantRange()) {
Craig Topper583a2512017-05-06 03:35:15 +00001600 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson9f014062010-08-10 20:03:09 +00001601 if (const APInt *SingleVal = CR.getSingleElement())
1602 return ConstantInt::get(V->getContext(), *SingleVal);
1603 }
Craig Topper570e52c2014-04-15 04:59:12 +00001604 return nullptr;
Chris Lattner38392bb2009-11-12 01:29:10 +00001605}
1606
Craig Toppera98fd552017-06-23 05:41:35 +00001607ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
1608 BasicBlock *FromBB,
1609 BasicBlock *ToBB,
1610 Instruction *CxtI) {
1611 unsigned Width = V->getType()->getIntegerBitWidth();
1612 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001613 ValueLatticeElement Result =
Craig Toppera98fd552017-06-23 05:41:35 +00001614 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1615
1616 if (Result.isUndefined())
1617 return ConstantRange(Width, /*isFullSet=*/false);
1618 if (Result.isConstantRange())
1619 return Result.getConstantRange();
1620 // We represent ConstantInt constants as constant ranges but other kinds
1621 // of integer constants, i.e. ConstantExpr will be tagged as constants
1622 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1623 "ConstantInt value must be represented as constantrange");
1624 return ConstantRange(Width, /*isFullSet=*/true);
1625}
1626
Florian Hahn5133f6f2017-09-28 11:09:22 +00001627static LazyValueInfo::Tristate
1628getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
1629 const DataLayout &DL, TargetLibraryInfo *TLI) {
Chris Lattnerb52675b2009-11-12 04:36:58 +00001630 // If we know the value is a constant, evaluate the conditional.
Craig Topper570e52c2014-04-15 04:59:12 +00001631 Constant *Res = nullptr;
Craig Topperf12aebd2017-06-09 21:18:16 +00001632 if (Val.isConstant()) {
1633 Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001634 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001635 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1636 return LazyValueInfo::Unknown;
Chris Lattner2c5adf82009-11-15 19:59:49 +00001637 }
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001638
Craig Topperf12aebd2017-06-09 21:18:16 +00001639 if (Val.isConstantRange()) {
Owen Anderson59b06dc2010-08-24 07:55:44 +00001640 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001641 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001642
Craig Topperf12aebd2017-06-09 21:18:16 +00001643 const ConstantRange &CR = Val.getConstantRange();
Owen Anderson9f014062010-08-10 20:03:09 +00001644 if (Pred == ICmpInst::ICMP_EQ) {
1645 if (!CR.contains(CI->getValue()))
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001646 return LazyValueInfo::False;
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001647
Craig Topperd05a5f22017-06-07 00:58:09 +00001648 if (CR.isSingleElement())
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001649 return LazyValueInfo::True;
Owen Anderson9f014062010-08-10 20:03:09 +00001650 } else if (Pred == ICmpInst::ICMP_NE) {
1651 if (!CR.contains(CI->getValue()))
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001652 return LazyValueInfo::True;
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001653
Craig Topperd05a5f22017-06-07 00:58:09 +00001654 if (CR.isSingleElement())
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001655 return LazyValueInfo::False;
Craig Topperc65472c2017-06-09 16:16:20 +00001656 } else {
1657 // Handle more complex predicates.
1658 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1659 (ICmpInst::Predicate)Pred, CI->getValue());
1660 if (TrueValues.contains(CR))
1661 return LazyValueInfo::True;
1662 if (TrueValues.inverse().contains(CR))
1663 return LazyValueInfo::False;
Owen Anderson9f014062010-08-10 20:03:09 +00001664 }
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001665 return LazyValueInfo::Unknown;
Owen Anderson9f014062010-08-10 20:03:09 +00001666 }
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001667
Craig Topperf12aebd2017-06-09 21:18:16 +00001668 if (Val.isNotConstant()) {
Chris Lattnerb52675b2009-11-12 04:36:58 +00001669 // If this is an equality comparison, we can try to fold it knowing that
1670 // "V != C1".
1671 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001672 // !C1 == C -> false iff C1 == C.
Chris Lattnerb52675b2009-11-12 04:36:58 +00001673 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topperf12aebd2017-06-09 21:18:16 +00001674 Val.getNotConstant(), C, DL,
Chad Rosieraab8e282011-12-02 01:26:24 +00001675 TLI);
Chris Lattnerb52675b2009-11-12 04:36:58 +00001676 if (Res->isNullValue())
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001677 return LazyValueInfo::False;
Chris Lattnerb52675b2009-11-12 04:36:58 +00001678 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001679 // !C1 != C -> true iff C1 == C.
Chris Lattner5553a3a2009-11-15 20:01:24 +00001680 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topperf12aebd2017-06-09 21:18:16 +00001681 Val.getNotConstant(), C, DL,
Chad Rosieraab8e282011-12-02 01:26:24 +00001682 TLI);
Chris Lattnerb52675b2009-11-12 04:36:58 +00001683 if (Res->isNullValue())
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001684 return LazyValueInfo::True;
Chris Lattnerb52675b2009-11-12 04:36:58 +00001685 }
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001686 return LazyValueInfo::Unknown;
Chris Lattnerb52675b2009-11-12 04:36:58 +00001687 }
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001688
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001689 return LazyValueInfo::Unknown;
1690}
1691
Sanjay Patel61478e32015-01-09 16:47:20 +00001692/// Determine whether the specified value comparison with a constant is known to
1693/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001694LazyValueInfo::Tristate
1695LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1696 BasicBlock *FromBB, BasicBlock *ToBB,
1697 Instruction *CxtI) {
Mehdi Amini529919f2015-03-10 02:37:25 +00001698 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn5133f6f2017-09-28 11:09:22 +00001699 ValueLatticeElement Result =
Daniel Jasper8de3a542016-12-19 08:22:17 +00001700 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001701
1702 return getPredicateResult(Pred, C, Result, DL, TLI);
1703}
1704
1705LazyValueInfo::Tristate
1706LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1707 Instruction *CxtI) {
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001708 // Is or is not NonNull are common predicates being queried. If
Nuno Lopesfe353a02017-09-09 18:23:11 +00001709 // isKnownNonZero can tell us the result of the predicate, we can
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001710 // return it quickly. But this is only a fastpath, and falling
1711 // through would still be correct.
Nuno Lopesfe353a02017-09-09 18:23:11 +00001712 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001713 if (V->getType()->isPointerTy() && C->isNullValue() &&
Nuno Lopesfe353a02017-09-09 18:23:11 +00001714 isKnownNonZero(V->stripPointerCasts(), DL)) {
Wei Mi0bcb8fb2016-09-15 06:28:34 +00001715 if (Pred == ICmpInst::ICMP_EQ)
1716 return LazyValueInfo::False;
1717 else if (Pred == ICmpInst::ICMP_NE)
1718 return LazyValueInfo::True;
1719 }
Florian Hahn5133f6f2017-09-28 11:09:22 +00001720 ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames94261332015-06-16 00:49:59 +00001721 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1722 if (Ret != Unknown)
1723 return Ret;
Hal Finkel3ef1aae2014-09-07 20:29:59 +00001724
Philip Reames6aa26792015-11-04 01:47:04 +00001725 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1726 // LVI as a whole tries to compute a lattice value which is conservatively
1727 // correct at a given location. In this case, we have a predicate which we
1728 // weren't able to prove about the merged result, and we're pushing that
1729 // predicate back along each incoming edge to see if we can prove it
1730 // separately for each input. As a motivating example, consider:
1731 // bb1:
1732 // %v1 = ... ; constantrange<1, 5>
1733 // br label %merge
1734 // bb2:
1735 // %v2 = ... ; constantrange<10, 20>
1736 // br label %merge
1737 // merge:
1738 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1739 // %pred = icmp eq i32 %phi, 8
1740 // We can't tell from the lattice value for '%phi' that '%pred' is false
1741 // along each path, but by checking the predicate over each input separately,
1742 // we can.
1743 // We limit the search to one step backwards from the current BB and value.
1744 // We could consider extending this to search further backwards through the
1745 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumi6f439632016-07-04 01:26:27 +00001746 // tradeoffs.
Philip Reames94261332015-06-16 00:49:59 +00001747 if (CxtI) {
Philip Reames9c89f442015-08-31 18:31:48 +00001748 BasicBlock *BB = CxtI->getParent();
1749
1750 // Function entry or an unreachable block. Bail to avoid confusing
1751 // analysis below.
1752 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1753 if (PI == PE)
1754 return Unknown;
1755
1756 // If V is a PHI node in the same block as the context, we need to ask
1757 // questions about the predicate as applied to the incoming value along
1758 // each edge. This is useful for eliminating cases where the predicate is
1759 // known along all incoming edges.
1760 if (auto *PHI = dyn_cast<PHINode>(V))
1761 if (PHI->getParent() == BB) {
1762 Tristate Baseline = Unknown;
1763 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1764 Value *Incoming = PHI->getIncomingValue(i);
1765 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumi6f439632016-07-04 01:26:27 +00001766 // Note that PredBB may be BB itself.
Philip Reames9c89f442015-08-31 18:31:48 +00001767 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1768 CxtI);
NAKAMURA Takumi283ee812016-07-04 01:26:33 +00001769
Philip Reames9c89f442015-08-31 18:31:48 +00001770 // Keep going as long as we've seen a consistent known result for
1771 // all inputs.
1772 Baseline = (i == 0) ? Result /* First iteration */
1773 : (Baseline == Result ? Baseline : Unknown); /* All others */
1774 if (Baseline == Unknown)
1775 break;
1776 }
1777 if (Baseline != Unknown)
1778 return Baseline;
NAKAMURA Takumib78624d2016-07-25 00:59:46 +00001779 }
Philip Reames9c89f442015-08-31 18:31:48 +00001780
Philip Reames94261332015-06-16 00:49:59 +00001781 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001782 // that we've branched on it before. Look to see if the value is known
Philip Reames94261332015-06-16 00:49:59 +00001783 // on all incoming edges.
Philip Reames9c89f442015-08-31 18:31:48 +00001784 if (!isa<Instruction>(V) ||
1785 cast<Instruction>(V)->getParent() != BB) {
Philip Reames94261332015-06-16 00:49:59 +00001786 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes456b44f2015-07-28 15:53:21 +00001787 // on that edge. If they're all true or all false, we can conclude
Philip Reames94261332015-06-16 00:49:59 +00001788 // the value of the comparison in this block.
1789 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1790 if (Baseline != Unknown) {
1791 // Check that all remaining incoming values match the first one.
1792 while (++PI != PE) {
1793 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1794 if (Ret != Baseline) break;
1795 }
1796 // If we terminated early, then one of the values didn't match.
1797 if (PI == PE) {
1798 return Baseline;
1799 }
1800 }
1801 }
1802 }
1803 return Unknown;
Chris Lattnercc4d3b22009-11-11 02:08:33 +00001804}
1805
Owen Andersoncfa7fb62010-07-26 18:48:03 +00001806void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky69bfdf52010-12-15 18:57:18 +00001807 BasicBlock *NewSucc) {
Mehdi Amini529919f2015-03-10 02:37:25 +00001808 if (PImpl) {
1809 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Daniel Jasper8de3a542016-12-19 08:22:17 +00001810 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Amini529919f2015-03-10 02:37:25 +00001811 }
Owen Anderson00ac77e2010-08-18 18:39:01 +00001812}
1813
1814void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Amini529919f2015-03-10 02:37:25 +00001815 if (PImpl) {
1816 const DataLayout &DL = BB->getModule()->getDataLayout();
Daniel Jasper8de3a542016-12-19 08:22:17 +00001817 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Amini529919f2015-03-10 02:37:25 +00001818 }
Owen Andersoncfa7fb62010-07-26 18:48:03 +00001819}
Anna Thomas6cde8772017-03-22 19:27:12 +00001820
1821
Anna Thomas46747f12017-06-06 19:25:31 +00001822void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
Anna Thomas6cde8772017-03-22 19:27:12 +00001823 if (PImpl) {
Anna Thomas46747f12017-06-06 19:25:31 +00001824 getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
Anna Thomas6cde8772017-03-22 19:27:12 +00001825 }
1826}
1827
Brian M. Rzycki55da8a32018-02-16 16:35:17 +00001828void LazyValueInfo::disableDT() {
1829 if (PImpl)
1830 getImpl(PImpl, AC, DL, DT).disableDT();
1831}
1832
1833void LazyValueInfo::enableDT() {
1834 if (PImpl)
1835 getImpl(PImpl, AC, DL, DT).enableDT();
1836}
1837
Anna Thomas46747f12017-06-06 19:25:31 +00001838// Print the LVI for the function arguments at the start of each basic block.
1839void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
1840 const BasicBlock *BB, formatted_raw_ostream &OS) {
1841 // Find if there are latticevalues defined for arguments of the function.
1842 auto *F = BB->getParent();
1843 for (auto &Arg : F->args()) {
Florian Hahn5133f6f2017-09-28 11:09:22 +00001844 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas46747f12017-06-06 19:25:31 +00001845 const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
1846 if (Result.isUndefined())
1847 continue;
1848 OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
1849 }
1850}
1851
1852// This function prints the LVI analysis for the instruction I at the beginning
1853// of various basic blocks. It relies on calculated values that are stored in
Xin Tongd5a2fd42018-04-24 07:38:07 +00001854// the LazyValueInfoCache, and in the absence of cached values, recalculate the
Anna Thomas46747f12017-06-06 19:25:31 +00001855// LazyValueInfo for `I`, and print that info.
1856void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
1857 const Instruction *I, formatted_raw_ostream &OS) {
1858
1859 auto *ParentBB = I->getParent();
1860 SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
1861 // We can generate (solve) LVI values only for blocks that are dominated by
1862 // the I's parent. However, to avoid generating LVI for all dominating blocks,
1863 // that contain redundant/uninteresting information, we print LVI for
1864 // blocks that may use this LVI information (such as immediate successor
1865 // blocks, and blocks that contain uses of `I`).
1866 auto printResult = [&](const BasicBlock *BB) {
1867 if (!BlocksContainingLVI.insert(BB).second)
1868 return;
Florian Hahn5133f6f2017-09-28 11:09:22 +00001869 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas46747f12017-06-06 19:25:31 +00001870 const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
1871 OS << "; LatticeVal for: '" << *I << "' in BB: '";
1872 BB->printAsOperand(OS, false);
1873 OS << "' is: " << Result << "\n";
1874 };
1875
1876 printResult(ParentBB);
Hiroshi Inoue5cba3282018-01-17 12:29:38 +00001877 // Print the LVI analysis results for the immediate successor blocks, that
Anna Thomas46747f12017-06-06 19:25:31 +00001878 // are dominated by `ParentBB`.
1879 for (auto *BBSucc : successors(ParentBB))
1880 if (DT.dominates(ParentBB, BBSucc))
1881 printResult(BBSucc);
1882
1883 // Print LVI in blocks where `I` is used.
1884 for (auto *U : I->users())
1885 if (auto *UseI = dyn_cast<Instruction>(U))
1886 if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
1887 printResult(UseI->getParent());
1888
1889}
1890
Anna Thomas6cde8772017-03-22 19:27:12 +00001891namespace {
1892// Printer class for LazyValueInfo results.
1893class LazyValueInfoPrinter : public FunctionPass {
1894public:
1895 static char ID; // Pass identification, replacement for typeid
1896 LazyValueInfoPrinter() : FunctionPass(ID) {
1897 initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
1898 }
1899
1900 void getAnalysisUsage(AnalysisUsage &AU) const override {
1901 AU.setPreservesAll();
1902 AU.addRequired<LazyValueInfoWrapperPass>();
Anna Thomas46747f12017-06-06 19:25:31 +00001903 AU.addRequired<DominatorTreeWrapperPass>();
Anna Thomas6cde8772017-03-22 19:27:12 +00001904 }
1905
Anna Thomas46747f12017-06-06 19:25:31 +00001906 // Get the mandatory dominator tree analysis and pass this in to the
1907 // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
Anna Thomas6cde8772017-03-22 19:27:12 +00001908 bool runOnFunction(Function &F) override {
1909 dbgs() << "LVI for function '" << F.getName() << "':\n";
1910 auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
Anna Thomas46747f12017-06-06 19:25:31 +00001911 auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1912 LVI.printLVI(F, DTree, dbgs());
Anna Thomas6cde8772017-03-22 19:27:12 +00001913 return false;
1914 }
1915};
1916}
1917
1918char LazyValueInfoPrinter::ID = 0;
1919INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
1920 "Lazy Value Info Printer Pass", false, false)
1921INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
1922INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
1923 "Lazy Value Info Printer Pass", false, false)