2014-11-22 02:58:23 +08:00
|
|
|
//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
|
2009-11-11 08:22:30 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-11-11 08:22:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the interface for lazy computation of value constraint
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/LazyValueInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2018-11-21 13:24:12 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2017-08-04 05:11:30 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2015-01-15 10:16:27 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2017-09-28 19:09:22 +08:00
|
|
|
#include "llvm/Analysis/ValueLattice.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2017-03-23 03:27:12 +08:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-03-04 20:24:34 +08:00
|
|
|
#include "llvm/IR/ConstantRange.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-09-08 04:29:59 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2016-08-12 23:52:23 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
[LVI/CVP] Teach LVI about range metadata
Somewhat shockingly for an analysis pass which is computing constant ranges, LVI did not understand the ranges provided by range metadata.
As part of this change, I included a change to CVP primarily because doing so made it much easier to write small self contained test cases. CVP was previously only handling the non-local operand case, but given that LVI can sometimes figure out information about instructions standalone, I don't see any reason to restrict this. There could possibly be a compile time impact from this, but I suspect it should be minimal. If anyone has an example which substaintially regresses, please let me know. I could restrict the block local handling to ICmps feeding Terminator instructions if needed.
Note that this patch continues a somewhat bad practice in LVI. In many cases, we know facts about values, and separate context sensitive facts about values. LVI makes no effort to distinguish and will frequently cache the same value fact repeatedly for different contexts. I would like to change this, but that's a large enough change that I want it to go in separately with clear documentation of what's changing. Other examples of this include the non-null handling, and arguments.
As a meta comment: the entire motivation of this change was being able to write smaller (aka reasonable sized) test cases for a future patch teaching LVI about select instructions.
Differential Revision: http://reviews.llvm.org/D13543
llvm-svn: 251606
2015-10-29 11:57:17 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-03-04 19:08:18 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2014-03-04 19:17:44 +08:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2009-11-12 09:22:16 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-03-23 03:27:12 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-01-12 07:43:34 +08:00
|
|
|
#include <map>
|
2009-11-11 08:22:30 +08:00
|
|
|
using namespace llvm;
|
2012-03-02 23:34:43 +08:00
|
|
|
using namespace PatternMatch;
|
2009-11-11 08:22:30 +08:00
|
|
|
|
2014-04-22 10:48:03 +08:00
|
|
|
#define DEBUG_TYPE "lazy-value-info"
|
|
|
|
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
// This is the number of worklist items we will process to try to discover an
|
|
|
|
// answer for a given value.
|
|
|
|
static const unsigned MaxProcessedPerValue = 500;
|
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
char LazyValueInfoWrapperPass::ID = 0;
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
LazyValueInfoWrapperPass::LazyValueInfoWrapperPass() : FunctionPass(ID) {
|
|
|
|
initializeLazyValueInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2016-06-14 06:01:25 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
|
2011-12-02 09:26:24 +08:00
|
|
|
"Lazy Value Information Analysis", false, true)
|
2016-12-19 16:22:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2015-01-15 18:41:28 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2016-06-14 06:01:25 +08:00
|
|
|
INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Lazy Value Information Analysis", false, true)
|
2009-11-11 08:22:30 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-06-14 06:01:25 +08:00
|
|
|
FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); }
|
2009-11-11 08:22:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 01:53:26 +08:00
|
|
|
AnalysisKey LazyValueAnalysis::Key;
|
2009-11-11 10:08:33 +08:00
|
|
|
|
2016-02-03 06:03:19 +08:00
|
|
|
/// Returns true if this lattice value represents at most one possible value.
|
|
|
|
/// This is as precise as any lattice value can get while still representing
|
|
|
|
/// reachable code.
|
2017-09-28 19:09:22 +08:00
|
|
|
static bool hasSingleValue(const ValueLatticeElement &Val) {
|
2016-02-03 06:03:19 +08:00
|
|
|
if (Val.isConstantRange() &&
|
|
|
|
Val.getConstantRange().isSingleElement())
|
|
|
|
// Integer constants are single element ranges
|
|
|
|
return true;
|
|
|
|
if (Val.isConstant())
|
|
|
|
// Non integer constants
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine two sets of facts about the same value into a single set of
|
|
|
|
/// facts. Note that this method is not suitable for merging facts along
|
|
|
|
/// different paths in a CFG; that's what the mergeIn function is for. This
|
|
|
|
/// is for merging facts gathered about the same value at the same location
|
|
|
|
/// through two independent means.
|
|
|
|
/// Notes:
|
|
|
|
/// * This method does not promise to return the most precise possible lattice
|
|
|
|
/// value implied by A and B. It is allowed to return any lattice element
|
|
|
|
/// which is at least as strong as *either* A or B (unless our facts
|
2016-07-04 09:26:27 +08:00
|
|
|
/// conflict, see below).
|
2016-02-03 06:03:19 +08:00
|
|
|
/// * Due to unreachable code, the intersection of two lattice values could be
|
|
|
|
/// contradictory. If this happens, we return some valid lattice value so as
|
|
|
|
/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
|
|
|
|
/// we do not make this guarantee. TODO: This would be a useful enhancement.
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement intersect(const ValueLatticeElement &A,
|
|
|
|
const ValueLatticeElement &B) {
|
2016-02-03 06:03:19 +08:00
|
|
|
// Undefined is the strongest state. It means the value is known to be along
|
|
|
|
// an unreachable path.
|
2020-03-15 00:50:09 +08:00
|
|
|
if (A.isUnknown())
|
2016-02-03 06:03:19 +08:00
|
|
|
return A;
|
2020-03-15 00:50:09 +08:00
|
|
|
if (B.isUnknown())
|
2016-02-03 06:03:19 +08:00
|
|
|
return B;
|
|
|
|
|
|
|
|
// If we gave up for one, but got a useable fact from the other, use it.
|
|
|
|
if (A.isOverdefined())
|
|
|
|
return B;
|
|
|
|
if (B.isOverdefined())
|
|
|
|
return A;
|
|
|
|
|
|
|
|
// Can't get any more precise than constants.
|
|
|
|
if (hasSingleValue(A))
|
|
|
|
return A;
|
|
|
|
if (hasSingleValue(B))
|
|
|
|
return B;
|
|
|
|
|
|
|
|
// Could be either constant range or not constant here.
|
|
|
|
if (!A.isConstantRange() || !B.isConstantRange()) {
|
|
|
|
// TODO: Arbitrary choice, could be improved
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect two constant ranges
|
|
|
|
ConstantRange Range =
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
A.getConstantRange().intersectWith(B.getConstantRange());
|
2016-02-03 06:03:19 +08:00
|
|
|
// Note: An empty range is implicitly converted to overdefined internally.
|
|
|
|
// TODO: We could instead use Undefined here since we've proven a conflict
|
2016-07-04 09:26:27 +08:00
|
|
|
// and thus know this path must be unreachable.
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
return ValueLatticeElement::getRange(
|
|
|
|
std::move(Range), /*MayIncludeUndef=*/A.isConstantRangeIncludingUndef() |
|
|
|
|
B.isConstantRangeIncludingUndef());
|
2016-02-03 06:03:19 +08:00
|
|
|
}
|
2016-02-03 05:57:37 +08:00
|
|
|
|
2009-11-11 10:08:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-16 03:59:49 +08:00
|
|
|
// LazyValueInfoCache Decl
|
2009-11-11 10:08:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
namespace {
|
2015-01-10 00:47:20 +08:00
|
|
|
/// A callback value handle updates the cache when values are erased.
|
2011-01-06 05:15:29 +08:00
|
|
|
class LazyValueInfoCache;
|
2015-08-04 06:30:24 +08:00
|
|
|
struct LVIValueHandle final : public CallbackVH {
|
2019-12-21 02:25:57 +08:00
|
|
|
// Needs to access getValPtr(), which is protected.
|
|
|
|
friend struct DenseMapInfo<LVIValueHandle>;
|
|
|
|
|
2011-01-06 05:15:29 +08:00
|
|
|
LazyValueInfoCache *Parent;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
LVIValueHandle(Value *V, LazyValueInfoCache *P)
|
2011-01-06 05:15:29 +08:00
|
|
|
: CallbackVH(V), Parent(P) { }
|
2014-03-05 15:30:04 +08:00
|
|
|
|
|
|
|
void deleted() override;
|
|
|
|
void allUsesReplacedWith(Value *V) override {
|
2011-01-06 05:15:29 +08:00
|
|
|
deleted();
|
|
|
|
}
|
|
|
|
};
|
2016-07-28 06:33:36 +08:00
|
|
|
} // end anonymous namespace
|
2011-01-06 05:15:29 +08:00
|
|
|
|
2015-07-28 23:53:21 +08:00
|
|
|
namespace {
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the cache kept by LazyValueInfo which
|
2009-11-16 03:59:49 +08:00
|
|
|
/// maintains information about queries across the clients' queries.
|
|
|
|
class LazyValueInfoCache {
|
2019-12-21 02:25:57 +08:00
|
|
|
/// This is all of the cached block information for exactly one Value*.
|
|
|
|
/// The entries are sorted by the BasicBlock* of the
|
|
|
|
/// entries, allowing us to do a lookup with a binary search.
|
|
|
|
/// Over-defined lattice values are recorded in OverDefinedCache to reduce
|
|
|
|
/// memory overhead.
|
|
|
|
struct ValueCacheEntryTy {
|
|
|
|
ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
|
|
|
|
LVIValueHandle Handle;
|
|
|
|
SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals;
|
2016-07-28 06:33:36 +08:00
|
|
|
};
|
2009-11-11 08:22:30 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
/// This tracks, on a per-block basis, the set of values that are
|
|
|
|
/// over-defined at the end of that block.
|
|
|
|
typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
|
|
|
|
OverDefinedCacheTy;
|
|
|
|
/// Keep track of all blocks that we have ever seen, so we
|
|
|
|
/// don't spend time removing unused blocks from our caches.
|
|
|
|
DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
|
|
|
|
|
|
|
|
/// This is all of the cached information for all values,
|
|
|
|
/// mapped from Value* to key information.
|
|
|
|
DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
|
|
|
|
OverDefinedCacheTy OverDefinedCache;
|
|
|
|
|
2017-03-23 03:27:12 +08:00
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
public:
|
2017-09-28 19:09:22 +08:00
|
|
|
void insertResult(Value *Val, BasicBlock *BB,
|
|
|
|
const ValueLatticeElement &Result) {
|
2019-12-21 02:25:57 +08:00
|
|
|
SeenBlocks.insert(BB);
|
|
|
|
|
2015-12-11 08:49:47 +08:00
|
|
|
// Insert over-defined values into their own cache to reduce memory
|
|
|
|
// overhead.
|
2014-11-26 01:23:05 +08:00
|
|
|
if (Result.isOverdefined())
|
2019-12-21 02:25:57 +08:00
|
|
|
OverDefinedCache[BB].insert(Val);
|
|
|
|
else {
|
|
|
|
auto It = ValueCache.find_as(Val);
|
|
|
|
if (It == ValueCache.end()) {
|
|
|
|
ValueCache[Val] = std::make_unique<ValueCacheEntryTy>(Val, this);
|
|
|
|
It = ValueCache.find_as(Val);
|
|
|
|
assert(It != ValueCache.end() && "Val was just added to the map!");
|
|
|
|
}
|
|
|
|
It->second->BlockVals[BB] = Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOverdefined(Value *V, BasicBlock *BB) const {
|
|
|
|
auto ODI = OverDefinedCache.find(BB);
|
|
|
|
|
|
|
|
if (ODI == OverDefinedCache.end())
|
|
|
|
return false;
|
2015-12-11 08:49:47 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
return ODI->second.count(V);
|
2015-12-11 08:49:47 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
|
2019-12-21 02:25:57 +08:00
|
|
|
if (isOverdefined(V, BB))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto I = ValueCache.find_as(V);
|
|
|
|
if (I == ValueCache.end())
|
2015-12-11 08:49:47 +08:00
|
|
|
return false;
|
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
return I->second->BlockVals.count(BB);
|
2015-12-11 08:49:47 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const {
|
2019-12-21 02:25:57 +08:00
|
|
|
if (isOverdefined(V, BB))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
2015-12-11 08:49:47 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
auto I = ValueCache.find_as(V);
|
|
|
|
if (I == ValueCache.end())
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement();
|
2019-12-21 02:25:57 +08:00
|
|
|
auto BBI = I->second->BlockVals.find(BB);
|
|
|
|
if (BBI == I->second->BlockVals.end())
|
|
|
|
return ValueLatticeElement();
|
|
|
|
return BBI->second;
|
Reapply [LVI] Normalize pointer behavior
This is a rebase of the change over D70376, which fixes an LVI cache
invalidation issue that also affected this patch.
-----
Related to D69686. As noted there, LVI currently behaves differently
for integer and pointer values: For integers, the block value is always
valid inside the basic block, while for pointers it is only valid at
the end of the basic block. I believe the integer behavior is the
correct one, and CVP relies on it via its getConstantRange() uses.
The reason for the special pointer behavior is that LVI checks whether
a pointer is dereferenced in a given basic block and marks it as
non-null in that case. Of course, this information is valid only after
the dereferencing instruction, or in conservative approximation,
at the end of the block.
This patch changes the treatment of dereferencability: Instead of
including it inside the block value, we instead treat it as something
similar to an assume (it essentially is a non-nullness assume) and
incorporate this information in intersectAssumeOrGuardBlockValueConstantRange()
if the context instruction is the terminator of the basic block.
This happens either when determining an edge-value internally in LVI,
or when a terminator was explicitly passed to getValueAt(). The latter
case makes this change not fully NFC, because we can now fold
terminator icmps based on the dereferencability information in the
same block. This is the reason why I changed one JumpThreading test
(it would optimize the condition away without the change).
Of course, we do not want to recompute dereferencability on each
intersectAssume call, so we need a new cache for this. The
dereferencability analysis requires walking the entire basic block
and computing underlying objects of all memory operands. This was
previously done separately for each queried pointer value. In the
new implementation (both because this makes the caching simpler,
and because it is faster), I instead only walk the full BB once and
cache all the dereferenced pointers. So the traversal is now performed
only once per BB, instead of once per queried pointer value.
I think the overall model now makes more sense than before, and there
will be no more pitfalls due to differing integer/pointer behavior.
Differential Revision: https://reviews.llvm.org/D69914
2019-12-05 03:51:31 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
/// clear - Empty the cache.
|
|
|
|
void clear() {
|
2019-12-21 02:25:57 +08:00
|
|
|
SeenBlocks.clear();
|
|
|
|
ValueCache.clear();
|
|
|
|
OverDefinedCache.clear();
|
2016-09-13 05:46:58 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:03:36 +08:00
|
|
|
/// Inform the cache that a given value has been deleted.
|
|
|
|
void eraseValue(Value *V);
|
|
|
|
|
|
|
|
/// This is part of the update interface to inform the cache
|
|
|
|
/// that a block has been deleted.
|
|
|
|
void eraseBlock(BasicBlock *BB);
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
/// Updates the cache to remove any influence an overdefined value in
|
|
|
|
/// OldSucc might have (unless also overdefined in NewSucc). This just
|
|
|
|
/// flushes elements from the cache and does not add any.
|
|
|
|
void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc);
|
2019-12-21 02:25:57 +08:00
|
|
|
|
|
|
|
friend struct LVIValueHandle;
|
2016-09-13 05:46:58 +08:00
|
|
|
};
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
2016-09-13 05:46:58 +08:00
|
|
|
|
2019-11-13 07:51:51 +08:00
|
|
|
void LazyValueInfoCache::eraseValue(Value *V) {
|
2019-12-21 02:25:57 +08:00
|
|
|
for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) {
|
|
|
|
// Copy and increment the iterator immediately so we can erase behind
|
|
|
|
// ourselves.
|
|
|
|
auto Iter = I++;
|
|
|
|
SmallPtrSetImpl<Value *> &ValueSet = Iter->second;
|
|
|
|
ValueSet.erase(V);
|
|
|
|
if (ValueSet.empty())
|
|
|
|
OverDefinedCache.erase(Iter);
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
ValueCache.erase(V);
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LVIValueHandle::deleted() {
|
|
|
|
// This erasure deallocates *this, so it MUST happen after we're done
|
|
|
|
// using any and all members of *this.
|
|
|
|
Parent->eraseValue(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
|
2019-12-21 02:25:57 +08:00
|
|
|
// Shortcut if we have never seen this block.
|
|
|
|
DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
|
|
|
|
if (I == SeenBlocks.end())
|
|
|
|
return;
|
|
|
|
SeenBlocks.erase(I);
|
|
|
|
|
|
|
|
auto ODI = OverDefinedCache.find(BB);
|
|
|
|
if (ODI != OverDefinedCache.end())
|
|
|
|
OverDefinedCache.erase(ODI);
|
|
|
|
|
|
|
|
for (auto &I : ValueCache)
|
|
|
|
I.second->BlockVals.erase(BB);
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc,
|
|
|
|
BasicBlock *NewSucc) {
|
|
|
|
// When an edge in the graph has been threaded, values that we could not
|
|
|
|
// determine a value for before (i.e. were marked overdefined) may be
|
|
|
|
// possible to solve now. We do NOT try to proactively update these values.
|
|
|
|
// Instead, we clear their entries from the cache, and allow lazy updating to
|
|
|
|
// recompute them when needed.
|
|
|
|
|
|
|
|
// The updating process is fairly simple: we need to drop cached info
|
|
|
|
// for all values that were marked overdefined in OldSucc, and for those same
|
|
|
|
// values in any successor of OldSucc (except NewSucc) in which they were
|
|
|
|
// also marked overdefined.
|
|
|
|
std::vector<BasicBlock*> worklist;
|
|
|
|
worklist.push_back(OldSucc);
|
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
auto I = OverDefinedCache.find(OldSucc);
|
|
|
|
if (I == OverDefinedCache.end())
|
2016-09-13 06:38:44 +08:00
|
|
|
return; // Nothing to process here.
|
2019-12-21 02:25:57 +08:00
|
|
|
SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
|
2016-09-13 06:38:44 +08:00
|
|
|
|
|
|
|
// Use a worklist to perform a depth-first search of OldSucc's successors.
|
|
|
|
// NOTE: We do not need a visited list since any blocks we have already
|
|
|
|
// visited will have had their overdefined markers cleared already, and we
|
|
|
|
// thus won't loop to their successors.
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
BasicBlock *ToUpdate = worklist.back();
|
|
|
|
worklist.pop_back();
|
|
|
|
|
|
|
|
// Skip blocks only accessible through NewSucc.
|
|
|
|
if (ToUpdate == NewSucc) continue;
|
|
|
|
|
2016-12-31 01:56:47 +08:00
|
|
|
// If a value was marked overdefined in OldSucc, and is here too...
|
2019-12-21 02:25:57 +08:00
|
|
|
auto OI = OverDefinedCache.find(ToUpdate);
|
|
|
|
if (OI == OverDefinedCache.end())
|
2016-12-31 01:56:47 +08:00
|
|
|
continue;
|
2019-12-21 02:25:57 +08:00
|
|
|
SmallPtrSetImpl<Value *> &ValueSet = OI->second;
|
2016-12-31 01:56:47 +08:00
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
bool changed = false;
|
|
|
|
for (Value *V : ValsToClear) {
|
2016-12-31 06:09:10 +08:00
|
|
|
if (!ValueSet.erase(V))
|
2016-09-13 06:38:44 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we removed anything, then we potentially need to update
|
|
|
|
// blocks successors too.
|
|
|
|
changed = true;
|
2019-12-21 02:25:57 +08:00
|
|
|
|
|
|
|
if (ValueSet.empty()) {
|
|
|
|
OverDefinedCache.erase(OI);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-13 06:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed) continue;
|
|
|
|
|
|
|
|
worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// An assembly annotator class to print LazyValueCache information in
|
|
|
|
/// comments.
|
|
|
|
class LazyValueInfoImpl;
|
|
|
|
class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter {
|
|
|
|
LazyValueInfoImpl *LVIImpl;
|
|
|
|
// While analyzing which blocks we can solve values for, we need the dominator
|
|
|
|
// information. Since this is an optional parameter in LVI, we require this
|
|
|
|
// DomTreeAnalysis pass in the printer pass, and pass the dominator
|
|
|
|
// tree to the LazyValueInfoAnnotatedWriter.
|
|
|
|
DominatorTree &DT;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree)
|
|
|
|
: LVIImpl(L), DT(DTree) {}
|
|
|
|
|
|
|
|
virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
|
|
|
|
formatted_raw_ostream &OS);
|
|
|
|
|
|
|
|
virtual void emitInstructionAnnot(const Instruction *I,
|
|
|
|
formatted_raw_ostream &OS);
|
|
|
|
};
|
|
|
|
}
|
2016-09-13 06:03:36 +08:00
|
|
|
namespace {
|
2016-09-13 05:46:58 +08:00
|
|
|
// The actual implementation of the lazy analysis and update. Note that the
|
|
|
|
// inheritance from LazyValueInfoCache is intended to be temporary while
|
|
|
|
// splitting the code and then transitioning to a has-a relationship.
|
2016-09-13 06:38:44 +08:00
|
|
|
class LazyValueInfoImpl {
|
|
|
|
|
|
|
|
/// Cached results from previous queries
|
|
|
|
LazyValueInfoCache TheCache;
|
2016-09-13 05:46:58 +08:00
|
|
|
|
|
|
|
/// This stack holds the state of the value solver during a query.
|
|
|
|
/// It basically emulates the callstack of the naive
|
|
|
|
/// recursive value lookup process.
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack;
|
2016-09-13 05:46:58 +08:00
|
|
|
|
|
|
|
/// Keeps track of which block-value pairs are in BlockValueStack.
|
|
|
|
DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet;
|
|
|
|
|
|
|
|
/// Push BV onto BlockValueStack unless it's already in there.
|
|
|
|
/// Returns true on success.
|
|
|
|
bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) {
|
|
|
|
if (!BlockValueSet.insert(BV).second)
|
|
|
|
return false; // It's already in the stack.
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "PUSH: " << *BV.second << " in "
|
|
|
|
<< BV.first->getName() << "\n");
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
BlockValueStack.push_back(BV);
|
2016-09-13 05:46:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
|
2016-09-13 05:46:58 +08:00
|
|
|
const DataLayout &DL; ///< A mandatory DataLayout
|
|
|
|
DominatorTree *DT; ///< An optional DT pointer.
|
2018-02-17 00:35:17 +08:00
|
|
|
DominatorTree *DisabledDT; ///< Stores DT if it's disabled.
|
2016-09-13 05:46:58 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB);
|
2016-09-13 05:46:58 +08:00
|
|
|
bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement &Result, Instruction *CxtI = nullptr);
|
2016-09-13 05:46:58 +08:00
|
|
|
bool hasBlockValue(Value *Val, BasicBlock *BB);
|
|
|
|
|
|
|
|
// These methods process one work item and may add more. A false value
|
|
|
|
// returned means that the work item was not completely processed and must
|
|
|
|
// be revisited after going through the new items.
|
|
|
|
bool solveBlockValue(Value *Val, BasicBlock *BB);
|
2017-09-28 19:09:22 +08:00
|
|
|
bool solveBlockValueImpl(ValueLatticeElement &Res, Value *Val,
|
|
|
|
BasicBlock *BB);
|
|
|
|
bool solveBlockValueNonLocal(ValueLatticeElement &BBLV, Value *Val,
|
|
|
|
BasicBlock *BB);
|
|
|
|
bool solveBlockValuePHINode(ValueLatticeElement &BBLV, PHINode *PN,
|
|
|
|
BasicBlock *BB);
|
|
|
|
bool solveBlockValueSelect(ValueLatticeElement &BBLV, SelectInst *S,
|
2016-09-13 05:46:58 +08:00
|
|
|
BasicBlock *BB);
|
2018-11-21 13:24:12 +08:00
|
|
|
Optional<ConstantRange> getRangeForOperand(unsigned Op, Instruction *I,
|
|
|
|
BasicBlock *BB);
|
2019-05-25 17:53:37 +08:00
|
|
|
bool solveBlockValueBinaryOpImpl(
|
|
|
|
ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
|
|
|
|
std::function<ConstantRange(const ConstantRange &,
|
|
|
|
const ConstantRange &)> OpFn);
|
2017-09-28 19:09:22 +08:00
|
|
|
bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI,
|
2016-09-13 05:46:58 +08:00
|
|
|
BasicBlock *BB);
|
2017-09-28 19:09:22 +08:00
|
|
|
bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI,
|
2016-09-13 05:46:58 +08:00
|
|
|
BasicBlock *BB);
|
2019-05-25 17:53:45 +08:00
|
|
|
bool solveBlockValueOverflowIntrinsic(
|
|
|
|
ValueLatticeElement &BBLV, WithOverflowInst *WO, BasicBlock *BB);
|
2019-10-23 23:05:05 +08:00
|
|
|
bool solveBlockValueSaturatingIntrinsic(ValueLatticeElement &BBLV,
|
|
|
|
SaturatingInst *SI, BasicBlock *BB);
|
2019-05-26 00:44:14 +08:00
|
|
|
bool solveBlockValueIntrinsic(ValueLatticeElement &BBLV, IntrinsicInst *II,
|
|
|
|
BasicBlock *BB);
|
2019-08-31 17:58:50 +08:00
|
|
|
bool solveBlockValueExtractValue(ValueLatticeElement &BBLV,
|
|
|
|
ExtractValueInst *EVI, BasicBlock *BB);
|
2016-09-13 05:46:58 +08:00
|
|
|
void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement &BBLV,
|
2017-06-03 01:28:12 +08:00
|
|
|
Instruction *BBI);
|
2016-09-13 05:46:58 +08:00
|
|
|
|
|
|
|
void solve();
|
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
public:
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the query interface to determine the lattice
|
2009-11-16 03:59:49 +08:00
|
|
|
/// value for the specified Value* at the end of the specified block.
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB,
|
|
|
|
Instruction *CxtI = nullptr);
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the query interface to determine the lattice
|
2014-09-08 04:29:59 +08:00
|
|
|
/// value for the specified Value* at the specified instruction (generally
|
|
|
|
/// from an assume intrinsic).
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getValueAt(Value *V, Instruction *CxtI);
|
2009-11-11 10:08:33 +08:00
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the query interface to determine the lattice
|
2009-11-16 03:59:49 +08:00
|
|
|
/// value for the specified Value* that is true on the specified edge.
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB,
|
|
|
|
BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI = nullptr);
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
/// Complete flush all previously computed values
|
|
|
|
void clear() {
|
|
|
|
TheCache.clear();
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
/// Printing the LazyValueInfo Analysis.
|
|
|
|
void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
|
|
|
|
LazyValueInfoAnnotatedWriter Writer(this, DTree);
|
|
|
|
F.print(OS, &Writer);
|
2017-03-23 03:27:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
/// This is part of the update interface to inform the cache
|
|
|
|
/// that a block has been deleted.
|
|
|
|
void eraseBlock(BasicBlock *BB) {
|
|
|
|
TheCache.eraseBlock(BB);
|
|
|
|
}
|
|
|
|
|
2018-02-17 00:35:17 +08:00
|
|
|
/// Disables use of the DominatorTree within LVI.
|
|
|
|
void disableDT() {
|
|
|
|
if (DT) {
|
|
|
|
assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!");
|
|
|
|
std::swap(DT, DisabledDT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enables use of the DominatorTree within LVI. Does nothing if the class
|
|
|
|
/// instance was initialized without a DT pointer.
|
|
|
|
void enableDT() {
|
|
|
|
if (DisabledDT) {
|
|
|
|
assert(!DT && "Both DT and DisabledDT are not nullptr!");
|
|
|
|
std::swap(DT, DisabledDT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the update interface to inform the cache that an edge from
|
|
|
|
/// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
|
2010-07-27 02:48:03 +08:00
|
|
|
void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
|
|
|
|
DominatorTree *DT = nullptr)
|
2018-02-17 00:35:17 +08:00
|
|
|
: AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {}
|
2009-11-16 03:59:49 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-11-12 06:48:44 +08:00
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
void LazyValueInfoImpl::solve() {
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack(
|
|
|
|
BlockValueStack.begin(), BlockValueStack.end());
|
|
|
|
|
|
|
|
unsigned processedCount = 0;
|
2011-01-06 07:26:22 +08:00
|
|
|
while (!BlockValueStack.empty()) {
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
processedCount++;
|
|
|
|
// Abort if we have to process too many values to get a result for this one.
|
|
|
|
// Because of the design of the overdefined cache currently being per-block
|
|
|
|
// to avoid naming-related issues (IE it wants to try to give different
|
|
|
|
// results for the same name in different blocks), overdefined results don't
|
|
|
|
// get cached globally, which in turn means we will often try to rediscover
|
|
|
|
// the same overdefined result again and again. Once something like
|
|
|
|
// PredicateInfo is used in LVI or CVP, we should be able to make the
|
|
|
|
// overdefined cache global, and remove this throttle.
|
|
|
|
if (processedCount > MaxProcessedPerValue) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Giving up on stack because we are getting too deep\n");
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
// Fill in the original values
|
|
|
|
while (!StartingStack.empty()) {
|
|
|
|
std::pair<BasicBlock *, Value *> &e = StartingStack.back();
|
|
|
|
TheCache.insertResult(e.second, e.first,
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement::getOverdefined());
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
StartingStack.pop_back();
|
|
|
|
}
|
|
|
|
BlockValueSet.clear();
|
|
|
|
BlockValueStack.clear();
|
|
|
|
return;
|
|
|
|
}
|
2017-02-09 17:28:05 +08:00
|
|
|
std::pair<BasicBlock *, Value *> e = BlockValueStack.back();
|
2014-11-26 01:23:05 +08:00
|
|
|
assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!");
|
|
|
|
|
2012-06-28 09:16:18 +08:00
|
|
|
if (solveBlockValue(e.second, e.first)) {
|
2014-11-26 01:23:05 +08:00
|
|
|
// The work item was completely processed.
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
assert(BlockValueStack.back() == e && "Nothing should have been pushed!");
|
2016-09-13 06:38:44 +08:00
|
|
|
assert(TheCache.hasCachedValueInfo(e.second, e.first) &&
|
2015-12-11 08:49:47 +08:00
|
|
|
"Result should be in cache!");
|
2014-11-26 01:23:05 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "POP " << *e.second << " in " << e.first->getName() << " = "
|
|
|
|
<< TheCache.getCachedValueInfo(e.second, e.first) << "\n");
|
2016-02-02 11:15:40 +08:00
|
|
|
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
BlockValueStack.pop_back();
|
2014-11-26 01:23:05 +08:00
|
|
|
BlockValueSet.erase(e);
|
|
|
|
} else {
|
|
|
|
// More work needs to be done before revisiting.
|
LVI: Add a per-value worklist limit to LazyValueInfo.
Summary:
LVI is now depth first, which is optimal for iteration strategy in
terms of work per call. However, the way the results get cached means
it can still go very badly N^2 or worse right now. The overdefined
cache is per-block, because LVI wants to try to get different results
for the same name in different blocks (IE solve the problem
PredicateInfo solves). This means even if we discover a value is
overdefined after going very deep, it doesn't cache this information,
causing it to end up trying to rediscover it again and again. The
same is true for values along the way. In practice, overdefined
anywhere should mean overdefined everywhere (this is how, for example,
SCCP works).
Until we get around to reworking the overdefined cache, we need to
limit the worklist size we process. Note that permanently reverting
the DFS strategy exploration seems the wrong strategy (temporarily
seems fine if we really want). BFS is clearly the wrong approach, it
just gets luckier on some testcases. It's also very hard to design
an effective throttle for BFS. For DFS, the throttle is directly related
to the depth of the CFG. So really deep CFGs will get cutoff, smaller
ones will not. As the CFG simplifies, you get better results.
In BFS, the limit is it's related to the fan-out times average block size,
which is harder to reason about or make good choices for.
Bug being filed about the overdefined cache, but it will require major
surgery to fix it (plumbing predicateinfo through CVP or LVI).
Note: I did not make this number configurable because i'm not sure
anyone really needs to tweak this knob. We run CVP 3 times. On the
testcases i have the slow ones happen in the middle, where CVP is
doing cleanup work other things are effective at. Over the course of
3 runs, we don't see to have any real loss of performance.
I haven't gotten a minimized testcase yet, but just imagine in your
head a testcase where, going *up* the CFG, you have branches, one of
which leads 50000 blocks deep, and the other, to something where the
answer is overdefined immediately. BFS would discover the overdefined
faster than DFS, but do more work to do so. In practice, the right
answer is "once DFS discovers overdefined for a value, stop trying to
get more info about that value" (and so, DFS would normally cache the
overdefined results for every value it passed through in those 50k
blocks, and never do that work again. But it don't, because of the
naming problem)
Reviewers: chandlerc, djasper
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29715
llvm-svn: 294463
2017-02-08 23:22:52 +08:00
|
|
|
assert(BlockValueStack.back() != e && "Stack should have been pushed!");
|
2012-06-28 09:16:18 +08:00
|
|
|
}
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
|
2010-12-18 09:00:40 +08:00
|
|
|
// If already a constant, there is nothing to compute.
|
|
|
|
if (isa<Constant>(Val))
|
|
|
|
return true;
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
return TheCache.hasCachedValueInfo(Val, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val,
|
|
|
|
BasicBlock *BB) {
|
2010-12-18 09:00:40 +08:00
|
|
|
// If already a constant, there is nothing to compute.
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(Val))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::get(VC);
|
2010-12-18 09:00:40 +08:00
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
return TheCache.getCachedValueInfo(Val, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) {
|
[LVI/CVP] Teach LVI about range metadata
Somewhat shockingly for an analysis pass which is computing constant ranges, LVI did not understand the ranges provided by range metadata.
As part of this change, I included a change to CVP primarily because doing so made it much easier to write small self contained test cases. CVP was previously only handling the non-local operand case, but given that LVI can sometimes figure out information about instructions standalone, I don't see any reason to restrict this. There could possibly be a compile time impact from this, but I suspect it should be minimal. If anyone has an example which substaintially regresses, please let me know. I could restrict the block local handling to ICmps feeding Terminator instructions if needed.
Note that this patch continues a somewhat bad practice in LVI. In many cases, we know facts about values, and separate context sensitive facts about values. LVI makes no effort to distinguish and will frequently cache the same value fact repeatedly for different contexts. I would like to change this, but that's a large enough change that I want it to go in separately with clear documentation of what's changing. Other examples of this include the non-null handling, and arguments.
As a meta comment: the entire motivation of this change was being able to write smaller (aka reasonable sized) test cases for a future patch teaching LVI about select instructions.
Differential Revision: http://reviews.llvm.org/D13543
llvm-svn: 251606
2015-10-29 11:57:17 +08:00
|
|
|
switch (BBI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::Load:
|
|
|
|
case Instruction::Call:
|
|
|
|
case Instruction::Invoke:
|
2016-07-25 08:59:46 +08:00
|
|
|
if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range))
|
2015-10-29 12:21:49 +08:00
|
|
|
if (isa<IntegerType>(BBI->getType())) {
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getRange(
|
|
|
|
getConstantRangeFromMetadata(*Ranges));
|
[LVI/CVP] Teach LVI about range metadata
Somewhat shockingly for an analysis pass which is computing constant ranges, LVI did not understand the ranges provided by range metadata.
As part of this change, I included a change to CVP primarily because doing so made it much easier to write small self contained test cases. CVP was previously only handling the non-local operand case, but given that LVI can sometimes figure out information about instructions standalone, I don't see any reason to restrict this. There could possibly be a compile time impact from this, but I suspect it should be minimal. If anyone has an example which substaintially regresses, please let me know. I could restrict the block local handling to ICmps feeding Terminator instructions if needed.
Note that this patch continues a somewhat bad practice in LVI. In many cases, we know facts about values, and separate context sensitive facts about values. LVI makes no effort to distinguish and will frequently cache the same value fact repeatedly for different contexts. I would like to change this, but that's a large enough change that I want it to go in separately with clear documentation of what's changing. Other examples of this include the non-null handling, and arguments.
As a meta comment: the entire motivation of this change was being able to write smaller (aka reasonable sized) test cases for a future patch teaching LVI about select instructions.
Differential Revision: http://reviews.llvm.org/D13543
llvm-svn: 251606
2015-10-29 11:57:17 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
};
|
2016-02-03 05:57:37 +08:00
|
|
|
// Nothing known - will be intersected with other facts
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
[LVI/CVP] Teach LVI about range metadata
Somewhat shockingly for an analysis pass which is computing constant ranges, LVI did not understand the ranges provided by range metadata.
As part of this change, I included a change to CVP primarily because doing so made it much easier to write small self contained test cases. CVP was previously only handling the non-local operand case, but given that LVI can sometimes figure out information about instructions standalone, I don't see any reason to restrict this. There could possibly be a compile time impact from this, but I suspect it should be minimal. If anyone has an example which substaintially regresses, please let me know. I could restrict the block local handling to ICmps feeding Terminator instructions if needed.
Note that this patch continues a somewhat bad practice in LVI. In many cases, we know facts about values, and separate context sensitive facts about values. LVI makes no effort to distinguish and will frequently cache the same value fact repeatedly for different contexts. I would like to change this, but that's a large enough change that I want it to go in separately with clear documentation of what's changing. Other examples of this include the non-null handling, and arguments.
As a meta comment: the entire motivation of this change was being able to write smaller (aka reasonable sized) test cases for a future patch teaching LVI about select instructions.
Differential Revision: http://reviews.llvm.org/D13543
llvm-svn: 251606
2015-10-29 11:57:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
|
2020-03-25 05:39:25 +08:00
|
|
|
assert(!isa<Constant>(Val) && "Value should not be constant");
|
|
|
|
assert(!TheCache.hasCachedValueInfo(Val, BB) &&
|
|
|
|
"Value should not be in cache");
|
2009-11-16 04:00:52 +08:00
|
|
|
|
2014-11-26 01:23:05 +08:00
|
|
|
// Hold off inserting this value into the Cache in case we have to return
|
|
|
|
// false and come back later.
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Res;
|
2016-12-06 11:22:03 +08:00
|
|
|
if (!solveBlockValueImpl(Res, Val, BB))
|
|
|
|
// Work pushed, will revisit
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TheCache.insertResult(Val, BB, Res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
|
2016-12-06 11:22:03 +08:00
|
|
|
Value *Val, BasicBlock *BB) {
|
2019-12-21 02:25:57 +08:00
|
|
|
|
|
|
|
Instruction *BBI = dyn_cast<Instruction>(Val);
|
|
|
|
if (!BBI || BBI->getParent() != BB)
|
|
|
|
return solveBlockValueNonLocal(Res, Val, BB);
|
|
|
|
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(BBI))
|
|
|
|
return solveBlockValuePHINode(Res, PN, BB);
|
|
|
|
|
|
|
|
if (auto *SI = dyn_cast<SelectInst>(BBI))
|
|
|
|
return solveBlockValueSelect(Res, SI, BB);
|
|
|
|
|
2016-04-27 09:02:25 +08:00
|
|
|
// If this value is a nonnull pointer, record it's range and bailout. Note
|
|
|
|
// that for all other pointer typed values, we terminate the search at the
|
|
|
|
// definition. We could easily extend this to look through geps, bitcasts,
|
|
|
|
// and the like to prove non-nullness, but it's not clear that's worth it
|
2017-06-10 05:21:17 +08:00
|
|
|
// compile time wise. The context-insensitive value walk done inside
|
2017-09-10 02:23:11 +08:00
|
|
|
// isKnownNonZero gets most of the profitable cases at much less expense.
|
2019-02-05 16:30:48 +08:00
|
|
|
// This does mean that we have a sensitivity to where the defining
|
2016-04-27 09:02:25 +08:00
|
|
|
// instruction is placed, even if it could legally be hoisted much higher.
|
|
|
|
// That is unfortunate.
|
2019-12-21 02:25:57 +08:00
|
|
|
PointerType *PT = dyn_cast<PointerType>(BBI->getType());
|
|
|
|
if (PT && isKnownNonZero(BBI, DL)) {
|
2017-09-28 19:09:22 +08:00
|
|
|
Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
|
2014-11-26 01:23:05 +08:00
|
|
|
return true;
|
2011-01-15 17:16:12 +08:00
|
|
|
}
|
2016-05-26 06:29:34 +08:00
|
|
|
if (BBI->getType()->isIntegerTy()) {
|
2017-06-03 15:47:08 +08:00
|
|
|
if (auto *CI = dyn_cast<CastInst>(BBI))
|
|
|
|
return solveBlockValueCast(Res, CI, BB);
|
|
|
|
|
2018-11-21 13:24:12 +08:00
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI))
|
2017-06-03 00:33:13 +08:00
|
|
|
return solveBlockValueBinaryOp(Res, BO, BB);
|
2019-05-25 17:53:45 +08:00
|
|
|
|
|
|
|
if (auto *EVI = dyn_cast<ExtractValueInst>(BBI))
|
2019-08-31 17:58:50 +08:00
|
|
|
return solveBlockValueExtractValue(Res, EVI, BB);
|
2019-05-26 00:44:14 +08:00
|
|
|
|
|
|
|
if (auto *II = dyn_cast<IntrinsicInst>(BBI))
|
|
|
|
return solveBlockValueIntrinsic(Res, II, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - unknown inst def found.\n");
|
2016-03-05 06:27:39 +08:00
|
|
|
Res = getFromRangeMetadata(BBI);
|
2014-11-26 01:23:05 +08:00
|
|
|
return true;
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
|
Reapply [LVI] Normalize pointer behavior
This is a rebase of the change over D70376, which fixes an LVI cache
invalidation issue that also affected this patch.
-----
Related to D69686. As noted there, LVI currently behaves differently
for integer and pointer values: For integers, the block value is always
valid inside the basic block, while for pointers it is only valid at
the end of the basic block. I believe the integer behavior is the
correct one, and CVP relies on it via its getConstantRange() uses.
The reason for the special pointer behavior is that LVI checks whether
a pointer is dereferenced in a given basic block and marks it as
non-null in that case. Of course, this information is valid only after
the dereferencing instruction, or in conservative approximation,
at the end of the block.
This patch changes the treatment of dereferencability: Instead of
including it inside the block value, we instead treat it as something
similar to an assume (it essentially is a non-nullness assume) and
incorporate this information in intersectAssumeOrGuardBlockValueConstantRange()
if the context instruction is the terminator of the basic block.
This happens either when determining an edge-value internally in LVI,
or when a terminator was explicitly passed to getValueAt(). The latter
case makes this change not fully NFC, because we can now fold
terminator icmps based on the dereferencability information in the
same block. This is the reason why I changed one JumpThreading test
(it would optimize the condition away without the change).
Of course, we do not want to recompute dereferencability on each
intersectAssume call, so we need a new cache for this. The
dereferencability analysis requires walking the entire basic block
and computing underlying objects of all memory operands. This was
previously done separately for each queried pointer value. In the
new implementation (both because this makes the caching simpler,
and because it is faster), I instead only walk the full BB once and
cache all the dereferenced pointers. So the traversal is now performed
only once per BB, instead of once per queried pointer value.
I think the overall model now makes more sense than before, and there
will be no more pitfalls due to differing integer/pointer behavior.
Differential Revision: https://reviews.llvm.org/D69914
2019-12-05 03:51:31 +08:00
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
|
2019-12-21 02:25:57 +08:00
|
|
|
return L->getPointerAddressSpace() == 0 &&
|
|
|
|
GetUnderlyingObject(L->getPointerOperand(),
|
|
|
|
L->getModule()->getDataLayout()) == Ptr;
|
|
|
|
}
|
|
|
|
if (StoreInst *S = dyn_cast<StoreInst>(I)) {
|
|
|
|
return S->getPointerAddressSpace() == 0 &&
|
|
|
|
GetUnderlyingObject(S->getPointerOperand(),
|
|
|
|
S->getModule()->getDataLayout()) == Ptr;
|
|
|
|
}
|
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
|
|
|
|
if (MI->isVolatile()) return false;
|
2011-01-15 17:16:12 +08:00
|
|
|
|
|
|
|
// FIXME: check whether it has a valuerange that excludes zero?
|
|
|
|
ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
|
2019-12-21 02:25:57 +08:00
|
|
|
if (!Len || Len->isZero()) return false;
|
2011-01-15 17:16:12 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
if (MI->getDestAddressSpace() == 0)
|
|
|
|
if (GetUnderlyingObject(MI->getRawDest(),
|
|
|
|
MI->getModule()->getDataLayout()) == Ptr)
|
|
|
|
return true;
|
2011-01-15 17:16:12 +08:00
|
|
|
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
|
2019-12-21 02:25:57 +08:00
|
|
|
if (MTI->getSourceAddressSpace() == 0)
|
|
|
|
if (GetUnderlyingObject(MTI->getRawSource(),
|
|
|
|
MTI->getModule()->getDataLayout()) == Ptr)
|
|
|
|
return true;
|
2011-01-15 17:16:12 +08:00
|
|
|
}
|
2019-12-21 02:25:57 +08:00
|
|
|
return false;
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
/// Return true if the allocation associated with Val is ever dereferenced
|
|
|
|
/// within the given basic block. This establishes the fact Val is not null,
|
|
|
|
/// but does not imply that the memory at Val is dereferenceable. (Val may
|
|
|
|
/// point off the end of the dereferenceable part of the object.)
|
|
|
|
static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
|
|
|
|
assert(Val->getType()->isPointerTy());
|
2016-04-27 08:30:55 +08:00
|
|
|
|
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
2019-12-21 02:25:57 +08:00
|
|
|
Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
|
|
|
|
// If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
|
|
|
|
// inside InstructionDereferencesPointer either.
|
|
|
|
if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
|
2016-04-27 08:30:55 +08:00
|
|
|
for (Instruction &I : *BB)
|
2019-12-21 02:25:57 +08:00
|
|
|
if (InstructionDereferencesPointer(&I, UnderlyingVal))
|
|
|
|
return true;
|
|
|
|
return false;
|
2016-04-27 08:30:55 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
|
2019-12-21 02:25:57 +08:00
|
|
|
Value *Val, BasicBlock *BB) {
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result; // Start Undefined.
|
2010-12-18 09:00:40 +08:00
|
|
|
|
|
|
|
// If this is the entry block, we must be asking about an argument. The
|
|
|
|
// value is overdefined.
|
|
|
|
if (BB == &BB->getParent()->getEntryBlock()) {
|
|
|
|
assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
|
2019-12-21 02:25:57 +08:00
|
|
|
// Before giving up, see if we can prove the pointer non-null local to
|
|
|
|
// this particular block.
|
|
|
|
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
|
|
|
|
if (PTy &&
|
|
|
|
(isKnownNonZero(Val, DL) ||
|
|
|
|
(isObjectDereferencedInBlock(Val, BB) &&
|
|
|
|
!NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
|
|
|
|
Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
|
|
|
|
} else {
|
|
|
|
Result = ValueLatticeElement::getOverdefined();
|
|
|
|
}
|
|
|
|
BBLV = Result;
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all of our predecessors, merging what we know from them into
|
2017-02-07 08:25:24 +08:00
|
|
|
// result. If we encounter an unexplored predecessor, we eagerly explore it
|
|
|
|
// in a depth first manner. In practice, this has the effect of discovering
|
|
|
|
// paths we can't analyze eagerly without spending compile times analyzing
|
|
|
|
// other paths. This heuristic benefits from the fact that predecessors are
|
|
|
|
// frequently arranged such that dominating ones come first and we quickly
|
|
|
|
// find a path to function entry. TODO: We should consider explicitly
|
|
|
|
// canonicalizing to make this true rather than relying on this happy
|
2018-07-31 03:41:25 +08:00
|
|
|
// accident.
|
2014-07-22 01:06:51 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement EdgeResult;
|
2017-02-07 08:25:24 +08:00
|
|
|
if (!getEdgeValue(Val, *PI, BB, EdgeResult))
|
|
|
|
// Explore that input, then return here
|
|
|
|
return false;
|
2010-12-18 09:00:40 +08:00
|
|
|
|
2020-04-14 19:32:43 +08:00
|
|
|
Result.mergeIn(EdgeResult);
|
2010-12-18 09:00:40 +08:00
|
|
|
|
|
|
|
// If we hit overdefined, exit early. The BlockVals entry is already set
|
|
|
|
// to overdefined.
|
|
|
|
if (Result.isOverdefined()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined because of pred (non local).\n");
|
2019-12-21 02:25:57 +08:00
|
|
|
// Before giving up, see if we can prove the pointer non-null local to
|
|
|
|
// this particular block.
|
|
|
|
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
|
|
|
|
if (PTy && isObjectDereferencedInBlock(Val, BB) &&
|
|
|
|
!NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
|
|
|
|
Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
|
|
|
|
}
|
|
|
|
|
2010-12-21 02:18:16 +08:00
|
|
|
BBLV = Result;
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the merged value, which is more precise than 'overdefined'.
|
|
|
|
assert(!Result.isOverdefined());
|
2010-12-21 02:18:16 +08:00
|
|
|
BBLV = Result;
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV,
|
|
|
|
PHINode *PN, BasicBlock *BB) {
|
|
|
|
ValueLatticeElement Result; // Start Undefined.
|
2010-12-18 09:00:40 +08:00
|
|
|
|
|
|
|
// Loop over all of our predecessors, merging what we know from them into
|
2017-02-07 08:25:24 +08:00
|
|
|
// result. See the comment about the chosen traversal order in
|
|
|
|
// solveBlockValueNonLocal; the same reasoning applies here.
|
2010-12-18 09:00:40 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
BasicBlock *PhiBB = PN->getIncomingBlock(i);
|
|
|
|
Value *PhiVal = PN->getIncomingValue(i);
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement EdgeResult;
|
2014-10-16 08:40:05 +08:00
|
|
|
// Note that we can provide PN as the context value to getEdgeValue, even
|
|
|
|
// though the results will be cached, because PN is the value being used as
|
|
|
|
// the cache key in the caller.
|
2017-02-07 08:25:24 +08:00
|
|
|
if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN))
|
|
|
|
// Explore that input, then return here
|
|
|
|
return false;
|
2010-12-18 09:00:40 +08:00
|
|
|
|
2020-04-14 19:32:43 +08:00
|
|
|
Result.mergeIn(EdgeResult);
|
2010-12-18 09:00:40 +08:00
|
|
|
|
|
|
|
// If we hit overdefined, exit early. The BlockVals entry is already set
|
|
|
|
// to overdefined.
|
|
|
|
if (Result.isOverdefined()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined because of pred (local).\n");
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2010-12-21 02:18:16 +08:00
|
|
|
BBLV = Result;
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-08-19 05:11:37 +08:00
|
|
|
|
2010-12-18 09:00:40 +08:00
|
|
|
// Return the merged value, which is more precise than 'overdefined'.
|
|
|
|
assert(!Result.isOverdefined() && "Possible PHI in entry block?");
|
2010-12-21 02:18:16 +08:00
|
|
|
BBLV = Result;
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
|
|
|
|
bool isTrueDest = true);
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2016-02-03 05:57:37 +08:00
|
|
|
// If we can determine a constraint on the value given conditions assumed by
|
|
|
|
// the program, intersect those constraints with BBLV
|
2016-09-13 05:46:58 +08:00
|
|
|
void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
|
2017-09-28 19:09:22 +08:00
|
|
|
Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) {
|
2014-09-08 04:29:59 +08:00
|
|
|
BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
|
|
|
|
if (!BBI)
|
|
|
|
return;
|
|
|
|
|
2017-01-11 21:24:24 +08:00
|
|
|
for (auto &AssumeVH : AC->assumptionsFor(Val)) {
|
2016-12-19 16:22:17 +08:00
|
|
|
if (!AssumeVH)
|
2015-01-04 20:03:27 +08:00
|
|
|
continue;
|
2016-12-19 16:22:17 +08:00
|
|
|
auto *I = cast<CallInst>(AssumeVH);
|
|
|
|
if (!isValidAssumeForContext(I, BBI, DT))
|
2014-09-08 04:29:59 +08:00
|
|
|
continue;
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0)));
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
2016-08-12 23:52:23 +08:00
|
|
|
|
|
|
|
// If guards are not used in the module, don't spend time looking for them
|
|
|
|
auto *GuardDecl = BBI->getModule()->getFunction(
|
|
|
|
Intrinsic::getName(Intrinsic::experimental_guard));
|
2019-12-21 02:25:57 +08:00
|
|
|
if (!GuardDecl || GuardDecl->use_empty())
|
|
|
|
return;
|
2016-08-12 23:52:23 +08:00
|
|
|
|
2019-12-21 02:25:57 +08:00
|
|
|
if (BBI->getIterator() == BBI->getParent()->begin())
|
|
|
|
return;
|
|
|
|
for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
|
|
|
|
BBI->getParent()->rend())) {
|
|
|
|
Value *Cond = nullptr;
|
|
|
|
if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
|
|
|
|
BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
|
2016-08-12 23:52:23 +08:00
|
|
|
}
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV,
|
|
|
|
SelectInst *SI, BasicBlock *BB) {
|
2016-02-02 06:57:53 +08:00
|
|
|
|
|
|
|
// Recurse on our inputs if needed
|
|
|
|
if (!hasBlockValue(SI->getTrueValue(), BB)) {
|
|
|
|
if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
|
|
|
|
return false;
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB);
|
2016-02-02 06:57:53 +08:00
|
|
|
// If we hit overdefined, don't ask more queries. We want to avoid poisoning
|
|
|
|
// extra slots in the table if we can.
|
|
|
|
if (TrueVal.isOverdefined()) {
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasBlockValue(SI->getFalseValue(), BB)) {
|
|
|
|
if (pushBlockValue(std::make_pair(BB, SI->getFalseValue())))
|
|
|
|
return false;
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB);
|
2016-02-02 06:57:53 +08:00
|
|
|
// If we hit overdefined, don't ask more queries. We want to avoid poisoning
|
|
|
|
// extra slots in the table if we can.
|
|
|
|
if (FalseVal.isOverdefined()) {
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-27 06:53:59 +08:00
|
|
|
if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
|
2017-05-06 11:35:15 +08:00
|
|
|
const ConstantRange &TrueCR = TrueVal.getConstantRange();
|
|
|
|
const ConstantRange &FalseCR = FalseVal.getConstantRange();
|
2016-02-27 06:53:59 +08:00
|
|
|
Value *LHS = nullptr;
|
|
|
|
Value *RHS = nullptr;
|
|
|
|
SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS);
|
|
|
|
// Is this a min specifically of our two inputs? (Avoid the risk of
|
|
|
|
// ValueTracking getting smarter looking back past our immediate inputs.)
|
|
|
|
if (SelectPatternResult::isMinOrMax(SPR.Flavor) &&
|
|
|
|
LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) {
|
2016-12-06 10:54:16 +08:00
|
|
|
ConstantRange ResultCR = [&]() {
|
|
|
|
switch (SPR.Flavor) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected minmax type!");
|
|
|
|
case SPF_SMIN: /// Signed minimum
|
|
|
|
return TrueCR.smin(FalseCR);
|
|
|
|
case SPF_UMIN: /// Unsigned minimum
|
|
|
|
return TrueCR.umin(FalseCR);
|
|
|
|
case SPF_SMAX: /// Signed maximum
|
|
|
|
return TrueCR.smax(FalseCR);
|
|
|
|
case SPF_UMAX: /// Unsigned maximum
|
|
|
|
return TrueCR.umax(FalseCR);
|
|
|
|
};
|
|
|
|
}();
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(
|
|
|
|
ResultCR, TrueVal.isConstantRangeIncludingUndef() |
|
|
|
|
FalseVal.isConstantRangeIncludingUndef());
|
2016-12-06 10:54:16 +08:00
|
|
|
return true;
|
2016-02-27 06:53:59 +08:00
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2019-05-15 02:53:47 +08:00
|
|
|
if (SPR.Flavor == SPF_ABS) {
|
|
|
|
if (LHS == SI->getTrueValue()) {
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(
|
|
|
|
TrueCR.abs(), TrueVal.isConstantRangeIncludingUndef());
|
2019-05-15 02:53:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (LHS == SI->getFalseValue()) {
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(
|
|
|
|
FalseCR.abs(), FalseVal.isConstantRangeIncludingUndef());
|
2019-05-15 02:53:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SPR.Flavor == SPF_NABS) {
|
|
|
|
ConstantRange Zero(APInt::getNullValue(TrueCR.getBitWidth()));
|
|
|
|
if (LHS == SI->getTrueValue()) {
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(
|
|
|
|
Zero.sub(TrueCR.abs()), FalseVal.isConstantRangeIncludingUndef());
|
2019-05-15 02:53:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (LHS == SI->getFalseValue()) {
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(
|
|
|
|
Zero.sub(FalseCR.abs()), FalseVal.isConstantRangeIncludingUndef());
|
2019-05-15 02:53:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 06:53:59 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 08:09:18 +08:00
|
|
|
// Can we constrain the facts about the true and false values by using the
|
|
|
|
// condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
|
|
|
|
// TODO: We could potentially refine an overdefined true value above.
|
2016-08-03 00:20:48 +08:00
|
|
|
Value *Cond = SI->getCondition();
|
2016-08-10 21:38:07 +08:00
|
|
|
TrueVal = intersect(TrueVal,
|
|
|
|
getValueFromCondition(SI->getTrueValue(), Cond, true));
|
|
|
|
FalseVal = intersect(FalseVal,
|
|
|
|
getValueFromCondition(SI->getFalseValue(), Cond, false));
|
2016-08-03 00:20:48 +08:00
|
|
|
|
|
|
|
// Handle clamp idioms such as:
|
|
|
|
// %24 = constantrange<0, 17>
|
|
|
|
// %39 = icmp eq i32 %24, 0
|
|
|
|
// %40 = add i32 %24, -1
|
|
|
|
// %siv.next = select i1 %39, i32 16, i32 %40
|
|
|
|
// %siv.next = constantrange<0, 17> not <-1, 17>
|
|
|
|
// In general, this can handle any clamp idiom which tests the edge
|
|
|
|
// condition via an equality or inequality.
|
|
|
|
if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
|
2016-02-27 06:53:59 +08:00
|
|
|
ICmpInst::Predicate Pred = ICI->getPredicate();
|
|
|
|
Value *A = ICI->getOperand(0);
|
|
|
|
if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
|
|
|
|
auto addConstants = [](ConstantInt *A, ConstantInt *B) {
|
|
|
|
assert(A->getType() == B->getType());
|
|
|
|
return ConstantInt::get(A->getType(), A->getValue() + B->getValue());
|
|
|
|
};
|
|
|
|
// See if either input is A + C2, subject to the constraint from the
|
|
|
|
// condition that A != C when that input is used. We can assume that
|
|
|
|
// that input doesn't include C + C2.
|
|
|
|
ConstantInt *CIAdded;
|
|
|
|
switch (Pred) {
|
2016-02-27 13:18:30 +08:00
|
|
|
default: break;
|
2016-02-27 06:53:59 +08:00
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
if (match(SI->getFalseValue(), m_Add(m_Specific(A),
|
|
|
|
m_ConstantInt(CIAdded)))) {
|
|
|
|
auto ResNot = addConstants(CIBase, CIAdded);
|
|
|
|
FalseVal = intersect(FalseVal,
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement::getNot(ResNot));
|
2016-02-27 06:53:59 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
if (match(SI->getTrueValue(), m_Add(m_Specific(A),
|
|
|
|
m_ConstantInt(CIAdded)))) {
|
|
|
|
auto ResNot = addConstants(CIBase, CIAdded);
|
|
|
|
TrueVal = intersect(TrueVal,
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement::getNot(ResNot));
|
2016-02-27 06:53:59 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result; // Start Undefined.
|
2020-04-14 19:32:43 +08:00
|
|
|
Result.mergeIn(TrueVal);
|
|
|
|
Result.mergeIn(FalseVal);
|
2016-02-02 06:57:53 +08:00
|
|
|
BBLV = Result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:24:12 +08:00
|
|
|
Optional<ConstantRange> LazyValueInfoImpl::getRangeForOperand(unsigned Op,
|
|
|
|
Instruction *I,
|
|
|
|
BasicBlock *BB) {
|
|
|
|
if (!hasBlockValue(I->getOperand(Op), BB))
|
|
|
|
if (pushBlockValue(std::make_pair(BB, I->getOperand(Op))))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
const unsigned OperandBitWidth =
|
|
|
|
DL.getTypeSizeInBits(I->getOperand(Op)->getType());
|
2019-03-24 17:34:40 +08:00
|
|
|
ConstantRange Range = ConstantRange::getFull(OperandBitWidth);
|
2018-11-21 13:24:12 +08:00
|
|
|
if (hasBlockValue(I->getOperand(Op), BB)) {
|
|
|
|
ValueLatticeElement Val = getBlockValue(I->getOperand(Op), BB);
|
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(I->getOperand(Op), Val, I);
|
|
|
|
if (Val.isConstantRange())
|
|
|
|
Range = Val.getConstantRange();
|
|
|
|
}
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV,
|
2017-06-03 15:47:08 +08:00
|
|
|
CastInst *CI,
|
|
|
|
BasicBlock *BB) {
|
|
|
|
if (!CI->getOperand(0)->getType()->isSized()) {
|
2016-04-27 06:52:30 +08:00
|
|
|
// Without knowing how wide the input is, we can't analyze it in any useful
|
|
|
|
// way.
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-04-27 06:52:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-27 07:27:33 +08:00
|
|
|
|
|
|
|
// Filter out casts we don't know how to reason about before attempting to
|
|
|
|
// recurse on our operand. This can cut a long search short if we know we're
|
|
|
|
// not going to be able to get any useful information anways.
|
2017-06-03 15:47:08 +08:00
|
|
|
switch (CI->getOpcode()) {
|
2016-04-27 07:27:33 +08:00
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::BitCast:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled instructions are overdefined.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown cast).\n");
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-04-27 07:27:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-27 05:48:16 +08:00
|
|
|
// Figure out the range of the LHS. If that fails, we still apply the
|
|
|
|
// transfer rule on the full set since we may be able to locally infer
|
|
|
|
// interesting facts.
|
2018-11-21 13:24:12 +08:00
|
|
|
Optional<ConstantRange> LHSRes = getRangeForOperand(0, CI, BB);
|
|
|
|
if (!LHSRes.hasValue())
|
|
|
|
// More work to do before applying this transfer rule.
|
|
|
|
return false;
|
|
|
|
ConstantRange LHSRange = LHSRes.getValue();
|
2016-04-26 02:30:31 +08:00
|
|
|
|
2017-06-03 15:47:14 +08:00
|
|
|
const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth();
|
2016-04-26 02:30:31 +08:00
|
|
|
|
|
|
|
// NOTE: We're currently limited by the set of operations that ConstantRange
|
|
|
|
// can evaluate symbolically. Enhancing that set will allows us to analyze
|
|
|
|
// more definitions.
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(),
|
|
|
|
ResultBitWidth));
|
2016-04-26 02:30:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-25 17:53:37 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueBinaryOpImpl(
|
|
|
|
ValueLatticeElement &BBLV, Instruction *I, BasicBlock *BB,
|
|
|
|
std::function<ConstantRange(const ConstantRange &,
|
|
|
|
const ConstantRange &)> OpFn) {
|
|
|
|
// Figure out the ranges of the operands. If that fails, use a
|
|
|
|
// conservative range, but apply the transfer rule anyways. This
|
|
|
|
// lets us pick up facts from expressions like "and i32 (call i32
|
|
|
|
// @foo()), 32"
|
|
|
|
Optional<ConstantRange> LHSRes = getRangeForOperand(0, I, BB);
|
|
|
|
Optional<ConstantRange> RHSRes = getRangeForOperand(1, I, BB);
|
|
|
|
if (!LHSRes.hasValue() || !RHSRes.hasValue())
|
|
|
|
// More work to do before applying this transfer rule.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstantRange LHSRange = LHSRes.getValue();
|
|
|
|
ConstantRange RHSRange = RHSRes.getValue();
|
|
|
|
BBLV = ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
|
|
|
|
BinaryOperator *BO,
|
|
|
|
BasicBlock *BB) {
|
2016-04-27 07:10:35 +08:00
|
|
|
|
2017-06-03 00:33:13 +08:00
|
|
|
assert(BO->getOperand(0)->getType()->isSized() &&
|
2016-04-27 07:10:35 +08:00
|
|
|
"all operands to binary operators are sized");
|
2019-06-05 00:24:09 +08:00
|
|
|
if (BO->getOpcode() == Instruction::Xor) {
|
|
|
|
// Xor is the only operation not supported by ConstantRange::binaryOp().
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown binary operator).\n");
|
2017-09-28 19:09:22 +08:00
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
2016-04-27 07:27:33 +08:00
|
|
|
return true;
|
2019-06-05 00:24:09 +08:00
|
|
|
}
|
|
|
|
|
2019-10-23 21:56:04 +08:00
|
|
|
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
|
|
|
|
unsigned NoWrapKind = 0;
|
|
|
|
if (OBO->hasNoUnsignedWrap())
|
|
|
|
NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
|
|
|
|
if (OBO->hasNoSignedWrap())
|
|
|
|
NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
|
|
|
|
|
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, BO, BB,
|
|
|
|
[BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) {
|
|
|
|
return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) {
|
2019-06-05 00:24:09 +08:00
|
|
|
return CR1.binaryOp(BO->getOpcode(), CR2);
|
|
|
|
});
|
2009-11-12 06:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 17:53:45 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueOverflowIntrinsic(
|
|
|
|
ValueLatticeElement &BBLV, WithOverflowInst *WO, BasicBlock *BB) {
|
|
|
|
return solveBlockValueBinaryOpImpl(BBLV, WO, BB,
|
|
|
|
[WO](const ConstantRange &CR1, const ConstantRange &CR2) {
|
|
|
|
return CR1.binaryOp(WO->getBinaryOp(), CR2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:05:05 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueSaturatingIntrinsic(
|
|
|
|
ValueLatticeElement &BBLV, SaturatingInst *SI, BasicBlock *BB) {
|
|
|
|
switch (SI->getIntrinsicID()) {
|
2019-05-26 00:44:14 +08:00
|
|
|
case Intrinsic::uadd_sat:
|
2019-10-23 23:05:05 +08:00
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
|
2019-05-26 00:44:14 +08:00
|
|
|
return CR1.uadd_sat(CR2);
|
|
|
|
});
|
|
|
|
case Intrinsic::usub_sat:
|
2019-10-23 23:05:05 +08:00
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
|
2019-05-26 00:44:14 +08:00
|
|
|
return CR1.usub_sat(CR2);
|
|
|
|
});
|
|
|
|
case Intrinsic::sadd_sat:
|
2019-10-23 23:05:05 +08:00
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
|
2019-05-26 00:44:14 +08:00
|
|
|
return CR1.sadd_sat(CR2);
|
|
|
|
});
|
|
|
|
case Intrinsic::ssub_sat:
|
2019-10-23 23:05:05 +08:00
|
|
|
return solveBlockValueBinaryOpImpl(
|
|
|
|
BBLV, SI, BB, [](const ConstantRange &CR1, const ConstantRange &CR2) {
|
2019-05-26 00:44:14 +08:00
|
|
|
return CR1.ssub_sat(CR2);
|
|
|
|
});
|
2019-10-23 23:05:05 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("All llvm.sat intrinsic are handled.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LazyValueInfoImpl::solveBlockValueIntrinsic(ValueLatticeElement &BBLV,
|
|
|
|
IntrinsicInst *II,
|
|
|
|
BasicBlock *BB) {
|
|
|
|
if (auto *SI = dyn_cast<SaturatingInst>(II))
|
|
|
|
return solveBlockValueSaturatingIntrinsic(BBLV, SI, BB);
|
|
|
|
|
2019-10-25 04:39:56 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown intrinsic).\n");
|
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
|
|
|
return true;
|
2019-05-26 00:44:14 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 17:58:50 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueExtractValue(
|
|
|
|
ValueLatticeElement &BBLV, ExtractValueInst *EVI, BasicBlock *BB) {
|
|
|
|
if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
|
|
|
|
if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 0)
|
|
|
|
return solveBlockValueOverflowIntrinsic(BBLV, WO, BB);
|
|
|
|
|
2019-09-07 20:03:59 +08:00
|
|
|
// Handle extractvalue of insertvalue to allow further simplification
|
|
|
|
// based on replaced with.overflow intrinsics.
|
|
|
|
if (Value *V = SimplifyExtractValueInst(
|
|
|
|
EVI->getAggregateOperand(), EVI->getIndices(),
|
|
|
|
EVI->getModule()->getDataLayout())) {
|
|
|
|
if (!hasBlockValue(V, BB)) {
|
|
|
|
if (pushBlockValue({ BB, V }))
|
|
|
|
return false;
|
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
BBLV = getBlockValue(V, BB);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:58:50 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown extractvalue).\n");
|
|
|
|
BBLV = ValueLatticeElement::getOverdefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
|
|
|
|
bool isTrueDest) {
|
2016-08-08 22:08:37 +08:00
|
|
|
Value *LHS = ICI->getOperand(0);
|
|
|
|
Value *RHS = ICI->getOperand(1);
|
|
|
|
CmpInst::Predicate Predicate = ICI->getPredicate();
|
|
|
|
|
|
|
|
if (isa<Constant>(RHS)) {
|
|
|
|
if (ICI->isEquality() && LHS == Val) {
|
2014-09-08 04:29:59 +08:00
|
|
|
// We know that V has the RHS constant if this is a true SETEQ or
|
2016-07-04 09:26:27 +08:00
|
|
|
// false SETNE.
|
2016-08-08 22:08:37 +08:00
|
|
|
if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::get(cast<Constant>(RHS));
|
2020-03-15 00:50:09 +08:00
|
|
|
else if (!isa<UndefValue>(RHS))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getNot(cast<Constant>(RHS));
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
2016-08-09 22:50:08 +08:00
|
|
|
}
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2016-08-09 22:50:08 +08:00
|
|
|
if (!Val->getType()->isIntegerTy())
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
2016-08-08 22:33:11 +08:00
|
|
|
|
2016-08-09 22:50:08 +08:00
|
|
|
// Use ConstantRange::makeAllowedICmpRegion in order to determine the possible
|
|
|
|
// range of Val guaranteed by the condition. Recognize comparisons in the from
|
|
|
|
// of:
|
|
|
|
// icmp <pred> Val, ...
|
2016-08-12 18:05:11 +08:00
|
|
|
// icmp <pred> (add Val, Offset), ...
|
2016-08-09 22:50:08 +08:00
|
|
|
// The latter is the range checking idiom that InstCombine produces. Subtract
|
|
|
|
// the offset from the allowed range for RHS in this case.
|
|
|
|
|
|
|
|
// Val or (add Val, Offset) can be on either hand of the comparison
|
|
|
|
if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) {
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
Predicate = CmpInst::getSwappedPredicate(Predicate);
|
|
|
|
}
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2016-08-09 22:50:08 +08:00
|
|
|
ConstantInt *Offset = nullptr;
|
2016-08-12 18:05:11 +08:00
|
|
|
if (LHS != Val)
|
2016-08-09 22:50:08 +08:00
|
|
|
match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset)));
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2016-08-09 22:50:08 +08:00
|
|
|
if (LHS == Val || Offset) {
|
|
|
|
// Calculate the range of values that are allowed by the comparison
|
|
|
|
ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(),
|
|
|
|
/*isFullSet=*/true);
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
|
|
|
|
RHSRange = ConstantRange(CI->getValue());
|
2016-08-12 18:14:11 +08:00
|
|
|
else if (Instruction *I = dyn_cast<Instruction>(RHS))
|
|
|
|
if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
|
|
|
|
RHSRange = getConstantRangeFromMetadata(*Ranges);
|
2016-08-09 17:41:34 +08:00
|
|
|
|
2016-08-09 22:50:08 +08:00
|
|
|
// If we're interested in the false dest, invert the condition
|
|
|
|
CmpInst::Predicate Pred =
|
|
|
|
isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate);
|
|
|
|
ConstantRange TrueValues =
|
|
|
|
ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
|
|
|
|
|
|
|
|
if (Offset) // Apply the offset from above.
|
|
|
|
TrueValues = TrueValues.subtract(Offset->getValue());
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getRange(std::move(TrueValues));
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 00:57:42 +08:00
|
|
|
// Handle conditions of the form
|
|
|
|
// extractvalue(op.with.overflow(%x, C), 1).
|
|
|
|
static ValueLatticeElement getValueFromOverflowCondition(
|
|
|
|
Value *Val, WithOverflowInst *WO, bool IsTrueDest) {
|
|
|
|
// TODO: This only works with a constant RHS for now. We could also compute
|
|
|
|
// the range of the RHS, but this doesn't fit into the current structure of
|
|
|
|
// the edge value calculation.
|
|
|
|
const APInt *C;
|
|
|
|
if (WO->getLHS() != Val || !match(WO->getRHS(), m_APInt(C)))
|
|
|
|
return ValueLatticeElement::getOverdefined();
|
|
|
|
|
|
|
|
// Calculate the possible values of %x for which no overflow occurs.
|
2019-04-28 23:40:56 +08:00
|
|
|
ConstantRange NWR = ConstantRange::makeExactNoWrapRegion(
|
|
|
|
WO->getBinaryOp(), *C, WO->getNoWrapKind());
|
2019-04-18 00:57:42 +08:00
|
|
|
|
|
|
|
// If overflow is false, %x is constrained to NWR. If overflow is true, %x is
|
|
|
|
// constrained to it's inverse (all values that might cause overflow).
|
|
|
|
if (IsTrueDest)
|
|
|
|
NWR = NWR.inverse();
|
|
|
|
return ValueLatticeElement::getRange(NWR);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement
|
2016-08-10 23:13:15 +08:00
|
|
|
getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
|
2020-03-21 21:54:46 +08:00
|
|
|
SmallDenseMap<Value*, ValueLatticeElement> &Visited);
|
2016-08-10 23:13:15 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement
|
2016-08-10 23:13:15 +08:00
|
|
|
getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
|
2020-03-21 21:54:46 +08:00
|
|
|
SmallDenseMap<Value*, ValueLatticeElement> &Visited) {
|
2016-08-10 23:13:15 +08:00
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
|
|
|
|
return getValueFromICmpCondition(Val, ICI, isTrueDest);
|
|
|
|
|
2019-04-18 00:57:42 +08:00
|
|
|
if (auto *EVI = dyn_cast<ExtractValueInst>(Cond))
|
|
|
|
if (auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()))
|
|
|
|
if (EVI->getNumIndices() == 1 && *EVI->idx_begin() == 1)
|
|
|
|
return getValueFromOverflowCondition(Val, WO, isTrueDest);
|
|
|
|
|
2016-08-10 23:13:15 +08:00
|
|
|
// Handle conditions in the form of (cond1 && cond2), we know that on the
|
2017-06-23 09:08:16 +08:00
|
|
|
// true dest path both of the conditions hold. Similarly for conditions of
|
|
|
|
// the form (cond1 || cond2), we know that on the false dest path neither
|
|
|
|
// condition holds.
|
2016-08-10 23:13:15 +08:00
|
|
|
BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
|
2017-06-23 09:08:16 +08:00
|
|
|
if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) ||
|
|
|
|
(!isTrueDest && BO->getOpcode() != BinaryOperator::Or))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
2016-08-10 23:13:15 +08:00
|
|
|
|
[LazyValueInfo] PR33357 prevent infinite recursion on BinaryOperator
Summary:
It is possible for LVI to encounter instructions that are not in valid
SSA form and reference themselves. One example is the following:
%tmp4 = and i1 %tmp4, undef
Before this patch LVI would recurse until running out of stack memory
and crashed. This patch marks these self-referential instructions as
Overdefined and aborts analysis on the instruction.
Fixes https://bugs.llvm.org/show_bug.cgi?id=33357
Reviewers: craig.topper, anna, efriedma, dberlin, sebpop, kuhar
Reviewed by: dberlin
Subscribers: uabelho, spatel, a.elovikov, fhahn, eli.friedman, mzolotukhin, spop, evandro, davide, llvm-commits
Differential Revision: https://reviews.llvm.org/D34135
llvm-svn: 327432
2018-03-14 02:14:10 +08:00
|
|
|
// Prevent infinite recursion if Cond references itself as in this example:
|
|
|
|
// Cond: "%tmp4 = and i1 %tmp4, undef"
|
|
|
|
// BL: "%tmp4 = and i1 %tmp4, undef"
|
|
|
|
// BR: "i1 undef"
|
|
|
|
Value *BL = BO->getOperand(0);
|
|
|
|
Value *BR = BO->getOperand(1);
|
|
|
|
if (BL == Cond || BR == Cond)
|
|
|
|
return ValueLatticeElement::getOverdefined();
|
|
|
|
|
|
|
|
return intersect(getValueFromCondition(Val, BL, isTrueDest, Visited),
|
|
|
|
getValueFromCondition(Val, BR, isTrueDest, Visited));
|
2016-08-10 23:13:15 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement
|
2016-08-10 23:13:15 +08:00
|
|
|
getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
|
2020-03-21 21:54:46 +08:00
|
|
|
SmallDenseMap<Value*, ValueLatticeElement> &Visited) {
|
2016-08-10 23:13:15 +08:00
|
|
|
auto I = Visited.find(Cond);
|
|
|
|
if (I != Visited.end())
|
|
|
|
return I->second;
|
2016-08-12 23:08:15 +08:00
|
|
|
|
|
|
|
auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited);
|
|
|
|
Visited[Cond] = Result;
|
|
|
|
return Result;
|
2016-08-10 23:13:15 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond,
|
|
|
|
bool isTrueDest) {
|
2016-08-10 23:13:15 +08:00
|
|
|
assert(Cond && "precondition");
|
2020-03-21 21:54:46 +08:00
|
|
|
SmallDenseMap<Value*, ValueLatticeElement> Visited;
|
2016-08-10 23:13:15 +08:00
|
|
|
return getValueFromCondition(Val, Cond, isTrueDest, Visited);
|
|
|
|
}
|
|
|
|
|
2017-08-04 05:11:30 +08:00
|
|
|
// Return true if Usr has Op as an operand, otherwise false.
|
|
|
|
static bool usesOperand(User *Usr, Value *Op) {
|
|
|
|
return find(Usr->operands(), Op) != Usr->op_end();
|
|
|
|
}
|
|
|
|
|
2017-08-10 10:23:14 +08:00
|
|
|
// Return true if the instruction type of Val is supported by
|
|
|
|
// constantFoldUser(). Currently CastInst and BinaryOperator only. Call this
|
|
|
|
// before calling constantFoldUser() to find out if it's even worth attempting
|
|
|
|
// to call it.
|
|
|
|
static bool isOperationFoldable(User *Usr) {
|
|
|
|
return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Usr can be simplified to an integer constant when the value of one
|
2017-08-04 05:11:30 +08:00
|
|
|
// of its operands Op is an integer constant OpConstVal. If so, return it as an
|
|
|
|
// lattice value range with a single element or otherwise return an overdefined
|
|
|
|
// lattice value.
|
2017-09-28 19:09:22 +08:00
|
|
|
static ValueLatticeElement constantFoldUser(User *Usr, Value *Op,
|
|
|
|
const APInt &OpConstVal,
|
|
|
|
const DataLayout &DL) {
|
2017-08-10 10:23:14 +08:00
|
|
|
assert(isOperationFoldable(Usr) && "Precondition");
|
2017-08-04 05:11:30 +08:00
|
|
|
Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal);
|
2017-08-10 10:23:14 +08:00
|
|
|
// Check if Usr can be simplified to a constant.
|
|
|
|
if (auto *CI = dyn_cast<CastInst>(Usr)) {
|
2017-08-04 05:11:30 +08:00
|
|
|
assert(CI->getOperand(0) == Op && "Operand 0 isn't Op");
|
|
|
|
if (auto *C = dyn_cast_or_null<ConstantInt>(
|
|
|
|
SimplifyCastInst(CI->getOpcode(), OpConst,
|
|
|
|
CI->getDestTy(), DL))) {
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
|
2017-08-04 05:11:30 +08:00
|
|
|
}
|
2017-08-10 10:23:14 +08:00
|
|
|
} else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) {
|
2017-08-04 05:11:30 +08:00
|
|
|
bool Op0Match = BO->getOperand(0) == Op;
|
|
|
|
bool Op1Match = BO->getOperand(1) == Op;
|
|
|
|
assert((Op0Match || Op1Match) &&
|
|
|
|
"Operand 0 nor Operand 1 isn't a match");
|
|
|
|
Value *LHS = Op0Match ? OpConst : BO->getOperand(0);
|
|
|
|
Value *RHS = Op1Match ? OpConst : BO->getOperand(1);
|
|
|
|
if (auto *C = dyn_cast_or_null<ConstantInt>(
|
|
|
|
SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) {
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getRange(ConstantRange(C->getValue()));
|
2017-08-04 05:11:30 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::getOverdefined();
|
2017-08-04 05:11:30 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Compute the value of Val on the edge BBFrom -> BBTo. Returns false if
|
2016-02-02 07:21:11 +08:00
|
|
|
/// Val is not constrained on the edge. Result is unspecified if return value
|
|
|
|
/// is false.
|
2012-06-28 09:16:18 +08:00
|
|
|
static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
|
2017-09-28 19:09:22 +08:00
|
|
|
BasicBlock *BBTo, ValueLatticeElement &Result) {
|
2015-07-28 23:53:21 +08:00
|
|
|
// TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
|
2009-11-16 04:02:12 +08:00
|
|
|
// know that v != 0.
|
2009-11-16 03:59:49 +08:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
|
|
|
|
// If this is a conditional branch and only one successor goes to BBTo, then
|
2015-01-10 00:35:37 +08:00
|
|
|
// we may be able to infer something from the condition.
|
2009-11-16 03:59:49 +08:00
|
|
|
if (BI->isConditional() &&
|
|
|
|
BI->getSuccessor(0) != BI->getSuccessor(1)) {
|
|
|
|
bool isTrueDest = BI->getSuccessor(0) == BBTo;
|
|
|
|
assert(BI->getSuccessor(!isTrueDest) == BBTo &&
|
|
|
|
"BBTo isn't a successor of BBFrom");
|
2017-08-04 05:11:30 +08:00
|
|
|
Value *Condition = BI->getCondition();
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
// If V is the condition of the branch itself, then we know exactly what
|
|
|
|
// it is.
|
2017-08-04 05:11:30 +08:00
|
|
|
if (Condition == Val) {
|
2017-09-28 19:09:22 +08:00
|
|
|
Result = ValueLatticeElement::get(ConstantInt::get(
|
2010-08-11 04:03:09 +08:00
|
|
|
Type::getInt1Ty(Val->getContext()), isTrueDest));
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
// If the condition of the branch is an equality comparison, we may be
|
|
|
|
// able to infer the value.
|
2017-08-04 05:11:30 +08:00
|
|
|
Result = getValueFromCondition(Val, Condition, isTrueDest);
|
|
|
|
if (!Result.isOverdefined())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (User *Usr = dyn_cast<User>(Val)) {
|
|
|
|
assert(Result.isOverdefined() && "Result isn't overdefined");
|
2017-08-10 10:23:14 +08:00
|
|
|
// Check with isOperationFoldable() first to avoid linearly iterating
|
|
|
|
// over the operands unnecessarily which can be expensive for
|
|
|
|
// instructions with many operands.
|
|
|
|
if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
|
2017-08-04 05:11:30 +08:00
|
|
|
const DataLayout &DL = BBTo->getModule()->getDataLayout();
|
|
|
|
if (usesOperand(Usr, Condition)) {
|
|
|
|
// If Val has Condition as an operand and Val can be folded into a
|
|
|
|
// constant with either Condition == true or Condition == false,
|
|
|
|
// propagate the constant.
|
|
|
|
// eg.
|
|
|
|
// ; %Val is true on the edge to %then.
|
|
|
|
// %Val = and i1 %Condition, true.
|
|
|
|
// br %Condition, label %then, label %else
|
|
|
|
APInt ConditionVal(1, isTrueDest ? 1 : 0);
|
2017-08-10 10:23:14 +08:00
|
|
|
Result = constantFoldUser(Usr, Condition, ConditionVal, DL);
|
2017-08-04 05:11:30 +08:00
|
|
|
} else {
|
|
|
|
// If one of Val's operand has an inferred value, we may be able to
|
|
|
|
// infer the value of Val.
|
|
|
|
// eg.
|
|
|
|
// ; %Val is 94 on the edge to %then.
|
|
|
|
// %Val = add i8 %Op, 1
|
|
|
|
// %Condition = icmp eq i8 %Op, 93
|
|
|
|
// br i1 %Condition, label %then, label %else
|
|
|
|
for (unsigned i = 0; i < Usr->getNumOperands(); ++i) {
|
|
|
|
Value *Op = Usr->getOperand(i);
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement OpLatticeVal =
|
2017-08-04 05:11:30 +08:00
|
|
|
getValueFromCondition(Op, Condition, isTrueDest);
|
|
|
|
if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) {
|
2017-08-10 10:23:14 +08:00
|
|
|
Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL);
|
2017-08-04 05:11:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 21:38:07 +08:00
|
|
|
if (!Result.isOverdefined())
|
2016-08-03 00:20:48 +08:00
|
|
|
return true;
|
2009-11-16 03:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-16 04:02:12 +08:00
|
|
|
|
|
|
|
// If the edge was formed by a switch on the value, then we may know exactly
|
|
|
|
// what it is.
|
|
|
|
if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
|
2017-08-04 05:11:30 +08:00
|
|
|
Value *Condition = SI->getCondition();
|
|
|
|
if (!isa<IntegerType>(Val->getType()))
|
2012-06-29 00:13:37 +08:00
|
|
|
return false;
|
2017-08-10 10:23:14 +08:00
|
|
|
bool ValUsesConditionAndMayBeFoldable = false;
|
2017-08-04 05:11:30 +08:00
|
|
|
if (Condition != Val) {
|
|
|
|
// Check if Val has Condition as an operand.
|
|
|
|
if (User *Usr = dyn_cast<User>(Val))
|
2017-08-10 10:23:14 +08:00
|
|
|
ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) &&
|
|
|
|
usesOperand(Usr, Condition);
|
|
|
|
if (!ValUsesConditionAndMayBeFoldable)
|
2017-08-04 05:11:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-10 10:23:14 +08:00
|
|
|
assert((Condition == Val || ValUsesConditionAndMayBeFoldable) &&
|
2017-08-04 05:11:30 +08:00
|
|
|
"Condition != Val nor Val doesn't use Condition");
|
2012-06-29 00:13:37 +08:00
|
|
|
|
|
|
|
bool DefaultCase = SI->getDefaultDest() == BBTo;
|
|
|
|
unsigned BitWidth = Val->getType()->getIntegerBitWidth();
|
|
|
|
ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
|
|
|
|
|
2017-04-12 15:27:28 +08:00
|
|
|
for (auto Case : SI->cases()) {
|
2017-08-04 05:11:30 +08:00
|
|
|
APInt CaseValue = Case.getCaseValue()->getValue();
|
|
|
|
ConstantRange EdgeVal(CaseValue);
|
2017-08-10 10:23:14 +08:00
|
|
|
if (ValUsesConditionAndMayBeFoldable) {
|
|
|
|
User *Usr = cast<User>(Val);
|
2017-08-04 05:11:30 +08:00
|
|
|
const DataLayout &DL = BBTo->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement EdgeLatticeVal =
|
2017-08-10 10:23:14 +08:00
|
|
|
constantFoldUser(Usr, Condition, CaseValue, DL);
|
2017-08-04 05:11:30 +08:00
|
|
|
if (EdgeLatticeVal.isOverdefined())
|
|
|
|
return false;
|
|
|
|
EdgeVal = EdgeLatticeVal.getConstantRange();
|
|
|
|
}
|
2012-09-06 07:45:58 +08:00
|
|
|
if (DefaultCase) {
|
|
|
|
// It is possible that the default destination is the destination of
|
2017-08-04 05:11:30 +08:00
|
|
|
// some cases. We cannot perform difference for those cases.
|
|
|
|
// We know Condition != CaseValue in BBTo. In some cases we can use
|
|
|
|
// this to infer Val == f(Condition) is != f(CaseValue). For now, we
|
|
|
|
// only do this when f is identity (i.e. Val == Condition), but we
|
|
|
|
// should be able to do this for any injective f.
|
|
|
|
if (Case.getCaseSuccessor() != BBTo && Condition == Val)
|
2012-09-06 07:45:58 +08:00
|
|
|
EdgesVals = EdgesVals.difference(EdgeVal);
|
2017-04-12 15:27:28 +08:00
|
|
|
} else if (Case.getCaseSuccessor() == BBTo)
|
2012-05-19 05:02:10 +08:00
|
|
|
EdgesVals = EdgesVals.unionWith(EdgeVal);
|
2009-11-16 04:02:12 +08:00
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
Result = ValueLatticeElement::getRange(std::move(EdgesVals));
|
2012-06-29 00:13:37 +08:00
|
|
|
return true;
|
2009-11-16 04:02:12 +08:00
|
|
|
}
|
2012-06-28 09:16:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Compute the value of Val on the edge BBFrom -> BBTo or the value at
|
2015-01-10 00:35:37 +08:00
|
|
|
/// the basic block if the edge does not constrain Val.
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom,
|
2017-09-28 19:09:22 +08:00
|
|
|
BasicBlock *BBTo,
|
|
|
|
ValueLatticeElement &Result,
|
2017-02-25 04:59:26 +08:00
|
|
|
Instruction *CxtI) {
|
2012-06-28 09:16:18 +08:00
|
|
|
// If already a constant, there is nothing to compute.
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(Val)) {
|
2017-09-28 19:09:22 +08:00
|
|
|
Result = ValueLatticeElement::get(VC);
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-28 09:16:18 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement LocalResult;
|
2016-02-02 11:15:40 +08:00
|
|
|
if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
|
|
|
|
// If we couldn't constrain the value on the edge, LocalResult doesn't
|
|
|
|
// provide any information.
|
2017-09-28 19:09:22 +08:00
|
|
|
LocalResult = ValueLatticeElement::getOverdefined();
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-02-02 11:15:40 +08:00
|
|
|
if (hasSingleValue(LocalResult)) {
|
|
|
|
// Can't get any more precise here
|
|
|
|
Result = LocalResult;
|
2012-06-28 09:16:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasBlockValue(Val, BBFrom)) {
|
2014-11-26 01:23:05 +08:00
|
|
|
if (pushBlockValue(std::make_pair(BBFrom, Val)))
|
|
|
|
return false;
|
2016-02-02 11:15:40 +08:00
|
|
|
// No new information.
|
|
|
|
Result = LocalResult;
|
2014-11-26 01:23:05 +08:00
|
|
|
return true;
|
2012-06-28 09:16:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 11:15:40 +08:00
|
|
|
// Try to intersect ranges of the BB and the constraint on the edge.
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement InBlock = getBlockValue(Val, BBFrom);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
|
|
|
|
BBFrom->getTerminator());
|
2014-10-16 08:40:05 +08:00
|
|
|
// We can use the context instruction (generically the ultimate instruction
|
|
|
|
// the calling pass is trying to simplify) here, even though the result of
|
|
|
|
// this function is generally cached when called from the solve* functions
|
|
|
|
// (and that cached result might be used with queries using a different
|
|
|
|
// context instruction), because when this function is called from the solve*
|
|
|
|
// functions, the context instruction is not provided. When called from
|
2016-09-13 05:46:58 +08:00
|
|
|
// LazyValueInfoImpl::getValueOnEdge, the context instruction is provided,
|
2014-10-16 08:40:05 +08:00
|
|
|
// but then the result is not cached.
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI);
|
2016-02-02 11:15:40 +08:00
|
|
|
|
|
|
|
Result = intersect(LocalResult, InBlock);
|
2012-06-28 09:16:18 +08:00
|
|
|
return true;
|
2009-11-16 03:59:49 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
|
|
|
|
Instruction *CxtI) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
|
|
|
|
<< BB->getName() << "'\n");
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2014-11-26 01:23:05 +08:00
|
|
|
assert(BlockValueStack.empty() && BlockValueSet.empty());
|
2016-02-11 05:46:32 +08:00
|
|
|
if (!hasBlockValue(V, BB)) {
|
2016-07-25 08:59:46 +08:00
|
|
|
pushBlockValue(std::make_pair(BB, V));
|
2016-02-11 05:46:32 +08:00
|
|
|
solve();
|
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result = getBlockValue(V, BB);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
|
2014-09-08 04:29:59 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "LVI Getting value " << *V << " at '" << CxtI->getName()
|
|
|
|
<< "'\n");
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2016-02-11 05:46:32 +08:00
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
2017-09-28 19:09:22 +08:00
|
|
|
return ValueLatticeElement::get(C);
|
2016-02-11 05:46:32 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result = ValueLatticeElement::getOverdefined();
|
[LVI/CVP] Teach LVI about range metadata
Somewhat shockingly for an analysis pass which is computing constant ranges, LVI did not understand the ranges provided by range metadata.
As part of this change, I included a change to CVP primarily because doing so made it much easier to write small self contained test cases. CVP was previously only handling the non-local operand case, but given that LVI can sometimes figure out information about instructions standalone, I don't see any reason to restrict this. There could possibly be a compile time impact from this, but I suspect it should be minimal. If anyone has an example which substaintially regresses, please let me know. I could restrict the block local handling to ICmps feeding Terminator instructions if needed.
Note that this patch continues a somewhat bad practice in LVI. In many cases, we know facts about values, and separate context sensitive facts about values. LVI makes no effort to distinguish and will frequently cache the same value fact repeatedly for different contexts. I would like to change this, but that's a large enough change that I want it to go in separately with clear documentation of what's changing. Other examples of this include the non-null handling, and arguments.
As a meta comment: the entire motivation of this change was being able to write smaller (aka reasonable sized) test cases for a future patch teaching LVI about select instructions.
Differential Revision: http://reviews.llvm.org/D13543
llvm-svn: 251606
2015-10-29 11:57:17 +08:00
|
|
|
if (auto *I = dyn_cast<Instruction>(V))
|
|
|
|
Result = getFromRangeMetadata(I);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
|
2016-02-02 08:45:30 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
|
2009-11-16 03:59:49 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement LazyValueInfoImpl::
|
2014-09-08 04:29:59 +08:00
|
|
|
getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
|
|
|
|
<< FromBB->getName() << "' to '" << ToBB->getName()
|
|
|
|
<< "'\n");
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result;
|
2014-09-08 04:29:59 +08:00
|
|
|
if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
|
2010-12-18 09:00:40 +08:00
|
|
|
solve();
|
2014-09-08 04:29:59 +08:00
|
|
|
bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI);
|
2010-12-18 09:00:40 +08:00
|
|
|
(void)WasFastQuery;
|
|
|
|
assert(WasFastQuery && "More work to do after problem solved?");
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
|
2009-11-16 03:59:49 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
|
2016-09-13 06:38:44 +08:00
|
|
|
BasicBlock *NewSucc) {
|
|
|
|
TheCache.threadEdgeImpl(OldSucc, NewSucc);
|
2010-07-27 02:48:03 +08:00
|
|
|
}
|
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LazyValueInfo Impl
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
/// This lazily constructs the LazyValueInfoImpl.
|
2016-12-19 16:22:17 +08:00
|
|
|
static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
|
|
|
|
const DataLayout *DL,
|
2016-09-13 05:46:58 +08:00
|
|
|
DominatorTree *DT = nullptr) {
|
2015-03-10 10:37:25 +08:00
|
|
|
if (!PImpl) {
|
|
|
|
assert(DL && "getCache() called with a null DataLayout");
|
2016-12-19 16:22:17 +08:00
|
|
|
PImpl = new LazyValueInfoImpl(AC, *DL, DT);
|
2015-03-10 10:37:25 +08:00
|
|
|
}
|
2016-09-13 05:46:58 +08:00
|
|
|
return *static_cast<LazyValueInfoImpl*>(PImpl);
|
2009-11-16 03:59:49 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
|
2016-12-19 16:22:17 +08:00
|
|
|
Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2014-09-08 04:29:59 +08:00
|
|
|
|
|
|
|
DominatorTreeWrapperPass *DTWP =
|
|
|
|
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
2016-06-14 06:01:25 +08:00
|
|
|
Info.DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
2011-12-02 09:26:24 +08:00
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
if (Info.PImpl)
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
|
2014-09-08 04:29:59 +08:00
|
|
|
|
2010-08-19 02:39:01 +08:00
|
|
|
// Fully lazy.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2011-12-02 09:26:24 +08:00
|
|
|
AU.setPreservesAll();
|
2016-12-19 16:22:17 +08:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
2015-01-15 18:41:28 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2011-12-02 09:26:24 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; }
|
|
|
|
|
|
|
|
LazyValueInfo::~LazyValueInfo() { releaseMemory(); }
|
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
void LazyValueInfo::releaseMemory() {
|
|
|
|
// If the cache was allocated, free it.
|
|
|
|
if (PImpl) {
|
2016-12-19 16:22:17 +08:00
|
|
|
delete &getImpl(PImpl, AC, nullptr);
|
2014-04-15 12:59:12 +08:00
|
|
|
PImpl = nullptr;
|
2009-11-16 03:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 06:48:44 +08:00
|
|
|
|
2017-01-23 14:35:12 +08:00
|
|
|
bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &Inv) {
|
|
|
|
// We need to invalidate if we have either failed to preserve this analyses
|
|
|
|
// result directly or if any of its dependencies have been invalidated.
|
|
|
|
auto PAC = PA.getChecker<LazyValueAnalysis>();
|
|
|
|
if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
|
|
|
|
(DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:01:25 +08:00
|
|
|
void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
LazyValueInfo LazyValueAnalysis::run(Function &F,
|
|
|
|
FunctionAnalysisManager &FAM) {
|
2016-12-19 16:22:17 +08:00
|
|
|
auto &AC = FAM.getResult<AssumptionAnalysis>(F);
|
2016-06-14 06:01:25 +08:00
|
|
|
auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
|
|
|
|
auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
|
|
|
|
|
2017-03-12 22:06:41 +08:00
|
|
|
return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT);
|
2016-06-14 06:01:25 +08:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:28:34 +08:00
|
|
|
/// Returns true if we can statically tell that this value will never be a
|
|
|
|
/// "useful" constant. In practice, this means we've got something like an
|
|
|
|
/// alloca or a malloc call for which a comparison against a constant can
|
|
|
|
/// only be guarding dead code. Note that we are potentially giving up some
|
|
|
|
/// precision in dead code (a constant result) in favour of avoiding a
|
|
|
|
/// expensive search for a easily answered common query.
|
|
|
|
static bool isKnownNonConstant(Value *V) {
|
|
|
|
V = V->stripPointerCasts();
|
|
|
|
// The return val of alloc cannot be a Constant.
|
|
|
|
if (isa<AllocaInst>(V))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-08 04:29:59 +08:00
|
|
|
Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
|
|
|
|
Instruction *CxtI) {
|
2016-09-15 14:28:34 +08:00
|
|
|
// Bail out early if V is known not to be a Constant.
|
|
|
|
if (isKnownNonConstant(V))
|
|
|
|
return nullptr;
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result =
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
|
2015-01-04 20:03:27 +08:00
|
|
|
|
2009-11-12 06:48:44 +08:00
|
|
|
if (Result.isConstant())
|
|
|
|
return Result.getConstant();
|
2010-12-16 02:57:18 +08:00
|
|
|
if (Result.isConstantRange()) {
|
2017-05-06 11:35:15 +08:00
|
|
|
const ConstantRange &CR = Result.getConstantRange();
|
2010-08-28 07:29:38 +08:00
|
|
|
if (const APInt *SingleVal = CR.getSingleElement())
|
|
|
|
return ConstantInt::get(V->getContext(), *SingleVal);
|
|
|
|
}
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-11-12 06:48:44 +08:00
|
|
|
}
|
2009-11-11 10:08:33 +08:00
|
|
|
|
2016-05-03 03:58:00 +08:00
|
|
|
ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB,
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
Instruction *CxtI,
|
|
|
|
bool UndefAllowed) {
|
2016-05-03 03:58:00 +08:00
|
|
|
assert(V->getType()->isIntegerTy());
|
|
|
|
unsigned Width = V->getType()->getIntegerBitWidth();
|
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result =
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
|
2020-03-15 00:50:09 +08:00
|
|
|
if (Result.isUnknown())
|
2019-03-24 17:34:40 +08:00
|
|
|
return ConstantRange::getEmpty(Width);
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 18:10:00 +08:00
|
|
|
if (Result.isConstantRange(UndefAllowed))
|
|
|
|
return Result.getConstantRange(UndefAllowed);
|
2016-08-10 20:54:54 +08:00
|
|
|
// We represent ConstantInt constants as constant ranges but other kinds
|
|
|
|
// of integer constants, i.e. ConstantExpr will be tagged as constants
|
|
|
|
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
|
|
|
|
"ConstantInt value must be represented as constantrange");
|
2019-03-24 17:34:40 +08:00
|
|
|
return ConstantRange::getFull(Width);
|
2016-05-03 03:58:00 +08:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// Determine whether the specified value is known to be a
|
2015-07-28 23:53:21 +08:00
|
|
|
/// constant on the specified edge. Return null if not.
|
2009-11-12 09:29:10 +08:00
|
|
|
Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
|
2014-09-08 04:29:59 +08:00
|
|
|
BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = FromBB->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result =
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
|
2015-01-04 20:03:27 +08:00
|
|
|
|
2009-11-12 09:29:10 +08:00
|
|
|
if (Result.isConstant())
|
|
|
|
return Result.getConstant();
|
2010-12-16 02:57:18 +08:00
|
|
|
if (Result.isConstantRange()) {
|
2017-05-06 11:35:15 +08:00
|
|
|
const ConstantRange &CR = Result.getConstantRange();
|
2010-08-11 04:03:09 +08:00
|
|
|
if (const APInt *SingleVal = CR.getSingleElement())
|
|
|
|
return ConstantInt::get(V->getContext(), *SingleVal);
|
|
|
|
}
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-11-12 09:29:10 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 13:41:35 +08:00
|
|
|
ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
|
|
|
|
BasicBlock *FromBB,
|
|
|
|
BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI) {
|
|
|
|
unsigned Width = V->getType()->getIntegerBitWidth();
|
|
|
|
const DataLayout &DL = FromBB->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result =
|
2017-06-23 13:41:35 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
|
|
|
|
|
2020-03-15 00:50:09 +08:00
|
|
|
if (Result.isUnknown())
|
2019-03-24 17:34:40 +08:00
|
|
|
return ConstantRange::getEmpty(Width);
|
2017-06-23 13:41:35 +08:00
|
|
|
if (Result.isConstantRange())
|
|
|
|
return Result.getConstantRange();
|
|
|
|
// We represent ConstantInt constants as constant ranges but other kinds
|
|
|
|
// of integer constants, i.e. ConstantExpr will be tagged as constants
|
|
|
|
assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) &&
|
|
|
|
"ConstantInt value must be represented as constantrange");
|
2019-03-24 17:34:40 +08:00
|
|
|
return ConstantRange::getFull(Width);
|
2017-06-23 13:41:35 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:09:22 +08:00
|
|
|
static LazyValueInfo::Tristate
|
|
|
|
getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val,
|
|
|
|
const DataLayout &DL, TargetLibraryInfo *TLI) {
|
2009-11-12 12:36:58 +08:00
|
|
|
// If we know the value is a constant, evaluate the conditional.
|
2014-04-15 12:59:12 +08:00
|
|
|
Constant *Res = nullptr;
|
2017-06-10 05:18:16 +08:00
|
|
|
if (Val.isConstant()) {
|
|
|
|
Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI);
|
2010-12-16 02:57:18 +08:00
|
|
|
if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
|
2014-09-08 04:29:59 +08:00
|
|
|
return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
|
|
|
|
return LazyValueInfo::Unknown;
|
2009-11-16 03:59:49 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-06-10 05:18:16 +08:00
|
|
|
if (Val.isConstantRange()) {
|
2010-08-24 15:55:44 +08:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(C);
|
2014-09-08 04:29:59 +08:00
|
|
|
if (!CI) return LazyValueInfo::Unknown;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-06-10 05:18:16 +08:00
|
|
|
const ConstantRange &CR = Val.getConstantRange();
|
2010-08-11 04:03:09 +08:00
|
|
|
if (Pred == ICmpInst::ICMP_EQ) {
|
|
|
|
if (!CR.contains(CI->getValue()))
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::False;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-06-07 08:58:09 +08:00
|
|
|
if (CR.isSingleElement())
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::True;
|
2010-08-11 04:03:09 +08:00
|
|
|
} else if (Pred == ICmpInst::ICMP_NE) {
|
|
|
|
if (!CR.contains(CI->getValue()))
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::True;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-06-07 08:58:09 +08:00
|
|
|
if (CR.isSingleElement())
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::False;
|
2017-06-10 00:16:20 +08:00
|
|
|
} else {
|
|
|
|
// Handle more complex predicates.
|
|
|
|
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
|
|
|
|
(ICmpInst::Predicate)Pred, CI->getValue());
|
|
|
|
if (TrueValues.contains(CR))
|
|
|
|
return LazyValueInfo::True;
|
|
|
|
if (TrueValues.inverse().contains(CR))
|
|
|
|
return LazyValueInfo::False;
|
2010-08-11 04:03:09 +08:00
|
|
|
}
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::Unknown;
|
2010-08-11 04:03:09 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2017-06-10 05:18:16 +08:00
|
|
|
if (Val.isNotConstant()) {
|
2009-11-12 12:36:58 +08:00
|
|
|
// If this is an equality comparison, we can try to fold it knowing that
|
|
|
|
// "V != C1".
|
|
|
|
if (Pred == ICmpInst::ICMP_EQ) {
|
2012-09-27 18:14:43 +08:00
|
|
|
// !C1 == C -> false iff C1 == C.
|
2009-11-12 12:36:58 +08:00
|
|
|
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
|
2017-06-10 05:18:16 +08:00
|
|
|
Val.getNotConstant(), C, DL,
|
2011-12-02 09:26:24 +08:00
|
|
|
TLI);
|
2009-11-12 12:36:58 +08:00
|
|
|
if (Res->isNullValue())
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::False;
|
2009-11-12 12:36:58 +08:00
|
|
|
} else if (Pred == ICmpInst::ICMP_NE) {
|
2012-09-27 18:14:43 +08:00
|
|
|
// !C1 != C -> true iff C1 == C.
|
2009-11-16 04:01:24 +08:00
|
|
|
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
|
2017-06-10 05:18:16 +08:00
|
|
|
Val.getNotConstant(), C, DL,
|
2011-12-02 09:26:24 +08:00
|
|
|
TLI);
|
2009-11-12 12:36:58 +08:00
|
|
|
if (Res->isNullValue())
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::True;
|
2009-11-12 12:36:58 +08:00
|
|
|
}
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::Unknown;
|
2009-11-11 10:08:33 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::Unknown;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// Determine whether the specified value comparison with a constant is known to
|
|
|
|
/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
|
2014-09-08 04:29:59 +08:00
|
|
|
LazyValueInfo::Tristate
|
|
|
|
LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
|
|
|
|
BasicBlock *FromBB, BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = FromBB->getModule()->getDataLayout();
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result =
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
|
2014-09-08 04:29:59 +08:00
|
|
|
|
|
|
|
return getPredicateResult(Pred, C, Result, DL, TLI);
|
|
|
|
}
|
|
|
|
|
|
|
|
LazyValueInfo::Tristate
|
|
|
|
LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
|
|
|
|
Instruction *CxtI) {
|
2016-09-15 14:28:34 +08:00
|
|
|
// Is or is not NonNull are common predicates being queried. If
|
2017-09-10 02:23:11 +08:00
|
|
|
// isKnownNonZero can tell us the result of the predicate, we can
|
2016-09-15 14:28:34 +08:00
|
|
|
// return it quickly. But this is only a fastpath, and falling
|
|
|
|
// through would still be correct.
|
2017-09-10 02:23:11 +08:00
|
|
|
const DataLayout &DL = CxtI->getModule()->getDataLayout();
|
2016-09-15 14:28:34 +08:00
|
|
|
if (V->getType()->isPointerTy() && C->isNullValue() &&
|
Introduce Value::stripPointerCastsSameRepresentation
This patch allows current users of Value::stripPointerCasts() to force
the result of the function to have the same representation as the value
it was called on. This is useful in various cases, e.g., (non-)null
checks.
In this patch only a single call site was adjusted to fix an existing
misuse that would cause nonnull where they may be wrong. Uses in
attribute deduction and other areas, e.g., D60047, are to be expected.
For a discussion on this topic, please see [0].
[0] http://lists.llvm.org/pipermail/llvm-dev/2018-December/128423.html
Reviewers: hfinkel, arsenm, reames
Subscribers: wdng, hiraditya, bollu, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61607
llvm-svn: 362545
2019-06-05 04:21:46 +08:00
|
|
|
isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) {
|
2016-09-15 14:28:34 +08:00
|
|
|
if (Pred == ICmpInst::ICMP_EQ)
|
|
|
|
return LazyValueInfo::False;
|
|
|
|
else if (Pred == ICmpInst::ICMP_NE)
|
|
|
|
return LazyValueInfo::True;
|
|
|
|
}
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
|
2015-06-16 08:49:59 +08:00
|
|
|
Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
|
|
|
|
if (Ret != Unknown)
|
|
|
|
return Ret;
|
|
|
|
|
2015-11-04 09:47:04 +08:00
|
|
|
// Note: The following bit of code is somewhat distinct from the rest of LVI;
|
|
|
|
// LVI as a whole tries to compute a lattice value which is conservatively
|
|
|
|
// correct at a given location. In this case, we have a predicate which we
|
|
|
|
// weren't able to prove about the merged result, and we're pushing that
|
|
|
|
// predicate back along each incoming edge to see if we can prove it
|
|
|
|
// separately for each input. As a motivating example, consider:
|
|
|
|
// bb1:
|
|
|
|
// %v1 = ... ; constantrange<1, 5>
|
|
|
|
// br label %merge
|
|
|
|
// bb2:
|
|
|
|
// %v2 = ... ; constantrange<10, 20>
|
|
|
|
// br label %merge
|
|
|
|
// merge:
|
|
|
|
// %phi = phi [%v1, %v2] ; constantrange<1,20>
|
|
|
|
// %pred = icmp eq i32 %phi, 8
|
|
|
|
// We can't tell from the lattice value for '%phi' that '%pred' is false
|
|
|
|
// along each path, but by checking the predicate over each input separately,
|
|
|
|
// we can.
|
|
|
|
// We limit the search to one step backwards from the current BB and value.
|
|
|
|
// We could consider extending this to search further backwards through the
|
|
|
|
// CFG and/or value graph, but there are non-obvious compile time vs quality
|
2016-07-04 09:26:27 +08:00
|
|
|
// tradeoffs.
|
2015-06-16 08:49:59 +08:00
|
|
|
if (CxtI) {
|
2015-09-01 02:31:48 +08:00
|
|
|
BasicBlock *BB = CxtI->getParent();
|
|
|
|
|
|
|
|
// Function entry or an unreachable block. Bail to avoid confusing
|
|
|
|
// analysis below.
|
|
|
|
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
if (PI == PE)
|
|
|
|
return Unknown;
|
|
|
|
|
|
|
|
// If V is a PHI node in the same block as the context, we need to ask
|
|
|
|
// questions about the predicate as applied to the incoming value along
|
|
|
|
// each edge. This is useful for eliminating cases where the predicate is
|
|
|
|
// known along all incoming edges.
|
|
|
|
if (auto *PHI = dyn_cast<PHINode>(V))
|
|
|
|
if (PHI->getParent() == BB) {
|
|
|
|
Tristate Baseline = Unknown;
|
|
|
|
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
|
|
|
|
Value *Incoming = PHI->getIncomingValue(i);
|
|
|
|
BasicBlock *PredBB = PHI->getIncomingBlock(i);
|
2016-07-04 09:26:27 +08:00
|
|
|
// Note that PredBB may be BB itself.
|
2015-09-01 02:31:48 +08:00
|
|
|
Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB,
|
|
|
|
CxtI);
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2015-09-01 02:31:48 +08:00
|
|
|
// Keep going as long as we've seen a consistent known result for
|
|
|
|
// all inputs.
|
|
|
|
Baseline = (i == 0) ? Result /* First iteration */
|
|
|
|
: (Baseline == Result ? Baseline : Unknown); /* All others */
|
|
|
|
if (Baseline == Unknown)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Baseline != Unknown)
|
|
|
|
return Baseline;
|
2016-07-25 08:59:46 +08:00
|
|
|
}
|
2015-09-01 02:31:48 +08:00
|
|
|
|
2015-06-16 08:49:59 +08:00
|
|
|
// For a comparison where the V is outside this block, it's possible
|
2015-07-28 23:53:21 +08:00
|
|
|
// that we've branched on it before. Look to see if the value is known
|
2015-06-16 08:49:59 +08:00
|
|
|
// on all incoming edges.
|
2015-09-01 02:31:48 +08:00
|
|
|
if (!isa<Instruction>(V) ||
|
|
|
|
cast<Instruction>(V)->getParent() != BB) {
|
2015-06-16 08:49:59 +08:00
|
|
|
// For predecessor edge, determine if the comparison is true or false
|
2015-07-28 23:53:21 +08:00
|
|
|
// on that edge. If they're all true or all false, we can conclude
|
2015-06-16 08:49:59 +08:00
|
|
|
// the value of the comparison in this block.
|
|
|
|
Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
|
|
|
|
if (Baseline != Unknown) {
|
|
|
|
// Check that all remaining incoming values match the first one.
|
|
|
|
while (++PI != PE) {
|
|
|
|
Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
|
|
|
|
if (Ret != Baseline) break;
|
|
|
|
}
|
|
|
|
// If we terminated early, then one of the values didn't match.
|
|
|
|
if (PI == PE) {
|
|
|
|
return Baseline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Unknown;
|
2009-11-11 08:22:30 +08:00
|
|
|
}
|
2009-11-11 10:08:33 +08:00
|
|
|
|
2010-07-27 02:48:03 +08:00
|
|
|
void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
|
2010-12-16 02:57:18 +08:00
|
|
|
BasicBlock *NewSucc) {
|
2015-03-10 10:37:25 +08:00
|
|
|
if (PImpl) {
|
|
|
|
const DataLayout &DL = PredBB->getModule()->getDataLayout();
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
|
2015-03-10 10:37:25 +08:00
|
|
|
}
|
2010-08-19 02:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LazyValueInfo::eraseBlock(BasicBlock *BB) {
|
2015-03-10 10:37:25 +08:00
|
|
|
if (PImpl) {
|
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
|
2015-03-10 10:37:25 +08:00
|
|
|
}
|
2010-07-27 02:48:03 +08:00
|
|
|
}
|
2017-03-23 03:27:12 +08:00
|
|
|
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) {
|
2017-03-23 03:27:12 +08:00
|
|
|
if (PImpl) {
|
2017-06-07 03:25:31 +08:00
|
|
|
getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 00:35:17 +08:00
|
|
|
void LazyValueInfo::disableDT() {
|
|
|
|
if (PImpl)
|
|
|
|
getImpl(PImpl, AC, DL, DT).disableDT();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LazyValueInfo::enableDT() {
|
|
|
|
if (PImpl)
|
|
|
|
getImpl(PImpl, AC, DL, DT).enableDT();
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
// Print the LVI for the function arguments at the start of each basic block.
|
|
|
|
void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot(
|
|
|
|
const BasicBlock *BB, formatted_raw_ostream &OS) {
|
|
|
|
// Find if there are latticevalues defined for arguments of the function.
|
|
|
|
auto *F = BB->getParent();
|
|
|
|
for (auto &Arg : F->args()) {
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result = LVIImpl->getValueInBlock(
|
2017-06-07 03:25:31 +08:00
|
|
|
const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB));
|
2020-03-15 00:50:09 +08:00
|
|
|
if (Result.isUnknown())
|
2017-06-07 03:25:31 +08:00
|
|
|
continue;
|
|
|
|
OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n";
|
2017-03-23 03:27:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
// This function prints the LVI analysis for the instruction I at the beginning
|
|
|
|
// of various basic blocks. It relies on calculated values that are stored in
|
2018-04-24 15:38:07 +08:00
|
|
|
// the LazyValueInfoCache, and in the absence of cached values, recalculate the
|
2017-06-07 03:25:31 +08:00
|
|
|
// LazyValueInfo for `I`, and print that info.
|
|
|
|
void LazyValueInfoAnnotatedWriter::emitInstructionAnnot(
|
|
|
|
const Instruction *I, formatted_raw_ostream &OS) {
|
|
|
|
|
|
|
|
auto *ParentBB = I->getParent();
|
|
|
|
SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI;
|
|
|
|
// We can generate (solve) LVI values only for blocks that are dominated by
|
|
|
|
// the I's parent. However, to avoid generating LVI for all dominating blocks,
|
|
|
|
// that contain redundant/uninteresting information, we print LVI for
|
|
|
|
// blocks that may use this LVI information (such as immediate successor
|
|
|
|
// blocks, and blocks that contain uses of `I`).
|
|
|
|
auto printResult = [&](const BasicBlock *BB) {
|
|
|
|
if (!BlocksContainingLVI.insert(BB).second)
|
|
|
|
return;
|
2017-09-28 19:09:22 +08:00
|
|
|
ValueLatticeElement Result = LVIImpl->getValueInBlock(
|
2017-06-07 03:25:31 +08:00
|
|
|
const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB));
|
|
|
|
OS << "; LatticeVal for: '" << *I << "' in BB: '";
|
|
|
|
BB->printAsOperand(OS, false);
|
|
|
|
OS << "' is: " << Result << "\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
printResult(ParentBB);
|
2018-01-17 20:29:38 +08:00
|
|
|
// Print the LVI analysis results for the immediate successor blocks, that
|
2017-06-07 03:25:31 +08:00
|
|
|
// are dominated by `ParentBB`.
|
|
|
|
for (auto *BBSucc : successors(ParentBB))
|
|
|
|
if (DT.dominates(ParentBB, BBSucc))
|
|
|
|
printResult(BBSucc);
|
|
|
|
|
|
|
|
// Print LVI in blocks where `I` is used.
|
|
|
|
for (auto *U : I->users())
|
|
|
|
if (auto *UseI = dyn_cast<Instruction>(U))
|
|
|
|
if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent()))
|
|
|
|
printResult(UseI->getParent());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:27:12 +08:00
|
|
|
namespace {
|
|
|
|
// Printer class for LazyValueInfo results.
|
|
|
|
class LazyValueInfoPrinter : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
LazyValueInfoPrinter() : FunctionPass(ID) {
|
|
|
|
initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<LazyValueInfoWrapperPass>();
|
2017-06-07 03:25:31 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2017-03-23 03:27:12 +08:00
|
|
|
}
|
|
|
|
|
2017-06-07 03:25:31 +08:00
|
|
|
// Get the mandatory dominator tree analysis and pass this in to the
|
|
|
|
// LVIPrinter. We cannot rely on the LVI's DT, since it's optional.
|
2017-03-23 03:27:12 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
dbgs() << "LVI for function '" << F.getName() << "':\n";
|
|
|
|
auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI();
|
2017-06-07 03:25:31 +08:00
|
|
|
auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
LVI.printLVI(F, DTree, dbgs());
|
2017-03-23 03:27:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char LazyValueInfoPrinter::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info",
|
|
|
|
"Lazy Value Info Printer Pass", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info",
|
|
|
|
"Lazy Value Info Printer Pass", false, false)
|