blob: 3a9dac5783f735a3a0705ba64e1cd2910e479838 [file] [log] [blame]
Hans Wennborgc5ec73d2014-11-21 18:58:23 +00001//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
Chris Lattner741c94c2009-11-11 00:22:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000018#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Analysis/ConstantFolding.h"
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +000020#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000022#include "llvm/Analysis/ValueTracking.h"
Florian Hahn8af01572017-09-28 11:09:22 +000023#include "llvm/Analysis/ValueLattice.h"
Anna Thomase27b39a2017-03-22 19:27:12 +000024#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000026#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Hal Finkel7e184492014-09-07 20:29:59 +000029#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
31#include "llvm/IR/IntrinsicInst.h"
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +000032#include "llvm/IR/Intrinsics.h"
Philip Reameseb3e9da2015-10-29 03:57:17 +000033#include "llvm/IR/LLVMContext.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000034#include "llvm/IR/PatternMatch.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000035#include "llvm/IR/ValueHandle.h"
Chris Lattnerb584d1e2009-11-12 01:22:16 +000036#include "llvm/Support/Debug.h"
Anna Thomase27b39a2017-03-22 19:27:12 +000037#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/raw_ostream.h"
Bill Wendling4ec081a2012-01-11 23:43:34 +000039#include <map>
Nick Lewycky55a700b2010-12-18 01:00:40 +000040#include <stack>
Chris Lattner741c94c2009-11-11 00:22:30 +000041using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000042using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000043
Chandler Carruthf1221bd2014-04-22 02:48:03 +000044#define DEBUG_TYPE "lazy-value-info"
45
Daniel Berlin9c92a462017-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 Silva687019f2016-06-13 22:01:25 +000050char LazyValueInfoWrapperPass::ID = 0;
51INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosier43a33062011-12-02 01:26:24 +000052 "Lazy Value Information Analysis", false, true)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000053INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000054INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva687019f2016-06-13 22:01:25 +000055INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000056 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000057
58namespace llvm {
Sean Silva687019f2016-06-13 22:01:25 +000059 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner741c94c2009-11-11 00:22:30 +000060}
61
Chandler Carruthdab4eae2016-11-23 17:53:26 +000062AnalysisKey LazyValueAnalysis::Key;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000063
Philip Reamesed8cd0d2016-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 Hahn8af01572017-09-28 11:09:22 +000067static bool hasSingleValue(const ValueLatticeElement &Val) {
Philip Reamesed8cd0d2016-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 Takumif2529512016-07-04 01:26:27 +000087/// conflict, see below).
Philip Reamesed8cd0d2016-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 Hahn8af01572017-09-28 11:09:22 +000092static ValueLatticeElement intersect(const ValueLatticeElement &A,
93 const ValueLatticeElement &B) {
Philip Reamesed8cd0d2016-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 Takumif2529512016-07-04 01:26:27 +0000124 // and thus know this path must be unreachable.
Florian Hahn8af01572017-09-28 11:09:22 +0000125 return ValueLatticeElement::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000126}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000127
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000128//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000129// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000130//===----------------------------------------------------------------------===//
131
Chris Lattneraf025d32009-11-15 19:59:49 +0000132namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000133 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000134 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000135 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000136 // Needs to access getValPtr(), which is protected.
137 friend struct DenseMapInfo<LVIValueHandle>;
138
Owen Anderson118ac802011-01-05 21:15:29 +0000139 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000140
Owen Anderson118ac802011-01-05 21:15:29 +0000141 LVIValueHandle(Value *V, LazyValueInfoCache *P)
142 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000143
144 void deleted() override;
145 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000146 deleted();
147 }
148 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000149} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000150
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000151namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000152 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000153 /// maintains information about queries across the clients' queries.
154 class LazyValueInfoCache {
Sanjay Patel2a385e22015-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 Lattneraf025d32009-11-15 19:59:49 +0000157 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000158 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
159 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000160 struct ValueCacheEntryTy {
161 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
162 LVIValueHandle Handle;
Florian Hahn8af01572017-09-28 11:09:22 +0000163 SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals;
Justin Lebar58b377e2016-07-27 22:33:36 +0000164 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000165
Sanjay Patel2a385e22015-01-09 16:47:20 +0000166 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000167 /// over-defined at the end of that block.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000168 typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000169 OverDefinedCacheTy;
Sanjay Patel2a385e22015-01-09 16:47:20 +0000170 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000171 /// don't spend time removing unused blocks from our caches.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000172 DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
Benjamin Kramer36647082011-12-03 15:16:45 +0000173
Anna Thomase27b39a2017-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 Reames9db79482016-09-12 22:38:44 +0000180 public:
Florian Hahn8af01572017-09-28 11:09:22 +0000181 void insertResult(Value *Val, BasicBlock *BB,
182 const ValueLatticeElement &Result) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000183 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000184
185 // Insert over-defined values into their own cache to reduce memory
186 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000187 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000188 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-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 Wennborg45172ac2014-11-25 17:23:05 +0000198 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000199
Akira Hatanaka2992bee2015-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 Reames9db79482016-09-12 22:38:44 +0000209 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000210 if (isOverdefined(V, BB))
211 return true;
212
Justin Lebar58b377e2016-07-27 22:33:36 +0000213 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000214 if (I == ValueCache.end())
215 return false;
216
Justin Lebar58b377e2016-07-27 22:33:36 +0000217 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000218 }
219
Florian Hahn8af01572017-09-28 11:09:22 +0000220 ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000221 if (isOverdefined(V, BB))
Florian Hahn8af01572017-09-28 11:09:22 +0000222 return ValueLatticeElement::getOverdefined();
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000223
Justin Lebar58b377e2016-07-27 22:33:36 +0000224 auto I = ValueCache.find_as(V);
225 if (I == ValueCache.end())
Florian Hahn8af01572017-09-28 11:09:22 +0000226 return ValueLatticeElement();
Justin Lebar58b377e2016-07-27 22:33:36 +0000227 auto BBI = I->second->BlockVals.find(BB);
228 if (BBI == I->second->BlockVals.end())
Florian Hahn8af01572017-09-28 11:09:22 +0000229 return ValueLatticeElement();
Justin Lebar58b377e2016-07-27 22:33:36 +0000230 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000231 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000232
Philip Reames92e5e1b2016-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 Reamesb627aec2016-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 Reames9db79482016-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 Reames92e5e1b2016-09-12 21:46:58 +0000252 friend struct LVIValueHandle;
253 };
Philip Reamesb627aec2016-09-12 22:03:36 +0000254}
Philip Reames92e5e1b2016-09-12 21:46:58 +0000255
Philip Reamesb627aec2016-09-12 22:03:36 +0000256void LazyValueInfoCache::eraseValue(Value *V) {
Chandler Carruth41421df2017-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 Reamesfdbb05b2016-12-30 22:09:10 +0000262 ValueSet.erase(V);
Philip Reamesb627aec2016-09-12 22:03:36 +0000263 if (ValueSet.empty())
Chandler Carruth41421df2017-01-26 08:31:54 +0000264 OverDefinedCache.erase(Iter);
Philip Reamesb627aec2016-09-12 22:03:36 +0000265 }
Philip Reamesb627aec2016-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 Carruth6acdca72017-01-24 12:55:57 +0000278 DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
Philip Reamesb627aec2016-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 Reames9db79482016-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 Reames1e48efc2016-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 Reames9db79482016-09-12 22:38:44 +0000328 bool changed = false;
329 for (Value *V : ValsToClear) {
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000330 if (!ValueSet.erase(V))
Philip Reames9db79482016-09-12 22:38:44 +0000331 continue;
332
Philip Reames9db79482016-09-12 22:38:44 +0000333 // If we removed anything, then we potentially need to update
334 // blocks successors too.
335 changed = true;
Philip Reames1e48efc2016-12-30 17:56:47 +0000336
337 if (ValueSet.empty()) {
338 OverDefinedCache.erase(OI);
339 break;
340 }
Philip Reames9db79482016-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 Thomas4acfc7e2017-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 Reamesb627aec2016-09-12 22:03:36 +0000373namespace {
Philip Reames92e5e1b2016-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 Reames9db79482016-09-12 22:38:44 +0000377 class LazyValueInfoImpl {
378
379 /// Cached results from previous queries
380 LazyValueInfoCache TheCache;
Philip Reames92e5e1b2016-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 Berlin9c92a462017-02-08 15:22:52 +0000385 SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
Philip Reames92e5e1b2016-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
396 DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName()
397 << "\n");
Daniel Berlin9c92a462017-02-08 15:22:52 +0000398 BlockValueStack.push_back(BV);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000399 return true;
400 }
401
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000402 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
Philip Reames92e5e1b2016-09-12 21:46:58 +0000403 const DataLayout &DL; ///< A mandatory DataLayout
404 DominatorTree *DT; ///< An optional DT pointer.
405
Florian Hahn8af01572017-09-28 11:09:22 +0000406 ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000407 bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
Florian Hahn8af01572017-09-28 11:09:22 +0000408 ValueLatticeElement &Result, Instruction *CxtI = nullptr);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000409 bool hasBlockValue(Value *Val, BasicBlock *BB);
410
411 // These methods process one work item and may add more. A false value
412 // returned means that the work item was not completely processed and must
413 // be revisited after going through the new items.
414 bool solveBlockValue(Value *Val, BasicBlock *BB);
Florian Hahn8af01572017-09-28 11:09:22 +0000415 bool solveBlockValueImpl(ValueLatticeElement &Res, Value *Val,
416 BasicBlock *BB);
417 bool solveBlockValueNonLocal(ValueLatticeElement &BBLV, Value *Val,
Philip Reames92e5e1b2016-09-12 21:46:58 +0000418 BasicBlock *BB);
Florian Hahn8af01572017-09-28 11:09:22 +0000419 bool solveBlockValuePHINode(ValueLatticeElement &BBLV, PHINode *PN,
420 BasicBlock *BB);
421 bool solveBlockValueSelect(ValueLatticeElement &BBLV, SelectInst *S,
422 BasicBlock *BB);
423 bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
424 BasicBlock *BB);
425 bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
Philip Reames92e5e1b2016-09-12 21:46:58 +0000426 BasicBlock *BB);
427 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
Florian Hahn8af01572017-09-28 11:09:22 +0000428 ValueLatticeElement &BBLV,
Craig Topper9277a862017-06-02 17:28:12 +0000429 Instruction *BBI);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000430
431 void solve();
432
433 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000434 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000435 /// value for the specified Value* at the end of the specified block.
Florian Hahn8af01572017-09-28 11:09:22 +0000436 ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
437 Instruction *CxtI = nullptr);
Hal Finkel7e184492014-09-07 20:29:59 +0000438
Sanjay Patel2a385e22015-01-09 16:47:20 +0000439 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000440 /// value for the specified Value* at the specified instruction (generally
441 /// from an assume intrinsic).
Florian Hahn8af01572017-09-28 11:09:22 +0000442 ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000443
Sanjay Patel2a385e22015-01-09 16:47:20 +0000444 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000445 /// value for the specified Value* that is true on the specified edge.
Florian Hahn8af01572017-09-28 11:09:22 +0000446 ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
447 BasicBlock *ToBB,
448 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000449
Philip Reames9db79482016-09-12 22:38:44 +0000450 /// Complete flush all previously computed values
451 void clear() {
452 TheCache.clear();
453 }
454
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000455 /// Printing the LazyValueInfo Analysis.
456 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
457 LazyValueInfoAnnotatedWriter Writer(this, DTree);
458 F.print(OS, &Writer);
Anna Thomase27b39a2017-03-22 19:27:12 +0000459 }
460
Philip Reames9db79482016-09-12 22:38:44 +0000461 /// This is part of the update interface to inform the cache
462 /// that a block has been deleted.
463 void eraseBlock(BasicBlock *BB) {
464 TheCache.eraseBlock(BB);
465 }
466
Sanjay Patel2a385e22015-01-09 16:47:20 +0000467 /// This is the update interface to inform the cache that an edge from
468 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000469 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000470
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000471 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
472 DominatorTree *DT = nullptr)
473 : AC(AC), DL(DL), DT(DT) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000474 };
475} // end anonymous namespace
476
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000477
Philip Reames92e5e1b2016-09-12 21:46:58 +0000478void LazyValueInfoImpl::solve() {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000479 SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
480 BlockValueStack.begin(), BlockValueStack.end());
481
482 unsigned processedCount = 0;
Owen Anderson6f060af2011-01-05 23:26:22 +0000483 while (!BlockValueStack.empty()) {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000484 processedCount++;
485 // Abort if we have to process too many values to get a result for this one.
486 // Because of the design of the overdefined cache currently being per-block
487 // to avoid naming-related issues (IE it wants to try to give different
488 // results for the same name in different blocks), overdefined results don't
489 // get cached globally, which in turn means we will often try to rediscover
490 // the same overdefined result again and again. Once something like
491 // PredicateInfo is used in LVI or CVP, we should be able to make the
492 // overdefined cache global, and remove this throttle.
493 if (processedCount > MaxProcessedPerValue) {
494 DEBUG(dbgs() << "Giving up on stack because we are getting too deep\n");
495 // Fill in the original values
496 while (!StartingStack.empty()) {
497 std::pair<BasicBlock *, Value *> &e = StartingStack.back();
498 TheCache.insertResult(e.second, e.first,
Florian Hahn8af01572017-09-28 11:09:22 +0000499 ValueLatticeElement::getOverdefined());
Daniel Berlin9c92a462017-02-08 15:22:52 +0000500 StartingStack.pop_back();
501 }
502 BlockValueSet.clear();
503 BlockValueStack.clear();
504 return;
505 }
Vitaly Buka9987d982017-02-09 09:28:05 +0000506 std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000507 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
508
Nuno Lopese6e04902012-06-28 01:16:18 +0000509 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000510 // The work item was completely processed.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000511 assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
Philip Reames9db79482016-09-12 22:38:44 +0000512 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000513 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000514
Philip Reames44456b82016-02-02 03:15:40 +0000515 DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000516 << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000517
Daniel Berlin9c92a462017-02-08 15:22:52 +0000518 BlockValueStack.pop_back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000519 BlockValueSet.erase(e);
520 } else {
521 // More work needs to be done before revisiting.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000522 assert(BlockValueStack.back() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000523 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000524 }
525}
526
Philip Reames92e5e1b2016-09-12 21:46:58 +0000527bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000528 // If already a constant, there is nothing to compute.
529 if (isa<Constant>(Val))
530 return true;
531
Philip Reames9db79482016-09-12 22:38:44 +0000532 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000533}
534
Florian Hahn8af01572017-09-28 11:09:22 +0000535ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
536 BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000537 // If already a constant, there is nothing to compute.
538 if (Constant *VC = dyn_cast<Constant>(Val))
Florian Hahn8af01572017-09-28 11:09:22 +0000539 return ValueLatticeElement::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000540
Philip Reames9db79482016-09-12 22:38:44 +0000541 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000542}
543
Florian Hahn8af01572017-09-28 11:09:22 +0000544static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
Philip Reameseb3e9da2015-10-29 03:57:17 +0000545 switch (BBI->getOpcode()) {
546 default: break;
547 case Instruction::Load:
548 case Instruction::Call:
549 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000550 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000551 if (isa<IntegerType>(BBI->getType())) {
Florian Hahn8af01572017-09-28 11:09:22 +0000552 return ValueLatticeElement::getRange(
553 getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000554 }
555 break;
556 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000557 // Nothing known - will be intersected with other facts
Florian Hahn8af01572017-09-28 11:09:22 +0000558 return ValueLatticeElement::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000559}
560
Philip Reames92e5e1b2016-09-12 21:46:58 +0000561bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000562 if (isa<Constant>(Val))
563 return true;
564
Philip Reames9db79482016-09-12 22:38:44 +0000565 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000566 // If we have a cached value, use that.
567 DEBUG(dbgs() << " reuse BB '" << BB->getName()
Philip Reames9db79482016-09-12 22:38:44 +0000568 << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000569
Hans Wennborg45172ac2014-11-25 17:23:05 +0000570 // Since we're reusing a cached value, we don't need to update the
571 // OverDefinedCache. The cache will have been properly updated whenever the
572 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000573 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000574 }
575
Hans Wennborg45172ac2014-11-25 17:23:05 +0000576 // Hold off inserting this value into the Cache in case we have to return
577 // false and come back later.
Florian Hahn8af01572017-09-28 11:09:22 +0000578 ValueLatticeElement Res;
Philip Reames05c435e2016-12-06 03:22:03 +0000579 if (!solveBlockValueImpl(Res, Val, BB))
580 // Work pushed, will revisit
581 return false;
582
583 TheCache.insertResult(Val, BB, Res);
584 return true;
585}
586
Florian Hahn8af01572017-09-28 11:09:22 +0000587bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
Philip Reames05c435e2016-12-06 03:22:03 +0000588 Value *Val, BasicBlock *BB) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000589
Chris Lattneraf025d32009-11-15 19:59:49 +0000590 Instruction *BBI = dyn_cast<Instruction>(Val);
Philip Reames05c435e2016-12-06 03:22:03 +0000591 if (!BBI || BBI->getParent() != BB)
592 return solveBlockValueNonLocal(Res, Val, BB);
Chris Lattner2c708562009-11-15 20:00:52 +0000593
Philip Reames05c435e2016-12-06 03:22:03 +0000594 if (PHINode *PN = dyn_cast<PHINode>(BBI))
595 return solveBlockValuePHINode(Res, PN, BB);
Owen Anderson80d19f02010-08-18 21:11:37 +0000596
Philip Reames05c435e2016-12-06 03:22:03 +0000597 if (auto *SI = dyn_cast<SelectInst>(BBI))
598 return solveBlockValueSelect(Res, SI, BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000599
Philip Reames2ab964e2016-04-27 01:02:25 +0000600 // If this value is a nonnull pointer, record it's range and bailout. Note
601 // that for all other pointer typed values, we terminate the search at the
602 // definition. We could easily extend this to look through geps, bitcasts,
603 // and the like to prove non-nullness, but it's not clear that's worth it
Craig Topper7ad13f22017-06-09 21:21:17 +0000604 // compile time wise. The context-insensitive value walk done inside
Nuno Lopes404f1062017-09-09 18:23:11 +0000605 // isKnownNonZero gets most of the profitable cases at much less expense.
Philip Reames2ab964e2016-04-27 01:02:25 +0000606 // This does mean that we have a sensativity to where the defining
607 // instruction is placed, even if it could legally be hoisted much higher.
608 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000609 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
Nuno Lopes404f1062017-09-09 18:23:11 +0000610 if (PT && isKnownNonZero(BBI, DL)) {
Florian Hahn8af01572017-09-28 11:09:22 +0000611 Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000612 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000613 }
Davide Italianobd543d02016-05-25 22:29:34 +0000614 if (BBI->getType()->isIntegerTy()) {
Craig Topper0e5f1092017-06-03 07:47:08 +0000615 if (auto *CI = dyn_cast<CastInst>(BBI))
616 return solveBlockValueCast(Res, CI, BB);
617
Philip Reames2ab964e2016-04-27 01:02:25 +0000618 BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
Philip Reames05c435e2016-12-06 03:22:03 +0000619 if (BO && isa<ConstantInt>(BO->getOperand(1)))
Craig Topper3778c892017-06-02 16:33:13 +0000620 return solveBlockValueBinaryOp(Res, BO, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000621 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000622
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000623 DEBUG(dbgs() << " compute BB '" << BB->getName()
624 << "' - unknown inst def found.\n");
625 Res = getFromRangeMetadata(BBI);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000626 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000627}
628
629static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
630 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
631 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000632 GetUnderlyingObject(L->getPointerOperand(),
633 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000634 }
635 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
636 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000637 GetUnderlyingObject(S->getPointerOperand(),
638 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000639 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000640 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
641 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000642
643 // FIXME: check whether it has a valuerange that excludes zero?
644 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
645 if (!Len || Len->isZero()) return false;
646
Eli Friedman7a5fc692011-05-31 20:40:16 +0000647 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000648 if (GetUnderlyingObject(MI->getRawDest(),
649 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000650 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000651 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000652 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000653 if (GetUnderlyingObject(MTI->getRawSource(),
654 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000655 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000656 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000657 return false;
658}
659
Philip Reames3f83dbe2016-04-27 00:30:55 +0000660/// Return true if the allocation associated with Val is ever dereferenced
661/// within the given basic block. This establishes the fact Val is not null,
662/// but does not imply that the memory at Val is dereferenceable. (Val may
663/// point off the end of the dereferenceable part of the object.)
664static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
665 assert(Val->getType()->isPointerTy());
666
667 const DataLayout &DL = BB->getModule()->getDataLayout();
668 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
669 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
670 // inside InstructionDereferencesPointer either.
671 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
672 for (Instruction &I : *BB)
673 if (InstructionDereferencesPointer(&I, UnderlyingVal))
674 return true;
675 return false;
676}
677
Florian Hahn8af01572017-09-28 11:09:22 +0000678bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000679 Value *Val, BasicBlock *BB) {
Florian Hahn8af01572017-09-28 11:09:22 +0000680 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000681
Nick Lewycky55a700b2010-12-18 01:00:40 +0000682 // If this is the entry block, we must be asking about an argument. The
683 // value is overdefined.
684 if (BB == &BB->getParent()->getEntryBlock()) {
685 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Craig Topper96d6ee852017-04-28 16:57:59 +0000686 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000687 // this particular block.
688 if (Val->getType()->isPointerTy() &&
Nuno Lopes404f1062017-09-09 18:23:11 +0000689 (isKnownNonZero(Val, DL) || isObjectDereferencedInBlock(Val, BB))) {
Chris Lattner229907c2011-07-18 04:54:35 +0000690 PointerType *PTy = cast<PointerType>(Val->getType());
Florian Hahn8af01572017-09-28 11:09:22 +0000691 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky55a700b2010-12-18 01:00:40 +0000692 } else {
Florian Hahn8af01572017-09-28 11:09:22 +0000693 Result = ValueLatticeElement::getOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000694 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000695 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000696 return true;
697 }
698
699 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000700 // result. If we encounter an unexplored predecessor, we eagerly explore it
701 // in a depth first manner. In practice, this has the effect of discovering
702 // paths we can't analyze eagerly without spending compile times analyzing
703 // other paths. This heuristic benefits from the fact that predecessors are
704 // frequently arranged such that dominating ones come first and we quickly
705 // find a path to function entry. TODO: We should consider explicitly
706 // canonicalizing to make this true rather than relying on this happy
707 // accident.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000708 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Florian Hahn8af01572017-09-28 11:09:22 +0000709 ValueLatticeElement EdgeResult;
Philip Reamesc80bd042017-02-07 00:25:24 +0000710 if (!getEdgeValue(Val, *PI, BB, EdgeResult))
711 // Explore that input, then return here
712 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000713
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000714 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000715
716 // If we hit overdefined, exit early. The BlockVals entry is already set
717 // to overdefined.
718 if (Result.isOverdefined()) {
719 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000720 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000721 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000722 // this particular block.
723 if (Val->getType()->isPointerTy() &&
724 isObjectDereferencedInBlock(Val, BB)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000725 PointerType *PTy = cast<PointerType>(Val->getType());
Florian Hahn8af01572017-09-28 11:09:22 +0000726 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky55a700b2010-12-18 01:00:40 +0000727 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000728
Owen Anderson64c2c572010-12-20 18:18:16 +0000729 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000730 return true;
731 }
732 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000733
734 // Return the merged value, which is more precise than 'overdefined'.
735 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000736 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000737 return true;
738}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000739
Florian Hahn8af01572017-09-28 11:09:22 +0000740bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
741 PHINode *PN, BasicBlock *BB) {
742 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000743
744 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000745 // result. See the comment about the chosen traversal order in
746 // solveBlockValueNonLocal; the same reasoning applies here.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000747 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
748 BasicBlock *PhiBB = PN->getIncomingBlock(i);
749 Value *PhiVal = PN->getIncomingValue(i);
Florian Hahn8af01572017-09-28 11:09:22 +0000750 ValueLatticeElement EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000751 // Note that we can provide PN as the context value to getEdgeValue, even
752 // though the results will be cached, because PN is the value being used as
753 // the cache key in the caller.
Philip Reamesc80bd042017-02-07 00:25:24 +0000754 if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
755 // Explore that input, then return here
756 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000757
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000758 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000759
760 // If we hit overdefined, exit early. The BlockVals entry is already set
761 // to overdefined.
762 if (Result.isOverdefined()) {
763 DEBUG(dbgs() << " compute BB '" << BB->getName()
Philip Reamesb7571042016-02-02 22:43:08 +0000764 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000765
Owen Anderson64c2c572010-12-20 18:18:16 +0000766 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000767 return true;
768 }
769 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000770
771 // Return the merged value, which is more precise than 'overdefined'.
772 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000773 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000774 return true;
775}
776
Florian Hahn8af01572017-09-28 11:09:22 +0000777static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
778 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000779
Philip Reamesd1f829d2016-02-02 21:57:37 +0000780// If we can determine a constraint on the value given conditions assumed by
781// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000782void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Florian Hahn8af01572017-09-28 11:09:22 +0000783 Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000784 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
785 if (!BBI)
786 return;
787
Hal Finkel8a9a7832017-01-11 13:24:24 +0000788 for (auto &AssumeVH : AC->assumptionsFor(Val)) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000789 if (!AssumeVH)
Chandler Carruth66b31302015-01-04 12:03:27 +0000790 continue;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000791 auto *I = cast<CallInst>(AssumeVH);
792 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000793 continue;
794
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000795 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +0000796 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000797
798 // If guards are not used in the module, don't spend time looking for them
799 auto *GuardDecl = BBI->getModule()->getFunction(
800 Intrinsic::getName(Intrinsic::experimental_guard));
801 if (!GuardDecl || GuardDecl->use_empty())
802 return;
803
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000804 for (Instruction &I : make_range(BBI->getIterator().getReverse(),
805 BBI->getParent()->rend())) {
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000806 Value *Cond = nullptr;
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000807 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
808 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000809 }
Hal Finkel7e184492014-09-07 20:29:59 +0000810}
811
Florian Hahn8af01572017-09-28 11:09:22 +0000812bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
813 SelectInst *SI, BasicBlock *BB) {
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000814
815 // Recurse on our inputs if needed
816 if (!hasBlockValue(SI->getTrueValue(), BB)) {
817 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
818 return false;
Florian Hahn8af01572017-09-28 11:09:22 +0000819 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000820 return true;
821 }
Florian Hahn8af01572017-09-28 11:09:22 +0000822 ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000823 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
824 // extra slots in the table if we can.
825 if (TrueVal.isOverdefined()) {
Florian Hahn8af01572017-09-28 11:09:22 +0000826 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000827 return true;
828 }
829
830 if (!hasBlockValue(SI->getFalseValue(), BB)) {
831 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
832 return false;
Florian Hahn8af01572017-09-28 11:09:22 +0000833 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000834 return true;
835 }
Florian Hahn8af01572017-09-28 11:09:22 +0000836 ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000837 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
838 // extra slots in the table if we can.
839 if (FalseVal.isOverdefined()) {
Florian Hahn8af01572017-09-28 11:09:22 +0000840 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000841 return true;
842 }
843
Philip Reamesadf0e352016-02-26 22:53:59 +0000844 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +0000845 const ConstantRange &TrueCR = TrueVal.getConstantRange();
846 const ConstantRange &FalseCR = FalseVal.getConstantRange();
Philip Reamesadf0e352016-02-26 22:53:59 +0000847 Value *LHS = nullptr;
848 Value *RHS = nullptr;
849 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
850 // Is this a min specifically of our two inputs? (Avoid the risk of
851 // ValueTracking getting smarter looking back past our immediate inputs.)
852 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
853 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reamesb2949622016-12-06 02:54:16 +0000854 ConstantRange ResultCR = [&]() {
855 switch (SPR.Flavor) {
856 default:
857 llvm_unreachable("unexpected minmax type!");
858 case SPF_SMIN: /// Signed minimum
859 return TrueCR.smin(FalseCR);
860 case SPF_UMIN: /// Unsigned minimum
861 return TrueCR.umin(FalseCR);
862 case SPF_SMAX: /// Signed maximum
863 return TrueCR.smax(FalseCR);
864 case SPF_UMAX: /// Unsigned maximum
865 return TrueCR.umax(FalseCR);
866 };
867 }();
Florian Hahn8af01572017-09-28 11:09:22 +0000868 BBLV = ValueLatticeElement::getRange(ResultCR);
Philip Reamesb2949622016-12-06 02:54:16 +0000869 return true;
Philip Reamesadf0e352016-02-26 22:53:59 +0000870 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000871
Philip Reamesadf0e352016-02-26 22:53:59 +0000872 // TODO: ABS, NABS from the SelectPatternResult
873 }
874
Philip Reames854a84c2016-02-12 00:09:18 +0000875 // Can we constrain the facts about the true and false values by using the
876 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
877 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000878 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000879 TrueVal = intersect(TrueVal,
880 getValueFromCondition(SI->getTrueValue(), Cond, true));
881 FalseVal = intersect(FalseVal,
882 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +0000883
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000884 // Handle clamp idioms such as:
885 // %24 = constantrange<0, 17>
886 // %39 = icmp eq i32 %24, 0
887 // %40 = add i32 %24, -1
888 // %siv.next = select i1 %39, i32 16, i32 %40
889 // %siv.next = constantrange<0, 17> not <-1, 17>
890 // In general, this can handle any clamp idiom which tests the edge
891 // condition via an equality or inequality.
892 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +0000893 ICmpInst::Predicate Pred = ICI->getPredicate();
894 Value *A = ICI->getOperand(0);
895 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
896 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
897 assert(A->getType() == B->getType());
898 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
899 };
900 // See if either input is A + C2, subject to the constraint from the
901 // condition that A != C when that input is used. We can assume that
902 // that input doesn't include C + C2.
903 ConstantInt *CIAdded;
904 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +0000905 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +0000906 case ICmpInst::ICMP_EQ:
907 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
908 m_ConstantInt(CIAdded)))) {
909 auto ResNot = addConstants(CIBase, CIAdded);
910 FalseVal = intersect(FalseVal,
Florian Hahn8af01572017-09-28 11:09:22 +0000911 ValueLatticeElement::getNot(ResNot));
Philip Reamesadf0e352016-02-26 22:53:59 +0000912 }
913 break;
914 case ICmpInst::ICMP_NE:
915 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
916 m_ConstantInt(CIAdded)))) {
917 auto ResNot = addConstants(CIBase, CIAdded);
918 TrueVal = intersect(TrueVal,
Florian Hahn8af01572017-09-28 11:09:22 +0000919 ValueLatticeElement::getNot(ResNot));
Philip Reamesadf0e352016-02-26 22:53:59 +0000920 }
921 break;
922 };
923 }
924 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000925
Florian Hahn8af01572017-09-28 11:09:22 +0000926 ValueLatticeElement Result; // Start Undefined.
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000927 Result.mergeIn(TrueVal, DL);
928 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000929 BBLV = Result;
930 return true;
931}
932
Florian Hahn8af01572017-09-28 11:09:22 +0000933bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
Craig Topper0e5f1092017-06-03 07:47:08 +0000934 CastInst *CI,
935 BasicBlock *BB) {
936 if (!CI->getOperand(0)->getType()->isSized()) {
Philip Reamese5030e82016-04-26 22:52:30 +0000937 // Without knowing how wide the input is, we can't analyze it in any useful
938 // way.
Florian Hahn8af01572017-09-28 11:09:22 +0000939 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamese5030e82016-04-26 22:52:30 +0000940 return true;
941 }
Philip Reamesf105db42016-04-26 23:27:33 +0000942
943 // Filter out casts we don't know how to reason about before attempting to
944 // recurse on our operand. This can cut a long search short if we know we're
945 // not going to be able to get any useful information anways.
Craig Topper0e5f1092017-06-03 07:47:08 +0000946 switch (CI->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +0000947 case Instruction::Trunc:
948 case Instruction::SExt:
949 case Instruction::ZExt:
950 case Instruction::BitCast:
951 break;
952 default:
953 // Unhandled instructions are overdefined.
954 DEBUG(dbgs() << " compute BB '" << BB->getName()
955 << "' - overdefined (unknown cast).\n");
Florian Hahn8af01572017-09-28 11:09:22 +0000956 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +0000957 return true;
958 }
959
Philip Reames38c87c22016-04-26 21:48:16 +0000960 // Figure out the range of the LHS. If that fails, we still apply the
961 // transfer rule on the full set since we may be able to locally infer
962 // interesting facts.
Craig Topper0e5f1092017-06-03 07:47:08 +0000963 if (!hasBlockValue(CI->getOperand(0), BB))
964 if (pushBlockValue(std::make_pair(BB, CI->getOperand(0))))
Philip Reames38c87c22016-04-26 21:48:16 +0000965 // More work to do before applying this transfer rule.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000966 return false;
Philip Reames38c87c22016-04-26 21:48:16 +0000967
968 const unsigned OperandBitWidth =
Craig Topper0e5f1092017-06-03 07:47:08 +0000969 DL.getTypeSizeInBits(CI->getOperand(0)->getType());
Philip Reames38c87c22016-04-26 21:48:16 +0000970 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
Craig Topper0e5f1092017-06-03 07:47:08 +0000971 if (hasBlockValue(CI->getOperand(0), BB)) {
Florian Hahn8af01572017-09-28 11:09:22 +0000972 ValueLatticeElement LHSVal = getBlockValue(CI->getOperand(0), BB);
Craig Topper0e5f1092017-06-03 07:47:08 +0000973 intersectAssumeOrGuardBlockValueConstantRange(CI->getOperand(0), LHSVal,
974 CI);
Philip Reames38c87c22016-04-26 21:48:16 +0000975 if (LHSVal.isConstantRange())
976 LHSRange = LHSVal.getConstantRange();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000977 }
978
Craig Toppera803d5b2017-06-03 07:47:14 +0000979 const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
Philip Reames66715772016-04-25 18:30:31 +0000980
981 // NOTE: We're currently limited by the set of operations that ConstantRange
982 // can evaluate symbolically. Enhancing that set will allows us to analyze
983 // more definitions.
Florian Hahn8af01572017-09-28 11:09:22 +0000984 BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
985 ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +0000986 return true;
987}
988
Florian Hahn8af01572017-09-28 11:09:22 +0000989bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
990 BinaryOperator *BO,
991 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +0000992
Craig Topper3778c892017-06-02 16:33:13 +0000993 assert(BO->getOperand(0)->getType()->isSized() &&
Philip Reames053c2a62016-04-26 23:10:35 +0000994 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +0000995
996 // Filter out operators we don't know how to reason about before attempting to
997 // recurse on our operand(s). This can cut a long search short if we know
Craig Topper84a9f162017-06-02 16:21:13 +0000998 // we're not going to be able to get any useful information anyways.
Craig Topper3778c892017-06-02 16:33:13 +0000999 switch (BO->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +00001000 case Instruction::Add:
1001 case Instruction::Sub:
1002 case Instruction::Mul:
1003 case Instruction::UDiv:
1004 case Instruction::Shl:
1005 case Instruction::LShr:
1006 case Instruction::And:
1007 case Instruction::Or:
1008 // continue into the code below
1009 break;
1010 default:
1011 // Unhandled instructions are overdefined.
1012 DEBUG(dbgs() << " compute BB '" << BB->getName()
1013 << "' - overdefined (unknown binary operator).\n");
Florian Hahn8af01572017-09-28 11:09:22 +00001014 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001015 return true;
1016 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001017
Philip Reames053c2a62016-04-26 23:10:35 +00001018 // Figure out the range of the LHS. If that fails, use a conservative range,
1019 // but apply the transfer rule anyways. This lets us pick up facts from
1020 // expressions like "and i32 (call i32 @foo()), 32"
Craig Topper3778c892017-06-02 16:33:13 +00001021 if (!hasBlockValue(BO->getOperand(0), BB))
1022 if (pushBlockValue(std::make_pair(BB, BO->getOperand(0))))
Philip Reames053c2a62016-04-26 23:10:35 +00001023 // More work to do before applying this transfer rule.
1024 return false;
1025
1026 const unsigned OperandBitWidth =
Craig Topper3778c892017-06-02 16:33:13 +00001027 DL.getTypeSizeInBits(BO->getOperand(0)->getType());
Philip Reames053c2a62016-04-26 23:10:35 +00001028 ConstantRange LHSRange = ConstantRange(OperandBitWidth);
Craig Topper3778c892017-06-02 16:33:13 +00001029 if (hasBlockValue(BO->getOperand(0), BB)) {
Florian Hahn8af01572017-09-28 11:09:22 +00001030 ValueLatticeElement LHSVal = getBlockValue(BO->getOperand(0), BB);
Craig Topper3778c892017-06-02 16:33:13 +00001031 intersectAssumeOrGuardBlockValueConstantRange(BO->getOperand(0), LHSVal,
1032 BO);
Philip Reames053c2a62016-04-26 23:10:35 +00001033 if (LHSVal.isConstantRange())
1034 LHSRange = LHSVal.getConstantRange();
Philip Reames66715772016-04-25 18:30:31 +00001035 }
Philip Reames66715772016-04-25 18:30:31 +00001036
Craig Topper3778c892017-06-02 16:33:13 +00001037 ConstantInt *RHS = cast<ConstantInt>(BO->getOperand(1));
Philip Reames66715772016-04-25 18:30:31 +00001038 ConstantRange RHSRange = ConstantRange(RHS->getValue());
1039
Owen Anderson80d19f02010-08-18 21:11:37 +00001040 // NOTE: We're currently limited by the set of operations that ConstantRange
1041 // can evaluate symbolically. Enhancing that set will allows us to analyze
1042 // more definitions.
Craig Topper3778c892017-06-02 16:33:13 +00001043 Instruction::BinaryOps BinOp = BO->getOpcode();
Florian Hahn8af01572017-09-28 11:09:22 +00001044 BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001045 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001046}
1047
Florian Hahn8af01572017-09-28 11:09:22 +00001048static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1049 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001050 Value *LHS = ICI->getOperand(0);
1051 Value *RHS = ICI->getOperand(1);
1052 CmpInst::Predicate Predicate = ICI->getPredicate();
1053
1054 if (isa<Constant>(RHS)) {
1055 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001056 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001057 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001058 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Florian Hahn8af01572017-09-28 11:09:22 +00001059 return ValueLatticeElement::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001060 else
Florian Hahn8af01572017-09-28 11:09:22 +00001061 return ValueLatticeElement::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001062 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001063 }
Hal Finkel7e184492014-09-07 20:29:59 +00001064
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001065 if (!Val->getType()->isIntegerTy())
Florian Hahn8af01572017-09-28 11:09:22 +00001066 return ValueLatticeElement::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001067
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001068 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1069 // range of Val guaranteed by the condition. Recognize comparisons in the from
1070 // of:
1071 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001072 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001073 // The latter is the range checking idiom that InstCombine produces. Subtract
1074 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001075
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001076 // Val or (add Val, Offset) can be on either hand of the comparison
1077 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1078 std::swap(LHS, RHS);
1079 Predicate = CmpInst::getSwappedPredicate(Predicate);
1080 }
Hal Finkel7e184492014-09-07 20:29:59 +00001081
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001082 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001083 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001084 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001085
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001086 if (LHS == Val || Offset) {
1087 // Calculate the range of values that are allowed by the comparison
1088 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1089 /*isFullSet=*/true);
1090 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1091 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001092 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1093 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1094 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001095
1096 // If we're interested in the false dest, invert the condition
1097 CmpInst::Predicate Pred =
1098 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1099 ConstantRange TrueValues =
1100 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1101
1102 if (Offset) // Apply the offset from above.
1103 TrueValues = TrueValues.subtract(Offset->getValue());
1104
Florian Hahn8af01572017-09-28 11:09:22 +00001105 return ValueLatticeElement::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001106 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001107
Florian Hahn8af01572017-09-28 11:09:22 +00001108 return ValueLatticeElement::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001109}
1110
Florian Hahn8af01572017-09-28 11:09:22 +00001111static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001112getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001113 DenseMap<Value*, ValueLatticeElement> &Visited);
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001114
Florian Hahn8af01572017-09-28 11:09:22 +00001115static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001116getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001117 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001118 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1119 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1120
1121 // Handle conditions in the form of (cond1 && cond2), we know that on the
Craig Topperb60f8662017-06-23 01:08:16 +00001122 // true dest path both of the conditions hold. Similarly for conditions of
1123 // the form (cond1 || cond2), we know that on the false dest path neither
1124 // condition holds.
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001125 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
Craig Topperb60f8662017-06-23 01:08:16 +00001126 if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
1127 (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
Florian Hahn8af01572017-09-28 11:09:22 +00001128 return ValueLatticeElement::getOverdefined();
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001129
1130 auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
1131 auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
1132 return intersect(RHS, LHS);
1133}
1134
Florian Hahn8af01572017-09-28 11:09:22 +00001135static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001136getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001137 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001138 auto I = Visited.find(Cond);
1139 if (I != Visited.end())
1140 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001141
1142 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1143 Visited[Cond] = Result;
1144 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001145}
1146
Florian Hahn8af01572017-09-28 11:09:22 +00001147ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
1148 bool isTrueDest) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001149 assert(Cond && "precondition");
Florian Hahn8af01572017-09-28 11:09:22 +00001150 DenseMap<Value*, ValueLatticeElement> Visited;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001151 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1152}
1153
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001154// Return true if Usr has Op as an operand, otherwise false.
1155static bool usesOperand(User *Usr, Value *Op) {
1156 return find(Usr->operands(), Op) != Usr->op_end();
1157}
1158
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001159// Return true if the instruction type of Val is supported by
1160// constantFoldUser(). Currently CastInst and BinaryOperator only. Call this
1161// before calling constantFoldUser() to find out if it's even worth attempting
1162// to call it.
1163static bool isOperationFoldable(User *Usr) {
1164 return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
1165}
1166
1167// Check if Usr can be simplified to an integer constant when the value of one
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001168// of its operands Op is an integer constant OpConstVal. If so, return it as an
1169// lattice value range with a single element or otherwise return an overdefined
1170// lattice value.
Florian Hahn8af01572017-09-28 11:09:22 +00001171static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
1172 const APInt &OpConstVal,
1173 const DataLayout &DL) {
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001174 assert(isOperationFoldable(Usr) && "Precondition");
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001175 Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001176 // Check if Usr can be simplified to a constant.
1177 if (auto *CI = dyn_cast<CastInst>(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001178 assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
1179 if (auto *C = dyn_cast_or_null<ConstantInt>(
1180 SimplifyCastInst(CI->getOpcode(), OpConst,
1181 CI->getDestTy(), DL))) {
Florian Hahn8af01572017-09-28 11:09:22 +00001182 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001183 }
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001184 } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001185 bool Op0Match = BO->getOperand(0) == Op;
1186 bool Op1Match = BO->getOperand(1) == Op;
1187 assert((Op0Match || Op1Match) &&
1188 "Operand 0 nor Operand 1 isn't a match");
1189 Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
1190 Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
1191 if (auto *C = dyn_cast_or_null<ConstantInt>(
1192 SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
Florian Hahn8af01572017-09-28 11:09:22 +00001193 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001194 }
1195 }
Florian Hahn8af01572017-09-28 11:09:22 +00001196 return ValueLatticeElement::getOverdefined();
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001197}
1198
Nuno Lopese6e04902012-06-28 01:16:18 +00001199/// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001200/// Val is not constrained on the edge. Result is unspecified if return value
1201/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001202static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
Florian Hahn8af01572017-09-28 11:09:22 +00001203 BasicBlock *BBTo, ValueLatticeElement &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001204 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001205 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001206 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1207 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001208 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001209 if (BI->isConditional() &&
1210 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1211 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1212 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1213 "BBTo isn't a successor of BBFrom");
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001214 Value *Condition = BI->getCondition();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001215
Chris Lattner19019ea2009-11-11 22:48:44 +00001216 // If V is the condition of the branch itself, then we know exactly what
1217 // it is.
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001218 if (Condition == Val) {
Florian Hahn8af01572017-09-28 11:09:22 +00001219 Result = ValueLatticeElement::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001220 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001221 return true;
1222 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001223
Chris Lattner19019ea2009-11-11 22:48:44 +00001224 // If the condition of the branch is an equality comparison, we may be
1225 // able to infer the value.
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001226 Result = getValueFromCondition(Val, Condition, isTrueDest);
1227 if (!Result.isOverdefined())
1228 return true;
1229
1230 if (User *Usr = dyn_cast<User>(Val)) {
1231 assert(Result.isOverdefined() && "Result isn't overdefined");
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001232 // Check with isOperationFoldable() first to avoid linearly iterating
1233 // over the operands unnecessarily which can be expensive for
1234 // instructions with many operands.
1235 if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001236 const DataLayout &DL = BBTo->getModule()->getDataLayout();
1237 if (usesOperand(Usr, Condition)) {
1238 // If Val has Condition as an operand and Val can be folded into a
1239 // constant with either Condition == true or Condition == false,
1240 // propagate the constant.
1241 // eg.
1242 // ; %Val is true on the edge to %then.
1243 // %Val = and i1 %Condition, true.
1244 // br %Condition, label %then, label %else
1245 APInt ConditionVal(1, isTrueDest ? 1 : 0);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001246 Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001247 } else {
1248 // If one of Val's operand has an inferred value, we may be able to
1249 // infer the value of Val.
1250 // eg.
1251 // ; %Val is 94 on the edge to %then.
1252 // %Val = add i8 %Op, 1
1253 // %Condition = icmp eq i8 %Op, 93
1254 // br i1 %Condition, label %then, label %else
1255 for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
1256 Value *Op = Usr->getOperand(i);
Florian Hahn8af01572017-09-28 11:09:22 +00001257 ValueLatticeElement OpLatticeVal =
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001258 getValueFromCondition(Op, Condition, isTrueDest);
1259 if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001260 Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001261 break;
1262 }
1263 }
1264 }
1265 }
1266 }
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001267 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001268 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001269 }
1270 }
Chris Lattner77358782009-11-15 20:02:12 +00001271
1272 // If the edge was formed by a switch on the value, then we may know exactly
1273 // what it is.
1274 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001275 Value *Condition = SI->getCondition();
1276 if (!isa<IntegerType>(Val->getType()))
Nuno Lopes8650fb82012-06-28 16:13:37 +00001277 return false;
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001278 bool ValUsesConditionAndMayBeFoldable = false;
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001279 if (Condition != Val) {
1280 // Check if Val has Condition as an operand.
1281 if (User *Usr = dyn_cast<User>(Val))
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001282 ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
1283 usesOperand(Usr, Condition);
1284 if (!ValUsesConditionAndMayBeFoldable)
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001285 return false;
1286 }
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001287 assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001288 "Condition != Val nor Val doesn't use Condition");
Nuno Lopes8650fb82012-06-28 16:13:37 +00001289
1290 bool DefaultCase = SI->getDefaultDest() == BBTo;
1291 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1292 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1293
Chandler Carruth927d8e62017-04-12 07:27:28 +00001294 for (auto Case : SI->cases()) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001295 APInt CaseValue = Case.getCaseValue()->getValue();
1296 ConstantRange EdgeVal(CaseValue);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001297 if (ValUsesConditionAndMayBeFoldable) {
1298 User *Usr = cast<User>(Val);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001299 const DataLayout &DL = BBTo->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001300 ValueLatticeElement EdgeLatticeVal =
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001301 constantFoldUser(Usr, Condition, CaseValue, DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001302 if (EdgeLatticeVal.isOverdefined())
1303 return false;
1304 EdgeVal = EdgeLatticeVal.getConstantRange();
1305 }
Manman Renf3fedb62012-09-05 23:45:58 +00001306 if (DefaultCase) {
1307 // It is possible that the default destination is the destination of
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001308 // some cases. We cannot perform difference for those cases.
1309 // We know Condition != CaseValue in BBTo. In some cases we can use
1310 // this to infer Val == f(Condition) is != f(CaseValue). For now, we
1311 // only do this when f is identity (i.e. Val == Condition), but we
1312 // should be able to do this for any injective f.
1313 if (Case.getCaseSuccessor() != BBTo && Condition == Val)
Manman Renf3fedb62012-09-05 23:45:58 +00001314 EdgesVals = EdgesVals.difference(EdgeVal);
Chandler Carruth927d8e62017-04-12 07:27:28 +00001315 } else if (Case.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001316 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001317 }
Florian Hahn8af01572017-09-28 11:09:22 +00001318 Result = ValueLatticeElement::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001319 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001320 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001321 return false;
1322}
1323
Sanjay Patel938e2792015-01-09 16:35:37 +00001324/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
1325/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001326bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Florian Hahn8af01572017-09-28 11:09:22 +00001327 BasicBlock *BBTo,
1328 ValueLatticeElement &Result,
Xin Tong68ea9aa2017-02-24 20:59:26 +00001329 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001330 // If already a constant, there is nothing to compute.
1331 if (Constant *VC = dyn_cast<Constant>(Val)) {
Florian Hahn8af01572017-09-28 11:09:22 +00001332 Result = ValueLatticeElement::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001333 return true;
1334 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001335
Florian Hahn8af01572017-09-28 11:09:22 +00001336 ValueLatticeElement LocalResult;
Philip Reames44456b82016-02-02 03:15:40 +00001337 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1338 // If we couldn't constrain the value on the edge, LocalResult doesn't
1339 // provide any information.
Florian Hahn8af01572017-09-28 11:09:22 +00001340 LocalResult = ValueLatticeElement::getOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001341
Philip Reames44456b82016-02-02 03:15:40 +00001342 if (hasSingleValue(LocalResult)) {
1343 // Can't get any more precise here
1344 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001345 return true;
1346 }
1347
1348 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001349 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1350 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001351 // No new information.
1352 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001353 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001354 }
1355
Philip Reames44456b82016-02-02 03:15:40 +00001356 // Try to intersect ranges of the BB and the constraint on the edge.
Florian Hahn8af01572017-09-28 11:09:22 +00001357 ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001358 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1359 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001360 // We can use the context instruction (generically the ultimate instruction
1361 // the calling pass is trying to simplify) here, even though the result of
1362 // this function is generally cached when called from the solve* functions
1363 // (and that cached result might be used with queries using a different
1364 // context instruction), because when this function is called from the solve*
1365 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001366 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001367 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001368 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001369
1370 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001371 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001372}
1373
Florian Hahn8af01572017-09-28 11:09:22 +00001374ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
1375 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001376 DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001377 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001378
Hans Wennborg45172ac2014-11-25 17:23:05 +00001379 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001380 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001381 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001382 solve();
1383 }
Florian Hahn8af01572017-09-28 11:09:22 +00001384 ValueLatticeElement Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001385 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001386
1387 DEBUG(dbgs() << " Result = " << Result << "\n");
1388 return Result;
1389}
1390
Florian Hahn8af01572017-09-28 11:09:22 +00001391ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Hal Finkel7e184492014-09-07 20:29:59 +00001392 DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
1393 << CxtI->getName() << "'\n");
1394
Philip Reamesbb781b42016-02-10 21:46:32 +00001395 if (auto *C = dyn_cast<Constant>(V))
Florian Hahn8af01572017-09-28 11:09:22 +00001396 return ValueLatticeElement::get(C);
Philip Reamesbb781b42016-02-10 21:46:32 +00001397
Florian Hahn8af01572017-09-28 11:09:22 +00001398 ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001399 if (auto *I = dyn_cast<Instruction>(V))
1400 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001401 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001402
David Greene37e98092009-12-23 20:43:58 +00001403 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001404 return Result;
1405}
Chris Lattner19019ea2009-11-11 22:48:44 +00001406
Florian Hahn8af01572017-09-28 11:09:22 +00001407ValueLatticeElement LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001408getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1409 Instruction *CxtI) {
David Greene37e98092009-12-23 20:43:58 +00001410 DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
Chris Lattneraf025d32009-11-15 19:59:49 +00001411 << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001412
Florian Hahn8af01572017-09-28 11:09:22 +00001413 ValueLatticeElement Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001414 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001415 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001416 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001417 (void)WasFastQuery;
1418 assert(WasFastQuery && "More work to do after problem solved?");
1419 }
1420
David Greene37e98092009-12-23 20:43:58 +00001421 DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001422 return Result;
1423}
1424
Philip Reames92e5e1b2016-09-12 21:46:58 +00001425void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames9db79482016-09-12 22:38:44 +00001426 BasicBlock *NewSucc) {
1427 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001428}
1429
Chris Lattneraf025d32009-11-15 19:59:49 +00001430//===----------------------------------------------------------------------===//
1431// LazyValueInfo Impl
1432//===----------------------------------------------------------------------===//
1433
Philip Reames92e5e1b2016-09-12 21:46:58 +00001434/// This lazily constructs the LazyValueInfoImpl.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001435static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1436 const DataLayout *DL,
Philip Reames92e5e1b2016-09-12 21:46:58 +00001437 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001438 if (!PImpl) {
1439 assert(DL && "getCache() called with a null DataLayout");
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001440 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001441 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001442 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001443}
1444
Sean Silva687019f2016-06-13 22:01:25 +00001445bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001446 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001447 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001448
1449 DominatorTreeWrapperPass *DTWP =
1450 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001451 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1452 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001453
Sean Silva687019f2016-06-13 22:01:25 +00001454 if (Info.PImpl)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001455 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001456
Owen Anderson208636f2010-08-18 18:39:01 +00001457 // Fully lazy.
1458 return false;
1459}
1460
Sean Silva687019f2016-06-13 22:01:25 +00001461void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001462 AU.setPreservesAll();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001463 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001464 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001465}
1466
Sean Silva687019f2016-06-13 22:01:25 +00001467LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1468
1469LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1470
Chris Lattneraf025d32009-11-15 19:59:49 +00001471void LazyValueInfo::releaseMemory() {
1472 // If the cache was allocated, free it.
1473 if (PImpl) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001474 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001475 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001476 }
1477}
1478
Chandler Carrutha504f2b2017-01-23 06:35:12 +00001479bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1480 FunctionAnalysisManager::Invalidator &Inv) {
1481 // We need to invalidate if we have either failed to preserve this analyses
1482 // result directly or if any of its dependencies have been invalidated.
1483 auto PAC = PA.getChecker<LazyValueAnalysis>();
1484 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1485 (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1486 return true;
1487
1488 return false;
1489}
1490
Sean Silva687019f2016-06-13 22:01:25 +00001491void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1492
Florian Hahn8af01572017-09-28 11:09:22 +00001493LazyValueInfo LazyValueAnalysis::run(Function &F,
1494 FunctionAnalysisManager &FAM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001495 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
Sean Silva687019f2016-06-13 22:01:25 +00001496 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1497 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1498
Anna Thomasa10e3e42017-03-12 14:06:41 +00001499 return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
Sean Silva687019f2016-06-13 22:01:25 +00001500}
1501
Wei Mif160e342016-09-15 06:28:34 +00001502/// Returns true if we can statically tell that this value will never be a
1503/// "useful" constant. In practice, this means we've got something like an
1504/// alloca or a malloc call for which a comparison against a constant can
1505/// only be guarding dead code. Note that we are potentially giving up some
1506/// precision in dead code (a constant result) in favour of avoiding a
1507/// expensive search for a easily answered common query.
1508static bool isKnownNonConstant(Value *V) {
1509 V = V->stripPointerCasts();
1510 // The return val of alloc cannot be a Constant.
1511 if (isa<AllocaInst>(V))
1512 return true;
1513 return false;
1514}
1515
Hal Finkel7e184492014-09-07 20:29:59 +00001516Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1517 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001518 // Bail out early if V is known not to be a Constant.
1519 if (isKnownNonConstant(V))
1520 return nullptr;
1521
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001522 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001523 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001524 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001525
Chris Lattner19019ea2009-11-11 22:48:44 +00001526 if (Result.isConstant())
1527 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001528 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001529 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001530 if (const APInt *SingleVal = CR.getSingleElement())
1531 return ConstantInt::get(V->getContext(), *SingleVal);
1532 }
Craig Topper9f008862014-04-15 04:59:12 +00001533 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001534}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001535
John Regehre1c481d2016-05-02 19:58:00 +00001536ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001537 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001538 assert(V->getType()->isIntegerTy());
1539 unsigned Width = V->getType()->getIntegerBitWidth();
1540 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001541 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001542 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001543 if (Result.isUndefined())
1544 return ConstantRange(Width, /*isFullSet=*/false);
1545 if (Result.isConstantRange())
1546 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001547 // We represent ConstantInt constants as constant ranges but other kinds
1548 // of integer constants, i.e. ConstantExpr will be tagged as constants
1549 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1550 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001551 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001552}
1553
Sanjay Patel2a385e22015-01-09 16:47:20 +00001554/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001555/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001556Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001557 BasicBlock *ToBB,
1558 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001559 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001560 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001561 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001562
Chris Lattnerd5e25432009-11-12 01:29:10 +00001563 if (Result.isConstant())
1564 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001565 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001566 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001567 if (const APInt *SingleVal = CR.getSingleElement())
1568 return ConstantInt::get(V->getContext(), *SingleVal);
1569 }
Craig Topper9f008862014-04-15 04:59:12 +00001570 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001571}
1572
Craig Topper2c20c422017-06-23 05:41:35 +00001573ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
1574 BasicBlock *FromBB,
1575 BasicBlock *ToBB,
1576 Instruction *CxtI) {
1577 unsigned Width = V->getType()->getIntegerBitWidth();
1578 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001579 ValueLatticeElement Result =
Craig Topper2c20c422017-06-23 05:41:35 +00001580 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1581
1582 if (Result.isUndefined())
1583 return ConstantRange(Width, /*isFullSet=*/false);
1584 if (Result.isConstantRange())
1585 return Result.getConstantRange();
1586 // We represent ConstantInt constants as constant ranges but other kinds
1587 // of integer constants, i.e. ConstantExpr will be tagged as constants
1588 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1589 "ConstantInt value must be represented as constantrange");
1590 return ConstantRange(Width, /*isFullSet=*/true);
1591}
1592
Florian Hahn8af01572017-09-28 11:09:22 +00001593static LazyValueInfo::Tristate
1594getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
1595 const DataLayout &DL, TargetLibraryInfo *TLI) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001596 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001597 Constant *Res = nullptr;
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001598 if (Val.isConstant()) {
1599 Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001600 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001601 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1602 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001603 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001604
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001605 if (Val.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001606 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001607 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001608
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001609 const ConstantRange &CR = Val.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001610 if (Pred == ICmpInst::ICMP_EQ) {
1611 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001612 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001613
Craig Topper79452482017-06-07 00:58:09 +00001614 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001615 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001616 } else if (Pred == ICmpInst::ICMP_NE) {
1617 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001618 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001619
Craig Topper79452482017-06-07 00:58:09 +00001620 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001621 return LazyValueInfo::False;
Craig Topper31ce4ec2017-06-09 16:16:20 +00001622 } else {
1623 // Handle more complex predicates.
1624 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1625 (ICmpInst::Predicate)Pred, CI->getValue());
1626 if (TrueValues.contains(CR))
1627 return LazyValueInfo::True;
1628 if (TrueValues.inverse().contains(CR))
1629 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001630 }
Hal Finkel7e184492014-09-07 20:29:59 +00001631 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001632 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001633
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001634 if (Val.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001635 // If this is an equality comparison, we can try to fold it knowing that
1636 // "V != C1".
1637 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001638 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001639 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001640 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001641 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001642 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001643 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001644 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001645 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001646 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001647 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001648 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001649 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001650 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001651 }
Hal Finkel7e184492014-09-07 20:29:59 +00001652 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001653 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001654
Hal Finkel7e184492014-09-07 20:29:59 +00001655 return LazyValueInfo::Unknown;
1656}
1657
Sanjay Patel2a385e22015-01-09 16:47:20 +00001658/// Determine whether the specified value comparison with a constant is known to
1659/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001660LazyValueInfo::Tristate
1661LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1662 BasicBlock *FromBB, BasicBlock *ToBB,
1663 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001664 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001665 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001666 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001667
1668 return getPredicateResult(Pred, C, Result, DL, TLI);
1669}
1670
1671LazyValueInfo::Tristate
1672LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1673 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001674 // Is or is not NonNull are common predicates being queried. If
Nuno Lopes404f1062017-09-09 18:23:11 +00001675 // isKnownNonZero can tell us the result of the predicate, we can
Wei Mif160e342016-09-15 06:28:34 +00001676 // return it quickly. But this is only a fastpath, and falling
1677 // through would still be correct.
Nuno Lopes404f1062017-09-09 18:23:11 +00001678 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Wei Mif160e342016-09-15 06:28:34 +00001679 if (V->getType()->isPointerTy() && C->isNullValue() &&
Nuno Lopes404f1062017-09-09 18:23:11 +00001680 isKnownNonZero(V->stripPointerCasts(), DL)) {
Wei Mif160e342016-09-15 06:28:34 +00001681 if (Pred == ICmpInst::ICMP_EQ)
1682 return LazyValueInfo::False;
1683 else if (Pred == ICmpInst::ICMP_NE)
1684 return LazyValueInfo::True;
1685 }
Florian Hahn8af01572017-09-28 11:09:22 +00001686 ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001687 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1688 if (Ret != Unknown)
1689 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001690
Philip Reamesaeefae02015-11-04 01:47:04 +00001691 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1692 // LVI as a whole tries to compute a lattice value which is conservatively
1693 // correct at a given location. In this case, we have a predicate which we
1694 // weren't able to prove about the merged result, and we're pushing that
1695 // predicate back along each incoming edge to see if we can prove it
1696 // separately for each input. As a motivating example, consider:
1697 // bb1:
1698 // %v1 = ... ; constantrange<1, 5>
1699 // br label %merge
1700 // bb2:
1701 // %v2 = ... ; constantrange<10, 20>
1702 // br label %merge
1703 // merge:
1704 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1705 // %pred = icmp eq i32 %phi, 8
1706 // We can't tell from the lattice value for '%phi' that '%pred' is false
1707 // along each path, but by checking the predicate over each input separately,
1708 // we can.
1709 // We limit the search to one step backwards from the current BB and value.
1710 // We could consider extending this to search further backwards through the
1711 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001712 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001713 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001714 BasicBlock *BB = CxtI->getParent();
1715
1716 // Function entry or an unreachable block. Bail to avoid confusing
1717 // analysis below.
1718 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1719 if (PI == PE)
1720 return Unknown;
1721
1722 // If V is a PHI node in the same block as the context, we need to ask
1723 // questions about the predicate as applied to the incoming value along
1724 // each edge. This is useful for eliminating cases where the predicate is
1725 // known along all incoming edges.
1726 if (auto *PHI = dyn_cast<PHINode>(V))
1727 if (PHI->getParent() == BB) {
1728 Tristate Baseline = Unknown;
1729 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1730 Value *Incoming = PHI->getIncomingValue(i);
1731 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001732 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001733 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1734 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001735
Philip Reamesbb11d622015-08-31 18:31:48 +00001736 // Keep going as long as we've seen a consistent known result for
1737 // all inputs.
1738 Baseline = (i == 0) ? Result /* First iteration */
1739 : (Baseline == Result ? Baseline : Unknown); /* All others */
1740 if (Baseline == Unknown)
1741 break;
1742 }
1743 if (Baseline != Unknown)
1744 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001745 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001746
Philip Reames66ab0f02015-06-16 00:49:59 +00001747 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001748 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001749 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001750 if (!isa<Instruction>(V) ||
1751 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001752 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001753 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001754 // the value of the comparison in this block.
1755 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1756 if (Baseline != Unknown) {
1757 // Check that all remaining incoming values match the first one.
1758 while (++PI != PE) {
1759 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1760 if (Ret != Baseline) break;
1761 }
1762 // If we terminated early, then one of the values didn't match.
1763 if (PI == PE) {
1764 return Baseline;
1765 }
1766 }
1767 }
1768 }
1769 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001770}
1771
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001772void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001773 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001774 if (PImpl) {
1775 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001776 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001777 }
Owen Anderson208636f2010-08-18 18:39:01 +00001778}
1779
1780void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001781 if (PImpl) {
1782 const DataLayout &DL = BB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001783 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001784 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001785}
Anna Thomase27b39a2017-03-22 19:27:12 +00001786
1787
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001788void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
Anna Thomase27b39a2017-03-22 19:27:12 +00001789 if (PImpl) {
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001790 getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
Anna Thomase27b39a2017-03-22 19:27:12 +00001791 }
1792}
1793
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001794// Print the LVI for the function arguments at the start of each basic block.
1795void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
1796 const BasicBlock *BB, formatted_raw_ostream &OS) {
1797 // Find if there are latticevalues defined for arguments of the function.
1798 auto *F = BB->getParent();
1799 for (auto &Arg : F->args()) {
Florian Hahn8af01572017-09-28 11:09:22 +00001800 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001801 const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
1802 if (Result.isUndefined())
1803 continue;
1804 OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
1805 }
1806}
1807
1808// This function prints the LVI analysis for the instruction I at the beginning
1809// of various basic blocks. It relies on calculated values that are stored in
1810// the LazyValueInfoCache, and in the absence of cached values, recalculte the
1811// LazyValueInfo for `I`, and print that info.
1812void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
1813 const Instruction *I, formatted_raw_ostream &OS) {
1814
1815 auto *ParentBB = I->getParent();
1816 SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
1817 // We can generate (solve) LVI values only for blocks that are dominated by
1818 // the I's parent. However, to avoid generating LVI for all dominating blocks,
1819 // that contain redundant/uninteresting information, we print LVI for
1820 // blocks that may use this LVI information (such as immediate successor
1821 // blocks, and blocks that contain uses of `I`).
1822 auto printResult = [&](const BasicBlock *BB) {
1823 if (!BlocksContainingLVI.insert(BB).second)
1824 return;
Florian Hahn8af01572017-09-28 11:09:22 +00001825 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001826 const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
1827 OS << "; LatticeVal for: '" << *I << "' in BB: '";
1828 BB->printAsOperand(OS, false);
1829 OS << "' is: " << Result << "\n";
1830 };
1831
1832 printResult(ParentBB);
1833 // Print the LVI analysis results for the the immediate successor blocks, that
1834 // are dominated by `ParentBB`.
1835 for (auto *BBSucc : successors(ParentBB))
1836 if (DT.dominates(ParentBB, BBSucc))
1837 printResult(BBSucc);
1838
1839 // Print LVI in blocks where `I` is used.
1840 for (auto *U : I->users())
1841 if (auto *UseI = dyn_cast<Instruction>(U))
1842 if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
1843 printResult(UseI->getParent());
1844
1845}
1846
Anna Thomase27b39a2017-03-22 19:27:12 +00001847namespace {
1848// Printer class for LazyValueInfo results.
1849class LazyValueInfoPrinter : public FunctionPass {
1850public:
1851 static char ID; // Pass identification, replacement for typeid
1852 LazyValueInfoPrinter() : FunctionPass(ID) {
1853 initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
1854 }
1855
1856 void getAnalysisUsage(AnalysisUsage &AU) const override {
1857 AU.setPreservesAll();
1858 AU.addRequired<LazyValueInfoWrapperPass>();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001859 AU.addRequired<DominatorTreeWrapperPass>();
Anna Thomase27b39a2017-03-22 19:27:12 +00001860 }
1861
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001862 // Get the mandatory dominator tree analysis and pass this in to the
1863 // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
Anna Thomase27b39a2017-03-22 19:27:12 +00001864 bool runOnFunction(Function &F) override {
1865 dbgs() << "LVI for function '" << F.getName() << "':\n";
1866 auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001867 auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1868 LVI.printLVI(F, DTree, dbgs());
Anna Thomase27b39a2017-03-22 19:27:12 +00001869 return false;
1870 }
1871};
1872}
1873
1874char LazyValueInfoPrinter::ID = 0;
1875INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
1876 "Lazy Value Info Printer Pass", false, false)
1877INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
1878INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
1879 "Lazy Value Info Printer Pass", false, false)