Skip to content

Commit 7357e3a

Browse files
committed
[CodeGenPrepare] Check if it is profitable for the target
1 parent 2e810b5 commit 7357e3a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,19 +2114,30 @@ bool CodeGenPrepare::optimizeURem(Instruction *Rem) {
21142114
/// Some targets have better codegen for `ctpop(X) u< 2` than `ctpop(X) == 1`.
21152115
/// This function converts `ctpop(X) ==/!= 1` into `ctpop(X) u</u> 2/1` if the
21162116
/// result cannot be zero.
2117-
static bool adjustIsPower2Test(CmpInst *Cmp, const DataLayout &DL) {
2117+
static bool adjustIsPower2Test(CmpInst *Cmp, const TargetLowering &TLI,
2118+
const TargetTransformInfo &TTI,
2119+
const DataLayout &DL) {
21182120
ICmpInst::Predicate Pred;
21192121
if (!match(Cmp, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(), m_One())))
21202122
return false;
21212123
if (!ICmpInst::isEquality(Pred))
21222124
return false;
21232125
auto *II = cast<IntrinsicInst>(Cmp->getOperand(0));
2126+
2127+
// Check if it is profitable for the target
2128+
ICmpInst::Predicate NewPred =
2129+
Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGT;
2130+
if (TLI.isCtpopFast(TLI.getValueType(DL, II->getType())) &&
2131+
TTI.getCmpSelInstrCost(Instruction::ICmp, II->getType(), Cmp->getType(),
2132+
Pred) <
2133+
TTI.getCmpSelInstrCost(Instruction::ICmp, II->getType(),
2134+
Cmp->getType(), NewPred))
2135+
return false;
2136+
21242137
if (isKnownNonZero(II, DL)) {
2125-
if (Pred == ICmpInst::ICMP_EQ) {
2126-
Cmp->setPredicate(ICmpInst::ICMP_ULT);
2138+
if (Pred == ICmpInst::ICMP_EQ)
21272139
Cmp->setOperand(1, ConstantInt::get(II->getType(), 2));
2128-
} else
2129-
Cmp->setPredicate(ICmpInst::ICMP_UGT);
2140+
Cmp->setPredicate(NewPred);
21302141
return true;
21312142
}
21322143
return false;
@@ -2151,7 +2162,7 @@ bool CodeGenPrepare::optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT) {
21512162
if (foldFCmpToFPClassTest(Cmp, *TLI, *DL))
21522163
return true;
21532164

2154-
if (adjustIsPower2Test(Cmp, *DL))
2165+
if (adjustIsPower2Test(Cmp, *TLI, *TTI, *DL))
21552166
return true;
21562167

21572168
return false;

0 commit comments

Comments
 (0)