[llvm] db57396 - [InstCombine] reduce code duplication; NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 6 10:28:58 PST 2019
Author: Sanjay Patel
Date: 2019-12-06T13:26:45-05:00
New Revision: db5739658467e20a52f20e769d3580412e13ff87
URL: https://github.com/llvm/llvm-project/commit/db5739658467e20a52f20e769d3580412e13ff87
DIFF: https://github.com/llvm/llvm-project/commit/db5739658467e20a52f20e769d3580412e13ff87.diff
LOG: [InstCombine] reduce code duplication; NFC
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 5112fb1a6c39..a3c1b2777e3b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -846,27 +846,32 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) {
Instruction *InstCombiner::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext,
bool DoTransform) {
+ Value *Op0 = Cmp->getOperand(0);
+ Value *Op1 = Cmp->getOperand(1);
+ ICmpInst::Predicate Pred = Cmp->getPredicate();
+ Type *ZType = Zext.getType();
+ Type *CmpOpType = Op0->getType();
+
// If we are just checking for a icmp eq of a single bit and zext'ing it
// to an integer, then shift the bit to the appropriate place and then
// cast to integer to avoid the comparison.
- const APInt *Op1CV;
- if (match(Cmp->getOperand(1), m_APInt(Op1CV))) {
-
+ const APInt *Op1C;
+ if (match(Op1, m_APInt(Op1C))) {
// zext (x <s 0) to i32 --> x>>u31 true if signbit set.
// zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
- if ((Cmp->getPredicate() == ICmpInst::ICMP_SLT && Op1CV->isNullValue()) ||
- (Cmp->getPredicate() == ICmpInst::ICMP_SGT && Op1CV->isAllOnesValue())) {
+ if ((Pred == ICmpInst::ICMP_SLT && Op1C->isNullValue()) ||
+ (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) {
if (!DoTransform) return Cmp;
- Value *In = Cmp->getOperand(0);
- Value *Sh = ConstantInt::get(In->getType(),
- In->getType()->getScalarSizeInBits() - 1);
- In = Builder.CreateLShr(In, Sh, In->getName() + ".lobit");
- if (In->getType() != Zext.getType())
- In = Builder.CreateIntCast(In, Zext.getType(), false /*ZExt*/);
+ Value *In = Op0;
+ Value *ShAmt = ConstantInt::get(CmpOpType,
+ CmpOpType->getScalarSizeInBits() - 1);
+ In = Builder.CreateLShr(In, ShAmt, In->getName() + ".lobit");
+ if (CmpOpType != ZType)
+ In = Builder.CreateIntCast(In, ZType, false /*ZExt*/);
- if (Cmp->getPredicate() == ICmpInst::ICMP_SGT) {
- Constant *One = ConstantInt::get(In->getType(), 1);
+ if (Pred == ICmpInst::ICMP_SGT) {
+ Constant *One = ConstantInt::get(CmpOpType, 1);
In = Builder.CreateXor(In, One, In->getName() + ".not");
}
@@ -881,42 +886,42 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext,
// zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
// zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
// zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
- if ((Op1CV->isNullValue() || Op1CV->isPowerOf2()) &&
+ if ((Op1C->isNullValue() || Op1C->isPowerOf2()) &&
// This only works for EQ and NE
Cmp->isEquality()) {
// If Op1C some other power of two, convert:
- KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext);
+ KnownBits Known = computeKnownBits(Op0, 0, &Zext);
APInt KnownZeroMask(~Known.Zero);
if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
if (!DoTransform) return Cmp;
- bool isNE = Cmp->getPredicate() == ICmpInst::ICMP_NE;
- if (!Op1CV->isNullValue() && (*Op1CV != KnownZeroMask)) {
+ bool isNE = Pred == ICmpInst::ICMP_NE;
+ if (!Op1C->isNullValue() && (*Op1C != KnownZeroMask)) {
// (X&4) == 2 --> false
// (X&4) != 2 --> true
- Constant *Res = ConstantInt::get(Zext.getType(), isNE);
+ Constant *Res = ConstantInt::get(ZType, isNE);
return replaceInstUsesWith(Zext, Res);
}
uint32_t ShAmt = KnownZeroMask.logBase2();
- Value *In = Cmp->getOperand(0);
+ Value *In = Op0;
if (ShAmt) {
// Perform a logical shr by shiftamt.
// Insert the shift to put the result in the low bit.
- In = Builder.CreateLShr(In, ConstantInt::get(In->getType(), ShAmt),
+ In = Builder.CreateLShr(In, ConstantInt::get(CmpOpType, ShAmt),
In->getName() + ".lobit");
}
- if (!Op1CV->isNullValue() == isNE) { // Toggle the low bit.
- Constant *One = ConstantInt::get(In->getType(), 1);
+ if (!Op1C->isNullValue() == isNE) { // Toggle the low bit.
+ Constant *One = ConstantInt::get(CmpOpType, 1);
In = Builder.CreateXor(In, One);
}
- if (Zext.getType() == In->getType())
+ if (ZType == CmpOpType)
return replaceInstUsesWith(Zext, In);
- Value *IntCast = Builder.CreateIntCast(In, Zext.getType(), false);
+ Value *IntCast = Builder.CreateIntCast(In, ZType, false);
return replaceInstUsesWith(Zext, IntCast);
}
}
@@ -925,32 +930,29 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext,
// icmp ne A, B is equal to xor A, B when A and B only really have one bit.
// It is also profitable to transform icmp eq into not(xor(A, B)) because that
// may lead to additional simplifications.
- if (Cmp->isEquality() && Zext.getType() == Cmp->getOperand(0)->getType()) {
- if (IntegerType *ITy = dyn_cast<IntegerType>(Zext.getType())) {
- Value *LHS = Cmp->getOperand(0);
- Value *RHS = Cmp->getOperand(1);
-
- KnownBits KnownLHS = computeKnownBits(LHS, 0, &Zext);
- KnownBits KnownRHS = computeKnownBits(RHS, 0, &Zext);
+ if (Cmp->isEquality() && ZType == CmpOpType) {
+ if (IntegerType *ITy = dyn_cast<IntegerType>(ZType)) {
+ KnownBits KnownOp0 = computeKnownBits(Op0, 0, &Zext);
+ KnownBits KnownOp1 = computeKnownBits(Op1, 0, &Zext);
- if (KnownLHS.Zero == KnownRHS.Zero && KnownLHS.One == KnownRHS.One) {
- APInt KnownBits = KnownLHS.Zero | KnownLHS.One;
+ if (KnownOp0.Zero == KnownOp1.Zero && KnownOp0.One == KnownOp1.One) {
+ APInt KnownBits = KnownOp0.Zero | KnownOp0.One;
APInt UnknownBit = ~KnownBits;
if (UnknownBit.countPopulation() == 1) {
if (!DoTransform) return Cmp;
- Value *Result = Builder.CreateXor(LHS, RHS);
+ Value *Result = Builder.CreateXor(Op0, Op1);
// Mask off any bits that are set and won't be shifted away.
- if (KnownLHS.One.uge(UnknownBit))
+ if (KnownOp0.One.uge(UnknownBit))
Result = Builder.CreateAnd(Result,
- ConstantInt::get(ITy, UnknownBit));
+ ConstantInt::get(ITy, UnknownBit));
// Shift the bit we're testing down to the lsb.
Result = Builder.CreateLShr(
- Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
+ Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
- if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
+ if (Pred == ICmpInst::ICMP_EQ)
Result = Builder.CreateXor(Result, ConstantInt::get(ITy, 1));
Result->takeName(Cmp);
return replaceInstUsesWith(Zext, Result);
More information about the llvm-commits
mailing list