[llvm] [RISCV] Remove redundant sign extensions from BEXT indices (PR #228398)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 04:24:11 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: tinfengyu

<details>
<summary>Changes</summary>

`RISCVOptWInstrs` can remove redundant `sext.w` operations when all users only depend on bits preserved by the original 32-bit value, but does not account for the limited width of the `BEXT` index operand.

Extend `hasAllNBitUsers` to recognize that the `BEXT` index operand only requires `log2(XLEN)` low bits. On RV64, `BEXT` effectively uses `index & 63`, so `sext.w` does not affect the selected bit when its result is used only as the index. The rule is intentionally limited to the index operand, as the data operand may require the full XLEN-wide value.

Tests cover index sign-extension elimination, preservation of the data operand, shared data/index uses, additional full-width users, propagation through `COPY` and `PHI`, the six-bit boundary and five-bit negative case, as well as RV32 behavior. Focused and nearby RISC-V tests pass with MachineVerifier enabled.

AI tool usage: OpenAI Codex assisted with investigation, implementation, and validation. I reviewed and understand the implementation, tests, and validation results and take responsibility for the contribution.


---
Full diff: https://github.com/llvm/llvm-project/pull/228398.diff


3 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp (+2-1) 
- (added) llvm/test/CodeGen/RISCV/opt-w-instrs-bext.ll (+54) 
- (added) llvm/test/CodeGen/RISCV/opt-w-instrs-bext.mir (+176) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp b/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
index 18c8611c337f8..44a09d6a91157 100644
--- a/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
+++ b/llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp
@@ -277,7 +277,8 @@ static bool hasAllNBitUsers(const MachineInstr &OrigMI,
       case RISCV::SRL:
       case RISCV::ROL:
       case RISCV::ROR:
-        // Operand 2 is the shift amount which uses 6 bits.
+      case RISCV::BEXT:
+        // Operand 2 is the shift amount or bit index, using log2(XLEN) bits.
         if (OpIdx == 2 && Bits >= Log2_32(ST.getXLen()))
           break;
         return false;
diff --git a/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.ll b/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.ll
new file mode 100644
index 0000000000000..23c34fc584e27
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.ll
@@ -0,0 +1,54 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv64 -mattr=+zbs -verify-machineinstrs < %s | FileCheck %s
+
+; The index operand only reads log2(XLEN) bits, while the data operand can
+; still read any bit selected by the index.
+; These unmasked IR shifts are poison for actual shift amounts >= 64.
+; The MIR test also covers unknown register values without that IR constraint.
+
+define i64 @bext_index(i64 %data, i64 %index, i1 %condition) {
+; CHECK-LABEL: bext_index:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    andi a2, a2, 1
+; CHECK-NEXT:    beqz a2, .LBB0_2
+; CHECK-NEXT:  # %bb.1: # %extract
+; CHECK-NEXT:    bext a0, a0, a1
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB0_2: # %other
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:    ret
+entry:
+  br i1 %condition, label %extract, label %other
+extract:
+  %narrow = trunc i64 %index to i32
+  %signed = sext i32 %narrow to i64
+  %shift = lshr i64 %data, %signed
+  %bit = and i64 %shift, 1
+  ret i64 %bit
+other:
+  ret i64 0
+}
+
+define i64 @bext_data(i64 %data, i64 %index, i1 %condition) {
+; CHECK-LABEL: bext_data:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    andi a2, a2, 1
+; CHECK-NEXT:    beqz a2, .LBB1_2
+; CHECK-NEXT:  # %bb.1: # %extract
+; CHECK-NEXT:    sext.w a0, a0
+; CHECK-NEXT:    bext a0, a0, a1
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB1_2: # %other
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:    ret
+entry:
+  br i1 %condition, label %extract, label %other
+extract:
+  %narrow = trunc i64 %data to i32
+  %signed = sext i32 %narrow to i64
+  %shift = lshr i64 %signed, %index
+  %bit = and i64 %shift, 1
+  ret i64 %bit
+other:
+  ret i64 0
+}
diff --git a/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.mir b/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.mir
new file mode 100644
index 0000000000000..4de574690cf9d
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/opt-w-instrs-bext.mir
@@ -0,0 +1,176 @@
+# RUN: llc -mtriple=riscv64 -mattr=+zbs -verify-machineinstrs -run-pass=riscv-opt-w-instrs %s -o - | FileCheck %s
+# RUN: llc -mtriple=riscv64 -mattr=+zbs -verify-machineinstrs -passes=riscv-opt-w-instrs %s -o - | FileCheck %s
+
+# Inputs are unknown XLEN-wide values, including values with bit 31 set.
+# BEXT masks the index to six bits: indices 31, 32, 63 and 64 select bits
+# 31, 32, 63 and 0. For example, sign-extending 0x80000001 does not change
+# its low six index bits, but does change its data bits above bit 31.
+
+---
+name: index
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10, $x11
+
+    ; CHECK-LABEL: name: index
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[DATA]], [[INDEX]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = ADDIW %1, 0
+    %3:gpr = BEXT %0, %2
+    $x10 = COPY %3
+    PseudoRET
+...
+
+---
+name: data
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10, $x11
+
+    ; CHECK-LABEL: name: data
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[SEXT:%[0-9]+]]:gpr = ADDIW [[DATA]], 0
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[SEXT]], [[INDEX]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = ADDIW %0, 0
+    %3:gpr = BEXT %2, %1
+    $x10 = COPY %3
+    PseudoRET
+...
+
+---
+name: data_and_index
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10
+
+    ; CHECK-LABEL: name: data_and_index
+    ; CHECK: [[INPUT:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[SEXT:%[0-9]+]]:gpr = ADDIW [[INPUT]], 0
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[SEXT]], [[SEXT]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = ADDIW %0, 0
+    %2:gpr = BEXT %1, %1
+    $x10 = COPY %2
+    PseudoRET
+...
+
+# An index use cannot justify dropping a sign-extension with a full-width use.
+---
+name: index_and_store
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10, $x11, $x12
+
+    ; CHECK-LABEL: name: index_and_store
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[ADDR:%[0-9]+]]:gpr = COPY $x12
+    ; CHECK-NEXT: [[SEXT:%[0-9]+]]:gpr = ADDIW [[INDEX]], 0
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[DATA]], [[SEXT]]
+    ; CHECK-NEXT: SD [[SEXT]], [[ADDR]], 0
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = COPY $x12
+    %3:gpr = ADDIW %1, 0
+    %4:gpr = BEXT %0, %3
+    SD %3, %2, 0
+    $x10 = COPY %4
+    PseudoRET
+...
+
+# Demand propagates through both virtual COPY and PHI users.
+---
+name: index_copy_phi
+tracksRegLiveness: true
+body: |
+  bb.0:
+    successors: %bb.1, %bb.2
+    liveins: $x10, $x11, $x12
+
+    ; CHECK-LABEL: name: index_copy_phi
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[OTHER:%[0-9]+]]:gpr = COPY $x12
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY [[INDEX]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = COPY $x12
+    %3:gpr = ADDIW %1, 0
+    %4:gpr = COPY %3
+    BEQ %0, $x0, %bb.2
+    PseudoBR %bb.1
+
+  bb.1:
+    successors: %bb.2
+    PseudoBR %bb.2
+
+  bb.2:
+    ; CHECK: [[PHI:%[0-9]+]]:gpr = PHI [[COPY]], %bb.0, [[OTHER]], %bb.1
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[DATA]], [[PHI]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %5:gpr = PHI %4, %bb.0, %2, %bb.1
+    %6:gpr = BEXT %0, %5
+    $x10 = COPY %6
+    PseudoRET
+...
+
+# Shifting the shared low 32 bits right by 26 leaves all six index bits.
+---
+name: index_srli26
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10, $x11
+
+    ; CHECK-LABEL: name: index_srli26
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INPUT:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = SRLI [[INPUT]], 26
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[DATA]], [[INDEX]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = ADDIW %1, 0
+    %3:gpr = SRLI %2, 26
+    %4:gpr = BEXT %0, %3
+    $x10 = COPY %4
+    PseudoRET
+...
+
+# With a shift by 27, input bit 32 reaches index bit 5 and must be preserved.
+---
+name: index_srli27
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x10, $x11
+
+    ; CHECK-LABEL: name: index_srli27
+    ; CHECK: [[DATA:%[0-9]+]]:gpr = COPY $x10
+    ; CHECK-NEXT: [[INPUT:%[0-9]+]]:gpr = COPY $x11
+    ; CHECK-NEXT: [[SEXT:%[0-9]+]]:gpr = ADDIW [[INPUT]], 0
+    ; CHECK-NEXT: [[INDEX:%[0-9]+]]:gpr = SRLI [[SEXT]], 27
+    ; CHECK-NEXT: [[BIT:%[0-9]+]]:gpr = BEXT [[DATA]], [[INDEX]]
+    ; CHECK-NEXT: $x10 = COPY [[BIT]]
+    %0:gpr = COPY $x10
+    %1:gpr = COPY $x11
+    %2:gpr = ADDIW %1, 0
+    %3:gpr = SRLI %2, 27
+    %4:gpr = BEXT %0, %3
+    $x10 = COPY %4
+    PseudoRET
+...

``````````

</details>


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


More information about the llvm-commits mailing list