[llvm] [InstCombine] Fold minmax intrinsic using KnownBits information (PR #76242)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 22 09:02:26 PST 2023


================
@@ -1796,6 +1796,19 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     if (Instruction *NewMinMax = factorizeMinMaxTree(II))
        return NewMinMax;
 
+    // Try to fold minmax based on range information
+    ICmpInst::Predicate Pred =
+        ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
+    bool IsSigned = MinMaxIntrinsic::isSigned(IID);
+    const auto LHS_CR = llvm::computeConstantRangeIncludingKnownBits(
+        I0, IsSigned, SQ.getWithInstruction(II));
----------------
dtcxzyw wrote:

Done.
Before: http://llvm-compile-time-tracker.com/compare.php?from=a863b121c7b28f2c30439ccbd70927ed481835cb&to=5297a541aa26eab6ac5e12e839e4931f08744671&stat=instructions:u
After: http://llvm-compile-time-tracker.com/compare.php?from=a863b121c7b28f2c30439ccbd70927ed481835cb&to=94c65a141a052df42508d40b0d6813721dba77fc&stat=instructions:u

I think it is enough to handle minmax with constant rhs. Then we can avoid computing `RHS_CR`.
See also https://github.com/dtcxzyw/llvm-opt-benchmark/pull/26/files.


https://github.com/llvm/llvm-project/pull/76242


More information about the llvm-commits mailing list