2014-11-22 02:58:23 +08:00
|
|
|
//===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===//
|
2009-11-11 08:22:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#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"
|
2015-01-15 10:16:27 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2010-12-16 04:02:24 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.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"
|
2009-11-12 09:22:16 +08:00
|
|
|
#include "llvm/Support/Debug.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>
|
2010-12-18 09:00:40 +08:00
|
|
|
#include <stack>
|
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;
|
|
|
|
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
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LVILatticeVal
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is the information tracked by LazyValueInfo for each value.
|
2009-11-11 10:08:33 +08:00
|
|
|
///
|
|
|
|
/// FIXME: This is basically just for bringup, this can be made a lot more rich
|
|
|
|
/// in the future.
|
|
|
|
///
|
|
|
|
namespace {
|
|
|
|
class LVILatticeVal {
|
|
|
|
enum LatticeValueTy {
|
2016-04-26 02:48:43 +08:00
|
|
|
/// This Value has no known value yet. As a result, this implies the
|
|
|
|
/// producing instruction is dead. Caution: We use this as the starting
|
|
|
|
/// state in our local meet rules. In this usage, it's taken to mean
|
2016-07-04 09:26:27 +08:00
|
|
|
/// "nothing known yet".
|
2009-11-11 10:08:33 +08:00
|
|
|
undefined,
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-07 12:48:50 +08:00
|
|
|
/// This Value has a specific constant value. (For constant integers,
|
|
|
|
/// constantrange is used instead. Integer typed constantexprs can appear
|
|
|
|
/// as constant.)
|
2009-11-11 10:08:33 +08:00
|
|
|
constant,
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-07 12:48:50 +08:00
|
|
|
/// This Value is known to not have the specified value. (For constant
|
|
|
|
/// integers, constantrange is used instead. As above, integer typed
|
|
|
|
/// constantexprs can appear here.)
|
2009-11-12 12:36:58 +08:00
|
|
|
notconstant,
|
2011-12-02 09:26:24 +08:00
|
|
|
|
2016-04-26 02:48:43 +08:00
|
|
|
/// The Value falls within this range. (Used only for integer typed values.)
|
2010-08-06 06:59:19 +08:00
|
|
|
constantrange,
|
2011-12-02 09:26:24 +08:00
|
|
|
|
2016-04-26 02:48:43 +08:00
|
|
|
/// We can not precisely model the dynamic values this value might take.
|
2009-11-11 10:08:33 +08:00
|
|
|
overdefined
|
|
|
|
};
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-11 10:08:33 +08:00
|
|
|
/// Val: This stores the current lattice value along with the Constant* for
|
2009-11-12 12:36:58 +08:00
|
|
|
/// the constant if this is a 'constant' or 'notconstant' value.
|
2010-08-06 06:10:46 +08:00
|
|
|
LatticeValueTy Tag;
|
|
|
|
Constant *Val;
|
2010-08-06 06:59:19 +08:00
|
|
|
ConstantRange Range;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-11 10:08:33 +08:00
|
|
|
public:
|
2014-04-15 12:59:12 +08:00
|
|
|
LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
|
2009-11-11 10:08:33 +08:00
|
|
|
|
2009-11-12 06:48:44 +08:00
|
|
|
static LVILatticeVal get(Constant *C) {
|
|
|
|
LVILatticeVal Res;
|
2010-12-16 02:57:18 +08:00
|
|
|
if (!isa<UndefValue>(C))
|
2010-08-11 04:03:09 +08:00
|
|
|
Res.markConstant(C);
|
2009-11-12 06:48:44 +08:00
|
|
|
return Res;
|
|
|
|
}
|
2009-11-12 12:36:58 +08:00
|
|
|
static LVILatticeVal getNot(Constant *C) {
|
|
|
|
LVILatticeVal Res;
|
2010-12-16 02:57:18 +08:00
|
|
|
if (!isa<UndefValue>(C))
|
2010-08-11 04:03:09 +08:00
|
|
|
Res.markNotConstant(C);
|
2009-11-12 12:36:58 +08:00
|
|
|
return Res;
|
|
|
|
}
|
2010-08-11 07:20:01 +08:00
|
|
|
static LVILatticeVal getRange(ConstantRange CR) {
|
|
|
|
LVILatticeVal Res;
|
2016-02-20 18:40:34 +08:00
|
|
|
Res.markConstantRange(std::move(CR));
|
2010-08-11 07:20:01 +08:00
|
|
|
return Res;
|
|
|
|
}
|
2015-12-11 08:49:47 +08:00
|
|
|
static LVILatticeVal getOverdefined() {
|
|
|
|
LVILatticeVal Res;
|
|
|
|
Res.markOverdefined();
|
|
|
|
return Res;
|
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2010-08-06 06:59:19 +08:00
|
|
|
bool isUndefined() const { return Tag == undefined; }
|
|
|
|
bool isConstant() const { return Tag == constant; }
|
|
|
|
bool isNotConstant() const { return Tag == notconstant; }
|
|
|
|
bool isConstantRange() const { return Tag == constantrange; }
|
|
|
|
bool isOverdefined() const { return Tag == overdefined; }
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-11 10:08:33 +08:00
|
|
|
Constant *getConstant() const {
|
|
|
|
assert(isConstant() && "Cannot get the constant of a non-constant!");
|
2010-08-06 06:10:46 +08:00
|
|
|
return Val;
|
2009-11-11 10:08:33 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-12 12:36:58 +08:00
|
|
|
Constant *getNotConstant() const {
|
|
|
|
assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
|
2010-08-06 06:10:46 +08:00
|
|
|
return Val;
|
2009-11-11 10:08:33 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2010-08-06 06:59:19 +08:00
|
|
|
ConstantRange getConstantRange() const {
|
|
|
|
assert(isConstantRange() &&
|
|
|
|
"Cannot get the constant-range of a non-constant-range!");
|
|
|
|
return Range;
|
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-06 11:01:08 +08:00
|
|
|
private:
|
2016-12-07 09:03:56 +08:00
|
|
|
void markOverdefined() {
|
2009-11-11 10:08:33 +08:00
|
|
|
if (isOverdefined())
|
2016-12-07 09:03:56 +08:00
|
|
|
return;
|
2010-08-06 06:10:46 +08:00
|
|
|
Tag = overdefined;
|
2009-11-11 10:08:33 +08:00
|
|
|
}
|
|
|
|
|
2016-12-07 09:03:56 +08:00
|
|
|
void markConstant(Constant *V) {
|
2010-12-16 02:57:18 +08:00
|
|
|
assert(V && "Marking constant with NULL");
|
2016-12-07 09:03:56 +08:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
markConstantRange(ConstantRange(CI->getValue()));
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 02:57:18 +08:00
|
|
|
if (isa<UndefValue>(V))
|
2016-12-07 09:03:56 +08:00
|
|
|
return;
|
2010-12-16 02:57:18 +08:00
|
|
|
|
|
|
|
assert((!isConstant() || getConstant() == V) &&
|
|
|
|
"Marking constant with different value");
|
2009-11-11 10:08:33 +08:00
|
|
|
assert(isUndefined());
|
2010-08-06 06:10:46 +08:00
|
|
|
Tag = constant;
|
|
|
|
Val = V;
|
2009-11-12 06:48:44 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-07 09:03:56 +08:00
|
|
|
void markNotConstant(Constant *V) {
|
2010-12-16 02:57:18 +08:00
|
|
|
assert(V && "Marking constant with NULL");
|
2016-12-07 09:03:56 +08:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 02:57:18 +08:00
|
|
|
if (isa<UndefValue>(V))
|
2016-12-07 09:03:56 +08:00
|
|
|
return;
|
2009-11-12 12:36:58 +08:00
|
|
|
|
2010-12-16 02:57:18 +08:00
|
|
|
assert((!isConstant() || getConstant() != V) &&
|
|
|
|
"Marking constant !constant with same value");
|
|
|
|
assert((!isNotConstant() || getNotConstant() == V) &&
|
|
|
|
"Marking !constant with different value");
|
|
|
|
assert(isUndefined() || isConstant());
|
2010-08-06 06:10:46 +08:00
|
|
|
Tag = notconstant;
|
|
|
|
Val = V;
|
2009-11-12 12:36:58 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-07 09:03:56 +08:00
|
|
|
void markConstantRange(ConstantRange NewR) {
|
2010-08-06 06:59:19 +08:00
|
|
|
if (isConstantRange()) {
|
|
|
|
if (NewR.isEmptySet())
|
2016-12-07 09:03:56 +08:00
|
|
|
markOverdefined();
|
|
|
|
else {
|
|
|
|
Range = std::move(NewR);
|
|
|
|
}
|
|
|
|
return;
|
2010-08-06 06:59:19 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2010-08-06 06:59:19 +08:00
|
|
|
assert(isUndefined());
|
|
|
|
if (NewR.isEmptySet())
|
2016-12-07 09:03:56 +08:00
|
|
|
markOverdefined();
|
|
|
|
else {
|
|
|
|
Tag = constantrange;
|
|
|
|
Range = std::move(NewR);
|
|
|
|
}
|
2010-08-06 06:59:19 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2016-12-06 10:54:16 +08:00
|
|
|
public:
|
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// Merge the specified lattice value into this one, updating this
|
2009-11-12 06:48:44 +08:00
|
|
|
/// one and returning true if anything changed.
|
2016-12-07 08:54:21 +08:00
|
|
|
void mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
|
|
|
|
if (RHS.isUndefined() || isOverdefined())
|
|
|
|
return;
|
|
|
|
if (RHS.isOverdefined()) {
|
|
|
|
markOverdefined();
|
|
|
|
return;
|
|
|
|
}
|
2009-11-12 06:48:44 +08:00
|
|
|
|
2010-12-16 02:57:18 +08:00
|
|
|
if (isUndefined()) {
|
2016-12-07 08:54:21 +08:00
|
|
|
*this = RHS;
|
|
|
|
return;
|
2010-12-16 02:57:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isConstant()) {
|
2016-12-07 08:54:21 +08:00
|
|
|
if (RHS.isConstant() && Val == RHS.Val)
|
|
|
|
return;
|
|
|
|
markOverdefined();
|
|
|
|
return;
|
2010-09-17 02:28:33 +08:00
|
|
|
}
|
2010-12-16 02:57:18 +08:00
|
|
|
|
|
|
|
if (isNotConstant()) {
|
2016-12-07 08:54:21 +08:00
|
|
|
if (RHS.isNotConstant() && Val == RHS.Val)
|
|
|
|
return;
|
|
|
|
markOverdefined();
|
|
|
|
return;
|
2009-11-12 12:57:13 +08:00
|
|
|
}
|
|
|
|
|
2010-12-16 02:57:18 +08:00
|
|
|
assert(isConstantRange() && "New LVILattice type?");
|
2016-12-07 12:48:50 +08:00
|
|
|
if (!RHS.isConstantRange()) {
|
|
|
|
// We can get here if we've encountered a constantexpr of integer type
|
|
|
|
// and merge it with a constantrange.
|
|
|
|
markOverdefined();
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 02:57:18 +08:00
|
|
|
ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
|
|
|
|
if (NewR.isFullSet())
|
2016-12-07 08:54:21 +08:00
|
|
|
markOverdefined();
|
|
|
|
else
|
|
|
|
markConstantRange(NewR);
|
2009-11-11 10:08:33 +08:00
|
|
|
}
|
|
|
|
};
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-11 10:08:33 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2009-11-12 06:48:44 +08:00
|
|
|
namespace llvm {
|
2011-04-19 02:49:44 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
|
|
|
|
LLVM_ATTRIBUTE_USED;
|
2009-11-12 06:48:44 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
|
|
|
|
if (Val.isUndefined())
|
|
|
|
return OS << "undefined";
|
|
|
|
if (Val.isOverdefined())
|
|
|
|
return OS << "overdefined";
|
2009-11-12 12:36:58 +08:00
|
|
|
|
|
|
|
if (Val.isNotConstant())
|
|
|
|
return OS << "notconstant<" << *Val.getNotConstant() << '>';
|
2016-05-26 06:29:34 +08:00
|
|
|
if (Val.isConstantRange())
|
2010-08-10 04:50:46 +08:00
|
|
|
return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
|
|
|
|
<< Val.getConstantRange().getUpper() << '>';
|
2009-11-12 06:48:44 +08:00
|
|
|
return OS << "constant<" << *Val.getConstant() << '>';
|
|
|
|
}
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
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.
|
2016-06-09 03:09:22 +08:00
|
|
|
static bool hasSingleValue(const LVILatticeVal &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.
|
|
|
|
static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) {
|
|
|
|
// Undefined is the strongest state. It means the value is known to be along
|
|
|
|
// an unreachable path.
|
|
|
|
if (A.isUndefined())
|
|
|
|
return A;
|
|
|
|
if (B.isUndefined())
|
|
|
|
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 =
|
|
|
|
A.getConstantRange().intersectWith(B.getConstantRange());
|
|
|
|
// 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.
|
2016-02-20 18:40:34 +08:00
|
|
|
return LVILatticeVal::getRange(std::move(Range));
|
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 {
|
2016-07-28 06:33:36 +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
|
|
|
|
2011-01-06 05:15:29 +08:00
|
|
|
LVIValueHandle(Value *V, LazyValueInfoCache *P)
|
|
|
|
: 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 {
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is all of the cached block information for exactly one Value*.
|
|
|
|
/// The entries are sorted by the BasicBlock* of the
|
2009-11-16 03:59:49 +08:00
|
|
|
/// entries, allowing us to do a lookup with a binary search.
|
2015-12-11 08:49:47 +08:00
|
|
|
/// Over-defined lattice values are recorded in OverDefinedCache to reduce
|
|
|
|
/// memory overhead.
|
2016-07-28 06:33:36 +08:00
|
|
|
struct ValueCacheEntryTy {
|
|
|
|
ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {}
|
|
|
|
LVIValueHandle Handle;
|
2017-01-24 20:55:57 +08:00
|
|
|
SmallDenseMap<PoisoningVH<BasicBlock>, LVILatticeVal, 4> BlockVals;
|
2016-07-28 06:33:36 +08:00
|
|
|
};
|
2009-11-11 08:22:30 +08:00
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This is all of the cached information for all values,
|
2011-01-06 07:26:22 +08:00
|
|
|
/// mapped from Value* to key information.
|
2016-07-28 06:33:36 +08:00
|
|
|
DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// This tracks, on a per-block basis, the set of values that are
|
2015-12-11 08:49:47 +08:00
|
|
|
/// over-defined at the end of that block.
|
2017-01-24 20:55:57 +08:00
|
|
|
typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
|
2015-08-19 00:34:27 +08:00
|
|
|
OverDefinedCacheTy;
|
|
|
|
OverDefinedCacheTy OverDefinedCache;
|
2011-12-03 23:16:45 +08:00
|
|
|
|
2015-01-10 00:47:20 +08:00
|
|
|
/// Keep track of all blocks that we have ever seen, so we
|
2011-12-03 23:16:45 +08:00
|
|
|
/// don't spend time removing unused blocks from our caches.
|
2017-01-24 20:55:57 +08:00
|
|
|
DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
|
2011-12-03 23:16:45 +08:00
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
public:
|
2014-11-26 01:23:05 +08:00
|
|
|
void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
|
|
|
|
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())
|
2015-08-19 00:34:27 +08:00
|
|
|
OverDefinedCache[BB].insert(Val);
|
2016-07-28 06:33:36 +08:00
|
|
|
else {
|
|
|
|
auto It = ValueCache.find_as(Val);
|
|
|
|
if (It == ValueCache.end()) {
|
|
|
|
ValueCache[Val] = 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;
|
|
|
|
}
|
2014-11-26 01:23:05 +08:00
|
|
|
}
|
2010-07-31 07:59:40 +08:00
|
|
|
|
2015-12-11 08:49:47 +08:00
|
|
|
bool isOverdefined(Value *V, BasicBlock *BB) const {
|
|
|
|
auto ODI = OverDefinedCache.find(BB);
|
|
|
|
|
|
|
|
if (ODI == OverDefinedCache.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ODI->second.count(V);
|
|
|
|
}
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
bool hasCachedValueInfo(Value *V, BasicBlock *BB) const {
|
2015-12-11 08:49:47 +08:00
|
|
|
if (isOverdefined(V, BB))
|
|
|
|
return true;
|
|
|
|
|
2016-07-28 06:33:36 +08:00
|
|
|
auto I = ValueCache.find_as(V);
|
2015-12-11 08:49:47 +08:00
|
|
|
if (I == ValueCache.end())
|
|
|
|
return false;
|
|
|
|
|
2016-07-28 06:33:36 +08:00
|
|
|
return I->second->BlockVals.count(BB);
|
2015-12-11 08:49:47 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) const {
|
2015-12-11 08:49:47 +08:00
|
|
|
if (isOverdefined(V, BB))
|
|
|
|
return LVILatticeVal::getOverdefined();
|
|
|
|
|
2016-07-28 06:33:36 +08:00
|
|
|
auto I = ValueCache.find_as(V);
|
|
|
|
if (I == ValueCache.end())
|
|
|
|
return LVILatticeVal();
|
|
|
|
auto BBI = I->second->BlockVals.find(BB);
|
|
|
|
if (BBI == I->second->BlockVals.end())
|
|
|
|
return LVILatticeVal();
|
|
|
|
return BBI->second;
|
2015-12-11 08:49:47 +08:00
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
/// clear - Empty the cache.
|
|
|
|
void clear() {
|
|
|
|
SeenBlocks.clear();
|
|
|
|
ValueCache.clear();
|
|
|
|
OverDefinedCache.clear();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
friend struct LVIValueHandle;
|
|
|
|
};
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
2016-09-13 05:46:58 +08:00
|
|
|
|
2016-09-13 06:03:36 +08:00
|
|
|
void LazyValueInfoCache::eraseValue(Value *V) {
|
2017-01-26 16:31:54 +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;
|
2016-12-31 06:09:10 +08:00
|
|
|
ValueSet.erase(V);
|
2016-09-13 06:03:36 +08:00
|
|
|
if (ValueSet.empty())
|
2017-01-26 16:31:54 +08:00
|
|
|
OverDefinedCache.erase(Iter);
|
2016-09-13 06:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ValueCache.erase(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// Shortcut if we have never seen this block.
|
2017-01-24 20:55:57 +08:00
|
|
|
DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
|
2016-09-13 06:03:36 +08:00
|
|
|
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: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);
|
|
|
|
|
|
|
|
auto I = OverDefinedCache.find(OldSucc);
|
|
|
|
if (I == OverDefinedCache.end())
|
|
|
|
return; // Nothing to process here.
|
|
|
|
SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end());
|
|
|
|
|
|
|
|
// 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...
|
|
|
|
auto OI = OverDefinedCache.find(ToUpdate);
|
|
|
|
if (OI == OverDefinedCache.end())
|
|
|
|
continue;
|
|
|
|
SmallPtrSetImpl<Value *> &ValueSet = OI->second;
|
|
|
|
|
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;
|
2016-12-31 01:56:47 +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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
|
|
|
|
bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
|
|
|
|
LVILatticeVal &Result, Instruction *CxtI = nullptr);
|
|
|
|
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);
|
2016-12-06 11:22:03 +08:00
|
|
|
bool solveBlockValueImpl(LVILatticeVal &Res, Value *Val, BasicBlock *BB);
|
2016-09-13 05:46:58 +08:00
|
|
|
bool solveBlockValueNonLocal(LVILatticeVal &BBLV, Value *Val, BasicBlock *BB);
|
|
|
|
bool solveBlockValuePHINode(LVILatticeVal &BBLV, PHINode *PN, BasicBlock *BB);
|
|
|
|
bool solveBlockValueSelect(LVILatticeVal &BBLV, SelectInst *S,
|
|
|
|
BasicBlock *BB);
|
|
|
|
bool solveBlockValueBinaryOp(LVILatticeVal &BBLV, Instruction *BBI,
|
|
|
|
BasicBlock *BB);
|
|
|
|
bool solveBlockValueCast(LVILatticeVal &BBLV, Instruction *BBI,
|
|
|
|
BasicBlock *BB);
|
|
|
|
void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
|
|
|
|
LVILatticeVal &BBLV,
|
|
|
|
Instruction *BBI);
|
|
|
|
|
|
|
|
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.
|
2014-09-08 04:29:59 +08:00
|
|
|
LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB,
|
|
|
|
Instruction *CxtI = nullptr);
|
|
|
|
|
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).
|
|
|
|
LVILatticeVal 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.
|
2014-09-08 04:29:59 +08:00
|
|
|
LVILatticeVal 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is part of the update interface to inform the cache
|
|
|
|
/// that a block has been deleted.
|
|
|
|
void eraseBlock(BasicBlock *BB) {
|
|
|
|
TheCache.eraseBlock(BB);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
: AC(AC), DL(DL), DT(DT) {}
|
2009-11-16 03:59:49 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-11-12 06:48:44 +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) {
|
|
|
|
DEBUG(dbgs() << "Giving up on stack because we are getting too deep\n");
|
|
|
|
// Fill in the original values
|
|
|
|
while (!StartingStack.empty()) {
|
|
|
|
std::pair<BasicBlock *, Value *> &e = StartingStack.back();
|
|
|
|
TheCache.insertResult(e.second, e.first,
|
|
|
|
LVILatticeVal::getOverdefined());
|
|
|
|
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
|
|
|
|
2016-02-02 11:15:40 +08:00
|
|
|
DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName()
|
2016-09-13 06:38:44 +08:00
|
|
|
<< " = " << 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
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
LVILatticeVal 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))
|
|
|
|
return LVILatticeVal::get(VC);
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
return TheCache.getCachedValueInfo(Val, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
[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
|
|
|
static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
|
|
|
|
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())) {
|
2016-02-20 18:40:34 +08:00
|
|
|
return LVILatticeVal::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
|
|
|
|
return LVILatticeVal::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) {
|
2010-12-18 09:00:40 +08:00
|
|
|
if (isa<Constant>(Val))
|
|
|
|
return true;
|
|
|
|
|
2016-09-13 06:38:44 +08:00
|
|
|
if (TheCache.hasCachedValueInfo(Val, BB)) {
|
2014-11-26 01:23:05 +08:00
|
|
|
// If we have a cached value, use that.
|
|
|
|
DEBUG(dbgs() << " reuse BB '" << BB->getName()
|
2016-09-13 06:38:44 +08:00
|
|
|
<< "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n');
|
2014-11-26 01:23:05 +08:00
|
|
|
|
|
|
|
// Since we're reusing a cached value, we don't need to update the
|
|
|
|
// OverDefinedCache. The cache will have been properly updated whenever the
|
|
|
|
// cached value was inserted.
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
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.
|
|
|
|
LVILatticeVal 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LazyValueInfoImpl::solveBlockValueImpl(LVILatticeVal &Res,
|
|
|
|
Value *Val, BasicBlock *BB) {
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
Instruction *BBI = dyn_cast<Instruction>(Val);
|
2016-12-06 11:22:03 +08:00
|
|
|
if (!BBI || BBI->getParent() != BB)
|
|
|
|
return solveBlockValueNonLocal(Res, Val, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
|
2016-12-06 11:22:03 +08:00
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(BBI))
|
|
|
|
return solveBlockValuePHINode(Res, PN, BB);
|
2009-11-16 04:00:52 +08:00
|
|
|
|
2016-12-06 11:22:03 +08:00
|
|
|
if (auto *SI = dyn_cast<SelectInst>(BBI))
|
|
|
|
return solveBlockValueSelect(Res, SI, BB);
|
2016-02-02 06:57:53 +08:00
|
|
|
|
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
|
|
|
|
// compile time wise. The context-insensative value walk done inside
|
|
|
|
// isKnownNonNull gets most of the profitable cases at much less expense.
|
|
|
|
// This does mean that we have a sensativity to where the defining
|
|
|
|
// instruction is placed, even if it could legally be hoisted much higher.
|
|
|
|
// That is unfortunate.
|
2015-09-18 21:01:48 +08:00
|
|
|
PointerType *PT = dyn_cast<PointerType>(BBI->getType());
|
|
|
|
if (PT && isKnownNonNull(BBI)) {
|
|
|
|
Res = LVILatticeVal::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()) {
|
2016-12-06 11:22:03 +08:00
|
|
|
if (isa<CastInst>(BBI))
|
|
|
|
return solveBlockValueCast(Res, BBI, BB);
|
|
|
|
|
2016-04-27 09:02:25 +08:00
|
|
|
BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
|
2016-12-06 11:22:03 +08:00
|
|
|
if (BO && isa<ConstantInt>(BO->getOperand(1)))
|
|
|
|
return solveBlockValueBinaryOp(Res, BBI, BB);
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
2016-03-05 06:27:39 +08:00
|
|
|
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - unknown inst def found.\n");
|
|
|
|
Res = getFromRangeMetadata(BBI);
|
2014-11-26 01:23:05 +08:00
|
|
|
return true;
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
|
|
|
|
return L->getPointerAddressSpace() == 0 &&
|
2015-03-10 10:37:25 +08:00
|
|
|
GetUnderlyingObject(L->getPointerOperand(),
|
|
|
|
L->getModule()->getDataLayout()) == Ptr;
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
|
|
|
if (StoreInst *S = dyn_cast<StoreInst>(I)) {
|
|
|
|
return S->getPointerAddressSpace() == 0 &&
|
2015-03-10 10:37:25 +08:00
|
|
|
GetUnderlyingObject(S->getPointerOperand(),
|
|
|
|
S->getModule()->getDataLayout()) == Ptr;
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
2011-01-15 17:16:12 +08:00
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
|
|
|
|
if (MI->isVolatile()) return false;
|
|
|
|
|
|
|
|
// FIXME: check whether it has a valuerange that excludes zero?
|
|
|
|
ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
|
|
|
|
if (!Len || Len->isZero()) return false;
|
|
|
|
|
2011-06-01 04:40:16 +08:00
|
|
|
if (MI->getDestAddressSpace() == 0)
|
2015-03-10 10:37:25 +08:00
|
|
|
if (GetUnderlyingObject(MI->getRawDest(),
|
|
|
|
MI->getModule()->getDataLayout()) == Ptr)
|
2011-06-01 04:40:16 +08:00
|
|
|
return true;
|
2011-01-15 17:16:12 +08:00
|
|
|
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
|
2011-06-01 04:40:16 +08:00
|
|
|
if (MTI->getSourceAddressSpace() == 0)
|
2015-03-10 10:37:25 +08:00
|
|
|
if (GetUnderlyingObject(MTI->getRawSource(),
|
|
|
|
MTI->getModule()->getDataLayout()) == Ptr)
|
2011-06-01 04:40:16 +08:00
|
|
|
return true;
|
2011-01-15 17:16:12 +08:00
|
|
|
}
|
2010-12-18 09:00:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-27 08:30:55 +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());
|
|
|
|
|
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
|
|
|
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))
|
|
|
|
for (Instruction &I : *BB)
|
|
|
|
if (InstructionDereferencesPointer(&I, UnderlyingVal))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueNonLocal(LVILatticeVal &BBLV,
|
2010-12-21 02:18:16 +08:00
|
|
|
Value *Val, BasicBlock *BB) {
|
2010-12-18 09:00:40 +08:00
|
|
|
LVILatticeVal Result; // Start Undefined.
|
|
|
|
|
|
|
|
// 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");
|
2016-04-27 08:30:55 +08:00
|
|
|
// Bofore giving up, see if we can prove the pointer non-null local to
|
|
|
|
// this particular block.
|
|
|
|
if (Val->getType()->isPointerTy() &&
|
|
|
|
(isKnownNonNull(Val) || isObjectDereferencedInBlock(Val, BB))) {
|
2011-07-18 12:54:35 +08:00
|
|
|
PointerType *PTy = cast<PointerType>(Val->getType());
|
2010-12-18 09:00:40 +08:00
|
|
|
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
|
|
|
|
} else {
|
2016-12-06 11:01:08 +08:00
|
|
|
Result = LVILatticeVal::getOverdefined();
|
2010-12-18 09:00:40 +08:00
|
|
|
}
|
2010-12-21 02:18:16 +08:00
|
|
|
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
|
|
|
|
// accident.
|
2014-07-22 01:06:51 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
|
2010-12-18 09:00:40 +08:00
|
|
|
LVILatticeVal 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
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
Result.mergeIn(EdgeResult, DL);
|
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()) {
|
|
|
|
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
2016-02-03 06:43:08 +08:00
|
|
|
<< "' - overdefined because of pred (non local).\n");
|
2016-08-09 17:14:29 +08:00
|
|
|
// Before giving up, see if we can prove the pointer non-null local to
|
2016-04-27 08:30:55 +08:00
|
|
|
// this particular block.
|
|
|
|
if (Val->getType()->isPointerTy() &&
|
|
|
|
isObjectDereferencedInBlock(Val, BB)) {
|
2011-07-18 12:54:35 +08:00
|
|
|
PointerType *PTy = cast<PointerType>(Val->getType());
|
2010-12-18 09:00:40 +08:00
|
|
|
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValuePHINode(LVILatticeVal &BBLV,
|
2010-12-21 02:18:16 +08:00
|
|
|
PHINode *PN, BasicBlock *BB) {
|
2010-12-18 09:00:40 +08:00
|
|
|
LVILatticeVal Result; // Start Undefined.
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
LVILatticeVal 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
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
Result.mergeIn(EdgeResult, DL);
|
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()) {
|
|
|
|
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
2016-02-03 06:43:08 +08:00
|
|
|
<< "' - 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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:38:07 +08:00
|
|
|
static LVILatticeVal 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(
|
2016-08-12 23:52:23 +08:00
|
|
|
Value *Val, LVILatticeVal &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));
|
|
|
|
if (!GuardDecl || GuardDecl->use_empty())
|
|
|
|
return;
|
|
|
|
|
2016-10-21 23:02:21 +08:00
|
|
|
for (Instruction &I : make_range(BBI->getIterator().getReverse(),
|
|
|
|
BBI->getParent()->rend())) {
|
2016-08-12 23:52:23 +08:00
|
|
|
Value *Cond = nullptr;
|
2016-10-21 23:02:21 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueSelect(LVILatticeVal &BBLV,
|
2016-02-02 06:57:53 +08:00
|
|
|
SelectInst *SI, BasicBlock *BB) {
|
|
|
|
|
|
|
|
// Recurse on our inputs if needed
|
|
|
|
if (!hasBlockValue(SI->getTrueValue(), BB)) {
|
|
|
|
if (pushBlockValue(std::make_pair(BB, SI->getTrueValue())))
|
|
|
|
return false;
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB);
|
|
|
|
// 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()) {
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::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;
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB);
|
|
|
|
// 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()) {
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::getOverdefined();
|
2016-02-02 06:57:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-27 06:53:59 +08:00
|
|
|
if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) {
|
|
|
|
ConstantRange TrueCR = TrueVal.getConstantRange();
|
|
|
|
ConstantRange FalseCR = FalseVal.getConstantRange();
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
BBLV = LVILatticeVal::getRange(ResultCR);
|
|
|
|
return true;
|
2016-02-27 06:53:59 +08:00
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-02-27 06:53:59 +08:00
|
|
|
// TODO: ABS, NABS from the SelectPatternResult
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
LVILatticeVal::getNot(ResNot));
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
LVILatticeVal::getNot(ResNot));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-02-02 06:57:53 +08:00
|
|
|
LVILatticeVal Result; // Start Undefined.
|
|
|
|
Result.mergeIn(TrueVal, DL);
|
|
|
|
Result.mergeIn(FalseVal, DL);
|
|
|
|
BBLV = Result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueCast(LVILatticeVal &BBLV,
|
2016-04-26 02:30:31 +08:00
|
|
|
Instruction *BBI,
|
2016-04-27 06:52:30 +08:00
|
|
|
BasicBlock *BB) {
|
|
|
|
if (!BBI->getOperand(0)->getType()->isSized()) {
|
|
|
|
// Without knowing how wide the input is, we can't analyze it in any useful
|
|
|
|
// way.
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::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.
|
|
|
|
switch (BBI->getOpcode()) {
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::BitCast:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled instructions are overdefined.
|
|
|
|
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown cast).\n");
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::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.
|
|
|
|
if (!hasBlockValue(BBI->getOperand(0), BB))
|
2014-11-26 01:23:05 +08:00
|
|
|
if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
|
2016-04-27 05:48:16 +08:00
|
|
|
// More work to do before applying this transfer rule.
|
2014-11-26 01:23:05 +08:00
|
|
|
return false;
|
2010-12-18 09:00:40 +08:00
|
|
|
|
2016-04-27 05:48:16 +08:00
|
|
|
const unsigned OperandBitWidth =
|
2016-04-27 06:52:30 +08:00
|
|
|
DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
|
2016-04-27 05:48:16 +08:00
|
|
|
ConstantRange LHSRange = ConstantRange(OperandBitWidth);
|
|
|
|
if (hasBlockValue(BBI->getOperand(0), BB)) {
|
|
|
|
LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
|
|
|
|
BBI);
|
2016-04-27 05:48:16 +08:00
|
|
|
if (LHSVal.isConstantRange())
|
|
|
|
LHSRange = LHSVal.getConstantRange();
|
2010-08-19 05:11:37 +08:00
|
|
|
}
|
2016-04-26 02:30:31 +08:00
|
|
|
|
2016-04-27 05:48:16 +08:00
|
|
|
const unsigned ResultBitWidth =
|
|
|
|
cast<IntegerType>(BBI->getType())->getBitWidth();
|
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.
|
2016-12-02 04:08:47 +08:00
|
|
|
auto CastOp = (Instruction::CastOps) BBI->getOpcode();
|
2016-12-06 10:36:58 +08:00
|
|
|
BBLV = LVILatticeVal::getRange(LHSRange.castOp(CastOp, ResultBitWidth));
|
2016-04-26 02:30:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
bool LazyValueInfoImpl::solveBlockValueBinaryOp(LVILatticeVal &BBLV,
|
2016-04-26 02:30:31 +08:00
|
|
|
Instruction *BBI,
|
2016-04-27 06:52:30 +08:00
|
|
|
BasicBlock *BB) {
|
2016-04-27 07:10:35 +08:00
|
|
|
|
|
|
|
assert(BBI->getOperand(0)->getType()->isSized() &&
|
|
|
|
"all operands to binary operators are sized");
|
2016-04-27 07:27:33 +08:00
|
|
|
|
|
|
|
// Filter out operators we don't know how to reason about before attempting to
|
|
|
|
// recurse on our operand(s). This can cut a long search short if we know
|
|
|
|
// we're not going to be able to get any useful information anways.
|
|
|
|
switch (BBI->getOpcode()) {
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
// continue into the code below
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled instructions are overdefined.
|
|
|
|
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
|
|
|
<< "' - overdefined (unknown binary operator).\n");
|
2016-12-06 11:01:08 +08:00
|
|
|
BBLV = LVILatticeVal::getOverdefined();
|
2016-04-27 07:27:33 +08:00
|
|
|
return true;
|
|
|
|
};
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-04-27 07:10:35 +08:00
|
|
|
// Figure out the range of the LHS. 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"
|
|
|
|
if (!hasBlockValue(BBI->getOperand(0), BB))
|
2016-04-26 02:30:31 +08:00
|
|
|
if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0))))
|
2016-04-27 07:10:35 +08:00
|
|
|
// More work to do before applying this transfer rule.
|
2016-04-26 02:30:31 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-27 07:10:35 +08:00
|
|
|
const unsigned OperandBitWidth =
|
|
|
|
DL.getTypeSizeInBits(BBI->getOperand(0)->getType());
|
|
|
|
ConstantRange LHSRange = ConstantRange(OperandBitWidth);
|
|
|
|
if (hasBlockValue(BBI->getOperand(0), BB)) {
|
|
|
|
LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(BBI->getOperand(0), LHSVal,
|
|
|
|
BBI);
|
2016-04-27 07:10:35 +08:00
|
|
|
if (LHSVal.isConstantRange())
|
|
|
|
LHSRange = LHSVal.getConstantRange();
|
2016-04-26 02:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstantInt *RHS = cast<ConstantInt>(BBI->getOperand(1));
|
|
|
|
ConstantRange RHSRange = ConstantRange(RHS->getValue());
|
|
|
|
|
2010-08-19 05:11:37 +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.
|
2016-12-02 04:08:47 +08:00
|
|
|
auto BinOp = (Instruction::BinaryOps) BBI->getOpcode();
|
2016-12-06 10:36:58 +08:00
|
|
|
BBLV = LVILatticeVal::getRange(LHSRange.binaryOp(BinOp, RHSRange));
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
2009-11-12 06:48:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:13:15 +08:00
|
|
|
static LVILatticeVal 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))
|
2016-08-10 21:38:07 +08:00
|
|
|
return LVILatticeVal::get(cast<Constant>(RHS));
|
2014-09-08 04:29:59 +08:00
|
|
|
else
|
2016-08-10 21:38:07 +08:00
|
|
|
return LVILatticeVal::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())
|
2016-08-10 21:38:07 +08:00
|
|
|
return LVILatticeVal::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());
|
|
|
|
|
2016-08-10 21:38:07 +08:00
|
|
|
return LVILatticeVal::getRange(std::move(TrueValues));
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
2016-07-04 09:26:33 +08:00
|
|
|
|
2016-08-10 21:38:07 +08:00
|
|
|
return LVILatticeVal::getOverdefined();
|
2014-09-08 04:29:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 23:13:15 +08:00
|
|
|
static LVILatticeVal
|
|
|
|
getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
|
|
|
|
DenseMap<Value*, LVILatticeVal> &Visited);
|
|
|
|
|
|
|
|
static LVILatticeVal
|
|
|
|
getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest,
|
|
|
|
DenseMap<Value*, LVILatticeVal> &Visited) {
|
|
|
|
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond))
|
|
|
|
return getValueFromICmpCondition(Val, ICI, isTrueDest);
|
|
|
|
|
|
|
|
// Handle conditions in the form of (cond1 && cond2), we know that on the
|
|
|
|
// true dest path both of the conditions hold.
|
|
|
|
if (!isTrueDest)
|
|
|
|
return LVILatticeVal::getOverdefined();
|
|
|
|
|
|
|
|
BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond);
|
|
|
|
if (!BO || BO->getOpcode() != BinaryOperator::And)
|
|
|
|
return LVILatticeVal::getOverdefined();
|
|
|
|
|
|
|
|
auto RHS = getValueFromCondition(Val, BO->getOperand(0), isTrueDest, Visited);
|
|
|
|
auto LHS = getValueFromCondition(Val, BO->getOperand(1), isTrueDest, Visited);
|
|
|
|
return intersect(RHS, LHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LVILatticeVal
|
|
|
|
getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest,
|
|
|
|
DenseMap<Value*, LVILatticeVal> &Visited) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
LVILatticeVal getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest) {
|
|
|
|
assert(Cond && "precondition");
|
|
|
|
DenseMap<Value*, LVILatticeVal> Visited;
|
|
|
|
return getValueFromCondition(Val, Cond, isTrueDest, Visited);
|
|
|
|
}
|
|
|
|
|
2012-06-28 09:16:18 +08:00
|
|
|
/// \brief 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,
|
|
|
|
BasicBlock *BBTo, LVILatticeVal &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");
|
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.
|
2010-12-18 09:00:40 +08:00
|
|
|
if (BI->getCondition() == Val) {
|
|
|
|
Result = LVILatticeVal::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.
|
2016-08-10 21:38:07 +08:00
|
|
|
Result = getValueFromCondition(Val, BI->getCondition(), isTrueDest);
|
|
|
|
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())) {
|
2012-06-29 00:13:37 +08:00
|
|
|
if (SI->getCondition() != Val)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool DefaultCase = SI->getDefaultDest() == BBTo;
|
|
|
|
unsigned BitWidth = Val->getType()->getIntegerBitWidth();
|
|
|
|
ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
|
|
|
|
|
2014-11-22 03:07:46 +08:00
|
|
|
for (SwitchInst::CaseIt i : SI->cases()) {
|
2012-06-29 00:13:37 +08:00
|
|
|
ConstantRange EdgeVal(i.getCaseValue()->getValue());
|
2012-09-06 07:45:58 +08:00
|
|
|
if (DefaultCase) {
|
|
|
|
// It is possible that the default destination is the destination of
|
|
|
|
// some cases. There is no need to perform difference for those cases.
|
|
|
|
if (i.getCaseSuccessor() != BBTo)
|
|
|
|
EdgesVals = EdgesVals.difference(EdgeVal);
|
|
|
|
} else if (i.getCaseSuccessor() == BBTo)
|
2012-05-19 05:02:10 +08:00
|
|
|
EdgesVals = EdgesVals.unionWith(EdgeVal);
|
2009-11-16 04:02:12 +08:00
|
|
|
}
|
2016-02-20 18:40:34 +08:00
|
|
|
Result = LVILatticeVal::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;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:35:37 +08:00
|
|
|
/// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at
|
|
|
|
/// 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,
|
2014-09-08 04:29:59 +08:00
|
|
|
BasicBlock *BBTo, LVILatticeVal &Result,
|
|
|
|
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)) {
|
|
|
|
Result = LVILatticeVal::get(VC);
|
2010-12-18 09:00:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-28 09:16:18 +08:00
|
|
|
|
2016-02-02 11:15:40 +08:00
|
|
|
LVILatticeVal LocalResult;
|
|
|
|
if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult))
|
|
|
|
// If we couldn't constrain the value on the edge, LocalResult doesn't
|
|
|
|
// provide any information.
|
2016-12-06 11:01:08 +08:00
|
|
|
LocalResult = LVILatticeVal::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.
|
|
|
|
LVILatticeVal 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
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
LVILatticeVal LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
|
2014-09-08 04:29:59 +08:00
|
|
|
Instruction *CxtI) {
|
2009-12-24 04:43:58 +08:00
|
|
|
DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
|
2009-11-16 03:59:49 +08:00
|
|
|
<< 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();
|
|
|
|
}
|
2010-12-09 14:14:58 +08:00
|
|
|
LVILatticeVal Result = getBlockValue(V, BB);
|
2016-08-12 23:52:23 +08:00
|
|
|
intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
|
2014-09-08 04:29:59 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << " Result = " << Result << "\n");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
LVILatticeVal LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) {
|
2014-09-08 04:29:59 +08:00
|
|
|
DEBUG(dbgs() << "LVI Getting value " << *V << " at '"
|
|
|
|
<< CxtI->getName() << "'\n");
|
|
|
|
|
2016-02-11 05:46:32 +08:00
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
return LVILatticeVal::get(C);
|
|
|
|
|
2016-02-03 05:57:37 +08:00
|
|
|
LVILatticeVal Result = LVILatticeVal::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
|
|
|
|
2009-12-24 04:43:58 +08:00
|
|
|
DEBUG(dbgs() << " Result = " << Result << "\n");
|
2009-11-16 03:59:49 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-09-13 05:46:58 +08:00
|
|
|
LVILatticeVal LazyValueInfoImpl::
|
2014-09-08 04:29:59 +08:00
|
|
|
getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI) {
|
2009-12-24 04:43:58 +08:00
|
|
|
DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
|
2009-11-16 03:59:49 +08:00
|
|
|
<< FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2010-12-18 09:00:40 +08:00
|
|
|
LVILatticeVal 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?");
|
|
|
|
}
|
|
|
|
|
2009-12-24 04:43:58 +08:00
|
|
|
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;
|
|
|
|
Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
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(); }
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
return LazyValueInfo(&AC, &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();
|
2014-09-08 04:29:59 +08:00
|
|
|
LVILatticeVal 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()) {
|
2010-08-28 07:29:38 +08:00
|
|
|
ConstantRange CR = Result.getConstantRange();
|
|
|
|
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,
|
2016-07-04 09:26:21 +08:00
|
|
|
Instruction *CxtI) {
|
2016-05-03 03:58:00 +08:00
|
|
|
assert(V->getType()->isIntegerTy());
|
|
|
|
unsigned Width = V->getType()->getIntegerBitWidth();
|
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
|
|
|
LVILatticeVal Result =
|
2016-12-19 16:22:17 +08:00
|
|
|
getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
|
2016-05-03 03:58:00 +08:00
|
|
|
if (Result.isUndefined())
|
|
|
|
return ConstantRange(Width, /*isFullSet=*/false);
|
|
|
|
if (Result.isConstantRange())
|
|
|
|
return Result.getConstantRange();
|
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");
|
2016-05-26 06:29:34 +08:00
|
|
|
return ConstantRange(Width, /*isFullSet=*/true);
|
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();
|
2014-09-08 04:29:59 +08:00
|
|
|
LVILatticeVal 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()) {
|
2010-08-11 04:03:09 +08:00
|
|
|
ConstantRange CR = Result.getConstantRange();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
|
|
|
|
LVILatticeVal &Result,
|
|
|
|
const DataLayout &DL,
|
|
|
|
TargetLibraryInfo *TLI) {
|
2014-09-08 04:29:59 +08:00
|
|
|
|
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;
|
2009-11-12 12:36:58 +08:00
|
|
|
if (Result.isConstant()) {
|
2014-02-18 23:33:12 +08:00
|
|
|
Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL,
|
2011-12-02 09:26:24 +08:00
|
|
|
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
|
|
|
|
2010-08-11 04:03:09 +08:00
|
|
|
if (Result.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
|
|
|
|
2010-08-11 04:03:09 +08:00
|
|
|
ConstantRange CR = Result.getConstantRange();
|
|
|
|
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
|
|
|
|
2010-08-11 04:03:09 +08:00
|
|
|
if (CR.isSingleElement() && CR.contains(CI->getValue()))
|
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
|
|
|
|
2010-08-11 04:03:09 +08:00
|
|
|
if (CR.isSingleElement() && CR.contains(CI->getValue()))
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::False;
|
2010-08-11 04:03:09 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2010-08-11 04:03:09 +08:00
|
|
|
// Handle more complex predicates.
|
2016-10-02 08:09:57 +08:00
|
|
|
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(
|
|
|
|
(ICmpInst::Predicate)Pred, CI->getValue());
|
2010-12-16 02:57:18 +08:00
|
|
|
if (TrueValues.contains(CR))
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::True;
|
2010-12-16 02:57:18 +08:00
|
|
|
if (TrueValues.inverse().contains(CR))
|
2014-09-08 04:29:59 +08:00
|
|
|
return LazyValueInfo::False;
|
|
|
|
return LazyValueInfo::Unknown;
|
2010-08-11 04:03:09 +08:00
|
|
|
}
|
2015-07-28 23:53:21 +08:00
|
|
|
|
2009-11-16 03:59:49 +08:00
|
|
|
if (Result.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,
|
2014-02-18 23:33:12 +08:00
|
|
|
Result.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,
|
2014-02-18 23:33:12 +08:00
|
|
|
Result.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();
|
2014-09-08 04:29:59 +08:00
|
|
|
LVILatticeVal 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
|
|
|
|
// isKnownNonNull can tell us the result of the predicate, we can
|
|
|
|
// return it quickly. But this is only a fastpath, and falling
|
|
|
|
// through would still be correct.
|
|
|
|
if (V->getType()->isPointerTy() && C->isNullValue() &&
|
|
|
|
isKnownNonNull(V->stripPointerCasts())) {
|
|
|
|
if (Pred == ICmpInst::ICMP_EQ)
|
|
|
|
return LazyValueInfo::False;
|
|
|
|
else if (Pred == ICmpInst::ICMP_NE)
|
|
|
|
return LazyValueInfo::True;
|
|
|
|
}
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = CxtI->getModule()->getDataLayout();
|
2016-12-19 16:22:17 +08:00
|
|
|
LVILatticeVal 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
|
|
|
}
|