1 //===-- RISCVInstrInfoA.td - RISC-V 'A' instructions -------*- tablegen -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file describes the RISC-V instructions from the standard 'A', Atomic
10 // Instructions extension.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // Instruction class templates
16 //===----------------------------------------------------------------------===//
18 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
19 class LR_r<bit aq, bit rl, bits<3> funct3, string opcodestr>
20 : RVInstRAtomic<0b00010, aq, rl, funct3, OPC_AMO,
21 (outs GPR:$rd), (ins GPR:$rs1),
22 opcodestr, "$rd, (${rs1})"> {
26 multiclass LR_r_aq_rl<bits<3> funct3, string opcodestr> {
27 def "" : LR_r<0, 0, funct3, opcodestr>;
28 def _AQ : LR_r<1, 0, funct3, opcodestr # ".aq">;
29 def _RL : LR_r<0, 1, funct3, opcodestr # ".rl">;
30 def _AQ_RL : LR_r<1, 1, funct3, opcodestr # ".aqrl">;
33 let hasSideEffects = 0, mayLoad = 1, mayStore = 1 in
34 class AMO_rr<bits<5> funct5, bit aq, bit rl, bits<3> funct3, string opcodestr>
35 : RVInstRAtomic<funct5, aq, rl, funct3, OPC_AMO,
36 (outs GPR:$rd), (ins GPR:$rs1, GPR:$rs2),
37 opcodestr, "$rd, $rs2, (${rs1})">;
39 multiclass AMO_rr_aq_rl<bits<5> funct5, bits<3> funct3, string opcodestr> {
40 def "" : AMO_rr<funct5, 0, 0, funct3, opcodestr>;
41 def _AQ : AMO_rr<funct5, 1, 0, funct3, opcodestr # ".aq">;
42 def _RL : AMO_rr<funct5, 0, 1, funct3, opcodestr # ".rl">;
43 def _AQ_RL : AMO_rr<funct5, 1, 1, funct3, opcodestr # ".aqrl">;
46 multiclass AtomicStPat<PatFrag StoreOp, RVInst Inst, RegisterClass StTy> {
47 def : Pat<(StoreOp GPR:$rs1, StTy:$rs2), (Inst StTy:$rs2, GPR:$rs1, 0)>;
48 def : Pat<(StoreOp AddrFI:$rs1, StTy:$rs2), (Inst StTy:$rs2, AddrFI:$rs1, 0)>;
49 def : Pat<(StoreOp (add GPR:$rs1, simm12:$imm12), StTy:$rs2),
50 (Inst StTy:$rs2, GPR:$rs1, simm12:$imm12)>;
51 def : Pat<(StoreOp (add AddrFI:$rs1, simm12:$imm12), StTy:$rs2),
52 (Inst StTy:$rs2, AddrFI:$rs1, simm12:$imm12)>;
53 def : Pat<(StoreOp (IsOrAdd AddrFI:$rs1, simm12:$imm12), StTy:$rs2),
54 (Inst StTy:$rs2, AddrFI:$rs1, simm12:$imm12)>;
57 //===----------------------------------------------------------------------===//
59 //===----------------------------------------------------------------------===//
61 let Predicates = [HasStdExtA] in {
62 defm LR_W : LR_r_aq_rl<0b010, "lr.w">;
63 defm SC_W : AMO_rr_aq_rl<0b00011, 0b010, "sc.w">;
64 defm AMOSWAP_W : AMO_rr_aq_rl<0b00001, 0b010, "amoswap.w">;
65 defm AMOADD_W : AMO_rr_aq_rl<0b00000, 0b010, "amoadd.w">;
66 defm AMOXOR_W : AMO_rr_aq_rl<0b00100, 0b010, "amoxor.w">;
67 defm AMOAND_W : AMO_rr_aq_rl<0b01100, 0b010, "amoand.w">;
68 defm AMOOR_W : AMO_rr_aq_rl<0b01000, 0b010, "amoor.w">;
69 defm AMOMIN_W : AMO_rr_aq_rl<0b10000, 0b010, "amomin.w">;
70 defm AMOMAX_W : AMO_rr_aq_rl<0b10100, 0b010, "amomax.w">;
71 defm AMOMINU_W : AMO_rr_aq_rl<0b11000, 0b010, "amominu.w">;
72 defm AMOMAXU_W : AMO_rr_aq_rl<0b11100, 0b010, "amomaxu.w">;
73 } // Predicates = [HasStdExtA]
75 let Predicates = [HasStdExtA, IsRV64] in {
76 defm LR_D : LR_r_aq_rl<0b011, "lr.d">;
77 defm SC_D : AMO_rr_aq_rl<0b00011, 0b011, "sc.d">;
78 defm AMOSWAP_D : AMO_rr_aq_rl<0b00001, 0b011, "amoswap.d">;
79 defm AMOADD_D : AMO_rr_aq_rl<0b00000, 0b011, "amoadd.d">;
80 defm AMOXOR_D : AMO_rr_aq_rl<0b00100, 0b011, "amoxor.d">;
81 defm AMOAND_D : AMO_rr_aq_rl<0b01100, 0b011, "amoand.d">;
82 defm AMOOR_D : AMO_rr_aq_rl<0b01000, 0b011, "amoor.d">;
83 defm AMOMIN_D : AMO_rr_aq_rl<0b10000, 0b011, "amomin.d">;
84 defm AMOMAX_D : AMO_rr_aq_rl<0b10100, 0b011, "amomax.d">;
85 defm AMOMINU_D : AMO_rr_aq_rl<0b11000, 0b011, "amominu.d">;
86 defm AMOMAXU_D : AMO_rr_aq_rl<0b11100, 0b011, "amomaxu.d">;
87 } // Predicates = [HasStedExtA, IsRV64]
89 //===----------------------------------------------------------------------===//
90 // Pseudo-instructions and codegen patterns
91 //===----------------------------------------------------------------------===//
93 let Predicates = [HasStdExtA] in {
95 /// Atomic loads and stores
97 // Fences will be inserted for atomic load/stores according to the logic in
98 // RISCVTargetLowering::{emitLeadingFence,emitTrailingFence}.
100 defm : LdPat<atomic_load_8, LB>;
101 defm : LdPat<atomic_load_16, LH>;
102 defm : LdPat<atomic_load_32, LW>;
104 defm : AtomicStPat<atomic_store_8, SB, GPR>;
105 defm : AtomicStPat<atomic_store_16, SH, GPR>;
106 defm : AtomicStPat<atomic_store_32, SW, GPR>;
110 multiclass AMOPat<string AtomicOp, string BaseInst> {
111 def : PatGprGpr<!cast<PatFrag>(AtomicOp#"_monotonic"),
112 !cast<RVInst>(BaseInst)>;
113 def : PatGprGpr<!cast<PatFrag>(AtomicOp#"_acquire"),
114 !cast<RVInst>(BaseInst#"_AQ")>;
115 def : PatGprGpr<!cast<PatFrag>(AtomicOp#"_release"),
116 !cast<RVInst>(BaseInst#"_RL")>;
117 def : PatGprGpr<!cast<PatFrag>(AtomicOp#"_acq_rel"),
118 !cast<RVInst>(BaseInst#"_AQ_RL")>;
119 def : PatGprGpr<!cast<PatFrag>(AtomicOp#"_seq_cst"),
120 !cast<RVInst>(BaseInst#"_AQ_RL")>;
123 defm : AMOPat<"atomic_swap_32", "AMOSWAP_W">;
124 defm : AMOPat<"atomic_load_add_32", "AMOADD_W">;
125 defm : AMOPat<"atomic_load_and_32", "AMOAND_W">;
126 defm : AMOPat<"atomic_load_or_32", "AMOOR_W">;
127 defm : AMOPat<"atomic_load_xor_32", "AMOXOR_W">;
128 defm : AMOPat<"atomic_load_max_32", "AMOMAX_W">;
129 defm : AMOPat<"atomic_load_min_32", "AMOMIN_W">;
130 defm : AMOPat<"atomic_load_umax_32", "AMOMAXU_W">;
131 defm : AMOPat<"atomic_load_umin_32", "AMOMINU_W">;
133 def : Pat<(atomic_load_sub_32_monotonic GPR:$addr, GPR:$incr),
134 (AMOADD_W GPR:$addr, (SUB X0, GPR:$incr))>;
135 def : Pat<(atomic_load_sub_32_acquire GPR:$addr, GPR:$incr),
136 (AMOADD_W_AQ GPR:$addr, (SUB X0, GPR:$incr))>;
137 def : Pat<(atomic_load_sub_32_release GPR:$addr, GPR:$incr),
138 (AMOADD_W_RL GPR:$addr, (SUB X0, GPR:$incr))>;
139 def : Pat<(atomic_load_sub_32_acq_rel GPR:$addr, GPR:$incr),
140 (AMOADD_W_AQ_RL GPR:$addr, (SUB X0, GPR:$incr))>;
141 def : Pat<(atomic_load_sub_32_seq_cst GPR:$addr, GPR:$incr),
142 (AMOADD_W_AQ_RL GPR:$addr, (SUB X0, GPR:$incr))>;
146 class PseudoAMO : Pseudo<(outs GPR:$res, GPR:$scratch),
147 (ins GPR:$addr, GPR:$incr, ixlenimm:$ordering), []> {
148 let Constraints = "@earlyclobber $res,@earlyclobber $scratch";
151 let hasSideEffects = 0;
154 def PseudoAtomicLoadNand32 : PseudoAMO;
155 // Ordering constants must be kept in sync with the AtomicOrdering enum in
157 def : Pat<(atomic_load_nand_32_monotonic GPR:$addr, GPR:$incr),
158 (PseudoAtomicLoadNand32 GPR:$addr, GPR:$incr, 2)>;
159 def : Pat<(atomic_load_nand_32_acquire GPR:$addr, GPR:$incr),
160 (PseudoAtomicLoadNand32 GPR:$addr, GPR:$incr, 4)>;
161 def : Pat<(atomic_load_nand_32_release GPR:$addr, GPR:$incr),
162 (PseudoAtomicLoadNand32 GPR:$addr, GPR:$incr, 5)>;
163 def : Pat<(atomic_load_nand_32_acq_rel GPR:$addr, GPR:$incr),
164 (PseudoAtomicLoadNand32 GPR:$addr, GPR:$incr, 6)>;
165 def : Pat<(atomic_load_nand_32_seq_cst GPR:$addr, GPR:$incr),
166 (PseudoAtomicLoadNand32 GPR:$addr, GPR:$incr, 7)>;
168 class PseudoMaskedAMO
169 : Pseudo<(outs GPR:$res, GPR:$scratch),
170 (ins GPR:$addr, GPR:$incr, GPR:$mask, ixlenimm:$ordering), []> {
171 let Constraints = "@earlyclobber $res,@earlyclobber $scratch";
174 let hasSideEffects = 0;
177 class PseudoMaskedAMOMinMax
178 : Pseudo<(outs GPR:$res, GPR:$scratch1, GPR:$scratch2),
179 (ins GPR:$addr, GPR:$incr, GPR:$mask, ixlenimm:$sextshamt,
180 ixlenimm:$ordering), []> {
181 let Constraints = "@earlyclobber $res,@earlyclobber $scratch1,"
182 "@earlyclobber $scratch2";
185 let hasSideEffects = 0;
188 class PseudoMaskedAMOUMinUMax
189 : Pseudo<(outs GPR:$res, GPR:$scratch1, GPR:$scratch2),
190 (ins GPR:$addr, GPR:$incr, GPR:$mask, ixlenimm:$ordering), []> {
191 let Constraints = "@earlyclobber $res,@earlyclobber $scratch1,"
192 "@earlyclobber $scratch2";
195 let hasSideEffects = 0;
198 class PseudoMaskedAMOPat<Intrinsic intrin, Pseudo AMOInst>
199 : Pat<(intrin GPR:$addr, GPR:$incr, GPR:$mask, imm:$ordering),
200 (AMOInst GPR:$addr, GPR:$incr, GPR:$mask, imm:$ordering)>;
202 class PseudoMaskedAMOMinMaxPat<Intrinsic intrin, Pseudo AMOInst>
203 : Pat<(intrin GPR:$addr, GPR:$incr, GPR:$mask, GPR:$shiftamt,
205 (AMOInst GPR:$addr, GPR:$incr, GPR:$mask, GPR:$shiftamt,
208 def PseudoMaskedAtomicSwap32 : PseudoMaskedAMO;
209 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_xchg_i32,
210 PseudoMaskedAtomicSwap32>;
211 def PseudoMaskedAtomicLoadAdd32 : PseudoMaskedAMO;
212 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_add_i32,
213 PseudoMaskedAtomicLoadAdd32>;
214 def PseudoMaskedAtomicLoadSub32 : PseudoMaskedAMO;
215 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_sub_i32,
216 PseudoMaskedAtomicLoadSub32>;
217 def PseudoMaskedAtomicLoadNand32 : PseudoMaskedAMO;
218 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_nand_i32,
219 PseudoMaskedAtomicLoadNand32>;
220 def PseudoMaskedAtomicLoadMax32 : PseudoMaskedAMOMinMax;
221 def : PseudoMaskedAMOMinMaxPat<int_riscv_masked_atomicrmw_max_i32,
222 PseudoMaskedAtomicLoadMax32>;
223 def PseudoMaskedAtomicLoadMin32 : PseudoMaskedAMOMinMax;
224 def : PseudoMaskedAMOMinMaxPat<int_riscv_masked_atomicrmw_min_i32,
225 PseudoMaskedAtomicLoadMin32>;
226 def PseudoMaskedAtomicLoadUMax32 : PseudoMaskedAMOUMinUMax;
227 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_umax_i32,
228 PseudoMaskedAtomicLoadUMax32>;
229 def PseudoMaskedAtomicLoadUMin32 : PseudoMaskedAMOUMinUMax;
230 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_umin_i32,
231 PseudoMaskedAtomicLoadUMin32>;
233 /// Compare and exchange
236 : Pseudo<(outs GPR:$res, GPR:$scratch),
237 (ins GPR:$addr, GPR:$cmpval, GPR:$newval, ixlenimm:$ordering), []> {
238 let Constraints = "@earlyclobber $res,@earlyclobber $scratch";
241 let hasSideEffects = 0;
244 // Ordering constants must be kept in sync with the AtomicOrdering enum in
246 multiclass PseudoCmpXchgPat<string Op, Pseudo CmpXchgInst> {
247 def : Pat<(!cast<PatFrag>(Op#"_monotonic") GPR:$addr, GPR:$cmp, GPR:$new),
248 (CmpXchgInst GPR:$addr, GPR:$cmp, GPR:$new, 2)>;
249 def : Pat<(!cast<PatFrag>(Op#"_acquire") GPR:$addr, GPR:$cmp, GPR:$new),
250 (CmpXchgInst GPR:$addr, GPR:$cmp, GPR:$new, 4)>;
251 def : Pat<(!cast<PatFrag>(Op#"_release") GPR:$addr, GPR:$cmp, GPR:$new),
252 (CmpXchgInst GPR:$addr, GPR:$cmp, GPR:$new, 5)>;
253 def : Pat<(!cast<PatFrag>(Op#"_acq_rel") GPR:$addr, GPR:$cmp, GPR:$new),
254 (CmpXchgInst GPR:$addr, GPR:$cmp, GPR:$new, 6)>;
255 def : Pat<(!cast<PatFrag>(Op#"_seq_cst") GPR:$addr, GPR:$cmp, GPR:$new),
256 (CmpXchgInst GPR:$addr, GPR:$cmp, GPR:$new, 7)>;
259 def PseudoCmpXchg32 : PseudoCmpXchg;
260 defm : PseudoCmpXchgPat<"atomic_cmp_swap_32", PseudoCmpXchg32>;
262 def PseudoMaskedCmpXchg32
263 : Pseudo<(outs GPR:$res, GPR:$scratch),
264 (ins GPR:$addr, GPR:$cmpval, GPR:$newval, GPR:$mask,
265 ixlenimm:$ordering), []> {
266 let Constraints = "@earlyclobber $res,@earlyclobber $scratch";
269 let hasSideEffects = 0;
272 def : Pat<(int_riscv_masked_cmpxchg_i32
273 GPR:$addr, GPR:$cmpval, GPR:$newval, GPR:$mask, imm:$ordering),
274 (PseudoMaskedCmpXchg32
275 GPR:$addr, GPR:$cmpval, GPR:$newval, GPR:$mask, imm:$ordering)>;
277 } // Predicates = [HasStdExtA]
279 let Predicates = [HasStdExtA, IsRV64] in {
281 /// 64-bit atomic loads and stores
283 // Fences will be inserted for atomic load/stores according to the logic in
284 // RISCVTargetLowering::{emitLeadingFence,emitTrailingFence}.
285 defm : LdPat<atomic_load_64, LD>;
286 defm : AtomicStPat<atomic_store_64, SD, GPR>;
288 defm : AMOPat<"atomic_swap_64", "AMOSWAP_D">;
289 defm : AMOPat<"atomic_load_add_64", "AMOADD_D">;
290 defm : AMOPat<"atomic_load_and_64", "AMOAND_D">;
291 defm : AMOPat<"atomic_load_or_64", "AMOOR_D">;
292 defm : AMOPat<"atomic_load_xor_64", "AMOXOR_D">;
293 defm : AMOPat<"atomic_load_max_64", "AMOMAX_D">;
294 defm : AMOPat<"atomic_load_min_64", "AMOMIN_D">;
295 defm : AMOPat<"atomic_load_umax_64", "AMOMAXU_D">;
296 defm : AMOPat<"atomic_load_umin_64", "AMOMINU_D">;
300 def : Pat<(atomic_load_sub_64_monotonic GPR:$addr, GPR:$incr),
301 (AMOADD_D GPR:$addr, (SUB X0, GPR:$incr))>;
302 def : Pat<(atomic_load_sub_64_acquire GPR:$addr, GPR:$incr),
303 (AMOADD_D_AQ GPR:$addr, (SUB X0, GPR:$incr))>;
304 def : Pat<(atomic_load_sub_64_release GPR:$addr, GPR:$incr),
305 (AMOADD_D_RL GPR:$addr, (SUB X0, GPR:$incr))>;
306 def : Pat<(atomic_load_sub_64_acq_rel GPR:$addr, GPR:$incr),
307 (AMOADD_D_AQ_RL GPR:$addr, (SUB X0, GPR:$incr))>;
308 def : Pat<(atomic_load_sub_64_seq_cst GPR:$addr, GPR:$incr),
309 (AMOADD_D_AQ_RL GPR:$addr, (SUB X0, GPR:$incr))>;
311 /// 64-bit pseudo AMOs
313 def PseudoAtomicLoadNand64 : PseudoAMO;
314 // Ordering constants must be kept in sync with the AtomicOrdering enum in
316 def : Pat<(atomic_load_nand_64_monotonic GPR:$addr, GPR:$incr),
317 (PseudoAtomicLoadNand64 GPR:$addr, GPR:$incr, 2)>;
318 def : Pat<(atomic_load_nand_64_acquire GPR:$addr, GPR:$incr),
319 (PseudoAtomicLoadNand64 GPR:$addr, GPR:$incr, 4)>;
320 def : Pat<(atomic_load_nand_64_release GPR:$addr, GPR:$incr),
321 (PseudoAtomicLoadNand64 GPR:$addr, GPR:$incr, 5)>;
322 def : Pat<(atomic_load_nand_64_acq_rel GPR:$addr, GPR:$incr),
323 (PseudoAtomicLoadNand64 GPR:$addr, GPR:$incr, 6)>;
324 def : Pat<(atomic_load_nand_64_seq_cst GPR:$addr, GPR:$incr),
325 (PseudoAtomicLoadNand64 GPR:$addr, GPR:$incr, 7)>;
327 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_xchg_i64,
328 PseudoMaskedAtomicSwap32>;
329 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_add_i64,
330 PseudoMaskedAtomicLoadAdd32>;
331 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_sub_i64,
332 PseudoMaskedAtomicLoadSub32>;
333 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_nand_i64,
334 PseudoMaskedAtomicLoadNand32>;
335 def : PseudoMaskedAMOMinMaxPat<int_riscv_masked_atomicrmw_max_i64,
336 PseudoMaskedAtomicLoadMax32>;
337 def : PseudoMaskedAMOMinMaxPat<int_riscv_masked_atomicrmw_min_i64,
338 PseudoMaskedAtomicLoadMin32>;
339 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_umax_i64,
340 PseudoMaskedAtomicLoadUMax32>;
341 def : PseudoMaskedAMOPat<int_riscv_masked_atomicrmw_umin_i64,
342 PseudoMaskedAtomicLoadUMin32>;
344 /// 64-bit compare and exchange
346 def PseudoCmpXchg64 : PseudoCmpXchg;
347 defm : PseudoCmpXchgPat<"atomic_cmp_swap_64", PseudoCmpXchg64>;
349 def : Pat<(int_riscv_masked_cmpxchg_i64
350 GPR:$addr, GPR:$cmpval, GPR:$newval, GPR:$mask, imm:$ordering),
351 (PseudoMaskedCmpXchg32
352 GPR:$addr, GPR:$cmpval, GPR:$newval, GPR:$mask, imm:$ordering)>;
353 } // Predicates = [HasStdExtA, IsRV64]