#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/InstVisitor.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
for (int i = 0; i < depth; ++i) { os << " "; }
os << "[" << depth << "] ";
- os << N->getBlock()->getName() << " (" << N->getDFSNumIn()
+ os << N->getBlock()->getNameStr() << " (" << N->getDFSNumIn()
<< ", " << N->getDFSNumOut() << ")\n";
for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I)
class VISIBILITY_HIDDEN ValueRanges {
ValueNumbering &VN;
TargetData *TD;
+ LLVMContext *Context;
class VISIBILITY_HIDDEN ScopedRange {
typedef std::vector<std::pair<DomTreeDFS::Node *, ConstantRange> >
std::lower_bound(begin(), E, std::make_pair(Subtree, empty), swo);
if (I != end() && I->first == Subtree) {
- ConstantRange CR2 = I->second.maximalIntersectWith(CR);
+ ConstantRange CR2 = I->second.intersectWith(CR);
assert(!CR2.isEmptySet() && !CR2.isSingleElement() &&
"Invalid union of ranges.");
I->second = CR2;
ConstantRange Range(CR.getBitWidth());
if (LV_s == SGT_BIT) {
- Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
+ Range = Range.intersectWith(ConstantRange::makeICmpRegion(
hasEQ ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, CR));
} else if (LV_s == SLT_BIT) {
- Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
+ Range = Range.intersectWith(ConstantRange::makeICmpRegion(
hasEQ ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, CR));
}
if (LV_u == UGT_BIT) {
- Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
+ Range = Range.intersectWith(ConstantRange::makeICmpRegion(
hasEQ ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_UGT, CR));
} else if (LV_u == ULT_BIT) {
- Range = Range.maximalIntersectWith(ConstantRange::makeICmpRegion(
+ Range = Range.intersectWith(ConstantRange::makeICmpRegion(
hasEQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT, CR));
}
public:
- ValueRanges(ValueNumbering &VN, TargetData *TD) : VN(VN), TD(TD) {}
+ ValueRanges(ValueNumbering &VN, TargetData *TD, LLVMContext *C) :
+ VN(VN), TD(TD), Context(C) {}
#ifndef NDEBUG
virtual ~ValueRanges() {}
switch (LV) {
default: assert(!"Impossible lattice value!");
case NE:
- return CR1.maximalIntersectWith(CR2).isEmptySet();
+ return CR1.intersectWith(CR2).isEmptySet();
case ULT:
return CR1.getUnsignedMax().ult(CR2.getUnsignedMin());
case ULE:
unsigned i = VN.valueNumber(*I, Subtree);
ConstantRange CR_Kill = i ? range(i, Subtree) : range(*I);
if (CR_Kill.isFullSet()) continue;
- Merged = Merged.maximalIntersectWith(CR_Kill);
+ Merged = Merged.intersectWith(CR_Kill);
}
if (Merged.isFullSet() || Merged == CR_New) return;
void applyRange(unsigned n, const ConstantRange &CR,
DomTreeDFS::Node *Subtree, VRPSolver *VRP) {
- ConstantRange Merged = CR.maximalIntersectWith(range(n, Subtree));
+ ConstantRange Merged = CR.intersectWith(range(n, Subtree));
if (Merged.isEmptySet()) {
markBlock(VRP);
return;
Value *V = VN.value(n); // XXX: redesign worklist.
const Type *Ty = V->getType();
if (Ty->isInteger()) {
- addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP);
+ addToWorklist(V, ConstantInt::get(*Context, *I),
+ ICmpInst::ICMP_EQ, VRP);
return;
} else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
assert(*I == 0 && "Pointer is null but not zero?");
ConstantRange CR2 = range(n2, Subtree);
if (!CR1.isSingleElement()) {
- ConstantRange NewCR1 = CR1.maximalIntersectWith(create(LV, CR2));
+ ConstantRange NewCR1 = CR1.intersectWith(create(LV, CR2));
if (NewCR1 != CR1)
applyRange(n1, NewCR1, Subtree, VRP);
}
if (!CR2.isSingleElement()) {
- ConstantRange NewCR2 = CR2.maximalIntersectWith(
+ ConstantRange NewCR2 = CR2.intersectWith(
create(reversePredicate(LV), CR1));
if (NewCR2 != CR2)
applyRange(n2, NewCR2, Subtree, VRP);
E = DeadBlocks.end(); I != E; ++I) {
BasicBlock *BB = *I;
- DOUT << "unreachable block: " << BB->getName() << "\n";
+ DEBUG(errs() << "unreachable block: " << BB->getName() << "\n");
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
SI != SE; ++SI) {
BasicBlock *TopBB;
Instruction *TopInst;
bool &modified;
+ LLVMContext *Context;
typedef InequalityGraph::Node Node;
bool makeEqual(Value *V1, Value *V2) {
DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n";
DOUT << "context is ";
- if (TopInst) DOUT << "I: " << *TopInst << "\n";
- else DOUT << "BB: " << TopBB->getName()
- << "(" << Top->getDFSNumIn() << ")\n";
+ DEBUG(if (TopInst)
+ errs() << "I: " << *TopInst << "\n";
+ else
+ errs() << "BB: " << TopBB->getName()
+ << "(" << Top->getDFSNumIn() << ")\n");
assert(V1->getType() == V2->getType() &&
"Can't make two values with different types equal.");
Top(DTDFS->getNodeForBlock(TopBB)),
TopBB(TopBB),
TopInst(NULL),
- modified(modified)
+ modified(modified),
+ Context(&TopBB->getContext())
{
assert(Top && "VRPSolver created for unreachable basic block.");
}
Top(DTDFS->getNodeForBlock(TopInst->getParent())),
TopBB(TopInst->getParent()),
TopInst(TopInst),
- modified(modified)
+ modified(modified),
+ Context(&TopInst->getContext())
{
assert(Top && "VRPSolver created for unreachable basic block.");
assert(Top->getBlock() == TopInst->getParent() && "Context mismatch.");
bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const {
if (Constant *C1 = dyn_cast<Constant>(V1))
if (Constant *C2 = dyn_cast<Constant>(V2))
- return ConstantExpr::getCompare(Pred, C1, C2) ==
- ConstantInt::getTrue();
+ return Context->getConstantExprCompare(Pred, C1, C2) ==
+ Context->getTrue();
unsigned n1 = VN.valueNumber(V1, Top);
unsigned n2 = VN.valueNumber(V2, Top);
switch (BO->getOpcode()) {
case Instruction::And: {
// "and i32 %a, %b" EQ -1 then %a EQ -1 and %b EQ -1
- ConstantInt *CI = ConstantInt::getAllOnesValue(Ty);
+ ConstantInt *CI = cast<ConstantInt>(Context->getAllOnesValue(Ty));
if (Canonical == CI) {
add(CI, Op0, ICmpInst::ICMP_EQ, NewContext);
add(CI, Op1, ICmpInst::ICMP_EQ, NewContext);
} break;
case Instruction::Or: {
// "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0
- Constant *Zero = Constant::getNullValue(Ty);
+ Constant *Zero = Context->getNullValue(Ty);
if (Canonical == Zero) {
add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext);
add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext);
if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) {
if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) {
- add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()),
+ add(RHS,
+ ConstantInt::get(*Context, CI->getValue() ^ Arg->getValue()),
ICmpInst::ICMP_EQ, NewContext);
}
}
if (Canonical == LHS) {
if (isa<ConstantInt>(Canonical))
- add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
+ add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_EQ,
NewContext);
} else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) {
- add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE,
+ add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_NE,
NewContext);
}
} break;
// "icmp ult i32 %a, %y" EQ true then %a u< y
// etc.
- if (Canonical == ConstantInt::getTrue()) {
+ if (Canonical == Context->getTrue()) {
add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(),
NewContext);
- } else if (Canonical == ConstantInt::getFalse()) {
+ } else if (Canonical == Context->getFalse()) {
add(IC->getOperand(0), IC->getOperand(1),
ICmpInst::getInversePredicate(IC->getPredicate()), NewContext);
}
if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) {
if (Canonical == VN.canonicalize(True, Top) ||
isRelatedBy(Canonical, False, ICmpInst::ICMP_NE))
- add(SI->getCondition(), ConstantInt::getTrue(),
+ add(SI->getCondition(), Context->getTrue(),
ICmpInst::ICMP_EQ, NewContext);
else if (Canonical == VN.canonicalize(False, Top) ||
isRelatedBy(Canonical, True, ICmpInst::ICMP_NE))
- add(SI->getCondition(), ConstantInt::getFalse(),
+ add(SI->getCondition(), Context->getFalse(),
ICmpInst::ICMP_EQ, NewContext);
}
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
}
// TODO: The GEPI indices are all zero. Copy from definition to operand,
// jumping the type plane as needed.
- if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()),
+ if (isRelatedBy(GEPI, Context->getNullValue(GEPI->getType()),
ICmpInst::ICMP_NE)) {
Value *Ptr = GEPI->getPointerOperand();
- add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
+ add(Ptr, Context->getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
NewContext);
}
} else if (CastInst *CI = dyn_cast<CastInst>(I)) {
const Type *Ty = BO->getType();
assert(!Ty->isFPOrFPVector() && "Float in work queue!");
- Constant *Zero = Constant::getNullValue(Ty);
+ Constant *Zero = Context->getNullValue(Ty);
Constant *One = ConstantInt::get(Ty, 1);
- ConstantInt *AllOnes = ConstantInt::getAllOnesValue(Ty);
+ ConstantInt *AllOnes = cast<ConstantInt>(Context->getAllOnesValue(Ty));
switch (Opcode) {
default: break;
ICmpInst::Predicate Pred = IC->getPredicate();
if (isRelatedBy(Op0, Op1, Pred))
- add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext);
+ add(IC, Context->getTrue(), ICmpInst::ICMP_EQ, NewContext);
else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred)))
- add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext);
+ add(IC, Context->getFalse(),
+ ICmpInst::ICMP_EQ, NewContext);
} else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
if (I->getType()->isFPOrFPVector()) return;
// %b EQ %c then %a EQ %b
Value *Canonical = VN.canonicalize(SI->getCondition(), Top);
- if (Canonical == ConstantInt::getTrue()) {
+ if (Canonical == Context->getTrue()) {
add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext);
- } else if (Canonical == ConstantInt::getFalse()) {
+ } else if (Canonical == Context->getFalse()) {
add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext);
} else if (VN.canonicalize(SI->getTrueValue(), Top) ==
VN.canonicalize(SI->getFalseValue(), Top)) {
// TODO: The GEPI indices are all zero. Copy from operand to definition,
// jumping the type plane as needed.
Value *Ptr = GEPI->getPointerOperand();
- if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()),
+ if (isRelatedBy(Ptr, Context->getNullValue(Ptr->getType()),
ICmpInst::ICMP_NE)) {
- add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
+ add(GEPI, Context->getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
NewContext);
}
}
assert(O.LHS == VN.canonicalize(O.LHS, Top) && "Canonicalize isn't.");
assert(O.RHS == VN.canonicalize(O.RHS, Top) && "Canonicalize isn't.");
- DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS;
- if (O.ContextInst) DOUT << " context inst: " << *O.ContextInst;
- else DOUT << " context block: " << O.ContextBB->getName();
- DOUT << "\n";
+ DEBUG(errs() << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS;
+ if (O.ContextInst)
+ errs() << " context inst: " << *O.ContextInst;
+ else
+ errs() << " context block: " << O.ContextBB->getName();
+ errs() << "\n";
- DEBUG(VN.dump());
- DEBUG(IG.dump());
- DEBUG(VR.dump());
+ VN.dump();
+ IG.dump();
+ VR.dump(););
// If they're both Constant, skip it. Check for contradiction and mark
// the BB as unreachable if so.
if (Constant *CI_L = dyn_cast<Constant>(O.LHS)) {
if (Constant *CI_R = dyn_cast<Constant>(O.RHS)) {
- if (ConstantExpr::getCompare(O.Op, CI_L, CI_R) ==
- ConstantInt::getFalse())
+ if (Context->getConstantExprCompare(O.Op, CI_L, CI_R) ==
+ Context->getFalse())
UB.mark(TopBB);
WorkList.pop_front();
std::vector<DomTreeDFS::Node *> WorkList;
+ LLVMContext *Context;
public:
static char ID; // Pass identification, replacement for typeid
PredicateSimplifier() : FunctionPass(&ID) {}
// Visits each instruction in the basic block.
void visitBasicBlock(DomTreeDFS::Node *Node) {
BasicBlock *BB = Node->getBlock();
- DOUT << "Entering Basic Block: " << BB->getName()
- << " (" << Node->getDFSNumIn() << ")\n";
+ DEBUG(errs() << "Entering Basic Block: " << BB->getName()
+ << " (" << Node->getDFSNumIn() << ")\n");
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
visitInstruction(I++, Node);
}
DominatorTree *DT = &getAnalysis<DominatorTree>();
DTDFS = new DomTreeDFS(DT);
TargetData *TD = &getAnalysis<TargetData>();
+ Context = &F.getContext();
- DOUT << "Entering Function: " << F.getName() << "\n";
+ DEBUG(errs() << "Entering Function: " << F.getName() << "\n");
modified = false;
DomTreeDFS::Node *Root = DTDFS->getRootNode();
VN = new ValueNumbering(DTDFS);
IG = new InequalityGraph(*VN, Root);
- VR = new ValueRanges(*VN, TD);
+ VR = new ValueRanges(*VN, TD, Context);
WorkList.push_back(Root);
do {
return;
}
+ LLVMContext *Context = &BI.getContext();
+
for (DomTreeDFS::Node::iterator I = DTNode->begin(), E = DTNode->end();
I != E; ++I) {
BasicBlock *Dest = (*I)->getBlock();
- DOUT << "Branch thinking about %" << Dest->getName()
- << "(" << PS->DTDFS->getNodeForBlock(Dest)->getDFSNumIn() << ")\n";
+ DEBUG(errs() << "Branch thinking about %" << Dest->getName()
+ << "(" << PS->DTDFS->getNodeForBlock(Dest)->getDFSNumIn() << ")\n");
if (Dest == TrueDest) {
- DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n";
+ DEBUG(errs() << "(" << DTNode->getBlock()->getName()
+ << ") true set:\n");
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, Dest);
- VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ);
+ VRP.add(Context->getTrue(), Condition, ICmpInst::ICMP_EQ);
VRP.solve();
DEBUG(VN.dump());
DEBUG(IG.dump());
DEBUG(VR.dump());
} else if (Dest == FalseDest) {
- DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n";
+ DEBUG(errs() << "(" << DTNode->getBlock()->getName()
+ << ") false set:\n");
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, Dest);
- VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ);
+ VRP.add(Context->getFalse(), Condition, ICmpInst::ICMP_EQ);
VRP.solve();
DEBUG(VN.dump());
DEBUG(IG.dump());
for (DomTreeDFS::Node::iterator I = DTNode->begin(), E = DTNode->end();
I != E; ++I) {
BasicBlock *BB = (*I)->getBlock();
- DOUT << "Switch thinking about BB %" << BB->getName()
- << "(" << PS->DTDFS->getNodeForBlock(BB)->getDFSNumIn() << ")\n";
+ DEBUG(errs() << "Switch thinking about BB %" << BB->getName()
+ << "(" << PS->DTDFS->getNodeForBlock(BB)->getDFSNumIn() << ")\n");
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, BB);
if (BB == SI.getDefaultDest()) {
void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &AI);
- VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE);
+ VRP.add(AI.getContext().getNullValue(AI.getType()),
+ &AI, ICmpInst::ICMP_NE);
VRP.solve();
}
if (isa<Constant>(Ptr)) return;
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &LI);
- VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
+ VRP.add(LI.getContext().getNullValue(Ptr->getType()),
+ Ptr, ICmpInst::ICMP_NE);
VRP.solve();
}
if (isa<Constant>(Ptr)) return;
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
- VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
+ VRP.add(SI.getContext().getNullValue(Ptr->getType()),
+ Ptr, ICmpInst::ICMP_NE);
VRP.solve();
}
void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) {
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
+ LLVMContext &Context = SI.getContext();
uint32_t SrcBitWidth = cast<IntegerType>(SI.getSrcTy())->getBitWidth();
uint32_t DstBitWidth = cast<IntegerType>(SI.getDestTy())->getBitWidth();
APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1));
APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1));
- VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE);
- VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE);
+ VRP.add(ConstantInt::get(Context, Min), &SI, ICmpInst::ICMP_SLE);
+ VRP.add(ConstantInt::get(Context, Max), &SI, ICmpInst::ICMP_SGE);
VRP.solve();
}
void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) {
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &ZI);
+ LLVMContext &Context = ZI.getContext();
uint32_t SrcBitWidth = cast<IntegerType>(ZI.getSrcTy())->getBitWidth();
uint32_t DstBitWidth = cast<IntegerType>(ZI.getDestTy())->getBitWidth();
APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth));
- VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE);
+ VRP.add(ConstantInt::get(Context, Max), &ZI, ICmpInst::ICMP_UGE);
VRP.solve();
}
case Instruction::SDiv: {
Value *Divisor = BO.getOperand(1);
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO);
- VRP.add(Constant::getNullValue(Divisor->getType()), Divisor,
- ICmpInst::ICMP_NE);
+ VRP.add(BO.getContext().getNullValue(Divisor->getType()),
+ Divisor, ICmpInst::ICMP_NE);
VRP.solve();
break;
}
Pred = IC.getPredicate();
+ LLVMContext &Context = IC.getContext();
+
if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) {
ConstantInt *NextVal = 0;
switch (Pred) {
case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_ULT:
if (Op1->getValue() != 0)
- NextVal = ConstantInt::get(Op1->getValue()-1);
+ NextVal = ConstantInt::get(Context, Op1->getValue()-1);
break;
case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_UGT:
if (!Op1->getValue().isAllOnesValue())
- NextVal = ConstantInt::get(Op1->getValue()+1);
+ NextVal = ConstantInt::get(Context, Op1->getValue()+1);
break;
}