#define DEBUG_TYPE "lazy-value-info"
#include "llvm/Analysis/LazyValueInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ValueHandle.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include <map>
#include <stack>
using namespace llvm;
char LazyValueInfo::ID = 0;
INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info",
"Lazy Value Information Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info",
"Lazy Value Information Analysis", false, true)
namespace llvm {
FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
}
namespace {
class LVILatticeVal {
enum LatticeValueTy {
undefined,
constant,
notconstant,
constantrange,
overdefined
};
LatticeValueTy Tag;
Constant *Val;
ConstantRange Range;
public:
LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {}
static LVILatticeVal get(Constant *C) {
LVILatticeVal Res;
if (!isa<UndefValue>(C))
Res.markConstant(C);
return Res;
}
static LVILatticeVal getNot(Constant *C) {
LVILatticeVal Res;
if (!isa<UndefValue>(C))
Res.markNotConstant(C);
return Res;
}
static LVILatticeVal getRange(ConstantRange CR) {
LVILatticeVal Res;
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;
}
bool markOverdefined() {
if (isOverdefined())
return false;
Tag = overdefined;
return true;
}
bool markConstant(Constant *V) {
assert(V && "Marking constant with NULL");
if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
return markConstantRange(ConstantRange(CI->getValue()));
if (isa<UndefValue>(V))
return false;
assert((!isConstant() || getConstant() == V) &&
"Marking constant with different value");
assert(isUndefined());
Tag = constant;
Val = V;
return true;
}
bool markNotConstant(Constant *V) {
assert(V && "Marking constant with NULL");
if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue()));
if (isa<UndefValue>(V))
return false;
assert((!isConstant() || getConstant() != V) &&
"Marking constant !constant with same value");
assert((!isNotConstant() || getNotConstant() == V) &&
"Marking !constant with different value");
assert(isUndefined() || isConstant());
Tag = notconstant;
Val = V;
return true;
}
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;
}
bool mergeIn(const LVILatticeVal &RHS) {
if (RHS.isUndefined() || isOverdefined()) return false;
if (RHS.isOverdefined()) return markOverdefined();
if (isUndefined()) {
Tag = RHS.Tag;
Val = RHS.Val;
Range = RHS.Range;
return true;
}
if (isConstant()) {
if (RHS.isConstant()) {
if (Val == RHS.Val)
return false;
return markOverdefined();
}
if (RHS.isNotConstant()) {
if (Val == RHS.Val)
return markOverdefined();
if (ConstantInt *Res = dyn_cast<ConstantInt>(
ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
getConstant(),
RHS.getNotConstant())))
if (Res->isOne())
return markNotConstant(RHS.getNotConstant());
return markOverdefined();
}
return markOverdefined();
}
if (isNotConstant()) {
if (RHS.isConstant()) {
if (Val == RHS.Val)
return markOverdefined();
if (ConstantInt *Res = dyn_cast<ConstantInt>(
ConstantFoldCompareInstOperands(CmpInst::ICMP_NE,
getNotConstant(),
RHS.getConstant())))
if (Res->isOne())
return false;
return markOverdefined();
}
if (RHS.isNotConstant()) {
if (Val == RHS.Val)
return false;
return markOverdefined();
}
return markOverdefined();
}
assert(isConstantRange() && "New LVILattice type?");
if (!RHS.isConstantRange())
return markOverdefined();
ConstantRange NewR = Range.unionWith(RHS.getConstantRange());
if (NewR.isFullSet())
return markOverdefined();
return markConstantRange(NewR);
}
};
}
namespace llvm {
raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val)
LLVM_ATTRIBUTE_USED;
raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
if (Val.isUndefined())
return OS << "undefined";
if (Val.isOverdefined())
return OS << "overdefined";
if (Val.isNotConstant())
return OS << "notconstant<" << *Val.getNotConstant() << '>';
else if (Val.isConstantRange())
return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
<< Val.getConstantRange().getUpper() << '>';
return OS << "constant<" << *Val.getConstant() << '>';
}
}
namespace {
class LazyValueInfoCache;
struct LVIValueHandle : public CallbackVH {
LazyValueInfoCache *Parent;
LVIValueHandle(Value *V, LazyValueInfoCache *P)
: CallbackVH(V), Parent(P) { }
void deleted();
void allUsesReplacedWith(Value *V) {
deleted();
}
};
}
namespace {
class LazyValueInfoCache {
typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
DenseSet<OverDefinedPairTy> OverDefinedCache;
DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
friend struct LVIValueHandle;
struct OverDefinedCacheUpdater {
LazyValueInfoCache *Parent;
Value *Val;
BasicBlock *BB;
LVILatticeVal &BBLV;
OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV,
LazyValueInfoCache *P)
: Parent(P), Val(V), BB(B), BBLV(LV) { }
bool markResult(bool changed) {
if (changed && BBLV.isOverdefined())
Parent->OverDefinedCache.insert(std::make_pair(BB, Val));
return changed;
}
};
LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB);
bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T,
LVILatticeVal &Result);
bool hasBlockValue(Value *Val, BasicBlock *BB);
bool solveBlockValue(Value *Val, BasicBlock *BB);
bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
Value *Val, BasicBlock *BB);
bool solveBlockValuePHINode(LVILatticeVal &BBLV,
PHINode *PN, BasicBlock *BB);
bool solveBlockValueConstantRange(LVILatticeVal &BBLV,
Instruction *BBI, BasicBlock *BB);
void solve();
ValueCacheEntryTy &lookup(Value *V) {
return ValueCache[LVIValueHandle(V, this)];
}
public:
LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB);
LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
void eraseBlock(BasicBlock *BB);
void clear() {
SeenBlocks.clear();
ValueCache.clear();
OverDefinedCache.clear();
}
};
}
void LVIValueHandle::deleted() {
typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
SmallVector<OverDefinedPairTy, 4> ToErase;
for (DenseSet<OverDefinedPairTy>::iterator
I = Parent->OverDefinedCache.begin(),
E = Parent->OverDefinedCache.end();
I != E; ++I) {
if (I->second == getValPtr())
ToErase.push_back(*I);
}
for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(),
E = ToErase.end(); I != E; ++I)
Parent->OverDefinedCache.erase(*I);
Parent->ValueCache.erase(*this);
}
void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
if (I == SeenBlocks.end())
return;
SeenBlocks.erase(I);
SmallVector<OverDefinedPairTy, 4> ToErase;
for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
E = OverDefinedCache.end(); I != E; ++I) {
if (I->first == BB)
ToErase.push_back(*I);
}
for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(),
E = ToErase.end(); I != E; ++I)
OverDefinedCache.erase(*I);
for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I)
I->second.erase(BB);
}
void LazyValueInfoCache::solve() {
while (!BlockValueStack.empty()) {
std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
if (solveBlockValue(e.second, e.first))
BlockValueStack.pop();
}
}
bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) {
if (isa<Constant>(Val))
return true;
LVIValueHandle ValHandle(Val, this);
if (!ValueCache.count(ValHandle)) return false;
return ValueCache[ValHandle].count(BB);
}
LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) {
if (Constant *VC = dyn_cast<Constant>(Val))
return LVILatticeVal::get(VC);
SeenBlocks.insert(BB);
return lookup(Val)[BB];
}
bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
if (isa<Constant>(Val))
return true;
ValueCacheEntryTy &Cache = lookup(Val);
SeenBlocks.insert(BB);
LVILatticeVal &BBLV = Cache[BB];
OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this);
if (!BBLV.isUndefined()) {
DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
ODCacheUpdater.markResult(false);
return true;
}
BBLV.markOverdefined();
Instruction *BBI = dyn_cast<Instruction>(Val);
if (BBI == 0 || BBI->getParent() != BB) {
return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB));
}
if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB));
}
if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) {
BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType()));
return ODCacheUpdater.markResult(true);
}
LVILatticeVal Result;
if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
!BBI->getType()->isIntegerTy()) {
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because inst def found.\n");
BBLV.markOverdefined();
return ODCacheUpdater.markResult(true);
}
BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
if (BO && !isa<ConstantInt>(BO->getOperand(1))) {
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because inst def found.\n");
BBLV.markOverdefined();
return ODCacheUpdater.markResult(true);
}
return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB));
}
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
return L->getPointerAddressSpace() == 0 &&
GetUnderlyingObject(L->getPointerOperand()) ==
GetUnderlyingObject(Ptr);
}
if (StoreInst *S = dyn_cast<StoreInst>(I)) {
return S->getPointerAddressSpace() == 0 &&
GetUnderlyingObject(S->getPointerOperand()) ==
GetUnderlyingObject(Ptr);
}
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
if (MI->isVolatile()) return false;
ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
if (!Len || Len->isZero()) return false;
if (MI->getDestAddressSpace() == 0)
if (MI->getRawDest() == Ptr || MI->getDest() == Ptr)
return true;
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
if (MTI->getSourceAddressSpace() == 0)
if (MTI->getRawSource() == Ptr || MTI->getSource() == Ptr)
return true;
}
return false;
}
bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
Value *Val, BasicBlock *BB) {
LVILatticeVal Result;
bool NotNull = false;
if (Val->getType()->isPointerTy()) {
if (isa<AllocaInst>(Val)) {
NotNull = true;
} else {
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){
if (InstructionDereferencesPointer(BI, Val)) {
NotNull = true;
break;
}
}
}
}
if (BB == &BB->getParent()->getEntryBlock()) {
assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
if (NotNull) {
PointerType *PTy = cast<PointerType>(Val->getType());
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
} else {
Result.markOverdefined();
}
BBLV = Result;
return true;
}
bool EdgesMissing = false;
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
LVILatticeVal EdgeResult;
EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
if (EdgesMissing)
continue;
Result.mergeIn(EdgeResult);
if (Result.isOverdefined()) {
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because of pred.\n");
if (NotNull) {
PointerType *PTy = cast<PointerType>(Val->getType());
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
}
BBLV = Result;
return true;
}
}
if (EdgesMissing)
return false;
assert(!Result.isOverdefined());
BBLV = Result;
return true;
}
bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
PHINode *PN, BasicBlock *BB) {
LVILatticeVal Result;
bool EdgesMissing = false;
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
BasicBlock *PhiBB = PN->getIncomingBlock(i);
Value *PhiVal = PN->getIncomingValue(i);
LVILatticeVal EdgeResult;
EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult);
if (EdgesMissing)
continue;
Result.mergeIn(EdgeResult);
if (Result.isOverdefined()) {
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because of pred.\n");
BBLV = Result;
return true;
}
}
if (EdgesMissing)
return false;
assert(!Result.isOverdefined() && "Possible PHI in entry block?");
BBLV = Result;
return true;
}
bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
Instruction *BBI,
BasicBlock *BB) {
if (!hasBlockValue(BBI->getOperand(0), BB)) {
BlockValueStack.push(std::make_pair(BB, BBI->getOperand(0)));
return false;
}
LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB);
if (!LHSVal.isConstantRange()) {
BBLV.markOverdefined();
return true;
}
ConstantRange LHSRange = LHSVal.getConstantRange();
ConstantRange RHSRange(1);
IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
if (isa<BinaryOperator>(BBI)) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) {
RHSRange = ConstantRange(RHS->getValue());
} else {
BBLV.markOverdefined();
return true;
}
}
LVILatticeVal Result;
switch (BBI->getOpcode()) {
case Instruction::Add:
Result.markConstantRange(LHSRange.add(RHSRange));
break;
case Instruction::Sub:
Result.markConstantRange(LHSRange.sub(RHSRange));
break;
case Instruction::Mul:
Result.markConstantRange(LHSRange.multiply(RHSRange));
break;
case Instruction::UDiv:
Result.markConstantRange(LHSRange.udiv(RHSRange));
break;
case Instruction::Shl:
Result.markConstantRange(LHSRange.shl(RHSRange));
break;
case Instruction::LShr:
Result.markConstantRange(LHSRange.lshr(RHSRange));
break;
case Instruction::Trunc:
Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth()));
break;
case Instruction::SExt:
Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth()));
break;
case Instruction::ZExt:
Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth()));
break;
case Instruction::BitCast:
Result.markConstantRange(LHSRange);
break;
case Instruction::And:
Result.markConstantRange(LHSRange.binaryAnd(RHSRange));
break;
case Instruction::Or:
Result.markConstantRange(LHSRange.binaryOr(RHSRange));
break;
default:
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because inst def found.\n");
Result.markOverdefined();
break;
}
BBLV = Result;
return true;
}
bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
BasicBlock *BBTo, LVILatticeVal &Result) {
if (Constant *VC = dyn_cast<Constant>(Val)) {
Result = LVILatticeVal::get(VC);
return true;
}
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
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");
if (BI->getCondition() == Val) {
Result = LVILatticeVal::get(ConstantInt::get(
Type::getInt1Ty(Val->getContext()), isTrueDest));
return true;
}
ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition());
if (ICI && ICI->getOperand(0) == Val &&
isa<Constant>(ICI->getOperand(1))) {
if (ICI->isEquality()) {
if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ))
Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1)));
else
Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1)));
return true;
}
if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
ConstantRange CmpRange(CI->getValue(), CI->getValue()+1);
ConstantRange TrueValues =
ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange);
if (!isTrueDest) TrueValues = TrueValues.inverse();
if (!hasBlockValue(Val, BBFrom)) {
BlockValueStack.push(std::make_pair(BBFrom, Val));
return false;
}
LVILatticeVal InBlock = getBlockValue(Val, BBFrom);
if (!InBlock.isConstantRange()) {
Result = LVILatticeVal::getRange(TrueValues);
return true;
}
ConstantRange PossibleValues =
TrueValues.intersectWith(InBlock.getConstantRange());
Result = LVILatticeVal::getRange(PossibleValues);
return true;
}
}
}
}
if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
if (SI->getCondition() == Val) {
if (SI->getDefaultDest() == BBTo) {
Result.markOverdefined();
return true;
}
unsigned NumEdges = 0;
ConstantInt *EdgeVal = 0;
for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
if (SI->getCaseSuccessor(i) != BBTo) continue;
if (NumEdges++) break;
EdgeVal = SI->getCaseValue(i);
}
assert(EdgeVal && "Missing successor?");
if (NumEdges == 1) {
Result = LVILatticeVal::get(EdgeVal);
return true;
}
}
}
if (hasBlockValue(Val, BBFrom)) {
Result = getBlockValue(Val, BBFrom);
return true;
}
BlockValueStack.push(std::make_pair(BBFrom, Val));
return false;
}
LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) {
DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
<< BB->getName() << "'\n");
BlockValueStack.push(std::make_pair(BB, V));
solve();
LVILatticeVal Result = getBlockValue(V, BB);
DEBUG(dbgs() << " Result = " << Result << "\n");
return Result;
}
LVILatticeVal LazyValueInfoCache::
getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) {
DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
<< FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
LVILatticeVal Result;
if (!getEdgeValue(V, FromBB, ToBB, Result)) {
solve();
bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result);
(void)WasFastQuery;
assert(WasFastQuery && "More work to do after problem solved?");
}
DEBUG(dbgs() << " Result = " << Result << "\n");
return Result;
}
void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
BasicBlock *NewSucc) {
std::vector<BasicBlock*> worklist;
worklist.push_back(OldSucc);
DenseSet<Value*> ClearSet;
for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(),
E = OverDefinedCache.end(); I != E; ++I) {
if (I->first == OldSucc)
ClearSet.insert(I->second);
}
while (!worklist.empty()) {
BasicBlock *ToUpdate = worklist.back();
worklist.pop_back();
if (ToUpdate == NewSucc) continue;
bool changed = false;
for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end();
I != E; ++I) {
DenseSet<OverDefinedPairTy>::iterator OI =
OverDefinedCache.find(std::make_pair(ToUpdate, *I));
if (OI == OverDefinedCache.end()) continue;
ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)];
ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
assert(CI != Entry.end() && "Couldn't find entry to update?");
Entry.erase(CI);
OverDefinedCache.erase(OI);
changed = true;
}
if (!changed) continue;
worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
}
}
static LazyValueInfoCache &getCache(void *&PImpl) {
if (!PImpl)
PImpl = new LazyValueInfoCache();
return *static_cast<LazyValueInfoCache*>(PImpl);
}
bool LazyValueInfo::runOnFunction(Function &F) {
if (PImpl)
getCache(PImpl).clear();
TD = getAnalysisIfAvailable<TargetData>();
TLI = &getAnalysis<TargetLibraryInfo>();
return false;
}
void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<TargetLibraryInfo>();
}
void LazyValueInfo::releaseMemory() {
if (PImpl) {
delete &getCache(PImpl);
PImpl = 0;
}
}
Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB);
if (Result.isConstant())
return Result.getConstant();
if (Result.isConstantRange()) {
ConstantRange CR = Result.getConstantRange();
if (const APInt *SingleVal = CR.getSingleElement())
return ConstantInt::get(V->getContext(), *SingleVal);
}
return 0;
}
Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
BasicBlock *ToBB) {
LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
if (Result.isConstant())
return Result.getConstant();
if (Result.isConstantRange()) {
ConstantRange CR = Result.getConstantRange();
if (const APInt *SingleVal = CR.getSingleElement())
return ConstantInt::get(V->getContext(), *SingleVal);
}
return 0;
}
LazyValueInfo::Tristate
LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
BasicBlock *FromBB, BasicBlock *ToBB) {
LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
Constant *Res = 0;
if (Result.isConstant()) {
Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD,
TLI);
if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
return ResCI->isZero() ? False : True;
return Unknown;
}
if (Result.isConstantRange()) {
ConstantInt *CI = dyn_cast<ConstantInt>(C);
if (!CI) return Unknown;
ConstantRange CR = Result.getConstantRange();
if (Pred == ICmpInst::ICMP_EQ) {
if (!CR.contains(CI->getValue()))
return False;
if (CR.isSingleElement() && CR.contains(CI->getValue()))
return True;
} else if (Pred == ICmpInst::ICMP_NE) {
if (!CR.contains(CI->getValue()))
return True;
if (CR.isSingleElement() && CR.contains(CI->getValue()))
return False;
}
ConstantRange TrueValues =
ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
if (TrueValues.contains(CR))
return True;
if (TrueValues.inverse().contains(CR))
return False;
return Unknown;
}
if (Result.isNotConstant()) {
if (Pred == ICmpInst::ICMP_EQ) {
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Result.getNotConstant(), C, TD,
TLI);
if (Res->isNullValue())
return False;
} else if (Pred == ICmpInst::ICMP_NE) {
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Result.getNotConstant(), C, TD,
TLI);
if (Res->isNullValue())
return True;
}
return Unknown;
}
return Unknown;
}
void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
BasicBlock *NewSucc) {
if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
}
void LazyValueInfo::eraseBlock(BasicBlock *BB) {
if (PImpl) getCache(PImpl).eraseBlock(BB);
}