Skip to content

[LoongArch] Allow recognition of b{lt,gt,le,ge}z in disassembly #132620

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
22 changes: 13 additions & 9 deletions llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -2294,11 +2294,15 @@ def : InstAlias<"move $dst, $src", (OR GPR:$dst, GPR:$src, R0)>;
def : InstAlias<"ret", (JIRL R0, R1, 0)>;
def : InstAlias<"jr $rj", (JIRL R0, GPR:$rj, 0)>;

// Branches implemented with alias.
// Always output the canonical mnemonic for the pseudo branch instructions.
// The GNU tools emit the canonical mnemonic for the branch pseudo instructions
// as well (e.g. "bgt" will be recognised by the assembler but never printed by
// objdump). Match this behaviour by setting a zero weight.
// Branches implemented with aliases.
// Disassemble branch instructions not having a $zero operand to the
// canonical mnemonics respectively, but disassemble BLT/BGE with a $zero
// operand to the corresponding pseudo-instruction.
// GNU Binutils behave like this since 2.41, e.g. "bgt" will be recognised
// by the assembler but always disassembles as "blt" by objdump, while "bgtz"
// will come back intact.
// Match this behaviour by setting a zero weight for the b{gt,le}{,u}
// patterns only.
def : InstAlias<"bgt $rj, $rd, $imm16",
(BLT GPR:$rd, GPR:$rj, simm16_lsl2_br:$imm16), 0>;
def : InstAlias<"bgtu $rj, $rd, $imm16",
Expand All @@ -2308,13 +2312,13 @@ def : InstAlias<"ble $rj, $rd, $imm16",
def : InstAlias<"bleu $rj, $rd, $imm16",
(BGEU GPR:$rd, GPR:$rj, simm16_lsl2_br:$imm16), 0>;
def : InstAlias<"bltz $rd, $imm16",
(BLT GPR:$rd, R0, simm16_lsl2_br:$imm16), 0>;
(BLT GPR:$rd, R0, simm16_lsl2_br:$imm16)>;
def : InstAlias<"bgtz $rj, $imm16",
(BLT R0, GPR:$rj, simm16_lsl2_br:$imm16), 0>;
(BLT R0, GPR:$rj, simm16_lsl2_br:$imm16)>;
def : InstAlias<"blez $rj, $imm16",
(BGE R0, GPR:$rj, simm16_lsl2_br:$imm16), 0>;
(BGE R0, GPR:$rj, simm16_lsl2_br:$imm16)>;
def : InstAlias<"bgez $rd, $imm16",
(BGE GPR:$rd, R0, simm16_lsl2_br:$imm16), 0>;
(BGE GPR:$rd, R0, simm16_lsl2_br:$imm16)>;

// Load immediate.
let hasSideEffects = 0, mayLoad = 0, mayStore = 0, isCodeGenOnly = 0,
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/MC/LoongArch/Macros/aliases-br.s
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ ble $a1, $a0, 16
bleu $a1, $a0, 16
# CHECK-NEXT: bgeu $a0, $a1, 16
bltz $a0, 16
# CHECK-NEXT: blt $a0, $zero, 16
# CHECK-NEXT: bltz $a0, 16
bgtz $a0, 16
# CHECK-NEXT: blt $zero, $a0, 16
# CHECK-NEXT: bgtz $a0, 16
blez $a0, 16
# CHECK-NEXT: bge $zero, $a0, 16
# CHECK-NEXT: blez $a0, 16
bgez $a0, 16
# CHECK-NEXT: bge $a0, $zero, 16
# CHECK-NEXT: bgez $a0, 16