[llvm] [Transforms] Remove m_OneUse requirement only for max, not min (PR #81505)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 25 11:00:03 PST 2024
================
@@ -2277,14 +2277,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
// max X, -X --> fabs X
// min X, -X --> -(fabs X)
- // TODO: Remove one-use limitation? That is obviously better for max.
- // It would be an extra instruction for min (fnabs), but that is
- // still likely better for analysis and codegen.
- if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) && Arg1 == X) ||
- (match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) && Arg0 == X)) {
+
+ // No one-use. Only for max.
+ // TODO: Remove one-use limitation? That is obviously better for max,
+ // hence why we don't check for one-use for that. However,
+ // it would be an extra instruction for min (fnabs), but
+ // that is still likely better for analysis and codegen.
+ // If so, then allow this if-statement clause to handle min,
+ // and delete the clause below this one.
+ if ((((match(Arg0, m_FNeg(m_Value(X)))) && match(Arg1, m_Specific(X))) ||
+ (match(Arg1, m_FNeg(m_Value(X))) && match(Arg0, m_Specific(X)))) &&
+ IID != Intrinsic::minimum && IID != Intrinsic::minnum) {
+ Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
+ return replaceInstUsesWith(*II, R);
+ }
+
+ // One-use version for min.
+ if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) &&
+ match(Arg1, m_Specific(X))) ||
+ (match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) &&
+ match(Arg0, m_Specific(X)))) {
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
- if (IID == Intrinsic::minimum || IID == Intrinsic::minnum)
- R = Builder.CreateFNegFMF(R, II);
+ R = Builder.CreateFNegFMF(R, II);
----------------
goldsteinn wrote:
Personally would just keep one condition and write it as:
```
auto IsMinMaxOrXNegX = [IID, &X](Value * Op0, Value * Op1) {
if(!match(Op0, m_FNeg(m_Value(X))) || X == Op1)
return false;
return Op0->hasOneUse() || (IID != Intrinsic::minimum && IID != Intrinsic::minnum);
};
if (IsMinMaxOrXNegX(Arg0, Arg1) || IsMinMaxOrXNegX(Arg1, Arg0)) {
...
}
```
https://github.com/llvm/llvm-project/pull/81505
More information about the llvm-commits
mailing list