Skip to content

[RISCV][MC] Add aliases for beq/bne with x0 as the first argument => beqz/bnez #139086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,12 @@ def : InstAlias<"sgtu $rd, $rs, $rt", (SLTU GPR:$rd, GPR:$rt, GPR:$rs), 0>;

def : InstAlias<"beqz $rs, $offset",
(BEQ GPR:$rs, X0, bare_simm13_lsb0:$offset)>;
def : InstAlias<"beqz $rs, $offset",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What guarantees the canonical order will be first in the matching table?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. Firstly it's worth noting that we do have test coverage that will catch if the order of the aliases is switched for some reason. Tracing through how it's ordered today:

  • AsmMatcherEmitter adds MatchableInfo generated from the InstAlias to the Matchables vector in the order they are returned from getAllDerivedDefinitions. This isn't explicitly documented as being the source order in the .td, but appears to be so (and I imagine other things would break if that were changed).
  • The Matchables are then sorted using a stable sort.
  • Looking at beqz as an example, neither definition is less than the other according to the comparison function (same number of operands, operands compare the same. It compares two operands MCK_GPR == MCK_GPR, MCK_BareSImm13Lsb0 == MCK_BareSImm13Lsb0 (i.e. it is comparing the alias rather than the transformed instruction). And ultimately the lessthan comparison function just returns fals, so the aliases remain in source order in the matching table.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the initial sort is by Record name. Anonymous records don't have name so they are sorted by the source order.

(BEQ X0, GPR:$rs, bare_simm13_lsb0:$offset)>;
def : InstAlias<"bnez $rs, $offset",
(BNE GPR:$rs, X0, bare_simm13_lsb0:$offset)>;
def : InstAlias<"bnez $rs, $offset",
(BNE X0, GPR:$rs, bare_simm13_lsb0:$offset)>;
def : InstAlias<"blez $rs, $offset",
(BGE X0, GPR:$rs, bare_simm13_lsb0:$offset)>;
def : InstAlias<"bgez $rs, $offset",
Expand Down
20 changes: 16 additions & 4 deletions llvm/test/MC/RISCV/rvi-aliases-valid.s
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,22 @@ bgtu x17, x18, 28
# CHECK-OBJ: bgeu s3, s2, 0x70
bleu x18, x19, 32

# Emit beqz/bnez alias even when operands are reversed to the canonical form.
# CHECK-S-NOALIAS: beq zero, a0, 512
# CHECK-S: beqz a0, 512
# CHECK-OBJ-NOALIAS: beq zero, a0, 0x254
# CHECK-OBJ: beqz a0, 0x254
beq zero, x10, 512
# CHECK-S-NOALIAS: bne zero, a1, 1024
# CHECK-S: bnez a1, 1024
# CHECK-OBJ-NOALIAS: bne zero, a1, 0x458
# CHECK-OBJ: bnez a1, 0x458
bne zero, x11, 1024

# CHECK-S-NOALIAS: jal zero, 2044
# CHECK-S: j 2044
# CHECK-OBJ-NOALIAS: jal zero, 0x850
# CHECK-OBJ: j 0x850
# CHECK-OBJ-NOALIAS: jal zero, 0x858
# CHECK-OBJ: j 0x858
j 2044
# CHECK-S-NOALIAS: jal zero, foo
# CHECK-S: j foo
Expand All @@ -148,8 +160,8 @@ j a0
j .
# CHECK-S-NOALIAS: jal ra, 2040
# CHECK-S: jal 2040
# CHECK-OBJ-NOALIAS: jal ra, 0x85c
# CHECK-OBJ: jal 0x85c
# CHECK-OBJ-NOALIAS: jal ra, 0x864
# CHECK-OBJ: jal 0x864
jal 2040
# CHECK-S-NOALIAS: jal ra, foo
# CHECK-S: jal foo
Expand Down
Loading