Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI);
Instruction *MatchBSwap(BinaryOperator &I);
- Value *EvaluateInDifferentType(Value *V, const Type *Ty);
+ Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned);
};
RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions");
// See if we can simplify things based on how the boolean was originally
// formed.
CastInst *BoolCast = 0;
- if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(0)))
- if (CI->getOperand(0)->getType() == Type::BoolTy &&
- CI->getOpcode() == Instruction::ZExt)
+ if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0)))
+ if (CI->getOperand(0)->getType() == Type::BoolTy)
BoolCast = CI;
if (!BoolCast)
- if (CastInst *CI = dyn_cast<CastInst>(I.getOperand(1)))
- if (CI->getOperand(0)->getType() == Type::BoolTy &&
- CI->getOpcode() == Instruction::ZExt)
+ if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
+ if (CI->getOperand(0)->getType() == Type::BoolTy)
BoolCast = CI;
if (BoolCast) {
if (SetCondInst *SCI = dyn_cast<SetCondInst>(BoolCast->getOperand(0))) {
// Check to see if there is a noop-cast between the shift and the and.
if (!Shift) {
if (CastInst *CI = dyn_cast<CastInst>(LHSI->getOperand(0)))
- if (CI->getOpcode() == Instruction::BitCast &&
- CI->getType()->isIntegral())
+ if (CI->getOpcode() == Instruction::BitCast)
Shift = dyn_cast<ShiftInst>(CI->getOperand(0));
}
Amt = Op0->getType()->getPrimitiveSizeInBits();
Value *Op = ShiftOp->getOperand(0);
- if (isShiftOfSignedShift != isSignedShift)
- Op = InsertNewInstBefore(new BitCastInst(Op, I.getType(), "tmp"), I);
ShiftInst *ShiftResult = new ShiftInst(I.getOpcode(), Op,
- ConstantInt::get(Type::UByteTy, Amt));
+ ConstantInt::get(Type::UByteTy, Amt));
if (I.getType() == ShiftResult->getType())
return ShiftResult;
InsertNewInstBefore(ShiftResult, I);
/// EvaluateInDifferentType - Given an expression that
/// CanEvaluateInDifferentType returns true for, actually insert the code to
/// evaluate the expression.
-Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty) {
+Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty,
+ bool isSigned ) {
if (Constant *C = dyn_cast<Constant>(V))
- return ConstantExpr::getIntegerCast(C, Ty, C->getType()->isSigned());
+ return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
// Otherwise, it must be an instruction.
Instruction *I = cast<Instruction>(V);
case Instruction::And:
case Instruction::Or:
case Instruction::Xor: {
- Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty);
- Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty);
+ 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, I->getName());
break;
case Instruction::AShr:
case Instruction::LShr:
case Instruction::Shl: {
- Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty);
+ Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
Res = new ShiftInst((Instruction::OtherOps)I->getOpcode(), LHS,
I->getOperand(1), I->getName());
break;
}
if (DoXForm) {
- Value *Res = EvaluateInDifferentType(SrcI, DestTy);
+ Value *Res = EvaluateInDifferentType(SrcI, DestTy,
+ CI.getOpcode() == Instruction::SExt);
assert(Res->getType() == DestTy);
switch (CI.getOpcode()) {
default: assert(0 && "Unknown cast type!");
Value *NewCast;
Instruction::CastOps opcode = Instruction::BitCast;
Value *SIOp0 = SI.getOperand(0);
- if (SrcPTy->getTypeID() == Type::PointerTyID) {
+ if (isa<PointerType>(SrcPTy)) {
if (SIOp0->getType()->isIntegral())
opcode = Instruction::IntToPtr;
} else if (SrcPTy->isIntegral()) {
- if (SIOp0->getType()->getTypeID() == Type::PointerTyID)
+ if (isa<PointerType>(SIOp0->getType()))
opcode = Instruction::PtrToInt;
}
if (Constant *C = dyn_cast<Constant>(SIOp0))