Simplify code a bit by changing instances of:

InsertNewInstBefore(new CastInst(Val, ValTy, Val->GetName()), I)
into:
   InsertCastBefore(Val, ValTy, I)


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31204 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2006-10-26 19:19:06 +00:00
parent 8910c1cacb
commit 811b0cbd6e

View File

@ -461,9 +461,7 @@ Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy,
if (Constant *C = dyn_cast<Constant>(V)) if (Constant *C = dyn_cast<Constant>(V))
return ConstantExpr::getCast(C, DestTy); return ConstantExpr::getCast(C, DestTy);
CastInst *CI = new CastInst(V, DestTy, V->getName()); return InsertCastBefore(V, DestTy, *InsertBefore);
InsertNewInstBefore(CI, *InsertBefore);
return CI;
} }
// SimplifyCommutative - This performs a few simplifications for commutative // SimplifyCommutative - This performs a few simplifications for commutative
@ -1087,13 +1085,11 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
// convert this into a zero extension. // convert this into a zero extension.
if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) { if ((KnownZero & InSignBit) || (NewBits & ~DemandedMask) == NewBits) {
// Convert to unsigned first. // Convert to unsigned first.
Instruction *NewVal; Value *NewVal =
NewVal = new CastInst(I->getOperand(0), SrcTy->getUnsignedVersion(), InsertCastBefore(I->getOperand(0), SrcTy->getUnsignedVersion(), *I);
I->getOperand(0)->getName());
InsertNewInstBefore(NewVal, *I);
// Then cast that to the destination type. // Then cast that to the destination type.
NewVal = new CastInst(NewVal, I->getType(), I->getName()); NewVal = new CastInst(NewVal, I->getType(), I->getName());
InsertNewInstBefore(NewVal, *I); InsertNewInstBefore(cast<Instruction>(NewVal), *I);
return UpdateValueUsesWith(I, NewVal); return UpdateValueUsesWith(I, NewVal);
} else if (KnownOne & InSignBit) { // Input sign bit known set } else if (KnownOne & InSignBit) { // Input sign bit known set
KnownOne |= NewBits; KnownOne |= NewBits;
@ -1124,17 +1120,15 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
// the shift amount is >= the size of the datatype, which is undefined. // the shift amount is >= the size of the datatype, which is undefined.
if (DemandedMask == 1 && I->getType()->isSigned()) { if (DemandedMask == 1 && I->getType()->isSigned()) {
// Convert the input to unsigned. // Convert the input to unsigned.
Instruction *NewVal = new CastInst(I->getOperand(0), Value *NewVal = InsertCastBefore(I->getOperand(0),
I->getType()->getUnsignedVersion(), I->getType()->getUnsignedVersion(), *I);
I->getOperand(0)->getName());
InsertNewInstBefore(NewVal, *I);
// Perform the unsigned shift right. // Perform the unsigned shift right.
NewVal = new ShiftInst(Instruction::Shr, NewVal, I->getOperand(1), NewVal = new ShiftInst(Instruction::Shr, NewVal, I->getOperand(1),
I->getName()); I->getName());
InsertNewInstBefore(NewVal, *I); InsertNewInstBefore(cast<Instruction>(NewVal), *I);
// Then cast that to the destination type. // Then cast that to the destination type.
NewVal = new CastInst(NewVal, I->getType(), I->getName()); NewVal = new CastInst(NewVal, I->getType(), I->getName());
InsertNewInstBefore(NewVal, *I); InsertNewInstBefore(cast<Instruction>(NewVal), *I);
return UpdateValueUsesWith(I, NewVal); return UpdateValueUsesWith(I, NewVal);
} }
@ -1175,17 +1169,14 @@ bool InstCombiner::SimplifyDemandedBits(Value *V, uint64_t DemandedMask,
// are demanded, turn this into an unsigned shift right. // are demanded, turn this into an unsigned shift right.
if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) { if ((KnownZero & SignBit) || (HighBits & ~DemandedMask) == HighBits) {
// Convert the input to unsigned. // Convert the input to unsigned.
Instruction *NewVal; Value *NewVal = InsertCastBefore(I->getOperand(0),
NewVal = new CastInst(I->getOperand(0), I->getType()->getUnsignedVersion(), *I);
I->getType()->getUnsignedVersion(),
I->getOperand(0)->getName());
InsertNewInstBefore(NewVal, *I);
// Perform the unsigned shift right. // Perform the unsigned shift right.
NewVal = new ShiftInst(Instruction::Shr, NewVal, SA, I->getName()); NewVal = new ShiftInst(Instruction::Shr, NewVal, SA, I->getName());
InsertNewInstBefore(NewVal, *I); InsertNewInstBefore(cast<Instruction>(NewVal), *I);
// Then cast that to the destination type. // Then cast that to the destination type.
NewVal = new CastInst(NewVal, I->getType(), I->getName()); NewVal = new CastInst(NewVal, I->getType(), I->getName());
InsertNewInstBefore(NewVal, *I); InsertNewInstBefore(cast<Instruction>(NewVal), *I);
return UpdateValueUsesWith(I, NewVal); return UpdateValueUsesWith(I, NewVal);
} else if (KnownOne & SignBit) { // New bits are known one. } else if (KnownOne & SignBit) { // New bits are known one.
KnownOne |= HighBits; KnownOne |= HighBits;
@ -1914,15 +1905,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
SI->getType()->getPrimitiveSizeInBits()-1) { SI->getType()->getPrimitiveSizeInBits()-1) {
// Ok, the transformation is safe. Insert a cast of the incoming // Ok, the transformation is safe. Insert a cast of the incoming
// value, then the new shift, then the new cast. // value, then the new shift, then the new cast.
Instruction *FirstCast = new CastInst(SI->getOperand(0), NewTy, Value *InV = InsertCastBefore(SI->getOperand(0), NewTy, I);
SI->getOperand(0)->getName()); Instruction *NewShift = new ShiftInst(Instruction::Shr, InV,
Value *InV = InsertNewInstBefore(FirstCast, I);
Instruction *NewShift = new ShiftInst(Instruction::Shr, FirstCast,
CU, SI->getName()); CU, SI->getName());
if (NewShift->getType() == I.getType()) if (NewShift->getType() == I.getType())
return NewShift; return NewShift;
else { else {
InV = InsertNewInstBefore(NewShift, I); InsertNewInstBefore(NewShift, I);
return new CastInst(NewShift, I.getType()); return new CastInst(NewShift, I.getType());
} }
} }
@ -2139,8 +2128,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
SCOpTy->getPrimitiveSizeInBits()-1); SCOpTy->getPrimitiveSizeInBits()-1);
if (SCIOp0->getType()->isUnsigned()) { if (SCIOp0->getType()->isUnsigned()) {
const Type *NewTy = SCIOp0->getType()->getSignedVersion(); const Type *NewTy = SCIOp0->getType()->getSignedVersion();
SCIOp0 = InsertNewInstBefore(new CastInst(SCIOp0, NewTy, SCIOp0 = InsertCastBefore(SCIOp0, NewTy, I);
SCIOp0->getName()), I);
} }
Value *V = Value *V =
@ -2151,7 +2139,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
// If the multiply type is not the same as the source type, sign extend // If the multiply type is not the same as the source type, sign extend
// or truncate to the multiply type. // or truncate to the multiply type.
if (I.getType() != V->getType()) if (I.getType() != V->getType())
V = InsertNewInstBefore(new CastInst(V, I.getType(), V->getName()),I); V = InsertCastBefore(V, I.getType(), I);
Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
return BinaryOperator::createAnd(V, OtherOp); return BinaryOperator::createAnd(V, OtherOp);
@ -4000,11 +3988,9 @@ Instruction *InstCombiner::FoldGEPSetCC(User *GEPLHS, Value *RHS,
// signed comparison. // signed comparison.
const Type *NewTy = LHSV->getType()->getSignedVersion(); const Type *NewTy = LHSV->getType()->getSignedVersion();
if (LHSV->getType() != NewTy) if (LHSV->getType() != NewTy)
LHSV = InsertNewInstBefore(new CastInst(LHSV, NewTy, LHSV = InsertCastBefore(LHSV, NewTy, I);
LHSV->getName()), I);
if (RHSV->getType() != NewTy) if (RHSV->getType() != NewTy)
RHSV = InsertNewInstBefore(new CastInst(RHSV, NewTy, RHSV = InsertCastBefore(RHSV, NewTy, I);
RHSV->getName()), I);
return new SetCondInst(Cond, LHSV, RHSV); return new SetCondInst(Cond, LHSV, RHSV);
} }
} }
@ -4838,8 +4824,7 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
Op1 = ConstantExpr::getCast(Op1C, Op0->getType()); Op1 = ConstantExpr::getCast(Op1C, Op0->getType());
} else { } else {
// Otherwise, cast the RHS right before the setcc // Otherwise, cast the RHS right before the setcc
Op1 = new CastInst(Op1, Op0->getType(), Op1->getName()); Op1 = InsertCastBefore(Op1, Op0->getType(), I);
InsertNewInstBefore(cast<Instruction>(Op1), I);
} }
return BinaryOperator::create(I.getOpcode(), Op0, Op1); return BinaryOperator::create(I.getOpcode(), Op0, Op1);
} }
@ -5276,7 +5261,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
Value *Op = ShiftOp->getOperand(0); Value *Op = ShiftOp->getOperand(0);
if (isShiftOfSignedShift != isSignedShift) if (isShiftOfSignedShift != isSignedShift)
Op = InsertNewInstBefore(new CastInst(Op, I.getType(),Op->getName()),I); Op = InsertCastBefore(Op, I.getType(), I);
Instruction *Mask = Instruction *Mask =
BinaryOperator::createAnd(Op, C, Op->getName()+".mask"); BinaryOperator::createAnd(Op, C, Op->getName()+".mask");
@ -5304,9 +5289,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
} }
} else { } else {
// (X >>s C1) << C2 where C1 > C2 === (X >>s (C1-C2)) & mask // (X >>s C1) << C2 where C1 > C2 === (X >>s (C1-C2)) & mask
Op = InsertNewInstBefore(new CastInst(Mask, Op = InsertCastBefore(Mask, I.getType()->getSignedVersion(), I);
I.getType()->getSignedVersion(),
Mask->getName()), I);
Instruction *Shift = Instruction *Shift =
new ShiftInst(ShiftOp->getOpcode(), Op, new ShiftInst(ShiftOp->getOpcode(), Op,
ConstantInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2)); ConstantInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
@ -5863,8 +5846,8 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) {
// Perform an unsigned shr by shiftamt. Convert input to // Perform an unsigned shr by shiftamt. Convert input to
// unsigned if it is signed. // unsigned if it is signed.
if (In->getType()->isSigned()) if (In->getType()->isSigned())
In = InsertNewInstBefore(new CastInst(In, In = InsertCastBefore(
In->getType()->getUnsignedVersion(), In->getName()),CI); In, In->getType()->getUnsignedVersion(), CI);
// Insert the shift to put the result in the low bit. // Insert the shift to put the result in the low bit.
In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In, In = InsertNewInstBefore(new ShiftInst(Instruction::Shr, In,
ConstantInt::get(Type::UByteTy, ShiftAmt), ConstantInt::get(Type::UByteTy, ShiftAmt),
@ -6952,10 +6935,8 @@ static Value *InsertSignExtendToPtrTy(Value *V, const Type *DTy,
const Type *VTy = V->getType(); const Type *VTy = V->getType();
if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS) if (!VTy->isSigned() && VTy->getPrimitiveSize() < PS)
// We must insert a cast to ensure we sign-extend. // We must insert a cast to ensure we sign-extend.
V = IC->InsertNewInstBefore(new CastInst(V, VTy->getSignedVersion(), V = IC->InsertCastBefore(V, VTy->getSignedVersion(), *InsertPoint);
V->getName()), *InsertPoint); return IC->InsertCastBefore(V, DTy, *InsertPoint);
return IC->InsertNewInstBefore(new CastInst(V, DTy, V->getName()),
*InsertPoint);
} }
@ -7019,8 +7000,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
TD->getIntPtrType()->getSignedVersion())); TD->getIntPtrType()->getSignedVersion()));
MadeChange = true; MadeChange = true;
} else { } else {
Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(), Op = InsertCastBefore(Op, TD->getIntPtrType(), GEP);
Op->getName()), GEP);
GEP.setOperand(i, Op); GEP.setOperand(i, Op);
MadeChange = true; MadeChange = true;
} }