forked from OSchip/llvm-project
parent
9e32199861
commit
51fd242cfc
|
@ -25,7 +25,7 @@ namespace llvm {
|
|||
class Instruction;
|
||||
class TargetLibraryInfo;
|
||||
class Value;
|
||||
|
||||
|
||||
/// This pass computes, caches, and vends lazy value constraint information.
|
||||
class LazyValueInfo : public FunctionPass {
|
||||
AssumptionCache *AC;
|
||||
|
@ -45,23 +45,22 @@ public:
|
|||
enum Tristate {
|
||||
Unknown = -1, False = 0, True = 1
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Public query interface.
|
||||
|
||||
|
||||
/// 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.
|
||||
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
|
||||
BasicBlock *FromBB, BasicBlock *ToBB,
|
||||
Instruction *CxtI = nullptr);
|
||||
|
||||
|
||||
/// Determine whether the specified value comparison with a constant is known
|
||||
/// to be true or false at the specified instruction
|
||||
/// (from an assume intrinsic). Pred is a CmpInst predicate.
|
||||
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C,
|
||||
Instruction *CxtI);
|
||||
|
||||
|
||||
/// Determine whether the specified value is known to be a
|
||||
/// constant at the end of the specified block. Return null if not.
|
||||
Constant *getConstant(Value *V, BasicBlock *BB, Instruction *CxtI = nullptr);
|
||||
|
@ -70,14 +69,14 @@ public:
|
|||
/// constant on the specified edge. Return null if not.
|
||||
Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
||||
Instruction *CxtI = nullptr);
|
||||
|
||||
|
||||
/// Inform the analysis cache that we have threaded an edge from
|
||||
/// PredBB to OldSucc to be from PredBB to NewSucc instead.
|
||||
void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
|
||||
|
||||
|
||||
/// Inform the analysis cache that we have erased a block.
|
||||
void eraseBlock(BasicBlock *BB);
|
||||
|
||||
|
||||
// Implementation boilerplate.
|
||||
|
||||
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
||||
|
|
|
@ -64,10 +64,10 @@ class LVILatticeVal {
|
|||
enum LatticeValueTy {
|
||||
/// This Value has no known value yet.
|
||||
undefined,
|
||||
|
||||
|
||||
/// This Value has a specific constant value.
|
||||
constant,
|
||||
|
||||
|
||||
/// This Value is known to not have the specified value.
|
||||
notconstant,
|
||||
|
||||
|
@ -77,13 +77,13 @@ class LVILatticeVal {
|
|||
/// This value is not known to be constant, and we know that it has a value.
|
||||
overdefined
|
||||
};
|
||||
|
||||
|
||||
/// Val: This stores the current lattice value along with the Constant* for
|
||||
/// the constant if this is a 'constant' or 'notconstant' value.
|
||||
LatticeValueTy Tag;
|
||||
Constant *Val;
|
||||
ConstantRange Range;
|
||||
|
||||
|
||||
public:
|
||||
LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
|
||||
|
||||
|
@ -104,29 +104,29 @@ public:
|
|||
Res.markConstantRange(CR);
|
||||
return Res;
|
||||
}
|
||||
|
||||
|
||||
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; }
|
||||
|
||||
|
||||
Constant *getConstant() const {
|
||||
assert(isConstant() && "Cannot get the constant of a non-constant!");
|
||||
return Val;
|
||||
}
|
||||
|
||||
|
||||
Constant *getNotConstant() const {
|
||||
assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
|
||||
return Val;
|
||||
}
|
||||
|
||||
|
||||
ConstantRange getConstantRange() const {
|
||||
assert(isConstantRange() &&
|
||||
"Cannot get the constant-range of a non-constant-range!");
|
||||
return Range;
|
||||
}
|
||||
|
||||
|
||||
/// Return true if this is a change in status.
|
||||
bool markOverdefined() {
|
||||
if (isOverdefined())
|
||||
|
@ -150,7 +150,7 @@ public:
|
|||
Val = V;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// Return true if this is a change in status.
|
||||
bool markNotConstant(Constant *V) {
|
||||
assert(V && "Marking constant with NULL");
|
||||
|
@ -168,27 +168,27 @@ public:
|
|||
Val = V;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// Return true if this is a change in status.
|
||||
bool markConstantRange(const ConstantRange NewR) {
|
||||
if (isConstantRange()) {
|
||||
if (NewR.isEmptySet())
|
||||
return markOverdefined();
|
||||
|
||||
|
||||
bool changed = Range != NewR;
|
||||
Range = NewR;
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
assert(isUndefined());
|
||||
if (NewR.isEmptySet())
|
||||
return markOverdefined();
|
||||
|
||||
|
||||
Tag = constantrange;
|
||||
Range = NewR;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// Merge the specified lattice value into this one, updating this
|
||||
/// one and returning true if anything changed.
|
||||
bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
|
||||
|
@ -267,7 +267,7 @@ public:
|
|||
return markConstantRange(NewR);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // end anonymous namespace.
|
||||
|
||||
namespace llvm {
|
||||
|
@ -297,7 +297,7 @@ namespace {
|
|||
class LazyValueInfoCache;
|
||||
struct LVIValueHandle : public CallbackVH {
|
||||
LazyValueInfoCache *Parent;
|
||||
|
||||
|
||||
LVIValueHandle(Value *V, LazyValueInfoCache *P)
|
||||
: CallbackVH(V), Parent(P) { }
|
||||
|
||||
|
@ -308,7 +308,7 @@ namespace {
|
|||
};
|
||||
}
|
||||
|
||||
namespace {
|
||||
namespace {
|
||||
/// This is the cache kept by LazyValueInfo which
|
||||
/// maintains information about queries across the clients' queries.
|
||||
class LazyValueInfoCache {
|
||||
|
@ -320,7 +320,7 @@ namespace {
|
|||
/// This is all of the cached information for all values,
|
||||
/// mapped from Value* to key information.
|
||||
std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
|
||||
|
||||
|
||||
/// This tracks, on a per-block basis, the set of values that are
|
||||
/// over-defined at the end of that block. This is required
|
||||
/// for cache updating.
|
||||
|
@ -382,7 +382,7 @@ namespace {
|
|||
Instruction *BBI);
|
||||
|
||||
void solve();
|
||||
|
||||
|
||||
ValueCacheEntryTy &lookup(Value *V) {
|
||||
return ValueCache[LVIValueHandle(V, this)];
|
||||
}
|
||||
|
@ -402,15 +402,15 @@ namespace {
|
|||
/// value for the specified Value* that is true on the specified edge.
|
||||
LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
|
||||
Instruction *CxtI = nullptr);
|
||||
|
||||
|
||||
/// 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.
|
||||
void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
|
||||
|
||||
|
||||
/// This is part of the update interface to inform the cache
|
||||
/// that a block has been deleted.
|
||||
void eraseBlock(BasicBlock *BB);
|
||||
|
||||
|
||||
/// clear - Empty the cache.
|
||||
void clear() {
|
||||
SeenBlocks.clear();
|
||||
|
@ -426,14 +426,14 @@ namespace {
|
|||
|
||||
void LVIValueHandle::deleted() {
|
||||
typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
|
||||
|
||||
|
||||
SmallVector<OverDefinedPairTy, 4> ToErase;
|
||||
for (const OverDefinedPairTy &P : Parent->OverDefinedCache)
|
||||
if (P.second == getValPtr())
|
||||
ToErase.push_back(P);
|
||||
for (const OverDefinedPairTy &P : ToErase)
|
||||
Parent->OverDefinedCache.erase(P);
|
||||
|
||||
|
||||
// This erasure deallocates *this, so it MUST happen after we're done
|
||||
// using any and all members of *this.
|
||||
Parent->ValueCache.erase(*this);
|
||||
|
@ -516,7 +516,7 @@ bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
|
|||
// Hold off inserting this value into the Cache in case we have to return
|
||||
// false and come back later.
|
||||
LVILatticeVal Res;
|
||||
|
||||
|
||||
Instruction *BBI = dyn_cast<Instruction>(Val);
|
||||
if (!BBI || BBI->getParent() != BB) {
|
||||
if (!solveBlockValueNonLocal(Res, Val, BB))
|
||||
|
@ -661,7 +661,7 @@ bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
|
|||
PointerType *PTy = cast<PointerType>(Val->getType());
|
||||
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
|
||||
}
|
||||
|
||||
|
||||
BBLV = Result;
|
||||
return true;
|
||||
}
|
||||
|
@ -674,7 +674,7 @@ bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
|
|||
BBLV = Result;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
|
||||
PHINode *PN, BasicBlock *BB) {
|
||||
LVILatticeVal Result; // Start Undefined.
|
||||
|
@ -700,7 +700,7 @@ bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
|
|||
if (Result.isOverdefined()) {
|
||||
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
||||
<< "' - overdefined because of pred.\n");
|
||||
|
||||
|
||||
BBLV = Result;
|
||||
return true;
|
||||
}
|
||||
|
@ -765,7 +765,7 @@ bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
|
|||
BBLV.markOverdefined();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
ConstantRange LHSRange = LHSVal.getConstantRange();
|
||||
ConstantRange RHSRange(1);
|
||||
IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
|
||||
|
@ -819,7 +819,7 @@ bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
|
|||
case Instruction::Or:
|
||||
Result.markConstantRange(LHSRange.binaryOr(RHSRange));
|
||||
break;
|
||||
|
||||
|
||||
// Unhandled instructions are overdefined.
|
||||
default:
|
||||
DEBUG(dbgs() << " compute BB '" << BB->getName()
|
||||
|
@ -827,7 +827,7 @@ bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
|
|||
Result.markOverdefined();
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
BBLV = Result;
|
||||
return true;
|
||||
}
|
||||
|
@ -877,7 +877,7 @@ bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
|
|||
/// Val is not constrained on the edge.
|
||||
static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
|
||||
BasicBlock *BBTo, LVILatticeVal &Result) {
|
||||
// TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
|
||||
// TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
|
||||
// know that v != 0.
|
||||
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
|
||||
// If this is a conditional branch and only one successor goes to BBTo, then
|
||||
|
@ -887,7 +887,7 @@ static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
|
|||
bool isTrueDest = BI->getSuccessor(0) == BBTo;
|
||||
assert(BI->getSuccessor(!isTrueDest) == BBTo &&
|
||||
"BBTo isn't a successor of BBFrom");
|
||||
|
||||
|
||||
// If V is the condition of the branch itself, then we know exactly what
|
||||
// it is.
|
||||
if (BI->getCondition() == Val) {
|
||||
|
@ -895,7 +895,7 @@ static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
|
|||
Type::getInt1Ty(Val->getContext()), isTrueDest));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// If the condition of the branch is an equality comparison, we may be
|
||||
// able to infer the value.
|
||||
if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
|
||||
|
@ -997,7 +997,7 @@ LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB,
|
|||
Instruction *CxtI) {
|
||||
DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
|
||||
<< BB->getName() << "'\n");
|
||||
|
||||
|
||||
assert(BlockValueStack.empty() && BlockValueSet.empty());
|
||||
pushBlockValue(std::make_pair(BB, V));
|
||||
|
||||
|
@ -1025,7 +1025,7 @@ getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
|||
Instruction *CxtI) {
|
||||
DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
|
||||
<< FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
|
||||
|
||||
|
||||
LVILatticeVal Result;
|
||||
if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
|
||||
solve();
|
||||
|
@ -1040,24 +1040,24 @@ getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
|||
|
||||
void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, 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.
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
DenseSet<Value*> ClearSet;
|
||||
for (OverDefinedPairTy &P : OverDefinedCache)
|
||||
if (P.first == OldSucc)
|
||||
ClearSet.insert(P.second);
|
||||
|
||||
|
||||
// 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
|
||||
|
@ -1065,10 +1065,10 @@ void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
|
|||
while (!worklist.empty()) {
|
||||
BasicBlock *ToUpdate = worklist.back();
|
||||
worklist.pop_back();
|
||||
|
||||
|
||||
// Skip blocks only accessible through NewSucc.
|
||||
if (ToUpdate == NewSucc) continue;
|
||||
|
||||
|
||||
bool changed = false;
|
||||
for (Value *V : ClearSet) {
|
||||
// If a value was marked overdefined in OldSucc, and is here too...
|
||||
|
@ -1084,13 +1084,13 @@ void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
|
|||
Entry.erase(CI);
|
||||
OverDefinedCache.erase(OI);
|
||||
|
||||
// If we removed anything, then we potentially need to update
|
||||
// If we removed anything, then we potentially need to update
|
||||
// blocks successors too.
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!changed) continue;
|
||||
|
||||
|
||||
worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
|
||||
}
|
||||
}
|
||||
|
@ -1158,7 +1158,7 @@ Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB,
|
|||
}
|
||||
|
||||
/// Determine whether the specified value is known to be a
|
||||
/// constant on the specified edge. Return null if not.
|
||||
/// constant on the specified edge. Return null if not.
|
||||
Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
|
||||
BasicBlock *ToBB,
|
||||
Instruction *CxtI) {
|
||||
|
@ -1190,26 +1190,26 @@ static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
|
|||
return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
|
||||
return LazyValueInfo::Unknown;
|
||||
}
|
||||
|
||||
|
||||
if (Result.isConstantRange()) {
|
||||
ConstantInt *CI = dyn_cast<ConstantInt>(C);
|
||||
if (!CI) return LazyValueInfo::Unknown;
|
||||
|
||||
|
||||
ConstantRange CR = Result.getConstantRange();
|
||||
if (Pred == ICmpInst::ICMP_EQ) {
|
||||
if (!CR.contains(CI->getValue()))
|
||||
return LazyValueInfo::False;
|
||||
|
||||
|
||||
if (CR.isSingleElement() && CR.contains(CI->getValue()))
|
||||
return LazyValueInfo::True;
|
||||
} else if (Pred == ICmpInst::ICMP_NE) {
|
||||
if (!CR.contains(CI->getValue()))
|
||||
return LazyValueInfo::True;
|
||||
|
||||
|
||||
if (CR.isSingleElement() && CR.contains(CI->getValue()))
|
||||
return LazyValueInfo::False;
|
||||
}
|
||||
|
||||
|
||||
// Handle more complex predicates.
|
||||
ConstantRange TrueValues =
|
||||
ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
|
||||
|
@ -1219,7 +1219,7 @@ static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
|
|||
return LazyValueInfo::False;
|
||||
return LazyValueInfo::Unknown;
|
||||
}
|
||||
|
||||
|
||||
if (Result.isNotConstant()) {
|
||||
// If this is an equality comparison, we can try to fold it knowing that
|
||||
// "V != C1".
|
||||
|
@ -1240,7 +1240,7 @@ static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C,
|
|||
}
|
||||
return LazyValueInfo::Unknown;
|
||||
}
|
||||
|
||||
|
||||
return LazyValueInfo::Unknown;
|
||||
}
|
||||
|
||||
|
@ -1267,11 +1267,11 @@ LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
|
|||
return Ret;
|
||||
|
||||
// TODO: Move this logic inside getValueAt so that it can be cached rather
|
||||
// than re-queried on each call. This would also allow us to merge the
|
||||
// underlying lattice values to get more information
|
||||
// than re-queried on each call. This would also allow us to merge the
|
||||
// underlying lattice values to get more information.
|
||||
if (CxtI) {
|
||||
// For a comparison where the V is outside this block, it's possible
|
||||
// that we've branched on it before. Look to see if the value is known
|
||||
// that we've branched on it before. Look to see if the value is known
|
||||
// on all incoming edges.
|
||||
BasicBlock *BB = CxtI->getParent();
|
||||
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
||||
|
@ -1279,7 +1279,7 @@ LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
|
|||
(!isa<Instruction>(V) ||
|
||||
cast<Instruction>(V)->getParent() != BB)) {
|
||||
// For predecessor edge, determine if the comparison is true or false
|
||||
// on that edge. If they're all true or all false, we can conclude
|
||||
// on that edge. If they're all true or all false, we can conclude
|
||||
// the value of the comparison in this block.
|
||||
Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
|
||||
if (Baseline != Unknown) {
|
||||
|
|
Loading…
Reference in New Issue