Refactor creation of overflow result tuples in InstCombineCalls.

Extract the creation of overflow result tuples in a separate function. NFC.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224006 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Erik Eckstein 2014-12-11 08:02:30 +00:00
parent 3490558860
commit 1bf1224a06
2 changed files with 30 additions and 57 deletions

View File

@ -331,6 +331,20 @@ public:
return &I; return &I;
} }
/// Creates a result tuple for an overflow intrinsic \p II with a given
/// \p Result and a constant \p Overflow value. If \p ReUseName is true the
/// \p Result's name is taken from \p II.
Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
bool Overflow, bool ReUseName = true) {
if (ReUseName)
Result->takeName(II);
Constant *V[] = { UndefValue::get(Result->getType()),
Overflow ? Builder->getTrue() : Builder->getFalse() };
StructType *ST = cast<StructType>(II->getType());
Constant *Struct = ConstantStruct::get(ST, V);
return InsertValueInst::Create(Struct, Result, 0);
}
// EraseInstFromFunction - When dealing with an instruction that has side // EraseInstFromFunction - When dealing with an instruction that has side
// effects or produces a void value, we can't rely on DCE to delete the // 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 // instruction. Instead, visit methods should return the value returned by

View File

@ -369,29 +369,14 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
if (LHSKnownNegative && RHSKnownNegative) { if (LHSKnownNegative && RHSKnownNegative) {
// The sign bit is set in both cases: this MUST overflow. // The sign bit is set in both cases: this MUST overflow.
// Create a simple add instruction, and insert it into the struct. // Create a simple add instruction, and insert it into the struct.
Value *Add = Builder->CreateAdd(LHS, RHS); return CreateOverflowTuple(II, Builder->CreateAdd(LHS, RHS), true,
Add->takeName(&CI); /*ReUseName*/true);
Constant *V[] = {
UndefValue::get(LHS->getType()),
ConstantInt::getTrue(II->getContext())
};
StructType *ST = cast<StructType>(II->getType());
Constant *Struct = ConstantStruct::get(ST, V);
return InsertValueInst::Create(Struct, Add, 0);
} }
if (LHSKnownPositive && RHSKnownPositive) { if (LHSKnownPositive && RHSKnownPositive) {
// The sign bit is clear in both cases: this CANNOT overflow. // The sign bit is clear in both cases: this CANNOT overflow.
// Create a simple add instruction, and insert it into the struct. // Create a simple add instruction, and insert it into the struct.
Value *Add = Builder->CreateNUWAdd(LHS, RHS); return CreateOverflowTuple(II, Builder->CreateNUWAdd(LHS, RHS), false);
Add->takeName(&CI);
Constant *V[] = {
UndefValue::get(LHS->getType()),
ConstantInt::getFalse(II->getContext())
};
StructType *ST = cast<StructType>(II->getType());
Constant *Struct = ConstantStruct::get(ST, V);
return InsertValueInst::Create(Struct, Add, 0);
} }
} }
} }
@ -413,13 +398,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
// X + 0 -> {X, false} // X + 0 -> {X, false}
if (RHS->isZero()) { if (RHS->isZero()) {
Constant *V[] = { return CreateOverflowTuple(II, II->getArgOperand(0), false,
UndefValue::get(II->getArgOperand(0)->getType()), /*ReUseName*/false);
ConstantInt::getFalse(II->getContext())
};
Constant *Struct =
ConstantStruct::get(cast<StructType>(II->getType()), V);
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
} }
} }
@ -428,37 +408,27 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow) { if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow) {
Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
if (WillNotOverflowSignedAdd(LHS, RHS, II)) { if (WillNotOverflowSignedAdd(LHS, RHS, II)) {
Value *Add = Builder->CreateNSWAdd(LHS, RHS); return CreateOverflowTuple(II, Builder->CreateNSWAdd(LHS, RHS), false);
Add->takeName(&CI);
Constant *V[] = {UndefValue::get(Add->getType()), Builder->getFalse()};
StructType *ST = cast<StructType>(II->getType());
Constant *Struct = ConstantStruct::get(ST, V);
return InsertValueInst::Create(Struct, Add, 0);
} }
} }
break; break;
case Intrinsic::usub_with_overflow: case Intrinsic::usub_with_overflow:
case Intrinsic::ssub_with_overflow: case Intrinsic::ssub_with_overflow: {
Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
// undef - X -> undef // undef - X -> undef
// X - undef -> undef // X - undef -> undef
if (isa<UndefValue>(II->getArgOperand(0)) || if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS))
isa<UndefValue>(II->getArgOperand(1)))
return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { if (ConstantInt *ConstRHS = dyn_cast<ConstantInt>(RHS)) {
// X - 0 -> {X, false} // X - 0 -> {X, false}
if (RHS->isZero()) { if (ConstRHS->isZero()) {
Constant *V[] = { return CreateOverflowTuple(II, LHS, false, /*ReUseName*/false);
UndefValue::get(II->getArgOperand(0)->getType()),
ConstantInt::getFalse(II->getContext())
};
Constant *Struct =
ConstantStruct::get(cast<StructType>(II->getType()), V);
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
} }
} }
break; break;
}
case Intrinsic::umul_with_overflow: { case Intrinsic::umul_with_overflow: {
Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth(); unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth();
@ -479,13 +449,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
bool Overflow; bool Overflow;
LHSMax.umul_ov(RHSMax, Overflow); LHSMax.umul_ov(RHSMax, Overflow);
if (!Overflow) { if (!Overflow) {
Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow"); return CreateOverflowTuple(II, Builder->CreateNUWMul(LHS, RHS), false);
Constant *V[] = {
UndefValue::get(LHS->getType()),
Builder->getFalse()
};
Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V);
return InsertValueInst::Create(Struct, Mul, 0);
} }
} // FALL THROUGH } // FALL THROUGH
case Intrinsic::smul_with_overflow: case Intrinsic::smul_with_overflow:
@ -509,13 +473,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
// X * 1 -> {X, false} // X * 1 -> {X, false}
if (RHSI->equalsInt(1)) { if (RHSI->equalsInt(1)) {
Constant *V[] = { return CreateOverflowTuple(II, II->getArgOperand(0), false,
UndefValue::get(II->getArgOperand(0)->getType()), /*ReUseName*/false);
ConstantInt::getFalse(II->getContext())
};
Constant *Struct =
ConstantStruct::get(cast<StructType>(II->getType()), V);
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
} }
} }
break; break;