#include "llvm/Support/MathExtras.h"
#include "llvm/Support/PatternMatch.h"
#include "llvm/Support/Compiler.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SmallPtrSet.h"
static char ID; // Pass identification, replacement for typeid
InstCombiner() : FunctionPass(&ID) {}
- LLVMContext *getContext() { return Context; }
+ LLVMContext *Context;
+ LLVMContext *getContext() const { return Context; }
/// AddToWorkList - Add the specified instruction to the worklist if it
/// isn't already in it.
bool DoOneIteration(Function &F, unsigned ItNum);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired<TargetData>();
AU.addPreservedID(LCSSAID);
AU.setPreservesCFG();
}
- TargetData &getTargetData() const { return *TD; }
+ TargetData *getTargetData() const { return TD; }
// Visitation implementation - Implement instruction combining for different
// instruction types. The semantics are as follows:
Instruction *visitSDiv(BinaryOperator &I);
Instruction *visitFDiv(BinaryOperator &I);
Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
+ Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
Instruction *visitAnd(BinaryOperator &I);
Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
+ Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS);
Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op,
Value *A, Value *B, Value *C);
Instruction *visitOr (BinaryOperator &I);
const Type *DstTy, ///< The target type for the second cast instruction
TargetData *TD ///< The target data for pointer size
) {
-
+
const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
const Type *MidTy = CI->getType(); // B from above
Instruction::CastOps secondOp = Instruction::CastOps(opcode);
unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
- DstTy, TD->getIntPtrType());
+ DstTy,
+ TD ? TD->getIntPtrType() : 0);
// We don't want to form an inttoptr or ptrtoint that converts to an integer
// type that differs from the pointer size.
// If this is another cast that can be eliminated, it isn't codegen either.
if (const CastInst *CI = dyn_cast<CastInst>(V))
- if (isEliminableCastPair(CI, opcode, Ty, TD))
+ if (isEliminableCastPair(CI, opcode, Ty, TD))
return false;
return true;
}
// Constants can be considered to be not'ed values...
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
- return Context->getConstantInt(~C->getValue());
+ return ConstantInt::get(*Context, ~C->getValue());
return 0;
}
// The multiplier is really 1 << CST.
uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
uint32_t CSTVal = CST->getLimitedValue(BitWidth);
- CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal));
+ CST = ConstantInt::get(*Context, APInt(BitWidth, 1).shl(CSTVal));
return I->getOperand(0);
}
}
/// AddOne - Add one to a ConstantInt
static Constant *AddOne(Constant *C, LLVMContext *Context) {
return Context->getConstantExprAdd(C,
- Context->getConstantInt(C->getType(), 1));
+ ConstantInt::get(C->getType(), 1));
}
/// SubOne - Subtract one from a ConstantInt
static Constant *SubOne(ConstantInt *C, LLVMContext *Context) {
return Context->getConstantExprSub(C,
- Context->getConstantInt(C->getType(), 1));
+ ConstantInt::get(C->getType(), 1));
}
/// MultiplyOverflows - True if the multiply can not be expressed in an int
/// this size.
// This instruction is producing bits that are not demanded. Shrink the RHS.
Demanded &= OpC->getValue();
- I->setOperand(OpNo, Context->getConstantInt(Demanded));
+ I->setOperand(OpNo, ConstantInt::get(*Context, Demanded));
return true;
}
if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
// all known
if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
- Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask);
+ Constant *AndC = ConstantInt::get(*Context,
+ ~RHSKnownOne & DemandedMask);
Instruction *And =
BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
return InsertNewInstBefore(And, *I);
Instruction *NewVal;
if (InputBit > ResultBit)
NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
- Context->getConstantInt(I->getType(), InputBit-ResultBit));
+ ConstantInt::get(I->getType(), InputBit-ResultBit));
else
NewVal = BinaryOperator::CreateShl(I->getOperand(1),
- Context->getConstantInt(I->getType(), ResultBit-InputBit));
+ ConstantInt::get(I->getType(), ResultBit-InputBit));
NewVal->takeName(I);
return InsertNewInstBefore(NewVal, *I);
}
// If the client is only demanding bits that we know, return the known
// constant.
if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
- Constant *C = Context->getConstantInt(RHSKnownOne);
+ Constant *C = ConstantInt::get(*Context, RHSKnownOne);
if (isa<PointerType>(V->getType()))
C = Context->getConstantExprIntToPtr(C, V->getType());
return C;
if (UndefElts[i])
Elts.push_back(Context->getUndef(Type::Int32Ty));
else
- Elts.push_back(Context->getConstantInt(Type::Int32Ty,
+ Elts.push_back(ConstantInt::get(Type::Int32Ty,
Shuffle->getMaskValue(i)));
}
I->setOperand(2, Context->getConstantVector(Elts));
Value *LHS = II->getOperand(1);
Value *RHS = II->getOperand(2);
// Extract the element as scalars.
- LHS = InsertNewInstBefore(new ExtractElementInst(LHS,
- Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
- RHS = InsertNewInstBefore(new ExtractElementInst(RHS,
- Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
+ LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS,
+ ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
+ RHS = InsertNewInstBefore(ExtractElementInst::Create(RHS,
+ ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
switch (II->getIntrinsicID()) {
default: llvm_unreachable("Case stmts out of sync!");
Instruction *New =
InsertElementInst::Create(
Context->getUndef(II->getType()), TmpV,
- Context->getConstantInt(Type::Int32Ty, 0U, false), II->getName());
+ ConstantInt::get(Type::Int32Ty, 0U, false), II->getName());
InsertNewInstBefore(New, *II);
AddSoonDeadInstToWorklist(*II, 0);
return New;
bool shouldApply(Value *LHS) const { return LHS == RHS; }
Instruction *apply(BinaryOperator &Add) const {
return BinaryOperator::CreateShl(Add.getOperand(0),
- Context->getConstantInt(Add.getType(), 1));
+ ConstantInt::get(Add.getType(), 1));
}
};
ConstantInt *C2 = 0;
if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) {
Constant *CP1 =
- Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1),
+ Context->getConstantExprSub(ConstantInt::get(I.getType(), 1),
C2);
return BinaryOperator::CreateMul(Op0, CP1);
}
const APInt& Val = cast<ConstantInt>(CI)->getValue();
if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
return BinaryOperator::CreateShl(Op0,
- Context->getConstantInt(Op0->getType(), Val.logBase2()));
+ ConstantInt::get(Op0->getType(), Val.logBase2()));
}
} else if (isa<VectorType>(Op1->getType())) {
if (Op1->isNullValue())
isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
TIS) {
// Shift the X value right to turn it into "all signbits".
- Constant *Amt = Context->getConstantInt(SCIOp0->getType(),
+ Constant *Amt = ConstantInt::get(SCIOp0->getType(),
SCOpTy->getPrimitiveSizeInBits()-1);
Value *V =
InsertNewInstBefore(
*I = SI->getOperand(NonNullOperand);
AddToWorkList(BBI);
} else if (*I == SelectCond) {
- *I = NonNullOperand == 1 ? Context->getConstantIntTrue() :
- Context->getConstantIntFalse();
+ *I = NonNullOperand == 1 ? Context->getTrue() :
+ Context->getFalse();
AddToWorkList(BBI);
}
}
// (sdiv X, X) --> 1 (udiv X, X) --> 1
if (Op0 == Op1) {
if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) {
- Constant *CI = Context->getConstantInt(Ty->getElementType(), 1);
+ Constant *CI = ConstantInt::get(Ty->getElementType(), 1);
std::vector<Constant*> Elts(Ty->getNumElements(), CI);
return ReplaceInstUsesWith(I, Context->getConstantVector(Elts));
}
- Constant *CI = Context->getConstantInt(I.getType(), 1);
+ Constant *CI = ConstantInt::get(I.getType(), 1);
return ReplaceInstUsesWith(I, CI);
}
// if so, convert to a right shift.
if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
return BinaryOperator::CreateLShr(Op0,
- Context->getConstantInt(Op0->getType(), C->getValue().logBase2()));
+ ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
// X udiv C, where C >= signbit
if (C->getValue().isNegative()) {
ICmpInst::ICMP_ULT, Op0, C),
I);
return SelectInst::Create(IC, Context->getNullValue(I.getType()),
- Context->getConstantInt(I.getType(), 1));
+ ConstantInt::get(I.getType(), 1));
}
}
Value *N = RHSI->getOperand(1);
const Type *NTy = N->getType();
if (uint32_t C2 = C1.logBase2()) {
- Constant *C2V = Context->getConstantInt(NTy, C2);
+ Constant *C2V = ConstantInt::get(NTy, C2);
N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I);
}
return BinaryOperator::CreateLShr(Op0, N);
// Compute the shift amounts
uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
// Construct the "on true" case of the select
- Constant *TC = Context->getConstantInt(Op0->getType(), TSA);
+ Constant *TC = ConstantInt::get(Op0->getType(), TSA);
Instruction *TSI = BinaryOperator::CreateLShr(
Op0, TC, SI->getName()+".t");
TSI = InsertNewInstBefore(TSI, I);
// Construct the "on false" case of the select
- Constant *FC = Context->getConstantInt(Op0->getType(), FSA);
+ Constant *FC = ConstantInt::get(Op0->getType(), FSA);
Instruction *FSI = BinaryOperator::CreateLShr(
Op0, FC, SI->getName()+".f");
FSI = InsertNewInstBefore(FSI, I);
LLVMContext *Context) {
switch (code) {
default: llvm_unreachable("Illegal ICmp code!");
- case 0: return Context->getConstantIntFalse();
+ case 0: return Context->getFalse();
case 1:
if (sign)
return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS);
return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS);
else
return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS);
- case 7: return Context->getConstantIntTrue();
+ case 7: return Context->getTrue();
}
}
return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS);
else
return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS);
- case 7: return Context->getConstantIntTrue();
+ case 7: return Context->getTrue();
}
}
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
- ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShlMask);
+ ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShlMask);
if (CI->getValue() == ShlMask) {
// Masking out bits that the shift already masks
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
- ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShrMask);
+ ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
if (CI->getValue() == ShrMask) {
// Masking out bits that the shift already masks.
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
- Constant *C = Context->getConstantInt(AndRHS->getValue() & ShrMask);
+ Constant *C = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
if (C == AndRHS) { // Masking out bits shifted in.
// (Val ashr C1) & C2 -> (Val lshr C1) & C2
// Make the argument unsigned.
case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false
case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false
case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
Val->getName()+".off");
InsertNewInstBefore(Add, I);
return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add,
- Context->getConstantInt(Add->getType(), 1));
+ ConstantInt::get(Add->getType(), 1));
}
break; // (X != 13 & X != 15) -> no change
}
default: llvm_unreachable("Unknown integer condition code!");
case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
break;
case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
default: llvm_unreachable("Unknown integer condition code!");
case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false
case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
break;
case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
return 0;
}
+Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS,
+ FCmpInst *RHS) {
+
+ if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
+ RHS->getPredicate() == FCmpInst::FCMP_ORD) {
+ // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
+ if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
+ if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
+ // If either of the constants are nans, then the whole thing returns
+ // false.
+ if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
+ return ReplaceInstUsesWith(I, Context->getFalse());
+ return new FCmpInst(*Context, FCmpInst::FCMP_ORD,
+ LHS->getOperand(0), RHS->getOperand(0));
+ }
+
+ // Handle vector zeros. This occurs because the canonical form of
+ // "fcmp ord x,x" is "fcmp ord x, 0".
+ if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
+ isa<ConstantAggregateZero>(RHS->getOperand(1)))
+ return new FCmpInst(*Context, FCmpInst::FCMP_ORD,
+ LHS->getOperand(0), RHS->getOperand(0));
+ return 0;
+ }
+
+ Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
+ Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
+ FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
+
+
+ if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
+ // Swap RHS operands to match LHS.
+ Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
+ std::swap(Op1LHS, Op1RHS);
+ }
+
+ if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
+ // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
+ if (Op0CC == Op1CC)
+ return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
+
+ if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
+ return ReplaceInstUsesWith(I, Context->getFalse());
+ if (Op0CC == FCmpInst::FCMP_TRUE)
+ return ReplaceInstUsesWith(I, RHS);
+ if (Op1CC == FCmpInst::FCMP_TRUE)
+ return ReplaceInstUsesWith(I, LHS);
+
+ bool Op0Ordered;
+ bool Op1Ordered;
+ unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
+ unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
+ if (Op1Pred == 0) {
+ std::swap(LHS, RHS);
+ std::swap(Op0Pred, Op1Pred);
+ std::swap(Op0Ordered, Op1Ordered);
+ }
+ if (Op0Pred == 0) {
+ // uno && ueq -> uno && (uno || eq) -> ueq
+ // ord && olt -> ord && (ord && lt) -> olt
+ if (Op0Ordered == Op1Ordered)
+ return ReplaceInstUsesWith(I, RHS);
+
+ // uno && oeq -> uno && (ord && eq) -> false
+ // uno && ord -> false
+ if (!Op0Ordered)
+ return ReplaceInstUsesWith(I, Context->getFalse());
+ // ord && ueq -> ord && (uno || eq) -> oeq
+ return cast<Instruction>(getFCmpValue(true, Op1Pred,
+ Op0LHS, Op0RHS, Context));
+ }
+ }
+
+ return 0;
+}
+
Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
bool Changed = SimplifyCommutative(I);
if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ?
const Type *SrcTy = Op0C->getOperand(0)->getType();
- if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
+ if (SrcTy == Op1C->getOperand(0)->getType() &&
+ SrcTy->isIntOrIntVector() &&
// Only do this if the casts both really cause code to be generated.
ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
I.getType(), TD) &&
// If and'ing two fcmp, try combine them into one.
if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
- if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
- if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
- RHS->getPredicate() == FCmpInst::FCMP_ORD) {
- // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
- if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
- if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
- // If either of the constants are nans, then the whole thing returns
- // false.
- if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
- return new FCmpInst(*Context, FCmpInst::FCMP_ORD,
- LHS->getOperand(0), RHS->getOperand(0));
- }
- } else {
- Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS;
- FCmpInst::Predicate Op0CC, Op1CC;
- if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS),
- m_Value(Op0RHS)), *Context) &&
- match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS),
- m_Value(Op1RHS)), *Context)) {
- if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
- // Swap RHS operands to match LHS.
- Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
- std::swap(Op1LHS, Op1RHS);
- }
- if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
- // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
- if (Op0CC == Op1CC)
- return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
- Op0LHS, Op0RHS);
- else if (Op0CC == FCmpInst::FCMP_FALSE ||
- Op1CC == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
- else if (Op0CC == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, Op1);
- else if (Op1CC == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, Op0);
- bool Op0Ordered;
- bool Op1Ordered;
- unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
- unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
- if (Op1Pred == 0) {
- std::swap(Op0, Op1);
- std::swap(Op0Pred, Op1Pred);
- std::swap(Op0Ordered, Op1Ordered);
- }
- if (Op0Pred == 0) {
- // uno && ueq -> uno && (uno || eq) -> ueq
- // ord && olt -> ord && (ord && lt) -> olt
- if (Op0Ordered == Op1Ordered)
- return ReplaceInstUsesWith(I, Op1);
- // uno && oeq -> uno && (ord && eq) -> false
- // uno && ord -> false
- if (!Op0Ordered)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
- // ord && ueq -> ord && (uno || eq) -> oeq
- return cast<Instruction>(getFCmpValue(true, Op1Pred,
- Op0LHS, Op0RHS, Context));
- }
- }
- }
- }
- }
+ if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
+ if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS))
+ return Res;
}
return Changed ? &I : 0;
case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
}
break;
case ICmpInst::ICMP_ULT:
break;
case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
break;
}
break;
case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
break;
}
return 0;
}
+Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS,
+ FCmpInst *RHS) {
+ if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
+ RHS->getPredicate() == FCmpInst::FCMP_UNO &&
+ LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
+ if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
+ if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
+ // If either of the constants are nans, then the whole thing returns
+ // true.
+ if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
+ return ReplaceInstUsesWith(I, Context->getTrue());
+
+ // Otherwise, no need to compare the two constants, compare the
+ // rest.
+ return new FCmpInst(*Context, FCmpInst::FCMP_UNO,
+ LHS->getOperand(0), RHS->getOperand(0));
+ }
+
+ // Handle vector zeros. This occurs because the canonical form of
+ // "fcmp uno x,x" is "fcmp uno x, 0".
+ if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
+ isa<ConstantAggregateZero>(RHS->getOperand(1)))
+ return new FCmpInst(*Context, FCmpInst::FCMP_UNO,
+ LHS->getOperand(0), RHS->getOperand(0));
+
+ return 0;
+ }
+
+ Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
+ Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
+ FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
+
+ if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
+ // Swap RHS operands to match LHS.
+ Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
+ std::swap(Op1LHS, Op1RHS);
+ }
+ if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
+ // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
+ if (Op0CC == Op1CC)
+ return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
+ Op0LHS, Op0RHS);
+ if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
+ return ReplaceInstUsesWith(I, Context->getTrue());
+ if (Op0CC == FCmpInst::FCMP_FALSE)
+ return ReplaceInstUsesWith(I, RHS);
+ if (Op1CC == FCmpInst::FCMP_FALSE)
+ return ReplaceInstUsesWith(I, LHS);
+ bool Op0Ordered;
+ bool Op1Ordered;
+ unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
+ unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
+ if (Op0Ordered == Op1Ordered) {
+ // If both are ordered or unordered, return a new fcmp with
+ // or'ed predicates.
+ Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred,
+ Op0LHS, Op0RHS, Context);
+ if (Instruction *I = dyn_cast<Instruction>(RV))
+ return I;
+ // Otherwise, it's a constant boolean value...
+ return ReplaceInstUsesWith(I, RV);
+ }
+ }
+ return 0;
+}
+
/// FoldOrWithConstants - This helper function folds:
///
/// ((A | B) & C1) | (B & C2)
InsertNewInstBefore(Or, I);
Or->takeName(Op0);
return BinaryOperator::CreateAnd(Or,
- Context->getConstantInt(RHS->getValue() | C1->getValue()));
+ ConstantInt::get(*Context, RHS->getValue() | C1->getValue()));
}
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
InsertNewInstBefore(Or, I);
Or->takeName(Op0);
return BinaryOperator::CreateXor(Or,
- Context->getConstantInt(C1->getValue() & ~RHS->getValue()));
+ ConstantInt::get(*Context, C1->getValue() & ~RHS->getValue()));
}
// Try to fold constant and into select arguments.
if (!isa<ICmpInst>(Op0C->getOperand(0)) ||
!isa<ICmpInst>(Op1C->getOperand(0))) {
const Type *SrcTy = Op0C->getOperand(0)->getType();
- if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() &&
+ if (SrcTy == Op1C->getOperand(0)->getType() &&
+ SrcTy->isIntOrIntVector() &&
// Only do this if the casts both really cause code to be
// generated.
ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0),
// (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
- if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
- if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
- RHS->getPredicate() == FCmpInst::FCMP_UNO &&
- LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
- if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
- if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
- // If either of the constants are nans, then the whole thing returns
- // true.
- if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
-
- // Otherwise, no need to compare the two constants, compare the
- // rest.
- return new FCmpInst(*Context, FCmpInst::FCMP_UNO,
- LHS->getOperand(0), RHS->getOperand(0));
- }
- } else {
- Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS;
- FCmpInst::Predicate Op0CC, Op1CC;
- if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS),
- m_Value(Op0RHS)), *Context) &&
- match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS),
- m_Value(Op1RHS)), *Context)) {
- if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
- // Swap RHS operands to match LHS.
- Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
- std::swap(Op1LHS, Op1RHS);
- }
- if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
- // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
- if (Op0CC == Op1CC)
- return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
- Op0LHS, Op0RHS);
- else if (Op0CC == FCmpInst::FCMP_TRUE ||
- Op1CC == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
- else if (Op0CC == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, Op1);
- else if (Op1CC == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, Op0);
- bool Op0Ordered;
- bool Op1Ordered;
- unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
- unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
- if (Op0Ordered == Op1Ordered) {
- // If both are ordered or unordered, return a new fcmp with
- // or'ed predicates.
- Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred,
- Op0LHS, Op0RHS, Context);
- if (Instruction *I = dyn_cast<Instruction>(RV))
- return I;
- // Otherwise, it's a constant boolean value...
- return ReplaceInstUsesWith(I, RV);
- }
- }
- }
- }
- }
+ if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
+ if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS))
+ return Res;
}
return Changed ? &I : 0;
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
- if (RHS == Context->getConstantIntTrue() && Op0->hasOneUse()) {
+ if (RHS == Context->getTrue() && Op0->hasOneUse()) {
// xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
return new ICmpInst(*Context, ICI->getInversePredicate(),
Instruction::CastOps Opcode = Op0C->getOpcode();
if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
if (RHS == Context->getConstantExprCast(Opcode,
- Context->getConstantIntTrue(),
+ Context->getTrue(),
Op0C->getDestTy())) {
Instruction *NewCI = InsertNewInstBefore(CmpInst::Create(
*Context,
if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
Constant *NegOp0I0C = Context->getConstantExprNeg(Op0I0C);
Constant *ConstantRHS = Context->getConstantExprSub(NegOp0I0C,
- Context->getConstantInt(I.getType(), 1));
+ ConstantInt::get(I.getType(), 1));
return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
}
Constant *NegOp0CI = Context->getConstantExprNeg(Op0CI);
return BinaryOperator::CreateSub(
Context->getConstantExprSub(NegOp0CI,
- Context->getConstantInt(I.getType(), 1)),
+ ConstantInt::get(I.getType(), 1)),
Op0I->getOperand(0));
} else if (RHS->getValue().isSignBit()) {
// (X + C) ^ signbit -> (X + C + signbit)
- Constant *C =
- Context->getConstantInt(RHS->getValue() + Op0CI->getValue());
+ Constant *C = ConstantInt::get(*Context,
+ RHS->getValue() + Op0CI->getValue());
return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
}
if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
- Constant *Idx = Context->getConstantInt(Type::Int32Ty, i);
+ Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
if (HasAddOverflow(ExtractElement(Result, Idx, Context),
ExtractElement(In1, Idx, Context),
ExtractElement(In2, Idx, Context),
if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
- Constant *Idx = Context->getConstantInt(Type::Int32Ty, i);
+ Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
if (HasSubOverflow(ExtractElement(Result, Idx, Context),
ExtractElement(In1, Idx, Context),
ExtractElement(In2, Idx, Context),
/// code necessary to compute the offset from the base pointer (without adding
/// in the base pointer). Return the result as a signed integer of intptr size.
static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
- TargetData &TD = IC.getTargetData();
+ TargetData &TD = *IC.getTargetData();
gep_type_iterator GTI = gep_type_begin(GEP);
const Type *IntPtrTy = TD.getIntPtrType();
LLVMContext *Context = IC.getContext();
if (ConstantInt *RC = dyn_cast<ConstantInt>(Result))
Result =
- Context->getConstantInt(RC->getValue() + APInt(IntPtrWidth, Size));
+ ConstantInt::get(*Context,
+ RC->getValue() + APInt(IntPtrWidth, Size));
else
Result = IC.InsertNewInstBefore(
BinaryOperator::CreateAdd(Result,
- Context->getConstantInt(IntPtrTy, Size),
+ ConstantInt::get(IntPtrTy, Size),
GEP->getName()+".offs"), I);
continue;
}
- Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
+ Constant *Scale = ConstantInt::get(IntPtrTy, Size);
Constant *OC =
Context->getConstantExprIntegerCast(OpC, IntPtrTy, true /*SExt*/);
Scale = Context->getConstantExprMul(OC, Scale);
Op->getName()+".c"), I);
}
if (Size != 1) {
- Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
+ Constant *Scale = ConstantInt::get(IntPtrTy, Size);
if (Constant *OpC = dyn_cast<Constant>(Op))
Op = Context->getConstantExprMul(OpC, Scale);
else // We'll let instcombine(mul) convert this to a shl if possible.
///
static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
InstCombiner &IC) {
- TargetData &TD = IC.getTargetData();
+ TargetData &TD = *IC.getTargetData();
gep_type_iterator GTI = gep_type_begin(GEP);
// Check to see if this gep only has a single variable index. If so, and if
VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
true /*SExt*/,
VariableIdx->getNameStart(), &I);
- Constant *OffsetVal = IC.getContext()->getConstantInt(IntPtrTy, NewOffs);
+ Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
}
RHS = BCI->getOperand(0);
Value *PtrBase = GEPLHS->getOperand(0);
- if (PtrBase == RHS) {
+ if (TD && PtrBase == RHS) {
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
// This transformation (ignoring the base and scales) is valid because we
// know pointers can't overflow. See if we can output an optimized form.
if (NumDifferences == 0) // SAME GEP?
return ReplaceInstUsesWith(I, // No comparison is needed here.
- Context->getConstantInt(Type::Int1Ty,
+ ConstantInt::get(Type::Int1Ty,
ICmpInst::isTrueWhenEqual(Cond)));
else if (NumDifferences == 1) {
// Only lower this if the icmp is the only user of the GEP or if we expect
// the result to fold to a constant!
- if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
+ if (TD &&
+ (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
(isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
Value *L = EmitGEPOffset(GEPLHS, I, *this);
Pred = ICmpInst::ICMP_NE;
break;
case FCmpInst::FCMP_ORD:
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
case FCmpInst::FCMP_UNO:
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
}
const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
Pred == ICmpInst::ICMP_SLE)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getTrue());
+ return ReplaceInstUsesWith(I, Context->getFalse());
}
} else {
// If the RHS value is > UnsignedMax, fold the comparison. This handles
if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
Pred == ICmpInst::ICMP_ULE)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getTrue());
+ return ReplaceInstUsesWith(I, Context->getFalse());
}
}
if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
Pred == ICmpInst::ICMP_SGE)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getTrue());
+ return ReplaceInstUsesWith(I, Context->getFalse());
}
}
switch (Pred) {
default: llvm_unreachable("Unexpected integer comparison!");
case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
case ICmpInst::ICMP_ULE:
// (float)int <= 4.4 --> int <= 4
// (float)int <= -4.4 --> false
if (RHS.isNegative())
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
case ICmpInst::ICMP_SLE:
// (float)int <= 4.4 --> int <= 4
// (float)int < -4.4 --> false
// (float)int < 4.4 --> int <= 4
if (RHS.isNegative())
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
Pred = ICmpInst::ICMP_ULE;
break;
case ICmpInst::ICMP_SLT:
// (float)int > 4.4 --> int > 4
// (float)int > -4.4 --> true
if (RHS.isNegative())
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
break;
case ICmpInst::ICMP_SGT:
// (float)int > 4.4 --> int > 4
// (float)int >= -4.4 --> true
// (float)int >= 4.4 --> int > 4
if (!RHS.isNegative())
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
Pred = ICmpInst::ICMP_UGT;
break;
case ICmpInst::ICMP_SGE:
// Fold trivial predicates.
if (I.getPredicate() == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
if (I.getPredicate() == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
// Simplify 'fcmp pred X, X'
if (Op0 == Op1) {
case FCmpInst::FCMP_UEQ: // True if unordered or equal
case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal
case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
case FCmpInst::FCMP_OGT: // True if ordered and greater than
case FCmpInst::FCMP_OLT: // True if ordered and less than
case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
case FCmpInst::FCMP_ULT: // True if unordered or less than
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
if (CFP->getValueAPF().isNaN()) {
if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and...
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
assert(FCmpInst::isUnordered(I.getPredicate()) &&
"Comparison must be either ordered or unordered!");
// True if unordered.
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
}
}
// icmp X, X
if (Op0 == Op1)
- return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
I.isTrueWhenEqual()));
if (isa<UndefValue>(Op1)) // X icmp undef -> undef
isa<ConstantPointerNull>(Op0)) &&
(isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
isa<ConstantPointerNull>(Op1)))
- return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
!I.isTrueWhenEqual()));
// icmp's with boolean values can always be turned into bitwise operations
default: break;
case ICmpInst::ICMP_ULE:
if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0,
AddOne(CI, Context));
case ICmpInst::ICMP_SLE:
if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
AddOne(CI, Context));
case ICmpInst::ICMP_UGE:
if (CI->isMinValue(false)) // A >=u MIN -> TRUE
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Op0,
SubOne(CI, Context));
case ICmpInst::ICMP_SGE:
if (CI->isMinValue(true)) // A >=s MIN -> TRUE
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
SubOne(CI, Context));
}
// that code below can assume that Min != Max.
if (!isa<Constant>(Op0) && Op0Min == Op0Max)
return new ICmpInst(*Context, I.getPredicate(),
- Context->getConstantInt(Op0Min), Op1);
+ ConstantInt::get(*Context, Op0Min), Op1);
if (!isa<Constant>(Op1) && Op1Min == Op1Max)
return new ICmpInst(*Context, I.getPredicate(), Op0,
- Context->getConstantInt(Op1Min));
+ ConstantInt::get(*Context, Op1Min));
// Based on the range information we know about the LHS, see if we can
// simplify this comparison. For example, (x&4) < 8 is always true.
default: llvm_unreachable("Unknown icmp opcode!");
case ICmpInst::ICMP_EQ:
if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
case ICmpInst::ICMP_NE:
if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
break;
case ICmpInst::ICMP_ULT:
if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
break;
case ICmpInst::ICMP_UGT:
if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
break;
case ICmpInst::ICMP_SLT:
if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
break;
case ICmpInst::ICMP_SGT:
if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
case ICmpInst::ICMP_SGE:
assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
case ICmpInst::ICMP_SLE:
assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
case ICmpInst::ICMP_UGE:
assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
case ICmpInst::ICMP_ULE:
assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getTrue());
if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
- return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(I, Context->getFalse());
break;
}
// can assume it is successful and remove the malloc.
if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
AddToWorkList(LHSI);
- return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
!I.isTrueWhenEqual()));
}
break;
// Mask = -1 >> count-trailing-zeros(Cst).
if (!CI->isZero() && !CI->isOne()) {
const APInt &AP = CI->getValue();
- ConstantInt *Mask = Context->getConstantInt(
+ ConstantInt *Mask = ConstantInt::get(*Context,
APInt::getLowBitsSet(AP.getBitWidth(),
AP.getBitWidth() -
AP.countTrailingZeros()));
if (match(B, m_ConstantInt(C1), *Context) &&
match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) {
Constant *NC =
- Context->getConstantInt(C1->getValue() ^ C2->getValue());
+ ConstantInt::get(*Context, C1->getValue() ^ C2->getValue());
Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
return new ICmpInst(*Context, I.getPredicate(), A,
InsertNewInstBefore(Xor, I));
default: llvm_unreachable("Unhandled icmp opcode!");
case ICmpInst::ICMP_EQ:
if (LoOverflow && HiOverflow)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(ICI, Context->getFalse());
else if (HiOverflow)
return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, LoBound);
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
case ICmpInst::ICMP_NE:
if (LoOverflow && HiOverflow)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(ICI, Context->getTrue());
else if (HiOverflow)
return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, LoBound);
case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_SLT:
if (LoOverflow == +1) // Low bound is greater than input range.
- return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(ICI, Context->getTrue());
if (LoOverflow == -1) // Low bound is less than input range.
- return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(ICI, Context->getFalse());
return new ICmpInst(*Context, Pred, X, LoBound);
case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_SGT:
if (HiOverflow == +1) // High bound greater than input range.
- return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(ICI, Context->getFalse());
else if (HiOverflow == -1) // High bound less than input range.
- return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(ICI, Context->getTrue());
if (Pred == ICmpInst::ICMP_UGT)
return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound);
else
NewRHS.zext(SrcBits);
NewRHS |= KnownOne;
return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
- Context->getConstantInt(NewRHS));
+ ConstantInt::get(*Context, NewRHS));
}
}
break;
? ICI.getUnsignedPredicate()
: ICI.getSignedPredicate();
return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
- Context->getConstantInt(RHSV ^ SignBit));
+ ConstantInt::get(*Context, RHSV ^ SignBit));
}
// (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
: ICI.getSignedPredicate();
Pred = ICI.getSwappedPredicate(Pred);
return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
- Context->getConstantInt(RHSV ^ NotSignBit));
+ ConstantInt::get(*Context, RHSV ^ NotSignBit));
}
}
}
NewCI.zext(BitWidth);
Instruction *NewAnd =
BinaryOperator::CreateAnd(Cast->getOperand(0),
- Context->getConstantInt(NewCST),LHSI->getName());
+ ConstantInt::get(*Context, NewCST), LHSI->getName());
InsertNewInstBefore(NewAnd, ICI);
return new ICmpInst(*Context, ICI.getPredicate(), NewAnd,
- Context->getConstantInt(NewCI));
+ ConstantInt::get(*Context, NewCI));
}
}
// As a special case, check to see if this means that the
// result is always true or false now.
if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(ICI, Context->getFalse());
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(ICI, Context->getTrue());
} else {
ICI.setOperand(1, NewCst);
Constant *NewAndCST;
ShAmt);
if (Comp != RHS) {// Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst);
}
// Otherwise strength reduce the shift into an and.
uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
Constant *Mask =
- Context->getConstantInt(APInt::getLowBitsSet(TypeBits,
+ ConstantInt::get(*Context, APInt::getLowBitsSet(TypeBits,
TypeBits-ShAmtVal));
Instruction *AndI =
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
return new ICmpInst(*Context, ICI.getPredicate(), And,
- Context->getConstantInt(RHSV.lshr(ShAmtVal)));
+ ConstantInt::get(*Context, RHSV.lshr(ShAmtVal)));
}
}
if (LHSI->hasOneUse() &&
isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
// (X << 31) <s 0 --> (X&1) != 0
- Constant *Mask = Context->getConstantInt(APInt(TypeBits, 1) <<
+ Constant *Mask = ConstantInt::get(*Context, APInt(TypeBits, 1) <<
(TypeBits-ShAmt->getZExtValue()-1));
Instruction *AndI =
BinaryOperator::CreateAnd(LHSI->getOperand(0),
if (Comp != RHSV) { // Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst);
}
if (LHSI->hasOneUse()) {
// Otherwise strength reduce the shift into an and.
APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
- Constant *Mask = Context->getConstantInt(Val);
+ Constant *Mask = ConstantInt::get(*Context, Val);
Instruction *AndI =
BinaryOperator::CreateAnd(LHSI->getOperand(0),
if (ICI.isSignedPredicate()) {
if (CR.getLower().isSignBit()) {
return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0),
- Context->getConstantInt(CR.getUpper()));
+ ConstantInt::get(*Context, CR.getUpper()));
} else if (CR.getUpper().isSignBit()) {
return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0),
- Context->getConstantInt(CR.getLower()));
+ ConstantInt::get(*Context, CR.getLower()));
}
} else {
if (CR.getLower().isMinValue()) {
return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0),
- Context->getConstantInt(CR.getUpper()));
+ ConstantInt::get(*Context, CR.getUpper()));
} else if (CR.getUpper().isMinValue()) {
return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0),
- Context->getConstantInt(CR.getLower()));
+ ConstantInt::get(*Context, CR.getLower()));
}
}
}
Constant *NotCI = Context->getConstantExprNot(RHS);
if (!Context->getConstantExprAnd(BOC, NotCI)->isNullValue())
return ReplaceInstUsesWith(ICI,
- Context->getConstantInt(Type::Int1Ty,
+ ConstantInt::get(Type::Int1Ty,
isICMP_NE));
}
break;
// comparison can never succeed!
if ((RHSV & ~BOC->getValue()) != 0)
return ReplaceInstUsesWith(ICI,
- Context->getConstantInt(Type::Int1Ty,
+ ConstantInt::get(Type::Int1Ty,
isICMP_NE));
// If we have ((X & C) == C), turn it into ((X & C) != 0).
if (II->getIntrinsicID() == Intrinsic::bswap) {
AddToWorkList(II);
ICI.setOperand(0, II->getOperand(1));
- ICI.setOperand(1, Context->getConstantInt(RHSV.byteSwap()));
+ ICI.setOperand(1, ConstantInt::get(*Context, RHSV.byteSwap()));
return &ICI;
}
}
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
// integer type is the same size as the pointer type.
- if (LHSCI->getOpcode() == Instruction::PtrToInt &&
- getTargetData().getPointerSizeInBits() ==
+ if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
+ TD->getPointerSizeInBits() ==
cast<IntegerType>(DestTy)->getBitWidth()) {
Value *RHSOp = 0;
if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
// First, handle some easy cases. We know the result cannot be equal at this
// point so handle the ICI.isEquality() cases
if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ return ReplaceInstUsesWith(ICI, Context->getFalse());
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
- return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(ICI, Context->getTrue());
// Evaluate the comparison for LT (we invert for GT below). LE and GE cases
// should have been folded away previously and not enter in here.
if (isSignedCmp) {
// We're performing a signed comparison.
if (cast<ConstantInt>(CI)->getValue().isNegative())
- Result = Context->getConstantIntFalse(); // X < (small) --> false
+ Result = Context->getFalse(); // X < (small) --> false
else
- Result = Context->getConstantIntTrue(); // X < (large) --> true
+ Result = Context->getTrue(); // X < (large) --> true
} else {
// We're performing an unsigned comparison.
if (isSignedExt) {
LHSCIOp, NegOne, ICI.getName()), ICI);
} else {
// Unsigned extend & unsigned compare -> always true.
- Result = Context->getConstantIntTrue();
+ Result = Context->getTrue();
}
}
if (I.getOpcode() != Instruction::AShr)
return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType()));
else {
- I.setOperand(1, Context->getConstantInt(I.getType(), TypeBits-1));
+ I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
return &I;
}
}
}
Instruction *And =
- BinaryOperator::CreateAnd(NSh, Context->getConstantInt(MaskV),
+ BinaryOperator::CreateAnd(NSh, ConstantInt::get(*Context, MaskV),
TI->getName());
InsertNewInstBefore(And, I); // shift1 & 0x00FF
Op0BO->getOperand(1)->getName());
InsertNewInstBefore(X, I); // (X + (Y << C))
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
- return BinaryOperator::CreateAnd(X, Context->getConstantInt(
+ return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
}
Op0BO->getOperand(0)->getName());
InsertNewInstBefore(X, I); // (X + (Y << C))
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
- return BinaryOperator::CreateAnd(X, Context->getConstantInt(
+ return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
}
}
return BinaryOperator::Create(I.getOpcode(), X,
- Context->getConstantInt(Ty, AmtSum));
+ ConstantInt::get(Ty, AmtSum));
} else if (ShiftOp->getOpcode() == Instruction::LShr &&
I.getOpcode() == Instruction::AShr) {
if (AmtSum >= TypeBits)
return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
- return BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, AmtSum));
+ return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
} else if (ShiftOp->getOpcode() == Instruction::AShr &&
I.getOpcode() == Instruction::LShr) {
// ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
AmtSum = TypeBits-1;
Instruction *Shift =
- BinaryOperator::CreateAShr(X, Context->getConstantInt(Ty, AmtSum));
+ BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(Shift, ConstantInt::get(*Context, Mask));
}
// Okay, if we get here, one shift must be left, and the other shift must be
// If we have ((X >>? C) << C), turn this into X & (-1 << C).
if (I.getOpcode() == Instruction::Shl) {
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
- return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
}
// If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
if (I.getOpcode() == Instruction::LShr) {
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
- return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
}
// We can simplify ((X << C) >>s C) into a trunc + sext.
// NOTE: we could do this for any C, but that would make 'unusual' integer
assert(ShiftOp->getOpcode() == Instruction::LShr ||
ShiftOp->getOpcode() == Instruction::AShr);
Instruction *Shift =
- BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
+ BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(Shift,
+ ConstantInt::get(*Context, Mask));
}
// (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
if (I.getOpcode() == Instruction::LShr) {
assert(ShiftOp->getOpcode() == Instruction::Shl);
Instruction *Shift =
- BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, ShiftDiff));
+ BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(Shift,
+ ConstantInt::get(*Context, Mask));
}
// We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
ShiftOp->getOpcode() == Instruction::AShr);
Instruction *Shift =
BinaryOperator::Create(ShiftOp->getOpcode(), X,
- Context->getConstantInt(Ty, ShiftDiff));
+ ConstantInt::get(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(Shift,
+ ConstantInt::get(*Context, Mask));
}
// (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
if (I.getOpcode() == Instruction::LShr) {
assert(ShiftOp->getOpcode() == Instruction::Shl);
Instruction *Shift =
- BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
+ BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
+ return BinaryOperator::CreateAnd(Shift,
+ ConstantInt::get(*Context, Mask));
}
// We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Offset = CI->getZExtValue();
Scale = 0;
- return Context->getConstantInt(Type::Int32Ty, 0);
+ return ConstantInt::get(Type::Int32Ty, 0);
} else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
if (I->getOpcode() == Instruction::Shl) {
EraseInstFromFunction(*User);
}
}
-
+
+ // This requires TargetData to get the alloca alignment and size information.
+ if (!TD) return 0;
+
// Get the type really allocated and the type casted to.
const Type *AllocElTy = AI.getAllocatedType();
const Type *CastElTy = PTy->getElementType();
Amt = NumElements;
} else {
// If the allocation size is constant, form a constant mul expression
- Amt = Context->getConstantInt(Type::Int32Ty, Scale);
+ Amt = ConstantInt::get(Type::Int32Ty, Scale);
if (isa<ConstantInt>(NumElements))
Amt = Context->getConstantExprMul(cast<ConstantInt>(NumElements),
cast<ConstantInt>(Amt));
}
if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
- Value *Off = Context->getConstantInt(Type::Int32Ty, Offset, true);
+ Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
Amt = InsertNewInstBefore(Tmp, AI);
}
SmallVectorImpl<Value*> &NewIndices,
const TargetData *TD,
LLVMContext *Context) {
+ if (!TD) return 0;
if (!Ty->isSized()) return 0;
// Start with the index over the outer type. Note that the type size
assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
}
- NewIndices.push_back(Context->getConstantInt(IntPtrTy, FirstIdx));
+ NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
// Index into the types. If we fail, set OrigBase to null.
while (Offset) {
"Offset must stay within the indexed type");
unsigned Elt = SL->getElementContainingOffset(Offset);
- NewIndices.push_back(Context->getConstantInt(Type::Int32Ty, Elt));
+ NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
Offset -= SL->getElementOffset(Elt);
Ty = STy->getElementType(Elt);
} else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
assert(EltSize && "Cannot index into a zero-sized array");
- NewIndices.push_back(Context->getConstantInt(IntPtrTy,Offset/EltSize));
+ NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
Offset %= EltSize;
Ty = AT->getElementType();
} else {
// GEP computes a constant offset, see if we can convert these three
// instructions into fewer. This typically happens with unions and other
// non-type-safe code.
- if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
+ if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
if (GEP->hasAllConstantIndices()) {
// We are guaranteed to get a constant from EmitGEPOffset.
ConstantInt *OffsetV =
return ReplaceInstUsesWith(CI, Res);
// We need to emit an AND to clear the high bits.
- Constant *C = Context->getConstantInt(APInt::getLowBitsSet(DestBitSize,
- SrcBitSize));
+ Constant *C = ConstantInt::get(*Context,
+ APInt::getLowBitsSet(DestBitSize, SrcBitSize));
return BinaryOperator::CreateAnd(Res, C);
}
case Instruction::SExt: {
// cast (xor bool X, true) to int --> xor (cast bool X to int), 1
if (isa<ZExtInst>(CI) && SrcBitSize == 1 &&
SrcI->getOpcode() == Instruction::Xor &&
- Op1 == Context->getConstantIntTrue() &&
+ Op1 == Context->getTrue() &&
(!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI);
return BinaryOperator::CreateXor(New,
- Context->getConstantInt(CI.getType(), 1));
+ ConstantInt::get(CI.getType(), 1));
}
break;
// Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
if (DestBitWidth == 1) {
- Constant *One = Context->getConstantInt(Src->getType(), 1);
+ Constant *One = ConstantInt::get(Src->getType(), 1);
Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
Value *Zero = Context->getNullValue(Src->getType());
return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero);
if (!DoXform) return ICI;
Value *In = ICI->getOperand(0);
- Value *Sh = Context->getConstantInt(In->getType(),
+ Value *Sh = ConstantInt::get(In->getType(),
In->getType()->getScalarSizeInBits()-1);
In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh,
In->getName()+".lobit"),
false/*ZExt*/, "tmp", &CI);
if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
- Constant *One = Context->getConstantInt(In->getType(), 1);
+ Constant *One = ConstantInt::get(In->getType(), 1);
In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One,
In->getName()+".not"),
CI);
if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
// (X&4) == 2 --> false
// (X&4) != 2 --> true
- Constant *Res = Context->getConstantInt(Type::Int1Ty, isNE);
+ Constant *Res = ConstantInt::get(Type::Int1Ty, isNE);
Res = Context->getConstantExprZExt(Res, CI.getType());
return ReplaceInstUsesWith(CI, Res);
}
// Perform a logical shr by shiftamt.
// Insert the shift to put the result in the low bit.
In = InsertNewInstBefore(BinaryOperator::CreateLShr(In,
- Context->getConstantInt(In->getType(), ShiftAmt),
+ ConstantInt::get(In->getType(), ShiftAmt),
In->getName()+".lobit"), CI);
}
if ((Op1CV != 0) == isNE) { // Toggle the low bit.
- Constant *One = Context->getConstantInt(In->getType(), 1);
+ Constant *One = ConstantInt::get(In->getType(), 1);
In = BinaryOperator::CreateXor(In, One, "tmp");
InsertNewInstBefore(cast<Instruction>(In), CI);
}
// SrcSize > DstSize: trunc(a) & mask
if (SrcSize < DstSize) {
APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
- Constant *AndConst = Context->getConstantInt(A->getType(), AndValue);
+ Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
Instruction *And =
BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask");
InsertNewInstBefore(And, CI);
return new ZExtInst(And, CI.getType());
} else if (SrcSize == DstSize) {
APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
- return BinaryOperator::CreateAnd(A, Context->getConstantInt(A->getType(),
+ return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
AndValue));
} else if (SrcSize > DstSize) {
Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp");
InsertNewInstBefore(Trunc, CI);
APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
return BinaryOperator::CreateAnd(Trunc,
- Context->getConstantInt(Trunc->getType(),
+ ConstantInt::get(Trunc->getType(),
AndValue));
}
}
unsigned MidSize = Src->getType()->getScalarSizeInBits();
unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
- Constant *ShAmtV = Context->getConstantInt(CI.getType(), ShAmt);
+ Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV,
CI.getName()), CI);
return BinaryOperator::CreateAShr(I, ShAmtV);
// trunc to be exposed to other transforms. Don't do this for extending
// ptrtoint's, because we don't know if the target sign or zero extends its
// pointers.
- if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
+ if (TD &&
+ CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0),
TD->getIntPtrType(),
"tmp"), CI);
// allows the trunc to be exposed to other transforms. Don't do this for
// extending inttoptr's, because we don't know if the target sign or zero
// extends to pointers.
- if (CI.getOperand(0)->getType()->getScalarSizeInBits() >
+ if (TD &&
+ CI.getOperand(0)->getType()->getScalarSizeInBits() >
TD->getPointerSizeInBits()) {
Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0),
TD->getIntPtrType(),
if (SrcVTy->getNumElements() == 1) {
if (!isa<VectorType>(DestTy)) {
Instruction *Elem =
- new ExtractElementInst(Src, Context->getNullValue(Type::Int32Ty));
+ ExtractElementInst::Create(Src, Context->getNullValue(Type::Int32Ty));
InsertNewInstBefore(Elem, CI);
return CastInst::Create(Instruction::BitCast, Elem, DestTy);
}
case Instruction::And:
return Context->getAllOnesValue(I->getType());
case Instruction::Mul:
- return Context->getConstantInt(I->getType(), 1);
+ return ConstantInt::get(I->getType(), 1);
}
}
// Fold this by inserting a select from the input values.
SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
- FI->getOperand(0), SI.getName()+".v");
+ FI->getOperand(0), SI.getName()+".v");
InsertNewInstBefore(NewSI, SI);
return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
TI->getType());
if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
(Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
Value *In = ICI->getOperand(0);
- Value *Sh = Context->getConstantInt(In->getType(),
+ Value *Sh = ConstantInt::get(In->getType(),
In->getType()->getScalarSizeInBits()-1);
In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
- In->getName()+".lobit"),
+ In->getName()+".lobit"),
*ICI);
if (In->getType() != SI.getType())
In = CastInst::CreateIntegerCast(In, SI.getType(),
unsigned CopyAlign = MI->getAlignment();
if (CopyAlign < MinAlign) {
- MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
+ MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
MinAlign, false));
return MI;
}
// integer datatype.
if (Value *Op = getBitCastOperand(MI->getOperand(1))) {
const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType();
- if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
+ if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
// The SrcETy might be something like {{{double}}} or [1 x double]. Rip
// down through these levels if so.
while (!SrcETy->isSingleValueType()) {
Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
if (MI->getAlignment() < Alignment) {
- MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
+ MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
Alignment, false));
return MI;
}
// Extract the fill value and store.
uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
- InsertNewInstBefore(new StoreInst(Context->getConstantInt(ITy, Fill),
+ InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
Dest, false, Alignment), *MI);
// Set the size of the copy to 0, it will be deleted on the next iteration.
if (ExtractedElts[Idx] == 0) {
Instruction *Elt =
- new ExtractElementInst(Idx < 16 ? Op0 : Op1,
- Context->getConstantInt(Type::Int32Ty, Idx&15, false), "tmp");
+ ExtractElementInst::Create(Idx < 16 ? Op0 : Op1,
+ ConstantInt::get(Type::Int32Ty, Idx&15, false), "tmp");
InsertNewInstBefore(Elt, CI);
ExtractedElts[Idx] = Elt;
}
// Insert this value into the result vector.
Result = InsertElementInst::Create(Result, ExtractedElts[Idx],
- Context->getConstantInt(Type::Int32Ty, i, false),
+ ConstantInt::get(Type::Int32Ty, i, false),
"tmp");
InsertNewInstBefore(cast<Instruction>(Result), CI);
}
const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
if (!SrcTy->isSized() || !DstTy->isSized())
return false;
- if (TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
+ if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
return false;
return true;
}
Instruction *OldCall = CS.getInstruction();
// If the call and callee calling conventions don't match, this call must
// be unreachable, as the call is undefined.
- new StoreInst(Context->getConstantIntTrue(),
+ new StoreInst(Context->getTrue(),
Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)),
OldCall);
if (!OldCall->use_empty())
// This instruction is not reachable, just remove it. We insert a store to
// undef so that we know that this code is not reachable, despite the fact
// that we can't modify the CFG here.
- new StoreInst(Context->getConstantIntTrue(),
+ new StoreInst(Context->getTrue(),
Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)),
CS.getInstruction());
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
// Don't break the CFG, insert a dummy cond branch.
BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
- Context->getConstantIntTrue(), II);
+ Context->getTrue(), II);
}
return EraseInstFromFunction(*CS.getInstruction());
}
if (Callee->isDeclaration() &&
// Conversion is ok if changing from one pointer type to another or from
// a pointer to an integer of the same size.
- !((isa<PointerType>(OldRetTy) || OldRetTy == TD->getIntPtrType()) &&
- (isa<PointerType>(NewRetTy) || NewRetTy == TD->getIntPtrType())))
+ !((isa<PointerType>(OldRetTy) || !TD ||
+ OldRetTy == TD->getIntPtrType()) &&
+ (isa<PointerType>(NewRetTy) || !TD ||
+ NewRetTy == TD->getIntPtrType())))
return false; // Cannot transform this return value.
if (!Caller->use_empty() &&
// Converting from one pointer type to another or between a pointer and an
// integer of the same size is safe even if we do not have a body.
bool isConvertible = ActTy == ParamTy ||
- ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) &&
- (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType()));
+ (TD && ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) &&
+ (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType())));
if (Callee->isDeclaration() && !isConvertible) return false;
}
// If we are removing arguments to the function, emit an obnoxious warning...
if (FT->getNumParams() < NumActualArgs) {
if (!FT->isVarArg()) {
- cerr << "WARNING: While resolving call to function '"
- << Callee->getName() << "' arguments were dropped!\n";
+ errs() << "WARNING: While resolving call to function '"
+ << Callee->getName() << "' arguments were dropped!\n";
} else {
// Add all of the arguments in their promoted form to the arg list...
for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
if (NewRetTy == Type::VoidTy)
Caller->setName(""); // Void type should not have a name.
- const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),attrVec.end());
+ const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
+ attrVec.end());
Instruction *NC;
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
NestF->getType() == Context->getPointerTypeUnqual(NewFTy) ?
NestF : Context->getConstantExprBitCast(NestF,
Context->getPointerTypeUnqual(NewFTy));
- const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),NewAttrs.end());
+ const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
+ NewAttrs.end());
Instruction *NewCaller;
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
gep_type_iterator GTI = gep_type_begin(GEP);
for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end();
i != e; ++i, ++GTI) {
- if (isa<SequentialType>(*GTI)) {
+ if (TD && isa<SequentialType>(*GTI)) {
if (CastInst *CI = dyn_cast<CastInst>(*i)) {
if (CI->getOpcode() == Instruction::ZExt ||
CI->getOpcode() == Instruction::SExt) {
*i = Op;
MadeChange = true;
}
- } else if (TD->getTypeSizeInBits(Op->getType()) < TD->getPointerSizeInBits()) {
+ } else if (TD->getTypeSizeInBits(Op->getType())
+ < TD->getPointerSizeInBits()) {
if (Constant *C = dyn_cast<Constant>(Op)) {
*i = Context->getConstantExprSExt(C, TD->getIntPtrType());
MadeChange = true;
} else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
GO1 =
Context->getConstantExprIntegerCast(GO1C, SO1->getType(), true);
- } else {
+ } else if (TD) {
unsigned PS = TD->getPointerSizeInBits();
if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
// Convert GO1 to SO1's type.
// into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
- if (isa<ArrayType>(SrcElTy) &&
+ if (TD && isa<ArrayType>(SrcElTy) &&
TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
TD->getTypeAllocSize(ResElTy)) {
Value *Idx[2];
// (where tmp = 8*tmp2) into:
// getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
- if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
+ if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
uint64_t ArrayEltSize =
TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
if (ArrayEltSize == 1) {
NewIdx = GEP.getOperand(1);
Scale =
- Context->getConstantInt(cast<IntegerType>(NewIdx->getType()), 1);
+ ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
} else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
- NewIdx = Context->getConstantInt(CI->getType(), 1);
+ NewIdx = ConstantInt::get(CI->getType(), 1);
Scale = CI;
} else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
if (Inst->getOpcode() == Instruction::Shl &&
isa<ConstantInt>(Inst->getOperand(1))) {
ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
- Scale = Context->getConstantInt(cast<IntegerType>(Inst->getType()),
+ Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
1ULL << ShAmtVal);
NewIdx = Inst->getOperand(0);
} else if (Inst->getOpcode() == Instruction::Mul &&
// operation after making sure Scale doesn't have the sign bit set.
if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
Scale->getZExtValue() % ArrayEltSize == 0) {
- Scale = Context->getConstantInt(Scale->getType(),
+ Scale = ConstantInt::get(Scale->getType(),
Scale->getZExtValue() / ArrayEltSize);
if (Scale->getZExtValue() != 1) {
Constant *C =
/// into a gep of the original struct. This is important for SROA and alias
/// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
- if (!isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
+ if (TD &&
+ !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
// Determine how much the GEP moves the pointer. We are guaranteed to get
// a constant back from EmitGEPOffset.
ConstantInt *OffsetV =
}
}
- if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
+ if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
// If alloca'ing a zero byte object, replace the alloca with a null pointer.
// Note that we only do this for alloca's, because malloc should allocate
// and return a unique pointer, even for a zero byte allocation.
// free undef -> unreachable.
if (isa<UndefValue>(Op)) {
// Insert a new store to null because we cannot modify the CFG here.
- new StoreInst(Context->getConstantIntTrue(),
+ new StoreInst(Context->getTrue(),
Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), &FI);
return EraseInstFromFunction(FI);
}
SingleChar = 0;
StrVal = (StrVal << 8) | SingleChar;
}
- Value *NL = Context->getConstantInt(StrVal);
+ Value *NL = ConstantInt::get(*Context, StrVal);
return IC.ReplaceInstUsesWith(LI, NL);
}
}
SrcPTy = SrcTy->getElementType();
}
- if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) ||
+ if (IC.getTargetData() &&
+ (SrcPTy->isInteger() || isa<PointerType>(SrcPTy) ||
isa<VectorType>(SrcPTy)) &&
// Do not allow turning this into a load of an integer, which is then
// casted to a pointer, this pessimizes pointer analysis a lot.
(isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) &&
- IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
- IC.getTargetData().getTypeSizeInBits(DestPTy)) {
+ IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
+ IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
// Okay, we are casting from one integer or pointer type to another of
// the same size. Instead of casting the pointer before the load, cast
Value *Op = LI.getOperand(0);
// Attempt to improve the alignment.
- unsigned KnownAlign =
- GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
- if (KnownAlign >
- (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
- LI.getAlignment()))
- LI.setAlignment(KnownAlign);
+ if (TD) {
+ unsigned KnownAlign =
+ GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
+ if (KnownAlign >
+ (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
+ LI.getAlignment()))
+ LI.setAlignment(KnownAlign);
+ }
// load (cast X) --> cast (load X) iff safe
if (isa<CastInst>(Op))
if (GV->isConstant() && GV->hasDefinitiveInitializer())
if (Constant *V =
ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE,
- Context))
+ *Context))
return ReplaceInstUsesWith(LI, V);
if (CE->getOperand(0)->isNullValue()) {
// Insert a new store to null instruction before the load to indicate
// If the pointers point into different address spaces or if they point to
// values with different sizes, we can't do the transformation.
- if (SrcTy->getAddressSpace() !=
+ if (!IC.getTargetData() ||
+ SrcTy->getAddressSpace() !=
cast<PointerType>(CI->getType())->getAddressSpace() ||
- IC.getTargetData().getTypeSizeInBits(SrcPTy) !=
- IC.getTargetData().getTypeSizeInBits(DestPTy))
+ IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
+ IC.getTargetData()->getTypeSizeInBits(DestPTy))
return 0;
// Okay, we are casting from one integer or pointer type to another of
}
// Attempt to improve the alignment.
- unsigned KnownAlign =
- GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()));
- if (KnownAlign >
- (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
- SI.getAlignment()))
- SI.setAlignment(KnownAlign);
+ if (TD) {
+ unsigned KnownAlign =
+ GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()));
+ if (KnownAlign >
+ (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) :
+ SI.getAlignment()))
+ SI.setAlignment(KnownAlign);
+ }
// Do really simple DSE, to catch cases where there are several consecutive
// stores to the same location, separated by a few arithmetic operations. This
bool isConstantElt = isa<ConstantInt>(EI.getOperand(1));
if (CheapToScalarize(BO, isConstantElt)) {
ExtractElementInst *newEI0 =
- new ExtractElementInst(BO->getOperand(0), EI.getOperand(1),
+ ExtractElementInst::Create(BO->getOperand(0), EI.getOperand(1),
EI.getName()+".lhs");
ExtractElementInst *newEI1 =
- new ExtractElementInst(BO->getOperand(1), EI.getOperand(1),
+ ExtractElementInst::Create(BO->getOperand(1), EI.getOperand(1),
EI.getName()+".rhs");
InsertNewInstBefore(newEI0, EI);
InsertNewInstBefore(newEI1, EI);
} else {
return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
}
- return new ExtractElementInst(Src,
- Context->getConstantInt(Type::Int32Ty, SrcIdx, false));
+ return ExtractElementInst::Create(Src,
+ ConstantInt::get(Type::Int32Ty, SrcIdx, false));
}
}
// FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
return true;
} else if (V == LHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
+ Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
return true;
} else if (V == RHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(Context->getConstantInt(Type::Int32Ty, i+NumElts));
+ Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
return true;
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
// If this is an insert of an extract from some other vector, include it.
// If so, update the mask to reflect the inserted value.
if (EI->getOperand(0) == LHS) {
Mask[InsertedIdx % NumElts] =
- Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
+ ConstantInt::get(Type::Int32Ty, ExtractedIdx);
} else {
assert(EI->getOperand(0) == RHS);
Mask[InsertedIdx % NumElts] =
- Context->getConstantInt(Type::Int32Ty, ExtractedIdx+NumElts);
+ ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
}
return true;
Mask.assign(NumElts, Context->getUndef(Type::Int32Ty));
return V;
} else if (isa<ConstantAggregateZero>(V)) {
- Mask.assign(NumElts, Context->getConstantInt(Type::Int32Ty, 0));
+ Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
return V;
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
// If this is an insert of an extract from some other vector, include it.
RHS = EI->getOperand(0);
Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
Mask[InsertedIdx % NumElts] =
- Context->getConstantInt(Type::Int32Ty, NumElts+ExtractedIdx);
+ ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
return V;
}
// Everything but the extracted element is replaced with the RHS.
for (unsigned i = 0; i != NumElts; ++i) {
if (i != InsertedIdx)
- Mask[i] = Context->getConstantInt(Type::Int32Ty, NumElts+i);
+ Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
}
return V;
}
// Otherwise, can't do anything fancy. Return an identity vector.
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
+ Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
return V;
}
Mask.assign(NumVectorElts, Context->getUndef(Type::Int32Ty));
else {
assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
- Mask.assign(NumVectorElts, Context->getConstantInt(Type::Int32Ty,
+ Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
NumVectorElts));
}
Mask[InsertedIdx] =
- Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
+ ConstantInt::get(Type::Int32Ty, ExtractedIdx);
return new ShuffleVectorInst(EI->getOperand(0), VecOp,
Context->getConstantVector(Mask));
}
Elts.push_back(Context->getUndef(Type::Int32Ty));
} else {
Mask[i] = Mask[i] % e; // Force to LHS.
- Elts.push_back(Context->getConstantInt(Type::Int32Ty, Mask[i]));
+ Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
}
}
}
if (NewMask[i] >= LHSInNElts*2) {
Elts.push_back(Context->getUndef(Type::Int32Ty));
} else {
- Elts.push_back(Context->getConstantInt(Type::Int32Ty, NewMask[i]));
+ Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
}
}
return new ShuffleVectorInst(LHSSVI->getOperand(0),
bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
bool Changed = false;
- TD = &getAnalysis<TargetData>();
+ TD = getAnalysisIfAvailable<TargetData>();
- DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
- << F.getNameStr() << "\n");
+ DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
+ << F.getNameStr() << "\n");
{
// Do a depth-first traversal of the function, populate the worklist with
bool InstCombiner::runOnFunction(Function &F) {
MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
+ Context = &F.getContext();
bool EverMadeChange = false;