| 1 |
;; Insn predicate definitions for AVR 8-bit microcontrollers. |
| 2 |
;; Copyright (C) 2006-2025 Free Software Foundation, Inc. |
| 3 |
;; |
| 4 |
;; This file is part of GCC. |
| 5 |
;; |
| 6 |
;; GCC is free software; you can redistribute it and/or modify |
| 7 |
;; it under the terms of the GNU General Public License as published by |
| 8 |
;; the Free Software Foundation; either version 3, or (at your option) |
| 9 |
;; any later version. |
| 10 |
;; |
| 11 |
;; GCC is distributed in the hope that it will be useful, |
| 12 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
;; GNU General Public License for more details. |
| 15 |
;; |
| 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/>. |
| 19 |
|
| 20 |
;; Registers from r0 to r15. |
| 21 |
(define_predicate "l_register_operand" |
| 22 |
(and (match_code "reg") |
| 23 |
(match_test "REGNO (op) <= 15"))) |
| 24 |
|
| 25 |
;; Registers from r16 to r31. |
| 26 |
(define_predicate "d_register_operand" |
| 27 |
(and (match_code "reg") |
| 28 |
(match_test "REGNO (op) >= 16 && REGNO (op) <= 31"))) |
| 29 |
|
| 30 |
(define_predicate "scratch_or_dreg_operand" |
| 31 |
(ior (match_operand 0 "d_register_operand") |
| 32 |
(and (match_code ("scratch")) |
| 33 |
(match_operand 0 "scratch_operand")))) |
| 34 |
|
| 35 |
(define_predicate "even_register_operand" |
| 36 |
(and (match_code "reg") |
| 37 |
(and (match_test "REGNO (op) <= 31") |
| 38 |
(match_test "(REGNO (op) & 1) == 0")))) |
| 39 |
|
| 40 |
(define_predicate "odd_register_operand" |
| 41 |
(and (match_code "reg") |
| 42 |
(and (match_test "REGNO (op) <= 31") |
| 43 |
(match_test "(REGNO (op) & 1) != 0")))) |
| 44 |
|
| 45 |
;; SP register. |
| 46 |
(define_predicate "stack_register_operand" |
| 47 |
(and (match_code "reg") |
| 48 |
(match_test "REGNO (op) == REG_SP"))) |
| 49 |
|
| 50 |
;; Return true if OP is a valid address for lower half of I/O space. |
| 51 |
(define_special_predicate "low_io_address_operand" |
| 52 |
(ior (and (match_code "const_int") |
| 53 |
(match_test "IN_RANGE (INTVAL (op) - avr_arch->sfr_offset, |
| 54 |
0, 0x1F)")) |
| 55 |
(and (match_code "symbol_ref") |
| 56 |
(match_test "SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_IO_LOW")))) |
| 57 |
|
| 58 |
;; Return true if OP is a register_operand or low_io_operand. |
| 59 |
(define_predicate "reg_or_low_io_operand" |
| 60 |
(ior (match_operand 0 "register_operand") |
| 61 |
(and (match_code "mem") |
| 62 |
; Deliberately only allow QImode no matter what the mode of |
| 63 |
; the operand is. This effectively disallows and I/O that |
| 64 |
; is not QImode for that operand. |
| 65 |
(match_test "GET_MODE (op) == QImode") |
| 66 |
(match_test "low_io_address_operand (XEXP (op, 0), Pmode)")))) |
| 67 |
|
| 68 |
;; Return true if OP is a valid address for high half of I/O space. |
| 69 |
(define_predicate "high_io_address_operand" |
| 70 |
(and (match_code "const_int") |
| 71 |
(match_test "IN_RANGE (INTVAL (op) - avr_arch->sfr_offset, |
| 72 |
0x20, 0x3F)"))) |
| 73 |
|
| 74 |
;; Return true if OP is a valid address of I/O space. |
| 75 |
(define_special_predicate "io_address_operand" |
| 76 |
(ior (and (match_code "const_int") |
| 77 |
(match_test "IN_RANGE (INTVAL (op) - avr_arch->sfr_offset, |
| 78 |
0, 0x3F)")) |
| 79 |
(and (match_code "symbol_ref") |
| 80 |
(match_test "SYMBOL_REF_FLAGS (op) & SYMBOL_FLAG_IO")))) |
| 81 |
|
| 82 |
;; Return 1 if OP is a general operand not in flash memory |
| 83 |
(define_predicate "nop_general_operand" |
| 84 |
(and (match_operand 0 "general_operand") |
| 85 |
(match_test "!avr_mem_flash_p (op)"))) |
| 86 |
|
| 87 |
;; Return 1 if OP is an "ordinary" general operand, i.e. a general |
| 88 |
;; operand whose load is not handled by a libgcc call or ELPM. |
| 89 |
(define_predicate "nox_general_operand" |
| 90 |
(and (match_operand 0 "general_operand") |
| 91 |
(not (match_test "avr_load_libgcc_p (op)")) |
| 92 |
(not (match_test "avr_mem_flashx_p (op)")) |
| 93 |
(not (match_test "avr_mem_memx_p (op)")))) |
| 94 |
|
| 95 |
;; Return 1 if OP is the zero constant for MODE. |
| 96 |
(define_predicate "const0_operand" |
| 97 |
(and (match_code "const_int,const_fixed,const_double") |
| 98 |
(match_test "op == CONST0_RTX (mode)"))) |
| 99 |
|
| 100 |
;; Return 1 if OP is the one constant integer for MODE. |
| 101 |
(define_predicate "const1_operand" |
| 102 |
(and (match_code "const_int") |
| 103 |
(match_test "op == CONST1_RTX (mode)"))) |
| 104 |
|
| 105 |
;; Return 1 if OP is the constant integer 7 for MODE. |
| 106 |
(define_predicate "const7_operand" |
| 107 |
(and (match_code "const_int") |
| 108 |
(match_test "INTVAL(op) == 7"))) |
| 109 |
|
| 110 |
;; Return 1 if OP is the constant integer 15 for MODE. |
| 111 |
(define_predicate "const15_operand" |
| 112 |
(and (match_code "const_int") |
| 113 |
(match_test "INTVAL(op) == 15"))) |
| 114 |
|
| 115 |
;; Return 1 if OP is the constant integer 23 for MODE. |
| 116 |
(define_predicate "const23_operand" |
| 117 |
(and (match_code "const_int") |
| 118 |
(match_test "INTVAL(op) == 23"))) |
| 119 |
|
| 120 |
;; Return 1 if OP is the constant integer 31 for MODE. |
| 121 |
(define_predicate "const31_operand" |
| 122 |
(and (match_code "const_int") |
| 123 |
(match_test "INTVAL(op) == 31"))) |
| 124 |
|
| 125 |
|
| 126 |
;; Return 1 if OP is constant integer 0..7 for MODE. |
| 127 |
(define_predicate "const_0_to_7_operand" |
| 128 |
(and (match_code "const_int") |
| 129 |
(match_test "IN_RANGE (INTVAL (op), 0, 7)"))) |
| 130 |
|
| 131 |
;; Return 1 if OP is constant integer 0..15 for MODE. |
| 132 |
(define_predicate "const_0_to_15_operand" |
| 133 |
(and (match_code "const_int") |
| 134 |
(match_test "IN_RANGE (INTVAL (op), 0, 15)"))) |
| 135 |
|
| 136 |
;; Return 1 if OP is constant integer 0..23 for MODE. |
| 137 |
(define_predicate "const_0_to_23_operand" |
| 138 |
(and (match_code "const_int") |
| 139 |
(match_test "IN_RANGE (INTVAL (op), 0, 23)"))) |
| 140 |
|
| 141 |
;; Return 1 if OP is constant integer 0..31 for MODE. |
| 142 |
(define_predicate "const_0_to_31_operand" |
| 143 |
(and (match_code "const_int") |
| 144 |
(match_test "IN_RANGE (INTVAL (op), 0, 31)"))) |
| 145 |
|
| 146 |
;; Return 1 if OP is constant integer 2..7 for MODE. |
| 147 |
(define_predicate "const_2_to_7_operand" |
| 148 |
(and (match_code "const_int") |
| 149 |
(match_test "IN_RANGE (INTVAL (op), 2, 7)"))) |
| 150 |
|
| 151 |
;; Return true if OP is constant integer 1..3 for MODE. |
| 152 |
(define_predicate "const_1_to_3_operand" |
| 153 |
(and (match_code "const_int") |
| 154 |
(match_test "IN_RANGE (INTVAL (op), 1, 3)"))) |
| 155 |
|
| 156 |
;; Return 1 if OP is constant integer 1..6 for MODE. |
| 157 |
(define_predicate "const_1_to_6_operand" |
| 158 |
(and (match_code "const_int") |
| 159 |
(match_test "IN_RANGE (INTVAL (op), 1, 6)"))) |
| 160 |
|
| 161 |
;; Return 1 if OP is constant integer 2..6 for MODE. |
| 162 |
(define_predicate "const_2_to_6_operand" |
| 163 |
(and (match_code "const_int") |
| 164 |
(match_test "IN_RANGE (INTVAL (op), 2, 6)"))) |
| 165 |
|
| 166 |
;; Return 1 if OP is constant integer -255..-1. |
| 167 |
(define_predicate "const_m255_to_m1_operand" |
| 168 |
(and (match_code "const_int") |
| 169 |
(match_test "IN_RANGE (INTVAL (op), -255, -1)"))) |
| 170 |
|
| 171 |
;; Return true if OP is a CONST_INT in { -2, -1, 1, 2 }. |
| 172 |
(define_predicate "abs1_abs2_operand" |
| 173 |
(and (match_code "const_int") |
| 174 |
(match_test "INTVAL (op) != 0") |
| 175 |
(match_test "IN_RANGE (INTVAL (op), -2, 2)"))) |
| 176 |
|
| 177 |
;; Returns true if OP is either the constant zero or a register. |
| 178 |
(define_predicate "reg_or_0_operand" |
| 179 |
(ior (match_operand 0 "register_operand") |
| 180 |
(match_operand 0 "const0_operand"))) |
| 181 |
|
| 182 |
;; Returns true if OP is either the constant zero or an upper register. |
| 183 |
(define_predicate "dreg_or_0_operand" |
| 184 |
(ior (match_operand 0 "d_register_operand") |
| 185 |
(match_operand 0 "const0_operand"))) |
| 186 |
|
| 187 |
;; Returns 1 if OP is a SYMBOL_REF. |
| 188 |
(define_predicate "symbol_ref_operand" |
| 189 |
(match_code "symbol_ref")) |
| 190 |
|
| 191 |
;; Returns true when OP is a SYMBOL_REF, CONST or CONST_INT that is |
| 192 |
;; a multiple of 256, i.e. lo8(OP) = 0. |
| 193 |
(define_predicate "const_0mod256_operand" |
| 194 |
(ior (and (match_code "symbol_ref") |
| 195 |
(match_test "SYMBOL_REF_DECL (op) |
| 196 |
&& DECL_P (SYMBOL_REF_DECL (op)) |
| 197 |
&& DECL_ALIGN (SYMBOL_REF_DECL (op)) >= 8 * 256")) |
| 198 |
(and (match_code "const") |
| 199 |
(match_test "GET_CODE (XEXP (op, 0)) == PLUS") |
| 200 |
(match_test "const_0mod256_operand (XEXP (XEXP (op, 0), 0), HImode)") |
| 201 |
(match_test "const_0mod256_operand (XEXP (XEXP (op, 0), 1), HImode)")) |
| 202 |
(and (match_code "const_int") |
| 203 |
(match_test "INTVAL (op) % 256 == 0")))) |
| 204 |
|
| 205 |
;; Return true if OP is a text segment reference. |
| 206 |
;; This is needed for program memory address expressions. |
| 207 |
(define_predicate "text_segment_operand" |
| 208 |
(match_code "code_label,label_ref,symbol_ref,plus,const") |
| 209 |
{ |
| 210 |
switch (GET_CODE (op)) |
| 211 |
{ |
| 212 |
case CODE_LABEL: |
| 213 |
return true; |
| 214 |
case LABEL_REF : |
| 215 |
return true; |
| 216 |
case SYMBOL_REF : |
| 217 |
return SYMBOL_REF_FUNCTION_P (op); |
| 218 |
case PLUS : |
| 219 |
// Assume canonical format of symbol + constant. |
| 220 |
// Fall through. |
| 221 |
case CONST : |
| 222 |
return text_segment_operand (XEXP (op, 0), VOIDmode); |
| 223 |
default : |
| 224 |
return false; |
| 225 |
} |
| 226 |
}) |
| 227 |
|
| 228 |
;; Return true if OP is a constant that contains only one 1 in its |
| 229 |
;; binary representation. |
| 230 |
(define_predicate "single_one_operand" |
| 231 |
(and (match_code "const_int") |
| 232 |
(match_test "exact_log2(INTVAL (op) & GET_MODE_MASK (mode)) >= 0"))) |
| 233 |
|
| 234 |
;; Return true if OP is a constant that contains only one 0 in its |
| 235 |
;; binary representation. |
| 236 |
(define_predicate "single_zero_operand" |
| 237 |
(and (match_code "const_int") |
| 238 |
(match_test "exact_log2(~INTVAL (op) & GET_MODE_MASK (mode)) >= 0"))) |
| 239 |
|
| 240 |
;; |
| 241 |
(define_predicate "avr_sp_immediate_operand" |
| 242 |
(and (match_code "const_int") |
| 243 |
(match_test "satisfies_constraint_Csp (op)"))) |
| 244 |
|
| 245 |
;; True for EQ & NE |
| 246 |
(define_predicate "eqne_operator" |
| 247 |
(match_code "eq,ne")) |
| 248 |
|
| 249 |
;; True for GE & LT |
| 250 |
(define_predicate "gelt_operator" |
| 251 |
(match_code "ge,lt")) |
| 252 |
|
| 253 |
;; True for GT, GTU, LE & LEU |
| 254 |
(define_predicate "difficult_comparison_operator" |
| 255 |
(match_code "gt,gtu,le,leu")) |
| 256 |
|
| 257 |
;; False for GT, GTU, LE & LEU |
| 258 |
(define_predicate "simple_comparison_operator" |
| 259 |
(and (match_operand 0 "comparison_operator") |
| 260 |
(not (match_code "gt,gtu,le,leu")))) |
| 261 |
|
| 262 |
;; True for EQ, NE, GE, LT, GT, LE |
| 263 |
(define_predicate "signed_comparison_operator" |
| 264 |
(match_code "eq,ne,ge,lt,gt,le")) |
| 265 |
|
| 266 |
;; True for SIGN_EXTEND, ZERO_EXTEND. |
| 267 |
(define_predicate "extend_operator" |
| 268 |
(match_code "sign_extend,zero_extend")) |
| 269 |
|
| 270 |
;; True for 8-bit operations that set SREG.N and SREG.Z in a |
| 271 |
;; usable way: |
| 272 |
;; * OP0 is a QImode register, and |
| 273 |
;; * OP1 is a QImode register or CONST_INT, and |
| 274 |
;; |
| 275 |
;; the allowed operations is one of: |
| 276 |
;; |
| 277 |
;; * SHIFTs with a const_int offset in { 1, 2, 3 }. |
| 278 |
;; * MINUS and XOR with a register operand |
| 279 |
;; * IOR and AND with a register operand, or d-reg + const_int |
| 280 |
;; * PLUS with a register operand, or d-reg + const_int, |
| 281 |
;; or a const_int in { -2, -1, 1, 2 }. */ |
| 282 |
(define_predicate "op8_ZN_operator" |
| 283 |
(and (match_code "plus,minus,ashift,ashiftrt,lshiftrt,and,ior,xor") |
| 284 |
(match_test "avr_op8_ZN_operator (op)"))) |
| 285 |
|
| 286 |
;; Return true if OP is a valid call operand. |
| 287 |
(define_predicate "call_insn_operand" |
| 288 |
(and (match_code "mem") |
| 289 |
(ior (match_test "register_operand (XEXP (op, 0), mode)") |
| 290 |
(match_test "CONSTANT_ADDRESS_P (XEXP (op, 0))")))) |
| 291 |
|
| 292 |
;; For some insns we must ensure that no hard register is inserted |
| 293 |
;; into their operands because the insns are split and the split |
| 294 |
;; involves hard registers. An example are divmod insn that are |
| 295 |
;; split to insns that represent implicit library calls. |
| 296 |
|
| 297 |
;; True for register that is pseudo register. |
| 298 |
(define_predicate "pseudo_register_operand" |
| 299 |
(and (match_operand 0 "register_operand") |
| 300 |
(not (and (match_code "reg") |
| 301 |
(match_test "HARD_REGISTER_P (op)"))))) |
| 302 |
|
| 303 |
;; True for operand that is pseudo register or CONST_INT. |
| 304 |
(define_predicate "pseudo_register_or_const_int_operand" |
| 305 |
(ior (match_operand 0 "const_int_operand") |
| 306 |
(match_operand 0 "pseudo_register_operand"))) |
| 307 |
|
| 308 |
;; We keep combiner from inserting hard registers into the input of sign- and |
| 309 |
;; zero-extends. A hard register in the input operand is not wanted because |
| 310 |
;; 32-bit multiply patterns clobber some hard registers and extends with a |
| 311 |
;; hard register that overlaps these clobbers won't combine to a widening |
| 312 |
;; multiplication. There is no need for combine to propagate or insert |
| 313 |
;; hard registers, register allocation can do it just as well. |
| 314 |
|
| 315 |
;; True for operand that is pseudo register at combine time. |
| 316 |
(define_predicate "combine_pseudo_register_operand" |
| 317 |
(ior (match_operand 0 "pseudo_register_operand") |
| 318 |
(and (match_operand 0 "register_operand") |
| 319 |
(match_test "reload_completed || reload_in_progress")))) |
| 320 |
|
| 321 |
;; Return true if OP is a constant integer that is either |
| 322 |
;; 8 or 16 or 24. |
| 323 |
(define_predicate "const_8_16_24_operand" |
| 324 |
(and (match_code "const_int") |
| 325 |
(match_test "INTVAL(op) == 8 || INTVAL(op) == 16 || INTVAL(op) == 24"))) |
| 326 |
|
| 327 |
;; Unsigned CONST_INT that fits in 8 bits, i.e. 0..255. |
| 328 |
(define_predicate "u8_operand" |
| 329 |
(and (match_code "const_int") |
| 330 |
(match_test "IN_RANGE (INTVAL (op), 0, 255)"))) |
| 331 |
|
| 332 |
;; Signed CONST_INT that fits in 8 bits, i.e. -128..127. |
| 333 |
(define_predicate "s8_operand" |
| 334 |
(and (match_code "const_int") |
| 335 |
(match_test "IN_RANGE (INTVAL (op), -128, 127)"))) |
| 336 |
|
| 337 |
;; One-extended CONST_INT that fits in 8 bits, i.e. -256..-1. |
| 338 |
(define_predicate "o8_operand" |
| 339 |
(and (match_code "const_int") |
| 340 |
(match_test "IN_RANGE (INTVAL (op), -256, -1)"))) |
| 341 |
|
| 342 |
;; Signed CONST_INT that fits in 9 bits, i.e. -256..255. |
| 343 |
(define_predicate "s9_operand" |
| 344 |
(and (match_code "const_int") |
| 345 |
(match_test "IN_RANGE (INTVAL (op), -256, 255)"))) |
| 346 |
|
| 347 |
(define_predicate "register_or_s9_operand" |
| 348 |
(ior (match_operand 0 "register_operand") |
| 349 |
(match_operand 0 "s9_operand"))) |
| 350 |
|
| 351 |
;; Unsigned CONST_INT that fits in 16 bits, i.e. 0..65536. |
| 352 |
(define_predicate "u16_operand" |
| 353 |
(and (match_code "const_int") |
| 354 |
(match_test "IN_RANGE (INTVAL (op), 0, (1<<16)-1)"))) |
| 355 |
|
| 356 |
;; Signed CONST_INT that fits in 16 bits, i.e. -32768..32767. |
| 357 |
(define_predicate "s16_operand" |
| 358 |
(and (match_code "const_int") |
| 359 |
(match_test "IN_RANGE (INTVAL (op), -(1<<15), (1<<15)-1)"))) |
| 360 |
|
| 361 |
;; One-extended CONST_INT that fits in 16 bits, i.e. -65536..-1. |
| 362 |
(define_predicate "o16_operand" |
| 363 |
(and (match_code "const_int") |
| 364 |
(match_test "IN_RANGE (INTVAL (op), -(1<<16), -1)"))) |
| 365 |
|
| 366 |
;; Const int, fixed, or double operand |
| 367 |
(define_predicate "const_operand" |
| 368 |
(ior (match_code "const_fixed") |
| 369 |
(match_code "const_double") |
| 370 |
(match_operand 0 "const_int_operand"))) |
| 371 |
|
| 372 |
;; Const int, const fixed, or const double operand |
| 373 |
(define_predicate "nonmemory_or_const_operand" |
| 374 |
(ior (match_code "const_fixed") |
| 375 |
(match_code "const_double") |
| 376 |
(match_operand 0 "nonmemory_operand"))) |
| 377 |
|
| 378 |
;; Immediate, const fixed, or const double operand |
| 379 |
(define_predicate "const_or_immediate_operand" |
| 380 |
(ior (match_code "const_fixed") |
| 381 |
(match_code "const_double") |
| 382 |
(match_operand 0 "immediate_operand"))) |
| 383 |
|
| 384 |
(define_predicate "set_some_operation" |
| 385 |
(and (match_code "parallel") |
| 386 |
(match_test "avr_set_some_operation (op)"))) |