@@ -2114,19 +2114,30 @@ bool CodeGenPrepare::optimizeURem(Instruction *Rem) {
2114
2114
// / Some targets have better codegen for `ctpop(X) u< 2` than `ctpop(X) == 1`.
2115
2115
// / This function converts `ctpop(X) ==/!= 1` into `ctpop(X) u</u> 2/1` if the
2116
2116
// / 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) {
2118
2120
ICmpInst::Predicate Pred;
2119
2121
if (!match (Cmp, m_ICmp (Pred, m_Intrinsic<Intrinsic::ctpop>(), m_One ())))
2120
2122
return false ;
2121
2123
if (!ICmpInst::isEquality (Pred))
2122
2124
return false ;
2123
2125
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
+
2124
2137
if (isKnownNonZero (II, DL)) {
2125
- if (Pred == ICmpInst::ICMP_EQ) {
2126
- Cmp->setPredicate (ICmpInst::ICMP_ULT);
2138
+ if (Pred == ICmpInst::ICMP_EQ)
2127
2139
Cmp->setOperand (1 , ConstantInt::get (II->getType (), 2 ));
2128
- } else
2129
- Cmp->setPredicate (ICmpInst::ICMP_UGT);
2140
+ Cmp->setPredicate (NewPred);
2130
2141
return true ;
2131
2142
}
2132
2143
return false ;
@@ -2151,7 +2162,7 @@ bool CodeGenPrepare::optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT) {
2151
2162
if (foldFCmpToFPClassTest (Cmp, *TLI, *DL))
2152
2163
return true ;
2153
2164
2154
- if (adjustIsPower2Test (Cmp, *DL))
2165
+ if (adjustIsPower2Test (Cmp, *TLI, *TTI, * DL))
2155
2166
return true ;
2156
2167
2157
2168
return false ;
0 commit comments