llvm-project/llvm/test/CodeGen/RISCV/and-negpow2-cmp.ll
Craig Topper da19383ae7
[RISCV] Fold (X & -4096) == 0 -> (X >> 12) == 0 (#154233)
This is a more general form of the recently added isel pattern
(seteq (i64 (and GPR:$rs1, 0x8000000000000000)), 0)
  -> (XORI (i64 (SRLI GPR:$rs1, 63)), 1)

We can use a shift right for any AND mask that is a negated power
of 2. But for every other constant we need to use seqz instead of
xori. I don't think there is a benefit to xori over seqz as neither
are compressible.

We already do this transform from target independent code when the setcc
constant is a non-zero subset of the AND mask that is not a legal icmp
immediate.

I don't believe any of these patterns comparing MSBs to 0 are
canonical according to InstCombine. The canonical form is (X < 4096).
I'm curious if these appear during SelectionDAG and if so, how.

My goal here was just to remove the special case isel patterns.
2025-08-18 21:24:35 -07:00

67 lines
1.6 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc < %s -mtriple=riscv32 | FileCheck %s --check-prefixes=CHECK,RV32
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s --check-prefixes=CHECK,RV64
define i1 @test1(i64 %x) {
; RV32-LABEL: test1:
; RV32: # %bb.0:
; RV32-NEXT: slli a2, a1, 2
; RV32-NEXT: srli a0, a0, 30
; RV32-NEXT: srai a1, a1, 30
; RV32-NEXT: or a0, a0, a2
; RV32-NEXT: xori a0, a0, -2
; RV32-NEXT: not a1, a1
; RV32-NEXT: or a0, a0, a1
; RV32-NEXT: seqz a0, a0
; RV32-NEXT: ret
;
; RV64-LABEL: test1:
; RV64: # %bb.0:
; RV64-NEXT: srai a0, a0, 30
; RV64-NEXT: addi a0, a0, 2
; RV64-NEXT: seqz a0, a0
; RV64-NEXT: ret
%a = and i64 %x, -1073741824
%b = icmp eq i64 %a, -2147483648
ret i1 %b
}
define i1 @test2(i32 signext %x) {
; CHECK-LABEL: test2:
; CHECK: # %bb.0:
; CHECK-NEXT: srli a0, a0, 30
; CHECK-NEXT: seqz a0, a0
; CHECK-NEXT: ret
%a = and i32 %x, -1073741824
%b = icmp eq i32 %a, 0
ret i1 %b
}
define i1 @test3(i32 signext %x) {
; CHECK-LABEL: test3:
; CHECK: # %bb.0:
; CHECK-NEXT: srli a0, a0, 29
; CHECK-NEXT: snez a0, a0
; CHECK-NEXT: ret
%a = and i32 %x, -536870912
%b = icmp ne i32 %a, 0
ret i1 %b
}
define i1 @test4(i64 %x) {
; RV32-LABEL: test4:
; RV32: # %bb.0:
; RV32-NEXT: srli a1, a1, 14
; RV32-NEXT: seqz a0, a1
; RV32-NEXT: ret
;
; RV64-LABEL: test4:
; RV64: # %bb.0:
; RV64-NEXT: srli a0, a0, 46
; RV64-NEXT: seqz a0, a0
; RV64-NEXT: ret
%a = and i64 %x, -70368744177664
%b = icmp eq i64 %a, 0
ret i1 %b
}