1 ;; Iterators for the machine description for RISC-V
2 ;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
4 ;; This file is part of GCC.
6 ;; GCC is free software; you can redistribute it and/or modify it
7 ;; under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; GCC is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GCC; see the file COPYING3. If not see
18 ;; <http://www.gnu.org/licenses/>.
21 ;; -------------------------------------------------------------------
23 ;; -------------------------------------------------------------------
25 ;; This mode iterator allows 32-bit and 64-bit GPR patterns to be generated
26 ;; from the same template.
27 (define_mode_iterator GPR [SI (DI "TARGET_64BIT")])
29 ;; A copy of GPR that can be used when a pattern has two independent
31 (define_mode_iterator GPR2 [SI (DI "TARGET_64BIT")])
33 ;; This mode iterator allows :P to be used for patterns that operate on
34 ;; pointer-sized quantities. Exactly one of the two alternatives will match.
35 (define_mode_iterator P [(SI "Pmode == SImode") (DI "Pmode == DImode")])
37 ;; Likewise, but for XLEN-sized quantities.
38 (define_mode_iterator X [(SI "!TARGET_64BIT") (DI "TARGET_64BIT")])
40 ;; Likewise, but for XLEN/2 -sized quantities.
41 (define_mode_iterator HX [(HI "!TARGET_64BIT") (SI "TARGET_64BIT")])
43 ;; Branches operate on XLEN-sized quantities, but for RV64 we accept
44 ;; QImode values so we can force zero-extension.
45 (define_mode_iterator BR [(QI "TARGET_64BIT") SI (DI "TARGET_64BIT")])
47 ;; 32-bit moves for which we provide move patterns.
48 (define_mode_iterator MOVE32 [SI])
50 ;; 64-bit modes for which we provide move patterns.
51 (define_mode_iterator MOVE64 [DI DF])
53 ;; Iterator for sub-32-bit integer modes.
54 (define_mode_iterator SHORT [QI HI])
56 ;; Iterator for HImode constant generation.
57 (define_mode_iterator HISI [HI SI])
59 ;; Iterator for QImode extension patterns.
60 (define_mode_iterator SUPERQI [HI SI (DI "TARGET_64BIT")])
62 ;; Iterator for hardware integer modes narrower than XLEN.
63 (define_mode_iterator SUBX [QI HI (SI "TARGET_64BIT")])
65 ;; Iterator for hardware integer modes narrower than XLEN, same as SUBX.
66 (define_mode_iterator SUBX1 [QI HI (SI "TARGET_64BIT")])
68 ;; Iterator for hardware-supported integer modes.
69 (define_mode_iterator ANYI [QI HI SI (DI "TARGET_64BIT")])
71 ;; Iterator for hardware integer modes narrower than XLEN, same as ANYI.
72 (define_mode_iterator ANYI1 [QI HI SI (DI "TARGET_64BIT")])
74 (define_mode_iterator ANYI_DOUBLE_TRUNC [HI SI (DI "TARGET_64BIT")])
76 (define_mode_iterator ANYI_QUAD_TRUNC [SI (DI "TARGET_64BIT")])
78 (define_mode_iterator ANYI_OCT_TRUNC [(DI "TARGET_64BIT")])
80 (define_mode_attr ANYI_DOUBLE_TRUNCATED [
81 (HI "QI") (SI "HI") (DI "SI")
84 (define_mode_attr ANYI_QUAD_TRUNCATED [
88 (define_mode_attr ANYI_OCT_TRUNCATED [
92 (define_mode_attr anyi_double_truncated [
93 (HI "qi") (SI "hi") (DI "si")
96 (define_mode_attr anyi_quad_truncated [
100 (define_mode_attr anyi_oct_truncated [
104 ;; Iterator for hardware-supported floating-point modes.
105 (define_mode_iterator ANYF [(SF "TARGET_HARD_FLOAT || TARGET_ZFINX")
106 (DF "TARGET_DOUBLE_FLOAT || TARGET_ZDINX")
107 (HF "TARGET_ZFH || TARGET_ZHINX")])
109 ;; Iterator for hardware-supported load/store floating-point modes.
110 (define_mode_iterator ANYLSF [(SF "TARGET_HARD_FLOAT || TARGET_ZFINX")
111 (DF "TARGET_DOUBLE_FLOAT || TARGET_ZDINX")
112 (HF "TARGET_ZFHMIN || TARGET_ZHINXMIN")])
114 ;; Iterator for floating-point modes that can be loaded into X registers.
115 (define_mode_iterator SOFTF [SF (DF "TARGET_64BIT") (HF "TARGET_ZFHMIN")])
117 ;; Iterator for floating-point modes of BF16.
118 (define_mode_iterator HFBF [HF BF])
120 ;; Conversion between floating-point modes and BF16.
121 ;; SF to BF16 have hardware instructions.
122 (define_mode_iterator FBF [HF DF TF])
124 ;; -------------------------------------------------------------------
126 ;; -------------------------------------------------------------------
128 ;; This attribute gives the length suffix for a sign- or zero-extension
130 (define_mode_attr size [(QI "b") (HI "h")])
132 ;; Mode attributes for loads.
133 (define_mode_attr load [(QI "lb") (HI "lh") (SI "lw") (DI "ld") (HF "flh") (SF "flw") (DF "fld")])
135 ;; Instruction names for integer loads that aren't explicitly sign or zero
136 ;; extended. See riscv_output_move and LOAD_EXTEND_OP.
137 (define_mode_attr default_load [(QI "lbu") (HI "lhu") (SI "lw") (DI "ld")])
139 ;; Mode attribute for FP loads into integer registers.
140 (define_mode_attr softload [(HF "lh") (SF "lw") (DF "ld")])
142 ;; Instruction names for stores.
143 (define_mode_attr store [(QI "sb") (HI "sh") (SI "sw") (DI "sd") (HF "fsh") (SF "fsw") (DF "fsd")])
145 ;; Instruction names for FP stores from integer registers.
146 (define_mode_attr softstore [(HF "sh") (SF "sw") (DF "sd")])
148 ;; This attribute gives the best constraint to use for registers of
150 (define_mode_attr reg [(SI "d") (DI "d") (CC "d")])
152 ;; This attribute gives the format suffix for floating-point operations.
153 (define_mode_attr fmt [(HF "h") (SF "s") (DF "d")])
155 ;; This attribute gives the integer suffix for floating-point conversions.
156 (define_mode_attr ifmt [(SI "w") (DI "l")])
158 ;; This attribute gives the format suffix for atomic memory operations.
159 (define_mode_attr amo [(SI "w") (DI "d")])
161 ;; This attribute gives the format suffix for byte and halfword atomic memory operations.
162 (define_mode_attr amobh [(QI "b") (HI "h")])
164 ;; This attribute gives the upper-case mode name for one unit of a
165 ;; floating-point mode.
166 (define_mode_attr UNITMODE [(HF "HF") (SF "SF") (DF "DF")])
168 ;; This attribute gives the integer mode that has half the size of
169 ;; the controlling mode.
170 (define_mode_attr HALFMODE [(DF "SI") (DI "SI") (TF "DI")])
172 ; bitmanip mode attribute
173 (define_mode_attr shiftm1 [(SI "const_si_mask_operand") (DI "const_di_mask_operand")])
174 (define_mode_attr shiftm1p [(SI "DsS") (DI "DsD")])
176 ; zcmp mode attribute
177 (define_mode_attr slot0_offset [(SI "-4") (DI "-8")])
178 (define_mode_attr slot1_offset [(SI "-8") (DI "-16")])
179 (define_mode_attr slot2_offset [(SI "-12") (DI "-24")])
180 (define_mode_attr slot3_offset [(SI "-16") (DI "-32")])
181 (define_mode_attr slot4_offset [(SI "-20") (DI "-40")])
182 (define_mode_attr slot5_offset [(SI "-24") (DI "-48")])
183 (define_mode_attr slot6_offset [(SI "-28") (DI "-56")])
184 (define_mode_attr slot7_offset [(SI "-32") (DI "-64")])
185 (define_mode_attr slot8_offset [(SI "-36") (DI "-72")])
186 (define_mode_attr slot9_offset [(SI "-40") (DI "-80")])
187 (define_mode_attr slot10_offset [(SI "-44") (DI "-88")])
188 (define_mode_attr slot11_offset [(SI "-48") (DI "-96")])
189 (define_mode_attr slot12_offset [(SI "-52") (DI "-104")])
191 ;; -------------------------------------------------------------------
193 ;; -------------------------------------------------------------------
195 ;; This code iterator allows signed and unsigned widening multiplications
196 ;; to use the same template.
197 (define_code_iterator any_extend [sign_extend zero_extend])
199 ;; These code iterators allow unsigned and signed extraction to be generated
200 ;; from the same template.
201 (define_code_iterator any_extract [sign_extract zero_extract])
202 (define_code_attr extract_sidi_shift [(sign_extract "sraiw")
203 (zero_extract "srliw")])
204 (define_code_attr extract_shift [(sign_extract "ashiftrt")
205 (zero_extract "lshiftrt")])
207 ;; This code iterator allows the two right shift instructions to be
208 ;; generated from the same template.
209 (define_code_iterator any_shiftrt [ashiftrt lshiftrt])
211 ;; This code iterator allows the three shift instructions to be generated
212 ;; from the same template.
213 (define_code_iterator any_shift [ashift ashiftrt lshiftrt])
215 ;; This code iterator allows the three bitwise instructions to be generated
216 ;; from the same template.
217 (define_code_iterator any_bitwise [and ior xor])
219 ;; This code iterator allows ior and xor instructions to be generated
220 ;; from the same template.
221 (define_code_iterator any_or [ior xor])
223 ;; This code iterator allows unsigned and signed division to be generated
224 ;; from the same template.
225 (define_code_iterator any_div [div udiv mod umod])
227 ;; This code iterator allows unsigned and signed modulus to be generated
228 ;; from the same template.
229 (define_code_iterator any_mod [mod umod])
231 ;; These code iterators allow unsigned and signed divmod to be generated
232 ;; from the same template.
233 (define_code_iterator only_div [div udiv])
234 (define_code_attr paired_mod [(div "mod") (udiv "umod")])
236 ;; These code iterators allow the signed and unsigned scc operations to use
237 ;; the same template.
238 (define_code_iterator any_gt [gt gtu])
239 (define_code_iterator any_ge [ge geu])
240 (define_code_iterator any_lt [lt ltu])
241 (define_code_iterator any_le [le leu])
242 (define_code_iterator any_eq [eq ne])
244 ;; Iterators for conditions we can emit a sCC against 0 or a reg directly
245 (define_code_iterator scc_0 [eq ne gt gtu])
247 ; atomics code iterator
248 (define_code_iterator any_atomic [plus ior xor and])
250 ; bitmanip code iterators
251 (define_code_iterator bitmanip_bitwise [and ior])
253 (define_code_iterator bitmanip_minmax [smin umin smax umax])
255 (define_code_iterator clz_ctz_pcnt [clz ctz popcount])
257 (define_code_iterator bitmanip_rotate [rotate rotatert])
259 ;; These code iterators allow the signed and unsigned fix operations to use
260 ;; the same template.
261 (define_code_iterator fix_ops [fix unsigned_fix])
263 (define_code_attr fix_uns [(fix "fix") (unsigned_fix "fixuns")])
266 ;; -------------------------------------------------------------------
268 ;; -------------------------------------------------------------------
270 ;; <u> expands to an empty string when doing a signed operation and
271 ;; "u" when doing an unsigned operation.
272 (define_code_attr u [(sign_extend "") (zero_extend "u")
277 (fix "") (unsigned_fix "u")
279 (float "") (unsigned_float "u")])
281 ;; <su> is like <u>, but the signed form expands to "s" rather than "".
282 (define_code_attr su [(sign_extend "s") (zero_extend "u")])
284 ;; <optab> expands to the name of the optab for a particular code.
285 (define_code_attr optab [(ashift "ashl")
316 (sign_extend "extend")
317 (zero_extend "zero_extend")
318 (sign_extract "extract")
319 (zero_extract "zero_extract")
321 (unsigned_fix "fixuns_trunc")])
323 (define_code_attr bit_optab [(ior "bset")
326 ;; <or_optab> code attributes
327 (define_code_attr or_optab [(ior "ior")
330 ;; <insn> expands to the name of the insn that implements a particular code.
331 (define_code_attr insn [(ashift "sll")
357 ; atomics code attribute
358 (define_code_attr atomic_optab
359 [(plus "add") (ior "or") (xor "xor") (and "and")])
361 ; bitmanip code attributes
362 ;; Unsigned variant of a min/max optab.
363 (define_code_attr uminmax_optab [(smin "umin")
367 (define_code_attr bitmanip_optab [(smin "smin")
373 (popcount "popcount")
376 (define_code_attr bitmanip_insn [(smin "min")
386 ;; -------------------------------------------------------------------
388 ;; -------------------------------------------------------------------
390 ;; Iterator and attributes for quiet comparisons.
391 (define_int_iterator QUIET_COMPARISON [UNSPEC_FLT_QUIET UNSPEC_FLE_QUIET])
392 (define_int_attr quiet_pattern [(UNSPEC_FLT_QUIET "lt") (UNSPEC_FLE_QUIET "le")])
393 (define_int_attr QUIET_PATTERN [(UNSPEC_FLT_QUIET "LT") (UNSPEC_FLE_QUIET "LE")])
395 (define_int_iterator ROUND [UNSPEC_ROUND UNSPEC_FLOOR UNSPEC_CEIL UNSPEC_BTRUNC UNSPEC_ROUNDEVEN UNSPEC_NEARBYINT])
396 (define_int_attr round_pattern [(UNSPEC_ROUND "round") (UNSPEC_FLOOR "floor") (UNSPEC_CEIL "ceil")
397 (UNSPEC_BTRUNC "btrunc") (UNSPEC_ROUNDEVEN "roundeven") (UNSPEC_NEARBYINT "nearbyint")])
398 (define_int_attr round_rm [(UNSPEC_ROUND "rmm") (UNSPEC_FLOOR "rdn") (UNSPEC_CEIL "rup")
399 (UNSPEC_BTRUNC "rtz") (UNSPEC_ROUNDEVEN "rne") (UNSPEC_NEARBYINT "dyn")])