1 ;; Machine description for the RISC-V Zicond extension and functionally-
2 ;; equivalent XVentanaCondOps vendor extension
3 ;; Copyright (C) 2022-2025 Free Software Foundation, Inc.
5 ;; This file is part of GCC.
7 ;; GCC is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; GCC is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GCC; see the file COPYING3. If not see
19 ;; <http://www.gnu.org/licenses/>.
21 (define_code_iterator eq_or_ne [eq ne])
22 (define_code_attr eqz [(eq "nez") (ne "eqz")])
23 (define_code_attr nez [(eq "eqz") (ne "nez")])
24 (define_code_attr eqz_ventana [(eq "n") (ne "")])
25 (define_code_attr nez_ventana [(eq "") (ne "n")])
27 ;; Zicond / XVentanaCondOps
28 (define_insn "*czero.<eqz>.<GPR:mode><X:mode>"
29 [(set (match_operand:GPR 0 "register_operand" "=r")
30 (if_then_else:GPR (eq_or_ne (match_operand:X 1 "register_operand" "r")
32 (match_operand:GPR 2 "register_operand" "r")
37 return "czero.<eqz>\t%0,%2,%1";
38 else if (TARGET_XVENTANACONDOPS && TARGET_64BIT)
39 return "vt.maskc<eqz_ventana>\t%0,%2,%1";
43 [(set_attr "type" "zicond")])
45 (define_insn "*czero.<nez>.<GPR:mode><X:mode>"
46 [(set (match_operand:GPR 0 "register_operand" "=r")
47 (if_then_else:GPR (eq_or_ne (match_operand:X 1 "register_operand" "r")
50 (match_operand:GPR 2 "register_operand" "r")))]
54 return "czero.<nez>\t%0,%2,%1";
55 else if (TARGET_XVENTANACONDOPS && TARGET_64BIT)
56 return "vt.maskc<nez_ventana>\t%0,%2,%1";
60 [(set_attr "type" "zicond")])
62 ;; Special optimization under eq/ne in primitive semantics
63 (define_insn "*czero.eqz.<GPR:mode><X:mode>.opt1"
64 [(set (match_operand:GPR 0 "register_operand" "=r")
65 (if_then_else:GPR (eq (match_operand:X 1 "register_operand" "r")
67 (match_operand:GPR 2 "register_operand" "1")
68 (match_operand:GPR 3 "register_operand" "r")))]
69 "TARGET_ZICOND_LIKE && rtx_equal_p (operands[1], operands[2])"
72 return "czero.eqz\t%0,%3,%1";
73 else if (TARGET_XVENTANACONDOPS && TARGET_64BIT)
74 return "vt.maskc\t%0,%3,%1";
78 [(set_attr "type" "zicond")])
80 (define_insn "*czero.nez.<GPR:mode><X:mode>.opt2"
81 [(set (match_operand:GPR 0 "register_operand" "=r")
82 (if_then_else:GPR (ne (match_operand:X 1 "register_operand" "r")
84 (match_operand:GPR 2 "register_operand" "r")
85 (match_operand:GPR 3 "register_operand" "1")))]
86 "TARGET_ZICOND && rtx_equal_p (operands[1], operands[3])"
89 return "czero.eqz\t%0,%2,%1";
90 else if (TARGET_XVENTANACONDOPS && TARGET_64BIT)
91 return "vt.maskc\t%0,%2,%1";
95 [(set_attr "type" "zicond")])
97 ;; Combine creates this form in some cases (particularly the coremark
100 [(set (match_operand:X 0 "register_operand")
101 (and:X (sign_extract:X (match_operand:X 1 "register_operand")
103 (match_operand 2 "immediate_operand"))
104 (match_operand:X 3 "register_operand")))
105 (clobber (match_operand:X 4 "register_operand"))]
106 "TARGET_ZICOND_LIKE && TARGET_ZBS"
107 [(set (match_dup 4) (zero_extract:X (match_dup 1) (const_int 1) (match_dup 2)))
108 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 4) (const_int 0))
113 [(set (match_operand:X 0 "register_operand")
114 (and:X (sign_extract:X (match_operand:X 1 "register_operand")
116 (match_operand 2 "immediate_operand"))
117 (match_operand:X 3 "register_operand")))
118 (clobber (match_operand:X 4 "register_operand"))]
119 "TARGET_ZICOND_LIKE && !TARGET_ZBS && (UINTVAL (operands[2]) < 11)"
120 [(set (match_dup 4) (and:X (match_dup 1) (match_dup 2)))
121 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 4) (const_int 0))
125 operands[2] = GEN_INT (1 << UINTVAL(operands[2]));
128 ;; In some cases gimple can give us a sequence with a logical and
129 ;; of two sCC insns. This can be implemented an sCC feeding a
132 [(set (match_operand:X 0 "register_operand")
133 (and:X (ne:X (match_operand:X 1 "register_operand") (const_int 0))
134 (scc_0:X (match_operand:X 2 "register_operand")
135 (match_operand:X 3 "reg_or_0_operand"))))
136 (clobber (match_operand:X 4 "register_operand"))]
137 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
138 [(set (match_dup 4) (scc_0:X (match_dup 2) (match_dup 3)))
139 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 1) (const_int 0))
143 ;; Similarly but GE/GEU which requires (const_int 1) as an operand.
145 [(set (match_operand:X 0 "register_operand")
146 (and:X (ne:X (match_operand:X 1 "register_operand") (const_int 0))
147 (any_ge:X (match_operand:X 2 "register_operand")
149 (clobber (match_operand:X 3 "register_operand"))]
150 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
151 [(set (match_dup 3) (any_ge:X (match_dup 2) (const_int 1)))
152 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 1) (const_int 0))
156 ;; Similarly but LU/LTU which allows an arith_operand
158 [(set (match_operand:X 0 "register_operand")
159 (and:X (ne:X (match_operand:X 1 "register_operand") (const_int 0))
160 (any_lt:X (match_operand:X 2 "register_operand")
161 (match_operand:X 3 "arith_operand"))))
162 (clobber (match_operand:X 4 "register_operand"))]
163 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
164 [(set (match_dup 4) (any_lt:X (match_dup 2) (match_dup 3)))
165 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 1) (const_int 0))
169 ;; Finally LE/LEU which requires sle_operand.
171 [(set (match_operand:X 0 "register_operand")
172 (and:X (ne:X (match_operand:X 1 "register_operand") (const_int 0))
173 (any_le:X (match_operand:X 2 "register_operand")
174 (match_operand:X 3 "sle_operand"))))
175 (clobber (match_operand:X 4 "register_operand"))]
176 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
177 [(set (match_dup 4) (any_le:X (match_dup 2) (match_dup 3)))
178 (set (match_dup 0) (if_then_else:X (eq:X (match_dup 1) (const_int 0))
183 ;; Inverted versions from above. I tried to get this to work with
184 ;; iterators, but didn't have any success disambiguating the code attr
185 ;; for the eq/ne flip we have to do.
187 [(set (match_operand:X 0 "register_operand")
188 (and:X (eq:X (match_operand:X 1 "register_operand") (const_int 0))
189 (scc_0:X (match_operand:X 2 "register_operand")
190 (match_operand:X 3 "reg_or_0_operand"))))
191 (clobber (match_operand:X 4 "register_operand"))]
192 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
193 [(set (match_dup 4) (scc_0:X (match_dup 2) (match_dup 3)))
194 (set (match_dup 0) (if_then_else:X (ne:X (match_dup 1) (const_int 0))
198 ;; Similarly but GE/GEU which requires (const_int 1) as an operand.
200 [(set (match_operand:X 0 "register_operand")
201 (and:X (eq:X (match_operand:X 1 "register_operand") (const_int 0))
202 (any_ge:X (match_operand:X 2 "register_operand")
204 (clobber (match_operand:X 3 "register_operand"))]
205 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
206 [(set (match_dup 3) (any_ge:X (match_dup 2) (const_int 1)))
207 (set (match_dup 0) (if_then_else:X (ne:X (match_dup 1) (const_int 0))
211 ;; Similarly but LU/LTU which allows an arith_operand
213 [(set (match_operand:X 0 "register_operand")
214 (and:X (eq:X (match_operand:X 1 "register_operand") (const_int 0))
215 (any_lt:X (match_operand:X 2 "register_operand")
216 (match_operand:X 3 "arith_operand"))))
217 (clobber (match_operand:X 4 "register_operand"))]
218 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
219 [(set (match_dup 4) (any_lt:X (match_dup 2) (match_dup 3)))
220 (set (match_dup 0) (if_then_else:X (ne:X (match_dup 1) (const_int 0))
224 ;; Finally LE/LEU which requires sle_operand.
226 [(set (match_operand:X 0 "register_operand")
227 (and:X (eq:X (match_operand:X 1 "register_operand") (const_int 0))
228 (any_le:X (match_operand:X 2 "register_operand")
229 (match_operand:X 3 "sle_operand"))))
230 (clobber (match_operand:X 4 "register_operand"))]
231 "TARGET_ZICOND_LIKE || TARGET_XTHEADCONDMOV"
232 [(set (match_dup 4) (any_le:X (match_dup 2) (match_dup 3)))
233 (set (match_dup 0) (if_then_else:X (ne:X (match_dup 1) (const_int 0))