1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-26 06:22:56 +02:00

Use IRBuilder instead of ConstantInt methods. It simplifies code a little bit.

llvm-svn: 183328
This commit is contained in:
Jakub Staszak 2013-06-05 18:27:02 +00:00
parent d464e3d65b
commit a8f4094724

View File

@ -402,7 +402,7 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
if (SecondTrueElement != Overdefined) { if (SecondTrueElement != Overdefined) {
// None true -> false. // None true -> false.
if (FirstTrueElement == Undefined) if (FirstTrueElement == Undefined)
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(GEP->getContext())); return ReplaceInstUsesWith(ICI, Builder->getFalse());
Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement); Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
@ -422,7 +422,7 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
if (SecondFalseElement != Overdefined) { if (SecondFalseElement != Overdefined) {
// None false -> true. // None false -> true.
if (FirstFalseElement == Undefined) if (FirstFalseElement == Undefined)
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(GEP->getContext())); return ReplaceInstUsesWith(ICI, Builder->getTrue());
Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement); Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
@ -710,10 +710,8 @@ Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
} }
} }
if (NumDifferences == 0) // SAME GEP? if (NumDifferences == 0) // SAME GEP? No comparison is needed here.
return ReplaceInstUsesWith(I, // No comparison is needed here. return ReplaceInstUsesWith(I, Builder->getInt1(Cond));
ConstantInt::get(Type::getInt1Ty(I.getContext()),
ICmpInst::isTrueWhenEqual(Cond)));
else if (NumDifferences == 1 && GEPsInBounds) { else if (NumDifferences == 1 && GEPsInBounds) {
Value *LHSV = GEPLHS->getOperand(DiffOperand); Value *LHSV = GEPLHS->getOperand(DiffOperand);
@ -752,11 +750,11 @@ Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI,
// (X+4) == X -> false. // (X+4) == X -> false.
if (Pred == ICmpInst::ICMP_EQ) if (Pred == ICmpInst::ICMP_EQ)
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext())); return ReplaceInstUsesWith(ICI, Builder->getFalse());
// (X+4) != X -> true. // (X+4) != X -> true.
if (Pred == ICmpInst::ICMP_NE) if (Pred == ICmpInst::ICMP_NE)
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext())); return ReplaceInstUsesWith(ICI, Builder->getTrue());
// From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0, // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
// so the values can never be equal. Similarly for all other "or equals" // so the values can never be equal. Similarly for all other "or equals"
@ -798,7 +796,7 @@ Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI,
// (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE); assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
Constant *C = ConstantInt::get(X->getContext(), CI->getValue()-1); Constant *C = Builder->getInt(CI->getValue()-1);
return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C)); return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
} }
@ -921,7 +919,7 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
default: llvm_unreachable("Unhandled icmp opcode!"); default: llvm_unreachable("Unhandled icmp opcode!");
case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_EQ:
if (LoOverflow && HiOverflow) if (LoOverflow && HiOverflow)
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getFalse());
if (HiOverflow) if (HiOverflow)
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, LoBound); ICmpInst::ICMP_UGE, X, LoBound);
@ -932,7 +930,7 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
DivIsSigned, true)); DivIsSigned, true));
case ICmpInst::ICMP_NE: case ICmpInst::ICMP_NE:
if (LoOverflow && HiOverflow) if (LoOverflow && HiOverflow)
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getTrue());
if (HiOverflow) if (HiOverflow)
return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, LoBound); ICmpInst::ICMP_ULT, X, LoBound);
@ -944,16 +942,16 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLT:
if (LoOverflow == +1) // Low bound is greater than input range. if (LoOverflow == +1) // Low bound is greater than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getTrue());
if (LoOverflow == -1) // Low bound is less than input range. if (LoOverflow == -1) // Low bound is less than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getFalse());
return new ICmpInst(Pred, X, LoBound); return new ICmpInst(Pred, X, LoBound);
case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGT:
if (HiOverflow == +1) // High bound greater than input range. if (HiOverflow == +1) // High bound greater than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getFalse());
if (HiOverflow == -1) // High bound less than input range. if (HiOverflow == -1) // High bound less than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); return ReplaceInstUsesWith(ICI, Builder->getTrue());
if (Pred == ICmpInst::ICMP_UGT) if (Pred == ICmpInst::ICMP_UGT)
return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
@ -1017,7 +1015,7 @@ Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
// If we are comparing against bits always shifted out, the // If we are comparing against bits always shifted out, the
// comparison cannot succeed. // comparison cannot succeed.
APInt Comp = CmpRHSV << ShAmtVal; APInt Comp = CmpRHSV << ShAmtVal;
ConstantInt *ShiftedCmpRHS = ConstantInt::get(ICI.getContext(), Comp); ConstantInt *ShiftedCmpRHS = Builder->getInt(Comp);
if (Shr->getOpcode() == Instruction::LShr) if (Shr->getOpcode() == Instruction::LShr)
Comp = Comp.lshr(ShAmtVal); Comp = Comp.lshr(ShAmtVal);
else else
@ -1025,8 +1023,7 @@ Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero. if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
Constant *Cst = ConstantInt::get(Type::getInt1Ty(ICI.getContext()), Constant *Cst = Builder->getInt1(IsICMP_NE);
IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst); return ReplaceInstUsesWith(ICI, Cst);
} }
@ -1039,7 +1036,7 @@ Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
if (Shr->hasOneUse()) { if (Shr->hasOneUse()) {
// Otherwise strength reduce the shift into an and. // Otherwise strength reduce the shift into an and.
APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
Constant *Mask = ConstantInt::get(ICI.getContext(), Val); Constant *Mask = Builder->getInt(Val);
Value *And = Builder->CreateAnd(Shr->getOperand(0), Value *And = Builder->CreateAnd(Shr->getOperand(0),
Mask, Shr->getName()+".mask"); Mask, Shr->getName()+".mask");
@ -1072,7 +1069,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
APInt NewRHS = RHS->getValue().zext(SrcBits); APInt NewRHS = RHS->getValue().zext(SrcBits);
NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits); NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits);
return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(), NewRHS)); Builder->getInt(NewRHS));
} }
} }
break; break;
@ -1115,8 +1112,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
? ICI.getUnsignedPredicate() ? ICI.getUnsignedPredicate()
: ICI.getSignedPredicate(); : ICI.getSignedPredicate();
return new ICmpInst(Pred, LHSI->getOperand(0), return new ICmpInst(Pred, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(), Builder->getInt(RHSV ^ SignBit));
RHSV ^ SignBit));
} }
// (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
@ -1127,8 +1123,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
: ICI.getSignedPredicate(); : ICI.getSignedPredicate();
Pred = ICI.getSwappedPredicate(Pred); Pred = ICI.getSwappedPredicate(Pred);
return new ICmpInst(Pred, LHSI->getOperand(0), return new ICmpInst(Pred, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(), Builder->getInt(RHSV ^ NotSignBit));
RHSV ^ NotSignBit));
} }
} }
} }
@ -1218,11 +1213,9 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
// As a special case, check to see if this means that the // As a special case, check to see if this means that the
// result is always true or false now. // result is always true or false now.
if (ICI.getPredicate() == ICmpInst::ICMP_EQ) if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
return ReplaceInstUsesWith(ICI, return ReplaceInstUsesWith(ICI, Builder->getFalse());
ConstantInt::getFalse(ICI.getContext()));
if (ICI.getPredicate() == ICmpInst::ICMP_NE) if (ICI.getPredicate() == ICmpInst::ICMP_NE)
return ReplaceInstUsesWith(ICI, return ReplaceInstUsesWith(ICI, Builder->getTrue());
ConstantInt::getTrue(ICI.getContext()));
} else { } else {
ICI.setOperand(1, NewCst); ICI.setOperand(1, NewCst);
Constant *NewAndCST; Constant *NewAndCST;
@ -1344,8 +1337,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
ShAmt); ShAmt);
if (Comp != RHS) {// Comparing against a bit that we know is zero. if (Comp != RHS) {// Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
Constant *Cst = Constant *Cst = Builder->getInt1(IsICMP_NE);
ConstantInt::get(Type::getInt1Ty(ICI.getContext()), IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst); return ReplaceInstUsesWith(ICI, Cst);
} }
@ -1364,9 +1356,8 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
if (LHSI->hasOneUse()) { if (LHSI->hasOneUse()) {
// Otherwise strength reduce the shift into an and. // Otherwise strength reduce the shift into an and.
uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
Constant *Mask = Constant *Mask = Builder->getInt(APInt::getLowBitsSet(TypeBits,
ConstantInt::get(ICI.getContext(), APInt::getLowBitsSet(TypeBits, TypeBits - ShAmtVal));
TypeBits-ShAmtVal));
Value *And = Value *And =
Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask"); Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
@ -1464,18 +1455,18 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
if (ICI.isSigned()) { if (ICI.isSigned()) {
if (CR.getLower().isSignBit()) { if (CR.getLower().isSignBit()) {
return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(),CR.getUpper())); Builder->getInt(CR.getUpper()));
} else if (CR.getUpper().isSignBit()) { } else if (CR.getUpper().isSignBit()) {
return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(),CR.getLower())); Builder->getInt(CR.getLower()));
} }
} else { } else {
if (CR.getLower().isMinValue()) { if (CR.getLower().isMinValue()) {
return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(),CR.getUpper())); Builder->getInt(CR.getUpper()));
} else if (CR.getUpper().isMinValue()) { } else if (CR.getUpper().isMinValue()) {
return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
ConstantInt::get(ICI.getContext(),CR.getLower())); Builder->getInt(CR.getLower()));
} }
} }
} }
@ -1555,9 +1546,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
Constant *NotCI = ConstantExpr::getNot(RHS); Constant *NotCI = ConstantExpr::getNot(RHS);
if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
return ReplaceInstUsesWith(ICI, return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
isICMP_NE));
} }
break; break;
@ -1566,9 +1555,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
// If bits are being compared against that are and'd out, then the // If bits are being compared against that are and'd out, then the
// comparison can never succeed! // comparison can never succeed!
if ((RHSV & ~BOC->getValue()) != 0) if ((RHSV & ~BOC->getValue()) != 0)
return ReplaceInstUsesWith(ICI, return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
isICMP_NE));
// If we have ((X & C) == C), turn it into ((X & C) != 0). // If we have ((X & C) == C), turn it into ((X & C) != 0).
if (RHS == BOC && RHSV.isPowerOf2()) if (RHS == BOC && RHSV.isPowerOf2())
@ -1619,7 +1606,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
case Intrinsic::bswap: case Intrinsic::bswap:
Worklist.Add(II); Worklist.Add(II);
ICI.setOperand(0, II->getArgOperand(0)); ICI.setOperand(0, II->getArgOperand(0));
ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap())); ICI.setOperand(1, Builder->getInt(RHSV.byteSwap()));
return &ICI; return &ICI;
case Intrinsic::ctlz: case Intrinsic::ctlz:
case Intrinsic::cttz: case Intrinsic::cttz:
@ -2041,19 +2028,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_ULE:
assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE
return new ICmpInst(ICmpInst::ICMP_ULT, Op0, return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()+1)); Builder->getInt(CI->getValue()+1));
case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SLE:
assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE
return new ICmpInst(ICmpInst::ICMP_SLT, Op0, return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()+1)); Builder->getInt(CI->getValue()+1));
case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_UGE:
assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE
return new ICmpInst(ICmpInst::ICMP_UGT, Op0, return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()-1)); Builder->getInt(CI->getValue()-1));
case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SGE:
assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE
return new ICmpInst(ICmpInst::ICMP_SGT, Op0, return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()-1)); Builder->getInt(CI->getValue()-1));
} }
// If this comparison is a normal comparison, it demands all // If this comparison is a normal comparison, it demands all
@ -2192,7 +2179,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()-1)); Builder->getInt(CI->getValue()-1));
// (x <u 2147483648) -> (x >s -1) -> true if sign bit clear // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
if (CI->isMinValue(true)) if (CI->isMinValue(true))
@ -2211,7 +2198,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()+1)); Builder->getInt(CI->getValue()+1));
// (x >u 2147483647) -> (x <s 0) -> true if sign bit set // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
if (CI->isMaxValue(true)) if (CI->isMaxValue(true))
@ -2229,7 +2216,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()-1)); Builder->getInt(CI->getValue()-1));
} }
break; break;
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGT:
@ -2243,7 +2230,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
return new ICmpInst(ICmpInst::ICMP_EQ, Op0, return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
ConstantInt::get(CI->getContext(), CI->getValue()+1)); Builder->getInt(CI->getValue()+1));
} }
break; break;
case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SGE:
@ -2719,8 +2706,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
ConstantInt *C1, *C2; ConstantInt *C1, *C2;
if (match(B, m_ConstantInt(C1)) && if (match(B, m_ConstantInt(C1)) &&
match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) { match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
Constant *NC = ConstantInt::get(I.getContext(), Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue());
C1->getValue() ^ C2->getValue());
Value *Xor = Builder->CreateXor(C, NC); Value *Xor = Builder->CreateXor(C, NC);
return new ICmpInst(I.getPredicate(), A, Xor); return new ICmpInst(I.getPredicate(), A, Xor);
} }
@ -2885,9 +2871,9 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
Pred = ICmpInst::ICMP_NE; Pred = ICmpInst::ICMP_NE;
break; break;
case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_ORD:
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
case FCmpInst::FCMP_UNO: case FCmpInst::FCMP_UNO:
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
} }
IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
@ -2907,8 +2893,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
Pred == ICmpInst::ICMP_SLE) Pred == ICmpInst::ICMP_SLE)
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
} }
} else { } else {
// If the RHS value is > UnsignedMax, fold the comparison. This handles // If the RHS value is > UnsignedMax, fold the comparison. This handles
@ -2919,8 +2905,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
Pred == ICmpInst::ICMP_ULE) Pred == ICmpInst::ICMP_ULE)
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
} }
} }
@ -2932,8 +2918,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
Pred == ICmpInst::ICMP_SGE) Pred == ICmpInst::ICMP_SGE)
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
} }
} else { } else {
// See if the RHS value is < UnsignedMin. // See if the RHS value is < UnsignedMin.
@ -2943,8 +2929,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT || if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
Pred == ICmpInst::ICMP_UGE) Pred == ICmpInst::ICMP_UGE)
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
} }
} }
@ -2966,14 +2952,14 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
switch (Pred) { switch (Pred) {
default: llvm_unreachable("Unexpected integer comparison!"); default: llvm_unreachable("Unexpected integer comparison!");
case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_ULE:
// (float)int <= 4.4 --> int <= 4 // (float)int <= 4.4 --> int <= 4
// (float)int <= -4.4 --> false // (float)int <= -4.4 --> false
if (RHS.isNegative()) if (RHS.isNegative())
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
break; break;
case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SLE:
// (float)int <= 4.4 --> int <= 4 // (float)int <= 4.4 --> int <= 4
@ -2985,7 +2971,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
// (float)int < -4.4 --> false // (float)int < -4.4 --> false
// (float)int < 4.4 --> int <= 4 // (float)int < 4.4 --> int <= 4
if (RHS.isNegative()) if (RHS.isNegative())
return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); return ReplaceInstUsesWith(I, Builder->getFalse());
Pred = ICmpInst::ICMP_ULE; Pred = ICmpInst::ICMP_ULE;
break; break;
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLT:
@ -2998,7 +2984,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
// (float)int > 4.4 --> int > 4 // (float)int > 4.4 --> int > 4
// (float)int > -4.4 --> true // (float)int > -4.4 --> true
if (RHS.isNegative()) if (RHS.isNegative())
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
break; break;
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGT:
// (float)int > 4.4 --> int > 4 // (float)int > 4.4 --> int > 4
@ -3010,7 +2996,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
// (float)int >= -4.4 --> true // (float)int >= -4.4 --> true
// (float)int >= 4.4 --> int > 4 // (float)int >= 4.4 --> int > 4
if (RHS.isNegative()) if (RHS.isNegative())
return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); return ReplaceInstUsesWith(I, Builder->getTrue());
Pred = ICmpInst::ICMP_UGT; Pred = ICmpInst::ICMP_UGT;
break; break;
case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SGE: