#define DEBUG_TYPE "instcombine"
#include "llvm/Transforms/Scalar.h"
#include "llvm/IntrinsicInst.h"
+#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h"
static char ID; // Pass identification, replacement for typeid
InstCombiner() : FunctionPass(&ID) {}
+ LLVMContext *getContext() { return Context; }
+
/// AddToWorkList - Add the specified instruction to the worklist if it
/// isn't already in it.
void AddToWorkList(Instruction *I) {
if (Instruction *Op = dyn_cast<Instruction>(*i)) {
AddToWorkList(Op);
// Set the operand to undef to drop the use.
- *i = UndefValue::get(Op->getType());
+ *i = Context->getUndef(Op->getType());
}
return R;
// otherwise - Change was made, replace I with returned instruction
//
Instruction *visitAdd(BinaryOperator &I);
+ Instruction *visitFAdd(BinaryOperator &I);
Instruction *visitSub(BinaryOperator &I);
+ Instruction *visitFSub(BinaryOperator &I);
Instruction *visitMul(BinaryOperator &I);
+ Instruction *visitFMul(BinaryOperator &I);
Instruction *visitURem(BinaryOperator &I);
Instruction *visitSRem(BinaryOperator &I);
Instruction *visitFRem(BinaryOperator &I);
Instruction *visitFPToSI(FPToSIInst &FI);
Instruction *visitUIToFP(CastInst &CI);
Instruction *visitSIToFP(CastInst &CI);
- Instruction *visitPtrToInt(CastInst &CI);
+ Instruction *visitPtrToInt(PtrToIntInst &CI);
Instruction *visitIntToPtr(IntToPtrInst &CI);
Instruction *visitBitCast(BitCastInst &CI);
Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
Instruction *FI);
+ Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*);
Instruction *visitSelectInst(SelectInst &SI);
Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
Instruction *visitCallInst(CallInst &CI);
Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
bool DoXform = true);
bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS);
+ DbgDeclareInst *hasOneUsePlusDeclare(Value *V);
+
public:
// InsertNewInstBefore - insert an instruction New before instruction Old
if (V->getType() == Ty) return V;
if (Constant *CV = dyn_cast<Constant>(V))
- return ConstantExpr::getCast(opc, CV, Ty);
+ return Context->getConstantExprCast(opc, CV, Ty);
Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos);
AddToWorkList(C);
} else {
// If we are replacing the instruction with itself, this must be in a
// segment of unreachable code, so just clobber the instruction.
- I.replaceAllUsesWith(UndefValue::get(I.getType()));
+ I.replaceAllUsesWith(Context->getUndef(I.getType()));
return &I;
}
}
- // UpdateValueUsesWith - This method is to be used when an value is
- // found to be replacable with another preexisting expression or was
- // updated. Here we add all uses of I to the worklist, replace all uses of
- // I with the new value (unless the instruction was just updated), then
- // return true, so that the inst combiner will know that I was modified.
- //
- bool UpdateValueUsesWith(Value *Old, Value *New) {
- AddUsersToWorkList(*Old); // Add all modified instrs to worklist
- if (Old != New)
- Old->replaceAllUsesWith(New);
- if (Instruction *I = dyn_cast<Instruction>(Old))
- AddToWorkList(I);
- if (Instruction *I = dyn_cast<Instruction>(New))
- AddToWorkList(I);
- return true;
- }
-
// EraseInstFromFunction - When dealing with an instruction that has side
// effects or produces a void value, we can't rely on DCE to delete the
// instruction. Instead, visit methods should return the value returned by
/// most-complex to least-complex order.
bool SimplifyCompare(CmpInst &I);
- /// SimplifyDemandedBits - Attempts to replace V with a simpler value based
- /// on the demanded bits.
- bool SimplifyDemandedBits(Value *V, APInt DemandedMask,
+ /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value
+ /// based on the demanded bits.
+ Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
+ APInt& KnownZero, APInt& KnownOne,
+ unsigned Depth);
+ bool SimplifyDemandedBits(Use &U, APInt DemandedMask,
APInt& KnownZero, APInt& KnownOne,
- unsigned Depth = 0);
-
- Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
- uint64_t &UndefElts, unsigned Depth = 0);
+ unsigned Depth=0);
+
+ /// SimplifyDemandedInstructionBits - Inst is an integer instruction that
+ /// SimplifyDemandedBits knows about. See if the instruction has any
+ /// properties that allow us to simplify its operands.
+ bool SimplifyDemandedInstructionBits(Instruction &Inst);
+
+ Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
+ APInt& UndefElts, unsigned Depth = 0);
// FoldOpIntoPhi - Given a binary operator or cast instruction which has a
// PHI node as operand #0, see if we can fold the instruction into the PHI
Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
- bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
- unsigned CastOpc,
- int &NumCastsRemoved);
+ bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
+ unsigned CastOpc, int &NumCastsRemoved);
unsigned GetOrEnforceKnownAlignment(Value *V,
unsigned PrefAlign = 0);
// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
static unsigned getComplexity(Value *V) {
if (isa<Instruction>(V)) {
- if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
+ if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
+ BinaryOperator::isNot(V))
return 3;
return 4;
}
Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
Instruction::CastOps secondOp = Instruction::CastOps(opcode);
- return Instruction::CastOps(
- CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
- DstTy, TD->getIntPtrType()));
+ unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
+ DstTy, TD->getIntPtrType());
+
+ // We don't want to form an inttoptr or ptrtoint that converts to an integer
+ // type that differs from the pointer size.
+ if ((Res == Instruction::IntToPtr && SrcTy != TD->getIntPtrType()) ||
+ (Res == Instruction::PtrToInt && DstTy != TD->getIntPtrType()))
+ Res = 0;
+
+ return Instruction::CastOps(Res);
}
/// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
if (isa<Constant>(I.getOperand(1))) {
- Constant *Folded = ConstantExpr::get(I.getOpcode(),
+ Constant *Folded = Context->getConstantExpr(I.getOpcode(),
cast<Constant>(I.getOperand(1)),
cast<Constant>(Op->getOperand(1)));
I.setOperand(0, Op->getOperand(0));
Constant *C2 = cast<Constant>(Op1->getOperand(1));
// Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
- Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
+ Constant *Folded = Context->getConstantExpr(I.getOpcode(), C1, C2);
Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
Op1->getOperand(0),
Op1->getName(), &I);
// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
// if the LHS is a constant zero (which is the 'negate' form).
//
-static inline Value *dyn_castNegVal(Value *V) {
+static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) {
if (BinaryOperator::isNeg(V))
return BinaryOperator::getNegArgument(V);
// Constants can be considered to be negated values if they can be folded.
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
- return ConstantExpr::getNeg(C);
+ return Context->getConstantExprNeg(C);
if (ConstantVector *C = dyn_cast<ConstantVector>(V))
if (C->getType()->getElementType()->isInteger())
- return ConstantExpr::getNeg(C);
+ return Context->getConstantExprNeg(C);
return 0;
}
-static inline Value *dyn_castNotVal(Value *V) {
+// dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
+// instruction if the LHS is a constant negative zero (which is the 'negate'
+// form).
+//
+static inline Value *dyn_castFNegVal(Value *V, LLVMContext *Context) {
+ if (BinaryOperator::isFNeg(V))
+ return BinaryOperator::getFNegArgument(V);
+
+ // Constants can be considered to be negated values if they can be folded.
+ if (ConstantFP *C = dyn_cast<ConstantFP>(V))
+ return Context->getConstantExprFNeg(C);
+
+ if (ConstantVector *C = dyn_cast<ConstantVector>(V))
+ if (C->getType()->getElementType()->isFloatingPoint())
+ return Context->getConstantExprFNeg(C);
+
+ return 0;
+}
+
+static inline Value *dyn_castNotVal(Value *V, LLVMContext *Context) {
if (BinaryOperator::isNot(V))
return BinaryOperator::getNotArgument(V);
// Constants can be considered to be not'ed values...
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
- return ConstantInt::get(~C->getValue());
+ return Context->getConstantInt(~C->getValue());
return 0;
}
// non-constant operand of the multiply, and set CST to point to the multiplier.
// Otherwise, return null.
//
-static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
+static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST,
+ LLVMContext *Context) {
if (V->hasOneUse() && V->getType()->isInteger())
if (Instruction *I = dyn_cast<Instruction>(V)) {
if (I->getOpcode() == Instruction::Mul)
// The multiplier is really 1 << CST.
uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
uint32_t CSTVal = CST->getLimitedValue(BitWidth);
- CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal));
+ CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal));
return I->getOperand(0);
}
}
}
/// AddOne - Add one to a ConstantInt
-static ConstantInt *AddOne(ConstantInt *C) {
- APInt Val(C->getValue());
- return ConstantInt::get(++Val);
+static Constant *AddOne(Constant *C, LLVMContext *Context) {
+ return Context->getConstantExprAdd(C,
+ Context->getConstantInt(C->getType(), 1));
}
/// SubOne - Subtract one from a ConstantInt
-static ConstantInt *SubOne(ConstantInt *C) {
- APInt Val(C->getValue());
- return ConstantInt::get(--Val);
-}
-/// Add - Add two ConstantInts together
-static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) {
- return ConstantInt::get(C1->getValue() + C2->getValue());
-}
-/// And - Bitwise AND two ConstantInts together
-static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) {
- return ConstantInt::get(C1->getValue() & C2->getValue());
-}
-/// Subtract - Subtract one ConstantInt from another
-static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) {
- return ConstantInt::get(C1->getValue() - C2->getValue());
-}
-/// Multiply - Multiply two ConstantInts together
-static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) {
- return ConstantInt::get(C1->getValue() * C2->getValue());
+static Constant *SubOne(ConstantInt *C, LLVMContext *Context) {
+ return Context->getConstantExprSub(C,
+ Context->getConstantInt(C->getType(), 1));
}
/// MultiplyOverflows - True if the multiply can not be expressed in an int
/// this size.
-static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
+static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign,
+ LLVMContext *Context) {
uint32_t W = C1->getBitWidth();
APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
if (sign) {
/// are any bits set in the constant that are not demanded. If so, shrink the
/// constant and return true.
static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
- APInt Demanded) {
+ APInt Demanded, LLVMContext *Context) {
assert(I && "No instruction?");
assert(OpNo < I->getNumOperands() && "Operand index too large");
// This instruction is producing bits that are not demanded. Shrink the RHS.
Demanded &= OpC->getValue();
- I->setOperand(OpNo, ConstantInt::get(Demanded));
+ I->setOperand(OpNo, Context->getConstantInt(Demanded));
return true;
}
// set of known zero and one bits, compute the maximum and minimum values that
// could have the specified known zero and known one bits, returning them in
// min/max.
-static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty,
- const APInt& KnownZero,
+static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
const APInt& KnownOne,
APInt& Min, APInt& Max) {
- uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- assert(KnownZero.getBitWidth() == BitWidth &&
- KnownOne.getBitWidth() == BitWidth &&
- Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth &&
- "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
+ assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
+ KnownZero.getBitWidth() == Min.getBitWidth() &&
+ KnownZero.getBitWidth() == Max.getBitWidth() &&
+ "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
APInt UnknownBits = ~(KnownZero|KnownOne);
// The minimum value is when all unknown bits are zeros, EXCEPT for the sign
Min = KnownOne;
Max = KnownOne|UnknownBits;
- if (UnknownBits[BitWidth-1]) { // Sign bit is unknown
- Min.set(BitWidth-1);
- Max.clear(BitWidth-1);
+ if (UnknownBits.isNegative()) { // Sign bit is unknown
+ Min.set(Min.getBitWidth()-1);
+ Max.clear(Max.getBitWidth()-1);
}
}
// a set of known zero and one bits, compute the maximum and minimum values that
// could have the specified known zero and known one bits, returning them in
// min/max.
-static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty,
- const APInt &KnownZero,
+static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
const APInt &KnownOne,
APInt &Min, APInt &Max) {
- uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth;
- assert(KnownZero.getBitWidth() == BitWidth &&
- KnownOne.getBitWidth() == BitWidth &&
- Min.getBitWidth() == BitWidth && Max.getBitWidth() &&
+ assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
+ KnownZero.getBitWidth() == Min.getBitWidth() &&
+ KnownZero.getBitWidth() == Max.getBitWidth() &&
"Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
APInt UnknownBits = ~(KnownZero|KnownOne);
Max = KnownOne|UnknownBits;
}
-/// SimplifyDemandedBits - This function attempts to replace V with a simpler
-/// value based on the demanded bits. When this function is called, it is known
+/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
+/// SimplifyDemandedBits knows about. See if the instruction has any
+/// properties that allow us to simplify its operands.
+bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
+ unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
+ APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
+ APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
+
+ Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
+ KnownZero, KnownOne, 0);
+ if (V == 0) return false;
+ if (V == &Inst) return true;
+ ReplaceInstUsesWith(Inst, V);
+ return true;
+}
+
+/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
+/// specified instruction operand if possible, updating it in place. It returns
+/// true if it made any change and false otherwise.
+bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
+ APInt &KnownZero, APInt &KnownOne,
+ unsigned Depth) {
+ Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
+ KnownZero, KnownOne, Depth);
+ if (NewVal == 0) return false;
+ U.set(NewVal);
+ return true;
+}
+
+
+/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
+/// value based on the demanded bits. When this function is called, it is known
/// that only the bits set in DemandedMask of the result of V are ever used
/// downstream. Consequently, depending on the mask and V, it may be possible
/// to replace V with a constant or one of its operands. In such cases, this
/// function does the replacement and returns true. In all other cases, it
/// returns false after analyzing the expression and setting KnownOne and known
-/// to be one in the expression. KnownZero contains all the bits that are known
+/// to be one in the expression. KnownZero contains all the bits that are known
/// to be zero in the expression. These are provided to potentially allow the
/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
/// the expression. KnownOne and KnownZero always follow the invariant that
/// the bits in KnownOne and KnownZero may only be accurate for those bits set
/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
/// and KnownOne must all be the same.
-bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask,
- APInt& KnownZero, APInt& KnownOne,
- unsigned Depth) {
+///
+/// This returns null if it did not change anything and it permits no
+/// simplification. This returns V itself if it did some simplification of V's
+/// operands based on the information about what bits are demanded. This returns
+/// some other non-null value if it found out that V is equal to another value
+/// in the context where the specified bits are demanded, but not for all users.
+Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
+ APInt &KnownZero, APInt &KnownOne,
+ unsigned Depth) {
assert(V != 0 && "Null pointer of Value???");
assert(Depth <= 6 && "Limit Search Depth");
uint32_t BitWidth = DemandedMask.getBitWidth();
- const IntegerType *VTy = cast<IntegerType>(V->getType());
- assert(VTy->getBitWidth() == BitWidth &&
- KnownZero.getBitWidth() == BitWidth &&
+ const Type *VTy = V->getType();
+ assert((TD || !isa<PointerType>(VTy)) &&
+ "SimplifyDemandedBits needs to know bit widths!");
+ assert((!TD || TD->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
+ (!VTy->isIntOrIntVector() ||
+ VTy->getScalarSizeInBits() == BitWidth) &&
+ KnownZero.getBitWidth() == BitWidth &&
KnownOne.getBitWidth() == BitWidth &&
- "Value *V, DemandedMask, KnownZero and KnownOne \
- must have same BitWidth");
+ "Value *V, DemandedMask, KnownZero and KnownOne "
+ "must have same BitWidth");
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
// We know all of the bits for a constant!
KnownOne = CI->getValue() & DemandedMask;
KnownZero = ~KnownOne & DemandedMask;
- return false;
+ return 0;
}
-
- KnownZero.clear();
+ if (isa<ConstantPointerNull>(V)) {
+ // We know all of the bits for a constant!
+ KnownOne.clear();
+ KnownZero = DemandedMask;
+ return 0;
+ }
+
+ KnownZero.clear();
KnownOne.clear();
- if (!V->hasOneUse()) { // Other users may use these bits.
- if (Depth != 0) { // Not at the root.
- // Just compute the KnownZero/KnownOne bits to simplify things downstream.
- ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth);
- return false;
- }
- // If this is the root being simplified, allow it to have multiple uses,
- // just set the DemandedMask to all bits.
- DemandedMask = APInt::getAllOnesValue(BitWidth);
- } else if (DemandedMask == 0) { // Not demanding any bits from V.
- if (V != UndefValue::get(VTy))
- return UpdateValueUsesWith(V, UndefValue::get(VTy));
- return false;
- } else if (Depth == 6) { // Limit search depth.
- return false;
+ if (DemandedMask == 0) { // Not demanding any bits from V.
+ if (isa<UndefValue>(V))
+ return 0;
+ return Context->getUndef(VTy);
}
- Instruction *I = dyn_cast<Instruction>(V);
- if (!I) return false; // Only analyze instructions.
-
+ if (Depth == 6) // Limit search depth.
+ return 0;
+
APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne;
+
+ Instruction *I = dyn_cast<Instruction>(V);
+ if (!I) {
+ ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
+ return 0; // Only analyze instructions.
+ }
+
+ // If there are multiple uses of this value and we aren't at the root, then
+ // we can't do any simplifications of the operands, because DemandedMask
+ // only reflects the bits demanded by *one* of the users.
+ if (Depth != 0 && !I->hasOneUse()) {
+ // Despite the fact that we can't simplify this instruction in all User's
+ // context, we can at least compute the knownzero/knownone bits, and we can
+ // do simplifications that apply to *just* the one user if we know that
+ // this instruction has a simpler value in that context.
+ if (I->getOpcode() == Instruction::And) {
+ // If either the LHS or the RHS are Zero, the result is zero.
+ ComputeMaskedBits(I->getOperand(1), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1);
+ ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
+ LHSKnownZero, LHSKnownOne, Depth+1);
+
+ // If all of the demanded bits are known 1 on one side, return the other.
+ // These bits cannot contribute to the result of the 'and' in this
+ // context.
+ if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
+ (DemandedMask & ~LHSKnownZero))
+ return I->getOperand(0);
+ if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
+ (DemandedMask & ~RHSKnownZero))
+ return I->getOperand(1);
+
+ // If all of the demanded bits in the inputs are known zeros, return zero.
+ if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
+ return Context->getNullValue(VTy);
+
+ } else if (I->getOpcode() == Instruction::Or) {
+ // We can simplify (X|Y) -> X or Y in the user's context if we know that
+ // only bits from X or Y are demanded.
+
+ // If either the LHS or the RHS are One, the result is One.
+ ComputeMaskedBits(I->getOperand(1), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1);
+ ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne,
+ LHSKnownZero, LHSKnownOne, Depth+1);
+
+ // If all of the demanded bits are known zero on one side, return the
+ // other. These bits cannot contribute to the result of the 'or' in this
+ // context.
+ if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
+ (DemandedMask & ~LHSKnownOne))
+ return I->getOperand(0);
+ if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
+ (DemandedMask & ~RHSKnownOne))
+ return I->getOperand(1);
+
+ // If all of the potentially set bits on one side are known to be set on
+ // the other side, just use the 'other' side.
+ if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
+ (DemandedMask & (~RHSKnownZero)))
+ return I->getOperand(0);
+ if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
+ (DemandedMask & (~LHSKnownZero)))
+ return I->getOperand(1);
+ }
+
+ // Compute the KnownZero/KnownOne bits to simplify things downstream.
+ ComputeMaskedBits(I, DemandedMask, KnownZero, KnownOne, Depth);
+ return 0;
+ }
+
+ // If this is the root being simplified, allow it to have multiple uses,
+ // just set the DemandedMask to all bits so that we can try to simplify the
+ // operands. This allows visitTruncInst (for example) to simplify the
+ // operand of a trunc without duplicating all the logic below.
+ if (Depth == 0 && !V->hasOneUse())
+ DemandedMask = APInt::getAllOnesValue(BitWidth);
+
switch (I->getOpcode()) {
default:
- ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
+ ComputeMaskedBits(I, DemandedMask, RHSKnownZero, RHSKnownOne, Depth);
break;
case Instruction::And:
// If either the LHS or the RHS are Zero, the result is zero.
- if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
- RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
-
- // If something is known zero on the RHS, the bits aren't demanded on the
- // LHS.
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero,
+ if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- assert((LHSKnownZero & LHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
+ assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
// If all of the demanded bits are known 1 on one side, return the other.
// These bits cannot contribute to the result of the 'and'.
if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
(DemandedMask & ~LHSKnownZero))
- return UpdateValueUsesWith(I, I->getOperand(0));
+ return I->getOperand(0);
if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
(DemandedMask & ~RHSKnownZero))
- return UpdateValueUsesWith(I, I->getOperand(1));
+ return I->getOperand(1);
// If all of the demanded bits in the inputs are known zeros, return zero.
if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
- return UpdateValueUsesWith(I, Constant::getNullValue(VTy));
+ return Context->getNullValue(VTy);
// If the RHS is a constant, see if we can simplify it.
- if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
- return UpdateValueUsesWith(I, I);
+ if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero, Context))
+ return I;
// Output known-1 bits are only known if set in both the LHS & RHS.
RHSKnownOne &= LHSKnownOne;
break;
case Instruction::Or:
// If either the LHS or the RHS are One, the result is One.
- if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
- RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
- // If something is known one on the RHS, the bits aren't demanded on the
- // LHS.
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne,
+ if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- assert((LHSKnownZero & LHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
+ assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
// If all of the demanded bits are known zero on one side, return the other.
// These bits cannot contribute to the result of the 'or'.
if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
(DemandedMask & ~LHSKnownOne))
- return UpdateValueUsesWith(I, I->getOperand(0));
+ return I->getOperand(0);
if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
(DemandedMask & ~RHSKnownOne))
- return UpdateValueUsesWith(I, I->getOperand(1));
+ return I->getOperand(1);
// If all of the potentially set bits on one side are known to be set on
// the other side, just use the 'other' side.
if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
(DemandedMask & (~RHSKnownZero)))
- return UpdateValueUsesWith(I, I->getOperand(0));
+ return I->getOperand(0);
if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
(DemandedMask & (~LHSKnownZero)))
- return UpdateValueUsesWith(I, I->getOperand(1));
+ return I->getOperand(1);
// If the RHS is a constant, see if we can simplify it.
- if (ShrinkDemandedConstant(I, 1, DemandedMask))
- return UpdateValueUsesWith(I, I);
+ if (ShrinkDemandedConstant(I, 1, DemandedMask, Context))
+ return I;
// Output known-0 bits are only known if clear in both the LHS & RHS.
RHSKnownZero &= LHSKnownZero;
RHSKnownOne |= LHSKnownOne;
break;
case Instruction::Xor: {
- if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
- RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- assert((LHSKnownZero & LHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
+ assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
// If all of the demanded bits are known zero on one side, return the other.
// These bits cannot contribute to the result of the 'xor'.
if ((DemandedMask & RHSKnownZero) == DemandedMask)
- return UpdateValueUsesWith(I, I->getOperand(0));
+ return I->getOperand(0);
if ((DemandedMask & LHSKnownZero) == DemandedMask)
- return UpdateValueUsesWith(I, I->getOperand(1));
+ return I->getOperand(1);
// Output known-0 bits are known if clear or set in both the LHS & RHS.
APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) |
Instruction *Or =
BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
I->getName());
- InsertNewInstBefore(Or, *I);
- return UpdateValueUsesWith(I, Or);
+ return InsertNewInstBefore(Or, *I);
}
// If all of the demanded bits on one side are known, and all of the set
if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
// all known
if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
- Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask);
+ Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask);
Instruction *And =
BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
- InsertNewInstBefore(And, *I);
- return UpdateValueUsesWith(I, And);
+ return InsertNewInstBefore(And, *I);
}
}
// If the RHS is a constant, see if we can simplify it.
// FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
- if (ShrinkDemandedConstant(I, 1, DemandedMask))
- return UpdateValueUsesWith(I, I);
+ if (ShrinkDemandedConstant(I, 1, DemandedMask, Context))
+ return I;
RHSKnownZero = KnownZeroOut;
RHSKnownOne = KnownOneOut;
break;
}
case Instruction::Select:
- if (SimplifyDemandedBits(I->getOperand(2), DemandedMask,
- RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- if (SimplifyDemandedBits(I->getOperand(1), DemandedMask,
+ if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
+ RHSKnownZero, RHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
- assert((LHSKnownZero & LHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
+ assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
// If the operands are constants, see if we can simplify them.
- if (ShrinkDemandedConstant(I, 1, DemandedMask))
- return UpdateValueUsesWith(I, I);
- if (ShrinkDemandedConstant(I, 2, DemandedMask))
- return UpdateValueUsesWith(I, I);
+ if (ShrinkDemandedConstant(I, 1, DemandedMask, Context) ||
+ ShrinkDemandedConstant(I, 2, DemandedMask, Context))
+ return I;
// Only known if known in both the LHS and RHS.
RHSKnownOne &= LHSKnownOne;
RHSKnownZero &= LHSKnownZero;
break;
case Instruction::Trunc: {
- uint32_t truncBf =
- cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth();
+ unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
DemandedMask.zext(truncBf);
RHSKnownZero.zext(truncBf);
RHSKnownOne.zext(truncBf);
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
+ return I;
DemandedMask.trunc(BitWidth);
RHSKnownZero.trunc(BitWidth);
RHSKnownOne.trunc(BitWidth);
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
break;
}
case Instruction::BitCast:
- if (!I->getOperand(0)->getType()->isInteger())
+ if (!I->getOperand(0)->getType()->isIntOrIntVector())
+ return false; // vector->int or fp->int?
+
+ if (const VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
+ if (const VectorType *SrcVTy =
+ dyn_cast<VectorType>(I->getOperand(0)->getType())) {
+ if (DstVTy->getNumElements() != SrcVTy->getNumElements())
+ // Don't touch a bitcast between vectors of different element counts.
+ return false;
+ } else
+ // Don't touch a scalar-to-vector bitcast.
+ return false;
+ } else if (isa<VectorType>(I->getOperand(0)->getType()))
+ // Don't touch a vector-to-scalar bitcast.
return false;
-
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
break;
case Instruction::ZExt: {
// Compute the bits in the result that are not present in the input.
- const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
- uint32_t SrcBitWidth = SrcTy->getBitWidth();
+ unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
DemandedMask.trunc(SrcBitWidth);
RHSKnownZero.trunc(SrcBitWidth);
RHSKnownOne.trunc(SrcBitWidth);
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
+ return I;
DemandedMask.zext(BitWidth);
RHSKnownZero.zext(BitWidth);
RHSKnownOne.zext(BitWidth);
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
// The top bits are known to be zero.
RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
break;
}
case Instruction::SExt: {
// Compute the bits in the result that are not present in the input.
- const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType());
- uint32_t SrcBitWidth = SrcTy->getBitWidth();
+ unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
APInt InputDemandedBits = DemandedMask &
APInt::getLowBitsSet(BitWidth, SrcBitWidth);
InputDemandedBits.trunc(SrcBitWidth);
RHSKnownZero.trunc(SrcBitWidth);
RHSKnownOne.trunc(SrcBitWidth);
- if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
+ if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
+ return I;
InputDemandedBits.zext(BitWidth);
RHSKnownZero.zext(BitWidth);
RHSKnownOne.zext(BitWidth);
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
// If the sign bit of the input is known set or clear, then we know the
// top bits of the result.
// If the input sign bit is known zero, or if the NewBits are not demanded
// convert this into a zero extension.
- if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits)
- {
+ if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
// Convert to ZExt cast
- CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I);
- return UpdateValueUsesWith(I, NewCast);
+ CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
+ return InsertNewInstBefore(NewCast, *I);
} else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set
RHSKnownOne |= NewBits;
}
// Figure out what the input bits are. If the top bits of the and result
// are not demanded, then the add doesn't demand them from its input
// either.
- uint32_t NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countLeadingZeros();
// If there is a constant on the RHS, there are a variety of xformations
// we can do.
APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
// Find information about known zero/one bits in the input.
- if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits,
+ if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
+ return I;
// If the RHS of the add has bits set that can't affect the input, reduce
// the constant.
- if (ShrinkDemandedConstant(I, 1, InDemandedBits))
- return UpdateValueUsesWith(I, I);
+ if (ShrinkDemandedConstant(I, 1, InDemandedBits, Context))
+ return I;
// Avoid excess work.
if (LHSKnownZero == 0 && LHSKnownOne == 0)
Instruction *Or =
BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
I->getName());
- InsertNewInstBefore(Or, *I);
- return UpdateValueUsesWith(I, Or);
+ return InsertNewInstBefore(Or, *I);
}
// We can say something about the output known-zero and known-one bits,
// To compute this, we first compute the potential carry bits. These are
// the bits which may be modified. I'm not aware of a better way to do
// this scan.
- const APInt& RHSVal = RHS->getValue();
+ const APInt &RHSVal = RHS->getValue();
APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
// Now that we know which bits have carries, compute the known-1/0 sets.
// Right fill the mask of bits for this ADD to demand the most
// significant bit and all those below it.
APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
- if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
+ LHSKnownZero, LHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
- LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
+ return I;
}
}
break;
// significant bit and all those below it.
uint32_t NLZ = DemandedMask.countLeadingZeros();
APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
- if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
+ LHSKnownZero, LHSKnownOne, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
- if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps,
- LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
+ return I;
}
// Otherwise just hand the sub off to ComputeMaskedBits to fill in
// the known zeros and ones.
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
RHSKnownZero <<= ShiftAmt;
RHSKnownOne <<= ShiftAmt;
// low bits known zero.
// Unsigned shift right.
APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt);
if (ShiftAmt) {
// the shift amount is >= the size of the datatype, which is undefined.
if (DemandedMask == 1) {
// Perform the logical shift right.
- Value *NewVal = BinaryOperator::CreateLShr(
+ Instruction *NewVal = BinaryOperator::CreateLShr(
I->getOperand(0), I->getOperand(1), I->getName());
- InsertNewInstBefore(cast<Instruction>(NewVal), *I);
- return UpdateValueUsesWith(I, NewVal);
+ return InsertNewInstBefore(NewVal, *I);
}
// If the sign bit is the only bit demanded by this ashr, then there is no
// need to do it, the shift doesn't change the high bit.
if (DemandedMask.isSignBit())
- return UpdateValueUsesWith(I, I->getOperand(0));
+ return I->getOperand(0);
if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
uint32_t ShiftAmt = SA->getLimitedValue(BitWidth);
// demanded.
if (DemandedMask.countLeadingZeros() <= ShiftAmt)
DemandedMaskIn.set(BitWidth-1);
- if (SimplifyDemandedBits(I->getOperand(0),
- DemandedMaskIn,
+ if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
RHSKnownZero, RHSKnownOne, Depth+1))
- return true;
- assert((RHSKnownZero & RHSKnownOne) == 0 &&
- "Bits known to be one AND zero?");
+ return I;
+ assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
// Compute the new bits that are at the top now.
APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt);
if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] ||
(HighBits & ~DemandedMask) == HighBits) {
// Perform the logical shift right.
- Value *NewVal = BinaryOperator::CreateLShr(
+ Instruction *NewVal = BinaryOperator::CreateLShr(
I->getOperand(0), SA, I->getName());
- InsertNewInstBefore(cast<Instruction>(NewVal), *I);
- return UpdateValueUsesWith(I, NewVal);
+ return InsertNewInstBefore(NewVal, *I);
} else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one.
RHSKnownOne |= HighBits;
}
if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
APInt RA = Rem->getValue().abs();
if (RA.isPowerOf2()) {
- if (DemandedMask.ule(RA)) // srem won't affect demanded bits
- return UpdateValueUsesWith(I, I->getOperand(0));
+ if (DemandedMask.ult(RA)) // srem won't affect demanded bits
+ return I->getOperand(0);
APInt LowBits = RA - 1;
APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
- if (SimplifyDemandedBits(I->getOperand(0), Mask2,
+ if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
LHSKnownZero, LHSKnownOne, Depth+1))
- return true;
+ return I;
if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
LHSKnownZero |= ~LowBits;
KnownZero |= LHSKnownZero & DemandedMask;
- assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
+ assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
}
}
break;
case Instruction::URem: {
APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
APInt AllOnes = APInt::getAllOnesValue(BitWidth);
- if (SimplifyDemandedBits(I->getOperand(0), AllOnes,
+ if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
+ KnownZero2, KnownOne2, Depth+1) ||
+ SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
KnownZero2, KnownOne2, Depth+1))
- return true;
-
- uint32_t Leaders = KnownZero2.countLeadingOnes();
- if (SimplifyDemandedBits(I->getOperand(1), AllOnes,
- KnownZero2, KnownOne2, Depth+1))
- return true;
+ return I;
+ unsigned Leaders = KnownZero2.countLeadingOnes();
Leaders = std::max(Leaders,
KnownZero2.countLeadingOnes());
KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
Instruction *NewVal;
if (InputBit > ResultBit)
NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
- ConstantInt::get(I->getType(), InputBit-ResultBit));
+ Context->getConstantInt(I->getType(), InputBit-ResultBit));
else
NewVal = BinaryOperator::CreateShl(I->getOperand(1),
- ConstantInt::get(I->getType(), ResultBit-InputBit));
+ Context->getConstantInt(I->getType(), ResultBit-InputBit));
NewVal->takeName(I);
- InsertNewInstBefore(NewVal, *I);
- return UpdateValueUsesWith(I, NewVal);
+ return InsertNewInstBefore(NewVal, *I);
}
// TODO: Could compute known zero/one bits based on the input.
// If the client is only demanding bits that we know, return the known
// constant.
- if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
- return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne));
+ if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
+ Constant *C = Context->getConstantInt(RHSKnownOne);
+ if (isa<PointerType>(V->getType()))
+ C = Context->getConstantExprIntToPtr(C, V->getType());
+ return C;
+ }
return false;
}
/// SimplifyDemandedVectorElts - The specified value produces a vector with
-/// 64 or fewer elements. DemandedElts contains the set of elements that are
+/// any number of elements. DemandedElts contains the set of elements that are
/// actually used by the caller. This method analyzes which elements of the
/// operand are undef and returns that information in UndefElts.
///
/// If the information about demanded elements can be used to simplify the
/// operation, the operation is simplified, then the resultant value is
/// returned. This returns null if no change was made.
-Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts,
- uint64_t &UndefElts,
+Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
+ APInt& UndefElts,
unsigned Depth) {
unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
- assert(VWidth <= 64 && "Vector too wide to analyze!");
- uint64_t EltMask = ~0ULL >> (64-VWidth);
+ APInt EltMask(APInt::getAllOnesValue(VWidth));
assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
if (isa<UndefValue>(V)) {
return 0;
} else if (DemandedElts == 0) { // If nothing is demanded, provide undef.
UndefElts = EltMask;
- return UndefValue::get(V->getType());
+ return Context->getUndef(V->getType());
}
UndefElts = 0;
if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
- Constant *Undef = UndefValue::get(EltTy);
+ Constant *Undef = Context->getUndef(EltTy);
std::vector<Constant*> Elts;
for (unsigned i = 0; i != VWidth; ++i)
- if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef.
+ if (!DemandedElts[i]) { // If not demanded, set to undef.
Elts.push_back(Undef);
- UndefElts |= (1ULL << i);
+ UndefElts.set(i);
} else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef.
Elts.push_back(Undef);
- UndefElts |= (1ULL << i);
+ UndefElts.set(i);
} else { // Otherwise, defined.
Elts.push_back(CP->getOperand(i));
}
// If we changed the constant, return it.
- Constant *NewCP = ConstantVector::get(Elts);
+ Constant *NewCP = Context->getConstantVector(Elts);
return NewCP != CP ? NewCP : 0;
} else if (isa<ConstantAggregateZero>(V)) {
// Simplify the CAZ to a ConstantVector where the non-demanded elements are
return 0;
const Type *EltTy = cast<VectorType>(V->getType())->getElementType();
- Constant *Zero = Constant::getNullValue(EltTy);
- Constant *Undef = UndefValue::get(EltTy);
+ Constant *Zero = Context->getNullValue(EltTy);
+ Constant *Undef = Context->getUndef(EltTy);
std::vector<Constant*> Elts;
- for (unsigned i = 0; i != VWidth; ++i)
- Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef);
+ for (unsigned i = 0; i != VWidth; ++i) {
+ Constant *Elt = DemandedElts[i] ? Zero : Undef;
+ Elts.push_back(Elt);
+ }
UndefElts = DemandedElts ^ EltMask;
- return ConstantVector::get(Elts);
+ return Context->getConstantVector(Elts);
}
// Limit search depth.
if (Depth == 10)
- return false;
+ return 0;
// If multiple users are using the root value, procede with
// simplification conservatively assuming that all elements
// the main instcombine process.
if (Depth != 0)
// TODO: Just compute the UndefElts information recursively.
- return false;
+ return 0;
// Conservatively assume that all elements are needed.
DemandedElts = EltMask;
}
Instruction *I = dyn_cast<Instruction>(V);
- if (!I) return false; // Only analyze instructions.
+ if (!I) return 0; // Only analyze instructions.
bool MadeChange = false;
- uint64_t UndefElts2;
+ APInt UndefElts2(VWidth, 0);
Value *TmpV;
switch (I->getOpcode()) {
default: break;
// If this is inserting an element that isn't demanded, remove this
// insertelement.
unsigned IdxNo = Idx->getZExtValue();
- if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0)
+ if (IdxNo >= VWidth || !DemandedElts[IdxNo])
return AddSoonDeadInstToWorklist(*I, 0);
// Otherwise, the element inserted overwrites whatever was there, so the
// input demanded set is simpler than the output set.
- TmpV = SimplifyDemandedVectorElts(I->getOperand(0),
- DemandedElts & ~(1ULL << IdxNo),
+ APInt DemandedElts2 = DemandedElts;
+ DemandedElts2.clear(IdxNo);
+ TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
UndefElts, Depth+1);
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
// The inserted element is defined.
- UndefElts &= ~(1ULL << IdxNo);
+ UndefElts.clear(IdxNo);
break;
}
case Instruction::ShuffleVector: {
ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
uint64_t LHSVWidth =
cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
- uint64_t LeftDemanded = 0, RightDemanded = 0;
+ APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
for (unsigned i = 0; i < VWidth; i++) {
- if (DemandedElts & (1ULL << i)) {
+ if (DemandedElts[i]) {
unsigned MaskVal = Shuffle->getMaskValue(i);
if (MaskVal != -1u) {
assert(MaskVal < LHSVWidth * 2 &&
"shufflevector mask index out of range!");
if (MaskVal < LHSVWidth)
- LeftDemanded |= 1ULL << MaskVal;
+ LeftDemanded.set(MaskVal);
else
- RightDemanded |= 1ULL << (MaskVal - LHSVWidth);
+ RightDemanded.set(MaskVal - LHSVWidth);
}
}
}
+ APInt UndefElts4(LHSVWidth, 0);
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
- UndefElts2, Depth+1);
+ UndefElts4, Depth+1);
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
- uint64_t UndefElts3;
+ APInt UndefElts3(LHSVWidth, 0);
TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
UndefElts3, Depth+1);
if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
for (unsigned i = 0; i < VWidth; i++) {
unsigned MaskVal = Shuffle->getMaskValue(i);
if (MaskVal == -1u) {
- uint64_t NewBit = 1ULL << i;
- UndefElts |= NewBit;
+ UndefElts.set(i);
} else if (MaskVal < LHSVWidth) {
- uint64_t NewBit = ((UndefElts2 >> MaskVal) & 1) << i;
- NewUndefElts |= NewBit;
- UndefElts |= NewBit;
+ if (UndefElts4[MaskVal]) {
+ NewUndefElts = true;
+ UndefElts.set(i);
+ }
} else {
- uint64_t NewBit = ((UndefElts3 >> (MaskVal - LHSVWidth)) & 1) << i;
- NewUndefElts |= NewBit;
- UndefElts |= NewBit;
+ if (UndefElts3[MaskVal - LHSVWidth]) {
+ NewUndefElts = true;
+ UndefElts.set(i);
+ }
}
}
// Add additional discovered undefs.
std::vector<Constant*> Elts;
for (unsigned i = 0; i < VWidth; ++i) {
- if (UndefElts & (1ULL << i))
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ if (UndefElts[i])
+ Elts.push_back(Context->getUndef(Type::Int32Ty));
else
- Elts.push_back(ConstantInt::get(Type::Int32Ty,
+ Elts.push_back(Context->getConstantInt(Type::Int32Ty,
Shuffle->getMaskValue(i)));
}
- I->setOperand(2, ConstantVector::get(Elts));
+ I->setOperand(2, Context->getConstantVector(Elts));
MadeChange = true;
}
break;
const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
if (!VTy) break;
unsigned InVWidth = VTy->getNumElements();
- uint64_t InputDemandedElts = 0;
+ APInt InputDemandedElts(InVWidth, 0);
unsigned Ratio;
if (VWidth == InVWidth) {
// elements are live.
Ratio = VWidth/InVWidth;
for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
- if (DemandedElts & (1ULL << OutIdx))
- InputDemandedElts |= 1ULL << (OutIdx/Ratio);
+ if (DemandedElts[OutIdx])
+ InputDemandedElts.set(OutIdx/Ratio);
}
} else {
// Untested so far.
// live.
Ratio = InVWidth/VWidth;
for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
- if (DemandedElts & (1ULL << InIdx/Ratio))
- InputDemandedElts |= 1ULL << InIdx;
+ if (DemandedElts[InIdx/Ratio])
+ InputDemandedElts.set(InIdx);
}
// div/rem demand all inputs, because they don't want divide by zero.
// then an output element is undef if the corresponding input element is
// undef.
for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
- if (UndefElts2 & (1ULL << (OutIdx/Ratio)))
- UndefElts |= 1ULL << OutIdx;
+ if (UndefElts2[OutIdx/Ratio])
+ UndefElts.set(OutIdx);
} else if (VWidth < InVWidth) {
assert(0 && "Unimp");
// If there are more elements in the source than there are in the result,
// elements are undef.
UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
- if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef?
- UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit.
+ if (!UndefElts2[InIdx]) // Not undef?
+ UndefElts.clear(InIdx/Ratio); // Clear undef bit.
}
break;
}
default: assert(0 && "Case stmts out of sync!");
case Intrinsic::x86_sse_sub_ss:
case Intrinsic::x86_sse2_sub_sd:
- TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS,
+ TmpV = InsertNewInstBefore(BinaryOperator::CreateFSub(LHS, RHS,
II->getName()), *II);
break;
case Intrinsic::x86_sse_mul_ss:
case Intrinsic::x86_sse2_mul_sd:
- TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS,
+ TmpV = InsertNewInstBefore(BinaryOperator::CreateFMul(LHS, RHS,
II->getName()), *II);
break;
}
Instruction *New =
- InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U,
- II->getName());
+ InsertElementInst::Create(
+ Context->getUndef(II->getType()), TmpV, 0U, II->getName());
InsertNewInstBefore(New, *II);
AddSoonDeadInstToWorklist(*II, 0);
return New;
/// 'shouldApply' and 'apply' methods.
///
template<typename Functor>
-static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
+static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F,
+ LLVMContext *Context) {
unsigned Opcode = Root.getOpcode();
Value *LHS = Root.getOperand(0);
// Make what used to be the LHS of the root be the user of the root...
Value *ExtraOperand = TmpLHSI->getOperand(1);
if (&Root == TmpLHSI) {
- Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
+ Root.replaceAllUsesWith(Context->getNullValue(TmpLHSI->getType()));
return 0;
}
Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI
// AddRHS - Implements: X + X --> X << 1
struct AddRHS {
Value *RHS;
- AddRHS(Value *rhs) : RHS(rhs) {}
+ LLVMContext *Context;
+ AddRHS(Value *rhs, LLVMContext *C) : RHS(rhs), Context(C) {}
bool shouldApply(Value *LHS) const { return LHS == RHS; }
Instruction *apply(BinaryOperator &Add) const {
return BinaryOperator::CreateShl(Add.getOperand(0),
- ConstantInt::get(Add.getType(), 1));
+ Context->getConstantInt(Add.getType(), 1));
}
};
// iff C1&C2 == 0
struct AddMaskingAnd {
Constant *C2;
- AddMaskingAnd(Constant *c) : C2(c) {}
+ LLVMContext *Context;
+ AddMaskingAnd(Constant *c, LLVMContext *C) : C2(c), Context(C) {}
bool shouldApply(Value *LHS) const {
ConstantInt *C1;
- return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
- ConstantExpr::getAnd(C1, C2)->isNullValue();
+ return match(LHS, m_And(m_Value(), m_ConstantInt(C1)), *Context) &&
+ Context->getConstantExprAnd(C1, C2)->isNullValue();
}
Instruction *apply(BinaryOperator &Add) const {
return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1));
static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
InstCombiner *IC) {
+ LLVMContext *Context = IC->getContext();
+
if (CastInst *CI = dyn_cast<CastInst>(&I)) {
return IC->InsertCastBefore(CI->getOpcode(), SO, I.getType(), I);
}
if (Constant *SOC = dyn_cast<Constant>(SO)) {
if (ConstIsRHS)
- return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
- return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
+ return Context->getConstantExpr(I.getOpcode(), SOC, ConstOperand);
+ return Context->getConstantExpr(I.getOpcode(), ConstOperand, SOC);
}
Value *Op0 = SO, *Op1 = ConstOperand;
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
- New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1,
- SO->getName()+".cmp");
+ New = CmpInst::Create(*Context, CI->getOpcode(), CI->getPredicate(),
+ Op0, Op1, SO->getName()+".cmp");
else {
assert(0 && "Unknown binary instruction type!");
abort();
Value *InV = 0;
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
if (CmpInst *CI = dyn_cast<CmpInst>(&I))
- InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
+ InV = Context->getConstantExprCompare(CI->getPredicate(), InC, C);
else
- InV = ConstantExpr::get(I.getOpcode(), InC, C);
+ InV = Context->getConstantExpr(I.getOpcode(), InC, C);
} else {
assert(PN->getIncomingBlock(i) == NonConstBB);
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
PN->getIncomingValue(i), C, "phitmp",
NonConstBB->getTerminator());
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
- InV = CmpInst::Create(CI->getOpcode(),
+ InV = CmpInst::Create(*Context, CI->getOpcode(),
CI->getPredicate(),
PN->getIncomingValue(i), C, "phitmp",
NonConstBB->getTerminator());
for (unsigned i = 0; i != NumPHIValues; ++i) {
Value *InV;
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
- InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
+ InV = Context->getConstantExprCast(CI->getOpcode(), InC, RetTy);
} else {
assert(PN->getIncomingBlock(i) == NonConstBB);
InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i),
return ReplaceInstUsesWith(I, RHS);
// X + 0 --> X
- if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0.
- if (RHSC->isNullValue())
- return ReplaceInstUsesWith(I, LHS);
- } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
- if (CFP->isExactlyValue(ConstantFP::getNegativeZero
- (I.getType())->getValueAPF()))
- return ReplaceInstUsesWith(I, LHS);
- }
+ if (RHSC->isNullValue())
+ return ReplaceInstUsesWith(I, LHS);
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) {
// X + (signbit) --> X ^ signbit
// See if SimplifyDemandedBits can simplify this. This handles stuff like
// (X & 254)+1 -> (X&254)|1
- if (!isa<VectorType>(I.getType())) {
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne))
- return &I;
- }
+ if (SimplifyDemandedInstructionBits(I))
+ return &I;
// zext(i1) - 1 -> select i1, 0, -1
if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
if (CI->isAllOnesValue() &&
ZI->getOperand(0)->getType() == Type::Int1Ty)
return SelectInst::Create(ZI->getOperand(0),
- Constant::getNullValue(I.getType()),
- ConstantInt::getAllOnesValue(I.getType()));
+ Context->getNullValue(I.getType()),
+ Context->getConstantIntAllOnesValue(I.getType()));
}
if (isa<PHINode>(LHS))
ConstantInt *XorRHS = 0;
Value *XorLHS = 0;
if (isa<ConstantInt>(RHSC) &&
- match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
- uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits();
+ match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)), *Context)) {
+ uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
uint32_t Size = TySizeBits / 2;
// X + X --> X << 1
if (I.getType()->isInteger()) {
- if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
+ if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS, Context), Context))
+ return Result;
if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
if (RHSI->getOpcode() == Instruction::Sub)
// -A + B --> B - A
// -A + -B --> -(A + B)
- if (Value *LHSV = dyn_castNegVal(LHS)) {
+ if (Value *LHSV = dyn_castNegVal(LHS, Context)) {
if (LHS->getType()->isIntOrIntVector()) {
- if (Value *RHSV = dyn_castNegVal(RHS)) {
+ if (Value *RHSV = dyn_castNegVal(RHS, Context)) {
Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum");
InsertNewInstBefore(NewAdd, I);
return BinaryOperator::CreateNeg(NewAdd);
// A + -B --> A - B
if (!isa<Constant>(RHS))
- if (Value *V = dyn_castNegVal(RHS))
+ if (Value *V = dyn_castNegVal(RHS, Context))
return BinaryOperator::CreateSub(LHS, V);
ConstantInt *C2;
- if (Value *X = dyn_castFoldableMul(LHS, C2)) {
+ if (Value *X = dyn_castFoldableMul(LHS, C2, Context)) {
if (X == RHS) // X*C + X --> X * (C+1)
- return BinaryOperator::CreateMul(RHS, AddOne(C2));
+ return BinaryOperator::CreateMul(RHS, AddOne(C2, Context));
// X*C1 + X*C2 --> X * (C1+C2)
ConstantInt *C1;
- if (X == dyn_castFoldableMul(RHS, C1))
- return BinaryOperator::CreateMul(X, Add(C1, C2));
+ if (X == dyn_castFoldableMul(RHS, C1, Context))
+ return BinaryOperator::CreateMul(X, Context->getConstantExprAdd(C1, C2));
}
// X + X*C --> X * (C+1)
- if (dyn_castFoldableMul(RHS, C2) == LHS)
- return BinaryOperator::CreateMul(LHS, AddOne(C2));
+ if (dyn_castFoldableMul(RHS, C2, Context) == LHS)
+ return BinaryOperator::CreateMul(LHS, AddOne(C2, Context));
// X + ~X --> -1 since ~X = -X-1
- if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS)
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ if (dyn_castNotVal(LHS, Context) == RHS ||
+ dyn_castNotVal(RHS, Context) == LHS)
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
// (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
- if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
- if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
+ if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)), *Context))
+ if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2, Context), Context))
return R;
// A+B --> A|B iff A and B have no bits set in common.
// W*X + Y*Z --> W * (X+Z) iff W == Y
if (I.getType()->isIntOrIntVector()) {
Value *W, *X, *Y, *Z;
- if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
- match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
+ if (match(LHS, m_Mul(m_Value(W), m_Value(X)), *Context) &&
+ match(RHS, m_Mul(m_Value(Y), m_Value(Z)), *Context)) {
if (W != Y) {
if (W == Z) {
std::swap(Y, Z);
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
Value *X = 0;
- if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
- return BinaryOperator::CreateSub(SubOne(CRHS), X);
+ if (match(LHS, m_Not(m_Value(X)), *Context)) // ~X + C --> (C-1) - X
+ return BinaryOperator::CreateSub(SubOne(CRHS, Context), X);
// (X & FF00) + xx00 -> (X+xx00) & FF00
- if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
- Constant *Anded = And(CRHS, C2);
+ if (LHS->hasOneUse() &&
+ match(LHS, m_And(m_Value(X), m_ConstantInt(C2)), *Context)) {
+ Constant *Anded = Context->getConstantExprAnd(CRHS, C2);
if (Anded == CRHS) {
// See if all bits from the first bit set in the Add RHS up are included
// in the mask. First, get the rightmost bit.
}
// add (cast *A to intptrtype) B ->
- // cast (GEP (cast *A to sbyte*) B) --> intptrtype
+ // cast (GEP (cast *A to i8*) B) --> intptrtype
{
CastInst *CI = dyn_cast<CastInst>(LHS);
Value *Other = RHS;
Other = LHS;
}
if (CI && CI->getType()->isSized() &&
- (CI->getType()->getPrimitiveSizeInBits() ==
+ (CI->getType()->getScalarSizeInBits() ==
TD->getIntPtrType()->getPrimitiveSizeInBits())
&& isa<PointerType>(CI->getOperand(0)->getType())) {
unsigned AS =
cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace();
Value *I2 = InsertBitCastBefore(CI->getOperand(0),
- PointerType::get(Type::Int8Ty, AS), I);
+ Context->getPointerType(Type::Int8Ty, AS), I);
I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I);
return new PtrToIntInst(I2, CI->getType());
}
// Can we fold the add into the argument of the select?
// We check both true and false select arguments for a matching subtract.
- if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
+ if (match(FV, m_Zero(), *Context) &&
+ match(TV, m_Sub(m_Value(N), m_Specific(A)), *Context))
// Fold the add into the true select value.
return SelectInst::Create(SI->getCondition(), N, A);
- if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
+ if (match(TV, m_Zero(), *Context) &&
+ match(FV, m_Sub(m_Value(N), m_Specific(A)), *Context))
// Fold the add into the false select value.
return SelectInst::Create(SI->getCondition(), A, N);
}
}
-
- // Check for X+0.0. Simplify it to X if we know X is not -0.0.
- if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
- if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
- return ReplaceInstUsesWith(I, LHS);
// Check for (add (sext x), y), see if we can merge this into an
// integer add followed by a sext.
// (add (sext x), cst) --> (sext (add x, cst'))
if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
Constant *CI =
- ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
+ Context->getConstantExprTrunc(RHSC, LHSConv->getOperand(0)->getType());
if (LHSConv->hasOneUse() &&
- ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
+ Context->getConstantExprSExt(CI, I.getType()) == RHSC &&
WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
// Insert the new, smaller add.
Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0),
}
}
}
-
+
+ return Changed ? &I : 0;
+}
+
+Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
+ bool Changed = SimplifyCommutative(I);
+ Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
+
+ if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
+ // X + 0 --> X
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
+ if (CFP->isExactlyValue(Context->getConstantFPNegativeZero
+ (I.getType())->getValueAPF()))
+ return ReplaceInstUsesWith(I, LHS);
+ }
+
+ if (isa<PHINode>(LHS))
+ if (Instruction *NV = FoldOpIntoPhi(I))
+ return NV;
+ }
+
+ // -A + B --> B - A
+ // -A + -B --> -(A + B)
+ if (Value *LHSV = dyn_castFNegVal(LHS, Context))
+ return BinaryOperator::CreateFSub(RHS, LHSV);
+
+ // A + -B --> A - B
+ if (!isa<Constant>(RHS))
+ if (Value *V = dyn_castFNegVal(RHS, Context))
+ return BinaryOperator::CreateFSub(LHS, V);
+
+ // Check for X+0.0. Simplify it to X if we know X is not -0.0.
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS))
+ if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS))
+ return ReplaceInstUsesWith(I, LHS);
+
// Check for (add double (sitofp x), y), see if we can merge this into an
// integer add followed by a promotion.
if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
// instcombined.
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
Constant *CI =
- ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
+ Context->getConstantExprFPToSI(CFP, LHSConv->getOperand(0)->getType());
if (LHSConv->hasOneUse() &&
- ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
+ Context->getConstantExprSIToFP(CI, I.getType()) == CFP &&
WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
// Insert the new integer add.
Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0),
Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- if (Op0 == Op1 && // sub X, X -> 0
- !I.getType()->isFPOrFPVector())
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ if (Op0 == Op1) // sub X, X -> 0
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// If this is a 'B = x-(-A)', change to B = x+A...
- if (Value *V = dyn_castNegVal(Op1))
+ if (Value *V = dyn_castNegVal(Op1, Context))
return BinaryOperator::CreateAdd(Op0, V);
if (isa<UndefValue>(Op0))
// C - ~X == X + (1+C)
Value *X = 0;
- if (match(Op1, m_Not(m_Value(X))))
- return BinaryOperator::CreateAdd(X, AddOne(C));
+ if (match(Op1, m_Not(m_Value(X)), *Context))
+ return BinaryOperator::CreateAdd(X, AddOne(C, Context));
// -(X >>u 31) -> (X >>s 31)
// -(X >>s 31) -> (X >>u 31)
return BinaryOperator::CreateXor(Op0, Op1);
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
- if (Op1I->getOpcode() == Instruction::Add &&
- !Op0->getType()->isFPOrFPVector()) {
+ if (Op1I->getOpcode() == Instruction::Add) {
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName());
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
// C1-(X+C2) --> (C1-C2)-X
- return BinaryOperator::CreateSub(Subtract(CI1, CI2),
- Op1I->getOperand(0));
+ return BinaryOperator::CreateSub(
+ Context->getConstantExprSub(CI1, CI2), Op1I->getOperand(0));
}
}
// Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
// is not used by anyone else...
//
- if (Op1I->getOpcode() == Instruction::Sub &&
- !Op1I->getType()->isFPOrFPVector()) {
+ if (Op1I->getOpcode() == Instruction::Sub) {
// Swap the two operands of the subexpr...
Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
Op1I->setOperand(0, IIOp1);
if (CSI->isZero())
if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1)))
return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
- ConstantExpr::getNeg(DivRHS));
+ Context->getConstantExprNeg(DivRHS));
// X - X*C --> X * (1-C)
ConstantInt *C2 = 0;
- if (dyn_castFoldableMul(Op1I, C2) == Op0) {
- Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2);
+ if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) {
+ Constant *CP1 =
+ Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1),
+ C2);
return BinaryOperator::CreateMul(Op0, CP1);
}
}
}
- if (!Op0->getType()->isFPOrFPVector())
- if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
- if (Op0I->getOpcode() == Instruction::Add) {
- if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X
- return ReplaceInstUsesWith(I, Op0I->getOperand(1));
- else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X
- return ReplaceInstUsesWith(I, Op0I->getOperand(0));
- } else if (Op0I->getOpcode() == Instruction::Sub) {
- if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
- return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName());
- }
+ if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
+ if (Op0I->getOpcode() == Instruction::Add) {
+ if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X
+ return ReplaceInstUsesWith(I, Op0I->getOperand(1));
+ else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X
+ return ReplaceInstUsesWith(I, Op0I->getOperand(0));
+ } else if (Op0I->getOpcode() == Instruction::Sub) {
+ if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
+ return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName());
}
+ }
ConstantInt *C1;
- if (Value *X = dyn_castFoldableMul(Op0, C1)) {
+ if (Value *X = dyn_castFoldableMul(Op0, C1, Context)) {
if (X == Op1) // X*C - X --> X * (C-1)
- return BinaryOperator::CreateMul(Op1, SubOne(C1));
+ return BinaryOperator::CreateMul(Op1, SubOne(C1, Context));
ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
- if (X == dyn_castFoldableMul(Op1, C2))
- return BinaryOperator::CreateMul(X, Subtract(C1, C2));
+ if (X == dyn_castFoldableMul(Op1, C2, Context))
+ return BinaryOperator::CreateMul(X, Context->getConstantExprSub(C1, C2));
+ }
+ return 0;
+}
+
+Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+
+ // If this is a 'B = x-(-A)', change to B = x+A...
+ if (Value *V = dyn_castFNegVal(Op1, Context))
+ return BinaryOperator::CreateFAdd(Op0, V);
+
+ if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
+ if (Op1I->getOpcode() == Instruction::FAdd) {
+ if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
+ return BinaryOperator::CreateFNeg(Op1I->getOperand(1), I.getName());
+ else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
+ return BinaryOperator::CreateFNeg(Op1I->getOperand(0), I.getName());
+ }
}
+
return 0;
}
bool Changed = SimplifyCommutative(I);
Value *Op0 = I.getOperand(0);
- if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ // TODO: If Op1 is undef and Op0 is finite, return zero.
+ if (!I.getType()->isFPOrFPVector() &&
+ isa<UndefValue>(I.getOperand(1))) // undef * X -> 0
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// Simplify mul instructions with a constant RHS...
if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
if (SI->getOpcode() == Instruction::Shl)
if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
return BinaryOperator::CreateMul(SI->getOperand(0),
- ConstantExpr::getShl(CI, ShOp));
+ Context->getConstantExprShl(CI, ShOp));
if (CI->isZero())
return ReplaceInstUsesWith(I, Op1); // X * 0 == 0
const APInt& Val = cast<ConstantInt>(CI)->getValue();
if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
return BinaryOperator::CreateShl(Op0,
- ConstantInt::get(Op0->getType(), Val.logBase2()));
+ Context->getConstantInt(Op0->getType(), Val.logBase2()));
}
- } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
- if (Op1F->isNullValue())
- return ReplaceInstUsesWith(I, Op1);
-
- // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
- // ANSI says we can drop signals, so we can do this anyway." (from GCC)
- if (Op1F->isExactlyValue(1.0))
- return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
} else if (isa<VectorType>(Op1->getType())) {
- if (isa<ConstantAggregateZero>(Op1))
- return ReplaceInstUsesWith(I, Op1);
+ // TODO: If Op1 is all zeros and Op0 is all finite, return all zeros.
if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
if (Op1V->isAllOnesValue()) // X * -1 == 0 - X
// As above, vector X*splat(1.0) -> X in all defined cases.
if (Constant *Splat = Op1V->getSplatValue()) {
- if (ConstantFP *F = dyn_cast<ConstantFP>(Splat))
- if (F->isExactlyValue(1.0))
- return ReplaceInstUsesWith(I, Op0);
if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat))
if (CI->equalsInt(1))
return ReplaceInstUsesWith(I, Op0);
Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0),
Op1, "tmp");
InsertNewInstBefore(Add, I);
- Value *C1C2 = ConstantExpr::getMul(Op1,
+ Value *C1C2 = Context->getConstantExprMul(Op1,
cast<Constant>(Op0I->getOperand(1)));
return BinaryOperator::CreateAdd(Add, C1C2);
return NV;
}
- if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
- if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
+ if (Value *Op0v = dyn_castNegVal(Op0, Context)) // -X * -Y = X*Y
+ if (Value *Op1v = dyn_castNegVal(I.getOperand(1), Context))
return BinaryOperator::CreateMul(Op0v, Op1v);
// (X / Y) * Y = X - (X % Y)
Op1 = Op0;
BO = dyn_cast<BinaryOperator>(I.getOperand(1));
}
- Value *Neg = dyn_castNegVal(Op1);
+ Value *Neg = dyn_castNegVal(Op1, Context);
if (BO && BO->hasOneUse() &&
(BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) &&
(BO->getOpcode() == Instruction::UDiv ||
isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) &&
TIS) {
// Shift the X value right to turn it into "all signbits".
- Constant *Amt = ConstantInt::get(SCIOp0->getType(),
+ Constant *Amt = Context->getConstantInt(SCIOp0->getType(),
SCOpTy->getPrimitiveSizeInBits()-1);
Value *V =
InsertNewInstBefore(
return Changed ? &I : 0;
}
+Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
+ bool Changed = SimplifyCommutative(I);
+ Value *Op0 = I.getOperand(0);
+
+ // Simplify mul instructions with a constant RHS...
+ if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
+ if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
+ // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
+ // ANSI says we can drop signals, so we can do this anyway." (from GCC)
+ if (Op1F->isExactlyValue(1.0))
+ return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
+ } else if (isa<VectorType>(Op1->getType())) {
+ if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
+ // As above, vector X*splat(1.0) -> X in all defined cases.
+ if (Constant *Splat = Op1V->getSplatValue()) {
+ if (ConstantFP *F = dyn_cast<ConstantFP>(Splat))
+ if (F->isExactlyValue(1.0))
+ return ReplaceInstUsesWith(I, Op0);
+ }
+ }
+ }
+
+ // Try to fold constant mul into select arguments.
+ if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
+ if (Instruction *R = FoldOpIntoSelect(I, SI, this))
+ return R;
+
+ if (isa<PHINode>(Op0))
+ if (Instruction *NV = FoldOpIntoPhi(I))
+ return NV;
+ }
+
+ if (Value *Op0v = dyn_castFNegVal(Op0, Context)) // -X * -Y = X*Y
+ if (Value *Op1v = dyn_castFNegVal(I.getOperand(1), Context))
+ return BinaryOperator::CreateFMul(Op0v, Op1v);
+
+ return Changed ? &I : 0;
+}
+
/// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
/// instruction.
bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
*I = SI->getOperand(NonNullOperand);
AddToWorkList(BBI);
} else if (*I == SelectCond) {
- *I = NonNullOperand == 1 ? ConstantInt::getTrue() :
- ConstantInt::getFalse();
+ *I = NonNullOperand == 1 ? Context->getConstantIntTrue() :
+ Context->getConstantIntFalse();
AddToWorkList(BBI);
}
}
if (isa<UndefValue>(Op0)) {
if (Op0->getType()->isFPOrFPVector())
return ReplaceInstUsesWith(I, Op0);
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
}
// X / undef -> undef
// (sdiv X, X) --> 1 (udiv X, X) --> 1
if (Op0 == Op1) {
if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) {
- ConstantInt *CI = ConstantInt::get(Ty->getElementType(), 1);
+ Constant *CI = Context->getConstantInt(Ty->getElementType(), 1);
std::vector<Constant*> Elts(Ty->getNumElements(), CI);
- return ReplaceInstUsesWith(I, ConstantVector::get(Elts));
+ return ReplaceInstUsesWith(I, Context->getConstantVector(Elts));
}
- ConstantInt *CI = ConstantInt::get(I.getType(), 1);
+ Constant *CI = Context->getConstantInt(I.getType(), 1);
return ReplaceInstUsesWith(I, CI);
}
if (Instruction *LHS = dyn_cast<Instruction>(Op0))
if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
- if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv))
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ if (MultiplyOverflows(RHS, LHSRHS,
+ I.getOpcode()==Instruction::SDiv, Context))
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
else
return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
- Multiply(RHS, LHSRHS));
+ Context->getConstantExprMul(RHS, LHSRHS));
}
if (!RHS->isZero()) { // avoid X udiv 0
// 0 / X == 0, we don't need to preserve faults!
if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0))
if (LHS->equalsInt(0))
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// It can't be division by zero, hence it must be division by one.
if (I.getType() == Type::Int1Ty)
// if so, convert to a right shift.
if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
return BinaryOperator::CreateLShr(Op0,
- ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
+ Context->getConstantInt(Op0->getType(), C->getValue().logBase2()));
// X udiv C, where C >= signbit
if (C->getValue().isNegative()) {
- Value *IC = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_ULT, Op0, C),
+ Value *IC = InsertNewInstBefore(new ICmpInst(*Context,
+ ICmpInst::ICMP_ULT, Op0, C),
I);
- return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
- ConstantInt::get(I.getType(), 1));
+ return SelectInst::Create(IC, Context->getNullValue(I.getType()),
+ Context->getConstantInt(I.getType(), 1));
}
}
Value *N = RHSI->getOperand(1);
const Type *NTy = N->getType();
if (uint32_t C2 = C1.logBase2()) {
- Constant *C2V = ConstantInt::get(NTy, C2);
+ Constant *C2V = Context->getConstantInt(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 = ConstantInt::get(Op0->getType(), TSA);
+ Constant *TC = Context->getConstantInt(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 = ConstantInt::get(Op0->getType(), FSA);
+ Constant *FC = Context->getConstantInt(Op0->getType(), FSA);
Instruction *FSI = BinaryOperator::CreateLShr(
Op0, FC, SI->getName()+".f");
FSI = InsertNewInstBefore(FSI, I);
Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- // 0 % X == 0 for integer, we don't need to preserve faults!
- if (Constant *LHS = dyn_cast<Constant>(Op0))
- if (LHS->isNullValue())
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
-
if (isa<UndefValue>(Op0)) { // undef % X -> 0
if (I.getType()->isFPOrFPVector())
return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN)
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
}
if (isa<UndefValue>(Op1))
return ReplaceInstUsesWith(I, Op1); // X % undef -> undef
if (Instruction *common = commonRemTransforms(I))
return common;
+ // 0 % X == 0 for integer, we don't need to preserve faults!
+ if (Constant *LHS = dyn_cast<Constant>(Op0))
+ if (LHS->isNullValue())
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
+
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
// X % 0 == undef, we don't need to preserve faults!
if (RHS->equalsInt(0))
- return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getUndef(I.getType()));
if (RHS->equalsInt(1)) // X % 1 == 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
}
// See if we can fold away this rem instruction.
- uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne))
+ if (SimplifyDemandedInstructionBits(I))
return &I;
}
}
// if so, convert to a bitwise and.
if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
if (C->getValue().isPowerOf2())
- return BinaryOperator::CreateAnd(Op0, SubOne(C));
+ return BinaryOperator::CreateAnd(Op0, SubOne(C, Context));
}
if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
if (RHSI->getOpcode() == Instruction::Shl &&
isa<ConstantInt>(RHSI->getOperand(0))) {
if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
- Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
+ Constant *N1 = Context->getConstantIntAllOnesValue(I.getType());
Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1,
"tmp"), I);
return BinaryOperator::CreateAnd(Op0, Add);
if ((STO->getValue().isPowerOf2()) &&
(SFO->getValue().isPowerOf2())) {
Value *TrueAnd = InsertNewInstBefore(
- BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I);
+ BinaryOperator::CreateAnd(Op0, SubOne(STO, Context),
+ SI->getName()+".t"), I);
Value *FalseAnd = InsertNewInstBefore(
- BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I);
+ BinaryOperator::CreateAnd(Op0, SubOne(SFO, Context),
+ SI->getName()+".f"), I);
return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
}
}
if (Instruction *common = commonIRemTransforms(I))
return common;
- if (Value *RHSNeg = dyn_castNegVal(Op1))
+ if (Value *RHSNeg = dyn_castNegVal(Op1, Context))
if (!isa<Constant>(RHSNeg) ||
(isa<ConstantInt>(RHSNeg) &&
cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
for (unsigned i = 0; i != VWidth; ++i) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) {
if (RHS->getValue().isNegative())
- Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
+ Elts[i] = cast<ConstantInt>(Context->getConstantExprNeg(RHS));
else
Elts[i] = RHS;
}
}
- Constant *NewRHSV = ConstantVector::get(Elts);
+ Constant *NewRHSV = Context->getConstantVector(Elts);
if (NewRHSV != RHSV) {
AddUsesToWorkList(I);
I.setOperand(1, NewRHSV);
/// opcode and two operands into either a constant true or false, or a brand
/// new ICmp instruction. The sign is passed in to determine which kind
/// of predicate to use in the new icmp instruction.
-static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) {
+static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS,
+ LLVMContext *Context) {
switch (code) {
default: assert(0 && "Illegal ICmp code!");
- case 0: return ConstantInt::getFalse();
+ case 0: return Context->getConstantIntFalse();
case 1:
if (sign)
- return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS);
else
- return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
- case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGT, LHS, RHS);
+ case 2: return new ICmpInst(*Context, ICmpInst::ICMP_EQ, LHS, RHS);
case 3:
if (sign)
- return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHS, RHS);
else
- return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHS, RHS);
case 4:
if (sign)
- return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHS, RHS);
else
- return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
- case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHS, RHS);
+ case 5: return new ICmpInst(*Context, ICmpInst::ICMP_NE, LHS, RHS);
case 6:
if (sign)
- return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS);
else
- return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
- case 7: return ConstantInt::getTrue();
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS);
+ case 7: return Context->getConstantIntTrue();
}
}
/// opcode and two operands into either a FCmp instruction. isordered is passed
/// in to determine which kind of predicate to use in the new fcmp instruction.
static Value *getFCmpValue(bool isordered, unsigned code,
- Value *LHS, Value *RHS) {
+ Value *LHS, Value *RHS, LLVMContext *Context) {
switch (code) {
default: assert(0 && "Illegal FCmp code!");
case 0:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_ORD, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_UNO, LHS, RHS);
case 1:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_OGT, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_UGT, LHS, RHS);
case 2:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_OEQ, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_UEQ, LHS, RHS);
case 3:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_OGE, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_UGE, LHS, RHS);
case 4:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_OLT, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_ULT, LHS, RHS);
case 5:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_ONE, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_UNE, LHS, RHS);
case 6:
if (isordered)
- return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS);
+ return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS);
else
- return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS);
- case 7: return ConstantInt::getTrue();
+ return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS);
+ case 7: return Context->getConstantIntTrue();
}
}
bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) ||
ICmpInst::isSignedPredicate(ICI->getPredicate());
- Value *RV = getICmpValue(isSigned, Code, LHS, RHS);
+ Value *RV = getICmpValue(isSigned, Code, LHS, RHS, IC.getContext());
if (Instruction *I = dyn_cast<Instruction>(RV))
return I;
// Otherwise, it's a constant boolean value...
Value *X = Op->getOperand(0);
Constant *Together = 0;
if (!Op->isShift())
- Together = And(AndRHS, OpRHS);
+ Together = Context->getConstantExprAnd(AndRHS, OpRHS);
switch (Op->getOpcode()) {
case Instruction::Xor:
uint32_t BitWidth = AndRHS->getType()->getBitWidth();
uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
- ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask);
+ ConstantInt *CI = Context->getConstantInt(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 = ConstantInt::get(AndRHS->getValue() & ShrMask);
+ ConstantInt *CI = Context->getConstantInt(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 = ConstantInt::get(AndRHS->getValue() & ShrMask);
+ Constant *C = Context->getConstantInt(AndRHS->getValue() & ShrMask);
if (C == AndRHS) { // Masking out bits shifted in.
// (Val ashr C1) & C2 -> (Val lshr C1) & C2
// Make the argument unsigned.
Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
bool isSigned, bool Inside,
Instruction &IB) {
- assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
+ assert(cast<ConstantInt>(Context->getConstantExprICmp((isSigned ?
ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
"Lo is not <= Hi in range emission code!");
if (Inside) {
if (Lo == Hi) // Trivially false.
- return new ICmpInst(ICmpInst::ICMP_NE, V, V);
+ return new ICmpInst(*Context, ICmpInst::ICMP_NE, V, V);
// V >= Min && V < Hi --> V < Hi
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
ICmpInst::Predicate pred = (isSigned ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
- return new ICmpInst(pred, V, Hi);
+ return new ICmpInst(*Context, pred, V, Hi);
}
// Emit V-Lo <u Hi-Lo
- Constant *NegLo = ConstantExpr::getNeg(Lo);
+ Constant *NegLo = Context->getConstantExprNeg(Lo);
Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
InsertNewInstBefore(Add, IB);
- Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
- return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
+ Constant *UpperBound = Context->getConstantExprAdd(NegLo, Hi);
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, UpperBound);
}
if (Lo == Hi) // Trivially true.
- return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
+ return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V);
// V < Min || V >= Hi -> V > Hi-1
- Hi = SubOne(cast<ConstantInt>(Hi));
+ Hi = SubOne(cast<ConstantInt>(Hi), Context);
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
ICmpInst::Predicate pred = (isSigned ?
ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
- return new ICmpInst(pred, V, Hi);
+ return new ICmpInst(*Context, pred, V, Hi);
}
// Emit V-Lo >u Hi-1-Lo
// Note that Hi has already had one subtracted from it, above.
- ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
+ ConstantInt *NegLo = cast<ConstantInt>(Context->getConstantExprNeg(Lo));
Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
InsertNewInstBefore(Add, IB);
- Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
- return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
+ Constant *LowerBound = Context->getConstantExprAdd(NegLo, Hi);
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, LowerBound);
}
// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
switch (LHSI->getOpcode()) {
default: return 0;
case Instruction::And:
- if (And(N, Mask) == Mask) {
+ if (Context->getConstantExprAnd(N, Mask) == Mask) {
// If the AndRHS is a power of two minus one (0+1+), this is simple.
if ((Mask->getValue().countLeadingZeros() +
Mask->getValue().countPopulation()) ==
// If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
if ((Mask->getValue().countLeadingZeros() +
Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
- && And(N, Mask)->isZero())
+ && Context->getConstantExprAnd(N, Mask)->isNullValue())
break;
return 0;
}
ICmpInst::Predicate LHSCC, RHSCC;
// This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
- if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) ||
- !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst))))
+ if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
+ m_ConstantInt(LHSCst)), *Context) ||
+ !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
+ m_ConstantInt(RHSCst)), *Context))
return 0;
// (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
LHSCst->getValue().isPowerOf2()) {
Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2);
InsertNewInstBefore(NewOr, I);
- return new ICmpInst(LHSCC, NewOr, LHSCst);
+ return new ICmpInst(*Context, LHSCC, NewOr, LHSCst);
}
// From here on, we only handle:
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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
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
switch (RHSCC) {
default: assert(0 && "Unknown integer condition code!");
case ICmpInst::ICMP_ULT:
- if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
- return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst);
+ if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X u< 14) -> X < 13
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst);
break; // (X != 13 & X u< 15) -> no change
case ICmpInst::ICMP_SLT:
- if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
- return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst);
+ if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X s< 14) -> X < 13
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst);
break; // (X != 13 & X s< 15) -> no change
case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
return ReplaceInstUsesWith(I, RHS);
case ICmpInst::ICMP_NE:
- if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
- Constant *AddCST = ConstantExpr::getNeg(LHSCst);
+ if (LHSCst == SubOne(RHSCst, Context)){// (X != 13 & X != 14) -> X-13 >u 1
+ Constant *AddCST = Context->getConstantExprNeg(LHSCst);
Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
Val->getName()+".off");
InsertNewInstBefore(Add, I);
- return new ICmpInst(ICmpInst::ICMP_UGT, Add,
- ConstantInt::get(Add->getType(), 1));
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add,
+ Context->getConstantInt(Add->getType(), 1));
}
break; // (X != 13 & X != 15) -> no change
}
default: assert(0 && "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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
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: assert(0 && "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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
break;
case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
break;
case ICmpInst::ICMP_NE:
- if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
- return new ICmpInst(LHSCC, Val, RHSCst);
+ if (RHSCst == AddOne(LHSCst, Context)) // (X u> 13 & X != 14) -> X u> 14
+ return new ICmpInst(*Context, LHSCC, Val, RHSCst);
break; // (X u> 13 & X != 15) -> no change
case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
- return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true, I);
+ return InsertRangeTest(Val, AddOne(LHSCst, Context),
+ RHSCst, false, true, I);
case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
break;
}
case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
break;
case ICmpInst::ICMP_NE:
- if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
- return new ICmpInst(LHSCC, Val, RHSCst);
+ if (RHSCst == AddOne(LHSCst, Context)) // (X s> 13 & X != 14) -> X s> 14
+ return new ICmpInst(*Context, LHSCC, Val, RHSCst);
break; // (X s> 13 & X != 15) -> no change
case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
- return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true, I);
+ return InsertRangeTest(Val, AddOne(LHSCst, Context),
+ RHSCst, true, true, I);
case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
break;
}
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (isa<UndefValue>(Op1)) // X & undef -> 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// and X, X = X
if (Op0 == Op1)
// See if we can simplify any instructions used by the instruction whose sole
// purpose is to compute bits we don't care about.
- if (!isa<VectorType>(I.getType())) {
- uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne))
- return &I;
- } else {
+ if (SimplifyDemandedInstructionBits(I))
+ return &I;
+ if (isa<VectorType>(I.getType())) {
if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
if (CP->isAllOnesValue()) // X & <-1,-1> -> X
return ReplaceInstUsesWith(I, I.getOperand(0));
return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0>
}
}
-
+
if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
const APInt& AndRHSMask = AndRHS->getValue();
APInt NotAndRHS(~AndRHSMask);
// (1 << x) & 1 --> zext(x == 0)
// (1 >> x) & 1 --> zext(x == 0)
if (AndRHSMask == 1 && Op0LHS == AndRHS) {
- Instruction *NewICmp = new ICmpInst(ICmpInst::ICMP_EQ, Op0RHS,
- Constant::getNullValue(I.getType()));
+ Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ,
+ Op0RHS, Context->getNullValue(I.getType()));
InsertNewInstBefore(NewICmp, I);
return new ZExtInst(NewICmp, I.getType());
}
CastOp->getName()+".shrunk");
NewCast = InsertNewInstBefore(NewCast, I);
// trunc_or_bitcast(C1)&C2
- Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
- C3 = ConstantExpr::getAnd(C3, AndRHS);
+ Constant *C3 =
+ Context->getConstantExprTruncOrBitCast(AndCI,I.getType());
+ C3 = Context->getConstantExprAnd(C3, AndRHS);
return BinaryOperator::CreateAnd(NewCast, C3);
} else if (CastOp->getOpcode() == Instruction::Or) {
// Change: and (cast (or X, C1) to T), C2
// into : trunc(C1)&C2 iff trunc(C1)&C2 == C2
- Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
- if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2
+ Constant *C3 =
+ Context->getConstantExprTruncOrBitCast(AndCI,I.getType());
+ if (Context->getConstantExprAnd(C3, AndRHS) == AndRHS)
+ // trunc(C1)&C2
return ReplaceInstUsesWith(I, AndRHS);
}
}
return NV;
}
- Value *Op0NotVal = dyn_castNotVal(Op0);
- Value *Op1NotVal = dyn_castNotVal(Op1);
+ Value *Op0NotVal = dyn_castNotVal(Op0, Context);
+ Value *Op1NotVal = dyn_castNotVal(Op1, Context);
if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
// (~A & ~B) == (~(A | B)) - De Morgan's Law
if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
{
Value *A = 0, *B = 0, *C = 0, *D = 0;
- if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
+ if (match(Op0, m_Or(m_Value(A), m_Value(B)), *Context)) {
if (A == Op1 || B == Op1) // (A | ?) & A --> A
return ReplaceInstUsesWith(I, Op1);
// (A|B) & ~(A&B) -> A^B
- if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
+ if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
}
- if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
+ if (match(Op1, m_Or(m_Value(A), m_Value(B)), *Context)) {
if (A == Op0 || B == Op0) // A & (A | ?) --> A
return ReplaceInstUsesWith(I, Op0);
// ~(A&B) & (A|B) -> A^B
- if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
+ if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
}
if (Op0->hasOneUse() &&
- match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
+ match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) {
if (A == Op1) { // (A^B)&A -> A&(A^B)
I.swapOperands(); // Simplify below
std::swap(Op0, Op1);
}
if (Op1->hasOneUse() &&
- match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
+ match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context)) {
if (B == Op0) { // B&(A^B) -> B&(B^A)
cast<BinaryOperator>(Op1)->swapOperands();
std::swap(A, B);
}
// (A&((~A)|B)) -> A&B
- if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
- match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
+ if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A)), *Context) ||
+ match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))), *Context))
return BinaryOperator::CreateAnd(A, Op1);
- if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
- match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
+ if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A)), *Context) ||
+ match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))), *Context))
return BinaryOperator::CreateAnd(A, Op0);
}
if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
// (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
return R;
if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
// If either of the constants are nans, then the whole thing returns
// false.
if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0),
- RHS->getOperand(0));
+ 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))) &&
- match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) {
+ 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);
if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
// Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
if (Op0CC == Op1CC)
- return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
+ return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
+ Op0LHS, Op0RHS);
else if (Op0CC == FCmpInst::FCMP_FALSE ||
Op1CC == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
else if (Op0CC == FCmpInst::FCMP_TRUE)
return ReplaceInstUsesWith(I, Op1);
else if (Op1CC == FCmpInst::FCMP_TRUE)
// uno && oeq -> uno && (ord && eq) -> false
// uno && ord -> false
if (!Op0Ordered)
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
// ord && ueq -> ord && (uno || eq) -> oeq
return cast<Instruction>(getFCmpValue(true, Op1Pred,
- Op0LHS, Op0RHS));
+ Op0LHS, Op0RHS, Context));
}
}
}
/// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
/// we can simplify this expression to "cond ? C : D or B".
static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
- Value *C, Value *D) {
+ Value *C, Value *D,
+ LLVMContext *Context) {
// If A is not a select of -1/0, this cannot match.
Value *Cond = 0;
- if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond))))
+ if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)), *Context))
return 0;
// ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
- if (match(D, m_SelectCst<0, -1>(m_Specific(Cond))))
+ if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)), *Context))
return SelectInst::Create(Cond, C, B);
- if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
+ if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context))
return SelectInst::Create(Cond, C, B);
// ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
- if (match(B, m_SelectCst<0, -1>(m_Specific(Cond))))
+ if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)), *Context))
return SelectInst::Create(Cond, C, D);
- if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
+ if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context))
return SelectInst::Create(Cond, C, D);
return 0;
}
ICmpInst::Predicate LHSCC, RHSCC;
// This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
- if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) ||
- !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst))))
+ if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
+ m_ConstantInt(LHSCst)), *Context) ||
+ !match(RHS, m_ICmp(RHSCC, m_Value(Val2),
+ m_ConstantInt(RHSCst)), *Context))
return 0;
// From here on, we only handle:
switch (RHSCC) {
default: assert(0 && "Unknown integer condition code!");
case ICmpInst::ICMP_EQ:
- if (LHSCst == SubOne(RHSCst)) { // (X == 13 | X == 14) -> X-13 <u 2
- Constant *AddCST = ConstantExpr::getNeg(LHSCst);
+ if (LHSCst == SubOne(RHSCst, Context)) {
+ // (X == 13 | X == 14) -> X-13 <u 2
+ Constant *AddCST = Context->getConstantExprNeg(LHSCst);
Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
Val->getName()+".off");
InsertNewInstBefore(Add, I);
- AddCST = Subtract(AddOne(RHSCst), LHSCst);
- return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
+ AddCST = Context->getConstantExprSub(AddOne(RHSCst, Context), LHSCst);
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST);
}
break; // (X == 13 | X == 15) -> no change
case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
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, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
}
break;
case ICmpInst::ICMP_ULT:
// this can cause overflow.
if (RHSCst->isMaxValue(false))
return ReplaceInstUsesWith(I, LHS);
- return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false, I);
+ return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context),
+ false, false, I);
case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
break;
case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
// this can cause overflow.
if (RHSCst->isMaxValue(true))
return ReplaceInstUsesWith(I, LHS);
- return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false, I);
+ return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context),
+ true, false, I);
case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
break;
case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
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, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
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, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
break;
}
Value *V1 = 0;
ConstantInt *CI2 = 0;
- if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
+ if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)), *Context)) return 0;
APInt Xor = CI1->getValue() ^ CI2->getValue();
if (!Xor.isAllOnesValue()) return 0;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (isa<UndefValue>(Op1)) // X | undef -> -1
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
// or X, X = X
if (Op0 == Op1)
// See if we can simplify any instructions used by the instruction whose sole
// purpose is to compute bits we don't care about.
- if (!isa<VectorType>(I.getType())) {
- uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne))
- return &I;
- } else if (isa<ConstantAggregateZero>(Op1)) {
- return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X
- } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
- if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1>
- return ReplaceInstUsesWith(I, I.getOperand(1));
+ if (SimplifyDemandedInstructionBits(I))
+ return &I;
+ if (isa<VectorType>(I.getType())) {
+ if (isa<ConstantAggregateZero>(Op1)) {
+ return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X
+ } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) {
+ if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1>
+ return ReplaceInstUsesWith(I, I.getOperand(1));
+ }
}
-
-
// or X, -1 == -1
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
ConstantInt *C1 = 0; Value *X = 0;
// (X & C1) | C2 --> (X | C2) & (C1|C2)
- if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
+ if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)), *Context) &&
+ isOnlyUse(Op0)) {
Instruction *Or = BinaryOperator::CreateOr(X, RHS);
InsertNewInstBefore(Or, I);
Or->takeName(Op0);
return BinaryOperator::CreateAnd(Or,
- ConstantInt::get(RHS->getValue() | C1->getValue()));
+ Context->getConstantInt(RHS->getValue() | C1->getValue()));
}
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
- if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
+ if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1)), *Context) &&
+ isOnlyUse(Op0)) {
Instruction *Or = BinaryOperator::CreateOr(X, RHS);
InsertNewInstBefore(Or, I);
Or->takeName(Op0);
return BinaryOperator::CreateXor(Or,
- ConstantInt::get(C1->getValue() & ~RHS->getValue()));
+ Context->getConstantInt(C1->getValue() & ~RHS->getValue()));
}
// Try to fold constant and into select arguments.
Value *A = 0, *B = 0;
ConstantInt *C1 = 0, *C2 = 0;
- if (match(Op0, m_And(m_Value(A), m_Value(B))))
+ if (match(Op0, m_And(m_Value(A), m_Value(B)), *Context))
if (A == Op1 || B == Op1) // (A & ?) | A --> A
return ReplaceInstUsesWith(I, Op1);
- if (match(Op1, m_And(m_Value(A), m_Value(B))))
+ if (match(Op1, m_And(m_Value(A), m_Value(B)), *Context))
if (A == Op0 || B == Op0) // A | (A & ?) --> A
return ReplaceInstUsesWith(I, Op0);
// (A | B) | C and A | (B | C) -> bswap if possible.
// (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
- if (match(Op0, m_Or(m_Value(), m_Value())) ||
- match(Op1, m_Or(m_Value(), m_Value())) ||
- (match(Op0, m_Shift(m_Value(), m_Value())) &&
- match(Op1, m_Shift(m_Value(), m_Value())))) {
+ if (match(Op0, m_Or(m_Value(), m_Value()), *Context) ||
+ match(Op1, m_Or(m_Value(), m_Value()), *Context) ||
+ (match(Op0, m_Shift(m_Value(), m_Value()), *Context) &&
+ match(Op1, m_Shift(m_Value(), m_Value()), *Context))) {
if (Instruction *BSwap = MatchBSwap(I))
return BSwap;
}
// (X^C)|Y -> (X|Y)^C iff Y&C == 0
- if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
+ if (Op0->hasOneUse() &&
+ match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) &&
MaskedValueIsZero(Op1, C1->getValue())) {
Instruction *NOr = BinaryOperator::CreateOr(A, Op1);
InsertNewInstBefore(NOr, I);
}
// Y|(X^C) -> (X|Y)^C iff Y&C == 0
- if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
+ if (Op1->hasOneUse() &&
+ match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) &&
MaskedValueIsZero(Op0, C1->getValue())) {
Instruction *NOr = BinaryOperator::CreateOr(A, Op0);
InsertNewInstBefore(NOr, I);
// (A & C)|(B & D)
Value *C = 0, *D = 0;
- if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
- match(Op1, m_And(m_Value(B), m_Value(D)))) {
+ if (match(Op0, m_And(m_Value(A), m_Value(C)), *Context) &&
+ match(Op1, m_And(m_Value(B), m_Value(D)), *Context)) {
Value *V1 = 0, *V2 = 0, *V3 = 0;
C1 = dyn_cast<ConstantInt>(C);
C2 = dyn_cast<ConstantInt>(D);
// replace with V+N.
if (C1->getValue() == ~C2->getValue()) {
if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
- match(A, m_Add(m_Value(V1), m_Value(V2)))) {
+ match(A, m_Add(m_Value(V1), m_Value(V2)), *Context)) {
// Add commutes, try both ways.
if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
return ReplaceInstUsesWith(I, A);
}
// Or commutes, try both ways.
if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
- match(B, m_Add(m_Value(V1), m_Value(V2)))) {
+ match(B, m_Add(m_Value(V1), m_Value(V2)), *Context)) {
// Add commutes, try both ways.
if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
return ReplaceInstUsesWith(I, B);
}
// (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants
- if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
+ if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D, Context))
return Match;
- if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
+ if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C, Context))
return Match;
- if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
+ if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D, Context))
return Match;
- if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
+ if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C, Context))
return Match;
// ((A&~B)|(~A&B)) -> A^B
- if ((match(C, m_Not(m_Specific(D))) &&
- match(B, m_Not(m_Specific(A)))))
+ if ((match(C, m_Not(m_Specific(D)), *Context) &&
+ match(B, m_Not(m_Specific(A)), *Context)))
return BinaryOperator::CreateXor(A, D);
// ((~B&A)|(~A&B)) -> A^B
- if ((match(A, m_Not(m_Specific(D))) &&
- match(B, m_Not(m_Specific(C)))))
+ if ((match(A, m_Not(m_Specific(D)), *Context) &&
+ match(B, m_Not(m_Specific(C)), *Context)))
return BinaryOperator::CreateXor(C, D);
// ((A&~B)|(B&~A)) -> A^B
- if ((match(C, m_Not(m_Specific(B))) &&
- match(D, m_Not(m_Specific(A)))))
+ if ((match(C, m_Not(m_Specific(B)), *Context) &&
+ match(D, m_Not(m_Specific(A)), *Context)))
return BinaryOperator::CreateXor(A, B);
// ((~B&A)|(B&~A)) -> A^B
- if ((match(A, m_Not(m_Specific(B))) &&
- match(D, m_Not(m_Specific(C)))))
+ if ((match(A, m_Not(m_Specific(B)), *Context) &&
+ match(D, m_Not(m_Specific(C)), *Context)))
return BinaryOperator::CreateXor(C, B);
}
}
// ((A|B)&1)|(B&-2) -> (A&1) | B
- if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
- match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
+ if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) ||
+ match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) {
Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C);
if (Ret) return Ret;
}
// (B&-2)|((A|B)&1) -> (A&1) | B
- if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
- match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
+ if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) ||
+ match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) {
Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
if (Ret) return Ret;
}
- if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1
+ if (match(Op0, m_Not(m_Value(A)), *Context)) { // ~A | Op1
if (A == Op1) // ~A | A == -1
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
} else {
A = 0;
}
// Note, A is still live here!
- if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B
+ if (match(Op1, m_Not(m_Value(B)), *Context)) { // Op0 | ~B
if (Op0 == B)
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
// (~A | ~B) == (~(A & B)) - De Morgan's Law
if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
// (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
return R;
if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
// If either of the constants are nans, then the whole thing returns
// true.
if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
// Otherwise, no need to compare the two constants, compare the
// rest.
- return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0),
- RHS->getOperand(0));
+ 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))) &&
- match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) {
+ 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);
if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
// Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
if (Op0CC == Op1CC)
- return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
+ return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
+ Op0LHS, Op0RHS);
else if (Op0CC == FCmpInst::FCMP_TRUE ||
Op1CC == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
else if (Op0CC == FCmpInst::FCMP_FALSE)
return ReplaceInstUsesWith(I, Op1);
else if (Op1CC == FCmpInst::FCMP_FALSE)
// If both are ordered or unordered, return a new fcmp with
// or'ed predicates.
Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred,
- Op0LHS, Op0RHS);
+ Op0LHS, Op0RHS, Context);
if (Instruction *I = dyn_cast<Instruction>(RV))
return I;
// Otherwise, it's a constant boolean value...
if (isa<UndefValue>(Op0))
// Handle undef ^ undef -> 0 special case. This is a common
// idiom (misuse).
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef
}
// xor X, X = 0, even if X is nested in a sequence of Xor's.
- if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
+ if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1), Context)) {
assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
}
// See if we can simplify any instructions used by the instruction whose sole
// purpose is to compute bits we don't care about.
- if (!isa<VectorType>(I.getType())) {
- uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth();
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne))
- return &I;
- } else if (isa<ConstantAggregateZero>(Op1)) {
- return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
- }
+ if (SimplifyDemandedInstructionBits(I))
+ return &I;
+ if (isa<VectorType>(I.getType()))
+ if (isa<ConstantAggregateZero>(Op1))
+ return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
// Is this a ~ operation?
- if (Value *NotOp = dyn_castNotVal(&I)) {
+ if (Value *NotOp = dyn_castNotVal(&I, Context)) {
// ~(~X & Y) --> (X | ~Y) - De Morgan's Law
// ~(~X | Y) === (X & ~Y) - De Morgan's Law
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
if (Op0I->getOpcode() == Instruction::And ||
Op0I->getOpcode() == Instruction::Or) {
- if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
- if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
+ if (dyn_castNotVal(Op0I->getOperand(1), Context)) Op0I->swapOperands();
+ if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0), Context)) {
Instruction *NotY =
BinaryOperator::CreateNot(Op0I->getOperand(1),
Op0I->getOperand(1)->getName()+".not");
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
- if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) {
+ if (RHS == Context->getConstantIntTrue() && Op0->hasOneUse()) {
// xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
- return new ICmpInst(ICI->getInversePredicate(),
+ return new ICmpInst(*Context, ICI->getInversePredicate(),
ICI->getOperand(0), ICI->getOperand(1));
if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
- return new FCmpInst(FCI->getInversePredicate(),
+ return new FCmpInst(*Context, FCI->getInversePredicate(),
FCI->getOperand(0), FCI->getOperand(1));
}
if (CI->hasOneUse() && Op0C->hasOneUse()) {
Instruction::CastOps Opcode = Op0C->getOpcode();
if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
- if (RHS == ConstantExpr::getCast(Opcode, ConstantInt::getTrue(),
+ if (RHS == Context->getConstantExprCast(Opcode,
+ Context->getConstantIntTrue(),
Op0C->getDestTy())) {
Instruction *NewCI = InsertNewInstBefore(CmpInst::Create(
+ *Context,
CI->getOpcode(), CI->getInversePredicate(),
CI->getOperand(0), CI->getOperand(1)), I);
NewCI->takeName(CI);
// ~(c-X) == X-c-1 == X+(-c-1)
if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
- Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
- Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
- ConstantInt::get(I.getType(), 1));
+ Constant *NegOp0I0C = Context->getConstantExprNeg(Op0I0C);
+ Constant *ConstantRHS = Context->getConstantExprSub(NegOp0I0C,
+ Context->getConstantInt(I.getType(), 1));
return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
}
if (Op0I->getOpcode() == Instruction::Add) {
// ~(X-c) --> (-c-1)-X
if (RHS->isAllOnesValue()) {
- Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
+ Constant *NegOp0CI = Context->getConstantExprNeg(Op0CI);
return BinaryOperator::CreateSub(
- ConstantExpr::getSub(NegOp0CI,
- ConstantInt::get(I.getType(), 1)),
- Op0I->getOperand(0));
+ Context->getConstantExprSub(NegOp0CI,
+ Context->getConstantInt(I.getType(), 1)),
+ Op0I->getOperand(0));
} else if (RHS->getValue().isSignBit()) {
// (X + C) ^ signbit -> (X + C + signbit)
- Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue());
+ Constant *C =
+ Context->getConstantInt(RHS->getValue() + Op0CI->getValue());
return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
}
} else if (Op0I->getOpcode() == Instruction::Or) {
// (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
- Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
+ Constant *NewRHS = Context->getConstantExprOr(Op0CI, RHS);
// Anything in both C1 and C2 is known to be zero, remove it from
// NewRHS.
- Constant *CommonBits = And(Op0CI, RHS);
- NewRHS = ConstantExpr::getAnd(NewRHS,
- ConstantExpr::getNot(CommonBits));
+ Constant *CommonBits = Context->getConstantExprAnd(Op0CI, RHS);
+ NewRHS = Context->getConstantExprAnd(NewRHS,
+ Context->getConstantExprNot(CommonBits));
AddToWorkList(Op0I);
I.setOperand(0, Op0I->getOperand(0));
I.setOperand(1, NewRHS);
return NV;
}
- if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
+ if (Value *X = dyn_castNotVal(Op0, Context)) // ~A ^ A == -1
if (X == Op1)
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
- if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
+ if (Value *X = dyn_castNotVal(Op1, Context)) // A ^ ~A == -1
if (X == Op0)
- return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
if (Op1I) {
Value *A, *B;
- if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
+ if (match(Op1I, m_Or(m_Value(A), m_Value(B)), *Context)) {
if (A == Op0) { // B^(B|A) == (A|B)^B
Op1I->swapOperands();
I.swapOperands();
I.swapOperands(); // Simplified below.
std::swap(Op0, Op1);
}
- } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
+ } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)), *Context)) {
return ReplaceInstUsesWith(I, B); // A^(A^B) == B
- } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
+ } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)), *Context)) {
return ReplaceInstUsesWith(I, A); // A^(B^A) == B
- } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){
+ } else if (match(Op1I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ Op1I->hasOneUse()){
if (A == Op0) { // A^(A&B) -> A^(B&A)
Op1I->swapOperands();
std::swap(A, B);
BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
if (Op0I) {
Value *A, *B;
- if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) {
+ if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) &&
+ Op0I->hasOneUse()) {
if (A == Op1) // (B|A)^B == (A|B)^B
std::swap(A, B);
if (B == Op1) { // (A|B)^B == A & ~B
InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I);
return BinaryOperator::CreateAnd(A, NotB);
}
- } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
+ } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)), *Context)) {
return ReplaceInstUsesWith(I, B); // (A^B)^A == B
- } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
+ } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)), *Context)) {
return ReplaceInstUsesWith(I, A); // (B^A)^A == B
- } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){
+ } else if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ Op0I->hasOneUse()){
if (A == Op1) // (A&B)^A -> (B&A)^A
std::swap(A, B);
if (B == Op1 && // (B&A)^A == ~B & A
if (Op0I && Op1I) {
Value *A, *B, *C, *D;
// (A & B)^(A | B) -> A ^ B
- if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
- match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
+ if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ match(Op1I, m_Or(m_Value(C), m_Value(D)), *Context)) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
// (A | B)^(A & B) -> A ^ B
- if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
- match(Op1I, m_And(m_Value(C), m_Value(D)))) {
+ if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) &&
+ match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
// (A & B)^(C & D)
if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
- match(Op0I, m_And(m_Value(A), m_Value(B))) &&
- match(Op1I, m_And(m_Value(C), m_Value(D)))) {
+ match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) {
// (X & Y)^(X & Y) -> (Y^Z) & X
Value *X = 0, *Y = 0, *Z = 0;
if (A == C)
// (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
return R;
// fold (xor (cast A), (cast B)) -> (cast (xor A, B))
return Changed ? &I : 0;
}
-/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
-/// overflowed for this type.
-static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1,
- ConstantInt *In2, bool IsSigned = false) {
- Result = cast<ConstantInt>(Add(In1, In2));
+static ConstantInt *ExtractElement(Constant *V, Constant *Idx,
+ LLVMContext *Context) {
+ return cast<ConstantInt>(Context->getConstantExprExtractElement(V, Idx));
+}
+static bool HasAddOverflow(ConstantInt *Result,
+ ConstantInt *In1, ConstantInt *In2,
+ bool IsSigned) {
if (IsSigned)
if (In2->getValue().isNegative())
return Result->getValue().sgt(In1->getValue());
return Result->getValue().ult(In1->getValue());
}
-/// SubWithOverflow - Compute Result = In1-In2, returning true if the result
+/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
/// overflowed for this type.
-static bool SubWithOverflow(ConstantInt *&Result, ConstantInt *In1,
- ConstantInt *In2, bool IsSigned = false) {
- Result = cast<ConstantInt>(Subtract(In1, In2));
+static bool AddWithOverflow(Constant *&Result, Constant *In1,
+ Constant *In2, LLVMContext *Context,
+ bool IsSigned = false) {
+ Result = Context->getConstantExprAdd(In1, In2);
+
+ 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);
+ if (HasAddOverflow(ExtractElement(Result, Idx, Context),
+ ExtractElement(In1, Idx, Context),
+ ExtractElement(In2, Idx, Context),
+ IsSigned))
+ return true;
+ }
+ return false;
+ }
+
+ return HasAddOverflow(cast<ConstantInt>(Result),
+ cast<ConstantInt>(In1), cast<ConstantInt>(In2),
+ IsSigned);
+}
+static bool HasSubOverflow(ConstantInt *Result,
+ ConstantInt *In1, ConstantInt *In2,
+ bool IsSigned) {
if (IsSigned)
if (In2->getValue().isNegative())
return Result->getValue().slt(In1->getValue());
return Result->getValue().ugt(In1->getValue());
}
+/// SubWithOverflow - Compute Result = In1-In2, returning true if the result
+/// overflowed for this type.
+static bool SubWithOverflow(Constant *&Result, Constant *In1,
+ Constant *In2, LLVMContext *Context,
+ bool IsSigned = false) {
+ Result = Context->getConstantExprSub(In1, In2);
+
+ 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);
+ if (HasSubOverflow(ExtractElement(Result, Idx, Context),
+ ExtractElement(In1, Idx, Context),
+ ExtractElement(In2, Idx, Context),
+ IsSigned))
+ return true;
+ }
+ return false;
+ }
+
+ return HasSubOverflow(cast<ConstantInt>(Result),
+ cast<ConstantInt>(In1), cast<ConstantInt>(In2),
+ IsSigned);
+}
+
/// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
/// 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.
TargetData &TD = IC.getTargetData();
gep_type_iterator GTI = gep_type_begin(GEP);
const Type *IntPtrTy = TD.getIntPtrType();
- Value *Result = Constant::getNullValue(IntPtrTy);
+ LLVMContext *Context = IC.getContext();
+ Value *Result = Context->getNullValue(IntPtrTy);
// Build a mask for high order bits.
unsigned IntPtrWidth = TD.getPointerSizeInBits();
for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
++i, ++GTI) {
Value *Op = *i;
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
+ uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
if (OpC->isZero()) continue;
Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
if (ConstantInt *RC = dyn_cast<ConstantInt>(Result))
- Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size));
+ Result =
+ Context->getConstantInt(RC->getValue() + APInt(IntPtrWidth, Size));
else
Result = IC.InsertNewInstBefore(
BinaryOperator::CreateAdd(Result,
- ConstantInt::get(IntPtrTy, Size),
+ Context->getConstantInt(IntPtrTy, Size),
GEP->getName()+".offs"), I);
continue;
}
- Constant *Scale = ConstantInt::get(IntPtrTy, Size);
- Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
- Scale = ConstantExpr::getMul(OC, Scale);
+ Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
+ Constant *OC =
+ Context->getConstantExprIntegerCast(OpC, IntPtrTy, true /*SExt*/);
+ Scale = Context->getConstantExprMul(OC, Scale);
if (Constant *RC = dyn_cast<Constant>(Result))
- Result = ConstantExpr::getAdd(RC, Scale);
+ Result = Context->getConstantExprAdd(RC, Scale);
else {
// Emit an add instruction.
Result = IC.InsertNewInstBefore(
// Convert to correct type.
if (Op->getType() != IntPtrTy) {
if (Constant *OpC = dyn_cast<Constant>(Op))
- Op = ConstantExpr::getSExt(OpC, IntPtrTy);
+ Op = Context->getConstantExprIntegerCast(OpC, IntPtrTy, true);
else
- Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy,
- Op->getName()+".c"), I);
+ Op = IC.InsertNewInstBefore(CastInst::CreateIntegerCast(Op, IntPtrTy,
+ true,
+ Op->getName()+".c"), I);
}
if (Size != 1) {
- Constant *Scale = ConstantInt::get(IntPtrTy, Size);
+ Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
if (Constant *OpC = dyn_cast<Constant>(Op))
- Op = ConstantExpr::getMul(OpC, Scale);
+ Op = Context->getConstantExprMul(OpC, Scale);
else // We'll let instcombine(mul) convert this to a shl if possible.
Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale,
GEP->getName()+".idx"), I);
// Emit an add instruction.
if (isa<Constant>(Op) && isa<Constant>(Result))
- Result = ConstantExpr::getAdd(cast<Constant>(Op),
+ Result = Context->getConstantExprAdd(cast<Constant>(Op),
cast<Constant>(Result));
else
Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result,
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
} else {
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
Offset += Size*CI->getSExtValue();
}
} else {
Value *VariableIdx = GEP->getOperand(i);
// Determine the scale factor of the variable element. For example, this is
// 4 if the variable index is into an array of i32.
- uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
// Verify that there are no other variable indices. If so, emit the hard way.
for (++i, ++GTI; i != e; ++i, ++GTI) {
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
} else {
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
Offset += Size*CI->getSExtValue();
}
}
VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
true /*SExt*/,
VariableIdx->getNameStart(), &I);
- Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
+ Constant *OffsetVal = IC.getContext()->getConstantInt(IntPtrTy, NewOffs);
return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
}
// If not, synthesize the offset the hard way.
if (Offset == 0)
Offset = EmitGEPOffset(GEPLHS, I, *this);
- return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
- Constant::getNullValue(Offset->getType()));
+ return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset,
+ Context->getNullValue(Offset->getType()));
} else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) {
// If the base pointers are different, but the indices are the same, just
// compare the base pointer.
// If all indices are the same, just compare the base pointers.
if (IndicesTheSame)
- return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
+ return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond),
GEPLHS->getOperand(0), GEPRHS->getOperand(0));
// Otherwise, the base pointers are different and the indices are
if (NumDifferences == 0) // SAME GEP?
return ReplaceInstUsesWith(I, // No comparison is needed here.
- ConstantInt::get(Type::Int1Ty,
+ Context->getConstantInt(Type::Int1Ty,
ICmpInst::isTrueWhenEqual(Cond)));
else if (NumDifferences == 1) {
Value *LHSV = GEPLHS->getOperand(DiffOperand);
Value *RHSV = GEPRHS->getOperand(DiffOperand);
// Make sure we do a signed comparison here.
- return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
+ return new ICmpInst(*Context,
+ ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
}
}
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
Value *L = EmitGEPOffset(GEPLHS, I, *this);
Value *R = EmitGEPOffset(GEPRHS, I, *this);
- return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
+ return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), L, R);
}
}
return 0;
// Check to see that the input is converted from an integer type that is small
// enough that preserves all bits. TODO: check here for "known" sign bits.
// This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
- unsigned InputSize = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
+ unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
// If this is a uitofp instruction, we need an extra bit to hold the sign.
bool LHSUnsigned = isa<UIToFPInst>(LHSI);
Pred = ICmpInst::ICMP_NE;
break;
case FCmpInst::FCMP_ORD:
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
case FCmpInst::FCMP_UNO:
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
}
const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
// See if the FP constant is too large for the integer. For example,
// comparing an i8 to 300.0.
- unsigned IntWidth = IntTy->getPrimitiveSizeInBits();
+ unsigned IntWidth = IntTy->getScalarSizeInBits();
if (!LHSUnsigned) {
// If the RHS value is > SignedMax, fold the comparison. This handles +INF
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, ConstantInt::getTrue());
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
}
} 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, ConstantInt::getTrue());
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
}
}
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,ConstantInt::getTrue());
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
}
}
// [0, UMAX], but it may still be fractional. See if it is fractional by
// casting the FP value to the integer value and back, checking for equality.
// Don't do this for zero, because -0.0 is not fractional.
- Constant *RHSInt = ConstantExpr::getFPToSI(RHSC, IntTy);
- if (!RHS.isZero() &&
- ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) != RHSC) {
- // If we had a comparison against a fractional value, we have to adjust the
- // compare predicate and sometimes the value. RHSC is rounded towards zero
- // at this point.
- switch (Pred) {
- default: assert(0 && "Unexpected integer comparison!");
- case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- case ICmpInst::ICMP_ULE:
- // (float)int <= 4.4 --> int <= 4
- // (float)int <= -4.4 --> false
- if (RHS.isNegative())
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- break;
- case ICmpInst::ICMP_SLE:
- // (float)int <= 4.4 --> int <= 4
- // (float)int <= -4.4 --> int < -4
- if (RHS.isNegative())
- Pred = ICmpInst::ICMP_SLT;
- break;
- case ICmpInst::ICMP_ULT:
- // (float)int < -4.4 --> false
- // (float)int < 4.4 --> int <= 4
- if (RHS.isNegative())
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- Pred = ICmpInst::ICMP_ULE;
- break;
- case ICmpInst::ICMP_SLT:
- // (float)int < -4.4 --> int < -4
- // (float)int < 4.4 --> int <= 4
- if (!RHS.isNegative())
- Pred = ICmpInst::ICMP_SLE;
- break;
- case ICmpInst::ICMP_UGT:
- // (float)int > 4.4 --> int > 4
- // (float)int > -4.4 --> true
- if (RHS.isNegative())
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- break;
- case ICmpInst::ICMP_SGT:
- // (float)int > 4.4 --> int > 4
- // (float)int > -4.4 --> int >= -4
- if (RHS.isNegative())
- Pred = ICmpInst::ICMP_SGE;
- break;
- case ICmpInst::ICMP_UGE:
- // (float)int >= -4.4 --> true
- // (float)int >= 4.4 --> int > 4
- if (!RHS.isNegative())
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- Pred = ICmpInst::ICMP_UGT;
- break;
- case ICmpInst::ICMP_SGE:
- // (float)int >= -4.4 --> int >= -4
- // (float)int >= 4.4 --> int > 4
- if (!RHS.isNegative())
- Pred = ICmpInst::ICMP_SGT;
- break;
+ Constant *RHSInt = LHSUnsigned
+ ? Context->getConstantExprFPToUI(RHSC, IntTy)
+ : Context->getConstantExprFPToSI(RHSC, IntTy);
+ if (!RHS.isZero()) {
+ bool Equal = LHSUnsigned
+ ? Context->getConstantExprUIToFP(RHSInt, RHSC->getType()) == RHSC
+ : Context->getConstantExprSIToFP(RHSInt, RHSC->getType()) == RHSC;
+ if (!Equal) {
+ // If we had a comparison against a fractional value, we have to adjust
+ // the compare predicate and sometimes the value. RHSC is rounded towards
+ // zero at this point.
+ switch (Pred) {
+ default: assert(0 && "Unexpected integer comparison!");
+ case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ case ICmpInst::ICMP_ULE:
+ // (float)int <= 4.4 --> int <= 4
+ // (float)int <= -4.4 --> false
+ if (RHS.isNegative())
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ break;
+ case ICmpInst::ICMP_SLE:
+ // (float)int <= 4.4 --> int <= 4
+ // (float)int <= -4.4 --> int < -4
+ if (RHS.isNegative())
+ Pred = ICmpInst::ICMP_SLT;
+ break;
+ case ICmpInst::ICMP_ULT:
+ // (float)int < -4.4 --> false
+ // (float)int < 4.4 --> int <= 4
+ if (RHS.isNegative())
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ Pred = ICmpInst::ICMP_ULE;
+ break;
+ case ICmpInst::ICMP_SLT:
+ // (float)int < -4.4 --> int < -4
+ // (float)int < 4.4 --> int <= 4
+ if (!RHS.isNegative())
+ Pred = ICmpInst::ICMP_SLE;
+ break;
+ case ICmpInst::ICMP_UGT:
+ // (float)int > 4.4 --> int > 4
+ // (float)int > -4.4 --> true
+ if (RHS.isNegative())
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ break;
+ case ICmpInst::ICMP_SGT:
+ // (float)int > 4.4 --> int > 4
+ // (float)int > -4.4 --> int >= -4
+ if (RHS.isNegative())
+ Pred = ICmpInst::ICMP_SGE;
+ break;
+ case ICmpInst::ICMP_UGE:
+ // (float)int >= -4.4 --> true
+ // (float)int >= 4.4 --> int > 4
+ if (!RHS.isNegative())
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ Pred = ICmpInst::ICMP_UGT;
+ break;
+ case ICmpInst::ICMP_SGE:
+ // (float)int >= -4.4 --> int >= -4
+ // (float)int >= 4.4 --> int > 4
+ if (!RHS.isNegative())
+ Pred = ICmpInst::ICMP_SGT;
+ break;
+ }
}
}
// Lower this FP comparison into an appropriate integer version of the
// comparison.
- return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
+ return new ICmpInst(*Context, Pred, LHSI->getOperand(0), RHSInt);
}
Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
// Fold trivial predicates.
if (I.getPredicate() == FCmpInst::FCMP_FALSE)
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
if (I.getPredicate() == FCmpInst::FCMP_TRUE)
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
// 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, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
case FCmpInst::FCMP_ULT: // True if unordered or less than
case FCmpInst::FCMP_UNE: // True if unordered or not equal
// Canonicalize these to be 'fcmp uno %X, 0.0'.
I.setPredicate(FCmpInst::FCMP_UNO);
- I.setOperand(1, Constant::getNullValue(Op0->getType()));
+ I.setOperand(1, Context->getNullValue(Op0->getType()));
return &I;
case FCmpInst::FCMP_ORD: // True if ordered (no nans)
case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
// Canonicalize these to be 'fcmp ord %X, 0.0'.
I.setPredicate(FCmpInst::FCMP_ORD);
- I.setOperand(1, Constant::getNullValue(Op0->getType()));
+ I.setOperand(1, Context->getNullValue(Op0->getType()));
return &I;
}
}
if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef
- return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
+ return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty));
// Handle fcmp with constant RHS
if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
if (CFP->getValueAPF().isNaN()) {
if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and...
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
assert(FCmpInst::isUnordered(I.getPredicate()) &&
"Comparison must be either ordered or unordered!");
// True if unordered.
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
}
}
if (LHSI->hasOneUse()) {
if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
// Fold the known value into the constant operand.
- Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
+ Op1 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC);
// Insert a new FCmp of the other select operand.
- Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
+ Op2 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
LHSI->getOperand(2), RHSC,
I.getName()), I);
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
// Fold the known value into the constant operand.
- Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
+ Op2 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC);
// Insert a new FCmp of the other select operand.
- Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
+ Op1 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
LHSI->getOperand(1), RHSC,
I.getName()), I);
}
// icmp X, X
if (Op0 == Op1)
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
I.isTrueWhenEqual()));
if (isa<UndefValue>(Op1)) // X icmp undef -> undef
- return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
+ return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty));
// icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
// addresses never equal each other! We already know that Op0 != Op1.
isa<ConstantPointerNull>(Op0)) &&
(isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
isa<ConstantPointerNull>(Op1)))
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
!I.isTrueWhenEqual()));
// icmp's with boolean values can always be turned into bitwise operations
}
}
+ unsigned BitWidth = 0;
+ if (TD)
+ BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
+ else if (Ty->isIntOrIntVector())
+ BitWidth = Ty->getScalarSizeInBits();
+
+ bool isSignBit = false;
+
// See if we are doing a comparison with a constant.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
- Value *A, *B;
+ Value *A = 0, *B = 0;
// (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
if (I.isEquality() && CI->isNullValue() &&
- match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
+ match(Op0, m_Sub(m_Value(A), m_Value(B)), *Context)) {
// (icmp cond A B) if cond is equality
- return new ICmpInst(I.getPredicate(), A, B);
+ return new ICmpInst(*Context, I.getPredicate(), A, B);
}
// If we have an icmp le or icmp ge instruction, turn it into the
default: break;
case ICmpInst::ICMP_ULE:
if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI));
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ 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, ConstantInt::getTrue());
- return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI));
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ 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, ConstantInt::getTrue());
- return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI));
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ 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, ConstantInt::getTrue());
- return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI));
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
+ SubOne(CI, Context));
}
- // See if we can fold the comparison based on range information we can get
- // by checking whether bits are known to be zero or one in the input.
- uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
-
// If this comparison is a normal comparison, it demands all
// bits, if it is a sign bit comparison, it only demands the sign bit.
bool UnusedBit;
- bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
-
- if (SimplifyDemandedBits(Op0,
+ isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
+ }
+
+ // See if we can fold the comparison based on range information we can get
+ // by checking whether bits are known to be zero or one in the input.
+ if (BitWidth != 0) {
+ APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
+ APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
+
+ if (SimplifyDemandedBits(I.getOperandUse(0),
isSignBit ? APInt::getSignBit(BitWidth)
: APInt::getAllOnesValue(BitWidth),
- KnownZero, KnownOne, 0))
+ Op0KnownZero, Op0KnownOne, 0))
return &I;
-
+ if (SimplifyDemandedBits(I.getOperandUse(1),
+ APInt::getAllOnesValue(BitWidth),
+ Op1KnownZero, Op1KnownOne, 0))
+ return &I;
+
// Given the known and unknown bits, compute a range that the LHS could be
// in. Compute the Min, Max and RHS values based on the known bits. For the
// EQ and NE we use unsigned values.
- APInt Min(BitWidth, 0), Max(BitWidth, 0);
- if (ICmpInst::isSignedPredicate(I.getPredicate()))
- ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, Max);
- else
- ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,Min,Max);
-
+ APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
+ APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
+ if (ICmpInst::isSignedPredicate(I.getPredicate())) {
+ ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
+ Op0Min, Op0Max);
+ ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
+ Op1Min, Op1Max);
+ } else {
+ ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
+ Op0Min, Op0Max);
+ ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
+ Op1Min, Op1Max);
+ }
+
// If Min and Max are known to be the same, then SimplifyDemandedBits
// figured out that the LHS is a constant. Just constant fold this now so
// that code below can assume that Min != Max.
- if (Min == Max)
- return ReplaceInstUsesWith(I, ConstantExpr::getICmp(I.getPredicate(),
- ConstantInt::get(Min),
- CI));
-
+ if (!isa<Constant>(Op0) && Op0Min == Op0Max)
+ return new ICmpInst(*Context, I.getPredicate(),
+ Context->getConstantInt(Op0Min), Op1);
+ if (!isa<Constant>(Op1) && Op1Min == Op1Max)
+ return new ICmpInst(*Context, I.getPredicate(), Op0,
+ Context->getConstantInt(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.
- const APInt &RHSVal = CI->getValue();
- switch (I.getPredicate()) { // LE/GE have been folded already.
+ switch (I.getPredicate()) {
default: assert(0 && "Unknown icmp opcode!");
case ICmpInst::ICMP_EQ:
- if (Max.ult(RHSVal) || Min.ugt(RHSVal))
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+ if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
break;
case ICmpInst::ICMP_NE:
- if (Max.ult(RHSVal) || Min.ugt(RHSVal))
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+ if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
break;
case ICmpInst::ICMP_ULT:
- if (Max.ult(RHSVal)) // A <u C -> true iff max(A) < C
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- if (Min.uge(RHSVal)) // A <u C -> false iff min(A) >= C
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- if (RHSVal == Max) // A <u MAX -> A != MAX
- return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
- if (RHSVal == Min+1) // A <u MIN+1 -> A == MIN
- return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
-
- // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
- if (CI->isMinValue(true))
- return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
- ConstantInt::getAllOnesValue(Op0->getType()));
+ if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ 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)) {
+ if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
+ return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
+ SubOne(CI, Context));
+
+ // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
+ if (CI->isMinValue(true))
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
+ Context->getConstantIntAllOnesValue(Op0->getType()));
+ }
break;
case ICmpInst::ICMP_UGT:
- if (Min.ugt(RHSVal)) // A >u C -> true iff min(A) > C
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- if (Max.ule(RHSVal)) // A >u C -> false iff max(A) <= C
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
-
- if (RHSVal == Min) // A >u MIN -> A != MIN
- return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
- if (RHSVal == Max-1) // A >u MAX-1 -> A == MAX
- return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
-
- // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
- if (CI->isMaxValue(true))
- return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
- ConstantInt::getNullValue(Op0->getType()));
+ if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+
+ if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
+ return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
+ if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
+ return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
+ AddOne(CI, Context));
+
+ // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
+ if (CI->isMaxValue(true))
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
+ Context->getNullValue(Op0->getType()));
+ }
break;
case ICmpInst::ICMP_SLT:
- if (Max.slt(RHSVal)) // A <s C -> true iff max(A) < C
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- if (Min.sge(RHSVal)) // A <s C -> false iff min(A) >= C
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
- if (RHSVal == Max) // A <s MAX -> A != MAX
- return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
- if (RHSVal == Min+1) // A <s MIN+1 -> A == MIN
- return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
+ if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ 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)) {
+ if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
+ return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
+ SubOne(CI, Context));
+ }
break;
- case ICmpInst::ICMP_SGT:
- if (Min.sgt(RHSVal)) // A >s C -> true iff min(A) > C
- return ReplaceInstUsesWith(I, ConstantInt::getTrue());
- if (Max.sle(RHSVal)) // A >s C -> false iff max(A) <= C
- return ReplaceInstUsesWith(I, ConstantInt::getFalse());
-
- if (RHSVal == Min) // A >s MIN -> A != MIN
- return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
- if (RHSVal == Max-1) // A >s MAX-1 -> A == MAX
- return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
+ case ICmpInst::ICMP_SGT:
+ if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntTrue());
+ if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+
+ if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
+ return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
+ if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
+ return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
+ AddOne(CI, Context));
+ }
+ break;
+ 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());
+ if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ 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());
+ if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ 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());
+ if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
+ 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());
+ if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
+ return ReplaceInstUsesWith(I, Context->getConstantIntFalse());
break;
}
+
+ // Turn a signed comparison into an unsigned one if both operands
+ // are known to have the same sign.
+ if (I.isSignedPredicate() &&
+ ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
+ (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
+ return new ICmpInst(*Context, I.getUnsignedPredicate(), Op0, Op1);
}
// Test if the ICmpInst instruction is used exclusively by a select as
break;
}
if (isAllZeros)
- return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
- Constant::getNullValue(LHSI->getOperand(0)->getType()));
+ return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0),
+ Context->getNullValue(LHSI->getOperand(0)->getType()));
}
break;
if (LHSI->hasOneUse()) {
if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
// Fold the known value into the constant operand.
- Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
+ Op1 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC);
// Insert a new ICmp of the other select operand.
- Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
+ Op2 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
LHSI->getOperand(2), RHSC,
I.getName()), I);
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
// Fold the known value into the constant operand.
- Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
+ Op2 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC);
// Insert a new ICmp of the other select operand.
- Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
+ Op1 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
LHSI->getOperand(1), RHSC,
I.getName()), I);
}
// can assume it is successful and remove the malloc.
if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
AddToWorkList(LHSI);
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
!I.isTrueWhenEqual()));
}
break;
// If Op1 is a constant, we can fold the cast into the constant.
if (Op0->getType() != Op1->getType()) {
if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
- Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
+ Op1 = Context->getConstantExprBitCast(Op1C, Op0->getType());
} else {
// Otherwise, cast the RHS right before the icmp
Op1 = InsertBitCastBefore(Op1, Op0->getType(), I);
}
}
- return new ICmpInst(I.getPredicate(), Op0, Op1);
+ return new ICmpInst(*Context, I.getPredicate(), Op0, Op1);
}
}
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() &&
- Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1) &&
- I.isEquality()) {
+ Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) {
switch (Op0I->getOpcode()) {
default: break;
case Instruction::Add:
case Instruction::Sub:
case Instruction::Xor:
- // a+x icmp eq/ne b+x --> a icmp b
- return new ICmpInst(I.getPredicate(), Op0I->getOperand(0),
- Op1I->getOperand(0));
+ if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
+ return new ICmpInst(*Context, I.getPredicate(), Op0I->getOperand(0),
+ Op1I->getOperand(0));
+ // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
+ if (CI->getValue().isSignBit()) {
+ ICmpInst::Predicate Pred = I.isSignedPredicate()
+ ? I.getUnsignedPredicate()
+ : I.getSignedPredicate();
+ return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
+ Op1I->getOperand(0));
+ }
+
+ if (CI->getValue().isMaxSignedValue()) {
+ ICmpInst::Predicate Pred = I.isSignedPredicate()
+ ? I.getUnsignedPredicate()
+ : I.getSignedPredicate();
+ Pred = I.getSwappedPredicate(Pred);
+ return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
+ Op1I->getOperand(0));
+ }
+ }
break;
case Instruction::Mul:
+ if (!I.isEquality())
+ break;
+
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
// a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
// Mask = -1 >> count-trailing-zeros(Cst).
if (!CI->isZero() && !CI->isOne()) {
const APInt &AP = CI->getValue();
- ConstantInt *Mask = ConstantInt::get(
+ ConstantInt *Mask = Context->getConstantInt(
APInt::getLowBitsSet(AP.getBitWidth(),
AP.getBitWidth() -
AP.countTrailingZeros()));
Mask);
InsertNewInstBefore(And1, I);
InsertNewInstBefore(And2, I);
- return new ICmpInst(I.getPredicate(), And1, And2);
+ return new ICmpInst(*Context, I.getPredicate(), And1, And2);
}
}
break;
// ~x < ~y --> y < x
{ Value *A, *B;
- if (match(Op0, m_Not(m_Value(A))) &&
- match(Op1, m_Not(m_Value(B))))
- return new ICmpInst(I.getPredicate(), B, A);
+ if (match(Op0, m_Not(m_Value(A)), *Context) &&
+ match(Op1, m_Not(m_Value(B)), *Context))
+ return new ICmpInst(*Context, I.getPredicate(), B, A);
}
if (I.isEquality()) {
Value *A, *B, *C, *D;
// -x == -y --> x == y
- if (match(Op0, m_Neg(m_Value(A))) &&
- match(Op1, m_Neg(m_Value(B))))
- return new ICmpInst(I.getPredicate(), A, B);
+ if (match(Op0, m_Neg(m_Value(A)), *Context) &&
+ match(Op1, m_Neg(m_Value(B)), *Context))
+ return new ICmpInst(*Context, I.getPredicate(), A, B);
- if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
+ if (match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) {
if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
Value *OtherVal = A == Op1 ? B : A;
- return new ICmpInst(I.getPredicate(), OtherVal,
- Constant::getNullValue(A->getType()));
+ return new ICmpInst(*Context, I.getPredicate(), OtherVal,
+ Context->getNullValue(A->getType()));
}
- if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
+ if (match(Op1, m_Xor(m_Value(C), m_Value(D)), *Context)) {
// A^c1 == C^c2 --> A == C^(c1^c2)
ConstantInt *C1, *C2;
- if (match(B, m_ConstantInt(C1)) &&
- match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
- Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue());
+ if (match(B, m_ConstantInt(C1), *Context) &&
+ match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) {
+ Constant *NC =
+ Context->getConstantInt(C1->getValue() ^ C2->getValue());
Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
- return new ICmpInst(I.getPredicate(), A,
+ return new ICmpInst(*Context, I.getPredicate(), A,
InsertNewInstBefore(Xor, I));
}
// A^B == A^D -> B == D
- if (A == C) return new ICmpInst(I.getPredicate(), B, D);
- if (A == D) return new ICmpInst(I.getPredicate(), B, C);
- if (B == C) return new ICmpInst(I.getPredicate(), A, D);
- if (B == D) return new ICmpInst(I.getPredicate(), A, C);
+ if (A == C) return new ICmpInst(*Context, I.getPredicate(), B, D);
+ if (A == D) return new ICmpInst(*Context, I.getPredicate(), B, C);
+ if (B == C) return new ICmpInst(*Context, I.getPredicate(), A, D);
+ if (B == D) return new ICmpInst(*Context, I.getPredicate(), A, C);
}
}
- if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
+ if (match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context) &&
(A == Op0 || B == Op0)) {
// A == (A^B) -> B == 0
Value *OtherVal = A == Op0 ? B : A;
- return new ICmpInst(I.getPredicate(), OtherVal,
- Constant::getNullValue(A->getType()));
+ return new ICmpInst(*Context, I.getPredicate(), OtherVal,
+ Context->getNullValue(A->getType()));
}
// (A-B) == A -> B == 0
- if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
- return new ICmpInst(I.getPredicate(), B,
- Constant::getNullValue(B->getType()));
+ if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context))
+ return new ICmpInst(*Context, I.getPredicate(), B,
+ Context->getNullValue(B->getType()));
// A == (A-B) -> B == 0
- if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
- return new ICmpInst(I.getPredicate(), B,
- Constant::getNullValue(B->getType()));
+ if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context))
+ return new ICmpInst(*Context, I.getPredicate(), B,
+ Context->getNullValue(B->getType()));
// (X&Z) == (Y&Z) -> (X^Y) & Z == 0
if (Op0->hasOneUse() && Op1->hasOneUse() &&
- match(Op0, m_And(m_Value(A), m_Value(B))) &&
- match(Op1, m_And(m_Value(C), m_Value(D)))) {
+ match(Op0, m_And(m_Value(A), m_Value(B)), *Context) &&
+ match(Op1, m_And(m_Value(C), m_Value(D)), *Context)) {
Value *X = 0, *Y = 0, *Z = 0;
if (A == C) {
Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I);
Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I);
I.setOperand(0, Op1);
- I.setOperand(1, Constant::getNullValue(Op1->getType()));
+ I.setOperand(1, Context->getNullValue(Op1->getType()));
return &I;
}
}
// of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
// C2 (CI). By solving for X we can turn this into a range check
// instead of computing a divide.
- ConstantInt *Prod = Multiply(CmpRHS, DivRHS);
+ Constant *Prod = Context->getConstantExprMul(CmpRHS, DivRHS);
// Determine if the product overflows by seeing if the product is
// not equal to the divide. Make sure we do the same kind of divide
// as in the LHS instruction that we're folding.
- bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
- ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
+ bool ProdOV = (DivIsSigned ? Context->getConstantExprSDiv(Prod, DivRHS) :
+ Context->getConstantExprUDiv(Prod, DivRHS)) != CmpRHS;
// Get the ICmp opcode
ICmpInst::Predicate Pred = ICI.getPredicate();
// overflow variable is set to 0 if it's corresponding bound variable is valid
// -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
int LoOverflow = 0, HiOverflow = 0;
- ConstantInt *LoBound = 0, *HiBound = 0;
+ Constant *LoBound = 0, *HiBound = 0;
if (!DivIsSigned) { // udiv
// e.g. X/5 op 3 --> [15, 20)
LoBound = Prod;
HiOverflow = LoOverflow = ProdOV;
if (!HiOverflow)
- HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false);
+ HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, Context, false);
} else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
if (CmpRHSV == 0) { // (X / pos) op 0
// Can't overflow. e.g. X/2 op 0 --> [-1, 2)
- LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
+ LoBound = cast<ConstantInt>(Context->getConstantExprNeg(SubOne(DivRHS,
+ Context)));
HiBound = DivRHS;
} else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
HiOverflow = LoOverflow = ProdOV;
if (!HiOverflow)
- HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true);
+ HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, Context, true);
} else { // (X / pos) op neg
// e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
- HiBound = AddOne(Prod);
+ HiBound = AddOne(Prod, Context);
LoOverflow = HiOverflow = ProdOV ? -1 : 0;
if (!LoOverflow) {
- ConstantInt* DivNeg = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
- LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg,
+ ConstantInt* DivNeg =
+ cast<ConstantInt>(Context->getConstantExprNeg(DivRHS));
+ LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, Context,
true) ? -1 : 0;
}
}
} else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
if (CmpRHSV == 0) { // (X / neg) op 0
// e.g. X/-5 op 0 --> [-4, 5)
- LoBound = AddOne(DivRHS);
- HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
+ LoBound = AddOne(DivRHS, Context);
+ HiBound = cast<ConstantInt>(Context->getConstantExprNeg(DivRHS));
if (HiBound == DivRHS) { // -INTMIN = INTMIN
HiOverflow = 1; // [INTMIN+1, overflow)
HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN
}
} else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
// e.g. X/-5 op 3 --> [-19, -14)
- HiBound = AddOne(Prod);
+ HiBound = AddOne(Prod, Context);
HiOverflow = LoOverflow = ProdOV ? -1 : 0;
if (!LoOverflow)
- LoOverflow = AddWithOverflow(LoBound, HiBound, DivRHS, true) ? -1 : 0;
+ LoOverflow = AddWithOverflow(LoBound, HiBound,
+ DivRHS, Context, true) ? -1 : 0;
} else { // (X / neg) op neg
LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
LoOverflow = HiOverflow = ProdOV;
if (!HiOverflow)
- HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, true);
+ HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, Context, true);
}
// Dividing by a negative swaps the condition. LT <-> GT
default: assert(0 && "Unhandled icmp opcode!");
case ICmpInst::ICMP_EQ:
if (LoOverflow && HiOverflow)
- return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
else if (HiOverflow)
- return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
+ return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, LoBound);
else if (LoOverflow)
- return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
+ return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, HiBound);
else
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
case ICmpInst::ICMP_NE:
if (LoOverflow && HiOverflow)
- return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
else if (HiOverflow)
- return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
+ return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, LoBound);
else if (LoOverflow)
- return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
+ return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, HiBound);
else
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_SLT:
if (LoOverflow == +1) // Low bound is greater than input range.
- return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
if (LoOverflow == -1) // Low bound is less than input range.
- return ReplaceInstUsesWith(ICI, ConstantInt::getFalse());
- return new ICmpInst(Pred, X, LoBound);
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
+ 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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
else if (HiOverflow == -1) // High bound less than input range.
- return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
if (Pred == ICmpInst::ICMP_UGT)
- return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound);
else
- return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGE, X, HiBound);
}
}
const APInt &RHSV = RHS->getValue();
switch (LHSI->getOpcode()) {
+ case Instruction::Trunc:
+ if (ICI.isEquality() && LHSI->hasOneUse()) {
+ // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
+ // of the high bits truncated out of x are known.
+ unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
+ SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
+ APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits));
+ APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
+ ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne);
+
+ // If all the high bits are known, we can do this xform.
+ if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
+ // Pull in the high bits from known-ones set.
+ APInt NewRHS(RHS->getValue());
+ NewRHS.zext(SrcBits);
+ NewRHS |= KnownOne;
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
+ Context->getConstantInt(NewRHS));
+ }
+ }
+ break;
+
case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
// If this is a comparison that tests the signbit (X < 0) or (x > -1),
isTrueIfPositive ^= true;
if (isTrueIfPositive)
- return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS));
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal,
+ SubOne(RHS, Context));
else
- return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS));
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal,
+ AddOne(RHS, Context));
+ }
+
+ if (LHSI->hasOneUse()) {
+ // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
+ if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
+ const APInt &SignBit = XorCST->getValue();
+ ICmpInst::Predicate Pred = ICI.isSignedPredicate()
+ ? ICI.getUnsignedPredicate()
+ : ICI.getSignedPredicate();
+ return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
+ Context->getConstantInt(RHSV ^ SignBit));
+ }
+
+ // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
+ if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) {
+ const APInt &NotSignBit = XorCST->getValue();
+ ICmpInst::Predicate Pred = ICI.isSignedPredicate()
+ ? ICI.getUnsignedPredicate()
+ : ICI.getSignedPredicate();
+ Pred = ICI.getSwappedPredicate(Pred);
+ return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
+ Context->getConstantInt(RHSV ^ NotSignBit));
+ }
}
}
break;
NewCI.zext(BitWidth);
Instruction *NewAnd =
BinaryOperator::CreateAnd(Cast->getOperand(0),
- ConstantInt::get(NewCST),LHSI->getName());
+ Context->getConstantInt(NewCST),LHSI->getName());
InsertNewInstBefore(NewAnd, ICI);
- return new ICmpInst(ICI.getPredicate(), NewAnd,
- ConstantInt::get(NewCI));
+ return new ICmpInst(*Context, ICI.getPredicate(), NewAnd,
+ Context->getConstantInt(NewCI));
}
}
if (CanFold) {
Constant *NewCst;
if (Shift->getOpcode() == Instruction::Shl)
- NewCst = ConstantExpr::getLShr(RHS, ShAmt);
+ NewCst = Context->getConstantExprLShr(RHS, ShAmt);
else
- NewCst = ConstantExpr::getShl(RHS, ShAmt);
+ NewCst = Context->getConstantExprShl(RHS, ShAmt);
// Check to see if we are shifting out any of the bits being
// compared.
- if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) {
+ if (Context->getConstantExpr(Shift->getOpcode(),
+ NewCst, ShAmt) != RHS) {
// If we shifted bits out, the fold is not going to work out.
// 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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
- return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
} else {
ICI.setOperand(1, NewCst);
Constant *NewAndCST;
if (Shift->getOpcode() == Instruction::Shl)
- NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
+ NewAndCST = Context->getConstantExprLShr(AndCST, ShAmt);
else
- NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
+ NewAndCST = Context->getConstantExprShl(AndCST, ShAmt);
LHSI->setOperand(1, NewAndCST);
LHSI->setOperand(0, Shift->getOperand(0));
AddToWorkList(Shift); // Shift is dead.
// of a loop if Y is invariant and X is not.
if (Shift && Shift->hasOneUse() && RHSV == 0 &&
ICI.isEquality() && !Shift->isArithmeticShift() &&
- isa<Instruction>(Shift->getOperand(0))) {
+ !isa<Constant>(Shift->getOperand(0))) {
// Compute C << Y.
Value *NS;
if (Shift->getOpcode() == Instruction::LShr) {
// If we are comparing against bits always shifted out, the
// comparison cannot succeed.
Constant *Comp =
- ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt);
+ Context->getConstantExprShl(Context->getConstantExprLShr(RHS, ShAmt),
+ ShAmt);
if (Comp != RHS) {// Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = Context->getConstantInt(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 =
- ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal));
+ Context->getConstantInt(APInt::getLowBitsSet(TypeBits,
+ TypeBits-ShAmtVal));
Instruction *AndI =
BinaryOperator::CreateAnd(LHSI->getOperand(0),
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(ICI.getPredicate(), And,
- ConstantInt::get(RHSV.lshr(ShAmtVal)));
+ return new ICmpInst(*Context, ICI.getPredicate(), And,
+ Context->getConstantInt(RHSV.lshr(ShAmtVal)));
}
}
if (LHSI->hasOneUse() &&
isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
// (X << 31) <s 0 --> (X&1) != 0
- Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) <<
+ Constant *Mask = Context->getConstantInt(APInt(TypeBits, 1) <<
(TypeBits-ShAmt->getZExtValue()-1));
Instruction *AndI =
BinaryOperator::CreateAnd(LHSI->getOperand(0),
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
- And, Constant::getNullValue(And->getType()));
+ return new ICmpInst(*Context,
+ TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
+ And, Context->getNullValue(And->getType()));
}
break;
}
if (Comp != RHSV) { // Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst);
}
if (LHSI->hasOneUse() &&
MaskedValueIsZero(LHSI->getOperand(0),
APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
- return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
- ConstantExpr::getShl(RHS, ShAmt));
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
+ Context->getConstantExprShl(RHS, ShAmt));
}
if (LHSI->hasOneUse()) {
// Otherwise strength reduce the shift into an and.
APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
- Constant *Mask = ConstantInt::get(Val);
+ Constant *Mask = Context->getConstantInt(Val);
Instruction *AndI =
BinaryOperator::CreateAnd(LHSI->getOperand(0),
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(ICI.getPredicate(), And,
- ConstantExpr::getShl(RHS, ShAmt));
+ return new ICmpInst(*Context, ICI.getPredicate(), And,
+ Context->getConstantExprShl(RHS, ShAmt));
}
break;
}
if (ICI.isSignedPredicate()) {
if (CR.getLower().isSignBit()) {
- return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
- ConstantInt::get(CR.getUpper()));
+ return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0),
+ Context->getConstantInt(CR.getUpper()));
} else if (CR.getUpper().isSignBit()) {
- return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
- ConstantInt::get(CR.getLower()));
+ return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0),
+ Context->getConstantInt(CR.getLower()));
}
} else {
if (CR.getLower().isMinValue()) {
- return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
- ConstantInt::get(CR.getUpper()));
+ return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0),
+ Context->getConstantInt(CR.getUpper()));
} else if (CR.getUpper().isMinValue()) {
- return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
- ConstantInt::get(CR.getLower()));
+ return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0),
+ Context->getConstantInt(CR.getLower()));
}
}
}
BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1),
BO->getName());
InsertNewInstBefore(NewRem, ICI);
- return new ICmpInst(ICI.getPredicate(), NewRem,
- Constant::getNullValue(BO->getType()));
+ return new ICmpInst(*Context, ICI.getPredicate(), NewRem,
+ Context->getNullValue(BO->getType()));
}
}
break;
// Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
if (BO->hasOneUse())
- return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
- Subtract(RHS, BOp1C));
+ return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
+ Context->getConstantExprSub(RHS, BOp1C));
} else if (RHSV == 0) {
// Replace ((add A, B) != 0) with (A != -B) if A or B is
// efficiently invertible, or if the add has just this one use.
Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
- if (Value *NegVal = dyn_castNegVal(BOp1))
- return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
- else if (Value *NegVal = dyn_castNegVal(BOp0))
- return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
+ if (Value *NegVal = dyn_castNegVal(BOp1, Context))
+ return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal);
+ else if (Value *NegVal = dyn_castNegVal(BOp0, Context))
+ return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1);
else if (BO->hasOneUse()) {
Instruction *Neg = BinaryOperator::CreateNeg(BOp1);
InsertNewInstBefore(Neg, ICI);
Neg->takeName(BO);
- return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
+ return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg);
}
}
break;
// For the xor case, we can xor two constants together, eliminating
// the explicit xor.
if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
- return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
- ConstantExpr::getXor(RHS, BOC));
+ return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
+ Context->getConstantExprXor(RHS, BOC));
// FALLTHROUGH
case Instruction::Sub:
// Replace (([sub|xor] A, B) != 0) with (A != B)
if (RHSV == 0)
- return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
+ return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
BO->getOperand(1));
break;
// If bits are being or'd in that are not present in the constant we
// are comparing against, then the comparison could never succeed!
if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
- Constant *NotCI = ConstantExpr::getNot(RHS);
- if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
- return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
- isICMP_NE));
+ Constant *NotCI = Context->getConstantExprNot(RHS);
+ if (!Context->getConstantExprAnd(BOC, NotCI)->isNullValue())
+ return ReplaceInstUsesWith(ICI,
+ Context->getConstantInt(Type::Int1Ty,
+ isICMP_NE));
}
break;
// If bits are being compared against that are and'd out, then the
// comparison can never succeed!
if ((RHSV & ~BOC->getValue()) != 0)
- return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty,
- isICMP_NE));
+ return ReplaceInstUsesWith(ICI,
+ Context->getConstantInt(Type::Int1Ty,
+ isICMP_NE));
// If we have ((X & C) == C), turn it into ((X & C) != 0).
if (RHS == BOC && RHSV.isPowerOf2())
- return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
+ return new ICmpInst(*Context, isICMP_NE ? ICmpInst::ICMP_EQ :
ICmpInst::ICMP_NE, LHSI,
- Constant::getNullValue(RHS->getType()));
+ Context->getNullValue(RHS->getType()));
// Replace (and X, (1 << size(X)-1) != 0) with x s< 0
if (BOC->getValue().isSignBit()) {
Value *X = BO->getOperand(0);
- Constant *Zero = Constant::getNullValue(X->getType());
+ Constant *Zero = Context->getNullValue(X->getType());
ICmpInst::Predicate pred = isICMP_NE ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
- return new ICmpInst(pred, X, Zero);
+ return new ICmpInst(*Context, pred, X, Zero);
}
// ((X & ~7) == 0) --> X < 8
if (RHSV == 0 && isHighOnes(BOC)) {
Value *X = BO->getOperand(0);
- Constant *NegX = ConstantExpr::getNeg(BOC);
+ Constant *NegX = Context->getConstantExprNeg(BOC);
ICmpInst::Predicate pred = isICMP_NE ?
ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
- return new ICmpInst(pred, X, NegX);
+ return new ICmpInst(*Context, pred, X, NegX);
}
}
default: break;
if (II->getIntrinsicID() == Intrinsic::bswap) {
AddToWorkList(II);
ICI.setOperand(0, II->getOperand(1));
- ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap()));
+ ICI.setOperand(1, Context->getConstantInt(RHSV.byteSwap()));
return &ICI;
}
}
cast<IntegerType>(DestTy)->getBitWidth()) {
Value *RHSOp = 0;
if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
- RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
+ RHSOp = Context->getConstantExprIntToPtr(RHSC, SrcTy);
} else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
RHSOp = RHSC->getOperand(0);
// If the pointer types don't match, insert a bitcast.
}
if (RHSOp)
- return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSOp);
}
// The code below only handles extension cast instructions, so far.
// Deal with equality cases early.
if (ICI.isEquality())
- return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
// A signed comparison of sign extended values simplifies into a
// signed comparison.
if (isSignedCmp && isSignedExt)
- return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
// The other three cases all fold into an unsigned comparison.
- return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(*Context, ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
}
// If we aren't dealing with a constant on the RHS, exit early
// Compute the constant that would happen if we truncated to SrcTy then
// reextended to DestTy.
- Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
- Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
+ Constant *Res1 = Context->getConstantExprTrunc(CI, SrcTy);
+ Constant *Res2 = Context->getConstantExprCast(LHSCI->getOpcode(),
+ Res1, DestTy);
// If the re-extended constant didn't change...
if (Res2 == CI) {
// Make sure that sign of the Cmp and the sign of the Cast are the same.
// For example, we might have:
- // %A = sext short %X to uint
- // %B = icmp ugt uint %A, 1330
+ // %A = sext i16 %X to i32
+ // %B = icmp ugt i32 %A, 1330
// It is incorrect to transform this into
- // %B = icmp ugt short %X, 1330
+ // %B = icmp ugt i16 %X, 1330
// because %A may have negative value.
//
// However, we allow this when the compare is EQ/NE, because they are
// signless.
if (isSignedExt == isSignedCmp || ICI.isEquality())
- return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
+ return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, Res1);
return 0;
}
// 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, ConstantInt::getFalse());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse());
if (ICI.getPredicate() == ICmpInst::ICMP_NE)
- return ReplaceInstUsesWith(ICI, ConstantInt::getTrue());
+ return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue());
// 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 = ConstantInt::getFalse(); // X < (small) --> false
+ Result = Context->getConstantIntFalse(); // X < (small) --> false
else
- Result = ConstantInt::getTrue(); // X < (large) --> true
+ Result = Context->getConstantIntTrue(); // X < (large) --> true
} else {
// We're performing an unsigned comparison.
if (isSignedExt) {
// We're performing an unsigned comp with a sign extended value.
// This is true if the input is >= 0. [aka >s -1]
- Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy);
- Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp,
- NegOne, ICI.getName()), ICI);
+ Constant *NegOne = Context->getConstantIntAllOnesValue(SrcTy);
+ Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT,
+ LHSCIOp, NegOne, ICI.getName()), ICI);
} else {
// Unsigned extend & unsigned compare -> always true.
- Result = ConstantInt::getTrue();
+ Result = Context->getConstantIntTrue();
}
}
ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
"ICmp should be folded!");
if (Constant *CI = dyn_cast<Constant>(Result))
- return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
+ return ReplaceInstUsesWith(ICI, Context->getConstantExprNot(CI));
return BinaryOperator::CreateNot(Result);
}
if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
if (CSI->isAllOnesValue())
return ReplaceInstUsesWith(I, CSI);
-
+
// See if we can turn a signed shr into an unsigned shr.
- if (!isa<VectorType>(I.getType()) &&
- MaskedValueIsZero(Op0,
- APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
+ if (MaskedValueIsZero(Op0,
+ APInt::getSignBit(I.getType()->getScalarSizeInBits())))
return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
-
+
+ // Arithmetic shifting an all-sign-bit value is a no-op.
+ unsigned NumSignBits = ComputeNumSignBits(Op0);
+ if (NumSignBits == Op0->getType()->getScalarSizeInBits())
+ return ReplaceInstUsesWith(I, Op0);
+
return 0;
}
// shl X, 0 == X and shr X, 0 == X
// shl 0, X == 0 and shr 0, X == 0
- if (Op1 == Constant::getNullValue(Op1->getType()) ||
- Op0 == Constant::getNullValue(Op0->getType()))
+ if (Op1 == Context->getNullValue(Op1->getType()) ||
+ Op0 == Context->getNullValue(Op0->getType()))
return ReplaceInstUsesWith(I, Op0);
if (isa<UndefValue>(Op0)) {
if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
return ReplaceInstUsesWith(I, Op0);
else // undef << X -> 0, undef >>u X -> 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
}
if (isa<UndefValue>(Op1)) {
if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X
return ReplaceInstUsesWith(I, Op0);
else // X << undef, X >>u undef -> 0
- return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
}
+ // See if we can fold away this shift.
+ if (SimplifyDemandedInstructionBits(I))
+ return &I;
+
// Try to fold constant and into select arguments.
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
BinaryOperator &I) {
- bool isLeftShift = I.getOpcode() == Instruction::Shl;
+ bool isLeftShift = I.getOpcode() == Instruction::Shl;
// See if we can simplify any instructions used by the instruction whose sole
// purpose is to compute bits we don't care about.
- uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits();
- APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0);
- if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits),
- KnownZero, KnownOne))
- return &I;
+ uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
- // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
- // of a signed value.
+ // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate
+ // a signed shift.
//
if (Op1->uge(TypeBits)) {
if (I.getOpcode() != Instruction::AShr)
- return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
+ return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType()));
else {
- I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
+ I.setOperand(1, Context->getConstantInt(I.getType(), TypeBits-1));
return &I;
}
}
if (BO->getOpcode() == Instruction::Mul && isLeftShift)
if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
return BinaryOperator::CreateMul(BO->getOperand(0),
- ConstantExpr::getShl(BOOp, Op1));
+ Context->getConstantExprShl(BOOp, Op1));
// Try to fold constant and into select arguments.
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
isa<ConstantInt>(TrOp->getOperand(1))) {
// Okay, we'll do this xform. Make the shift of shift.
- Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
+ Constant *ShAmt = Context->getConstantExprZExt(Op1, TrOp->getType());
Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt,
I.getName());
InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2)
// part of the register be zeros. Emulate this by inserting an AND to
// clear the top bits as needed. This 'and' will usually be zapped by
// other xforms later if dead.
- unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits();
- unsigned DstSize = TI->getType()->getPrimitiveSizeInBits();
+ unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
+ unsigned DstSize = TI->getType()->getScalarSizeInBits();
APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
// The mask we constructed says what the trunc would do if occurring
MaskV = MaskV.lshr(Op1->getZExtValue());
}
- Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV),
- TI->getName());
+ Instruction *And =
+ BinaryOperator::CreateAnd(NSh, Context->getConstantInt(MaskV),
+ TI->getName());
InsertNewInstBefore(And, I); // shift1 & 0x00FF
// Return the value truncated to the interesting size.
// These operators commute.
// Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
- match(Op0BO->getOperand(1), m_Shr(m_Value(V1), m_Specific(Op1)))){
+ match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
+ m_Specific(Op1)), *Context)){
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(0), Op1,
Op0BO->getName());
Op0BO->getOperand(1)->getName());
InsertNewInstBefore(X, I); // (X + (Y << C))
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
- return BinaryOperator::CreateAnd(X, ConstantInt::get(
+ return BinaryOperator::CreateAnd(X, Context->getConstantInt(
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
}
if (isLeftShift && Op0BOOp1->hasOneUse() &&
match(Op0BOOp1,
m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
- m_ConstantInt(CC))) &&
+ m_ConstantInt(CC)), *Context) &&
cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(0), Op1,
Op0BO->getName());
InsertNewInstBefore(YS, I); // (Y << C)
Instruction *XM =
- BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
+ BinaryOperator::CreateAnd(V1,
+ Context->getConstantExprShl(CC, Op1),
V1->getName()+".mask");
InsertNewInstBefore(XM, I); // X & (CC << C)
case Instruction::Sub: {
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
- match(Op0BO->getOperand(0), m_Shr(m_Value(V1), m_Specific(Op1)))){
+ match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
+ m_Specific(Op1)), *Context)){
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(1), Op1,
Op0BO->getName());
Op0BO->getOperand(0)->getName());
InsertNewInstBefore(X, I); // (X + (Y << C))
uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
- return BinaryOperator::CreateAnd(X, ConstantInt::get(
+ return BinaryOperator::CreateAnd(X, Context->getConstantInt(
APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
}
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
match(Op0BO->getOperand(0),
m_And(m_Shr(m_Value(V1), m_Value(V2)),
- m_ConstantInt(CC))) && V2 == Op1 &&
+ m_ConstantInt(CC)), *Context) && V2 == Op1 &&
cast<BinaryOperator>(Op0BO->getOperand(0))
->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getName());
InsertNewInstBefore(YS, I); // (Y << C)
Instruction *XM =
- BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
+ BinaryOperator::CreateAnd(V1,
+ Context->getConstantExprShl(CC, Op1),
V1->getName()+".mask");
InsertNewInstBefore(XM, I); // X & (CC << C)
isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
if (isValid) {
- Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
+ Constant *NewRHS = Context->getConstantExpr(I.getOpcode(), Op0C, Op1);
Instruction *NewShift =
BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1);
Value *X = ShiftOp->getOperand(0);
uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
- if (AmtSum > TypeBits)
- AmtSum = TypeBits;
const IntegerType *Ty = cast<IntegerType>(I.getType());
// Check for (X << c1) << c2 and (X >> c1) >> c2
if (I.getOpcode() == ShiftOp->getOpcode()) {
+ // If this is oversized composite shift, then unsigned shifts get 0, ashr
+ // saturates.
+ if (AmtSum >= TypeBits) {
+ if (I.getOpcode() != Instruction::AShr)
+ return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
+ AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
+ }
+
return BinaryOperator::Create(I.getOpcode(), X,
- ConstantInt::get(Ty, AmtSum));
+ Context->getConstantInt(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, ConstantInt::get(Ty, AmtSum));
+ return BinaryOperator::CreateLShr(X, Context->getConstantInt(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.
+ if (AmtSum >= TypeBits)
+ AmtSum = TypeBits-1;
+
Instruction *Shift =
- BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
+ BinaryOperator::CreateAShr(X, Context->getConstantInt(Ty, AmtSum));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(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, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(X, Context->getConstantInt(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, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(X, Context->getConstantInt(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
case 32 :
case 64 :
case 128:
- SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1);
+ SExtType = Context->getIntegerType(Ty->getBitWidth() - ShiftAmt1);
break;
default: break;
}
assert(ShiftOp->getOpcode() == Instruction::LShr ||
ShiftOp->getOpcode() == Instruction::AShr);
Instruction *Shift =
- BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
+ BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(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, ConstantInt::get(Ty, ShiftDiff));
+ BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(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,
- ConstantInt::get(Ty, ShiftDiff));
+ Context->getConstantInt(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(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, ConstantInt::get(Ty, ShiftDiff));
+ BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
InsertNewInstBefore(Shift, I);
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
- return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
+ return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
}
// We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
/// X*Scale+Offset.
///
static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
- int &Offset) {
+ int &Offset, LLVMContext *Context) {
assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!");
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Offset = CI->getZExtValue();
Scale = 0;
- return ConstantInt::get(Type::Int32Ty, 0);
+ return Context->getConstantInt(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) {
// where C1 is divisible by C2.
unsigned SubScale;
Value *SubVal =
- DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset);
+ DecomposeSimpleLinearExpr(I->getOperand(0), SubScale,
+ Offset, Context);
Offset += RHS->getZExtValue();
Scale = SubScale;
return SubVal;
// If the allocation has multiple uses, only promote it if we are strictly
// increasing the alignment of the resultant allocation. If we keep it the
- // same, we open the door to infinite loops of various kinds.
- if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
+ // same, we open the door to infinite loops of various kinds. (A reference
+ // from a dbg.declare doesn't count as a use for this purpose.)
+ if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) &&
+ CastElTyAlign == AllocElTyAlign) return 0;
- uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
- uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
+ uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy);
+ uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy);
if (CastElTySize == 0 || AllocElTySize == 0) return 0;
// See if we can satisfy the modulus by pulling a scale out of the array
unsigned ArraySizeScale;
int ArrayOffset;
Value *NumElements = // See if the array size is a decomposable linear expr.
- DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
+ DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale,
+ ArrayOffset, Context);
// If we can now satisfy the modulus, by using a non-1 scale, we really can
// do the xform.
Amt = NumElements;
} else {
// If the allocation size is constant, form a constant mul expression
- Amt = ConstantInt::get(Type::Int32Ty, Scale);
+ Amt = Context->getConstantInt(Type::Int32Ty, Scale);
if (isa<ConstantInt>(NumElements))
- Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt));
+ Amt = Context->getConstantExprMul(cast<ConstantInt>(NumElements),
+ cast<ConstantInt>(Amt));
// otherwise multiply the amount and the number of elements
- else if (Scale != 1) {
+ else {
Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp");
Amt = InsertNewInstBefore(Tmp, AI);
}
}
if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
- Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
+ Value *Off = Context->getConstantInt(Type::Int32Ty, Offset, true);
Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
Amt = InsertNewInstBefore(Tmp, AI);
}
InsertNewInstBefore(New, AI);
New->takeName(&AI);
- // If the allocation has multiple uses, insert a cast and change all things
- // that used it to use the new cast. This will also hack on CI, but it will
- // die soon.
- if (!AI.hasOneUse()) {
+ // If the allocation has one real use plus a dbg.declare, just remove the
+ // declare.
+ if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) {
+ EraseInstFromFunction(*DI);
+ }
+ // If the allocation has multiple real uses, insert a cast and change all
+ // things that used it to use the new cast. This will also hack on CI, but it
+ // will die soon.
+ else if (!AI.hasOneUse()) {
AddUsesToWorkList(AI);
// New is the allocation instruction, pointer typed. AI is the original
// allocation instruction, also pointer typed. Thus, cast to use is BitCast.
/// If CastOpc is a sext or zext, we are asking if the low bits of the value can
/// bit computed in a larger type, which is then and'd or sext_in_reg'd to get
/// the final result.
-bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty,
+bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty,
unsigned CastOpc,
- int &NumCastsRemoved) {
+ int &NumCastsRemoved){
// We can always evaluate constants in another type.
- if (isa<ConstantInt>(V))
+ if (isa<Constant>(V))
return true;
Instruction *I = dyn_cast<Instruction>(V);
if (!I) return false;
- const IntegerType *OrigTy = cast<IntegerType>(V->getType());
+ const Type *OrigTy = V->getType();
// If this is an extension or truncate, we can often eliminate it.
if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) {
// require duplicating the instruction in general, which isn't profitable.
if (!I->hasOneUse()) return false;
- switch (I->getOpcode()) {
+ unsigned Opc = I->getOpcode();
+ switch (Opc) {
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
// If we are truncating the result of this SHL, and if it's a shift of a
// constant amount, we can always perform a SHL in a smaller type.
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
- uint32_t BitWidth = Ty->getBitWidth();
- if (BitWidth < OrigTy->getBitWidth() &&
+ uint32_t BitWidth = Ty->getScalarSizeInBits();
+ if (BitWidth < OrigTy->getScalarSizeInBits() &&
CI->getLimitedValue(BitWidth) < BitWidth)
return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc,
NumCastsRemoved);
// lshr iff we know that the bits we would otherwise be shifting in are
// already zeros.
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
- uint32_t OrigBitWidth = OrigTy->getBitWidth();
- uint32_t BitWidth = Ty->getBitWidth();
+ uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
+ uint32_t BitWidth = Ty->getScalarSizeInBits();
if (BitWidth < OrigBitWidth &&
MaskedValueIsZero(I->getOperand(0),
APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
// If this is the same kind of case as our original (e.g. zext+zext), we
// can safely replace it. Note that replacing it does not reduce the number
// of casts in the input.
- if (I->getOpcode() == CastOpc)
+ if (Opc == CastOpc)
+ return true;
+
+ // sext (zext ty1), ty2 -> zext ty2
+ if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt)
return true;
break;
case Instruction::Select: {
Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
bool isSigned) {
if (Constant *C = dyn_cast<Constant>(V))
- return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
+ return Context->getConstantExprIntegerCast(C, Ty,
+ isSigned /*Sext or ZExt*/);
// Otherwise, it must be an instruction.
Instruction *I = cast<Instruction>(V);
Instruction *Res = 0;
- switch (I->getOpcode()) {
+ unsigned Opc = I->getOpcode();
+ switch (Opc) {
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
case Instruction::Shl: {
Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
- Res = BinaryOperator::Create((Instruction::BinaryOps)I->getOpcode(),
- LHS, RHS);
+ Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
break;
}
case Instruction::Trunc:
/// FindElementAtOffset - Given a type and a constant offset, determine whether
/// or not there is a sequence of GEP indices into the type that will land us at
-/// the specified offset. If so, fill them into NewIndices and return true,
-/// otherwise return false.
-static bool FindElementAtOffset(const Type *Ty, int64_t Offset,
- SmallVectorImpl<Value*> &NewIndices,
- const TargetData *TD) {
- if (!Ty->isSized()) return false;
+/// the specified offset. If so, fill them into NewIndices and return the
+/// resultant element type, otherwise return null.
+static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset,
+ SmallVectorImpl<Value*> &NewIndices,
+ const TargetData *TD,
+ LLVMContext *Context) {
+ if (!Ty->isSized()) return 0;
// Start with the index over the outer type. Note that the type size
// might be zero (even if the offset isn't zero) if the indexed type
// is something like [0 x {int, int}]
const Type *IntPtrTy = TD->getIntPtrType();
int64_t FirstIdx = 0;
- if (int64_t TySize = TD->getABITypeSize(Ty)) {
+ if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
FirstIdx = Offset/TySize;
- Offset %= TySize;
+ Offset -= FirstIdx*TySize;
- // Handle silly modulus not returning values values [0..TySize).
+ // Handle hosts where % returns negative instead of values [0..TySize).
if (Offset < 0) {
--FirstIdx;
Offset += TySize;
assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
}
- NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
+ NewIndices.push_back(Context->getConstantInt(IntPtrTy, FirstIdx));
// Index into the types. If we fail, set OrigBase to null.
while (Offset) {
+ // Indexing into tail padding between struct/array elements.
+ if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
+ return 0;
+
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
const StructLayout *SL = TD->getStructLayout(STy);
- if (Offset >= (int64_t)SL->getSizeInBytes()) {
- // We can't index into this, bail out.
- return false;
- }
+ assert(Offset < (int64_t)SL->getSizeInBytes() &&
+ "Offset must stay within the indexed type");
+
unsigned Elt = SL->getElementContainingOffset(Offset);
- NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
+ NewIndices.push_back(Context->getConstantInt(Type::Int32Ty, Elt));
Offset -= SL->getElementOffset(Elt);
Ty = STy->getElementType(Elt);
- } else if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
- const SequentialType *STy = cast<SequentialType>(Ty);
- if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())) {
- NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
- Offset %= EltSize;
- } else {
- NewIndices.push_back(ConstantInt::get(IntPtrTy, 0));
- }
- Ty = STy->getElementType();
+ } 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));
+ Offset %= EltSize;
+ Ty = AT->getElementType();
} else {
- // Otherwise, we can't index into this, bail out.
- return false;
+ // Otherwise, we can't index into the middle of this atomic type, bail.
+ return 0;
}
}
- return true;
+ return Ty;
}
/// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint)
if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) {
if (GEP->hasAllConstantIndices()) {
// We are guaranteed to get a constant from EmitGEPOffset.
- ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
+ ConstantInt *OffsetV =
+ cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this));
int64_t Offset = OffsetV->getSExtValue();
// Get the base pointer input of the bitcast, and the type it points to.
const Type *GEPIdxTy =
cast<PointerType>(OrigBase->getType())->getElementType();
SmallVector<Value*, 8> NewIndices;
- if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD)) {
+ if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD, Context)) {
// If we were able to index down into an element, create the GEP
// and bitcast the result. This eliminates one bitcast, potentially
// two.
return commonCastTransforms(CI);
}
-
+/// isSafeIntegerType - Return true if this is a basic integer type, not a crazy
+/// type like i42. We don't want to introduce operations on random non-legal
+/// integer types where they don't already exist in the code. In the future,
+/// we should consider making this based off target-data, so that 32-bit targets
+/// won't get i64 operations etc.
+static bool isSafeIntegerType(const Type *Ty) {
+ switch (Ty->getPrimitiveSizeInBits()) {
+ case 8:
+ case 16:
+ case 32:
+ case 64:
+ return true;
+ default:
+ return false;
+ }
+}
/// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as
/// integer types. This function implements the common transforms for all those
Value *Src = CI.getOperand(0);
const Type *SrcTy = Src->getType();
const Type *DestTy = CI.getType();
- uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits();
- uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits();
+ uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
+ uint32_t DestBitSize = DestTy->getScalarSizeInBits();
// See if we can simplify any instructions used by the LHS whose sole
// purpose is to compute bits we don't care about.
- APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0);
- if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize),
- KnownZero, KnownOne))
+ if (SimplifyDemandedInstructionBits(CI))
return &CI;
// If the source isn't an instruction or has more than one use then we
// Attempt to propagate the cast into the instruction for int->int casts.
int NumCastsRemoved = 0;
if (!isa<BitCastInst>(CI) &&
- CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy),
+ // Only do this if the dest type is a simple type, don't convert the
+ // expression tree to something weird like i93 unless the source is also
+ // strange.
+ (isSafeIntegerType(DestTy->getScalarType()) ||
+ !isSafeIntegerType(SrcI->getType()->getScalarType())) &&
+ CanEvaluateInDifferentType(SrcI, DestTy,
CI.getOpcode(), NumCastsRemoved)) {
// If this cast is a truncate, evaluting in a different type always
// eliminates the cast, so it is always a win. If this is a zero-extension,
// so we require that the input have eliminated at least one cast. If this
// is a sign extension, we insert two new casts (to do the extension) so we
// require that two casts have been eliminated.
- bool DoXForm;
+ bool DoXForm = false;
+ bool JustReplace = false;
switch (CI.getOpcode()) {
default:
// All the others use floating point so we shouldn't actually
case Instruction::Trunc:
DoXForm = true;
break;
- case Instruction::ZExt:
+ case Instruction::ZExt: {
DoXForm = NumCastsRemoved >= 1;
+ if (!DoXForm && 0) {
+ // If it's unnecessary to issue an AND to clear the high bits, it's
+ // always profitable to do this xform.
+ Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, false);
+ APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
+ if (MaskedValueIsZero(TryRes, Mask))
+ return ReplaceInstUsesWith(CI, TryRes);
+
+ if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
+ if (TryI->use_empty())
+ EraseInstFromFunction(*TryI);
+ }
break;
- case Instruction::SExt:
+ }
+ case Instruction::SExt: {
DoXForm = NumCastsRemoved >= 2;
+ if (!DoXForm && !isa<TruncInst>(SrcI) && 0) {
+ // If we do not have to emit the truncate + sext pair, then it's always
+ // profitable to do this xform.
+ //
+ // It's not safe to eliminate the trunc + sext pair if one of the
+ // eliminated cast is a truncate. e.g.
+ // t2 = trunc i32 t1 to i16
+ // t3 = sext i16 t2 to i32
+ // !=
+ // i32 t1
+ Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, true);
+ unsigned NumSignBits = ComputeNumSignBits(TryRes);
+ if (NumSignBits > (DestBitSize - SrcBitSize))
+ return ReplaceInstUsesWith(CI, TryRes);
+
+ if (Instruction *TryI = dyn_cast<Instruction>(TryRes))
+ if (TryI->use_empty())
+ EraseInstFromFunction(*TryI);
+ }
break;
}
+ }
if (DoXForm) {
+ DOUT << "ICE: EvaluateInDifferentType converting expression type to avoid"
+ << " cast: " << CI;
Value *Res = EvaluateInDifferentType(SrcI, DestTy,
CI.getOpcode() == Instruction::SExt);
+ if (JustReplace)
+ // Just replace this cast with the result.
+ return ReplaceInstUsesWith(CI, Res);
+
assert(Res->getType() == DestTy);
switch (CI.getOpcode()) {
default: assert(0 && "Unknown cast type!");
// Just replace this cast with the result.
return ReplaceInstUsesWith(CI, Res);
case Instruction::ZExt: {
- // We need to emit an AND to clear the high bits.
assert(SrcBitSize < DestBitSize && "Not a zext?");
- Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize,
+
+ // If the high bits are already zero, just replace this cast with the
+ // result.
+ APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize));
+ if (MaskedValueIsZero(Res, Mask))
+ return ReplaceInstUsesWith(CI, Res);
+
+ // We need to emit an AND to clear the high bits.
+ Constant *C = Context->getConstantInt(APInt::getLowBitsSet(DestBitSize,
SrcBitSize));
return BinaryOperator::CreateAnd(Res, C);
}
- case Instruction::SExt:
+ case Instruction::SExt: {
+ // If the high bits are already filled with sign bit, just replace this
+ // cast with the result.
+ unsigned NumSignBits = ComputeNumSignBits(Res);
+ if (NumSignBits > (DestBitSize - SrcBitSize))
+ return ReplaceInstUsesWith(CI, Res);
+
// We need to emit a cast to truncate, then a cast to sext.
return CastInst::Create(Instruction::SExt,
InsertCastBefore(Instruction::Trunc, Res, Src->getType(),
CI), DestTy);
}
+ }
}
}
// 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 == ConstantInt::getTrue() &&
+ Op1 == Context->getConstantIntTrue() &&
(!Op0->hasOneUse() || !isa<CmpInst>(Op0))) {
Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI);
- return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
+ return BinaryOperator::CreateXor(New,
+ Context->getConstantInt(CI.getType(), 1));
}
break;
case Instruction::SDiv:
Value *Src = CI.getOperand(0);
const Type *Ty = CI.getType();
- uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits();
- uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth();
-
- if (Instruction *SrcI = dyn_cast<Instruction>(Src)) {
- switch (SrcI->getOpcode()) {
- default: break;
- case Instruction::LShr:
- // We can shrink lshr to something smaller if we know the bits shifted in
- // are already zeros.
- if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) {
- uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
-
- // Get a mask for the bits shifting in.
- APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
- Value* SrcIOp0 = SrcI->getOperand(0);
- if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) {
- if (ShAmt >= DestBitWidth) // All zeros.
- return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
-
- // Okay, we can shrink this. Truncate the input, then return a new
- // shift.
- Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI);
- Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1),
- Ty, CI);
- return BinaryOperator::CreateLShr(V1, V2);
- }
- } else { // This is a variable shr.
-
- // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is
- // more LLVM instructions, but allows '1 << Y' to be hoisted if
- // loop-invariant and CSE'd.
- if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) {
- Value *One = ConstantInt::get(SrcI->getType(), 1);
-
- Value *V = InsertNewInstBefore(
- BinaryOperator::CreateShl(One, SrcI->getOperand(1),
- "tmp"), CI);
- V = InsertNewInstBefore(BinaryOperator::CreateAnd(V,
- SrcI->getOperand(0),
- "tmp"), CI);
- Value *Zero = Constant::getNullValue(V->getType());
- return new ICmpInst(ICmpInst::ICMP_NE, V, Zero);
- }
- }
- break;
+ uint32_t DestBitWidth = Ty->getScalarSizeInBits();
+ uint32_t SrcBitWidth = Src->getType()->getScalarSizeInBits();
+
+ // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
+ if (DestBitWidth == 1 &&
+ isa<VectorType>(Ty) == isa<VectorType>(Src->getType())) {
+ Constant *One = Context->getConstantInt(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);
+ }
+
+ // Optimize trunc(lshr(), c) to pull the shift through the truncate.
+ ConstantInt *ShAmtV = 0;
+ Value *ShiftOp = 0;
+ if (Src->hasOneUse() &&
+ match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)), *Context)) {
+ uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
+
+ // Get a mask for the bits shifting in.
+ APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
+ if (MaskedValueIsZero(ShiftOp, Mask)) {
+ if (ShAmt >= DestBitWidth) // All zeros.
+ return ReplaceInstUsesWith(CI, Context->getNullValue(Ty));
+
+ // Okay, we can shrink this. Truncate the input, then return a new
+ // shift.
+ Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI);
+ Value *V2 = Context->getConstantExprTrunc(ShAmtV, Ty);
+ return BinaryOperator::CreateLShr(V1, V2);
}
}
if (!DoXform) return ICI;
Value *In = ICI->getOperand(0);
- Value *Sh = ConstantInt::get(In->getType(),
- In->getType()->getPrimitiveSizeInBits()-1);
+ Value *Sh = Context->getConstantInt(In->getType(),
+ In->getType()->getScalarSizeInBits()-1);
In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh,
In->getName()+".lobit"),
CI);
false/*ZExt*/, "tmp", &CI);
if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
- Constant *One = ConstantInt::get(In->getType(), 1);
+ Constant *One = Context->getConstantInt(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 = ConstantInt::get(Type::Int1Ty, isNE);
- Res = ConstantExpr::getZExt(Res, CI.getType());
+ Constant *Res = Context->getConstantInt(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,
- ConstantInt::get(In->getType(), ShiftAmt),
+ Context->getConstantInt(In->getType(), ShiftAmt),
In->getName()+".lobit"), CI);
}
if ((Op1CV != 0) == isNE) { // Toggle the low bit.
- Constant *One = ConstantInt::get(In->getType(), 1);
+ Constant *One = Context->getConstantInt(In->getType(), 1);
In = BinaryOperator::CreateXor(In, One, "tmp");
InsertNewInstBefore(cast<Instruction>(In), CI);
}
Value *Src = CI.getOperand(0);
- // If this is a cast of a cast
- if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
- // If this is a TRUNC followed by a ZEXT then we are dealing with integral
- // types and if the sizes are just right we can convert this into a logical
- // 'and' which will be much cheaper than the pair of casts.
- if (isa<TruncInst>(CSrc)) {
- // Get the sizes of the types involved
- Value *A = CSrc->getOperand(0);
- uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits();
- uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits();
- uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits();
- // If we're actually extending zero bits and the trunc is a no-op
- if (MidSize < DstSize && SrcSize == DstSize) {
- // Replace both of the casts with an And of the type mask.
- APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
- Constant *AndConst = ConstantInt::get(AndValue);
- Instruction *And =
- BinaryOperator::CreateAnd(CSrc->getOperand(0), AndConst);
- // Unfortunately, if the type changed, we need to cast it back.
- if (And->getType() != CI.getType()) {
- And->setName(CSrc->getName()+".mask");
- InsertNewInstBefore(And, CI);
- And = CastInst::CreateIntegerCast(And, CI.getType(), false/*ZExt*/);
- }
- return And;
- }
+ // If this is a TRUNC followed by a ZEXT then we are dealing with integral
+ // types and if the sizes are just right we can convert this into a logical
+ // 'and' which will be much cheaper than the pair of casts.
+ if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
+ // Get the sizes of the types involved. We know that the intermediate type
+ // will be smaller than A or C, but don't know the relation between A and C.
+ Value *A = CSrc->getOperand(0);
+ unsigned SrcSize = A->getType()->getScalarSizeInBits();
+ unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
+ unsigned DstSize = CI.getType()->getScalarSizeInBits();
+ // If we're actually extending zero bits, then if
+ // SrcSize < DstSize: zext(a & mask)
+ // SrcSize == DstSize: a & mask
+ // SrcSize > DstSize: trunc(a) & mask
+ if (SrcSize < DstSize) {
+ APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
+ Constant *AndConst = Context->getConstantInt(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(),
+ 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(),
+ AndValue));
}
}
}
}
+ // zext(trunc(t) & C) -> (t & zext(C)).
+ if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse())
+ if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
+ if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) {
+ Value *TI0 = TI->getOperand(0);
+ if (TI0->getType() == CI.getType())
+ return
+ BinaryOperator::CreateAnd(TI0,
+ Context->getConstantExprZExt(C, CI.getType()));
+ }
+
+ // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)).
+ if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse())
+ if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1)))
+ if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0)))
+ if (And->getOpcode() == Instruction::And && And->hasOneUse() &&
+ And->getOperand(1) == C)
+ if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) {
+ Value *TI0 = TI->getOperand(0);
+ if (TI0->getType() == CI.getType()) {
+ Constant *ZC = Context->getConstantExprZExt(C, CI.getType());
+ Instruction *NewAnd = BinaryOperator::CreateAnd(TI0, ZC, "tmp");
+ InsertNewInstBefore(NewAnd, *And);
+ return BinaryOperator::CreateXor(NewAnd, ZC);
+ }
+ }
+
return 0;
}
// Canonicalize sign-extend from i1 to a select.
if (Src->getType() == Type::Int1Ty)
return SelectInst::Create(Src,
- ConstantInt::getAllOnesValue(CI.getType()),
- Constant::getNullValue(CI.getType()));
+ Context->getConstantIntAllOnesValue(CI.getType()),
+ Context->getNullValue(CI.getType()));
// See if the value being truncated is already sign extended. If so, just
// eliminate the trunc/sext pair.
if (getOpcode(Src) == Instruction::Trunc) {
Value *Op = cast<User>(Src)->getOperand(0);
- unsigned OpBits = cast<IntegerType>(Op->getType())->getBitWidth();
- unsigned MidBits = cast<IntegerType>(Src->getType())->getBitWidth();
- unsigned DestBits = cast<IntegerType>(CI.getType())->getBitWidth();
+ unsigned OpBits = Op->getType()->getScalarSizeInBits();
+ unsigned MidBits = Src->getType()->getScalarSizeInBits();
+ unsigned DestBits = CI.getType()->getScalarSizeInBits();
unsigned NumSignBits = ComputeNumSignBits(Op);
if (OpBits == DestBits) {
Value *A = 0;
ConstantInt *BA = 0, *CA = 0;
if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)),
- m_ConstantInt(CA))) &&
+ m_ConstantInt(CA)), *Context) &&
BA == CA && isa<TruncInst>(A)) {
Value *I = cast<TruncInst>(A)->getOperand(0);
if (I->getType() == CI.getType()) {
- unsigned MidSize = Src->getType()->getPrimitiveSizeInBits();
- unsigned SrcDstSize = CI.getType()->getPrimitiveSizeInBits();
+ unsigned MidSize = Src->getType()->getScalarSizeInBits();
+ unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
- Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
+ Constant *ShAmtV = Context->getConstantInt(CI.getType(), ShAmt);
I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV,
CI.getName()), CI);
return BinaryOperator::CreateAShr(I, ShAmtV);
/// FitsInFPType - Return a Constant* for the specified FP constant if it fits
/// in the specified FP type without changing its value.
-static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
+static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem,
+ LLVMContext *Context) {
bool losesInfo;
APFloat F = CFP->getValueAPF();
(void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
if (!losesInfo)
- return ConstantFP::get(F);
+ return Context->getConstantFP(F);
return 0;
}
/// LookThroughFPExtensions - If this is an fp extension instruction, look
/// through it until we get the source value.
-static Value *LookThroughFPExtensions(Value *V) {
+static Value *LookThroughFPExtensions(Value *V, LLVMContext *Context) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::FPExt)
- return LookThroughFPExtensions(I->getOperand(0));
+ return LookThroughFPExtensions(I->getOperand(0), Context);
// If this value is a constant, return the constant in the smallest FP type
// that can accurately represent it. This allows us to turn
if (CFP->getType() == Type::PPC_FP128Ty)
return V; // No constant folding of this.
// See if the value can be truncated to float and then reextended.
- if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle))
+ if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle, Context))
return V;
if (CFP->getType() == Type::DoubleTy)
return V; // Won't shrink.
- if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble))
+ if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble, Context))
return V;
// Don't try to shrink to various long double types.
}
if (Instruction *I = commonCastTransforms(CI))
return I;
- // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are
+ // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are
// smaller than the destination type, we can eliminate the truncate by doing
- // the add as the smaller type. This applies to add/sub/mul/div as well as
+ // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well as
// many builtins (sqrt, etc).
BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0));
if (OpI && OpI->hasOneUse()) {
switch (OpI->getOpcode()) {
default: break;
- case Instruction::Add:
- case Instruction::Sub:
- case Instruction::Mul:
+ case Instruction::FAdd:
+ case Instruction::FSub:
+ case Instruction::FMul:
case Instruction::FDiv:
case Instruction::FRem:
const Type *SrcTy = OpI->getType();
- Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0));
- Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1));
+ Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0), Context);
+ Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1), Context);
if (LHSTrunc->getType() != SrcTy &&
RHSTrunc->getType() != SrcTy) {
- unsigned DstSize = CI.getType()->getPrimitiveSizeInBits();
+ unsigned DstSize = CI.getType()->getScalarSizeInBits();
// If the source types were both smaller than the destination type of
// the cast, do this xform.
- if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize &&
- RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) {
+ if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize &&
+ RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) {
LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc,
CI.getType(), CI);
RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc,
// 'X' value would cause an undefined result for the fptoui.
if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
OpI->getOperand(0)->getType() == FI.getType() &&
- (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */
+ (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
OpI->getType()->getFPMantissaWidth())
return ReplaceInstUsesWith(FI, OpI->getOperand(0));
// 'X' value would cause an undefined result for the fptoui.
if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
OpI->getOperand(0)->getType() == FI.getType() &&
- (int)FI.getType()->getPrimitiveSizeInBits() <=
+ (int)FI.getType()->getScalarSizeInBits() <=
OpI->getType()->getFPMantissaWidth())
return ReplaceInstUsesWith(FI, OpI->getOperand(0));
return commonCastTransforms(CI);
}
-Instruction *InstCombiner::visitPtrToInt(CastInst &CI) {
+Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) {
+ // If the destination integer type is smaller than the intptr_t type for
+ // this target, do a ptrtoint to intptr_t then do a trunc. This allows the
+ // 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()) {
+ Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0),
+ TD->getIntPtrType(),
+ "tmp"), CI);
+ return new TruncInst(P, CI.getType());
+ }
+
return commonPointerCastTransforms(CI);
}
Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) {
+ // If the source integer type is larger than the intptr_t type for
+ // this target, do a trunc to the intptr_t type, then inttoptr of it. This
+ // 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() >
+ TD->getPointerSizeInBits()) {
+ Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0),
+ TD->getIntPtrType(),
+ "tmp"), CI);
+ return new IntToPtrInst(P, CI.getType());
+ }
+
if (Instruction *I = commonCastTransforms(CI))
return I;
ConstantInt *Cst;
Value *X;
if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)),
- m_ConstantInt(Cst)))) {
+ m_ConstantInt(Cst)), *Context)) {
// If the source and destination operands have the same type, see if this
// is a single-index GEP.
if (X->getType() == CI.getType()) {
// Get the size of the pointee type.
- uint64_t Size = TD->getABITypeSize(DestPointee);
+ uint64_t Size = TD->getTypeAllocSize(DestPointee);
// Convert the constant to intptr type.
APInt Offset = Cst->getValue();
// If Offset is evenly divisible by Size, we can do this xform.
if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
- return GetElementPtrInst::Create(X, ConstantInt::get(Offset));
+ return GetElementPtrInst::Create(X, Context->getConstantInt(Offset));
}
}
// TODO: Could handle other cases, e.g. where add is indexing into field of
// struct etc.
} else if (CI.getOperand(0)->hasOneUse() &&
- match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) {
+ match(CI.getOperand(0), m_Add(m_Value(X),
+ m_ConstantInt(Cst)), *Context)) {
// Otherwise, if this is inttoptr(add x, cst), try to turn this into an
// "inttoptr+GEP" instead of "add+intptr".
// Get the size of the pointee type.
- uint64_t Size = TD->getABITypeSize(DestPointee);
+ uint64_t Size = TD->getTypeAllocSize(DestPointee);
// Convert the constant to intptr type.
APInt Offset = Cst->getValue();
Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(),
"tmp"), CI);
- return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp");
+ return GetElementPtrInst::Create(P,
+ Context->getConstantInt(Offset), "tmp");
}
}
return 0;
// If the source and destination are pointers, and this cast is equivalent
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
// This can enhance SROA and other transforms that want type-safe pointers.
- Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty);
+ Constant *ZeroUInt = Context->getNullValue(Type::Int32Ty);
unsigned NumZeros = 0;
while (SrcElTy != DstElTy &&
isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
/// GetSelectFoldableConstant - For the same transformation as the previous
/// function, return the identity constant that goes into the select.
-static Constant *GetSelectFoldableConstant(Instruction *I) {
+static Constant *GetSelectFoldableConstant(Instruction *I,
+ LLVMContext *Context) {
switch (I->getOpcode()) {
default: assert(0 && "This cannot happen!"); abort();
case Instruction::Add:
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
- return Constant::getNullValue(I->getType());
+ return Context->getNullValue(I->getType());
case Instruction::And:
- return Constant::getAllOnesValue(I->getType());
+ return Context->getAllOnesValue(I->getType());
case Instruction::Mul:
- return ConstantInt::get(I->getType(), 1);
+ return Context->getConstantInt(I->getType(), 1);
}
}
return 0;
}
+static bool isSelect01(Constant *C1, Constant *C2) {
+ ConstantInt *C1I = dyn_cast<ConstantInt>(C1);
+ if (!C1I)
+ return false;
+ ConstantInt *C2I = dyn_cast<ConstantInt>(C2);
+ if (!C2I)
+ return false;
+ return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne());
+}
+
+/// FoldSelectIntoOp - Try fold the select into one of the operands to
+/// facilitate further optimization.
+Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal,
+ Value *FalseVal) {
+ // See the comment above GetSelectFoldableOperands for a description of the
+ // transformation we are doing here.
+ if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
+ if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
+ !isa<Constant>(FalseVal)) {
+ if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
+ unsigned OpToFold = 0;
+ if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
+ OpToFold = 1;
+ } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
+ OpToFold = 2;
+ }
+
+ if (OpToFold) {
+ Constant *C = GetSelectFoldableConstant(TVI, Context);
+ Value *OOp = TVI->getOperand(2-OpToFold);
+ // Avoid creating select between 2 constants unless it's selecting
+ // between 0 and 1.
+ if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
+ Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C);
+ InsertNewInstBefore(NewSel, SI);
+ NewSel->takeName(TVI);
+ if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
+ return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
+ assert(0 && "Unknown instruction!!");
+ }
+ }
+ }
+ }
+ }
+
+ if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
+ if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
+ !isa<Constant>(TrueVal)) {
+ if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
+ unsigned OpToFold = 0;
+ if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
+ OpToFold = 1;
+ } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
+ OpToFold = 2;
+ }
+
+ if (OpToFold) {
+ Constant *C = GetSelectFoldableConstant(FVI, Context);
+ Value *OOp = FVI->getOperand(2-OpToFold);
+ // Avoid creating select between 2 constants unless it's selecting
+ // between 0 and 1.
+ if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
+ Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp);
+ InsertNewInstBefore(NewSel, SI);
+ NewSel->takeName(FVI);
+ if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
+ return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
+ assert(0 && "Unknown instruction!!");
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
/// visitSelectInstWithICmp - Visit a SelectInst that has an
/// ICmpInst as its first operand.
///
if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
return ReplaceInstUsesWith(SI, FalseVal);
// X < C ? X : C-1 --> X > C-1 ? C-1 : X
- Constant *AdjustedRHS = SubOne(CI);
+ Constant *AdjustedRHS = SubOne(CI, Context);
if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
(CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
Pred = ICmpInst::getSwappedPredicate(Pred);
if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
return ReplaceInstUsesWith(SI, FalseVal);
// X > C ? X : C+1 --> X < C+1 ? C+1 : X
- Constant *AdjustedRHS = AddOne(CI);
+ Constant *AdjustedRHS = AddOne(CI, Context);
if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
(CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
Pred = ICmpInst::getSwappedPredicate(Pred);
// (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed
// (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed
CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
- if (match(TrueVal, m_ConstantInt<-1>()) &&
- match(FalseVal, m_ConstantInt<0>()))
+ if (match(TrueVal, m_ConstantInt<-1>(), *Context) &&
+ match(FalseVal, m_ConstantInt<0>(), *Context))
Pred = ICI->getPredicate();
- else if (match(TrueVal, m_ConstantInt<0>()) &&
- match(FalseVal, m_ConstantInt<-1>()))
+ else if (match(TrueVal, m_ConstantInt<0>(), *Context) &&
+ match(FalseVal, m_ConstantInt<-1>(), *Context))
Pred = CmpInst::getInversePredicate(ICI->getPredicate());
if (Pred != CmpInst::BAD_ICMP_PREDICATE) {
if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
(Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
Value *In = ICI->getOperand(0);
- Value *Sh = ConstantInt::get(In->getType(),
- In->getType()->getPrimitiveSizeInBits()-1);
+ Value *Sh = Context->getConstantInt(In->getType(),
+ In->getType()->getScalarSizeInBits()-1);
In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
In->getName()+".lobit"),
*ICI);
// The comparison constant and the result are not neccessarily the
// same width. Make an all-ones value by inserting a AShr.
Value *X = IC->getOperand(0);
- uint32_t Bits = X->getType()->getPrimitiveSizeInBits();
- Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1);
+ uint32_t Bits = X->getType()->getScalarSizeInBits();
+ Constant *ShAmt = Context->getConstantInt(X->getType(), Bits-1);
Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X,
ShAmt, "ones");
InsertNewInstBefore(SRA, SI);
// Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is
// even legal for FP.
- if (TI->getOpcode() == Instruction::Sub &&
- FI->getOpcode() == Instruction::Add) {
+ if ((TI->getOpcode() == Instruction::Sub &&
+ FI->getOpcode() == Instruction::Add) ||
+ (TI->getOpcode() == Instruction::FSub &&
+ FI->getOpcode() == Instruction::FAdd)) {
AddOp = FI; SubOp = TI;
- } else if (FI->getOpcode() == Instruction::Sub &&
- TI->getOpcode() == Instruction::Add) {
+ } else if ((FI->getOpcode() == Instruction::Sub &&
+ TI->getOpcode() == Instruction::Add) ||
+ (FI->getOpcode() == Instruction::FSub &&
+ TI->getOpcode() == Instruction::FAdd)) {
AddOp = TI; SubOp = FI;
}
// select C, (add X, Y), (sub X, Z)
Value *NegVal; // Compute -Z
if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) {
- NegVal = ConstantExpr::getNeg(C);
+ NegVal = Context->getConstantExprNeg(C);
} else {
NegVal = InsertNewInstBefore(
BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI);
// See if we can fold the select into one of our operands.
if (SI.getType()->isInteger()) {
- // See the comment above GetSelectFoldableOperands for a description of the
- // transformation we are doing here.
- if (Instruction *TVI = dyn_cast<Instruction>(TrueVal))
- if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
- !isa<Constant>(FalseVal))
- if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
- unsigned OpToFold = 0;
- if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
- OpToFold = 1;
- } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
- OpToFold = 2;
- }
-
- if (OpToFold) {
- Constant *C = GetSelectFoldableConstant(TVI);
- Instruction *NewSel =
- SelectInst::Create(SI.getCondition(),
- TVI->getOperand(2-OpToFold), C);
- InsertNewInstBefore(NewSel, SI);
- NewSel->takeName(TVI);
- if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI))
- return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
- else {
- assert(0 && "Unknown instruction!!");
- }
- }
- }
-
- if (Instruction *FVI = dyn_cast<Instruction>(FalseVal))
- if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
- !isa<Constant>(TrueVal))
- if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
- unsigned OpToFold = 0;
- if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
- OpToFold = 1;
- } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
- OpToFold = 2;
- }
-
- if (OpToFold) {
- Constant *C = GetSelectFoldableConstant(FVI);
- Instruction *NewSel =
- SelectInst::Create(SI.getCondition(), C,
- FVI->getOperand(2-OpToFold));
- InsertNewInstBefore(NewSel, SI);
- NewSel->takeName(FVI);
- if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI))
- return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
- else
- assert(0 && "Unknown instruction!!");
- }
- }
+ Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal);
+ if (FoldI)
+ return FoldI;
}
if (BinaryOperator::isNot(CondVal)) {
// If there is a large requested alignment and we can, bump up the alignment
// of the global.
if (!GV->isDeclaration()) {
- GV->setAlignment(PrefAlign);
- Align = PrefAlign;
+ if (GV->getAlignment() >= PrefAlign)
+ Align = GV->getAlignment();
+ else {
+ GV->setAlignment(PrefAlign);
+ Align = PrefAlign;
+ }
}
} else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
// If there is a requested alignment and if this is an alloca, round up. We
// don't do this for malloc, because some systems can't respect the request.
if (isa<AllocaInst>(AI)) {
- AI->setAlignment(PrefAlign);
- Align = PrefAlign;
+ if (AI->getAlignment() >= PrefAlign)
+ Align = AI->getAlignment();
+ else {
+ AI->setAlignment(PrefAlign);
+ Align = PrefAlign;
+ }
}
}
unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
unsigned MinAlign = std::min(DstAlign, SrcAlign);
- unsigned CopyAlign = MI->getAlignment()->getZExtValue();
+ unsigned CopyAlign = MI->getAlignment();
if (CopyAlign < MinAlign) {
- MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign));
+ MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
+ MinAlign, false));
return MI;
}
return 0; // If not 1/2/4/8 bytes, exit.
// Use an integer load+store unless we can find something better.
- Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
+ Type *NewPtrTy =
+ Context->getPointerTypeUnqual(Context->getIntegerType(Size<<3));
// Memcpy forces the use of i8* for the source and destination. That means
// that if you're using memcpy to move one double around, you'll get a cast
}
if (SrcETy->isSingleValueType())
- NewPtrTy = PointerType::getUnqual(SrcETy);
+ NewPtrTy = Context->getPointerTypeUnqual(SrcETy);
}
}
InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
// Set the size of the copy to 0, it will be deleted on the next iteration.
- MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
+ MI->setOperand(3, Context->getNullValue(MemOpLength->getType()));
return MI;
}
Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
- if (MI->getAlignment()->getZExtValue() < Alignment) {
- MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment));
+ if (MI->getAlignment() < Alignment) {
+ MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
+ Alignment, false));
return MI;
}
if (!LenC || !FillC || FillC->getType() != Type::Int8Ty)
return 0;
uint64_t Len = LenC->getZExtValue();
- Alignment = MI->getAlignment()->getZExtValue();
+ Alignment = MI->getAlignment();
// If the length is zero, this is a no-op
if (Len == 0) return MI; // memset(d,c,0,a) -> noop
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
- const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8.
+ const Type *ITy = Context->getIntegerType(Len*8); // n=1 -> i8.
Value *Dest = MI->getDest();
- Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI);
+ Dest = InsertBitCastBefore(Dest, Context->getPointerTypeUnqual(ITy), *MI);
// Alignment 0 is identity for alignment 1 for memset, but not store.
if (Alignment == 0) Alignment = 1;
// Extract the fill value and store.
uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
- InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false,
- Alignment), *MI);
+ InsertNewInstBefore(new StoreInst(Context->getConstantInt(ITy, Fill),
+ Dest, false, Alignment), *MI);
// Set the size of the copy to 0, it will be deleted on the next iteration.
- MI->setLength(Constant::getNullValue(LenC->getType()));
+ MI->setLength(Context->getNullValue(LenC->getType()));
return MI;
}
/// the heavy lifting.
///
Instruction *InstCombiner::visitCallInst(CallInst &CI) {
+ // If the caller function is nounwind, mark the call as nounwind, even if the
+ // callee isn't.
+ if (CI.getParent()->getParent()->doesNotThrow() &&
+ !CI.doesNotThrow()) {
+ CI.setDoesNotThrow();
+ return &CI;
+ }
+
+
+
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
if (!II) return visitCallSite(&CI);
// If we can determine a pointer alignment that is bigger than currently
// set, update the alignment.
- if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) {
+ if (isa<MemTransferInst>(MI)) {
if (Instruction *I = SimplifyMemTransfer(MI))
return I;
} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
// Turn X86 loadups -> load if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
Value *Ptr = InsertBitCastBefore(II->getOperand(1),
- PointerType::getUnqual(II->getType()),
+ Context->getPointerTypeUnqual(II->getType()),
CI);
return new LoadInst(Ptr);
}
// Turn stvx -> store if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
const Type *OpPtrTy =
- PointerType::getUnqual(II->getOperand(1)->getType());
+ Context->getPointerTypeUnqual(II->getOperand(1)->getType());
Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI);
return new StoreInst(II->getOperand(1), Ptr);
}
// Turn X86 storeu -> store if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
const Type *OpPtrTy =
- PointerType::getUnqual(II->getOperand(2)->getType());
+ Context->getPointerTypeUnqual(II->getOperand(2)->getType());
Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI);
return new StoreInst(II->getOperand(2), Ptr);
}
case Intrinsic::x86_sse_cvttss2si: {
// These intrinsics only demands the 0th element of its input vector. If
// we can simplify the input based on that, do so now.
- uint64_t UndefElts;
- if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1,
+ unsigned VWidth =
+ cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
+ APInt DemandedElts(VWidth, 1);
+ APInt UndefElts(VWidth, 0);
+ if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
UndefElts)) {
II->setOperand(1, V);
return II;
// Cast the input vectors to byte vectors.
Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI);
Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI);
- Value *Result = UndefValue::get(Op0->getType());
+ Value *Result = Context->getUndef(Op0->getType());
// Only extract each element once.
Value *ExtractedElts[32];
const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
if (!SrcTy->isSized() || !DstTy->isSized())
return false;
- if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy))
+ if (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(ConstantInt::getTrue(),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
- OldCall);
+ new StoreInst(Context->getConstantIntTrue(),
+ Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)),
+ OldCall);
if (!OldCall->use_empty())
- OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
+ OldCall->replaceAllUsesWith(Context->getUndef(OldCall->getType()));
if (isa<CallInst>(OldCall)) // Not worth removing an invoke here.
return EraseInstFromFunction(*OldCall);
return 0;
// 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(ConstantInt::getTrue(),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
+ new StoreInst(Context->getConstantIntTrue(),
+ Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)),
CS.getInstruction());
if (!CS.getInstruction()->use_empty())
CS.getInstruction()->
- replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
+ replaceAllUsesWith(Context->getUndef(CS.getInstruction()->getType()));
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
// Don't break the CFG, insert a dummy cond branch.
BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
- ConstantInt::getTrue(), II);
+ Context->getConstantIntTrue(), II);
}
return EraseInstFromFunction(*CS.getInstruction());
}
// If the function takes more arguments than the call was taking, add them
// now...
for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
- Args.push_back(Constant::getNullValue(FT->getParamType(i)));
+ Args.push_back(Context->getNullValue(FT->getParamType(i)));
// If we are removing arguments to the function, emit an obnoxious warning...
if (FT->getNumParams() < NumActualArgs) {
}
AddUsersToWorkList(*Caller);
} else {
- NV = UndefValue::get(Caller->getType());
+ NV = Context->getUndef(Caller->getType());
}
}
// Replace the trampoline call with a direct call. Let the generic
// code sort out any function type mismatches.
FunctionType *NewFTy =
- FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
- Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ?
- NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy));
+ Context->getFunctionType(FTy->getReturnType(), NewTypes,
+ FTy->isVarArg());
+ Constant *NewCallee =
+ NestF->getType() == Context->getPointerTypeUnqual(NewFTy) ?
+ NestF : Context->getConstantExprBitCast(NestF,
+ Context->getPointerTypeUnqual(NewFTy));
const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),NewAttrs.end());
Instruction *NewCaller;
// parameter, there is no need to adjust the argument list. Let the generic
// code sort out any function type mismatches.
Constant *NewCallee =
- NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy);
+ NestF->getType() == PTy ? NestF :
+ Context->getConstantExprBitCast(NestF, PTy);
CS.setCalledFunction(NewCallee);
return CS.getInstruction();
}
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
CmpInst *CIOp = cast<CmpInst>(FirstInst);
- return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal,
- RHSVal);
+ return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(),
+ LHSVal, RHSVal);
}
Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(),
FirstInst->op_end());
+ // This is true if all GEP bases are allocas and if all indices into them are
+ // constants.
+ bool AllBasePointersAreAllocas = true;
// Scan to see if all operands are the same opcode, all have one use, and all
// kill their operands (i.e. the operands have one use).
GEP->getNumOperands() != FirstInst->getNumOperands())
return 0;
+ // Keep track of whether or not all GEPs are of alloca pointers.
+ if (AllBasePointersAreAllocas &&
+ (!isa<AllocaInst>(GEP->getOperand(0)) ||
+ !GEP->hasAllConstantIndices()))
+ AllBasePointersAreAllocas = false;
+
// Compare the operand lists.
for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
if (FirstInst->getOperand(op) == GEP->getOperand(op))
}
}
+ // If all of the base pointers of the PHI'd GEPs are from allocas, don't
+ // bother doing this transformation. At best, this will just save a bit of
+ // offset calculation, but all the predecessors will have to materialize the
+ // stack address into a register anyway. We'd actually rather *clone* the
+ // load up into the predecessors so that we have a load of a gep of an alloca,
+ // which can usually all be folded into the load.
+ if (AllBasePointersAreAllocas)
+ return 0;
+
// Otherwise, this is safe to transform. Insert PHI nodes for each operand
// that is variable.
SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
}
-/// isSafeToSinkLoad - Return true if we know that it is safe sink the load out
-/// of the block that defines it. This means that it must be obvious the value
-/// of the load is not changed from the point of the load to the end of the
-/// block it is in.
+/// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to
+/// sink the load out of the block that defines it. This means that it must be
+/// obvious the value of the load is not changed from the point of the load to
+/// the end of the block it is in.
///
/// Finally, it is safe, but not profitable, to sink a load targetting a
/// non-address-taken alloca. Doing so will cause us to not promote the alloca
/// to a register.
-static bool isSafeToSinkLoad(LoadInst *L) {
+static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
BasicBlock::iterator BBI = L, E = L->getParent()->end();
for (++BBI; BBI != E; ++BBI)
break;
}
- if (!isAddressTaken)
+ if (!isAddressTaken && AI->isStaticAlloca())
return false;
}
+ // If this load is a load from a GEP with a constant offset from an alloca,
+ // then we don't want to sink it. In its present form, it will be
+ // load [constant stack offset]. Sinking it will cause us to have to
+ // materialize the stack addresses in each predecessor in a register only to
+ // do a shared load from register in the successor.
+ if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0)))
+ if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0)))
+ if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
+ return false;
+
return true;
}
// We can't sink the load if the loaded value could be modified between the
// load and the PHI.
if (LI->getParent() != PN.getIncomingBlock(0) ||
- !isSafeToSinkLoad(LI))
+ !isSafeAndProfitableToSinkLoad(LI))
return 0;
// If the PHI is of volatile loads and the load block has multiple
// the load and the PHI.
if (LI->isVolatile() != isVolatile ||
LI->getParent() != PN.getIncomingBlock(i) ||
- !isSafeToSinkLoad(LI))
+ !isSafeAndProfitableToSinkLoad(LI))
return 0;
// If the PHI is of volatile loads and the load block has multiple
if (isVolatile &&
LI->getParent()->getTerminator()->getNumSuccessors() != 1)
return 0;
-
} else if (I->getOperand(1) != ConstantOp) {
return 0;
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
- return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
+ return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(),
PhiVal, ConstantOp);
assert(isa<LoadInst>(FirstInst) && "Unknown operation");
SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
PotentiallyDeadPHIs.insert(&PN);
if (DeadPHICycle(PU, PotentiallyDeadPHIs))
- return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
+ return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType()));
}
// If this phi has a single use, and if that use just computes a value for
if (PHIUser->hasOneUse() &&
(isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
PHIUser->use_back() == &PN) {
- return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
+ return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType()));
}
}
static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy,
Instruction *InsertPoint,
InstCombiner *IC) {
- unsigned PtrSize = DTy->getPrimitiveSizeInBits();
- unsigned VTySize = V->getType()->getPrimitiveSizeInBits();
+ unsigned PtrSize = DTy->getScalarSizeInBits();
+ unsigned VTySize = V->getType()->getScalarSizeInBits();
// We must cast correctly to the pointer type. Ensure that we
// sign extend the integer value if it is smaller as this is
// used for address computation.
return ReplaceInstUsesWith(GEP, PtrOp);
if (isa<UndefValue>(GEP.getOperand(0)))
- return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
+ return ReplaceInstUsesWith(GEP, Context->getUndef(GEP.getType()));
bool HasZeroPointerIndex = false;
if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
const Type *SrcTy = CI->getOperand(0)->getType();
// We can eliminate a cast from i32 to i64 iff the target
// is a 32-bit pointer target.
- if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) {
+ if (SrcTy->getScalarSizeInBits() >= TD->getPointerSizeInBits()) {
MadeChange = true;
*i = CI->getOperand(0);
}
Value *Op = *i;
if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) {
if (Constant *C = dyn_cast<Constant>(Op)) {
- *i = ConstantExpr::getTrunc(C, TD->getIntPtrType());
+ *i = Context->getConstantExprTrunc(C, TD->getIntPtrType());
MadeChange = true;
} else {
Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(),
}
} else if (TD->getTypeSizeInBits(Op->getType()) < TD->getPointerSizeInBits()) {
if (Constant *C = dyn_cast<Constant>(Op)) {
- *i = ConstantExpr::getSExt(C, TD->getIntPtrType());
+ *i = Context->getConstantExprSExt(C, TD->getIntPtrType());
MadeChange = true;
} else {
Op = InsertCastBefore(Instruction::SExt, Op, TD->getIntPtrType(),
// With: T = long A+B; gep %P, T, ...
//
Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
- if (SO1 == Constant::getNullValue(SO1->getType())) {
+ if (SO1 == Context->getNullValue(SO1->getType())) {
Sum = GO1;
- } else if (GO1 == Constant::getNullValue(GO1->getType())) {
+ } else if (GO1 == Context->getNullValue(GO1->getType())) {
Sum = SO1;
} else {
// If they aren't the same type, convert both to an integer of the
// target's pointer size.
if (SO1->getType() != GO1->getType()) {
if (Constant *SO1C = dyn_cast<Constant>(SO1)) {
- SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true);
+ SO1 =
+ Context->getConstantExprIntegerCast(SO1C, GO1->getType(), true);
} else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
- GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
+ GO1 =
+ Context->getConstantExprIntegerCast(GO1C, SO1->getType(), true);
} else {
unsigned PS = TD->getPointerSizeInBits();
if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
}
}
if (isa<Constant>(SO1) && isa<Constant>(GO1))
- Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1));
+ Sum = Context->getConstantExprAdd(cast<Constant>(SO1),
+ cast<Constant>(GO1));
else {
Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
InsertNewInstBefore(cast<Instruction>(Sum), GEP);
Indices.push_back(cast<Constant>(*I));
if (I == E) { // If they are all constants...
- Constant *CE = ConstantExpr::getGetElementPtr(GV,
+ Constant *CE = Context->getConstantExprGetElementPtr(GV,
&Indices[0],Indices.size());
// Replace all uses of the GEP with the new constexpr...
// transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
// into : GEP [10 x i8]* X, i32 0, ...
//
+ // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
+ // into : GEP i8* X, ...
+ //
// This occurs when the program declares an array extern like "int X[];"
- //
const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
const PointerType *XTy = cast<PointerType>(X->getType());
- if (const ArrayType *XATy =
- dyn_cast<ArrayType>(XTy->getElementType()))
- if (const ArrayType *CATy =
- dyn_cast<ArrayType>(CPTy->getElementType()))
+ if (const ArrayType *CATy =
+ dyn_cast<ArrayType>(CPTy->getElementType())) {
+ // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
+ if (CATy->getElementType() == XTy->getElementType()) {
+ // -> GEP i8* X, ...
+ SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
+ return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
+ GEP.getName());
+ } else if (const ArrayType *XATy =
+ dyn_cast<ArrayType>(XTy->getElementType())) {
+ // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
if (CATy->getElementType() == XATy->getElementType()) {
+ // -> GEP [10 x i8]* X, i32 0, ...
// At this point, we know that the cast source type is a pointer
// to an array of the same type as the destination pointer
// array. Because the array type is never stepped over (there
GEP.setOperand(0, X);
return &GEP;
}
+ }
+ }
} else if (GEP.getNumOperands() == 2) {
// Transform things like:
// %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
if (isa<ArrayType>(SrcElTy) &&
- TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
- TD->getABITypeSize(ResElTy)) {
+ TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
+ TD->getTypeAllocSize(ResElTy)) {
Value *Idx[2];
- Idx[0] = Constant::getNullValue(Type::Int32Ty);
+ Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = GEP.getOperand(1);
Value *V = InsertNewInstBefore(
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP);
if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
uint64_t ArrayEltSize =
- TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
+ TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
// Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
// allow either a mul, shift, or constant here.
ConstantInt *Scale = 0;
if (ArrayEltSize == 1) {
NewIdx = GEP.getOperand(1);
- Scale = ConstantInt::get(NewIdx->getType(), 1);
+ Scale =
+ Context->getConstantInt(cast<IntegerType>(NewIdx->getType()), 1);
} else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
- NewIdx = ConstantInt::get(CI->getType(), 1);
+ NewIdx = Context->getConstantInt(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 = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal);
+ Scale = Context->getConstantInt(cast<IntegerType>(Inst->getType()),
+ 1ULL << ShAmtVal);
NewIdx = Inst->getOperand(0);
} else if (Inst->getOpcode() == Instruction::Mul &&
isa<ConstantInt>(Inst->getOperand(1))) {
// out, perform the transformation. Note, we don't know whether Scale is
// signed or not. We'll use unsigned version of division/modulo
// operation after making sure Scale doesn't have the sign bit set.
- if (Scale && Scale->getSExtValue() >= 0LL &&
+ if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
Scale->getZExtValue() % ArrayEltSize == 0) {
- Scale = ConstantInt::get(Scale->getType(),
+ Scale = Context->getConstantInt(Scale->getType(),
Scale->getZExtValue() / ArrayEltSize);
if (Scale->getZExtValue() != 1) {
- Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
+ Constant *C =
+ Context->getConstantExprIntegerCast(Scale, NewIdx->getType(),
false /*ZExt*/);
Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale");
NewIdx = InsertNewInstBefore(Sc, GEP);
// Insert the new GEP instruction.
Value *Idx[2];
- Idx[0] = Constant::getNullValue(Type::Int32Ty);
+ Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = NewIdx;
Instruction *NewGEP =
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
if (!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 = cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this));
+ ConstantInt *OffsetV =
+ cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this));
int64_t Offset = OffsetV->getSExtValue();
// If this GEP instruction doesn't move the pointer, just replace the GEP
SmallVector<Value*, 8> NewIndices;
const Type *InTy =
cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
- if (FindElementAtOffset(InTy, Offset, NewIndices, TD)) {
+ if (FindElementAtOffset(InTy, Offset, NewIndices, TD, Context)) {
Instruction *NGEP =
GetElementPtrInst::Create(BCI->getOperand(0), NewIndices.begin(),
NewIndices.end());
if (AI.isArrayAllocation()) { // Check C != 1
if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
const Type *NewTy =
- ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
+ Context->getArrayType(AI.getAllocatedType(), C->getZExtValue());
AllocationInst *New = 0;
// Create and insert the replacement instruction...
InsertNewInstBefore(New, AI);
// Scan to the end of the allocation instructions, to skip over a block of
- // allocas if possible...
+ // allocas if possible...also skip interleaved debug info
//
BasicBlock::iterator It = New;
- while (isa<AllocationInst>(*It)) ++It;
+ while (isa<AllocationInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
// Now that I is pointing to the first non-allocation-inst in the block,
// insert our getelementptr instruction...
//
- Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
+ Value *NullIdx = Context->getNullValue(Type::Int32Ty);
Value *Idx[2];
Idx[0] = NullIdx;
Idx[1] = NullIdx;
// allocation.
return ReplaceInstUsesWith(AI, V);
} else if (isa<UndefValue>(AI.getArraySize())) {
- return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
+ return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType()));
}
}
- // 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.
- if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
- TD->getABITypeSize(AI.getAllocatedType()) == 0)
- return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
+ if (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.
+ if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
+ return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType()));
+
+ // If the alignment is 0 (unspecified), assign it the preferred alignment.
+ if (AI.getAlignment() == 0)
+ AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
+ }
return 0;
}
// free undef -> unreachable.
if (isa<UndefValue>(Op)) {
// Insert a new store to null because we cannot modify the CFG here.
- new StoreInst(ConstantInt::getTrue(),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI);
+ new StoreInst(Context->getConstantIntTrue(),
+ Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), &FI);
return EraseInstFromFunction(FI);
}
const TargetData *TD) {
User *CI = cast<User>(LI.getOperand(0));
Value *CastOp = CI->getOperand(0);
-
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
- // Instead of loading constant c string, use corresponding integer value
- // directly if string length is small enough.
- std::string Str;
- if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
- unsigned len = Str.length();
- const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
- unsigned numBits = Ty->getPrimitiveSizeInBits();
- // Replace LI with immediate integer store.
- if ((numBits >> 3) == len + 1) {
- APInt StrVal(numBits, 0);
- APInt SingleChar(numBits, 0);
- if (TD->isLittleEndian()) {
- for (signed i = len-1; i >= 0; i--) {
- SingleChar = (uint64_t) Str[i];
- StrVal = (StrVal << 8) | SingleChar;
- }
- } else {
- for (unsigned i = 0; i < len; i++) {
- SingleChar = (uint64_t) Str[i];
+ LLVMContext *Context = IC.getContext();
+
+ if (TD) {
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
+ // Instead of loading constant c string, use corresponding integer value
+ // directly if string length is small enough.
+ std::string Str;
+ if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
+ unsigned len = Str.length();
+ const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
+ unsigned numBits = Ty->getPrimitiveSizeInBits();
+ // Replace LI with immediate integer store.
+ if ((numBits >> 3) == len + 1) {
+ APInt StrVal(numBits, 0);
+ APInt SingleChar(numBits, 0);
+ if (TD->isLittleEndian()) {
+ for (signed i = len-1; i >= 0; i--) {
+ SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
+ StrVal = (StrVal << 8) | SingleChar;
+ }
+ } else {
+ for (unsigned i = 0; i < len; i++) {
+ SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
+ StrVal = (StrVal << 8) | SingleChar;
+ }
+ // Append NULL at the end.
+ SingleChar = 0;
StrVal = (StrVal << 8) | SingleChar;
}
- // Append NULL at the end.
- SingleChar = 0;
- StrVal = (StrVal << 8) | SingleChar;
+ Value *NL = Context->getConstantInt(StrVal);
+ return IC.ReplaceInstUsesWith(LI, NL);
}
- Value *NL = ConstantInt::get(StrVal);
- return IC.ReplaceInstUsesWith(LI, NL);
}
}
}
- const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
+ const PointerType *DestTy = cast<PointerType>(CI->getType());
+ const Type *DestPTy = DestTy->getElementType();
if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
+
+ // If the address spaces don't match, don't eliminate the cast.
+ if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
+ return 0;
+
const Type *SrcPTy = SrcTy->getElementType();
if (DestPTy->isInteger() || isa<PointerType>(DestPTy) ||
if (Constant *CSrc = dyn_cast<Constant>(CastOp))
if (ASrcTy->getNumElements() != 0) {
Value *Idxs[2];
- Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
- CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
+ Idxs[0] = Idxs[1] = Context->getNullValue(Type::Int32Ty);
+ CastOp = Context->getConstantExprGetElementPtr(CSrc, Idxs, 2);
SrcTy = cast<PointerType>(CastOp->getType());
SrcPTy = SrcTy->getElementType();
}
return 0;
}
-/// isSafeToLoadUnconditionally - Return true if we know that executing a load
-/// from this value cannot trap. If it is not obviously safe to load from the
-/// specified pointer, we do a quick local scan of the basic block containing
-/// ScanFrom, to determine if the address is already accessed.
-static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
- // If it is an alloca it is always safe to load from.
- if (isa<AllocaInst>(V)) return true;
-
- // If it is a global variable it is mostly safe to load from.
- if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V))
- // Don't try to evaluate aliases. External weak GV can be null.
- return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage();
-
- // Otherwise, be a little bit agressive by scanning the local block where we
- // want to check to see if the pointer is already being loaded or stored
- // from/to. If so, the previous load or store would have already trapped,
- // so there is no harm doing an extra load (also, CSE will later eliminate
- // the load entirely).
- BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
-
- while (BBI != E) {
- --BBI;
-
- // If we see a free or a call (which might do a free) the pointer could be
- // marked invalid.
- if (isa<FreeInst>(BBI) || isa<CallInst>(BBI))
- return false;
-
- if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
- if (LI->getOperand(0) == V) return true;
- } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
- if (SI->getOperand(1) == V) return true;
- }
-
- }
- return false;
-}
-
Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
Value *Op = LI.getOperand(0);
// Attempt to improve the alignment.
- unsigned KnownAlign = GetOrEnforceKnownAlignment(Op);
+ unsigned KnownAlign =
+ GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
if (KnownAlign >
(LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) :
LI.getAlignment()))
// that this code is not reachable. We do this instead of inserting
// an unreachable instruction directly because we cannot modify the
// CFG.
- new StoreInst(UndefValue::get(LI.getType()),
- Constant::getNullValue(Op->getType()), &LI);
- return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
+ new StoreInst(Context->getUndef(LI.getType()),
+ Context->getNullValue(Op->getType()), &LI);
+ return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType()));
}
}
// Insert a new store to null instruction before the load to indicate that
// this code is not reachable. We do this instead of inserting an
// unreachable instruction directly because we cannot modify the CFG.
- new StoreInst(UndefValue::get(LI.getType()),
- Constant::getNullValue(Op->getType()), &LI);
- return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
+ new StoreInst(Context->getUndef(LI.getType()),
+ Context->getNullValue(Op->getType()), &LI);
+ return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType()));
}
// Instcombine load (constant global) into the value loaded.
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
- if (GV->isConstant() && !GV->isDeclaration())
+ if (GV->isConstant() && GV->hasDefinitiveInitializer())
return ReplaceInstUsesWith(LI, GV->getInitializer());
// Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
if (CE->getOpcode() == Instruction::GetElementPtr) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
- if (GV->isConstant() && !GV->isDeclaration())
+ if (GV->isConstant() && GV->hasDefinitiveInitializer())
if (Constant *V =
- ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
+ ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE,
+ Context))
return ReplaceInstUsesWith(LI, V);
if (CE->getOperand(0)->isNullValue()) {
// Insert a new store to null instruction before the load to indicate
// that this code is not reachable. We do this instead of inserting
// an unreachable instruction directly because we cannot modify the
// CFG.
- new StoreInst(UndefValue::get(LI.getType()),
- Constant::getNullValue(Op->getType()), &LI);
- return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
+ new StoreInst(Context->getUndef(LI.getType()),
+ Context->getNullValue(Op->getType()), &LI);
+ return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType()));
}
} else if (CE->isCast()) {
// If this load comes from anywhere in a constant global, and if the global
// is all undef or zero, we know what it loads.
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
- if (GV->isConstant() && GV->hasInitializer()) {
+ if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
if (GV->getInitializer()->isNullValue())
- return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
+ return ReplaceInstUsesWith(LI, Context->getNullValue(LI.getType()));
else if (isa<UndefValue>(GV->getInitializer()))
- return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
+ return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType()));
}
}
}
/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
-/// when possible.
+/// when possible. This makes it generally easy to do alias analysis and/or
+/// SROA/mem2reg of the memory object.
static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
User *CI = cast<User>(SI.getOperand(1));
Value *CastOp = CI->getOperand(0);
+ LLVMContext *Context = IC.getContext();
const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
- if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
- const Type *SrcPTy = SrcTy->getElementType();
-
- if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) {
- // If the source is an array, the code below will not succeed. Check to
- // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
- // constants.
- if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
- if (Constant *CSrc = dyn_cast<Constant>(CastOp))
- if (ASrcTy->getNumElements() != 0) {
- Value* Idxs[2];
- Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
- CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
- SrcTy = cast<PointerType>(CastOp->getType());
- SrcPTy = SrcTy->getElementType();
- }
-
- if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
- IC.getTargetData().getTypeSizeInBits(SrcPTy) ==
- IC.getTargetData().getTypeSizeInBits(DestPTy)) {
+ const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
+ if (SrcTy == 0) return 0;
+
+ const Type *SrcPTy = SrcTy->getElementType();
- // Okay, we are casting from one integer or pointer type to another of
- // the same size. Instead of casting the pointer before
- // the store, cast the value to be stored.
- Value *NewCast;
- Value *SIOp0 = SI.getOperand(0);
- Instruction::CastOps opcode = Instruction::BitCast;
- const Type* CastSrcTy = SIOp0->getType();
- const Type* CastDstTy = SrcPTy;
- if (isa<PointerType>(CastDstTy)) {
- if (CastSrcTy->isInteger())
- opcode = Instruction::IntToPtr;
- } else if (isa<IntegerType>(CastDstTy)) {
- if (isa<PointerType>(SIOp0->getType()))
- opcode = Instruction::PtrToInt;
- }
- if (Constant *C = dyn_cast<Constant>(SIOp0))
- NewCast = ConstantExpr::getCast(opcode, C, CastDstTy);
- else
- NewCast = IC.InsertNewInstBefore(
- CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
- SI);
- return new StoreInst(NewCast, CastOp);
+ if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy))
+ return 0;
+
+ /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
+ /// to its first element. This allows us to handle things like:
+ /// store i32 xxx, (bitcast {foo*, float}* %P to i32*)
+ /// on 32-bit hosts.
+ SmallVector<Value*, 4> NewGEPIndices;
+
+ // If the source is an array, the code below will not succeed. Check to
+ // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
+ // constants.
+ if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) {
+ // Index through pointer.
+ Constant *Zero = Context->getNullValue(Type::Int32Ty);
+ NewGEPIndices.push_back(Zero);
+
+ while (1) {
+ if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) {
+ if (!STy->getNumElements()) /* Struct can be empty {} */
+ break;
+ NewGEPIndices.push_back(Zero);
+ SrcPTy = STy->getElementType(0);
+ } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
+ NewGEPIndices.push_back(Zero);
+ SrcPTy = ATy->getElementType();
+ } else {
+ break;
}
}
+
+ SrcTy = Context->getPointerType(SrcPTy, SrcTy->getAddressSpace());
}
- return 0;
+
+ if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy))
+ return 0;
+
+ // 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() !=
+ cast<PointerType>(CI->getType())->getAddressSpace() ||
+ IC.getTargetData().getTypeSizeInBits(SrcPTy) !=
+ IC.getTargetData().getTypeSizeInBits(DestPTy))
+ return 0;
+
+ // Okay, we are casting from one integer or pointer type to another of
+ // the same size. Instead of casting the pointer before
+ // the store, cast the value to be stored.
+ Value *NewCast;
+ Value *SIOp0 = SI.getOperand(0);
+ Instruction::CastOps opcode = Instruction::BitCast;
+ const Type* CastSrcTy = SIOp0->getType();
+ const Type* CastDstTy = SrcPTy;
+ if (isa<PointerType>(CastDstTy)) {
+ if (CastSrcTy->isInteger())
+ opcode = Instruction::IntToPtr;
+ } else if (isa<IntegerType>(CastDstTy)) {
+ if (isa<PointerType>(SIOp0->getType()))
+ opcode = Instruction::PtrToInt;
+ }
+
+ // SIOp0 is a pointer to aggregate and this is a store to the first field,
+ // emit a GEP to index into its first field.
+ if (!NewGEPIndices.empty()) {
+ if (Constant *C = dyn_cast<Constant>(CastOp))
+ CastOp = Context->getConstantExprGetElementPtr(C, &NewGEPIndices[0],
+ NewGEPIndices.size());
+ else
+ CastOp = IC.InsertNewInstBefore(
+ GetElementPtrInst::Create(CastOp, NewGEPIndices.begin(),
+ NewGEPIndices.end()), SI);
+ }
+
+ if (Constant *C = dyn_cast<Constant>(SIOp0))
+ NewCast = Context->getConstantExprCast(opcode, C, CastDstTy);
+ else
+ NewCast = IC.InsertNewInstBefore(
+ CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
+ SI);
+ return new StoreInst(NewCast, CastOp);
}
/// equivalentAddressValues - Test if A and B will obviously have the same
/// value. This includes recognizing that %t0 and %t1 will have the same
/// value in code like this:
-/// %t0 = getelementptr @a, 0, 3
+/// %t0 = getelementptr \@a, 0, 3
/// store i32 0, i32* %t0
-/// %t1 = getelementptr @a, 0, 3
+/// %t1 = getelementptr \@a, 0, 3
/// %t2 = load i32* %t1
///
static bool equivalentAddressValues(Value *A, Value *B) {
return false;
}
+// If this instruction has two uses, one of which is a llvm.dbg.declare,
+// return the llvm.dbg.declare.
+DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) {
+ if (!V->hasNUses(2))
+ return 0;
+ for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
+ UI != E; ++UI) {
+ if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI))
+ return DI;
+ if (isa<BitCastInst>(UI) && UI->hasOneUse()) {
+ if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin()))
+ return DI;
+ }
+ }
+ return 0;
+}
+
Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
Value *Val = SI.getOperand(0);
Value *Ptr = SI.getOperand(1);
// If the RHS is an alloca with a single use, zapify the store, making the
// alloca dead.
- if (Ptr->hasOneUse() && !SI.isVolatile()) {
- if (isa<AllocaInst>(Ptr)) {
- EraseInstFromFunction(SI);
- ++NumCombined;
- return 0;
- }
-
- if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
- if (isa<AllocaInst>(GEP->getOperand(0)) &&
- GEP->getOperand(0)->hasOneUse()) {
+ // If the RHS is an alloca with a two uses, the other one being a
+ // llvm.dbg.declare, zapify the store and the declare, making the
+ // alloca dead. We must do this to prevent declare's from affecting
+ // codegen.
+ if (!SI.isVolatile()) {
+ if (Ptr->hasOneUse()) {
+ if (isa<AllocaInst>(Ptr)) {
EraseInstFromFunction(SI);
++NumCombined;
return 0;
}
+ if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
+ if (isa<AllocaInst>(GEP->getOperand(0))) {
+ if (GEP->getOperand(0)->hasOneUse()) {
+ EraseInstFromFunction(SI);
+ ++NumCombined;
+ return 0;
+ }
+ if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) {
+ EraseInstFromFunction(*DI);
+ EraseInstFromFunction(SI);
+ ++NumCombined;
+ return 0;
+ }
+ }
+ }
+ }
+ if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) {
+ EraseInstFromFunction(*DI);
+ EraseInstFromFunction(SI);
+ ++NumCombined;
+ return 0;
+ }
}
// Attempt to improve the alignment.
- unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr);
+ 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 consequtive
+ // Do really simple DSE, to catch cases where there are several consecutive
// stores to the same location, separated by a few arithmetic operations. This
// situation often occurs with bitfield accesses.
BasicBlock::iterator BBI = &SI;
for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
--ScanInsts) {
--BBI;
+ // Don't count debug info directives, lest they affect codegen,
+ // and we skip pointer-to-pointer bitcasts, which are NOPs.
+ // It is necessary for correctness to skip those that feed into a
+ // llvm.dbg.declare, as these are not present when debugging is off.
+ if (isa<DbgInfoIntrinsic>(BBI) ||
+ (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
+ ScanInsts++;
+ continue;
+ }
if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
// Prev store isn't volatile, and stores to the same location?
if (SI.isVolatile()) return 0; // Don't hack volatile stores.
// store X, null -> turns into 'unreachable' in SimplifyCFG
- if (isa<ConstantPointerNull>(Ptr)) {
+ if (isa<ConstantPointerNull>(Ptr) &&
+ cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) {
if (!isa<UndefValue>(Val)) {
- SI.setOperand(0, UndefValue::get(Val->getType()));
+ SI.setOperand(0, Context->getUndef(Val->getType()));
if (Instruction *U = dyn_cast<Instruction>(Val))
AddToWorkList(U); // Dropped a use.
++NumCombined;
return Res;
- // If this store is the last instruction in the basic block, and if the block
- // ends with an unconditional branch, try to move it to the successor block.
- BBI = &SI; ++BBI;
+ // If this store is the last instruction in the basic block (possibly
+ // excepting debug info instructions and the pointer bitcasts that feed
+ // into them), and if the block ends with an unconditional branch, try
+ // to move it to the successor block.
+ BBI = &SI;
+ do {
+ ++BBI;
+ } while (isa<DbgInfoIntrinsic>(BBI) ||
+ (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType())));
if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
if (BI->isUnconditional())
if (SimplifyStoreAtEndOfBlock(SI))
// else' case. there is an instruction before the branch.
StoreInst *OtherStore = 0;
if (OtherBr->isUnconditional()) {
- // If this isn't a store, or isn't a store to the same location, bail out.
--BBI;
+ // Skip over debugging info.
+ while (isa<DbgInfoIntrinsic>(BBI) ||
+ (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) {
+ if (BBI==OtherBB->begin())
+ return false;
+ --BBI;
+ }
+ // If this isn't a store, or isn't a store to the same location, bail out.
OtherStore = dyn_cast<StoreInst>(BBI);
if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1))
return false;
Value *X = 0;
BasicBlock *TrueDest;
BasicBlock *FalseDest;
- if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
+ if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest), *Context) &&
!isa<Constant>(X)) {
// Swap Destinations and condition...
BI.setCondition(X);
// Cannonicalize fcmp_one -> fcmp_oeq
FCmpInst::Predicate FPred; Value *Y;
if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
- TrueDest, FalseDest)))
+ TrueDest, FalseDest), *Context))
if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) {
FCmpInst *I = cast<FCmpInst>(BI.getCondition());
FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred);
- Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I);
+ Instruction *NewSCC = new FCmpInst(I, NewPred, X, Y, "");
NewSCC->takeName(I);
// Swap Destinations and condition...
BI.setCondition(NewSCC);
// Cannonicalize icmp_ne -> icmp_eq
ICmpInst::Predicate IPred;
if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
- TrueDest, FalseDest)))
+ TrueDest, FalseDest), *Context))
if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) {
ICmpInst *I = cast<ICmpInst>(BI.getCondition());
ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred);
- Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I);
+ Instruction *NewSCC = new ICmpInst(I, NewPred, X, Y, "");
NewSCC->takeName(I);
// Swap Destinations and condition...
BI.setCondition(NewSCC);
if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
// change 'switch (X+4) case 1:' into 'switch (X) case -3'
for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
- SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
+ SI.setOperand(i,
+ Context->getConstantExprSub(cast<Constant>(SI.getOperand(i)),
AddRHS));
SI.setOperand(0, I->getOperand(0));
AddToWorkList(I);
if (Constant *C = dyn_cast<Constant>(Agg)) {
if (isa<UndefValue>(C))
- return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
+ return ReplaceInstUsesWith(EV, Context->getUndef(EV.getType()));
if (isa<ConstantAggregateZero>(C))
- return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
+ return ReplaceInstUsesWith(EV, Context->getNullValue(EV.getType()));
if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
// Extract the element indexed by the first index out of the constant
/// FindScalarElement - Given a vector and an element number, see if the scalar
/// value is already around as a register, for example if it were inserted then
/// extracted from the vector.
-static Value *FindScalarElement(Value *V, unsigned EltNo) {
+static Value *FindScalarElement(Value *V, unsigned EltNo,
+ LLVMContext *Context) {
assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
const VectorType *PTy = cast<VectorType>(V->getType());
unsigned Width = PTy->getNumElements();
if (EltNo >= Width) // Out of range access.
- return UndefValue::get(PTy->getElementType());
+ return Context->getUndef(PTy->getElementType());
if (isa<UndefValue>(V))
- return UndefValue::get(PTy->getElementType());
+ return Context->getUndef(PTy->getElementType());
else if (isa<ConstantAggregateZero>(V))
- return Constant::getNullValue(PTy->getElementType());
+ return Context->getNullValue(PTy->getElementType());
else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
return CP->getOperand(EltNo);
else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
// Otherwise, the insertelement doesn't modify the value, recurse on its
// vector input.
- return FindScalarElement(III->getOperand(0), EltNo);
+ return FindScalarElement(III->getOperand(0), EltNo, Context);
} else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
unsigned LHSWidth =
cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
unsigned InEl = getShuffleMask(SVI)[EltNo];
if (InEl < LHSWidth)
- return FindScalarElement(SVI->getOperand(0), InEl);
+ return FindScalarElement(SVI->getOperand(0), InEl, Context);
else if (InEl < LHSWidth*2)
- return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
+ return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth, Context);
else
- return UndefValue::get(PTy->getElementType());
+ return Context->getUndef(PTy->getElementType());
}
// Otherwise, we don't know.
Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
// If vector val is undef, replace extract with scalar undef.
if (isa<UndefValue>(EI.getOperand(0)))
- return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
+ return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
// If vector val is constant 0, replace extract with scalar 0.
if (isa<ConstantAggregateZero>(EI.getOperand(0)))
- return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
+ return ReplaceInstUsesWith(EI, Context->getNullValue(EI.getType()));
if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
// If vector val is constant with all elements the same, replace EI with
// If this is extracting an invalid index, turn this into undef, to avoid
// crashing the code below.
if (IndexVal >= VectorWidth)
- return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
+ return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
// This instruction only demands the single element from the input vector.
// If the input vector has a single use, simplify it based on this use
// property.
if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
- uint64_t UndefElts;
+ APInt UndefElts(VectorWidth, 0);
+ APInt DemandedMask(VectorWidth, 1 << IndexVal);
if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
- 1 << IndexVal,
- UndefElts)) {
+ DemandedMask, UndefElts)) {
EI.setOperand(0, V);
return &EI;
}
}
- if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
+ if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal, Context))
return ReplaceInstUsesWith(EI, Elt);
// If the this extractelement is directly using a bitcast from a vector of
if (const VectorType *VT =
dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
if (VT->getNumElements() == VectorWidth)
- if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
+ if (Value *Elt = FindScalarElement(BCI->getOperand(0),
+ IndexVal, Context))
return new BitCastInst(Elt, EI.getType());
}
}
unsigned AS =
cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
Value *Ptr = InsertBitCastBefore(I->getOperand(0),
- PointerType::get(EI.getType(), AS),EI);
+ Context->getPointerType(EI.getType(), AS),EI);
GetElementPtrInst *GEP =
GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep");
InsertNewInstBefore(GEP, EI);
SrcIdx -= LHSWidth;
Src = SVI->getOperand(1);
} else {
- return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
+ return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
}
return new ExtractElementInst(Src, SrcIdx);
}
/// elements from either LHS or RHS, return the shuffle mask and true.
/// Otherwise, return false.
static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
- std::vector<Constant*> &Mask) {
+ std::vector<Constant*> &Mask,
+ LLVMContext *Context) {
assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
"Invalid CollectSingleShuffleElements");
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
if (isa<UndefValue>(V)) {
- Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumElts, Context->getUndef(Type::Int32Ty));
return true;
} else if (V == LHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
+ Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
return true;
} else if (V == RHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
+ Mask.push_back(Context->getConstantInt(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 (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
// Okay, we can handle this if the vector we are insertinting into is
// transitively ok.
- if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
+ if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
// If so, update the mask to reflect the inserted undef.
- Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty);
+ Mask[InsertedIdx] = Context->getUndef(Type::Int32Ty);
return true;
}
} else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
// Okay, we can handle this if the vector we are insertinting into is
// transitively ok.
- if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
+ if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
// If so, update the mask to reflect the inserted value.
if (EI->getOperand(0) == LHS) {
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, ExtractedIdx);
+ Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
} else {
assert(EI->getOperand(0) == RHS);
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
+ Context->getConstantInt(Type::Int32Ty, ExtractedIdx+NumElts);
}
return true;
/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
/// that computes V and the LHS value of the shuffle.
static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
- Value *&RHS) {
+ Value *&RHS, LLVMContext *Context) {
assert(isa<VectorType>(V->getType()) &&
(RHS == 0 || V->getType() == RHS->getType()) &&
"Invalid shuffle!");
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
if (isa<UndefValue>(V)) {
- Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumElts, Context->getUndef(Type::Int32Ty));
return V;
} else if (isa<ConstantAggregateZero>(V)) {
- Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
+ Mask.assign(NumElts, Context->getConstantInt(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.
// otherwise we'd end up with a shuffle of three inputs.
if (EI->getOperand(0) == RHS || RHS == 0) {
RHS = EI->getOperand(0);
- Value *V = CollectShuffleElements(VecOp, Mask, RHS);
+ Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
+ Context->getConstantInt(Type::Int32Ty, NumElts+ExtractedIdx);
return V;
}
if (VecOp == RHS) {
- Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
+ Value *V = CollectShuffleElements(EI->getOperand(0), Mask,
+ RHS, Context);
// Everything but the extracted element is replaced with the RHS.
for (unsigned i = 0; i != NumElts; ++i) {
if (i != InsertedIdx)
- Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
+ Mask[i] = Context->getConstantInt(Type::Int32Ty, NumElts+i);
}
return V;
}
// If this insertelement is a chain that comes from exactly these two
// vectors, return the vector and the effective shuffle.
- if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
+ if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask,
+ Context))
return EI->getOperand(0);
}
// Otherwise, can't do anything fancy. Return an identity vector.
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
+ Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
return V;
}
return ReplaceInstUsesWith(IE, VecOp);
if (InsertedIdx >= NumVectorElts) // Out of range insert.
- return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
+ return ReplaceInstUsesWith(IE, Context->getUndef(IE.getType()));
// If we are extracting a value from a vector, then inserting it right
// back into the same place, just use the input vector.
// Build a new shuffle mask.
std::vector<Constant*> Mask;
if (isa<UndefValue>(VecOp))
- Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumVectorElts, Context->getUndef(Type::Int32Ty));
else {
assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
- Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
+ Mask.assign(NumVectorElts, Context->getConstantInt(Type::Int32Ty,
NumVectorElts));
}
- Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx);
+ Mask[InsertedIdx] =
+ Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
return new ShuffleVectorInst(EI->getOperand(0), VecOp,
- ConstantVector::get(Mask));
+ Context->getConstantVector(Mask));
}
// If this insertelement isn't used by some other insertelement, turn it
if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
std::vector<Constant*> Mask;
Value *RHS = 0;
- Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
- if (RHS == 0) RHS = UndefValue::get(LHS->getType());
+ Value *LHS = CollectShuffleElements(&IE, Mask, RHS, Context);
+ if (RHS == 0) RHS = Context->getUndef(LHS->getType());
// We now have a shuffle of LHS, RHS, Mask.
- return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
+ return new ShuffleVectorInst(LHS, RHS,
+ Context->getConstantVector(Mask));
}
}
}
+ unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
+ APInt UndefElts(VWidth, 0);
+ APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
+ if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts))
+ return &IE;
+
return 0;
}
// Undefined shuffle mask -> undefined value.
if (isa<UndefValue>(SVI.getOperand(2)))
- return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
+ return ReplaceInstUsesWith(SVI, Context->getUndef(SVI.getType()));
- uint64_t UndefElts;
unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
return 0;
- uint64_t AllOnesEltMask = ~0ULL >> (64-VWidth);
- if (VWidth <= 64 &&
- SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
+ APInt UndefElts(VWidth, 0);
+ APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
+ if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
LHS = SVI.getOperand(0);
RHS = SVI.getOperand(1);
MadeChange = true;
std::vector<Constant*> Elts;
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
if (Mask[i] >= 2*e)
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(Context->getUndef(Type::Int32Ty));
else {
if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
(Mask[i] < e && isa<UndefValue>(LHS))) {
Mask[i] = 2*e; // Turn into undef.
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(Context->getUndef(Type::Int32Ty));
} else {
Mask[i] = Mask[i] % e; // Force to LHS.
- Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
+ Elts.push_back(Context->getConstantInt(Type::Int32Ty, Mask[i]));
}
}
}
SVI.setOperand(0, SVI.getOperand(1));
- SVI.setOperand(1, UndefValue::get(RHS->getType()));
- SVI.setOperand(2, ConstantVector::get(Elts));
+ SVI.setOperand(1, Context->getUndef(RHS->getType()));
+ SVI.setOperand(2, Context->getConstantVector(Elts));
LHS = SVI.getOperand(0);
RHS = SVI.getOperand(1);
MadeChange = true;
// If the result mask is equal to the src shuffle or this shuffle mask, do
// the replacement.
if (NewMask == LHSMask || NewMask == Mask) {
+ unsigned LHSInNElts =
+ cast<VectorType>(LHSSVI->getOperand(0)->getType())->getNumElements();
std::vector<Constant*> Elts;
for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
- if (NewMask[i] >= e*2) {
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ if (NewMask[i] >= LHSInNElts*2) {
+ Elts.push_back(Context->getUndef(Type::Int32Ty));
} else {
- Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
+ Elts.push_back(Context->getConstantInt(Type::Int32Ty, NewMask[i]));
}
}
return new ShuffleVectorInst(LHSSVI->getOperand(0),
LHSSVI->getOperand(1),
- ConstantVector::get(Elts));
+ Context->getConstantVector(Elts));
}
}
}
assert(I->hasOneUse() && "Invariants didn't hold!");
// Cannot move control-flow-involving, volatile loads, vaarg, etc.
- if (isa<PHINode>(I) || I->mayWriteToMemory() || isa<TerminatorInst>(I))
+ if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I))
return false;
// Do not sink alloca instructions out of the entry block.
BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
+ CopyPrecedingStopPoint(I, InsertPos);
I->moveBefore(InsertPos);
++NumSunkInst;
return true;
}
// ConstantProp instruction if trivially constant.
- if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
+ if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) {
DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst;
Inst->replaceAllUsesWith(C);
++NumConstProp;
DBI_Prev->eraseFromParent();
}
DBI_Prev = DBI_Next;
+ } else {
+ DBI_Prev = 0;
}
IC.AddToWorkList(Inst);
BasicBlock::iterator I = Term; --I;
DOUT << "IC: DCE: " << *I;
- ++NumDeadInst;
-
+ // A debug intrinsic shouldn't force another iteration if we weren't
+ // going to do one without it.
+ if (!isa<DbgInfoIntrinsic>(I)) {
+ ++NumDeadInst;
+ Changed = true;
+ }
if (!I->use_empty())
- I->replaceAllUsesWith(UndefValue::get(I->getType()));
+ I->replaceAllUsesWith(Context->getUndef(I->getType()));
I->eraseFromParent();
}
}
I->eraseFromParent();
RemoveFromWorkList(I);
+ Changed = true;
continue;
}
// Instruction isn't dead, see if we can constant propagate it.
- if (Constant *C = ConstantFoldInstruction(I, TD)) {
+ if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) {
DOUT << "IC: ConstFold to: " << *C << " from: " << *I;
// Add operands to the worklist.
++NumConstProp;
I->eraseFromParent();
RemoveFromWorkList(I);
+ Changed = true;
continue;
}
- if (TD && I->getType()->getTypeID() == Type::VoidTyID) {
+ if (TD &&
+ (I->getType()->getTypeID() == Type::VoidTyID ||
+ I->isTrapping())) {
// See if we can constant fold its operands.
- for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i)) {
- if (Constant *NewC = ConstantFoldConstantExpression(CE, TD))
- i->set(NewC);
- }
- }
+ for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i))
+ if (Constant *NewC = ConstantFoldConstantExpression(CE,
+ F.getContext(), TD))
+ if (NewC != CE) {
+ i->set(NewC);
+ Changed = true;
+ }
}
// See if we can trivially sink this instruction to a successor basic block.
FunctionPass *llvm::createInstructionCombiningPass() {
return new InstCombiner();
}
-
-