Skip to content

Commit fb6c736

Browse files
author
Nimit Sachdeva
committed
apply suggestions from code review
1 parent 5bc8ace commit fb6c736

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,69 +2005,45 @@ Value *InstCombinerImpl::foldSelectWithConstOpToBinOp(ICmpInst *Cmp,
20052005
/// Likewise, for vector arguments as well.
20062006
static Instruction *foldICmpUSubSatWithAndForMostSignificantBitCmp(
20072007
SelectInst &SI, ICmpInst *ICI, InstCombiner::BuilderTy &Builder) {
2008-
auto *CI = dyn_cast<ICmpInst>(SI.getCondition());
2009-
if (!CI)
2010-
return nullptr;
2011-
2012-
Value *CmpLHS = CI->getOperand(0);
2013-
Value *CmpRHS = CI->getOperand(1);
2014-
if (!match(CmpRHS, m_Zero()))
2015-
return nullptr;
2016-
2017-
auto Pred = CI->getPredicate();
2018-
if (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
2019-
return nullptr;
2020-
2008+
CmpPredicate Pred;
20212009
Value *A, *B;
2022-
const APInt *Constant1, *Constant2, *PossibleMSB;
2023-
if (!match(CmpLHS, m_Or(m_Intrinsic<Intrinsic::usub_sat>(m_Value(A),
2024-
m_APInt(Constant1)),
2025-
m_Intrinsic<Intrinsic::usub_sat>(
2026-
m_Value(B), m_APInt(Constant2)))))
2010+
const APInt *Constant1, *Constant2;
2011+
if (!match(SI.getCondition(),
2012+
m_ICmp(Pred,
2013+
m_Or(m_Intrinsic<Intrinsic::usub_sat>(m_Value(A),
2014+
m_APInt(Constant1)),
2015+
m_Intrinsic<Intrinsic::usub_sat>(m_Value(B),
2016+
m_APInt(Constant2))),
2017+
m_Zero())))
20272018
return nullptr;
20282019

20292020
Value *TrueVal = SI.getTrueValue();
20302021
Value *FalseVal = SI.getFalseValue();
2031-
if (!((match(TrueVal, m_Zero()) && match(FalseVal, m_APInt(PossibleMSB))) ||
2032-
(match(TrueVal, m_APInt(PossibleMSB)) && match(FalseVal, m_Zero()))))
2022+
if (!(Pred == ICmpInst::ICMP_EQ &&
2023+
(match(TrueVal, m_Zero()) && match(FalseVal, m_SignMask()))) ||
2024+
(Pred == ICmpInst::ICMP_NE &&
2025+
(match(TrueVal, m_SignMask()) && match(FalseVal, m_Zero()))))
20332026
return nullptr;
20342027

20352028
auto *Ty = A->getType();
2036-
auto *VecTy = dyn_cast<VectorType>(Ty);
2037-
unsigned BW = PossibleMSB->getBitWidth();
2038-
APInt MostSignificantBit = APInt::getOneBitSet(BW, BW - 1);
2029+
unsigned BW = Constant1->getBitWidth();
2030+
APInt MostSignificantBit = APInt::getSignMask(BW);
20392031

2040-
if (*PossibleMSB != MostSignificantBit ||
2041-
Constant1->ult(MostSignificantBit) || Constant2->ult(MostSignificantBit))
2032+
// Anything over MSB is negative
2033+
if (Constant1->isNonNegative() || Constant2->isNonNegative())
20422034
return nullptr;
20432035

20442036
APInt AdjAP1 = *Constant1 - MostSignificantBit + 1;
20452037
APInt AdjAP2 = *Constant2 - MostSignificantBit + 1;
20462038

2047-
Constant *Adj1, *Adj2;
2048-
if (VecTy) {
2049-
Constant *Elt1 = ConstantInt::get(VecTy->getElementType(), AdjAP1);
2050-
Constant *Elt2 = ConstantInt::get(VecTy->getElementType(), AdjAP2);
2051-
Adj1 = ConstantVector::getSplat(VecTy->getElementCount(), Elt1);
2052-
Adj2 = ConstantVector::getSplat(VecTy->getElementCount(), Elt2);
2053-
} else {
2054-
Adj1 = ConstantInt::get(Ty, AdjAP1);
2055-
Adj2 = ConstantInt::get(Ty, AdjAP2);
2056-
}
2039+
auto *Adj1 = ConstantInt::get(Ty, AdjAP1);
2040+
auto *Adj2 = ConstantInt::get(Ty, AdjAP2);
20572041

20582042
Value *NewA = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, Adj1);
20592043
Value *NewB = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, B, Adj2);
20602044
Value *Or = Builder.CreateOr(NewA, NewB);
2061-
Constant *MSBConst;
2062-
if (VecTy) {
2063-
MSBConst = ConstantVector::getSplat(
2064-
VecTy->getElementCount(),
2065-
ConstantInt::get(VecTy->getScalarType(), *PossibleMSB));
2066-
} else {
2067-
MSBConst = ConstantInt::get(Ty->getScalarType(), *PossibleMSB);
2068-
}
2069-
Value *And = Builder.CreateAnd(Or, MSBConst);
2070-
return cast<Instruction>(And);
2045+
Constant *MSBConst = ConstantInt::get(Ty, MostSignificantBit);
2046+
return BinaryOperator::CreateAnd(Or, MSBConst);
20712047
}
20722048

20732049
/// Visit a SelectInst that has an ICmpInst as its first operand.
@@ -2088,7 +2064,8 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
20882064
return NewSel;
20892065
if (Instruction *Folded =
20902066
foldICmpUSubSatWithAndForMostSignificantBitCmp(SI, ICI, Builder))
2091-
return replaceInstUsesWith(SI, Folded);
2067+
return Folded;
2068+
;
20922069

20932070
// NOTE: if we wanted to, this is where to detect integer MIN/MAX
20942071
bool Changed = false;

0 commit comments

Comments
 (0)