aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/InstructionCombining.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Transforms/Scalar/InstructionCombining.cpp')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp1300
1 files changed, 705 insertions, 595 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 4e5d6639d4..a13b7e76ac 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -151,7 +151,20 @@ namespace {
Instruction *visitShiftInst(ShiftInst &I);
Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
ShiftInst &I);
- Instruction *visitCastInst(CastInst &CI);
+ Instruction *commonCastTransforms(CastInst &CI);
+ Instruction *commonIntCastTransforms(CastInst &CI);
+ Instruction *visitTrunc(CastInst &CI);
+ Instruction *visitZExt(CastInst &CI);
+ Instruction *visitSExt(CastInst &CI);
+ Instruction *visitFPTrunc(CastInst &CI);
+ Instruction *visitFPExt(CastInst &CI);
+ Instruction *visitFPToUI(CastInst &CI);
+ Instruction *visitFPToSI(CastInst &CI);
+ Instruction *visitUIToFP(CastInst &CI);
+ Instruction *visitSIToFP(CastInst &CI);
+ Instruction *visitPtrToInt(CastInst &CI);
+ Instruction *visitIntToPtr(CastInst &CI);
+ Instruction *visitBitCast(CastInst &CI);
Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI,
Instruction *FI);
Instruction *visitSelectInst(SelectInst &CI);
@@ -198,7 +211,7 @@ namespace {
if (Constant *CV = dyn_cast<Constant>(V))
return ConstantExpr::getCast(CV, Ty);
- Instruction *C = new CastInst(V, Ty, V->getName(), &Pos);
+ Instruction *C = CastInst::createInferredCast(V, Ty, V->getName(), &Pos);
WorkList.push_back(C);
return C;
}
@@ -329,113 +342,38 @@ static const Type *getPromotedType(const Type *Ty) {
}
}
-/// isCast - If the specified operand is a CastInst or a constant expr cast,
-/// return the operand value, otherwise return null.
-static Value *isCast(Value *V) {
- if (CastInst *I = dyn_cast<CastInst>(V))
+/// getBitCastOperand - If the specified operand is a CastInst or a constant
+/// expression bitcast, return the operand value, otherwise return null.
+static Value *getBitCastOperand(Value *V) {
+ if (BitCastInst *I = dyn_cast<BitCastInst>(V))
return I->getOperand(0);
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
- if (CE->getOpcode() == Instruction::Cast)
+ if (CE->getOpcode() == Instruction::BitCast)
return CE->getOperand(0);
return 0;
}
-enum CastType {
- Noop = 0,
- Truncate = 1,
- Signext = 2,
- Zeroext = 3
-};
-
-/// getCastType - In the future, we will split the cast instruction into these
-/// various types. Until then, we have to do the analysis here.
-static CastType getCastType(const Type *Src, const Type *Dest) {
- assert(Src->isIntegral() && Dest->isIntegral() &&
- "Only works on integral types!");
- unsigned SrcSize = Src->getPrimitiveSizeInBits();
- unsigned DestSize = Dest->getPrimitiveSizeInBits();
+/// This function is a wrapper around CastInst::isEliminableCastPair. It
+/// simply extracts arguments and returns what that function returns.
+/// @Determine if it is valid to eliminate a Convert pair
+static Instruction::CastOps
+isEliminableCastPair(
+ const CastInst *CI, ///< The first cast instruction
+ unsigned opcode, ///< The opcode of the second cast instruction
+ const Type *DstTy, ///< The target type for the second cast instruction
+ TargetData *TD ///< The target data for pointer size
+) {
- if (SrcSize == DestSize) return Noop;
- if (SrcSize > DestSize) return Truncate;
- if (Src->isSigned()) return Signext;
- return Zeroext;
-}
+ const Type *SrcTy = CI->getOperand(0)->getType(); // A from above
+ const Type *MidTy = CI->getType(); // B from above
+ // Get the opcodes of the two Cast instructions
+ Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode());
+ Instruction::CastOps secondOp = Instruction::CastOps(opcode);
-// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
-// instruction.
-//
-static bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy,
- const Type *DstTy, TargetData *TD) {
-
- // It is legal to eliminate the instruction if casting A->B->A if the sizes
- // are identical and the bits don't get reinterpreted (for example
- // int->float->int would not be allowed).
- if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
- return true;
-
- // If we are casting between pointer and integer types, treat pointers as
- // integers of the appropriate size for the code below.
- if (isa<PointerType>(SrcTy)) SrcTy = TD->getIntPtrType();
- if (isa<PointerType>(MidTy)) MidTy = TD->getIntPtrType();
- if (isa<PointerType>(DstTy)) DstTy = TD->getIntPtrType();
-
- // Allow free casting and conversion of sizes as long as the sign doesn't
- // change...
- if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
- CastType FirstCast = getCastType(SrcTy, MidTy);
- CastType SecondCast = getCastType(MidTy, DstTy);
-
- // Capture the effect of these two casts. If the result is a legal cast,
- // the CastType is stored here, otherwise a special code is used.
- static const unsigned CastResult[] = {
- // First cast is noop
- 0, 1, 2, 3,
- // First cast is a truncate
- 1, 1, 4, 4, // trunc->extend is not safe to eliminate
- // First cast is a sign ext
- 2, 5, 2, 4, // signext->zeroext never ok
- // First cast is a zero ext
- 3, 5, 3, 3,
- };
-
- unsigned Result = CastResult[FirstCast*4+SecondCast];
- switch (Result) {
- default: assert(0 && "Illegal table value!");
- case 0:
- case 1:
- case 2:
- case 3:
- // FIXME: in the future, when LLVM has explicit sign/zeroextends and
- // truncates, we could eliminate more casts.
- return (unsigned)getCastType(SrcTy, DstTy) == Result;
- case 4:
- return false; // Not possible to eliminate this here.
- case 5:
- // Sign or zero extend followed by truncate is always ok if the result
- // is a truncate or noop.
- CastType ResultCast = getCastType(SrcTy, DstTy);
- if (ResultCast == Noop || ResultCast == Truncate)
- return true;
- // Otherwise we are still growing the value, we are only safe if the
- // result will match the sign/zeroextendness of the result.
- return ResultCast == FirstCast;
- }
- }
-
- // If this is a cast from 'float -> double -> integer', cast from
- // 'float -> integer' directly, as the value isn't changed by the
- // float->double conversion.
- if (SrcTy->isFloatingPoint() && MidTy->isFloatingPoint() &&
- DstTy->isIntegral() &&
- SrcTy->getPrimitiveSize() < MidTy->getPrimitiveSize())
- return true;
-
- // Packed type conversions don't modify bits.
- if (isa<PackedType>(SrcTy) && isa<PackedType>(MidTy) &&isa<PackedType>(DstTy))
- return true;
-
- return false;
+ return Instruction::CastOps(
+ CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
+ DstTy, TD->getIntPtrType()));
}
/// ValueRequiresCast - Return true if the cast from "V to Ty" actually results
@@ -445,13 +383,12 @@ static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) {
if (V->getType() == Ty || isa<Constant>(V)) return false;
// If this is a noop cast, it isn't real codegen.
- if (V->getType()->isLosslesslyConvertibleTo(Ty))
+ if (V->getType()->canLosslesslyBitCastTo(Ty))
return false;
// If this is another cast that can be eliminated, it isn't codegen either.
if (const CastInst *CI = dyn_cast<CastInst>(V))
- if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty,
- TD))
+ if (isEliminableCastPair(CI, CastInst::getCastOpcode(V, Ty), Ty, TD))
return false;
return true;
}
@@ -672,48 +609,62 @@ static void ComputeMaskedBits(Value *V, uint64_t Mask, uint64_t &KnownZero,
KnownOne &= KnownOne2;
KnownZero &= KnownZero2;
return;
- case Instruction::Cast: {
+ case Instruction::FPTrunc:
+ case Instruction::FPExt:
+ case Instruction::FPToUI:
+ case Instruction::FPToSI:
+ case Instruction::SIToFP:
+ case Instruction::PtrToInt:
+ case Instruction::UIToFP:
+ case Instruction::IntToPtr:
+ return; // Can't work with floating point or pointers
+ case Instruction::Trunc:
+ // All these have integer operands
+ ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
+ return;
+ case Instruction::BitCast: {
const Type *SrcTy = I->getOperand(0)->getType();
- if (!SrcTy->isIntegral()) return;
-
- // If this is an integer truncate or noop, just look in the input.
- if (SrcTy->getPrimitiveSizeInBits() >=
- I->getType()->getPrimitiveSizeInBits()) {
+ if (SrcTy->isIntegral()) {
ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
return;
}
-
- // Sign or Zero extension. Compute the bits in the result that are not
- // present in the input.
+ break;
+ }
+ case Instruction::ZExt: {
+ // Compute the bits in the result that are not present in the input.
+ const Type *SrcTy = I->getOperand(0)->getType();
uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
- // Handle zero extension.
- if (!SrcTy->isSigned()) {
- Mask &= SrcTy->getIntegralTypeMask();
- ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
- assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
- // The top bits are known to be zero.
- KnownZero |= NewBits;
- } else {
- // Sign extension.
- Mask &= SrcTy->getIntegralTypeMask();
- ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
- assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
+ Mask &= SrcTy->getIntegralTypeMask();
+ ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
+ assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
+ // The top bits are known to be zero.
+ KnownZero |= NewBits;
+ return;
+ }
+ case Instruction::SExt: {
+ // Compute the bits in the result that are not present in the input.
+ const Type *SrcTy = I->getOperand(0)->getType();
+ uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
+ uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
+
+ Mask &= SrcTy->getIntegralTypeMask();
+ ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
+ assert((KnownZero & KnownOne) == 0 && "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.
- uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
- if (KnownZero & InSignBit) { // Input sign bit known zero
- KnownZero |= NewBits;
- KnownOne &= ~NewBits;
- } else if (KnownOne & InSignBit) { // Input sign bit known set
- KnownOne |= NewBits;
- KnownZero &= ~NewBits;
- } else { // Input sign bit unknown
- KnownZero &= ~NewBits;
- KnownOne &= ~NewBits;
- }
+ // If the sign bit of the input is known set or clear, then we know the
+ // top bits of the result.
+ uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
+ if (KnownZero & InSignBit) { // Input sign bit known zero
+ KnownZero |= NewBits;
+ KnownOne &= ~NewBits;
+ } else if (KnownOne & InSignBit) { // Input sign bit known set
+ KnownOne |= NewBits;
+ KnownZero &= ~NewBits;
+ } else { // Input sign bit unknown
+ KnownZero &= ~NewBits;
+ KnownOne &= ~NewBits;
}
return;
}
@@ -894,7 +845,7 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
DemandedMask &= V->getType()->getIntegralTypeMask();
- uint64_t KnownZero2, KnownOne2;
+ uint64_t KnownZero2 = 0, KnownOne2 = 0;
switch (I->getOpcode()) {
default: break;
case Instruction::And:
@@ -911,7 +862,7 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
return true;
assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
- // If all of the demanded bits are known one on one side, return the other.
+ // 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 & ~KnownZero2 & KnownOne) == (DemandedMask & ~KnownZero2))
return UpdateValueUsesWith(I, I->getOperand(0));
@@ -1045,74 +996,72 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
KnownOne &= KnownOne2;
KnownZero &= KnownZero2;
break;
- case Instruction::Cast: {
- const Type *SrcTy = I->getOperand(0)->getType();
- if (!SrcTy->isIntegral()) return false;
-
- // If this is an integer truncate or noop, just look in the input.
- if (SrcTy->getPrimitiveSizeInBits() >=
- I->getType()->getPrimitiveSizeInBits()) {
- // Cast to bool is a comparison against 0, which demands all bits. We
- // can't propagate anything useful up.
- if (I->getType() == Type::BoolTy)
- break;
+ case Instruction::Trunc:
+ if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ KnownZero, KnownOne, Depth+1))
+ return true;
+ assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
+ break;
+ case Instruction::BitCast:
+ if (!I->getOperand(0)->getType()->isIntegral())
+ return false;
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
- KnownZero, KnownOne, Depth+1))
- return true;
- assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
- break;
- }
+ if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ KnownZero, KnownOne, Depth+1))
+ return true;
+ assert((KnownZero & KnownOne) == 0 && "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 Type *SrcTy = I->getOperand(0)->getType();
+ uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
+ uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
- // Sign or Zero extension. Compute the bits in the result that are not
- // present in the input.
+ DemandedMask &= SrcTy->getIntegralTypeMask();
+ if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
+ KnownZero, KnownOne, Depth+1))
+ return true;
+ assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
+ // The top bits are known to be zero.
+ KnownZero |= NewBits;
+ break;
+ }
+ case Instruction::SExt: {
+ // Compute the bits in the result that are not present in the input.
+ const Type *SrcTy = I->getOperand(0)->getType();
uint64_t NotIn = ~SrcTy->getIntegralTypeMask();
uint64_t NewBits = I->getType()->getIntegralTypeMask() & NotIn;
- // Handle zero extension.
- if (!SrcTy->isSigned()) {
- DemandedMask &= SrcTy->getIntegralTypeMask();
- if (SimplifyDemandedBits(I->getOperand(0), DemandedMask,
- KnownZero, KnownOne, Depth+1))
- return true;
- assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
- // The top bits are known to be zero.
- KnownZero |= NewBits;
- } else {
- // Sign extension.
- uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
- int64_t InputDemandedBits = DemandedMask & SrcTy->getIntegralTypeMask();
-
- // If any of the sign extended bits are demanded, we know that the sign
- // bit is demanded.
- if (NewBits & DemandedMask)
- InputDemandedBits |= InSignBit;
+ // Get the sign bit for the source type
+ uint64_t InSignBit = 1ULL << (SrcTy->getPrimitiveSizeInBits()-1);
+ int64_t InputDemandedBits = DemandedMask & SrcTy->getIntegralTypeMask();
+
+ // If any of the sign extended bits are demanded, we know that the sign
+ // bit is demanded.
+ if (NewBits & DemandedMask)
+ InputDemandedBits |= InSignBit;
- if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
- KnownZero, KnownOne, Depth+1))
- return true;
- assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
+ if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits,
+ KnownZero, KnownOne, Depth+1))
+ return true;
+ assert((KnownZero & KnownOne) == 0 && "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 ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) {
- // Convert to unsigned first.
- Value *NewVal =
- InsertCastBefore(I->getOperand(0), SrcTy->getUnsignedVersion(), *I);
- // Then cast that to the destination type.
- NewVal = new CastInst(NewVal, I->getType(), I->getName());
- InsertNewInstBefore(cast<Instruction>(NewVal), *I);
- return UpdateValueUsesWith(I, NewVal);
- } else if (KnownOne & InSignBit) { // Input sign bit known set
- KnownOne |= NewBits;
- KnownZero &= ~NewBits;
- } else { // Input sign bit unknown
- KnownZero &= ~NewBits;
- KnownOne &= ~NewBits;
- }
+ // 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 ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) {
+ // Convert to ZExt cast
+ CastInst *NewCast = CastInst::create(
+ Instruction::ZExt, I->getOperand(0), I->getType(), I->getName(), I);
+ return UpdateValueUsesWith(I, NewCast);
+ } else if (KnownOne & InSignBit) { // Input sign bit known set
+ KnownOne |= NewBits;
+ KnownZero &= ~NewBits;
+ } else { // Input sign bit unknown
+ KnownZero &= ~NewBits;
+ KnownOne &= ~NewBits;
}
break;
}
@@ -1618,12 +1567,12 @@ struct AddMaskingAnd {
static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
InstCombiner *IC) {
- if (isa<CastInst>(I)) {
+ if (CastInst *CI = dyn_cast<CastInst>(&I)) {
if (Constant *SOC = dyn_cast<Constant>(SO))
- return ConstantExpr::getCast(SOC, I.getType());
+ return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType());
- return IC->InsertNewInstBefore(new CastInst(SO, I.getType(),
- SO->getName() + ".cast"), I);
+ return IC->InsertNewInstBefore(CastInst::create(
+ CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I);
}
// Figure out if the constant is the left or the right argument.
@@ -1738,17 +1687,18 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) {
}
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
}
- } else {
- assert(isa<CastInst>(I) && "Unary op should be a cast!");
- const Type *RetTy = I.getType();
+ } else {
+ CastInst *CI = cast<CastInst>(&I);
+ const Type *RetTy = CI->getType();
for (unsigned i = 0; i != NumPHIValues; ++i) {
Value *InV;
if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
- InV = ConstantExpr::getCast(InC, RetTy);
+ InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
} else {
assert(PN->getIncomingBlock(i) == NonConstBB);
- InV = new CastInst(PN->getIncomingValue(i), I.getType(), "phitmp",
- NonConstBB->getTerminator());
+ InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i),
+ I.getType(), "phitmp",
+ NonConstBB->getTerminator());
WorkList.push_back(cast<Instruction>(InV));
}
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
@@ -1840,9 +1790,10 @@ FoundSExt:
case 8: MiddleType = Type::SByteTy; break;
}
if (MiddleType) {
- Instruction *NewTrunc = new CastInst(XorLHS, MiddleType, "sext");
+ Instruction *NewTrunc =
+ CastInst::createInferredCast(XorLHS, MiddleType, "sext");
InsertNewInstBefore(NewTrunc, I);
- return new CastInst(NewTrunc, I.getType());
+ return new SExtInst(NewTrunc, I.getType());
}
}
}
@@ -1934,8 +1885,8 @@ FoundSExt:
// cast (GEP (cast *A to sbyte*) B) ->
// intptrtype
{
- CastInst* CI = dyn_cast<CastInst>(LHS);
- Value* Other = RHS;
+ CastInst *CI = dyn_cast<CastInst>(LHS);
+ Value *Other = RHS;
if (!CI) {
CI = dyn_cast<CastInst>(RHS);
Other = LHS;
@@ -1944,10 +1895,10 @@ FoundSExt:
(CI->getType()->getPrimitiveSize() ==
TD->getIntPtrType()->getPrimitiveSize())
&& isa<PointerType>(CI->getOperand(0)->getType())) {
- Value* I2 = InsertCastBefore(CI->getOperand(0),
+ Value *I2 = InsertCastBefore(CI->getOperand(0),
PointerType::get(Type::SByteTy), I);
I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I);
- return new CastInst(I2, CI->getType());
+ return new PtrToIntInst(I2, CI->getType());
}
}
@@ -2266,7 +2217,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
/// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is
/// used by the visitors to those instructions.
/// @brief Transforms common to all three div instructions
-Instruction* InstCombiner::commonDivTransforms(BinaryOperator &I) {
+Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
// undef / X -> 0
@@ -2317,7 +2268,7 @@ Instruction* InstCombiner::commonDivTransforms(BinaryOperator &I) {
/// instructions (udiv and sdiv). It is called by the visitors to those integer
/// division instructions.
/// @brief Common integer divide transforms
-Instruction* InstCombiner::commonIDivTransforms(BinaryOperator &I) {
+Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
if (Instruction *Common = commonDivTransforms(I))
@@ -2380,7 +2331,7 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
uint64_t C1 = cast<ConstantInt>(RHSI->getOperand(0))->getZExtValue();
if (isPowerOf2_64(C1)) {
Value *N = RHSI->getOperand(1);
- const Type* NTy = N->getType();
+ const Type *NTy = N->getType();
if (uint64_t C2 = Log2_64(C1)) {
Constant *C2V = ConstantInt::get(NTy, C2);
N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I);
@@ -2483,11 +2434,12 @@ static Constant *GetFactor(Value *V) {
return ConstantExpr::getShl(Result,
ConstantInt::get(Type::UByteTy, Zeros));
}
- } else if (I->getOpcode() == Instruction::Cast) {
- Value *Op = I->getOperand(0);
+ } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
// Only handle int->int casts.
- if (!Op->getType()->isInteger()) return Result;
- return ConstantExpr::getCast(GetFactor(Op), V->getType());
+ if (!CI->isIntegerCast())
+ return Result;
+ Value *Op = CI->getOperand(0);
+ return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType());
}
return Result;
}
@@ -3123,33 +3075,34 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
return Res;
} else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
- const Type *SrcTy = CI->getOperand(0)->getType();
-
// If this is an integer truncation or change from signed-to-unsigned, and
// if the source is an and/or with immediate, transform it. This
// frequently occurs for bitfield accesses.
if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
- if (SrcTy->getPrimitiveSizeInBits() >=
- I.getType()->getPrimitiveSizeInBits() &&
+ if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
CastOp->getNumOperands() == 2)
if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1)))
if (CastOp->getOpcode() == Instruction::And) {
// Change: and (cast (and X, C1) to T), C2
- // into : and (cast X to T), trunc(C1)&C2
- // This will folds the two ands together, which may allow other
- // simplifications.
+ // into : and (cast X to T), trunc_or_bitcast(C1)&C2
+ // This will fold the two constants together, which may allow
+ // other simplifications.
Instruction *NewCast =
- new CastInst(CastOp->getOperand(0), I.getType(),
+ CastInst::createInferredCast(CastOp->getOperand(0), I.getType(),
CastOp->getName()+".shrunk");
NewCast = InsertNewInstBefore(NewCast, I);
-
- Constant *C3=ConstantExpr::getCast(AndCI, I.getType());//trunc(C1)
- C3 = ConstantExpr::getAnd(C3, AndRHS); // trunc(C1)&C2
+ // trunc_or_bitcast(C1)&C2
+ Instruction::CastOps opc = (
+ AndCI->getType()->getPrimitiveSizeInBits() ==
+ I.getType()->getPrimitiveSizeInBits() ?
+ Instruction::BitCast : Instruction::Trunc);
+ Constant *C3 = ConstantExpr::getCast(opc, AndCI, I.getType());
+ C3 = ConstantExpr::getAnd(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::getCast(AndCI, I.getType());//trunc(C1)
+ Constant *C3 = ConstantExpr::getCast(AndCI, I.getType());
if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2
return ReplaceInstUsesWith(I, AndRHS);
}
@@ -3322,7 +3275,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Op1C->getOperand(0),
I.getName());
InsertNewInstBefore(NewOp, I);
- return new CastInst(NewOp, I.getType());
+ return CastInst::createInferredCast(NewOp, I.getType());
}
}
}
@@ -3725,7 +3678,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Op1C->getOperand(0),
I.getName());
InsertNewInstBefore(NewOp, I);
- return new CastInst(NewOp, I.getType());
+ return CastInst::createInferredCast(NewOp, I.getType());
}
}
@@ -3906,7 +3859,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Op1C->getOperand(0),
I.getName());
InsertNewInstBefore(NewOp, I);
- return new CastInst(NewOp, I.getType());
+ return CastInst::createInferredCast(NewOp, I.getType());
}
}
@@ -3982,7 +3935,7 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
}
} else {
// Convert to correct type.
- Op = IC.InsertNewInstBefore(new CastInst(Op, SIntPtrTy,
+ Op = IC.InsertNewInstBefore(CastInst::createInferredCast(Op, SIntPtrTy,
Op->getName()+".c"), I);
if (Size != 1)
// We'll let instcombine(mul) convert this to a shl if possible.
@@ -4344,7 +4297,7 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
// have its sign bit set or if it is an equality comparison.
// Extending a relational comparison when we're checking the sign
// bit would not work.
- if (Cast->hasOneUse() && Cast->isTruncIntCast() &&
+ if (Cast->hasOneUse() && isa<TruncInst>(Cast) &&
(I.isEquality() ||
(AndCST->getZExtValue() == (uint64_t)AndCST->getSExtValue()) &&
(CI->getZExtValue() == (uint64_t)CI->getSExtValue()))) {
@@ -4604,7 +4557,7 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
// (x /u C1) <u C2. Simply casting the operands and result won't
// work. :( The if statement below tests that condition and bails
// if it finds it.
- const Type* DivRHSTy = DivRHS->getType();
+ const Type *DivRHSTy = DivRHS->getType();
unsigned DivOpCode = LHSI->getOpcode();
if (I.isEquality() &&
((DivOpCode == Instruction::SDiv && DivRHSTy->isUnsigned()) ||
@@ -4936,18 +4889,19 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
// values. If the cast can be stripped off both arguments, we do so now.
if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
Value *CastOp0 = CI->getOperand(0);
- if (CastOp0->getType()->isLosslesslyConvertibleTo(CI->getType()) &&
- (isa<Constant>(Op1) || isa<CastInst>(Op1)) && I.isEquality()) {
+ if (CI->isLosslessCast() && I.isEquality() &&
+ (isa<Constant>(Op1) || isa<CastInst>(Op1))) {
// We keep moving the cast from the left operand over to the right
// operand, where it can often be eliminated completely.
Op0 = CastOp0;
// If operand #1 is a cast instruction, see if we can eliminate it as
// well.
- if (CastInst *CI2 = dyn_cast<CastInst>(Op1))
- if (CI2->getOperand(0)->getType()->isLosslesslyConvertibleTo(
- Op0->getType()))
- Op1 = CI2->getOperand(0);
+ if (CastInst *CI2 = dyn_cast<CastInst>(Op1)) {
+ Value *CI2Op0 = CI2->getOperand(0);
+ if (CI2Op0->getType()->canLosslesslyBitCastTo(Op0->getType()))
+ Op1 = CI2Op0;
+ }
// If Op1 is a constant, we can fold the cast into the constant.
if (Op1->getType() != Op0->getType())
@@ -5028,9 +4982,10 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
// We only handle extending casts so far.
//
Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) {
- Value *LHSCIOp = cast<CastInst>(SCI.getOperand(0))->getOperand(0);
- const Type *SrcTy = LHSCIOp->getType();
- const Type *DestTy = SCI.getOperand(0)->getType();
+ const CastInst *LHSCI = cast<CastInst>(SCI.getOperand(0));
+ Value *LHSCIOp = LHSCI->getOperand(0);
+ const Type *SrcTy = LHSCIOp->getType();
+ const Type *DestTy = SCI.getOperand(0)->getType();
Value *RHSCIOp;
if (!DestTy->isIntegral() || !SrcTy->isIntegral())
@@ -5051,9 +5006,10 @@ Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) {
} else if (ConstantInt *CI = dyn_cast<ConstantInt>(SCI.getOperand(1))) {
// Compute the constant that would happen if we truncated to SrcTy then
// reextended to DestTy.
- Constant *Res = ConstantExpr::getCast(CI, SrcTy);
+ Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
+ Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
- if (ConstantExpr::getCast(Res, DestTy) == CI) {
+ if (Res2 == CI) {
// Make sure that src sign and dest sign match. For example,
//
// %A = cast short %X to uint
@@ -5067,7 +5023,7 @@ Instruction *InstCombiner::visitSetCondInstWithCastAndCast(SetCondInst &SCI) {
// However, it is OK if SrcTy is bool (See cast-set.ll testcase)
// OR operation is EQ/NE.
if (isSignSrc == isSignDest || SrcTy == Type::BoolTy || SCI.isEquality())
- RHSCIOp = Res;
+ RHSCIOp = Res1;
else
return 0;
} else {
@@ -5361,12 +5317,9 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
ShiftInst *ShiftOp = 0;
if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0))
ShiftOp = Op0SI;
- else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
- // If this is a noop-integer case of a shift instruction, use the shift.
- if (CI->getOperand(0)->getType()->isInteger() &&
- CI->getOperand(0)->getType()->getPrimitiveSizeInBits() ==
- CI->getType()->getPrimitiveSizeInBits() &&
- isa<ShiftInst>(CI->getOperand(0))) {
+ else if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
+ // If this is a noop-integer cast of a shift instruction, use the shift.
+ if (isa<ShiftInst>(CI->getOperand(0))) {
ShiftOp = cast<ShiftInst>(CI->getOperand(0));
}
}
@@ -5400,13 +5353,14 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
Value *Op = ShiftOp->getOperand(0);
if (isShiftOfSignedShift != isSignedShift)
- Op = InsertNewInstBefore(new CastInst(Op, I.getType(), "tmp"), I);
- ShiftInst* ShiftResult = new ShiftInst(I.getOpcode(), Op,
+ Op = InsertNewInstBefore(
+ CastInst::createInferredCast(Op, I.getType(), "tmp"), I);
+ ShiftInst *ShiftResult = new ShiftInst(I.getOpcode(), Op,
ConstantInt::get(Type::UByteTy, Amt));
if (I.getType() == ShiftResult->getType())
return ShiftResult;
InsertNewInstBefore(ShiftResult, I);
- return new CastInst(ShiftResult, I.getType());
+ return CastInst::create(Instruction::BitCast, ShiftResult, I.getType());
}
// Check for (A << c1) >> c2 or (A >> c1) << c2. If we are dealing with
@@ -5454,7 +5408,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
C = ConstantExpr::getShl(C, Op1);
Mask = BinaryOperator::createAnd(Shift, C, Op->getName()+".mask");
InsertNewInstBefore(Mask, I);
- return new CastInst(Mask, I.getType());
+ return CastInst::create(Instruction::BitCast, Mask, I.getType());
}
} else {
// We can handle signed (X << C1) >>s C2 if it's a sign extend. In
@@ -5468,10 +5422,10 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
}
if (SExtType) {
- Instruction *NewTrunc = new CastInst(ShiftOp->getOperand(0),
- SExtType, "sext");
+ Instruction *NewTrunc =
+ new TruncInst(ShiftOp->getOperand(0), SExtType, "sext");
InsertNewInstBefore(NewTrunc, I);
- return new CastInst(NewTrunc, I.getType());
+ return new SExtInst(NewTrunc, I.getType());
}
}
}
@@ -5622,7 +5576,9 @@ Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI,
// die soon.
if (!AI.hasOneUse()) {
AddUsesToWorkList(AI);
- CastInst *NewCast = new CastInst(New, AI.getType(), "tmpcast");
+ // New is the allocation instruction, pointer typed. AI is the original
+ // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
+ CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast");
InsertNewInstBefore(NewCast, AI);
AI.replaceAllUsesWith(NewCast);
}
@@ -5647,7 +5603,10 @@ static bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
// These operators can all arbitrarily be extended or truncated.
return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) &&
CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved);
- case Instruction::Cast:
+ case Instruction::Trunc:
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::BitCast:
// If this is a cast from the destination type, we can trivially eliminate
// it, and this will remove a cast overall.
if (I->getOperand(0)->getType() == Ty) {
@@ -5660,6 +5619,8 @@ static bool CanEvaluateInDifferentType(Value *V, const Type *Ty,
++NumCastsRemoved;
return true;
}
+ break;
+ default:
// TODO: Can handle more cases here.
break;
}
@@ -5687,11 +5648,18 @@ Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty) {
LHS, RHS, I->getName());
break;
}
- case Instruction::Cast:
- // If this is a cast from the destination type, return the input.
+ case Instruction::Trunc:
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::BitCast:
+ // If the source type of the cast is the type we're trying for then we can
+ // just return the source. There's no need to insert it because its not new.
if (I->getOperand(0)->getType() == Ty)
return I->getOperand(0);
+ // Some other kind of cast, which shouldn't happen, so just ..
+ // FALL THROUGH
+ default:
// TODO: Can handle more cases here.
assert(0 && "Unreachable!");
break;
@@ -5700,73 +5668,26 @@ Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty) {
return InsertNewInstBefore(Res, *I);
}