| 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. |
| 4 |
|
| 5 |
;; This file is part of GCC. |
| 6 |
|
| 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) |
| 10 |
;; any later version. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. |
| 20 |
|
| 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")]) |
| 26 |
|
| 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") |
| 31 |
(const_int 0)) |
| 32 |
(match_operand:GPR 2 "register_operand" "r") |
| 33 |
(const_int 0)))] |
| 34 |
"TARGET_ZICOND_LIKE" |
| 35 |
{ |
| 36 |
if (TARGET_ZICOND) |
| 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"; |
| 40 |
else |
| 41 |
gcc_unreachable (); |
| 42 |
} |
| 43 |
[(set_attr "type" "zicond")]) |
| 44 |
|
| 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") |
| 48 |
(const_int 0)) |
| 49 |
(const_int 0) |
| 50 |
(match_operand:GPR 2 "register_operand" "r")))] |
| 51 |
"TARGET_ZICOND_LIKE" |
| 52 |
{ |
| 53 |
if (TARGET_ZICOND) |
| 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"; |
| 57 |
else |
| 58 |
gcc_unreachable (); |
| 59 |
} |
| 60 |
[(set_attr "type" "zicond")]) |
| 61 |
|
| 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") |
| 66 |
(const_int 0)) |
| 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])" |
| 70 |
{ |
| 71 |
if (TARGET_ZICOND) |
| 72 |
return "czero.eqz\t%0,%3,%1"; |
| 73 |
else if (TARGET_XVENTANACONDOPS && TARGET_64BIT) |
| 74 |
return "vt.maskc\t%0,%3,%1"; |
| 75 |
else |
| 76 |
gcc_unreachable (); |
| 77 |
} |
| 78 |
[(set_attr "type" "zicond")]) |
| 79 |
|
| 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") |
| 83 |
(const_int 0)) |
| 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])" |
| 87 |
{ |
| 88 |
if (TARGET_ZICOND) |
| 89 |
return "czero.eqz\t%0,%2,%1"; |
| 90 |
else if (TARGET_XVENTANACONDOPS && TARGET_64BIT) |
| 91 |
return "vt.maskc\t%0,%2,%1"; |
| 92 |
else |
| 93 |
gcc_unreachable (); |
| 94 |
} |
| 95 |
[(set_attr "type" "zicond")]) |
| 96 |
|
| 97 |
;; Combine creates this form in some cases (particularly the coremark |
| 98 |
;; CRC loop). |
| 99 |
(define_split |
| 100 |
[(set (match_operand:X 0 "register_operand") |
| 101 |
(and:X (sign_extract:X (match_operand:X 1 "register_operand") |
| 102 |
(const_int 1) |
| 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)) |
| 109 |
(const_int 0) |
| 110 |
(match_dup 3)))]) |
| 111 |
|
| 112 |
(define_split |
| 113 |
[(set (match_operand:X 0 "register_operand") |
| 114 |
(and:X (sign_extract:X (match_operand:X 1 "register_operand") |
| 115 |
(const_int 1) |
| 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)) |
| 122 |
(const_int 0) |
| 123 |
(match_dup 3)))] |
| 124 |
{ |
| 125 |
operands[2] = GEN_INT (1 << UINTVAL(operands[2])); |
| 126 |
}) |
| 127 |
|
| 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 |
| 130 |
;; conditional zero. |
| 131 |
(define_split |
| 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)) |
| 140 |
(const_int 0) |
| 141 |
(match_dup 4)))]) |
| 142 |
|
| 143 |
;; Similarly but GE/GEU which requires (const_int 1) as an operand. |
| 144 |
(define_split |
| 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") |
| 148 |
(const_int 1)))) |
| 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)) |
| 153 |
(const_int 0) |
| 154 |
(match_dup 3)))]) |
| 155 |
|
| 156 |
;; Similarly but LU/LTU which allows an arith_operand |
| 157 |
(define_split |
| 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)) |
| 166 |
(const_int 0) |
| 167 |
(match_dup 4)))]) |
| 168 |
|
| 169 |
;; Finally LE/LEU which requires sle_operand. |
| 170 |
(define_split |
| 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)) |
| 179 |
(const_int 0) |
| 180 |
(match_dup 4)))]) |
| 181 |
|
| 182 |
|
| 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. |
| 186 |
(define_split |
| 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)) |
| 195 |
(const_int 0) |
| 196 |
(match_dup 4)))]) |
| 197 |
|
| 198 |
;; Similarly but GE/GEU which requires (const_int 1) as an operand. |
| 199 |
(define_split |
| 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") |
| 203 |
(const_int 1)))) |
| 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)) |
| 208 |
(const_int 0) |
| 209 |
(match_dup 3)))]) |
| 210 |
|
| 211 |
;; Similarly but LU/LTU which allows an arith_operand |
| 212 |
(define_split |
| 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)) |
| 221 |
(const_int 0) |
| 222 |
(match_dup 4)))]) |
| 223 |
|
| 224 |
;; Finally LE/LEU which requires sle_operand. |
| 225 |
(define_split |
| 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)) |
| 234 |
(const_int 0) |
| 235 |
(match_dup 4)))]) |
| 236 |
|
| 237 |
|
| 238 |
|