llvm.org GIT mirror llvm / 4926f35
[RISCV] Match GNU tools canonical JALR and add aliases The canonical GNU form of JALR resembles a load/store instruction rather than placing the immediate offset as a separate argument, so match this behaviour. Also add parser-only aliases for the three-operand form, and add other shorter aliases also emitted by GNU tools. Differential Revision: https://reviews.llvm.org/D55277 Patch by James Clarke. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366179 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Bradbury 3 months ago
9 changed file(s) with 60 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
356356 let isCall = 1 in
357357 def JALR : RVInstI<0b000, OPC_JALR, (outs GPR:$rd),
358358 (ins GPR:$rs1, simm12:$imm12),
359 "jalr", "$rd, $rs1, $imm12">;
359 "jalr", "$rd, ${imm12}(${rs1})">;
360360 } // hasSideEffects = 0, mayLoad = 0, mayStore = 0
361361
362362 def BEQ : BranchCC_rri<0b000, "beq">;
596596 def : InstAlias<"bleu $rs, $rt, $offset",
597597 (BGEU GPR:$rt, GPR:$rs, simm13_lsb0:$offset), 0>;
598598
599 // "ret" has more weight since "ret" and "jr" alias the same "jalr" instruction.
600 def : InstAlias<"j $offset", (JAL X0, simm21_lsb0_jal:$offset)>;
601 def : InstAlias<"jal $offset", (JAL X1, simm21_lsb0_jal:$offset)>;
602 def : InstAlias<"jr $rs", (JALR X0, GPR:$rs, 0)>;
603 def : InstAlias<"jalr $rs", (JALR X1, GPR:$rs, 0)>;
604 def : InstAlias<"ret", (JALR X0, X1, 0), 2>;
599 def : InstAlias<"j $offset", (JAL X0, simm21_lsb0_jal:$offset)>;
600 def : InstAlias<"jal $offset", (JAL X1, simm21_lsb0_jal:$offset)>;
601
602 // Non-zero offset aliases of "jalr" are the lowest weight, followed by the
603 // two-register form, then the one-register forms and finally "ret".
604 def : InstAlias<"jr $rs", (JALR X0, GPR:$rs, 0), 3>;
605 def : InstAlias<"jr ${offset}(${rs})", (JALR X0, GPR:$rs, simm12:$offset)>;
606 def : InstAlias<"jalr $rs", (JALR X1, GPR:$rs, 0), 3>;
607 def : InstAlias<"jalr ${offset}(${rs})", (JALR X1, GPR:$rs, simm12:$offset)>;
608 def : InstAlias<"jalr $rd, $rs", (JALR GPR:$rd, GPR:$rs, 0), 2>;
609 def : InstAlias<"ret", (JALR X0, X1, 0), 4>;
610
611 // Non-canonical forms for jump targets also accepted by the assembler.
612 def : InstAlias<"jr $rs, $offset", (JALR X0, GPR:$rs, simm12:$offset), 0>;
613 def : InstAlias<"jalr $rs, $offset", (JALR X1, GPR:$rs, simm12:$offset), 0>;
614 def : InstAlias<"jalr $rd, $rs, $offset", (JALR GPR:$rd, GPR:$rs, simm12:$offset), 0>;
615
605616 // TODO call
606617 // TODO tail
607618
2424 ret void
2525 }
2626
27 ; TODO: Extend simm12's MCOperandPredicate so the jalr zero is printed as a jr.
2728 define i32 @relax_jal(i1 %a) nounwind {
2829 ; CHECK-LABEL: relax_jal:
2930 ; CHECK: # %bb.0:
3132 ; CHECK-NEXT: bnez a0, .LBB1_1
3233 ; CHECK-NEXT: # %bb.3:
3334 ; CHECK-NEXT: lui a0, %hi(.LBB1_2)
34 ; CHECK-NEXT: jalr zero, a0, %lo(.LBB1_2)
35 ; CHECK-NEXT: jalr zero, %lo(.LBB1_2)(a0)
3536 ; CHECK-NEXT: .LBB1_1: # %iftrue
3637 ; CHECK-NEXT: #APP
3738 ; CHECK-NEXT: #NO_APP
2424 ; RV32I: # %bb.0:
2525 ; RV32I-NEXT: addi sp, sp, -16
2626 ; RV32I-NEXT: sw ra, 12(sp)
27 ; RV32I-NEXT: jalr zero, a0, 1380
27 ; RV32I-NEXT: jr 1380(a0)
2828 ; RV32I-NEXT: .LBB1_1:
2929 ; RV32I-NEXT: mv a0, zero
3030 ; RV32I-NEXT: lw ra, 12(sp)
77 define i32 @add(i32 %a, i32 %b) nounwind {
88 ; CHECK-LABEL: add:
99 ; CHECK: add a0, a1, a0
10 ; CHECK-NEXT: jalr zero, ra, 0
10 ; CHECK-NEXT: jalr zero, 0(ra)
1111 tail call void asm sideeffect ".option rvc", ""()
1212 %add = add nsw i32 %b, %a
1313 ret i32 %add
167167 # CHECK-ALIAS: ret
168168 # CHECK-INST: c.jr ra
169169 # CHECK: # encoding: [0x82,0x80]
170 jalr zero, ra, 0
170 jalr zero, 0(ra)
171171
172172 # CHECK-BYTES: 92 80
173173 # CHECK-ALIAS: add ra, zero, tp
191191 # CHECK-ALIAS: jalr s0
192192 # CHECK-INST: c.jalr s0
193193 # CHECK: # encoding: [0x02,0x94]
194 jalr ra, s0, 0
194 jalr ra, 0(s0)
195195
196196 # CHECK-BYTES: 3e 94
197197 # CHECK-ALIAS: add s0, s0, a5
6767 call func
6868 # CHECK-FIXUP: fixup A - offset: 0, value: func, kind: fixup_riscv_call
6969 # CHECK-INSTR: auipc ra, 0
70 # CHECK-INSTR: jalr ra, ra, -100
70 # CHECK-INSTR: jalr ra, -100(ra)
7171
7272 .fill 10000
7373 call func
7474 # CHECK-FIXUP: fixup A - offset: 0, value: func, kind: fixup_riscv_call
7575 # CHECK-INSTR: auipc ra, 1048574
76 # CHECK-INSTR: jalr ra, ra, -1916
76 # CHECK-INSTR: jalr ra, -1916(ra)
7777
7878 .fill 20888
7979 call func
8080 # CHECK-FIXUP: fixup A - offset: 0, value: func, kind: fixup_riscv_call
8181 # CHECK-INSTR: auipc ra, 1048568
82 # CHECK-INSTR: jalr ra, ra, 1764
82 # CHECK-INSTR: jalr ra, 1764(ra)
1313
1414 # CHECK-ASM-AND-OBJ: jal sp, 4
1515 jal x2, 4
16 # CHECK-ASM-AND-OBJ: jalr gp, gp, 4
16 # CHECK-ASM-AND-OBJ: jalr gp, 4(gp)
1717 jalr x3, x3, 4
1818
1919 # CHECK-ASM-AND-OBJ: beq tp, t0, 8
9393 # CHECK-OBJ: jal zero, 0
9494 jal zero, .
9595
96 # CHECK-ASM-AND-OBJ: jalr a0, a1, -2048
96 # CHECK-ASM-AND-OBJ: jalr a0, -2048(a1)
9797 # CHECK-ASM: encoding: [0x67,0x85,0x05,0x80]
98 jalr a0, a1, -2048
99 # CHECK-ASM-AND-OBJ: jalr a0, a1, -2048
98 jalr a0, -2048(a1)
99 # CHECK-ASM-AND-OBJ: jalr a0, -2048(a1)
100100 # CHECK-ASM: encoding: [0x67,0x85,0x05,0x80]
101 jalr a0, a1, ~2047
102 # CHECK-ASM-AND-OBJ: jalr a0, a1, 0
103 # CHECK-ASM: encoding: [0x67,0x85,0x05,0x00]
104 jalr a0, a1, !1
105 # CHECK-ASM-AND-OBJ: jalr a0, a1, -2048
106 # CHECK-ASM: encoding: [0x67,0x85,0x05,0x80]
107 jalr a0, a1, %lo(2048)
108 # CHECK-ASM-AND-OBJ: jalr t2, t1, 2047
101 jalr a0, %lo(2048)(a1)
102 # CHECK-ASM-AND-OBJ: jalr t2, 2047(t1)
109103 # CHECK-ASM: encoding: [0xe7,0x03,0xf3,0x7f]
110 jalr t2, t1, 2047
111 # CHECK-ASM-AND-OBJ: jalr sp, zero, 256
104 jalr t2, 2047(t1)
105 # CHECK-ASM-AND-OBJ: jalr sp, 256(zero)
112106 # CHECK-ASM: encoding: [0x67,0x01,0x00,0x10]
113107 jalr sp, zero, 256
114 # CHECK-ASM-AND-OBJ: jalr a1, a2, 30
108 # CHECK-ASM-AND-OBJ: jalr a1, 30(a2)
115109 # CHECK-ASM: encoding: [0xe7,0x05,0xe6,0x01]
116 jalr a1, a2, CONST
110 jalr a1, CONST(a2)
117111
118112 # CHECK-ASM-AND-OBJ: beq s1, s1, 102
119113 # CHECK-ASM: encoding: [0x63,0x83,0x94,0x06]
138138 # CHECK-OBJ: jal 0
139139 # CHECK-OBJ: R_RISCV_JAL a0
140140 jal a0
141 # CHECK-S-OBJ-NOALIAS: jalr zero, s4, 0
141 # CHECK-S-OBJ-NOALIAS: jalr zero, 0(s4)
142142 # CHECK-S-OBJ: jr s4
143143 jr x20
144 # CHECK-S-OBJ-NOALIAS: jalr ra, s5, 0
145 # CHECK-S-OBJ: jalr s5
146 jalr x21
147 # CHECK-S-OBJ-NOALIAS: jalr zero, ra, 0
144 # CHECK-S-OBJ-NOALIAS: jalr zero, 6(s5)
145 # CHECK-S-OBJ: jr 6(s5)
146 jr 6(x21)
147 # CHECK-S-OBJ-NOALIAS: jalr zero, 7(s6)
148 # CHECK-S-OBJ: jr 7(s6)
149 jr x22, 7
150 # CHECK-S-OBJ-NOALIAS: jalr ra, 0(s4)
151 # CHECK-S-OBJ: jalr s4
152 jalr x20
153 # CHECK-S-OBJ-NOALIAS: jalr ra, 8(s5)
154 # CHECK-S-OBJ: jalr 8(s5)
155 jalr 8(x21)
156 # CHECK-S-OBJ-NOALIAS: jalr s6, 0(s7)
157 # CHECK-S-OBJ: jalr s6, s7
158 jalr x22, x23
159 # CHECK-S-OBJ-NOALIAS: jalr ra, 9(s8)
160 # CHECK-S-OBJ: jalr 9(s8)
161 jalr x24, 9
162 # CHECK-S-OBJ-NOALIAS: jalr s9, 11(s10)
163 # CHECK-S-OBJ: jalr s9, 11(s10)
164 jalr x25, x26, 11
165 # CHECK-S-OBJ-NOALIAS: jalr zero, 0(ra)
148166 # CHECK-S-OBJ: ret
149167 ret
150168 # TODO call