blob: 4e915be303a404273772969a09a68d637f9bc20e [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner741c94c2009-11-11 00:22:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interface for lazy computation of value constraint
10// information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/DenseSet.h"
John Regehr3a1c9d52018-11-21 05:24:12 +000016#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#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>
Chris Lattner741c94c2009-11-11 00:22:30 +000040using namespace llvm;
Benjamin Kramerd9d80b12012-03-02 15:34:43 +000041using namespace PatternMatch;
Chris Lattner741c94c2009-11-11 00:22:30 +000042
Chandler Carruthf1221bd2014-04-22 02:48:03 +000043#define DEBUG_TYPE "lazy-value-info"
44
Daniel Berlin9c92a462017-02-08 15:22:52 +000045// This is the number of worklist items we will process to try to discover an
46// answer for a given value.
47static const unsigned MaxProcessedPerValue = 500;
48
Sean Silva687019f2016-06-13 22:01:25 +000049char LazyValueInfoWrapperPass::ID = 0;
50INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
Chad Rosier43a33062011-12-02 01:26:24 +000051 "Lazy Value Information Analysis", false, true)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000052INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000053INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sean Silva687019f2016-06-13 22:01:25 +000054INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +000055 "Lazy Value Information Analysis", false, true)
Chris Lattner741c94c2009-11-11 00:22:30 +000056
57namespace llvm {
Sean Silva687019f2016-06-13 22:01:25 +000058 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
Chris Lattner741c94c2009-11-11 00:22:30 +000059}
60
Chandler Carruthdab4eae2016-11-23 17:53:26 +000061AnalysisKey LazyValueAnalysis::Key;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +000062
Philip Reamesed8cd0d2016-02-02 22:03:19 +000063/// Returns true if this lattice value represents at most one possible value.
64/// This is as precise as any lattice value can get while still representing
65/// reachable code.
Florian Hahn8af01572017-09-28 11:09:22 +000066static bool hasSingleValue(const ValueLatticeElement &Val) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +000067 if (Val.isConstantRange() &&
68 Val.getConstantRange().isSingleElement())
69 // Integer constants are single element ranges
70 return true;
71 if (Val.isConstant())
72 // Non integer constants
73 return true;
74 return false;
75}
76
77/// Combine two sets of facts about the same value into a single set of
78/// facts. Note that this method is not suitable for merging facts along
79/// different paths in a CFG; that's what the mergeIn function is for. This
80/// is for merging facts gathered about the same value at the same location
81/// through two independent means.
82/// Notes:
83/// * This method does not promise to return the most precise possible lattice
84/// value implied by A and B. It is allowed to return any lattice element
85/// which is at least as strong as *either* A or B (unless our facts
NAKAMURA Takumif2529512016-07-04 01:26:27 +000086/// conflict, see below).
Philip Reamesed8cd0d2016-02-02 22:03:19 +000087/// * Due to unreachable code, the intersection of two lattice values could be
88/// contradictory. If this happens, we return some valid lattice value so as
89/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
90/// we do not make this guarantee. TODO: This would be a useful enhancement.
Florian Hahn8af01572017-09-28 11:09:22 +000091static ValueLatticeElement intersect(const ValueLatticeElement &A,
92 const ValueLatticeElement &B) {
Philip Reamesed8cd0d2016-02-02 22:03:19 +000093 // Undefined is the strongest state. It means the value is known to be along
94 // an unreachable path.
95 if (A.isUndefined())
96 return A;
97 if (B.isUndefined())
98 return B;
99
100 // If we gave up for one, but got a useable fact from the other, use it.
101 if (A.isOverdefined())
102 return B;
103 if (B.isOverdefined())
104 return A;
105
106 // Can't get any more precise than constants.
107 if (hasSingleValue(A))
108 return A;
109 if (hasSingleValue(B))
110 return B;
111
112 // Could be either constant range or not constant here.
113 if (!A.isConstantRange() || !B.isConstantRange()) {
114 // TODO: Arbitrary choice, could be improved
115 return A;
116 }
117
118 // Intersect two constant ranges
119 ConstantRange Range =
120 A.getConstantRange().intersectWith(B.getConstantRange());
121 // Note: An empty range is implicitly converted to overdefined internally.
122 // TODO: We could instead use Undefined here since we've proven a conflict
NAKAMURA Takumif2529512016-07-04 01:26:27 +0000123 // and thus know this path must be unreachable.
Florian Hahn8af01572017-09-28 11:09:22 +0000124 return ValueLatticeElement::getRange(std::move(Range));
Philip Reamesed8cd0d2016-02-02 22:03:19 +0000125}
Philip Reamesd1f829d2016-02-02 21:57:37 +0000126
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000127//===----------------------------------------------------------------------===//
Chris Lattneraf025d32009-11-15 19:59:49 +0000128// LazyValueInfoCache Decl
Chris Lattnerfde1f8d2009-11-11 02:08:33 +0000129//===----------------------------------------------------------------------===//
130
Chris Lattneraf025d32009-11-15 19:59:49 +0000131namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000132 /// A callback value handle updates the cache when values are erased.
Owen Anderson118ac802011-01-05 21:15:29 +0000133 class LazyValueInfoCache;
David Blaikie774b5842015-08-03 22:30:24 +0000134 struct LVIValueHandle final : public CallbackVH {
Justin Lebar58b377e2016-07-27 22:33:36 +0000135 // Needs to access getValPtr(), which is protected.
136 friend struct DenseMapInfo<LVIValueHandle>;
137
Owen Anderson118ac802011-01-05 21:15:29 +0000138 LazyValueInfoCache *Parent;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000139
Owen Anderson118ac802011-01-05 21:15:29 +0000140 LVIValueHandle(Value *V, LazyValueInfoCache *P)
141 : CallbackVH(V), Parent(P) { }
Craig Toppere9ba7592014-03-05 07:30:04 +0000142
143 void deleted() override;
144 void allUsesReplacedWith(Value *V) override {
Owen Anderson118ac802011-01-05 21:15:29 +0000145 deleted();
146 }
147 };
Justin Lebar58b377e2016-07-27 22:33:36 +0000148} // end anonymous namespace
Owen Anderson118ac802011-01-05 21:15:29 +0000149
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000150namespace {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000151 /// This is the cache kept by LazyValueInfo which
Chris Lattneraf025d32009-11-15 19:59:49 +0000152 /// maintains information about queries across the clients' queries.
153 class LazyValueInfoCache {
Sanjay Patel2a385e22015-01-09 16:47:20 +0000154 /// This is all of the cached block information for exactly one Value*.
155 /// The entries are sorted by the BasicBlock* of the
Chris Lattneraf025d32009-11-15 19:59:49 +0000156 /// entries, allowing us to do a lookup with a binary search.
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000157 /// Over-defined lattice values are recorded in OverDefinedCache to reduce
158 /// memory overhead.
Justin Lebar58b377e2016-07-27 22:33:36 +0000159 struct ValueCacheEntryTy {
160 ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
161 LVIValueHandle Handle;
Florian Hahn8af01572017-09-28 11:09:22 +0000162 SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals;
Justin Lebar58b377e2016-07-27 22:33:36 +0000163 };
Chris Lattneraf025d32009-11-15 19:59:49 +0000164
Sanjay Patel2a385e22015-01-09 16:47:20 +0000165 /// This tracks, on a per-block basis, the set of values that are
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000166 /// over-defined at the end of that block.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000167 typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000168 OverDefinedCacheTy;
Sanjay Patel2a385e22015-01-09 16:47:20 +0000169 /// Keep track of all blocks that we have ever seen, so we
Benjamin Kramer36647082011-12-03 15:16:45 +0000170 /// don't spend time removing unused blocks from our caches.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000171 DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
Benjamin Kramer36647082011-12-03 15:16:45 +0000172
Anna Thomase27b39a2017-03-22 19:27:12 +0000173 /// This is all of the cached information for all values,
174 /// mapped from Value* to key information.
175 DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
176 OverDefinedCacheTy OverDefinedCache;
177
178
Philip Reames9db79482016-09-12 22:38:44 +0000179 public:
Florian Hahn8af01572017-09-28 11:09:22 +0000180 void insertResult(Value *Val, BasicBlock *BB,
181 const ValueLatticeElement &Result) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000182 SeenBlocks.insert(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000183
184 // Insert over-defined values into their own cache to reduce memory
185 // overhead.
Hans Wennborg45172ac2014-11-25 17:23:05 +0000186 if (Result.isOverdefined())
Bruno Cardoso Lopes6ac4ea42015-08-18 16:34:27 +0000187 OverDefinedCache[BB].insert(Val);
Justin Lebar58b377e2016-07-27 22:33:36 +0000188 else {
189 auto It = ValueCache.find_as(Val);
190 if (It == ValueCache.end()) {
191 ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this);
192 It = ValueCache.find_as(Val);
193 assert(It != ValueCache.end() && "Val was just added to the map!");
194 }
195 It->second->BlockVals[BB] = Result;
196 }
Hans Wennborg45172ac2014-11-25 17:23:05 +0000197 }
Owen Andersonc1561b82010-07-30 23:59:40 +0000198
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000199 bool isOverdefined(Value *V, BasicBlock *BB) const {
200 auto ODI = OverDefinedCache.find(BB);
201
202 if (ODI == OverDefinedCache.end())
203 return false;
204
205 return ODI->second.count(V);
206 }
207
Philip Reames9db79482016-09-12 22:38:44 +0000208 bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000209 if (isOverdefined(V, BB))
210 return true;
211
Justin Lebar58b377e2016-07-27 22:33:36 +0000212 auto I = ValueCache.find_as(V);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000213 if (I == ValueCache.end())
214 return false;
215
Justin Lebar58b377e2016-07-27 22:33:36 +0000216 return I->second->BlockVals.count(BB);
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000217 }
218
Florian Hahn8af01572017-09-28 11:09:22 +0000219 ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const {
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000220 if (isOverdefined(V, BB))
Florian Hahn8af01572017-09-28 11:09:22 +0000221 return ValueLatticeElement::getOverdefined();
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000222
Justin Lebar58b377e2016-07-27 22:33:36 +0000223 auto I = ValueCache.find_as(V);
224 if (I == ValueCache.end())
Florian Hahn8af01572017-09-28 11:09:22 +0000225 return ValueLatticeElement();
Justin Lebar58b377e2016-07-27 22:33:36 +0000226 auto BBI = I->second->BlockVals.find(BB);
227 if (BBI == I->second->BlockVals.end())
Florian Hahn8af01572017-09-28 11:09:22 +0000228 return ValueLatticeElement();
Justin Lebar58b377e2016-07-27 22:33:36 +0000229 return BBI->second;
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000230 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000231
Philip Reames92e5e1b2016-09-12 21:46:58 +0000232 /// clear - Empty the cache.
233 void clear() {
234 SeenBlocks.clear();
235 ValueCache.clear();
236 OverDefinedCache.clear();
237 }
238
Philip Reamesb627aec2016-09-12 22:03:36 +0000239 /// Inform the cache that a given value has been deleted.
240 void eraseValue(Value *V);
241
242 /// This is part of the update interface to inform the cache
243 /// that a block has been deleted.
244 void eraseBlock(BasicBlock *BB);
245
Philip Reames9db79482016-09-12 22:38:44 +0000246 /// Updates the cache to remove any influence an overdefined value in
247 /// OldSucc might have (unless also overdefined in NewSucc). This just
248 /// flushes elements from the cache and does not add any.
249 void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
250
Philip Reames92e5e1b2016-09-12 21:46:58 +0000251 friend struct LVIValueHandle;
252 };
Philip Reamesb627aec2016-09-12 22:03:36 +0000253}
Philip Reames92e5e1b2016-09-12 21:46:58 +0000254
Philip Reamesb627aec2016-09-12 22:03:36 +0000255void LazyValueInfoCache::eraseValue(Value *V) {
Chandler Carruth41421df2017-01-26 08:31:54 +0000256 for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) {
257 // Copy and increment the iterator immediately so we can erase behind
258 // ourselves.
259 auto Iter = I++;
260 SmallPtrSetImpl<Value *> &ValueSet = Iter->second;
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000261 ValueSet.erase(V);
Philip Reamesb627aec2016-09-12 22:03:36 +0000262 if (ValueSet.empty())
Chandler Carruth41421df2017-01-26 08:31:54 +0000263 OverDefinedCache.erase(Iter);
Philip Reamesb627aec2016-09-12 22:03:36 +0000264 }
Philip Reamesb627aec2016-09-12 22:03:36 +0000265
266 ValueCache.erase(V);
267}
268
269void LVIValueHandle::deleted() {
270 // This erasure deallocates *this, so it MUST happen after we're done
271 // using any and all members of *this.
272 Parent->eraseValue(*this);
273}
274
275void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
276 // Shortcut if we have never seen this block.
Chandler Carruth6acdca72017-01-24 12:55:57 +0000277 DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
Philip Reamesb627aec2016-09-12 22:03:36 +0000278 if (I == SeenBlocks.end())
279 return;
280 SeenBlocks.erase(I);
281
282 auto ODI = OverDefinedCache.find(BB);
283 if (ODI != OverDefinedCache.end())
284 OverDefinedCache.erase(ODI);
285
286 for (auto &I : ValueCache)
287 I.second->BlockVals.erase(BB);
288}
289
Philip Reames9db79482016-09-12 22:38:44 +0000290void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
291 BasicBlock *NewSucc) {
292 // When an edge in the graph has been threaded, values that we could not
293 // determine a value for before (i.e. were marked overdefined) may be
294 // possible to solve now. We do NOT try to proactively update these values.
295 // Instead, we clear their entries from the cache, and allow lazy updating to
296 // recompute them when needed.
297
298 // The updating process is fairly simple: we need to drop cached info
299 // for all values that were marked overdefined in OldSucc, and for those same
300 // values in any successor of OldSucc (except NewSucc) in which they were
301 // also marked overdefined.
302 std::vector<BasicBlock*> worklist;
303 worklist.push_back(OldSucc);
304
305 auto I = OverDefinedCache.find(OldSucc);
306 if (I == OverDefinedCache.end())
307 return; // Nothing to process here.
308 SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
309
310 // Use a worklist to perform a depth-first search of OldSucc's successors.
311 // NOTE: We do not need a visited list since any blocks we have already
312 // visited will have had their overdefined markers cleared already, and we
313 // thus won't loop to their successors.
314 while (!worklist.empty()) {
315 BasicBlock *ToUpdate = worklist.back();
316 worklist.pop_back();
317
318 // Skip blocks only accessible through NewSucc.
319 if (ToUpdate == NewSucc) continue;
320
Philip Reames1e48efc2016-12-30 17:56:47 +0000321 // If a value was marked overdefined in OldSucc, and is here too...
322 auto OI = OverDefinedCache.find(ToUpdate);
323 if (OI == OverDefinedCache.end())
324 continue;
325 SmallPtrSetImpl<Value *> &ValueSet = OI->second;
326
Philip Reames9db79482016-09-12 22:38:44 +0000327 bool changed = false;
328 for (Value *V : ValsToClear) {
Philip Reamesfdbb05b2016-12-30 22:09:10 +0000329 if (!ValueSet.erase(V))
Philip Reames9db79482016-09-12 22:38:44 +0000330 continue;
331
Philip Reames9db79482016-09-12 22:38:44 +0000332 // If we removed anything, then we potentially need to update
333 // blocks successors too.
334 changed = true;
Philip Reames1e48efc2016-12-30 17:56:47 +0000335
336 if (ValueSet.empty()) {
337 OverDefinedCache.erase(OI);
338 break;
339 }
Philip Reames9db79482016-09-12 22:38:44 +0000340 }
341
342 if (!changed) continue;
343
344 worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
345 }
346}
347
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000348
349namespace {
350/// An assembly annotator class to print LazyValueCache information in
351/// comments.
352class LazyValueInfoImpl;
353class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {
354 LazyValueInfoImpl *LVIImpl;
355 // While analyzing which blocks we can solve values for, we need the dominator
356 // information. Since this is an optional parameter in LVI, we require this
357 // DomTreeAnalysis pass in the printer pass, and pass the dominator
358 // tree to the LazyValueInfoAnnotatedWriter.
359 DominatorTree &DT;
360
361public:
362 LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)
363 : LVIImpl(L), DT(DTree) {}
364
365 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
366 formatted_raw_ostream &OS);
367
368 virtual void emitInstructionAnnot(const Instruction *I,
369 formatted_raw_ostream &OS);
370};
371}
Philip Reamesb627aec2016-09-12 22:03:36 +0000372namespace {
Philip Reames92e5e1b2016-09-12 21:46:58 +0000373 // The actual implementation of the lazy analysis and update. Note that the
374 // inheritance from LazyValueInfoCache is intended to be temporary while
375 // splitting the code and then transitioning to a has-a relationship.
Philip Reames9db79482016-09-12 22:38:44 +0000376 class LazyValueInfoImpl {
377
378 /// Cached results from previous queries
379 LazyValueInfoCache TheCache;
Philip Reames92e5e1b2016-09-12 21:46:58 +0000380
381 /// This stack holds the state of the value solver during a query.
382 /// It basically emulates the callstack of the naive
383 /// recursive value lookup process.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000384 SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
Philip Reames92e5e1b2016-09-12 21:46:58 +0000385
386 /// Keeps track of which block-value pairs are in BlockValueStack.
387 DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
388
389 /// Push BV onto BlockValueStack unless it's already in there.
390 /// Returns true on success.
391 bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
392 if (!BlockValueSet.insert(BV).second)
393 return false; // It's already in the stack.
394
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000395 LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in "
396 << BV.first->getName() << "\n");
Daniel Berlin9c92a462017-02-08 15:22:52 +0000397 BlockValueStack.push_back(BV);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000398 return true;
399 }
400
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000401 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
Philip Reames92e5e1b2016-09-12 21:46:58 +0000402 const DataLayout &DL; ///< A mandatory DataLayout
403 DominatorTree *DT; ///< An optional DT pointer.
Brian M. Rzyckif1a7df52018-02-16 16:35:17 +0000404 DominatorTree *DisabledDT; ///< Stores DT if it's disabled.
Philip Reames92e5e1b2016-09-12 21:46:58 +0000405
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);
John Regehr3a1c9d52018-11-21 05:24:12 +0000423 Optional<ConstantRange> getRangeForOperand(unsigned Op, Instruction *I,
424 BasicBlock *BB);
Florian Hahn8af01572017-09-28 11:09:22 +0000425 bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
426 BasicBlock *BB);
427 bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
Philip Reames92e5e1b2016-09-12 21:46:58 +0000428 BasicBlock *BB);
429 void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
Florian Hahn8af01572017-09-28 11:09:22 +0000430 ValueLatticeElement &BBLV,
Craig Topper9277a862017-06-02 17:28:12 +0000431 Instruction *BBI);
Philip Reames92e5e1b2016-09-12 21:46:58 +0000432
433 void solve();
434
435 public:
Sanjay Patel2a385e22015-01-09 16:47:20 +0000436 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000437 /// value for the specified Value* at the end of the specified block.
Florian Hahn8af01572017-09-28 11:09:22 +0000438 ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
439 Instruction *CxtI = nullptr);
Hal Finkel7e184492014-09-07 20:29:59 +0000440
Sanjay Patel2a385e22015-01-09 16:47:20 +0000441 /// This is the query interface to determine the lattice
Hal Finkel7e184492014-09-07 20:29:59 +0000442 /// value for the specified Value* at the specified instruction (generally
443 /// from an assume intrinsic).
Florian Hahn8af01572017-09-28 11:09:22 +0000444 ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
Chris Lattneraf025d32009-11-15 19:59:49 +0000445
Sanjay Patel2a385e22015-01-09 16:47:20 +0000446 /// This is the query interface to determine the lattice
Chris Lattneraf025d32009-11-15 19:59:49 +0000447 /// value for the specified Value* that is true on the specified edge.
Florian Hahn8af01572017-09-28 11:09:22 +0000448 ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
449 BasicBlock *ToBB,
450 Instruction *CxtI = nullptr);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000451
Philip Reames9db79482016-09-12 22:38:44 +0000452 /// Complete flush all previously computed values
453 void clear() {
454 TheCache.clear();
455 }
456
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000457 /// Printing the LazyValueInfo Analysis.
458 void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
459 LazyValueInfoAnnotatedWriter Writer(this, DTree);
460 F.print(OS, &Writer);
Anna Thomase27b39a2017-03-22 19:27:12 +0000461 }
462
Philip Reames9db79482016-09-12 22:38:44 +0000463 /// This is part of the update interface to inform the cache
464 /// that a block has been deleted.
465 void eraseBlock(BasicBlock *BB) {
466 TheCache.eraseBlock(BB);
467 }
468
Brian M. Rzyckif1a7df52018-02-16 16:35:17 +0000469 /// Disables use of the DominatorTree within LVI.
470 void disableDT() {
471 if (DT) {
472 assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
473 std::swap(DT, DisabledDT);
474 }
475 }
476
477 /// Enables use of the DominatorTree within LVI. Does nothing if the class
478 /// instance was initialized without a DT pointer.
479 void enableDT() {
480 if (DisabledDT) {
481 assert(!DT && "Both DT and DisabledDT are not nullptr!");
482 std::swap(DT, DisabledDT);
483 }
484 }
485
Sanjay Patel2a385e22015-01-09 16:47:20 +0000486 /// This is the update interface to inform the cache that an edge from
487 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
Owen Andersonaa7f66b2010-07-26 18:48:03 +0000488 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000489
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000490 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
491 DominatorTree *DT = nullptr)
Brian M. Rzyckif1a7df52018-02-16 16:35:17 +0000492 : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
Chris Lattneraf025d32009-11-15 19:59:49 +0000493 };
494} // end anonymous namespace
495
Anna Thomas4acfc7e2017-06-06 19:25:31 +0000496
Philip Reames92e5e1b2016-09-12 21:46:58 +0000497void LazyValueInfoImpl::solve() {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000498 SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
499 BlockValueStack.begin(), BlockValueStack.end());
500
501 unsigned processedCount = 0;
Owen Anderson6f060af2011-01-05 23:26:22 +0000502 while (!BlockValueStack.empty()) {
Daniel Berlin9c92a462017-02-08 15:22:52 +0000503 processedCount++;
504 // Abort if we have to process too many values to get a result for this one.
505 // Because of the design of the overdefined cache currently being per-block
506 // to avoid naming-related issues (IE it wants to try to give different
507 // results for the same name in different blocks), overdefined results don't
508 // get cached globally, which in turn means we will often try to rediscover
509 // the same overdefined result again and again. Once something like
510 // PredicateInfo is used in LVI or CVP, we should be able to make the
511 // overdefined cache global, and remove this throttle.
512 if (processedCount > MaxProcessedPerValue) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000513 LLVM_DEBUG(
514 dbgs() << "Giving up on stack because we are getting too deep\n");
Daniel Berlin9c92a462017-02-08 15:22:52 +0000515 // Fill in the original values
516 while (!StartingStack.empty()) {
517 std::pair<BasicBlock *, Value *> &e = StartingStack.back();
518 TheCache.insertResult(e.second, e.first,
Florian Hahn8af01572017-09-28 11:09:22 +0000519 ValueLatticeElement::getOverdefined());
Daniel Berlin9c92a462017-02-08 15:22:52 +0000520 StartingStack.pop_back();
521 }
522 BlockValueSet.clear();
523 BlockValueStack.clear();
524 return;
525 }
Vitaly Buka9987d982017-02-09 09:28:05 +0000526 std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000527 assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
528
Nuno Lopese6e04902012-06-28 01:16:18 +0000529 if (solveBlockValue(e.second, e.first)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000530 // The work item was completely processed.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000531 assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
Philip Reames9db79482016-09-12 22:38:44 +0000532 assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
Akira Hatanaka2992bee2015-12-11 00:49:47 +0000533 "Result should be in cache!");
Hans Wennborg45172ac2014-11-25 17:23:05 +0000534
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000535 LLVM_DEBUG(
536 dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "
537 << TheCache.getCachedValueInfo(e.second, e.first) << "\n");
Philip Reames44456b82016-02-02 03:15:40 +0000538
Daniel Berlin9c92a462017-02-08 15:22:52 +0000539 BlockValueStack.pop_back();
Hans Wennborg45172ac2014-11-25 17:23:05 +0000540 BlockValueSet.erase(e);
541 } else {
542 // More work needs to be done before revisiting.
Daniel Berlin9c92a462017-02-08 15:22:52 +0000543 assert(BlockValueStack.back() != e && "Stack should have been pushed!");
Nuno Lopese6e04902012-06-28 01:16:18 +0000544 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000545 }
546}
547
Philip Reames92e5e1b2016-09-12 21:46:58 +0000548bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000549 // If already a constant, there is nothing to compute.
550 if (isa<Constant>(Val))
551 return true;
552
Philip Reames9db79482016-09-12 22:38:44 +0000553 return TheCache.hasCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000554}
555
Florian Hahn8af01572017-09-28 11:09:22 +0000556ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
557 BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000558 // If already a constant, there is nothing to compute.
559 if (Constant *VC = dyn_cast<Constant>(Val))
Florian Hahn8af01572017-09-28 11:09:22 +0000560 return ValueLatticeElement::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000561
Philip Reames9db79482016-09-12 22:38:44 +0000562 return TheCache.getCachedValueInfo(Val, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000563}
564
Florian Hahn8af01572017-09-28 11:09:22 +0000565static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
Philip Reameseb3e9da2015-10-29 03:57:17 +0000566 switch (BBI->getOpcode()) {
567 default: break;
568 case Instruction::Load:
569 case Instruction::Call:
570 case Instruction::Invoke:
NAKAMURA Takumibd072a92016-07-25 00:59:46 +0000571 if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
Philip Reames70efccd2015-10-29 04:21:49 +0000572 if (isa<IntegerType>(BBI->getType())) {
Florian Hahn8af01572017-09-28 11:09:22 +0000573 return ValueLatticeElement::getRange(
574 getConstantRangeFromMetadata(*Ranges));
Philip Reameseb3e9da2015-10-29 03:57:17 +0000575 }
576 break;
577 };
Philip Reamesd1f829d2016-02-02 21:57:37 +0000578 // Nothing known - will be intersected with other facts
Florian Hahn8af01572017-09-28 11:09:22 +0000579 return ValueLatticeElement::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +0000580}
581
Philip Reames92e5e1b2016-09-12 21:46:58 +0000582bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
Nick Lewycky55a700b2010-12-18 01:00:40 +0000583 if (isa<Constant>(Val))
584 return true;
585
Philip Reames9db79482016-09-12 22:38:44 +0000586 if (TheCache.hasCachedValueInfo(Val, BB)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +0000587 // If we have a cached value, use that.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000588 LLVM_DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val="
589 << TheCache.getCachedValueInfo(Val, BB) << '\n');
Nick Lewycky55a700b2010-12-18 01:00:40 +0000590
Hans Wennborg45172ac2014-11-25 17:23:05 +0000591 // Since we're reusing a cached value, we don't need to update the
592 // OverDefinedCache. The cache will have been properly updated whenever the
593 // cached value was inserted.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000594 return true;
Chris Lattner2c708562009-11-15 20:00:52 +0000595 }
596
Hans Wennborg45172ac2014-11-25 17:23:05 +0000597 // Hold off inserting this value into the Cache in case we have to return
598 // false and come back later.
Florian Hahn8af01572017-09-28 11:09:22 +0000599 ValueLatticeElement Res;
Philip Reames05c435e2016-12-06 03:22:03 +0000600 if (!solveBlockValueImpl(Res, Val, BB))
601 // Work pushed, will revisit
602 return false;
603
604 TheCache.insertResult(Val, BB, Res);
605 return true;
606}
607
Florian Hahn8af01572017-09-28 11:09:22 +0000608bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
Philip Reames05c435e2016-12-06 03:22:03 +0000609 Value *Val, BasicBlock *BB) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000610
Chris Lattneraf025d32009-11-15 19:59:49 +0000611 Instruction *BBI = dyn_cast<Instruction>(Val);
Philip Reames05c435e2016-12-06 03:22:03 +0000612 if (!BBI || BBI->getParent() != BB)
613 return solveBlockValueNonLocal(Res, Val, BB);
Chris Lattner2c708562009-11-15 20:00:52 +0000614
Philip Reames05c435e2016-12-06 03:22:03 +0000615 if (PHINode *PN = dyn_cast<PHINode>(BBI))
616 return solveBlockValuePHINode(Res, PN, BB);
Owen Anderson80d19f02010-08-18 21:11:37 +0000617
Philip Reames05c435e2016-12-06 03:22:03 +0000618 if (auto *SI = dyn_cast<SelectInst>(BBI))
619 return solveBlockValueSelect(Res, SI, BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000620
Philip Reames2ab964e2016-04-27 01:02:25 +0000621 // If this value is a nonnull pointer, record it's range and bailout. Note
622 // that for all other pointer typed values, we terminate the search at the
623 // definition. We could easily extend this to look through geps, bitcasts,
624 // and the like to prove non-nullness, but it's not clear that's worth it
Craig Topper7ad13f22017-06-09 21:21:17 +0000625 // compile time wise. The context-insensitive value walk done inside
Nuno Lopes404f1062017-09-09 18:23:11 +0000626 // isKnownNonZero gets most of the profitable cases at much less expense.
Hiroshi Inoue02a2bb22019-02-05 08:30:48 +0000627 // This does mean that we have a sensitivity to where the defining
Philip Reames2ab964e2016-04-27 01:02:25 +0000628 // instruction is placed, even if it could legally be hoisted much higher.
629 // That is unfortunate.
Igor Laevsky0fa48192015-09-18 13:01:48 +0000630 PointerType *PT = dyn_cast<PointerType>(BBI->getType());
Nuno Lopes404f1062017-09-09 18:23:11 +0000631 if (PT && isKnownNonZero(BBI, DL)) {
Florian Hahn8af01572017-09-28 11:09:22 +0000632 Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
Hans Wennborg45172ac2014-11-25 17:23:05 +0000633 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000634 }
Davide Italianobd543d02016-05-25 22:29:34 +0000635 if (BBI->getType()->isIntegerTy()) {
Craig Topper0e5f1092017-06-03 07:47:08 +0000636 if (auto *CI = dyn_cast<CastInst>(BBI))
637 return solveBlockValueCast(Res, CI, BB);
638
John Regehr3a1c9d52018-11-21 05:24:12 +0000639 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))
Craig Topper3778c892017-06-02 16:33:13 +0000640 return solveBlockValueBinaryOp(Res, BO, BB);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000641 }
Owen Anderson80d19f02010-08-18 21:11:37 +0000642
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000643 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
644 << "' - unknown inst def found.\n");
Philip Reamesa0c9f6e2016-03-04 22:27:39 +0000645 Res = getFromRangeMetadata(BBI);
Hans Wennborg45172ac2014-11-25 17:23:05 +0000646 return true;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000647}
648
649static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
650 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
651 return L->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000652 GetUnderlyingObject(L->getPointerOperand(),
653 L->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000654 }
655 if (StoreInst *S = dyn_cast<StoreInst>(I)) {
656 return S->getPointerAddressSpace() == 0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000657 GetUnderlyingObject(S->getPointerOperand(),
658 S->getModule()->getDataLayout()) == Ptr;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000659 }
Nick Lewycky367f98f2011-01-15 09:16:12 +0000660 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
661 if (MI->isVolatile()) return false;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000662
663 // FIXME: check whether it has a valuerange that excludes zero?
664 ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
665 if (!Len || Len->isZero()) return false;
666
Eli Friedman7a5fc692011-05-31 20:40:16 +0000667 if (MI->getDestAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000668 if (GetUnderlyingObject(MI->getRawDest(),
669 MI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000670 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000671 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
Eli Friedman7a5fc692011-05-31 20:40:16 +0000672 if (MTI->getSourceAddressSpace() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000673 if (GetUnderlyingObject(MTI->getRawSource(),
674 MTI->getModule()->getDataLayout()) == Ptr)
Eli Friedman7a5fc692011-05-31 20:40:16 +0000675 return true;
Nick Lewycky367f98f2011-01-15 09:16:12 +0000676 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000677 return false;
678}
679
Philip Reames3f83dbe2016-04-27 00:30:55 +0000680/// Return true if the allocation associated with Val is ever dereferenced
681/// within the given basic block. This establishes the fact Val is not null,
682/// but does not imply that the memory at Val is dereferenceable. (Val may
683/// point off the end of the dereferenceable part of the object.)
684static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
685 assert(Val->getType()->isPointerTy());
686
687 const DataLayout &DL = BB->getModule()->getDataLayout();
688 Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
689 // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
690 // inside InstructionDereferencesPointer either.
691 if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
692 for (Instruction &I : *BB)
693 if (InstructionDereferencesPointer(&I, UnderlyingVal))
694 return true;
695 return false;
696}
697
Florian Hahn8af01572017-09-28 11:09:22 +0000698bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
Owen Anderson64c2c572010-12-20 18:18:16 +0000699 Value *Val, BasicBlock *BB) {
Florian Hahn8af01572017-09-28 11:09:22 +0000700 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000701
Nick Lewycky55a700b2010-12-18 01:00:40 +0000702 // If this is the entry block, we must be asking about an argument. The
703 // value is overdefined.
704 if (BB == &BB->getParent()->getEntryBlock()) {
705 assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
Craig Topper96d6ee852017-04-28 16:57:59 +0000706 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000707 // this particular block.
Manoj Gupta77eeac32018-07-09 22:27:23 +0000708 PointerType *PTy = dyn_cast<PointerType>(Val->getType());
709 if (PTy &&
710 (isKnownNonZero(Val, DL) ||
711 (isObjectDereferencedInBlock(Val, BB) &&
712 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
Florian Hahn8af01572017-09-28 11:09:22 +0000713 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky55a700b2010-12-18 01:00:40 +0000714 } else {
Florian Hahn8af01572017-09-28 11:09:22 +0000715 Result = ValueLatticeElement::getOverdefined();
Nick Lewycky55a700b2010-12-18 01:00:40 +0000716 }
Owen Anderson64c2c572010-12-20 18:18:16 +0000717 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000718 return true;
719 }
720
721 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000722 // result. If we encounter an unexplored predecessor, we eagerly explore it
723 // in a depth first manner. In practice, this has the effect of discovering
724 // paths we can't analyze eagerly without spending compile times analyzing
725 // other paths. This heuristic benefits from the fact that predecessors are
726 // frequently arranged such that dominating ones come first and we quickly
727 // find a path to function entry. TODO: We should consider explicitly
728 // canonicalizing to make this true rather than relying on this happy
Fangrui Songf78650a2018-07-30 19:41:25 +0000729 // accident.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000730 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Florian Hahn8af01572017-09-28 11:09:22 +0000731 ValueLatticeElement EdgeResult;
Philip Reamesc80bd042017-02-07 00:25:24 +0000732 if (!getEdgeValue(Val, *PI, BB, EdgeResult))
733 // Explore that input, then return here
734 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000735
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000736 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000737
738 // If we hit overdefined, exit early. The BlockVals entry is already set
739 // to overdefined.
740 if (Result.isOverdefined()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000741 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
742 << "' - overdefined because of pred (non local).\n");
Artur Pilipenkoadcd01f2016-08-09 09:14:29 +0000743 // Before giving up, see if we can prove the pointer non-null local to
Philip Reames3f83dbe2016-04-27 00:30:55 +0000744 // this particular block.
Manoj Gupta77eeac32018-07-09 22:27:23 +0000745 PointerType *PTy = dyn_cast<PointerType>(Val->getType());
746 if (PTy && isObjectDereferencedInBlock(Val, BB) &&
747 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
Florian Hahn8af01572017-09-28 11:09:22 +0000748 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
Nick Lewycky55a700b2010-12-18 01:00:40 +0000749 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000750
Owen Anderson64c2c572010-12-20 18:18:16 +0000751 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000752 return true;
753 }
754 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000755
756 // Return the merged value, which is more precise than 'overdefined'.
757 assert(!Result.isOverdefined());
Owen Anderson64c2c572010-12-20 18:18:16 +0000758 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000759 return true;
760}
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000761
Florian Hahn8af01572017-09-28 11:09:22 +0000762bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
763 PHINode *PN, BasicBlock *BB) {
764 ValueLatticeElement Result; // Start Undefined.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000765
766 // Loop over all of our predecessors, merging what we know from them into
Philip Reamesc80bd042017-02-07 00:25:24 +0000767 // result. See the comment about the chosen traversal order in
768 // solveBlockValueNonLocal; the same reasoning applies here.
Nick Lewycky55a700b2010-12-18 01:00:40 +0000769 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
770 BasicBlock *PhiBB = PN->getIncomingBlock(i);
771 Value *PhiVal = PN->getIncomingValue(i);
Florian Hahn8af01572017-09-28 11:09:22 +0000772 ValueLatticeElement EdgeResult;
Hal Finkel2400c962014-10-16 00:40:05 +0000773 // Note that we can provide PN as the context value to getEdgeValue, even
774 // though the results will be cached, because PN is the value being used as
775 // the cache key in the caller.
Philip Reamesc80bd042017-02-07 00:25:24 +0000776 if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
777 // Explore that input, then return here
778 return false;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000779
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000780 Result.mergeIn(EdgeResult, DL);
Nick Lewycky55a700b2010-12-18 01:00:40 +0000781
782 // If we hit overdefined, exit early. The BlockVals entry is already set
783 // to overdefined.
784 if (Result.isOverdefined()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000785 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
786 << "' - overdefined because of pred (local).\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +0000787
Owen Anderson64c2c572010-12-20 18:18:16 +0000788 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000789 return true;
790 }
791 }
Nick Lewycky55a700b2010-12-18 01:00:40 +0000792
793 // Return the merged value, which is more precise than 'overdefined'.
794 assert(!Result.isOverdefined() && "Possible PHI in entry block?");
Owen Anderson64c2c572010-12-20 18:18:16 +0000795 BBLV = Result;
Nick Lewycky55a700b2010-12-18 01:00:40 +0000796 return true;
797}
798
Florian Hahn8af01572017-09-28 11:09:22 +0000799static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
800 bool isTrueDest = true);
Hal Finkel7e184492014-09-07 20:29:59 +0000801
Philip Reamesd1f829d2016-02-02 21:57:37 +0000802// If we can determine a constraint on the value given conditions assumed by
803// the program, intersect those constraints with BBLV
Philip Reames92e5e1b2016-09-12 21:46:58 +0000804void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
Florian Hahn8af01572017-09-28 11:09:22 +0000805 Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
Hal Finkel7e184492014-09-07 20:29:59 +0000806 BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
807 if (!BBI)
808 return;
809
Hal Finkel8a9a7832017-01-11 13:24:24 +0000810 for (auto &AssumeVH : AC->assumptionsFor(Val)) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000811 if (!AssumeVH)
Chandler Carruth66b31302015-01-04 12:03:27 +0000812 continue;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000813 auto *I = cast<CallInst>(AssumeVH);
814 if (!isValidAssumeForContext(I, BBI, DT))
Hal Finkel7e184492014-09-07 20:29:59 +0000815 continue;
816
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000817 BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
Hal Finkel7e184492014-09-07 20:29:59 +0000818 }
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000819
820 // If guards are not used in the module, don't spend time looking for them
821 auto *GuardDecl = BBI->getModule()->getFunction(
822 Intrinsic::getName(Intrinsic::experimental_guard));
823 if (!GuardDecl || GuardDecl->use_empty())
824 return;
825
Philip Reames390c0e22019-01-22 01:34:33 +0000826 if (BBI->getIterator() == BBI->getParent()->begin())
827 return;
828 for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000829 BBI->getParent()->rend())) {
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000830 Value *Cond = nullptr;
Artur Pilipenko47dc0982016-10-21 15:02:21 +0000831 if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
832 BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +0000833 }
Hal Finkel7e184492014-09-07 20:29:59 +0000834}
835
Florian Hahn8af01572017-09-28 11:09:22 +0000836bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
837 SelectInst *SI, BasicBlock *BB) {
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000838
839 // Recurse on our inputs if needed
840 if (!hasBlockValue(SI->getTrueValue(), BB)) {
841 if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
842 return false;
Florian Hahn8af01572017-09-28 11:09:22 +0000843 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000844 return true;
845 }
Florian Hahn8af01572017-09-28 11:09:22 +0000846 ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000847 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
848 // extra slots in the table if we can.
849 if (TrueVal.isOverdefined()) {
Florian Hahn8af01572017-09-28 11:09:22 +0000850 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000851 return true;
852 }
853
854 if (!hasBlockValue(SI->getFalseValue(), BB)) {
855 if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
856 return false;
Florian Hahn8af01572017-09-28 11:09:22 +0000857 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000858 return true;
859 }
Florian Hahn8af01572017-09-28 11:09:22 +0000860 ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000861 // If we hit overdefined, don't ask more queries. We want to avoid poisoning
862 // extra slots in the table if we can.
863 if (FalseVal.isOverdefined()) {
Florian Hahn8af01572017-09-28 11:09:22 +0000864 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000865 return true;
866 }
867
Philip Reamesadf0e352016-02-26 22:53:59 +0000868 if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +0000869 const ConstantRange &TrueCR = TrueVal.getConstantRange();
870 const ConstantRange &FalseCR = FalseVal.getConstantRange();
Philip Reamesadf0e352016-02-26 22:53:59 +0000871 Value *LHS = nullptr;
872 Value *RHS = nullptr;
873 SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
874 // Is this a min specifically of our two inputs? (Avoid the risk of
875 // ValueTracking getting smarter looking back past our immediate inputs.)
876 if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
877 LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
Philip Reamesb2949622016-12-06 02:54:16 +0000878 ConstantRange ResultCR = [&]() {
879 switch (SPR.Flavor) {
880 default:
881 llvm_unreachable("unexpected minmax type!");
882 case SPF_SMIN: /// Signed minimum
883 return TrueCR.smin(FalseCR);
884 case SPF_UMIN: /// Unsigned minimum
885 return TrueCR.umin(FalseCR);
886 case SPF_SMAX: /// Signed maximum
887 return TrueCR.smax(FalseCR);
888 case SPF_UMAX: /// Unsigned maximum
889 return TrueCR.umax(FalseCR);
890 };
891 }();
Florian Hahn8af01572017-09-28 11:09:22 +0000892 BBLV = ValueLatticeElement::getRange(ResultCR);
Philip Reamesb2949622016-12-06 02:54:16 +0000893 return true;
Philip Reamesadf0e352016-02-26 22:53:59 +0000894 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000895
Philip Reamesadf0e352016-02-26 22:53:59 +0000896 // TODO: ABS, NABS from the SelectPatternResult
897 }
898
Philip Reames854a84c2016-02-12 00:09:18 +0000899 // Can we constrain the facts about the true and false values by using the
900 // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
901 // TODO: We could potentially refine an overdefined true value above.
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000902 Value *Cond = SI->getCondition();
Artur Pilipenko933c07a2016-08-10 13:38:07 +0000903 TrueVal = intersect(TrueVal,
904 getValueFromCondition(SI->getTrueValue(), Cond, true));
905 FalseVal = intersect(FalseVal,
906 getValueFromCondition(SI->getFalseValue(), Cond, false));
Philip Reames854a84c2016-02-12 00:09:18 +0000907
Artur Pilipenko2e19f592016-08-02 16:20:48 +0000908 // Handle clamp idioms such as:
909 // %24 = constantrange<0, 17>
910 // %39 = icmp eq i32 %24, 0
911 // %40 = add i32 %24, -1
912 // %siv.next = select i1 %39, i32 16, i32 %40
913 // %siv.next = constantrange<0, 17> not <-1, 17>
914 // In general, this can handle any clamp idiom which tests the edge
915 // condition via an equality or inequality.
916 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
Philip Reamesadf0e352016-02-26 22:53:59 +0000917 ICmpInst::Predicate Pred = ICI->getPredicate();
918 Value *A = ICI->getOperand(0);
919 if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
920 auto addConstants = [](ConstantInt *A, ConstantInt *B) {
921 assert(A->getType() == B->getType());
922 return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
923 };
924 // See if either input is A + C2, subject to the constraint from the
925 // condition that A != C when that input is used. We can assume that
926 // that input doesn't include C + C2.
927 ConstantInt *CIAdded;
928 switch (Pred) {
Philip Reames70b39182016-02-27 05:18:30 +0000929 default: break;
Philip Reamesadf0e352016-02-26 22:53:59 +0000930 case ICmpInst::ICMP_EQ:
931 if (match(SI->getFalseValue(), m_Add(m_Specific(A),
932 m_ConstantInt(CIAdded)))) {
933 auto ResNot = addConstants(CIBase, CIAdded);
934 FalseVal = intersect(FalseVal,
Florian Hahn8af01572017-09-28 11:09:22 +0000935 ValueLatticeElement::getNot(ResNot));
Philip Reamesadf0e352016-02-26 22:53:59 +0000936 }
937 break;
938 case ICmpInst::ICMP_NE:
939 if (match(SI->getTrueValue(), m_Add(m_Specific(A),
940 m_ConstantInt(CIAdded)))) {
941 auto ResNot = addConstants(CIBase, CIAdded);
942 TrueVal = intersect(TrueVal,
Florian Hahn8af01572017-09-28 11:09:22 +0000943 ValueLatticeElement::getNot(ResNot));
Philip Reamesadf0e352016-02-26 22:53:59 +0000944 }
945 break;
946 };
947 }
948 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +0000949
Florian Hahn8af01572017-09-28 11:09:22 +0000950 ValueLatticeElement Result; // Start Undefined.
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000951 Result.mergeIn(TrueVal, DL);
952 Result.mergeIn(FalseVal, DL);
Philip Reamesc0bdb0c2016-02-01 22:57:53 +0000953 BBLV = Result;
954 return true;
955}
956
John Regehr3a1c9d52018-11-21 05:24:12 +0000957Optional<ConstantRange> LazyValueInfoImpl::getRangeForOperand(unsigned Op,
958 Instruction *I,
959 BasicBlock *BB) {
960 if (!hasBlockValue(I->getOperand(Op), BB))
961 if (pushBlockValue(std::make_pair(BB, I->getOperand(Op))))
962 return None;
963
964 const unsigned OperandBitWidth =
965 DL.getTypeSizeInBits(I->getOperand(Op)->getType());
966 ConstantRange Range = ConstantRange(OperandBitWidth);
967 if (hasBlockValue(I->getOperand(Op), BB)) {
968 ValueLatticeElement Val = getBlockValue(I->getOperand(Op), BB);
969 intersectAssumeOrGuardBlockValueConstantRange(I->getOperand(Op), Val, I);
970 if (Val.isConstantRange())
971 Range = Val.getConstantRange();
972 }
973 return Range;
974}
975
Florian Hahn8af01572017-09-28 11:09:22 +0000976bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
Craig Topper0e5f1092017-06-03 07:47:08 +0000977 CastInst *CI,
978 BasicBlock *BB) {
979 if (!CI->getOperand(0)->getType()->isSized()) {
Philip Reamese5030e82016-04-26 22:52:30 +0000980 // Without knowing how wide the input is, we can't analyze it in any useful
981 // way.
Florian Hahn8af01572017-09-28 11:09:22 +0000982 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamese5030e82016-04-26 22:52:30 +0000983 return true;
984 }
Philip Reamesf105db42016-04-26 23:27:33 +0000985
986 // Filter out casts we don't know how to reason about before attempting to
987 // recurse on our operand. This can cut a long search short if we know we're
988 // not going to be able to get any useful information anways.
Craig Topper0e5f1092017-06-03 07:47:08 +0000989 switch (CI->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +0000990 case Instruction::Trunc:
991 case Instruction::SExt:
992 case Instruction::ZExt:
993 case Instruction::BitCast:
994 break;
995 default:
996 // Unhandled instructions are overdefined.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000997 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
998 << "' - overdefined (unknown cast).\n");
Florian Hahn8af01572017-09-28 11:09:22 +0000999 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001000 return true;
1001 }
1002
Philip Reames38c87c22016-04-26 21:48:16 +00001003 // Figure out the range of the LHS. If that fails, we still apply the
1004 // transfer rule on the full set since we may be able to locally infer
1005 // interesting facts.
John Regehr3a1c9d52018-11-21 05:24:12 +00001006 Optional<ConstantRange> LHSRes = getRangeForOperand(0, CI, BB);
1007 if (!LHSRes.hasValue())
1008 // More work to do before applying this transfer rule.
1009 return false;
1010 ConstantRange LHSRange = LHSRes.getValue();
Nick Lewycky55a700b2010-12-18 01:00:40 +00001011
Craig Toppera803d5b2017-06-03 07:47:14 +00001012 const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
Philip Reames66715772016-04-25 18:30:31 +00001013
1014 // NOTE: We're currently limited by the set of operations that ConstantRange
1015 // can evaluate symbolically. Enhancing that set will allows us to analyze
1016 // more definitions.
Florian Hahn8af01572017-09-28 11:09:22 +00001017 BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
1018 ResultBitWidth));
Philip Reames66715772016-04-25 18:30:31 +00001019 return true;
1020}
1021
Florian Hahn8af01572017-09-28 11:09:22 +00001022bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
1023 BinaryOperator *BO,
1024 BasicBlock *BB) {
Philip Reames66715772016-04-25 18:30:31 +00001025
Craig Topper3778c892017-06-02 16:33:13 +00001026 assert(BO->getOperand(0)->getType()->isSized() &&
Philip Reames053c2a62016-04-26 23:10:35 +00001027 "all operands to binary operators are sized");
Philip Reamesf105db42016-04-26 23:27:33 +00001028
1029 // Filter out operators we don't know how to reason about before attempting to
1030 // recurse on our operand(s). This can cut a long search short if we know
Craig Topper84a9f162017-06-02 16:21:13 +00001031 // we're not going to be able to get any useful information anyways.
Craig Topper3778c892017-06-02 16:33:13 +00001032 switch (BO->getOpcode()) {
Philip Reamesf105db42016-04-26 23:27:33 +00001033 case Instruction::Add:
1034 case Instruction::Sub:
1035 case Instruction::Mul:
1036 case Instruction::UDiv:
1037 case Instruction::Shl:
1038 case Instruction::LShr:
Max Kazantsev1acab002017-12-18 14:23:30 +00001039 case Instruction::AShr:
Philip Reamesf105db42016-04-26 23:27:33 +00001040 case Instruction::And:
1041 case Instruction::Or:
1042 // continue into the code below
1043 break;
1044 default:
1045 // Unhandled instructions are overdefined.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001046 LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
1047 << "' - overdefined (unknown binary operator).\n");
Florian Hahn8af01572017-09-28 11:09:22 +00001048 BBLV = ValueLatticeElement::getOverdefined();
Philip Reamesf105db42016-04-26 23:27:33 +00001049 return true;
1050 };
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001051
John Regehr3a1c9d52018-11-21 05:24:12 +00001052 // Figure out the ranges of the operands. If that fails, use a
1053 // conservative range, but apply the transfer rule anyways. This
1054 // lets us pick up facts from expressions like "and i32 (call i32
1055 // @foo()), 32"
1056 Optional<ConstantRange> LHSRes = getRangeForOperand(0, BO, BB);
1057 Optional<ConstantRange> RHSRes = getRangeForOperand(1, BO, BB);
Philip Reames053c2a62016-04-26 23:10:35 +00001058
John Regehr3a1c9d52018-11-21 05:24:12 +00001059 if (!LHSRes.hasValue() || !RHSRes.hasValue())
1060 // More work to do before applying this transfer rule.
1061 return false;
Philip Reames66715772016-04-25 18:30:31 +00001062
John Regehr3a1c9d52018-11-21 05:24:12 +00001063 ConstantRange LHSRange = LHSRes.getValue();
1064 ConstantRange RHSRange = RHSRes.getValue();
Philip Reames66715772016-04-25 18:30:31 +00001065
Owen Anderson80d19f02010-08-18 21:11:37 +00001066 // NOTE: We're currently limited by the set of operations that ConstantRange
1067 // can evaluate symbolically. Enhancing that set will allows us to analyze
1068 // more definitions.
Craig Topper3778c892017-06-02 16:33:13 +00001069 Instruction::BinaryOps BinOp = BO->getOpcode();
Florian Hahn8af01572017-09-28 11:09:22 +00001070 BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001071 return true;
Chris Lattner741c94c2009-11-11 00:22:30 +00001072}
1073
Florian Hahn8af01572017-09-28 11:09:22 +00001074static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
1075 bool isTrueDest) {
Artur Pilipenko21472912016-08-08 14:08:37 +00001076 Value *LHS = ICI->getOperand(0);
1077 Value *RHS = ICI->getOperand(1);
1078 CmpInst::Predicate Predicate = ICI->getPredicate();
1079
1080 if (isa<Constant>(RHS)) {
1081 if (ICI->isEquality() && LHS == Val) {
Hal Finkel7e184492014-09-07 20:29:59 +00001082 // We know that V has the RHS constant if this is a true SETEQ or
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001083 // false SETNE.
Artur Pilipenko21472912016-08-08 14:08:37 +00001084 if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
Florian Hahn8af01572017-09-28 11:09:22 +00001085 return ValueLatticeElement::get(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001086 else
Florian Hahn8af01572017-09-28 11:09:22 +00001087 return ValueLatticeElement::getNot(cast<Constant>(RHS));
Hal Finkel7e184492014-09-07 20:29:59 +00001088 }
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001089 }
Hal Finkel7e184492014-09-07 20:29:59 +00001090
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001091 if (!Val->getType()->isIntegerTy())
Florian Hahn8af01572017-09-28 11:09:22 +00001092 return ValueLatticeElement::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001093
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001094 // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
1095 // range of Val guaranteed by the condition. Recognize comparisons in the from
1096 // of:
1097 // icmp <pred> Val, ...
Artur Pilipenko63562582016-08-12 10:05:11 +00001098 // icmp <pred> (add Val, Offset), ...
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001099 // The latter is the range checking idiom that InstCombine produces. Subtract
1100 // the offset from the allowed range for RHS in this case.
Artur Pilipenkoeed618d2016-08-08 14:33:11 +00001101
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001102 // Val or (add Val, Offset) can be on either hand of the comparison
1103 if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
1104 std::swap(LHS, RHS);
1105 Predicate = CmpInst::getSwappedPredicate(Predicate);
1106 }
Hal Finkel7e184492014-09-07 20:29:59 +00001107
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001108 ConstantInt *Offset = nullptr;
Artur Pilipenko63562582016-08-12 10:05:11 +00001109 if (LHS != Val)
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001110 match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
Hal Finkel7e184492014-09-07 20:29:59 +00001111
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001112 if (LHS == Val || Offset) {
1113 // Calculate the range of values that are allowed by the comparison
1114 ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
1115 /*isFullSet=*/true);
1116 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
1117 RHSRange = ConstantRange(CI->getValue());
Artur Pilipenko6669f252016-08-12 10:14:11 +00001118 else if (Instruction *I = dyn_cast<Instruction>(RHS))
1119 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1120 RHSRange = getConstantRangeFromMetadata(*Ranges);
Artur Pilipenkoc710a462016-08-09 14:50:08 +00001121
1122 // If we're interested in the false dest, invert the condition
1123 CmpInst::Predicate Pred =
1124 isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
1125 ConstantRange TrueValues =
1126 ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
1127
1128 if (Offset) // Apply the offset from above.
1129 TrueValues = TrueValues.subtract(Offset->getValue());
1130
Florian Hahn8af01572017-09-28 11:09:22 +00001131 return ValueLatticeElement::getRange(std::move(TrueValues));
Hal Finkel7e184492014-09-07 20:29:59 +00001132 }
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001133
Florian Hahn8af01572017-09-28 11:09:22 +00001134 return ValueLatticeElement::getOverdefined();
Hal Finkel7e184492014-09-07 20:29:59 +00001135}
1136
Florian Hahn8af01572017-09-28 11:09:22 +00001137static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001138getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001139 DenseMap<Value*, ValueLatticeElement> &Visited);
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001140
Florian Hahn8af01572017-09-28 11:09:22 +00001141static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001142getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001143 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001144 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
1145 return getValueFromICmpCondition(Val, ICI, isTrueDest);
1146
1147 // Handle conditions in the form of (cond1 && cond2), we know that on the
Craig Topperb60f8662017-06-23 01:08:16 +00001148 // true dest path both of the conditions hold. Similarly for conditions of
1149 // the form (cond1 || cond2), we know that on the false dest path neither
1150 // condition holds.
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001151 BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
Craig Topperb60f8662017-06-23 01:08:16 +00001152 if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
1153 (!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
Florian Hahn8af01572017-09-28 11:09:22 +00001154 return ValueLatticeElement::getOverdefined();
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001155
Brian M. Rzycki252165b2018-03-13 18:14:10 +00001156 // Prevent infinite recursion if Cond references itself as in this example:
1157 // Cond: "%tmp4 = and i1 %tmp4, undef"
1158 // BL: "%tmp4 = and i1 %tmp4, undef"
1159 // BR: "i1 undef"
1160 Value *BL = BO->getOperand(0);
1161 Value *BR = BO->getOperand(1);
1162 if (BL == Cond || BR == Cond)
1163 return ValueLatticeElement::getOverdefined();
1164
1165 return intersect(getValueFromCondition(Val, BL, isTrueDest, Visited),
1166 getValueFromCondition(Val, BR, isTrueDest, Visited));
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001167}
1168
Florian Hahn8af01572017-09-28 11:09:22 +00001169static ValueLatticeElement
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001170getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
Florian Hahn8af01572017-09-28 11:09:22 +00001171 DenseMap<Value*, ValueLatticeElement> &Visited) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001172 auto I = Visited.find(Cond);
1173 if (I != Visited.end())
1174 return I->second;
Artur Pilipenkob6230882016-08-12 15:08:15 +00001175
1176 auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
1177 Visited[Cond] = Result;
1178 return Result;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001179}
1180
Florian Hahn8af01572017-09-28 11:09:22 +00001181ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
1182 bool isTrueDest) {
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001183 assert(Cond && "precondition");
Florian Hahn8af01572017-09-28 11:09:22 +00001184 DenseMap<Value*, ValueLatticeElement> Visited;
Artur Pilipenkofd223d52016-08-10 15:13:15 +00001185 return getValueFromCondition(Val, Cond, isTrueDest, Visited);
1186}
1187
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001188// Return true if Usr has Op as an operand, otherwise false.
1189static bool usesOperand(User *Usr, Value *Op) {
1190 return find(Usr->operands(), Op) != Usr->op_end();
1191}
1192
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001193// Return true if the instruction type of Val is supported by
1194// constantFoldUser(). Currently CastInst and BinaryOperator only. Call this
1195// before calling constantFoldUser() to find out if it's even worth attempting
1196// to call it.
1197static bool isOperationFoldable(User *Usr) {
1198 return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
1199}
1200
1201// Check if Usr can be simplified to an integer constant when the value of one
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001202// of its operands Op is an integer constant OpConstVal. If so, return it as an
1203// lattice value range with a single element or otherwise return an overdefined
1204// lattice value.
Florian Hahn8af01572017-09-28 11:09:22 +00001205static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
1206 const APInt &OpConstVal,
1207 const DataLayout &DL) {
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001208 assert(isOperationFoldable(Usr) && "Precondition");
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001209 Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001210 // Check if Usr can be simplified to a constant.
1211 if (auto *CI = dyn_cast<CastInst>(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001212 assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
1213 if (auto *C = dyn_cast_or_null<ConstantInt>(
1214 SimplifyCastInst(CI->getOpcode(), OpConst,
1215 CI->getDestTy(), DL))) {
Florian Hahn8af01572017-09-28 11:09:22 +00001216 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001217 }
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001218 } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001219 bool Op0Match = BO->getOperand(0) == Op;
1220 bool Op1Match = BO->getOperand(1) == Op;
1221 assert((Op0Match || Op1Match) &&
1222 "Operand 0 nor Operand 1 isn't a match");
1223 Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
1224 Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
1225 if (auto *C = dyn_cast_or_null<ConstantInt>(
1226 SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
Florian Hahn8af01572017-09-28 11:09:22 +00001227 return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001228 }
1229 }
Florian Hahn8af01572017-09-28 11:09:22 +00001230 return ValueLatticeElement::getOverdefined();
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001231}
1232
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001233/// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
Philip Reames13f73242016-02-01 23:21:11 +00001234/// Val is not constrained on the edge. Result is unspecified if return value
1235/// is false.
Nuno Lopese6e04902012-06-28 01:16:18 +00001236static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
Florian Hahn8af01572017-09-28 11:09:22 +00001237 BasicBlock *BBTo, ValueLatticeElement &Result) {
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001238 // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
Chris Lattner77358782009-11-15 20:02:12 +00001239 // know that v != 0.
Chris Lattner19019ea2009-11-11 22:48:44 +00001240 if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
1241 // If this is a conditional branch and only one successor goes to BBTo, then
Sanjay Patel938e2792015-01-09 16:35:37 +00001242 // we may be able to infer something from the condition.
Chris Lattner19019ea2009-11-11 22:48:44 +00001243 if (BI->isConditional() &&
1244 BI->getSuccessor(0) != BI->getSuccessor(1)) {
1245 bool isTrueDest = BI->getSuccessor(0) == BBTo;
1246 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
1247 "BBTo isn't a successor of BBFrom");
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001248 Value *Condition = BI->getCondition();
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001249
Chris Lattner19019ea2009-11-11 22:48:44 +00001250 // If V is the condition of the branch itself, then we know exactly what
1251 // it is.
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001252 if (Condition == Val) {
Florian Hahn8af01572017-09-28 11:09:22 +00001253 Result = ValueLatticeElement::get(ConstantInt::get(
Owen Anderson185fe002010-08-10 20:03:09 +00001254 Type::getInt1Ty(Val->getContext()), isTrueDest));
Nick Lewycky55a700b2010-12-18 01:00:40 +00001255 return true;
1256 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001257
Chris Lattner19019ea2009-11-11 22:48:44 +00001258 // If the condition of the branch is an equality comparison, we may be
1259 // able to infer the value.
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001260 Result = getValueFromCondition(Val, Condition, isTrueDest);
1261 if (!Result.isOverdefined())
1262 return true;
1263
1264 if (User *Usr = dyn_cast<User>(Val)) {
1265 assert(Result.isOverdefined() && "Result isn't overdefined");
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001266 // Check with isOperationFoldable() first to avoid linearly iterating
1267 // over the operands unnecessarily which can be expensive for
1268 // instructions with many operands.
1269 if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001270 const DataLayout &DL = BBTo->getModule()->getDataLayout();
1271 if (usesOperand(Usr, Condition)) {
1272 // If Val has Condition as an operand and Val can be folded into a
1273 // constant with either Condition == true or Condition == false,
1274 // propagate the constant.
1275 // eg.
1276 // ; %Val is true on the edge to %then.
1277 // %Val = and i1 %Condition, true.
1278 // br %Condition, label %then, label %else
1279 APInt ConditionVal(1, isTrueDest ? 1 : 0);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001280 Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001281 } else {
1282 // If one of Val's operand has an inferred value, we may be able to
1283 // infer the value of Val.
1284 // eg.
1285 // ; %Val is 94 on the edge to %then.
1286 // %Val = add i8 %Op, 1
1287 // %Condition = icmp eq i8 %Op, 93
1288 // br i1 %Condition, label %then, label %else
1289 for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
1290 Value *Op = Usr->getOperand(i);
Florian Hahn8af01572017-09-28 11:09:22 +00001291 ValueLatticeElement OpLatticeVal =
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001292 getValueFromCondition(Op, Condition, isTrueDest);
1293 if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001294 Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001295 break;
1296 }
1297 }
1298 }
1299 }
1300 }
Artur Pilipenko933c07a2016-08-10 13:38:07 +00001301 if (!Result.isOverdefined())
Artur Pilipenko2e19f592016-08-02 16:20:48 +00001302 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001303 }
1304 }
Chris Lattner77358782009-11-15 20:02:12 +00001305
1306 // If the edge was formed by a switch on the value, then we may know exactly
1307 // what it is.
1308 if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001309 Value *Condition = SI->getCondition();
1310 if (!isa<IntegerType>(Val->getType()))
Nuno Lopes8650fb82012-06-28 16:13:37 +00001311 return false;
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001312 bool ValUsesConditionAndMayBeFoldable = false;
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001313 if (Condition != Val) {
1314 // Check if Val has Condition as an operand.
1315 if (User *Usr = dyn_cast<User>(Val))
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001316 ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
1317 usesOperand(Usr, Condition);
1318 if (!ValUsesConditionAndMayBeFoldable)
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001319 return false;
1320 }
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001321 assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001322 "Condition != Val nor Val doesn't use Condition");
Nuno Lopes8650fb82012-06-28 16:13:37 +00001323
1324 bool DefaultCase = SI->getDefaultDest() == BBTo;
1325 unsigned BitWidth = Val->getType()->getIntegerBitWidth();
1326 ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
1327
Chandler Carruth927d8e62017-04-12 07:27:28 +00001328 for (auto Case : SI->cases()) {
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001329 APInt CaseValue = Case.getCaseValue()->getValue();
1330 ConstantRange EdgeVal(CaseValue);
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001331 if (ValUsesConditionAndMayBeFoldable) {
1332 User *Usr = cast<User>(Val);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001333 const DataLayout &DL = BBTo->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001334 ValueLatticeElement EdgeLatticeVal =
Hiroshi Yamauchiccd412f2017-08-10 02:23:14 +00001335 constantFoldUser(Usr, Condition, CaseValue, DL);
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001336 if (EdgeLatticeVal.isOverdefined())
1337 return false;
1338 EdgeVal = EdgeLatticeVal.getConstantRange();
1339 }
Manman Renf3fedb62012-09-05 23:45:58 +00001340 if (DefaultCase) {
1341 // It is possible that the default destination is the destination of
Hiroshi Yamauchi144ee2b2017-08-03 21:11:30 +00001342 // some cases. We cannot perform difference for those cases.
1343 // We know Condition != CaseValue in BBTo. In some cases we can use
1344 // this to infer Val == f(Condition) is != f(CaseValue). For now, we
1345 // only do this when f is identity (i.e. Val == Condition), but we
1346 // should be able to do this for any injective f.
1347 if (Case.getCaseSuccessor() != BBTo && Condition == Val)
Manman Renf3fedb62012-09-05 23:45:58 +00001348 EdgesVals = EdgesVals.difference(EdgeVal);
Chandler Carruth927d8e62017-04-12 07:27:28 +00001349 } else if (Case.getCaseSuccessor() == BBTo)
Nuno Lopesac593802012-05-18 21:02:10 +00001350 EdgesVals = EdgesVals.unionWith(EdgeVal);
Chris Lattner77358782009-11-15 20:02:12 +00001351 }
Florian Hahn8af01572017-09-28 11:09:22 +00001352 Result = ValueLatticeElement::getRange(std::move(EdgesVals));
Nuno Lopes8650fb82012-06-28 16:13:37 +00001353 return true;
Chris Lattner77358782009-11-15 20:02:12 +00001354 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001355 return false;
1356}
1357
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001358/// Compute the value of Val on the edge BBFrom -> BBTo or the value at
Sanjay Patel938e2792015-01-09 16:35:37 +00001359/// the basic block if the edge does not constrain Val.
Philip Reames92e5e1b2016-09-12 21:46:58 +00001360bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
Florian Hahn8af01572017-09-28 11:09:22 +00001361 BasicBlock *BBTo,
1362 ValueLatticeElement &Result,
Xin Tong68ea9aa2017-02-24 20:59:26 +00001363 Instruction *CxtI) {
Nuno Lopese6e04902012-06-28 01:16:18 +00001364 // If already a constant, there is nothing to compute.
1365 if (Constant *VC = dyn_cast<Constant>(Val)) {
Florian Hahn8af01572017-09-28 11:09:22 +00001366 Result = ValueLatticeElement::get(VC);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001367 return true;
1368 }
Nuno Lopese6e04902012-06-28 01:16:18 +00001369
Florian Hahn8af01572017-09-28 11:09:22 +00001370 ValueLatticeElement LocalResult;
Philip Reames44456b82016-02-02 03:15:40 +00001371 if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
1372 // If we couldn't constrain the value on the edge, LocalResult doesn't
1373 // provide any information.
Florian Hahn8af01572017-09-28 11:09:22 +00001374 LocalResult = ValueLatticeElement::getOverdefined();
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001375
Philip Reames44456b82016-02-02 03:15:40 +00001376 if (hasSingleValue(LocalResult)) {
1377 // Can't get any more precise here
1378 Result = LocalResult;
Nuno Lopese6e04902012-06-28 01:16:18 +00001379 return true;
1380 }
1381
1382 if (!hasBlockValue(Val, BBFrom)) {
Hans Wennborg45172ac2014-11-25 17:23:05 +00001383 if (pushBlockValue(std::make_pair(BBFrom, Val)))
1384 return false;
Philip Reames44456b82016-02-02 03:15:40 +00001385 // No new information.
1386 Result = LocalResult;
Hans Wennborg45172ac2014-11-25 17:23:05 +00001387 return true;
Nuno Lopese6e04902012-06-28 01:16:18 +00001388 }
1389
Philip Reames44456b82016-02-02 03:15:40 +00001390 // Try to intersect ranges of the BB and the constraint on the edge.
Florian Hahn8af01572017-09-28 11:09:22 +00001391 ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001392 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1393 BBFrom->getTerminator());
Hal Finkel2400c962014-10-16 00:40:05 +00001394 // We can use the context instruction (generically the ultimate instruction
1395 // the calling pass is trying to simplify) here, even though the result of
1396 // this function is generally cached when called from the solve* functions
1397 // (and that cached result might be used with queries using a different
1398 // context instruction), because when this function is called from the solve*
1399 // functions, the context instruction is not provided. When called from
Philip Reames92e5e1b2016-09-12 21:46:58 +00001400 // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
Hal Finkel2400c962014-10-16 00:40:05 +00001401 // but then the result is not cached.
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001402 intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
Philip Reames44456b82016-02-02 03:15:40 +00001403
1404 Result = intersect(LocalResult, InBlock);
Nuno Lopese6e04902012-06-28 01:16:18 +00001405 return true;
Chris Lattner19019ea2009-11-11 22:48:44 +00001406}
1407
Florian Hahn8af01572017-09-28 11:09:22 +00001408ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
1409 Instruction *CxtI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001410 LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
1411 << BB->getName() << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001412
Hans Wennborg45172ac2014-11-25 17:23:05 +00001413 assert(BlockValueStack.empty() && BlockValueSet.empty());
Philip Reamesbb781b42016-02-10 21:46:32 +00001414 if (!hasBlockValue(V, BB)) {
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001415 pushBlockValue(std::make_pair(BB, V));
Philip Reamesbb781b42016-02-10 21:46:32 +00001416 solve();
1417 }
Florian Hahn8af01572017-09-28 11:09:22 +00001418 ValueLatticeElement Result = getBlockValue(V, BB);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001419 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001420
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001421 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Hal Finkel7e184492014-09-07 20:29:59 +00001422 return Result;
1423}
1424
Florian Hahn8af01572017-09-28 11:09:22 +00001425ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001426 LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()
1427 << "'\n");
Hal Finkel7e184492014-09-07 20:29:59 +00001428
Philip Reamesbb781b42016-02-10 21:46:32 +00001429 if (auto *C = dyn_cast<Constant>(V))
Florian Hahn8af01572017-09-28 11:09:22 +00001430 return ValueLatticeElement::get(C);
Philip Reamesbb781b42016-02-10 21:46:32 +00001431
Florian Hahn8af01572017-09-28 11:09:22 +00001432 ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
Philip Reameseb3e9da2015-10-29 03:57:17 +00001433 if (auto *I = dyn_cast<Instruction>(V))
1434 Result = getFromRangeMetadata(I);
Artur Pilipenko2e8f82d2016-08-12 15:52:23 +00001435 intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
Philip Reames2c275cc2016-02-02 00:45:30 +00001436
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001437 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001438 return Result;
1439}
Chris Lattner19019ea2009-11-11 22:48:44 +00001440
Florian Hahn8af01572017-09-28 11:09:22 +00001441ValueLatticeElement LazyValueInfoImpl::
Hal Finkel7e184492014-09-07 20:29:59 +00001442getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1443 Instruction *CxtI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001444 LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
1445 << FromBB->getName() << "' to '" << ToBB->getName()
1446 << "'\n");
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001447
Florian Hahn8af01572017-09-28 11:09:22 +00001448 ValueLatticeElement Result;
Hal Finkel7e184492014-09-07 20:29:59 +00001449 if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
Nick Lewycky55a700b2010-12-18 01:00:40 +00001450 solve();
Hal Finkel7e184492014-09-07 20:29:59 +00001451 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
Nick Lewycky55a700b2010-12-18 01:00:40 +00001452 (void)WasFastQuery;
1453 assert(WasFastQuery && "More work to do after problem solved?");
1454 }
1455
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001456 LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
Chris Lattneraf025d32009-11-15 19:59:49 +00001457 return Result;
1458}
1459
Philip Reames92e5e1b2016-09-12 21:46:58 +00001460void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Philip Reames9db79482016-09-12 22:38:44 +00001461 BasicBlock *NewSucc) {
1462 TheCache.threadEdgeImpl(OldSucc, NewSucc);
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001463}
1464
Chris Lattneraf025d32009-11-15 19:59:49 +00001465//===----------------------------------------------------------------------===//
1466// LazyValueInfo Impl
1467//===----------------------------------------------------------------------===//
1468
Philip Reames92e5e1b2016-09-12 21:46:58 +00001469/// This lazily constructs the LazyValueInfoImpl.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001470static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1471 const DataLayout *DL,
Philip Reames92e5e1b2016-09-12 21:46:58 +00001472 DominatorTree *DT = nullptr) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001473 if (!PImpl) {
1474 assert(DL && "getCache() called with a null DataLayout");
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001475 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001476 }
Philip Reames92e5e1b2016-09-12 21:46:58 +00001477 return *static_cast<LazyValueInfoImpl*>(PImpl);
Chris Lattneraf025d32009-11-15 19:59:49 +00001478}
1479
Sean Silva687019f2016-06-13 22:01:25 +00001480bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001481 Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001482 const DataLayout &DL = F.getParent()->getDataLayout();
Hal Finkel7e184492014-09-07 20:29:59 +00001483
1484 DominatorTreeWrapperPass *DTWP =
1485 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Sean Silva687019f2016-06-13 22:01:25 +00001486 Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
1487 Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +00001488
Sean Silva687019f2016-06-13 22:01:25 +00001489 if (Info.PImpl)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001490 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
Hal Finkel7e184492014-09-07 20:29:59 +00001491
Owen Anderson208636f2010-08-18 18:39:01 +00001492 // Fully lazy.
1493 return false;
1494}
1495
Sean Silva687019f2016-06-13 22:01:25 +00001496void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chad Rosier43a33062011-12-02 01:26:24 +00001497 AU.setPreservesAll();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001498 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001499 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chad Rosier43a33062011-12-02 01:26:24 +00001500}
1501
Sean Silva687019f2016-06-13 22:01:25 +00001502LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
1503
1504LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
1505
Chris Lattneraf025d32009-11-15 19:59:49 +00001506void LazyValueInfo::releaseMemory() {
1507 // If the cache was allocated, free it.
1508 if (PImpl) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001509 delete &getImpl(PImpl, AC, nullptr);
Craig Topper9f008862014-04-15 04:59:12 +00001510 PImpl = nullptr;
Chris Lattneraf025d32009-11-15 19:59:49 +00001511 }
1512}
1513
Chandler Carrutha504f2b2017-01-23 06:35:12 +00001514bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
1515 FunctionAnalysisManager::Invalidator &Inv) {
1516 // We need to invalidate if we have either failed to preserve this analyses
1517 // result directly or if any of its dependencies have been invalidated.
1518 auto PAC = PA.getChecker<LazyValueAnalysis>();
1519 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
1520 (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
1521 return true;
1522
1523 return false;
1524}
1525
Sean Silva687019f2016-06-13 22:01:25 +00001526void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
1527
Florian Hahn8af01572017-09-28 11:09:22 +00001528LazyValueInfo LazyValueAnalysis::run(Function &F,
1529 FunctionAnalysisManager &FAM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001530 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
Sean Silva687019f2016-06-13 22:01:25 +00001531 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1532 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1533
Anna Thomasa10e3e42017-03-12 14:06:41 +00001534 return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
Sean Silva687019f2016-06-13 22:01:25 +00001535}
1536
Wei Mif160e342016-09-15 06:28:34 +00001537/// Returns true if we can statically tell that this value will never be a
1538/// "useful" constant. In practice, this means we've got something like an
1539/// alloca or a malloc call for which a comparison against a constant can
1540/// only be guarding dead code. Note that we are potentially giving up some
1541/// precision in dead code (a constant result) in favour of avoiding a
1542/// expensive search for a easily answered common query.
1543static bool isKnownNonConstant(Value *V) {
1544 V = V->stripPointerCasts();
1545 // The return val of alloc cannot be a Constant.
1546 if (isa<AllocaInst>(V))
1547 return true;
1548 return false;
1549}
1550
Hal Finkel7e184492014-09-07 20:29:59 +00001551Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
1552 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001553 // Bail out early if V is known not to be a Constant.
1554 if (isKnownNonConstant(V))
1555 return nullptr;
1556
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001557 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001558 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001559 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001560
Chris Lattner19019ea2009-11-11 22:48:44 +00001561 if (Result.isConstant())
1562 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001563 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001564 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson38f6b7f2010-08-27 23:29:38 +00001565 if (const APInt *SingleVal = CR.getSingleElement())
1566 return ConstantInt::get(V->getContext(), *SingleVal);
1567 }
Craig Topper9f008862014-04-15 04:59:12 +00001568 return nullptr;
Chris Lattner19019ea2009-11-11 22:48:44 +00001569}
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001570
John Regehre1c481d2016-05-02 19:58:00 +00001571ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00001572 Instruction *CxtI) {
John Regehre1c481d2016-05-02 19:58:00 +00001573 assert(V->getType()->isIntegerTy());
1574 unsigned Width = V->getType()->getIntegerBitWidth();
1575 const DataLayout &DL = BB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001576 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001577 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
John Regehre1c481d2016-05-02 19:58:00 +00001578 if (Result.isUndefined())
1579 return ConstantRange(Width, /*isFullSet=*/false);
1580 if (Result.isConstantRange())
1581 return Result.getConstantRange();
Artur Pilipenkoa4b6a702016-08-10 12:54:54 +00001582 // We represent ConstantInt constants as constant ranges but other kinds
1583 // of integer constants, i.e. ConstantExpr will be tagged as constants
1584 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1585 "ConstantInt value must be represented as constantrange");
Davide Italianobd543d02016-05-25 22:29:34 +00001586 return ConstantRange(Width, /*isFullSet=*/true);
John Regehre1c481d2016-05-02 19:58:00 +00001587}
1588
Sanjay Patel2a385e22015-01-09 16:47:20 +00001589/// Determine whether the specified value is known to be a
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001590/// constant on the specified edge. Return null if not.
Chris Lattnerd5e25432009-11-12 01:29:10 +00001591Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
Hal Finkel7e184492014-09-07 20:29:59 +00001592 BasicBlock *ToBB,
1593 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001594 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001595 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001596 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Chandler Carruth66b31302015-01-04 12:03:27 +00001597
Chris Lattnerd5e25432009-11-12 01:29:10 +00001598 if (Result.isConstant())
1599 return Result.getConstant();
Nick Lewycky11678bd2010-12-15 18:57:18 +00001600 if (Result.isConstantRange()) {
Craig Topper2b195fd2017-05-06 03:35:15 +00001601 const ConstantRange &CR = Result.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001602 if (const APInt *SingleVal = CR.getSingleElement())
1603 return ConstantInt::get(V->getContext(), *SingleVal);
1604 }
Craig Topper9f008862014-04-15 04:59:12 +00001605 return nullptr;
Chris Lattnerd5e25432009-11-12 01:29:10 +00001606}
1607
Craig Topper2c20c422017-06-23 05:41:35 +00001608ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
1609 BasicBlock *FromBB,
1610 BasicBlock *ToBB,
1611 Instruction *CxtI) {
1612 unsigned Width = V->getType()->getIntegerBitWidth();
1613 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001614 ValueLatticeElement Result =
Craig Topper2c20c422017-06-23 05:41:35 +00001615 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1616
1617 if (Result.isUndefined())
1618 return ConstantRange(Width, /*isFullSet=*/false);
1619 if (Result.isConstantRange())
1620 return Result.getConstantRange();
1621 // We represent ConstantInt constants as constant ranges but other kinds
1622 // of integer constants, i.e. ConstantExpr will be tagged as constants
1623 assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
1624 "ConstantInt value must be represented as constantrange");
1625 return ConstantRange(Width, /*isFullSet=*/true);
1626}
1627
Florian Hahn8af01572017-09-28 11:09:22 +00001628static LazyValueInfo::Tristate
1629getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
1630 const DataLayout &DL, TargetLibraryInfo *TLI) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001631 // If we know the value is a constant, evaluate the conditional.
Craig Topper9f008862014-04-15 04:59:12 +00001632 Constant *Res = nullptr;
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001633 if (Val.isConstant()) {
1634 Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
Nick Lewycky11678bd2010-12-15 18:57:18 +00001635 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
Hal Finkel7e184492014-09-07 20:29:59 +00001636 return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1637 return LazyValueInfo::Unknown;
Chris Lattneraf025d32009-11-15 19:59:49 +00001638 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001639
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001640 if (Val.isConstantRange()) {
Owen Andersonc62f7042010-08-24 07:55:44 +00001641 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Hal Finkel7e184492014-09-07 20:29:59 +00001642 if (!CI) return LazyValueInfo::Unknown;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001643
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001644 const ConstantRange &CR = Val.getConstantRange();
Owen Anderson185fe002010-08-10 20:03:09 +00001645 if (Pred == ICmpInst::ICMP_EQ) {
1646 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001647 return LazyValueInfo::False;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001648
Craig Topper79452482017-06-07 00:58:09 +00001649 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001650 return LazyValueInfo::True;
Owen Anderson185fe002010-08-10 20:03:09 +00001651 } else if (Pred == ICmpInst::ICMP_NE) {
1652 if (!CR.contains(CI->getValue()))
Hal Finkel7e184492014-09-07 20:29:59 +00001653 return LazyValueInfo::True;
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001654
Craig Topper79452482017-06-07 00:58:09 +00001655 if (CR.isSingleElement())
Hal Finkel7e184492014-09-07 20:29:59 +00001656 return LazyValueInfo::False;
Craig Topper31ce4ec2017-06-09 16:16:20 +00001657 } else {
1658 // Handle more complex predicates.
1659 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
1660 (ICmpInst::Predicate)Pred, CI->getValue());
1661 if (TrueValues.contains(CR))
1662 return LazyValueInfo::True;
1663 if (TrueValues.inverse().contains(CR))
1664 return LazyValueInfo::False;
Owen Anderson185fe002010-08-10 20:03:09 +00001665 }
Hal Finkel7e184492014-09-07 20:29:59 +00001666 return LazyValueInfo::Unknown;
Owen Anderson185fe002010-08-10 20:03:09 +00001667 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001668
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001669 if (Val.isNotConstant()) {
Chris Lattner565ee2f2009-11-12 04:36:58 +00001670 // If this is an equality comparison, we can try to fold it knowing that
1671 // "V != C1".
1672 if (Pred == ICmpInst::ICMP_EQ) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001673 // !C1 == C -> false iff C1 == C.
Chris Lattner565ee2f2009-11-12 04:36:58 +00001674 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001675 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001676 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001677 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001678 return LazyValueInfo::False;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001679 } else if (Pred == ICmpInst::ICMP_NE) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001680 // !C1 != C -> true iff C1 == C.
Chris Lattnerb0c0a0d2009-11-15 20:01:24 +00001681 Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Craig Topper6dd9dcf2017-06-09 21:18:16 +00001682 Val.getNotConstant(), C, DL,
Chad Rosier43a33062011-12-02 01:26:24 +00001683 TLI);
Chris Lattner565ee2f2009-11-12 04:36:58 +00001684 if (Res->isNullValue())
Hal Finkel7e184492014-09-07 20:29:59 +00001685 return LazyValueInfo::True;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001686 }
Hal Finkel7e184492014-09-07 20:29:59 +00001687 return LazyValueInfo::Unknown;
Chris Lattner565ee2f2009-11-12 04:36:58 +00001688 }
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001689
Hal Finkel7e184492014-09-07 20:29:59 +00001690 return LazyValueInfo::Unknown;
1691}
1692
Sanjay Patel2a385e22015-01-09 16:47:20 +00001693/// Determine whether the specified value comparison with a constant is known to
1694/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
Hal Finkel7e184492014-09-07 20:29:59 +00001695LazyValueInfo::Tristate
1696LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
1697 BasicBlock *FromBB, BasicBlock *ToBB,
1698 Instruction *CxtI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001699 const DataLayout &DL = FromBB->getModule()->getDataLayout();
Florian Hahn8af01572017-09-28 11:09:22 +00001700 ValueLatticeElement Result =
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001701 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
Hal Finkel7e184492014-09-07 20:29:59 +00001702
1703 return getPredicateResult(Pred, C, Result, DL, TLI);
1704}
1705
1706LazyValueInfo::Tristate
1707LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
1708 Instruction *CxtI) {
Wei Mif160e342016-09-15 06:28:34 +00001709 // Is or is not NonNull are common predicates being queried. If
Nuno Lopes404f1062017-09-09 18:23:11 +00001710 // isKnownNonZero can tell us the result of the predicate, we can
Wei Mif160e342016-09-15 06:28:34 +00001711 // return it quickly. But this is only a fastpath, and falling
1712 // through would still be correct.
Nuno Lopes404f1062017-09-09 18:23:11 +00001713 const DataLayout &DL = CxtI->getModule()->getDataLayout();
Wei Mif160e342016-09-15 06:28:34 +00001714 if (V->getType()->isPointerTy() && C->isNullValue() &&
Nuno Lopes404f1062017-09-09 18:23:11 +00001715 isKnownNonZero(V->stripPointerCasts(), DL)) {
Wei Mif160e342016-09-15 06:28:34 +00001716 if (Pred == ICmpInst::ICMP_EQ)
1717 return LazyValueInfo::False;
1718 else if (Pred == ICmpInst::ICMP_NE)
1719 return LazyValueInfo::True;
1720 }
Florian Hahn8af01572017-09-28 11:09:22 +00001721 ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
Philip Reames66ab0f02015-06-16 00:49:59 +00001722 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1723 if (Ret != Unknown)
1724 return Ret;
Hal Finkel7e184492014-09-07 20:29:59 +00001725
Philip Reamesaeefae02015-11-04 01:47:04 +00001726 // Note: The following bit of code is somewhat distinct from the rest of LVI;
1727 // LVI as a whole tries to compute a lattice value which is conservatively
1728 // correct at a given location. In this case, we have a predicate which we
1729 // weren't able to prove about the merged result, and we're pushing that
1730 // predicate back along each incoming edge to see if we can prove it
1731 // separately for each input. As a motivating example, consider:
1732 // bb1:
1733 // %v1 = ... ; constantrange<1, 5>
1734 // br label %merge
1735 // bb2:
1736 // %v2 = ... ; constantrange<10, 20>
1737 // br label %merge
1738 // merge:
1739 // %phi = phi [%v1, %v2] ; constantrange<1,20>
1740 // %pred = icmp eq i32 %phi, 8
1741 // We can't tell from the lattice value for '%phi' that '%pred' is false
1742 // along each path, but by checking the predicate over each input separately,
1743 // we can.
1744 // We limit the search to one step backwards from the current BB and value.
1745 // We could consider extending this to search further backwards through the
1746 // CFG and/or value graph, but there are non-obvious compile time vs quality
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001747 // tradeoffs.
Philip Reames66ab0f02015-06-16 00:49:59 +00001748 if (CxtI) {
Philip Reamesbb11d622015-08-31 18:31:48 +00001749 BasicBlock *BB = CxtI->getParent();
1750
1751 // Function entry or an unreachable block. Bail to avoid confusing
1752 // analysis below.
1753 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1754 if (PI == PE)
1755 return Unknown;
1756
1757 // If V is a PHI node in the same block as the context, we need to ask
1758 // questions about the predicate as applied to the incoming value along
1759 // each edge. This is useful for eliminating cases where the predicate is
1760 // known along all incoming edges.
1761 if (auto *PHI = dyn_cast<PHINode>(V))
1762 if (PHI->getParent() == BB) {
1763 Tristate Baseline = Unknown;
1764 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
1765 Value *Incoming = PHI->getIncomingValue(i);
1766 BasicBlock *PredBB = PHI->getIncomingBlock(i);
NAKAMURA Takumif2529512016-07-04 01:26:27 +00001767 // Note that PredBB may be BB itself.
Philip Reamesbb11d622015-08-31 18:31:48 +00001768 Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
1769 CxtI);
NAKAMURA Takumi4cb46e62016-07-04 01:26:33 +00001770
Philip Reamesbb11d622015-08-31 18:31:48 +00001771 // Keep going as long as we've seen a consistent known result for
1772 // all inputs.
1773 Baseline = (i == 0) ? Result /* First iteration */
1774 : (Baseline == Result ? Baseline : Unknown); /* All others */
1775 if (Baseline == Unknown)
1776 break;
1777 }
1778 if (Baseline != Unknown)
1779 return Baseline;
NAKAMURA Takumibd072a92016-07-25 00:59:46 +00001780 }
Philip Reamesbb11d622015-08-31 18:31:48 +00001781
Philip Reames66ab0f02015-06-16 00:49:59 +00001782 // For a comparison where the V is outside this block, it's possible
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001783 // that we've branched on it before. Look to see if the value is known
Philip Reames66ab0f02015-06-16 00:49:59 +00001784 // on all incoming edges.
Philip Reamesbb11d622015-08-31 18:31:48 +00001785 if (!isa<Instruction>(V) ||
1786 cast<Instruction>(V)->getParent() != BB) {
Philip Reames66ab0f02015-06-16 00:49:59 +00001787 // For predecessor edge, determine if the comparison is true or false
Bruno Cardoso Lopes51fd2422015-07-28 15:53:21 +00001788 // on that edge. If they're all true or all false, we can conclude
Philip Reames66ab0f02015-06-16 00:49:59 +00001789 // the value of the comparison in this block.
1790 Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1791 if (Baseline != Unknown) {
1792 // Check that all remaining incoming values match the first one.
1793 while (++PI != PE) {
1794 Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1795 if (Ret != Baseline) break;
1796 }
1797 // If we terminated early, then one of the values didn't match.
1798 if (PI == PE) {
1799 return Baseline;
1800 }
1801 }
1802 }
1803 }
1804 return Unknown;
Chris Lattnerfde1f8d2009-11-11 02:08:33 +00001805}
1806
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001807void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
Nick Lewycky11678bd2010-12-15 18:57:18 +00001808 BasicBlock *NewSucc) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001809 if (PImpl) {
1810 const DataLayout &DL = PredBB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001811 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001812 }
Owen Anderson208636f2010-08-18 18:39:01 +00001813}
1814
1815void LazyValueInfo::eraseBlock(BasicBlock *BB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001816 if (PImpl) {
1817 const DataLayout &DL = BB->getModule()->getDataLayout();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001818 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001819 }
Owen Andersonaa7f66b2010-07-26 18:48:03 +00001820}
Anna Thomase27b39a2017-03-22 19:27:12 +00001821
1822
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001823void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
Anna Thomase27b39a2017-03-22 19:27:12 +00001824 if (PImpl) {
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001825 getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
Anna Thomase27b39a2017-03-22 19:27:12 +00001826 }
1827}
1828
Brian M. Rzyckif1a7df52018-02-16 16:35:17 +00001829void LazyValueInfo::disableDT() {
1830 if (PImpl)
1831 getImpl(PImpl, AC, DL, DT).disableDT();
1832}
1833
1834void LazyValueInfo::enableDT() {
1835 if (PImpl)
1836 getImpl(PImpl, AC, DL, DT).enableDT();
1837}
1838
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001839// Print the LVI for the function arguments at the start of each basic block.
1840void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
1841 const BasicBlock *BB, formatted_raw_ostream &OS) {
1842 // Find if there are latticevalues defined for arguments of the function.
1843 auto *F = BB->getParent();
1844 for (auto &Arg : F->args()) {
Florian Hahn8af01572017-09-28 11:09:22 +00001845 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001846 const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
1847 if (Result.isUndefined())
1848 continue;
1849 OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
1850 }
1851}
1852
1853// This function prints the LVI analysis for the instruction I at the beginning
1854// of various basic blocks. It relies on calculated values that are stored in
Xin Tongadb5bfe2018-04-24 07:38:07 +00001855// the LazyValueInfoCache, and in the absence of cached values, recalculate the
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001856// LazyValueInfo for `I`, and print that info.
1857void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
1858 const Instruction *I, formatted_raw_ostream &OS) {
1859
1860 auto *ParentBB = I->getParent();
1861 SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
1862 // We can generate (solve) LVI values only for blocks that are dominated by
1863 // the I's parent. However, to avoid generating LVI for all dominating blocks,
1864 // that contain redundant/uninteresting information, we print LVI for
1865 // blocks that may use this LVI information (such as immediate successor
1866 // blocks, and blocks that contain uses of `I`).
1867 auto printResult = [&](const BasicBlock *BB) {
1868 if (!BlocksContainingLVI.insert(BB).second)
1869 return;
Florian Hahn8af01572017-09-28 11:09:22 +00001870 ValueLatticeElement Result = LVIImpl->getValueInBlock(
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001871 const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
1872 OS << "; LatticeVal for: '" << *I << "' in BB: '";
1873 BB->printAsOperand(OS, false);
1874 OS << "' is: " << Result << "\n";
1875 };
1876
1877 printResult(ParentBB);
Hiroshi Inoue8f976ba2018-01-17 12:29:38 +00001878 // Print the LVI analysis results for the immediate successor blocks, that
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001879 // are dominated by `ParentBB`.
1880 for (auto *BBSucc : successors(ParentBB))
1881 if (DT.dominates(ParentBB, BBSucc))
1882 printResult(BBSucc);
1883
1884 // Print LVI in blocks where `I` is used.
1885 for (auto *U : I->users())
1886 if (auto *UseI = dyn_cast<Instruction>(U))
1887 if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
1888 printResult(UseI->getParent());
1889
1890}
1891
Anna Thomase27b39a2017-03-22 19:27:12 +00001892namespace {
1893// Printer class for LazyValueInfo results.
1894class LazyValueInfoPrinter : public FunctionPass {
1895public:
1896 static char ID; // Pass identification, replacement for typeid
1897 LazyValueInfoPrinter() : FunctionPass(ID) {
1898 initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
1899 }
1900
1901 void getAnalysisUsage(AnalysisUsage &AU) const override {
1902 AU.setPreservesAll();
1903 AU.addRequired<LazyValueInfoWrapperPass>();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001904 AU.addRequired<DominatorTreeWrapperPass>();
Anna Thomase27b39a2017-03-22 19:27:12 +00001905 }
1906
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001907 // Get the mandatory dominator tree analysis and pass this in to the
1908 // LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
Anna Thomase27b39a2017-03-22 19:27:12 +00001909 bool runOnFunction(Function &F) override {
1910 dbgs() << "LVI for function '" << F.getName() << "':\n";
1911 auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
Anna Thomas4acfc7e2017-06-06 19:25:31 +00001912 auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1913 LVI.printLVI(F, DTree, dbgs());
Anna Thomase27b39a2017-03-22 19:27:12 +00001914 return false;
1915 }
1916};
1917}
1918
1919char LazyValueInfoPrinter::ID = 0;
1920INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
1921 "Lazy Value Info Printer Pass", false, false)
1922INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
1923INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
1924 "Lazy Value Info Printer Pass", false, false)