Skip to content

Commit 0a903bf

Browse files
asbsvkeerthy
authored andcommitted
[RISCV] Canonicalize beq/bne with x0 as first arg to beqz/bnez (#141781)
This covers similar ground as #139086. Although I think it makes sense to land both, the practical motivation for #139086 is significantly reduced here. This canonicalisation makes no difference to compressibility (we have compress patterns for the operands in either order), but it does mean that easier to read assembly is printed, as we don't have aliases defined for beq/bne with x0 in either position.
1 parent 56745e0 commit 0a903bf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,6 +4189,15 @@ bool RISCVInstrInfo::simplifyInstruction(MachineInstr &MI) const {
41894189
return true;
41904190
}
41914191
break;
4192+
case RISCV::BEQ:
4193+
case RISCV::BNE:
4194+
// b{eq,ne} zero, rs, imm => b{eq,ne} rs, zero, imm
4195+
if (MI.getOperand(0).getReg() == RISCV::X0) {
4196+
MachineOperand MO0 = MI.getOperand(0);
4197+
MI.removeOperand(0);
4198+
MI.insert(MI.operands_begin() + 1, {MO0});
4199+
}
4200+
break;
41924201
case RISCV::BLTU:
41934202
// bltu zero, rs, imm => bne rs, zero, imm
41944203
if (MI.getOperand(0).getReg() == RISCV::X0) {

llvm/test/CodeGen/RISCV/machine-copyprop-simplifyinstruction.mir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,42 @@ body: |
743743
PseudoRET implicit $x10
744744
...
745745
---
746+
name: beq
747+
body: |
748+
; CHECK-LABEL: name: beq
749+
; CHECK: bb.0:
750+
; CHECK-NEXT: successors: %bb.1(0x80000000)
751+
; CHECK-NEXT: {{ $}}
752+
; CHECK-NEXT: renamable $x11 = COPY $x12
753+
; CHECK-NEXT: BEQ $x12, $x0, %bb.1
754+
; CHECK-NEXT: {{ $}}
755+
; CHECK-NEXT: bb.1:
756+
; CHECK-NEXT: PseudoRET
757+
bb.0:
758+
renamable $x11 = COPY $x12
759+
BEQ $x0, renamable $x11, %bb.1
760+
bb.1:
761+
PseudoRET
762+
...
763+
---
764+
name: bne
765+
body: |
766+
; CHECK-LABEL: name: bne
767+
; CHECK: bb.0:
768+
; CHECK-NEXT: successors: %bb.1(0x80000000)
769+
; CHECK-NEXT: {{ $}}
770+
; CHECK-NEXT: renamable $x11 = COPY $x12
771+
; CHECK-NEXT: BNE $x12, $x0, %bb.1
772+
; CHECK-NEXT: {{ $}}
773+
; CHECK-NEXT: bb.1:
774+
; CHECK-NEXT: PseudoRET
775+
bb.0:
776+
renamable $x11 = COPY $x12
777+
BNE $x0, renamable $x11, %bb.1
778+
bb.1:
779+
PseudoRET
780+
...
781+
---
746782
name: bltu
747783
body: |
748784
; CHECK-LABEL: name: bltu

0 commit comments

Comments
 (0)