Skip to content

Commit 78b1681

Browse files
committed
[RISCV] Canonicalize beq/bne with x0 as first arg to beqz/bnez
This covers similar ground as llvm#139086. Although I think it makes sense to land both, the practical motivation for llvm#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 dcf3c79 commit 78b1681

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
@@ -4191,6 +4191,15 @@ bool RISCVInstrInfo::simplifyInstruction(MachineInstr &MI) const {
41914191
return true;
41924192
}
41934193
break;
4194+
case RISCV::BEQ:
4195+
case RISCV::BNE:
4196+
// b{eq,ne} zero, rs, imm => b{eq,ne} rs, zero, imm
4197+
if (MI.getOperand(0).getReg() == RISCV::X0) {
4198+
MachineOperand MO0 = MI.getOperand(0);
4199+
MI.removeOperand(0);
4200+
MI.insert(MI.operands_begin() + 1, {MO0});
4201+
}
4202+
break;
41944203
}
41954204
return false;
41964205
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,39 @@ body: |
742742
renamable $x10 = MAXU renamable $x11, renamable $x11
743743
PseudoRET implicit $x10
744744
...
745+
---
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+
...

0 commit comments

Comments
 (0)