[llvm] [InstCombine] Prefer to keep power-of-2 constants when combining ashr exact and slt/ult of a constant (PR #86111)
Alex Bradbury via llvm-commits
llvm-commits at lists.llvm.org
Fri May 10 05:49:41 PDT 2024
https://github.com/asb updated https://github.com/llvm/llvm-project/pull/86111
>From 8624cf869bd8585f6966449332bca9cdf8322ed0 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb at igalia.com>
Date: Thu, 21 Mar 2024 11:10:06 +0000
Subject: [PATCH] [InstCombine] Prefer to keep power-of-2 constants when
combining ashr exact and slt/ult of a constant
We have flexibility in what constant to use when combining an `ashr
exact` with a slt or ult of a constant, and it's not possible to revisit
this decision later in the compilation pipeline after the `ashr exact`
is removed. Keeping a constant close to power-of-2 (pow2val + 1) should
be no worse than neutral, and in some cases may allow better codegen
later on for targets that can more cheaply generate power of 2 or near
power of 2 constants.
Alive2 proofs:
<https://alive2.llvm.org/ce/z/2BmPnq> and
<https://alive2.llvm.org/ce/z/DtuhnR>
---
.../lib/Transforms/InstCombine/InstCombineCompares.cpp | 10 ++++++++++
llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll | 10 ++++------
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e1a3194a1beb7..9883d02c87a32 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2479,6 +2479,16 @@ Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
// those conditions rather than checking them. This is difficult because of
// undef/poison (PR34838).
if (IsAShr && Shr->hasOneUse()) {
+ if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
+ (C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
+ // When C - 1 is a power of two and the transform can be legally
+ // performed, prefer this form so the produced constant is close to a
+ // power of two.
+ // icmp slt/ult (ashr exact X, ShAmtC), C
+ // --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
+ APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
+ return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
+ }
if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
// When ShAmtC can be shifted losslessly:
// icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
diff --git a/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll b/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
index 4dd5b09259144..5f09964fd93ad 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
@@ -3379,7 +3379,7 @@ define i1 @ashrslt_01_01_exact(i4 %x) {
define i1 @ashrslt_01_02_exact(i4 %x) {
; CHECK-LABEL: @ashrslt_01_02_exact(
-; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 4
+; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 3
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i4 %x, 1
@@ -3389,7 +3389,7 @@ define i1 @ashrslt_01_02_exact(i4 %x) {
define i1 @ashrslt_01_03_exact(i4 %x) {
; CHECK-LABEL: @ashrslt_01_03_exact(
-; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 6
+; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 5
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i4 %x, 1
@@ -3800,11 +3800,9 @@ define i1 @ashrslt_03_15_exact(i4 %x) {
ret i1 %c
}
-; TODO: The resulting compared constant can be safely replaced with one that
-; is closer to a power of two.
define i1 @ashr_slt_exact_near_pow2_cmpval(i8 %x) {
; CHECK-LABEL: @ashr_slt_exact_near_pow2_cmpval(
-; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X:%.*]], 10
+; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X:%.*]], 9
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i8 %x, 1
@@ -3814,7 +3812,7 @@ define i1 @ashr_slt_exact_near_pow2_cmpval(i8 %x) {
define i1 @ashr_ult_exact_near_pow2_cmpval(i8 %x) {
; CHECK-LABEL: @ashr_ult_exact_near_pow2_cmpval(
-; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[X:%.*]], 10
+; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[X:%.*]], 9
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i8 %x, 1
More information about the llvm-commits
mailing list