| 1 |
;; ALU operations with zero extensions |
| 2 |
;; |
| 3 |
;; Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 4 |
;; Contributed by Dimitar Dimitrov <[email protected]> |
| 5 |
;; |
| 6 |
;; This file is part of GCC. |
| 7 |
;; |
| 8 |
;; GCC is free software; you can redistribute it and/or modify |
| 9 |
;; it under the terms of the GNU General Public License as published by |
| 10 |
;; the Free Software Foundation; either version 3, or (at your option) |
| 11 |
;; any later version. |
| 12 |
;; |
| 13 |
;; GCC is distributed in the hope that it will be useful, |
| 14 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
;; GNU General Public License for more details. |
| 17 |
;; |
| 18 |
;; You should have received a copy of the GNU General Public License |
| 19 |
;; along with GCC; see the file COPYING3. If not see |
| 20 |
;; <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
; All PRU ALU instructions automatically zero-extend their source operands, |
| 23 |
; and zero-extract the result into the destination register. This is |
| 24 |
; described in the machine description by defining a separate pattern |
| 25 |
; for each possible combination of zero_extend and mode for input operands. |
| 26 |
; |
| 27 |
; An unfortunate side effect is that quite a few invalid RTL patterns are |
| 28 |
; generated. For example: |
| 29 |
; ... (zero_extend:SI (match_operand:SI ...)) ... |
| 30 |
; These patterns are harmless since no pass should generate such RTL. This |
| 31 |
; shortcut allows us to keep small and concise machine description patterns. |
| 32 |
|
| 33 |
|
| 34 |
(define_subst_attr "alu2_zext" "alu2_zext_subst" "_z" "_noz") |
| 35 |
|
| 36 |
(define_subst_attr "alu3_zext_op0" "alu3_zext_op0_subst" "_z0" "_noz0") |
| 37 |
(define_subst_attr "alu3_zext_op1" "alu3_zext_op1_subst" "_z1" "_noz1") |
| 38 |
(define_subst_attr "alu3_zext_op2" "alu3_zext_op2_subst" "_z2" "_noz2") |
| 39 |
(define_subst_attr "alu3_zext" "alu3_zext_subst" "_z" "_noz") |
| 40 |
|
| 41 |
(define_subst_attr "lmbd_zext_op1" "lmbd_zext_op1_subst" "_z1" "_noz1") |
| 42 |
(define_subst_attr "lmbd_zext_op2" "lmbd_zext_op2_subst" "_z2" "_noz2") |
| 43 |
(define_subst_attr "lmbd_zext" "lmbd_zext_subst" "_z" "_noz") |
| 44 |
|
| 45 |
(define_subst_attr "bitalu_zext" "bitalu_zext_subst" "_z" "_noz") |
| 46 |
|
| 47 |
(define_code_iterator ALUOP3 [plus minus and ior xor umin umax ashift lshiftrt]) |
| 48 |
(define_code_iterator ALUOP3_ZEXT0 [and ior xor umin umax lshiftrt]) |
| 49 |
(define_code_iterator ALUOP2 [neg not]) |
| 50 |
|
| 51 |
;; Arithmetic Operations |
| 52 |
|
| 53 |
(define_insn "add_impl<EQD:mode><EQS0:mode><EQS1:mode>_<alu3_zext><alu3_zext_op1><alu3_zext_op2>" |
| 54 |
[(set (match_operand:EQD 0 "register_operand" "=r,r,r") |
| 55 |
(plus:EQD |
| 56 |
(zero_extend:EQD |
| 57 |
(match_operand:EQS0 1 "register_operand" "%r,r,r")) |
| 58 |
(zero_extend:EQD |
| 59 |
(match_operand:EQS1 2 "nonmemory_operand" "r,<EQS1:ubyte_constr>,M"))))] |
| 60 |
"" |
| 61 |
"@ |
| 62 |
add\\t%0, %1, %2 |
| 63 |
add\\t%0, %1, %u2 |
| 64 |
sub\\t%0, %1, %n2" |
| 65 |
[(set_attr "type" "alu")]) |
| 66 |
|
| 67 |
(define_insn "sub_impl<EQD:mode><EQS0:mode><EQS1:mode>_<alu3_zext><alu3_zext_op1><alu3_zext_op2>" |
| 68 |
[(set (match_operand:EQD 0 "register_operand" "=r,r") |
| 69 |
(minus:EQD |
| 70 |
(zero_extend:EQD |
| 71 |
(match_operand:EQS0 1 "reg_or_ubyte_operand" "r,<EQS0:ubyte_constr>")) |
| 72 |
(zero_extend:EQD |
| 73 |
(match_operand:EQS1 2 "register_operand" "r,r"))))] |
| 74 |
"" |
| 75 |
"@ |
| 76 |
sub\\t%0, %1, %2 |
| 77 |
rsb\\t%0, %2, %u1" |
| 78 |
[(set_attr "type" "alu")]) |
| 79 |
|
| 80 |
|
| 81 |
;; Left Most Bit Detect instruction. |
| 82 |
(define_insn "pru_lmbd_impl<EQD:mode><EQS0:mode><EQS1:mode>_<lmbd_zext><lmbd_zext_op1><lmbd_zext_op2>" |
| 83 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 84 |
(unspec:EQD |
| 85 |
[(zero_extend:EQD |
| 86 |
(match_operand:EQS0 1 "register_operand" "r")) |
| 87 |
(zero_extend:EQD |
| 88 |
(match_operand:EQS1 2 "reg_or_ubyte_operand" "r<EQS1:ubyte_constr>"))] |
| 89 |
UNSPEC_LMBD))] |
| 90 |
"" |
| 91 |
"lmbd\t%0, %1, %2" |
| 92 |
[(set_attr "type" "alu")]) |
| 93 |
|
| 94 |
(define_insn "neg_impl<EQD:mode><EQS0:mode>_<alu2_zext>" |
| 95 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 96 |
(neg:EQD |
| 97 |
(zero_extend:EQD (match_operand:EQS0 1 "register_operand" "r"))))] |
| 98 |
"" |
| 99 |
"rsb\\t%0, %1, 0" |
| 100 |
[(set_attr "type" "alu")]) |
| 101 |
|
| 102 |
|
| 103 |
(define_insn "one_cmpl_impl<EQD:mode><EQS0:mode>_<alu2_zext>" |
| 104 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 105 |
(not:EQD |
| 106 |
(zero_extend:EQD (match_operand:EQS0 1 "register_operand" "r"))))] |
| 107 |
"" |
| 108 |
"not\\t%0, %1" |
| 109 |
[(set_attr "type" "alu")]) |
| 110 |
|
| 111 |
; Specialized IOR/AND patterns for matching setbit/clearbit instructions. |
| 112 |
; |
| 113 |
; TODO - allow clrbit and setbit to support (1 << REG) constructs |
| 114 |
|
| 115 |
(define_insn "clearbit_<EQD:mode><EQS0:mode>_<bitalu_zext>" |
| 116 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 117 |
(and:EQD |
| 118 |
(zero_extend:EQD |
| 119 |
(match_operand:EQS0 1 "register_operand" "r")) |
| 120 |
(match_operand:EQD 2 "single_zero_operand" "n")))] |
| 121 |
"" |
| 122 |
"clr\\t%0, %1, %V2" |
| 123 |
[(set_attr "type" "alu")]) |
| 124 |
|
| 125 |
(define_insn "setbit_<EQD:mode><EQS0:mode>_<bitalu_zext>" |
| 126 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 127 |
(ior:EQD |
| 128 |
(zero_extend:EQD |
| 129 |
(match_operand:EQS0 1 "register_operand" "r")) |
| 130 |
(match_operand:EQD 2 "single_one_operand" "n")))] |
| 131 |
"" |
| 132 |
"set\\t%0, %1, %T2" |
| 133 |
[(set_attr "type" "alu")]) |
| 134 |
|
| 135 |
; Regular ALU ops. For all of them it is safe to present the result as |
| 136 |
; zero-extended, because there is no carry or shifted-out bits. |
| 137 |
(define_insn "<code>_impl<EQD:mode><EQS0:mode><EQS1:mode>_<alu3_zext><alu3_zext_op0><alu3_zext_op1><alu3_zext_op2>" |
| 138 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 139 |
(LOGICAL:EQD |
| 140 |
(zero_extend:EQD |
| 141 |
(match_operand:EQS0 1 "register_operand" "%r")) |
| 142 |
(zero_extend:EQD |
| 143 |
(match_operand:EQS1 2 "reg_or_ubyte_operand" "r<EQS1:ubyte_constr>"))))] |
| 144 |
"" |
| 145 |
"<logical_asm>\\t%0, %1, %u2" |
| 146 |
[(set_attr "type" "alu")]) |
| 147 |
|
| 148 |
; Shift left ALU op. Cannot present the result as zero-extended because |
| 149 |
; of the shifted-out bits. |
| 150 |
(define_insn "ashl_impl<EQD:mode><EQS0:mode><EQS1:mode>_<alu3_zext><alu3_zext_op1><alu3_zext_op2>" |
| 151 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 152 |
(ashift:EQD |
| 153 |
(zero_extend:EQD (match_operand:EQS0 1 "register_operand" "r")) |
| 154 |
(zero_extend:EQD (match_operand:EQS1 2 "shift_operand" "rL"))))] |
| 155 |
"" |
| 156 |
"lsl\\t%0, %1, %2" |
| 157 |
[(set_attr "type" "alu")]) |
| 158 |
|
| 159 |
; Shift right ALU op. The result can be presented as zero-extended. |
| 160 |
(define_insn "lshr_impl<EQD:mode><EQS0:mode><EQS1:mode>_<alu3_zext><alu3_zext_op0><alu3_zext_op1><alu3_zext_op2>" |
| 161 |
[(set (match_operand:EQD 0 "register_operand" "=r") |
| 162 |
(lshiftrt:EQD |
| 163 |
(zero_extend:EQD (match_operand:EQS0 1 "register_operand" "r")) |
| 164 |
(zero_extend:EQD (match_operand:EQS1 2 "shift_operand" "rL"))))] |
| 165 |
"" |
| 166 |
"lsr\\t%0, %1, %2" |
| 167 |
[(set_attr "type" "alu")]) |
| 168 |
|
| 169 |
;; Substitutions |
| 170 |
|
| 171 |
(define_subst "alu2_zext_subst" |
| 172 |
[(set (match_operand:EQD 0) |
| 173 |
(ALUOP2:EQD (zero_extend:EQD (match_operand:EQD 1))))] |
| 174 |
"" |
| 175 |
[(set (match_dup 0) |
| 176 |
(ALUOP2:EQD (match_dup 1)))]) |
| 177 |
|
| 178 |
(define_subst "bitalu_zext_subst" |
| 179 |
[(set (match_operand:EQD 0) |
| 180 |
(ALUOP3:EQD (zero_extend:EQD (match_operand:EQD 1)) |
| 181 |
(match_operand:EQD 2)))] |
| 182 |
"" |
| 183 |
[(set (match_dup 0) |
| 184 |
(ALUOP3:EQD (match_dup 1) |
| 185 |
(match_dup 2)))]) |
| 186 |
|
| 187 |
(define_subst "alu3_zext_subst" |
| 188 |
[(set (match_operand:EQD 0) |
| 189 |
(ALUOP3:EQD (zero_extend:EQD (match_operand:EQD 1)) |
| 190 |
(zero_extend:EQD (match_operand:EQD 2))))] |
| 191 |
"" |
| 192 |
[(set (match_dup 0) |
| 193 |
(ALUOP3:EQD (match_dup 1) |
| 194 |
(match_dup 2)))]) |
| 195 |
|
| 196 |
(define_subst "alu3_zext_op1_subst" |
| 197 |
[(set (match_operand:EQD 0) |
| 198 |
(ALUOP3:EQD (zero_extend:EQD (match_operand:EQD 1)) |
| 199 |
(zero_extend:EQD (match_operand:EQS1 2))))] |
| 200 |
"" |
| 201 |
[(set (match_dup 0) |
| 202 |
(ALUOP3:EQD (match_dup 1) |
| 203 |
(zero_extend:EQD (match_dup 2))))]) |
| 204 |
|
| 205 |
(define_subst "alu3_zext_op2_subst" |
| 206 |
[(set (match_operand:EQD 0) |
| 207 |
(ALUOP3:EQD (zero_extend:EQD (match_operand:EQS0 1)) |
| 208 |
(zero_extend:EQD (match_operand:EQD 2))))] |
| 209 |
"" |
| 210 |
[(set (match_dup 0) |
| 211 |
(ALUOP3:EQD (zero_extend:EQD (match_dup 1)) |
| 212 |
(match_dup 2)))]) |
| 213 |
|
| 214 |
;; Some ALU operations with zero-extended inputs are |
| 215 |
;; equivalent to doing the same ALU operation in the |
| 216 |
;; smaller mode, and then zero-extending the output. |
| 217 |
(define_subst "alu3_zext_op0_subst" |
| 218 |
[(set (match_operand:EQD 0) |
| 219 |
(ALUOP3_ZEXT0:EQD (zero_extend:EQD (match_operand:EQS0 1)) |
| 220 |
(zero_extend:EQD (match_operand:EQS0 2))))] |
| 221 |
"GET_MODE_SIZE (<EQS0:MODE>mode) < GET_MODE_SIZE (<EQD:MODE>mode)" |
| 222 |
[(set (match_dup 0) |
| 223 |
(zero_extend:EQD |
| 224 |
(ALUOP3_ZEXT0:EQS0 (match_dup 1) |
| 225 |
(match_dup 2))))]) |
| 226 |
|
| 227 |
(define_subst "lmbd_zext_subst" |
| 228 |
[(set (match_operand:EQD 0) |
| 229 |
(unspec:EQD [(zero_extend:EQD (match_operand:EQD 1)) |
| 230 |
(zero_extend:EQD (match_operand:EQD 2))] |
| 231 |
UNSPEC_LMBD))] |
| 232 |
"" |
| 233 |
[(set (match_dup 0) |
| 234 |
(unspec:EQD [(match_dup 1) |
| 235 |
(match_dup 2)] |
| 236 |
UNSPEC_LMBD))]) |
| 237 |
|
| 238 |
(define_subst "lmbd_zext_op1_subst" |
| 239 |
[(set (match_operand:EQD 0) |
| 240 |
(unspec:EQD [(zero_extend:EQD (match_operand:EQD 1)) |
| 241 |
(zero_extend:EQD (match_operand:EQS1 2))] |
| 242 |
UNSPEC_LMBD))] |
| 243 |
"" |
| 244 |
[(set (match_dup 0) |
| 245 |
(unspec:EQD [(match_dup 1) |
| 246 |
(zero_extend:EQD (match_dup 2))] |
| 247 |
UNSPEC_LMBD))]) |
| 248 |
|
| 249 |
(define_subst "lmbd_zext_op2_subst" |
| 250 |
[(set (match_operand:EQD 0) |
| 251 |
(unspec:EQD [(zero_extend:EQD (match_operand:EQD 1)) |
| 252 |
(zero_extend:EQD (match_operand:EQD 2))] |
| 253 |
UNSPEC_LMBD))] |
| 254 |
"" |
| 255 |
[(set (match_dup 0) |
| 256 |
(unspec:EQD [(zero_extend:EQD (match_dup 1)) |
| 257 |
(match_dup 2)] |
| 258 |
UNSPEC_LMBD))]) |