Debian Codegraph @

gcc/config/avr/avr.cc

raw
1 /* Subroutines for insn-output.cc for AVR 8-bit microcontrollers
2 Copyright (C) 1998-2025 Free Software Foundation, Inc.
3 Contributed by Denis Chertykov ([email protected])
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 IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "intl.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "cgraph.h"
34 #include "c-family/c-common.h"
35 #include "cfghooks.h"
36 #include "df.h"
37 #include "memmodel.h"
38 #include "tm_p.h"
39 #include "optabs.h"
40 #include "regs.h"
41 #include "emit-rtl.h"
42 #include "recog.h"
43 #include "conditions.h"
44 #include "insn-attr.h"
45 #include "reload.h"
46 #include "varasm.h"
47 #include "calls.h"
48 #include "stor-layout.h"
49 #include "output.h"
50 #include "explow.h"
51 #include "expr.h"
52 #include "langhooks.h"
53 #include "builtins.h"
54 #include "tree-pass.h"
55 #include "context.h"
56 #include "pass_manager.h"
57 #include "rtl-iter.h"
58
59 /* This file should be included last. */
60 #include "target-def.h"
61
62 /* Maximal allowed offset for an address in the LD command */
63 #define MAX_LD_OFFSET(MODE) (64 - (signed)GET_MODE_SIZE (MODE))
64
65 /* The 4 bits starting at SECTION_MACH_DEP are reserved to store the
66 address space where data is to be located.
67 As the only non-generic address spaces are all located in flash,
68 this can be used to test if data shall go into some .progmem* section.
69 This must be the rightmost field of machine dependent section flags. */
70 #define AVR_SECTION_PROGMEM (0xf * SECTION_MACH_DEP)
71
72 /* Similar 4-bit region for SYMBOL_REF_FLAGS. */
73 #define AVR_SYMBOL_FLAG_PROGMEM (0xf * SYMBOL_FLAG_MACH_DEP)
74
75 /* Similar 4-bit region in SYMBOL_REF_FLAGS:
76 Set address-space AS in SYMBOL_REF_FLAGS of SYM */
77 #define AVR_SYMBOL_SET_ADDR_SPACE(SYM,AS) \
78 do { \
79 SYMBOL_REF_FLAGS (sym) &= ~AVR_SYMBOL_FLAG_PROGMEM; \
80 SYMBOL_REF_FLAGS (sym) |= (AS) * SYMBOL_FLAG_MACH_DEP; \
81 } while (0)
82
83 /* Read address-space from SYMBOL_REF_FLAGS of SYM */
84 #define AVR_SYMBOL_GET_ADDR_SPACE(SYM) \
85 ((SYMBOL_REF_FLAGS (sym) & AVR_SYMBOL_FLAG_PROGMEM) \
86 / SYMBOL_FLAG_MACH_DEP)
87
88 /* (AVR_TINY only): Symbol has attribute progmem */
89 #define AVR_SYMBOL_FLAG_TINY_PM \
90 (SYMBOL_FLAG_MACH_DEP << 7)
91
92 /* (AVR_TINY only): Symbol has attribute absdata */
93 #define AVR_SYMBOL_FLAG_TINY_ABSDATA \
94 (SYMBOL_FLAG_MACH_DEP << 8)
95
96 #define TINY_ADIW(REG1, REG2, I) \
97 "subi " #REG1 ",lo8(-(" #I "))" CR_TAB \
98 "sbci " #REG2 ",hi8(-(" #I "))"
99
100 #define TINY_SBIW(REG1, REG2, I) \
101 "subi " #REG1 ",lo8((" #I "))" CR_TAB \
102 "sbci " #REG2 ",hi8((" #I "))"
103
104 #define AVR_TMP_REGNO (AVR_TINY ? TMP_REGNO_TINY : TMP_REGNO)
105 #define AVR_ZERO_REGNO (AVR_TINY ? ZERO_REGNO_TINY : ZERO_REGNO)
106
107 /* Known address spaces. The order must be the same as in the respective
108 enum from avr.h (or designated initialized must be used). */
109 const avr_addrspace_t avr_addrspace[ADDR_SPACE_COUNT] =
110 {
111 { ADDR_SPACE_RAM, 0, 2, "", 0, nullptr },
112 { ADDR_SPACE_FLASH, 1, 2, "__flash", 0, ".progmem.data" },
113 { ADDR_SPACE_FLASH1, 1, 2, "__flash1", 1, ".progmem1.data" },
114 { ADDR_SPACE_FLASH2, 1, 2, "__flash2", 2, ".progmem2.data" },
115 { ADDR_SPACE_FLASH3, 1, 2, "__flash3", 3, ".progmem3.data" },
116 { ADDR_SPACE_FLASH4, 1, 2, "__flash4", 4, ".progmem4.data" },
117 { ADDR_SPACE_FLASH5, 1, 2, "__flash5", 5, ".progmem5.data" },
118 { ADDR_SPACE_FLASHX, 1, 3, "__flashx", 0, ".progmemx.data" },
119 { ADDR_SPACE_MEMX, 1, 3, "__memx", 0, ".progmemx.data" },
120 };
121
122
123 /* Holding RAM addresses of some SFRs used by the compiler and that
124 are unique over all devices in an architecture like 'avr4'. */
125
126 typedef struct
127 {
128 /* SREG: The processor status */
129 int sreg;
130
131 /* RAMPX, RAMPY, RAMPD and CCP of XMEGA */
132 int ccp;
133 int rampd;
134 int rampx;
135 int rampy;
136
137 /* RAMPZ: The high byte of 24-bit address used with ELPM */
138 int rampz;
139
140 /* SP: The stack pointer and its low and high byte */
141 int sp_l;
142 int sp_h;
143 } avr_addr_t;
144
145 static avr_addr_t avr_addr;
146
147
148 /* Prototypes for local helper functions. */
149
150 static const char *out_movqi_r_mr (rtx_insn *, rtx[], int *);
151 static const char *out_movhi_r_mr (rtx_insn *, rtx[], int *);
152 static const char *out_movsi_r_mr (rtx_insn *, rtx[], int *);
153 static const char *out_movqi_mr_r (rtx_insn *, rtx[], int *);
154 static const char *out_movhi_mr_r (rtx_insn *, rtx[], int *);
155 static const char *out_movsi_mr_r (rtx_insn *, rtx[], int *);
156
157
158 /* Prototypes for hook implementors if needed before their implementation. */
159
160 static bool avr_rtx_costs (rtx, machine_mode, int, int, int *, bool);
161
162
163 /* Allocate registers from r25 to r8 for parameters for function calls
164 resp. r25 to r20 for reduced Tiny. */
165 #define FIRST_CUM_REG REG_26
166
167 /* Last call saved register */
168 #define LAST_CALLEE_SAVED_REG (AVR_TINY ? REG_19 : REG_17)
169
170 /* Implicit target register of LPM instruction (R0) */
171 extern GTY(()) rtx lpm_reg_rtx;
172 rtx lpm_reg_rtx;
173
174 /* (Implicit) address register of LPM instruction (R31:R30 = Z) */
175 extern GTY(()) rtx lpm_addr_reg_rtx;
176 rtx lpm_addr_reg_rtx;
177
178 /* Temporary register RTX (reg:QI TMP_REGNO) */
179 extern GTY(()) rtx tmp_reg_rtx;
180 rtx tmp_reg_rtx;
181
182 /* Zeroed register RTX (reg:QI ZERO_REGNO) */
183 extern GTY(()) rtx zero_reg_rtx;
184 rtx zero_reg_rtx;
185
186 /* Condition Code register RTX (reg:CC REG_CC) */
187 extern GTY(()) rtx cc_reg_rtx;
188 extern GTY(()) rtx ccn_reg_rtx;
189 extern GTY(()) rtx cczn_reg_rtx;
190 rtx cc_reg_rtx;
191 rtx ccn_reg_rtx;
192 rtx cczn_reg_rtx;
193
194 /* RTXs for all general purpose registers as QImode */
195 extern GTY(()) rtx all_regs_rtx[REG_32];
196 rtx all_regs_rtx[REG_32];
197
198 /* SREG, the processor status */
199 extern GTY(()) rtx sreg_rtx;
200 rtx sreg_rtx;
201
202 /* RAMP* special function registers */
203 extern GTY(()) rtx rampd_rtx;
204 extern GTY(()) rtx rampx_rtx;
205 extern GTY(()) rtx rampy_rtx;
206 extern GTY(()) rtx rampz_rtx;
207 rtx rampd_rtx;
208 rtx rampx_rtx;
209 rtx rampy_rtx;
210 rtx rampz_rtx;
211
212 /* RTX containing the strings "" and "e", respectively */
213 static GTY(()) rtx xstring_empty;
214 static GTY(()) rtx xstring_e;
215
216 /* Current architecture. */
217 const avr_arch_t *avr_arch;
218 avr_arch_id avr_arch_index;
219
220 /* Unnamed sections associated to __attribute__((progmem)) aka. PROGMEM
221 or to address space __flash* or __memx. Only used as singletons inside
222 avr_asm_select_section, but it must not be local there because of GTY. */
223 static GTY(()) section *progmem_section[ADDR_SPACE_COUNT];
224
225 /* Condition for insns/expanders from avr-dimode.md. */
226 bool avr_have_dimode = true;
227
228 /* To track if code will use .bss, .data, .rodata. */
229 bool avr_need_clear_bss_p = false;
230 bool avr_need_copy_data_p = false;
231 bool avr_has_rodata_p = false;
232
233 /* Counts how often pass avr-fuse-add has been executed. Is is kept in
234 sync with cfun->machine->n_avr_fuse_add_executed and serves as an
235 insn condition for shift insn splitters. */
236 int n_avr_fuse_add_executed = 0;
237
238 static location_t avr_insn_location = UNKNOWN_LOCATION;
239
240 /* Similar to ctz_hwi etc, but as constexpr so we can use it in
241 static_assert. */
242 static constexpr int
243 avr_ctz (uint64_t x)
244 {
245 #define TSTB(n) (x & ((uint64_t) 1 << n)) ? n
246 return
247 TSTB (0) : TSTB (1) : TSTB (2) : TSTB (3) : TSTB (4)
248 : TSTB (5) : TSTB (6) : TSTB (7) : TSTB (8) : TSTB (9)
249 : TSTB (10) : TSTB (11) : TSTB (12) : TSTB (13) : TSTB (14)
250 : TSTB (15) : TSTB (16) : TSTB (17) : TSTB (18) : TSTB (19)
251 : TSTB (20) : TSTB (21) : TSTB (22) : TSTB (23) : TSTB (24)
252 : TSTB (25) : TSTB (26) : TSTB (27) : TSTB (28) : TSTB (29)
253 : TSTB (30) : TSTB (31) : TSTB (32) : TSTB (33) : TSTB (34)
254 : TSTB (35) : TSTB (36) : TSTB (37) : TSTB (38) : TSTB (39)
255 : TSTB (40) : TSTB (41) : TSTB (42) : TSTB (43) : TSTB (44)
256 : TSTB (45) : TSTB (46) : TSTB (47) : TSTB (48) : TSTB (49)
257 : TSTB (50) : TSTB (51) : TSTB (52) : TSTB (53) : TSTB (54)
258 : TSTB (55) : TSTB (56) : TSTB (57) : TSTB (58) : TSTB (59)
259 : TSTB (60) : TSTB (61) : TSTB (62) : TSTB (63)
260 : 64;
261 #undef TSTB
262 }
263
264 /* Make sure that there are enough section flags bits. avr allocates 4. */
265 static_assert (8 * sizeof (decltype (section_common::flags))
266 >= 4u + avr_ctz (SECTION_MACH_DEP),
267 "section_common::flags is too narrow");
268
269
270 /* Transform UP into lowercase and write the result to LO.
271 You must provide enough space for LO. Return LO. */
272
273 static char *
274 avr_tolower (char *lo, const char *up)
275 {
276 char *lo0 = lo;
277
278 for (; *up; up++, lo++)
279 *lo = TOLOWER (*up);
280
281 *lo = '\0';
282
283 return lo0;
284 }
285
286
287 /* Return chunk of mode MODE of X as an rtx. N specifies the subreg
288 byte at which the chunk starts. N must be an integral multiple
289 of the mode size. */
290
291 rtx
292 avr_chunk (machine_mode mode, rtx x, int n)
293 {
294 gcc_assert (n % GET_MODE_SIZE (mode) == 0);
295 machine_mode xmode = GET_MODE (x) == VOIDmode ? DImode : GET_MODE (x);
296 return simplify_gen_subreg (mode, x, xmode, n);
297 }
298
299
300 /* Return the N-th byte of X as an rtx. */
301
302 rtx
303 avr_byte (rtx x, int n)
304 {
305 return avr_chunk (QImode, x, n);
306 }
307
308
309 /* Return the sub-word of X starting at byte number N. */
310
311 rtx
312 avr_word (rtx x, int n)
313 {
314 return avr_chunk (HImode, x, n);
315 }
316
317
318 /* Return the N-th byte of compile-time constant X as an int8_t. */
319
320 int8_t
321 avr_int8 (rtx x, int n)
322 {
323 gcc_assert (CONST_INT_P (x) || CONST_FIXED_P (x) || CONST_DOUBLE_P (x));
324
325 return (int8_t) trunc_int_for_mode (INTVAL (avr_byte (x, n)), QImode);
326 }
327
328 /* Return the N-th byte of compile-time constant X as an uint8_t. */
329
330 uint8_t
331 avr_uint8 (rtx x, int n)
332 {
333 return (uint8_t) avr_int8 (x, n);
334 }
335
336
337 /* Return the sub-word of compile-time constant X that starts
338 at byte N as an int16_t. */
339
340 int16_t
341 avr_int16 (rtx x, int n)
342 {
343 gcc_assert (CONST_INT_P (x) || CONST_FIXED_P (x) || CONST_DOUBLE_P (x));
344
345 return (int16_t) trunc_int_for_mode (INTVAL (avr_word (x, n)), HImode);
346 }
347
348 /* Return the sub-word of compile-time constant X that starts
349 at byte N as an uint16_t. */
350
351 uint16_t
352 avr_uint16 (rtx x, int n)
353 {
354 return (uint16_t) avr_int16 (x, n);
355 }
356
357
358 /* Constraint helper function. XVAL is a CONST_INT or a CONST_DOUBLE.
359 Return true if the least significant N_BYTES bytes of XVAL all have a
360 popcount in POP_MASK and false, otherwise. POP_MASK represents a subset
361 of integers which contains an integer N iff bit N of POP_MASK is set. */
362
363 bool
364 avr_popcount_each_byte (rtx xval, int n_bytes, int pop_mask)
365 {
366 for (int i = 0; i < n_bytes; i++)
367 {
368 unsigned int val8 = avr_uint8 (xval, i);
369
370 if ((pop_mask & (1 << popcount_hwi (val8))) == 0)
371 return false;
372 }
373
374 return true;
375 }
376
377
378 /* Constraint helper function. XVAL is a CONST_INT. Return true if we
379 can perform XOR without a clobber reg, provided the operation is on
380 a d-register. This means each byte is in { 0, 0xff, 0x80 }. */
381
382 bool
383 avr_xor_noclobber_dconst (rtx xval, int n_bytes)
384 {
385 for (int i = 0; i < n_bytes; ++i)
386 {
387 unsigned int val8 = avr_uint8 (xval, i);
388
389 if (val8 != 0 && val8 != 0xff && val8 != 0x80)
390 return false;
391 }
392
393 return true;
394 }
395
396
397 /* Access some RTX as INT_MODE. If X is a CONST_FIXED we can get
398 the bit representation of X by "casting" it to CONST_INT. */
399
400 rtx
401 avr_to_int_mode (rtx x)
402 {
403 machine_mode mode = GET_MODE (x);
404
405 return VOIDmode == mode
406 ? x
407 : simplify_gen_subreg (int_mode_for_mode (mode).require (), x, mode, 0);
408 }
409
410
411 /* Return true if hard register REG supports the ADIW and SBIW instructions. */
412
413 bool
414 avr_adiw_reg_p (rtx reg)
415 {
416 return (AVR_HAVE_ADIW
417 && test_hard_reg_class (ADDW_REGS, reg));
418 }
419
420
421 /* Return true iff REGNO is in R16...R31. */
422
423 static bool
424 avr_ld_regno_p (int regno)
425 {
426 return TEST_HARD_REG_CLASS (LD_REGS, regno);
427 }
428
429
430 static bool
431 ra_in_progress ()
432 {
433 return avropt_lra_p ? lra_in_progress : reload_in_progress;
434 }
435
436
437 /* Set `avr_arch' as specified by `-mmcu='.
438 Return true on success. */
439
440 static bool
441 avr_set_core_architecture (void)
442 {
443 /* Search for mcu core architecture. */
444
445 if (!avropt_mmcu)
446 avropt_mmcu = AVR_MMCU_DEFAULT;
447
448 avr_arch = &avr_arch_types[0];
449
450 for (const avr_mcu_t *mcu = avr_mcu_types; ; mcu++)
451 {
452 if (!mcu->name)
453 {
454 /* Reached the end of `avr_mcu_types'. This should actually never
455 happen as options are provided by device-specs. It could be a
456 typo in a device-specs or calling the compiler proper directly
457 with -mmcu=<device>. */
458
459 error ("unknown core architecture %qs specified with %qs",
460 avropt_mmcu, "-mmcu=");
461 avr_inform_core_architectures ();
462 break;
463 }
464 else if (strcmp (mcu->name, avropt_mmcu) == 0
465 // Is this a proper architecture ?
466 && !mcu->macro)
467 {
468 avr_arch = &avr_arch_types[mcu->arch_id];
469 avr_arch_index = mcu->arch_id;
470 if (avropt_n_flash < 0)
471 avropt_n_flash = 1 + (mcu->flash_size - 1) / 0x10000;
472
473 return true;
474 }
475 }
476
477 return false;
478 }
479
480
481 /* Implement `TARGET_OPTION_OVERRIDE'. */
482
483 static void
484 avr_option_override (void)
485 {
486 /* caller-save.cc looks for call-clobbered hard registers that are assigned
487 to pseudos that cross calls and tries so save-restore them around calls
488 in order to reduce the number of stack slots needed.
489
490 This might lead to situations where reload is no more able to cope
491 with the challenge of AVR's very few address registers and fails to
492 perform the requested spills. */
493
494 if (avropt_strict_X)
495 flag_caller_saves = 0;
496
497 /* Unwind tables currently require a frame pointer for correctness,
498 see toplev.cc:process_options(). */
499
500 if ((flag_unwind_tables
501 || flag_non_call_exceptions
502 || flag_asynchronous_unwind_tables)
503 && !ACCUMULATE_OUTGOING_ARGS)
504 {
505 flag_omit_frame_pointer = 0;
506 }
507
508 /* Disable flag_delete_null_pointer_checks if zero is a valid address. */
509 if (targetm.addr_space.zero_address_valid (ADDR_SPACE_GENERIC))
510 flag_delete_null_pointer_checks = 0;
511
512 /* PR ipa/92606: Inter-procedural analysis optimizes data across
513 address-spaces and PROGMEM. As of v14, the PROGMEM part is
514 still not fixed (and there is still no target hook as proposed
515 in PR92932). Just disable respective bogus optimization. */
516 flag_ipa_icf_variables = 0;
517
518 if (flag_pic == 1)
519 warning (OPT_fpic, "%<-fpic%> is not supported");
520 if (flag_pic == 2)
521 warning (OPT_fPIC, "%<-fPIC%> is not supported");
522 if (flag_pie == 1)
523 warning (OPT_fpie, "%<-fpie%> is not supported");
524 if (flag_pie == 2)
525 warning (OPT_fPIE, "%<-fPIE%> is not supported");
526
527 #if !defined (HAVE_AS_AVR_MGCCISR_OPTION)
528 avropt_gasisr_prologues = 0;
529 #endif
530
531 if (!avr_set_core_architecture ())
532 return;
533
534 /* Sould be set by avr-common.cc */
535 gcc_assert (avropt_long_double >= avropt_double && avropt_double >= 32);
536
537 /* RAM addresses of some SFRs common to all devices in respective arch. */
538
539 /* SREG: Status Register containing flags like I (global IRQ) */
540 avr_addr.sreg = 0x3F + avr_arch->sfr_offset;
541
542 /* RAMPZ: Address' high part when loading via ELPM */
543 avr_addr.rampz = 0x3B + avr_arch->sfr_offset;
544
545 avr_addr.rampy = 0x3A + avr_arch->sfr_offset;
546 avr_addr.rampx = 0x39 + avr_arch->sfr_offset;
547 avr_addr.rampd = 0x38 + avr_arch->sfr_offset;
548 avr_addr.ccp = (AVR_TINY ? 0x3C : 0x34) + avr_arch->sfr_offset;
549
550 /* SP: Stack Pointer (SP_H:SP_L) */
551 avr_addr.sp_l = 0x3D + avr_arch->sfr_offset;
552 avr_addr.sp_h = avr_addr.sp_l + 1;
553
554 init_machine_status = []()
555 {
556 return ggc_cleared_alloc<machine_function> ();
557 };
558
559 avr_log_set_avr_log ();
560
561 /* As long as peep2_rescan is not implemented, see
562 http://gcc.gnu.org/ml/gcc-patches/2011-10/msg02819.html
563 we add a second peephole2 run to get best results. */
564 {
565 opt_pass *extra_peephole2
566 = g->get_passes ()->get_pass_peephole2 ()->clone ();
567 extra_peephole2->name = "avr-peep2-after-fuse-move";
568 register_pass_info peep2_2_info
569 = { extra_peephole2, "avr-fuse-move", 1, PASS_POS_INSERT_AFTER };
570
571 register_pass (&peep2_2_info);
572 }
573 }
574
575
576 int
577 avr_optimize_size_level ()
578 {
579 return cfun && cfun->decl
580 ? opt_for_fn (cfun->decl, optimize_size)
581 : optimize_size;
582 }
583
584
585 static bool
586 avr_optimize_size_max_p ()
587 {
588 return avr_optimize_size_level () == OPTIMIZE_SIZE_MAX;
589 }
590
591
592 /* Implement `INIT_EXPANDERS'. */
593 /* The function works like a singleton. */
594
595 void
596 avr_init_expanders (void)
597 {
598 for (int regno = REG_0; regno < REG_32; regno ++)
599 all_regs_rtx[regno] = gen_rtx_REG (QImode, regno);
600
601 lpm_reg_rtx = all_regs_rtx[LPM_REGNO];
602 tmp_reg_rtx = all_regs_rtx[AVR_TMP_REGNO];
603 zero_reg_rtx = all_regs_rtx[AVR_ZERO_REGNO];
604
605 cc_reg_rtx = gen_rtx_REG (CCmode, REG_CC);
606 ccn_reg_rtx = gen_rtx_REG (CCNmode, REG_CC);
607 cczn_reg_rtx = gen_rtx_REG (CCZNmode, REG_CC);
608
609 lpm_addr_reg_rtx = gen_rtx_REG (HImode, REG_Z);
610
611 sreg_rtx = gen_rtx_MEM (QImode, GEN_INT (avr_addr.sreg));
612 rampd_rtx = gen_rtx_MEM (QImode, GEN_INT (avr_addr.rampd));
613 rampx_rtx = gen_rtx_MEM (QImode, GEN_INT (avr_addr.rampx));
614 rampy_rtx = gen_rtx_MEM (QImode, GEN_INT (avr_addr.rampy));
615 rampz_rtx = gen_rtx_MEM (QImode, GEN_INT (avr_addr.rampz));
616
617 MEM_VOLATILE_P (sreg_rtx) = 1;
618 MEM_VOLATILE_P (rampd_rtx) = 1;
619 MEM_VOLATILE_P (rampx_rtx) = 1;
620 MEM_VOLATILE_P (rampy_rtx) = 1;
621 MEM_VOLATILE_P (rampz_rtx) = 1;
622
623 xstring_empty = gen_rtx_CONST_STRING (VOIDmode, "");
624 xstring_e = gen_rtx_CONST_STRING (VOIDmode, "e");
625
626 /* TINY core does not have regs r10-r16, but avr-dimode.md expects them
627 to be present */
628 if (AVR_TINY)
629 avr_have_dimode = false;
630 }
631
632
633 /* Implement `REGNO_REG_CLASS'. */
634 /* Return register class for register R. */
635
636 reg_class
637 avr_regno_reg_class (int r)
638 {
639 static const reg_class reg_class_tab[] =
640 {
641 R0_REG,
642 /* r1 - r15 */
643 NO_LD_REGS, NO_LD_REGS, NO_LD_REGS,
644 NO_LD_REGS, NO_LD_REGS, NO_LD_REGS, NO_LD_REGS,
645 NO_LD_REGS, NO_LD_REGS, NO_LD_REGS, NO_LD_REGS,
646 NO_LD_REGS, NO_LD_REGS, NO_LD_REGS, NO_LD_REGS,
647 /* r16 - r23 */
648 SIMPLE_LD_REGS, SIMPLE_LD_REGS, SIMPLE_LD_REGS, SIMPLE_LD_REGS,
649 SIMPLE_LD_REGS, SIMPLE_LD_REGS, SIMPLE_LD_REGS, SIMPLE_LD_REGS,
650 /* r24, r25 */
651 ADDW_REGS, ADDW_REGS,
652 /* X: r26, 27 */
653 POINTER_X_REGS, POINTER_X_REGS,
654 /* Y: r28, r29 */
655 POINTER_Y_REGS, POINTER_Y_REGS,
656 /* Z: r30, r31 */
657 POINTER_Z_REGS, POINTER_Z_REGS,
658 /* SP: SPL, SPH */
659 STACK_REG, STACK_REG
660 };
661
662 if (r <= 33)
663 return reg_class_tab[r];
664
665 if (r == REG_CC)
666 return CC_REG;
667
668 return ALL_REGS;
669 }
670
671
672 /* Implement `TARGET_SCALAR_MODE_SUPPORTED_P'. */
673
674 static bool
675 avr_scalar_mode_supported_p (scalar_mode mode)
676 {
677 if (ALL_FIXED_POINT_MODE_P (mode))
678 return true;
679
680 if (PSImode == mode)
681 return true;
682
683 return default_scalar_mode_supported_p (mode);
684 }
685
686
687 /* Return TRUE if DECL is a VAR_DECL located in flash and FALSE, otherwise. */
688
689 static bool
690 avr_decl_flash_p (tree decl)
691 {
692 if (TREE_CODE (decl) != VAR_DECL
693 || TREE_TYPE (decl) == error_mark_node)
694 {
695 return false;
696 }
697
698 return !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (decl)));
699 }
700
701
702 /* Return TRUE if DECL is a VAR_DECL located in the 24-bit flash
703 address space __memx and FALSE, otherwise. */
704
705 static bool
706 avr_decl_memx_p (tree decl)
707 {
708 if (TREE_CODE (decl) != VAR_DECL
709 || TREE_TYPE (decl) == error_mark_node)
710 {
711 return false;
712 }
713
714 return (ADDR_SPACE_MEMX == TYPE_ADDR_SPACE (TREE_TYPE (decl)));
715 }
716
717
718 /* Return TRUE if DECL is a VAR_DECL located in the 24-bit flash
719 address space __flashx and FALSE, otherwise. */
720
721 static bool
722 avr_decl_flashx_p (tree decl)
723 {
724 if (TREE_CODE (decl) != VAR_DECL
725 || TREE_TYPE (decl) == error_mark_node)
726 {
727 return false;
728 }
729
730 return ADDR_SPACE_FLASHX == TYPE_ADDR_SPACE (TREE_TYPE (decl));
731 }
732
733
734 /* Return TRUE if X is a MEM rtx located in flash and FALSE, otherwise. */
735
736 bool
737 avr_mem_flash_p (rtx x)
738 {
739 return (MEM_P (x)
740 && !ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x)));
741 }
742
743
744 /* Return TRUE if X is a MEM rtx located in the 24-bit
745 address space __memx and FALSE, otherwise. */
746
747 bool
748 avr_mem_memx_p (rtx x)
749 {
750 return (MEM_P (x)
751 && ADDR_SPACE_MEMX == MEM_ADDR_SPACE (x));
752 }
753
754
755 /* Return TRUE if X is a MEM rtx located in the 24-bit flash
756 address space __flashx and FALSE, otherwise. */
757
758 bool
759 avr_mem_flashx_p (rtx x)
760 {
761 return (MEM_P (x)
762 && ADDR_SPACE_FLASHX == MEM_ADDR_SPACE (x));
763 }
764
765
766 /* A helper for the subsequent function attribute used to dig for
767 attribute 'name' in a FUNCTION_DECL or FUNCTION_TYPE. */
768
769 static inline bool
770 avr_lookup_function_attribute1 (const_tree func, const char *name)
771 {
772 if (FUNCTION_DECL == TREE_CODE (func))
773 {
774 if (NULL_TREE != lookup_attribute (name, DECL_ATTRIBUTES (func)))
775 {
776 return true;
777 }
778
779 func = TREE_TYPE (func);
780 }
781
782 gcc_assert (FUNC_OR_METHOD_TYPE_P (func));
783
784 return NULL_TREE != lookup_attribute (name, TYPE_ATTRIBUTES (func));
785 }
786
787
788 /* Call WORKER on all NAME attributes of function FUNC. */
789
790 static void
791 avr_foreach_function_attribute (tree func, const char *name,
792 void (*worker) (tree, tree, void *),
793 void *cookie)
794 {
795 tree attrs = NULL_TREE;
796
797 if (TREE_CODE (func) == FUNCTION_DECL)
798 attrs = DECL_ATTRIBUTES (func);
799 else if (FUNC_OR_METHOD_TYPE_P (func))
800 attrs = TYPE_ATTRIBUTES (TREE_TYPE (func));
801
802 while (attrs)
803 {
804 attrs = lookup_attribute (name, attrs);
805 if (attrs)
806 {
807 worker (func, attrs, cookie);
808 attrs = TREE_CHAIN (attrs);
809 }
810 }
811 }
812
813
814 /* Return nonzero if FUNC is a naked function. */
815
816 static bool
817 avr_naked_function_p (tree func)
818 {
819 return avr_lookup_function_attribute1 (func, "naked");
820 }
821
822
823 /* Return nonzero if FUNC is a noblock function. */
824
825 static bool
826 avr_noblock_function_p (tree func)
827 {
828 return avr_lookup_function_attribute1 (func, "noblock");
829 }
830
831 /* Return 1 if FUNC is a function that has a "ATTR_NAME" attribute
832 (and perhaps also "ATTR_NAME(num)" attributes. Return -1 if FUNC has
833 "ATTR_NAME(num)" attribute(s) but no "ATTR_NAME" attribute.
834 When no form of ATTR_NAME is present, return 0. */
835
836 static int
837 avr_interrupt_signal_function (tree func, const char *attr_name)
838 {
839 int res = 0;
840
841 avr_foreach_function_attribute (func, attr_name,
842 [] (tree, tree attr, void *cookie)
843 {
844 int *pcook = (int *) cookie;
845
846 *pcook = TREE_VALUE (attr)
847 ? *pcook ? *pcook : -1
848 : 1;
849 }, &res);
850
851 return res;
852 }
853
854
855 /* Return 1 if FUNC is an interrupt function that has an "interrupt" attribute
856 (and perhaps also "interrupt(num)" attributes. Return -1 if FUNC has
857 "interrupt(num)" attribute(s) but no "interrupt" attribute. */
858
859 static int
860 avr_interrupt_function (tree func)
861 {
862 return avr_interrupt_signal_function (func, "interrupt");
863 }
864
865
866 /* Return 1 if FUNC is a signal function that has a "signal" attribute
867 (and perhaps also "signal(num)" attributes. Return -1 if FUNC has
868 "signal(num)" attribute(s) but no "signal" attribute. */
869
870 static int
871 avr_signal_function (tree func)
872 {
873 return avr_interrupt_signal_function (func, "signal");
874 }
875
876
877 /* Return nonzero if FUNC is an OS_task function. */
878
879 static bool
880 avr_OS_task_function_p (tree func)
881 {
882 return avr_lookup_function_attribute1 (func, "OS_task");
883 }
884
885
886 /* Return nonzero if FUNC is an OS_main function. */
887
888 static bool
889 avr_OS_main_function_p (tree func)
890 {
891 return avr_lookup_function_attribute1 (func, "OS_main");
892 }
893
894
895 /* Return nonzero if FUNC is a no_gccisr function as specified
896 by the "no_gccisr" attribute. */
897
898 static bool
899 avr_no_gccisr_function_p (tree func)
900 {
901 return avr_lookup_function_attribute1 (func, "no_gccisr");
902 }
903
904
905 /* Implement `TARGET_CAN_INLINE_P'. */
906 /* Some options like -mgas_isr_prologues depend on optimization level,
907 and the inliner might think that due to different options, inlining
908 is not permitted; see PR104327. */
909
910 static bool
911 avr_can_inline_p (tree /* caller */, tree /* callee */)
912 {
913 // No restrictions whatsoever.
914 return true;
915 }
916
917
918 /* Implement `TARGET_SET_CURRENT_FUNCTION'. */
919 /* Sanity cheching for above function attributes. */
920
921 static void
922 avr_set_current_function (tree decl)
923 {
924 if (decl == NULL_TREE
925 || current_function_decl == NULL_TREE
926 || current_function_decl == error_mark_node
927 || ! cfun->machine)
928 return;
929
930 n_avr_fuse_add_executed = cfun->machine->n_avr_fuse_add_executed;
931
932 if (cfun->machine->attributes_checked_p)
933 return;
934
935 location_t loc = DECL_SOURCE_LOCATION (decl);
936
937 cfun->machine->is_naked = avr_naked_function_p (decl);
938 cfun->machine->is_signal = avr_signal_function (decl);
939 cfun->machine->is_interrupt = avr_interrupt_function (decl);
940 cfun->machine->is_noblock = avr_noblock_function_p (decl);
941 cfun->machine->is_OS_task = avr_OS_task_function_p (decl);
942 cfun->machine->is_OS_main = avr_OS_main_function_p (decl);
943 cfun->machine->is_no_gccisr = avr_no_gccisr_function_p (decl);
944
945 const char *isr = cfun->machine->is_interrupt ? "interrupt" : "signal";
946
947 /* Too much attributes make no sense as they request conflicting features. */
948
949 if (cfun->machine->is_OS_task
950 && (cfun->machine->is_signal || cfun->machine->is_interrupt))
951 error_at (loc, "function attributes %qs and %qs are mutually exclusive",
952 "OS_task", isr);
953
954 if (cfun->machine->is_OS_main
955 && (cfun->machine->is_signal || cfun->machine->is_interrupt))
956 error_at (loc, "function attributes %qs and %qs are mutually exclusive",
957 "OS_main", isr);
958
959 if (cfun->machine->is_interrupt || cfun->machine->is_signal)
960 {
961 tree args = TYPE_ARG_TYPES (TREE_TYPE (decl));
962 tree ret = TREE_TYPE (TREE_TYPE (decl));
963 const char *name;
964
965 name = DECL_ASSEMBLER_NAME_SET_P (decl)
966 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))
967 : IDENTIFIER_POINTER (DECL_NAME (decl));
968
969 /* Skip a leading '*' that might still prefix the assembler name,
970 e.g. in non-LTO runs. */
971
972 name = default_strip_name_encoding (name);
973
974 /* Interrupt handlers must be void __vector (void) functions. */
975
976 if (args && TREE_CODE (TREE_VALUE (args)) != VOID_TYPE)
977 {
978 error_at (loc, "%qs function cannot have arguments", isr);
979 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
980 inform (loc, "method %qs has an implicit %<this%> argument", name);
981 }
982
983 if (TREE_CODE (ret) != VOID_TYPE)
984 error_at (loc, "%qs function cannot return a value", isr);
985
986 #if defined WITH_AVRLIBC
987 /* If the function has the 'signal' or 'interrupt' attribute, ensure
988 that the name of the function is "__vector_NN" so as to catch
989 when the user misspells the vector name. This check is only
990 required when the "interrupt" resp. "signal" attribute does not
991 have an IRQ-number argument. */
992
993 if (!startswith (name, "__vector")
994 && (cfun->machine->is_interrupt == 1
995 || cfun->machine->is_signal == 1))
996 warning_at (loc, OPT_Wmisspelled_isr, "%qs appears to be a misspelled "
997 "%qs handler, missing %<__vector%> prefix", name, isr);
998 #endif // AVR-LibC naming conventions
999 }
1000 else if (cfun->machine->is_noblock)
1001 {
1002 warning (OPT_Wattributes, "%qs attribute ignored on non-ISR function",
1003 "noblock");
1004 }
1005
1006 #if defined WITH_AVRLIBC
1007 // Common problem is using "ISR" without first including avr/interrupt.h.
1008 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1009 name = default_strip_name_encoding (name);
1010 if (strcmp ("ISR", name) == 0)
1011 {
1012 warning_at (loc, OPT_Wmisspelled_isr, "%qs is a reserved identifier"
1013 " in AVR-LibC. Consider %<#include <avr/interrupt.h>%>"
1014 " before using the %qs macro", name, name);
1015 }
1016 if (strcmp ("INTERRUPT", name) == 0
1017 || strcmp ("SIGNAL", name) == 0)
1018 {
1019 warning_at (loc, OPT_Wmisspelled_isr, "%qs is a deprecated identifier"
1020 " in AVR-LibC. Consider %<#include <avr/interrupt.h>%>"
1021 " or %<#include <compat/deprecated.h>%>"
1022 " before using the %qs macro", name, name);
1023 }
1024 #endif // AVR-LibC naming conventions
1025
1026 /* Don't print the above diagnostics more than once. */
1027
1028 cfun->machine->attributes_checked_p = true;
1029 }
1030
1031
1032 /* Implement `ACCUMULATE_OUTGOING_ARGS'. */
1033
1034 int
1035 avr_accumulate_outgoing_args (void)
1036 {
1037 if (!cfun)
1038 return TARGET_ACCUMULATE_OUTGOING_ARGS;
1039
1040 /* FIXME: For setjmp and in avr_builtin_setjmp_frame_value we don't know
1041 what offset is correct. In some cases it is relative to
1042 virtual_outgoing_args_rtx and in others it is relative to
1043 virtual_stack_vars_rtx. For example code see
1044 gcc.c-torture/execute/built-in-setjmp.c
1045 gcc.c-torture/execute/builtins/sprintf-chk.c */
1046
1047 return (TARGET_ACCUMULATE_OUTGOING_ARGS
1048 && !(cfun->calls_setjmp
1049 || cfun->has_nonlocal_label));
1050 }
1051
1052
1053 /* Report contribution of accumulated outgoing arguments to stack size. */
1054
1055 static inline int
1056 avr_outgoing_args_size (void)
1057 {
1058 return (ACCUMULATE_OUTGOING_ARGS
1059 ? (HOST_WIDE_INT) crtl->outgoing_args_size
1060 : 0);
1061 }
1062
1063
1064 /* Implement `TARGET_STARTING_FRAME_OFFSET'. */
1065 /* This is the offset from the frame pointer register to the first stack slot
1066 that contains a variable living in the frame. */
1067
1068 static HOST_WIDE_INT
1069 avr_starting_frame_offset (void)
1070 {
1071 return 1 + avr_outgoing_args_size ();
1072 }
1073
1074
1075 /* Return the number of hard registers to push/pop in the prologue/epilogue
1076 of the current function, and optionally store these registers in SET. */
1077
1078 static int
1079 avr_regs_to_save (HARD_REG_SET *set)
1080 {
1081 int count = 0;
1082 bool int_or_sig_p = cfun->machine->is_interrupt || cfun->machine->is_signal;
1083
1084 if (set)
1085 CLEAR_HARD_REG_SET (*set);
1086
1087 /* No need to save any registers if the function never returns or
1088 has the "OS_task" or "OS_main" attribute. */
1089
1090 if (TREE_THIS_VOLATILE (current_function_decl)
1091 || cfun->machine->is_OS_task
1092 || cfun->machine->is_OS_main)
1093 return 0;
1094
1095 for (int reg = REG_0; reg < REG_32; reg++)
1096 {
1097 /* Do not push/pop __tmp_reg__, __zero_reg__, as well as
1098 any global register variables. */
1099
1100 if (fixed_regs[reg])
1101 continue;
1102
1103 if ((int_or_sig_p && !crtl->is_leaf && call_used_or_fixed_reg_p (reg))
1104 || (df_regs_ever_live_p (reg)
1105 && (int_or_sig_p || !call_used_or_fixed_reg_p (reg))
1106 /* Don't record frame pointer registers here. They are treated
1107 individually in the prologue. */
1108 && !(frame_pointer_needed
1109 && (reg == REG_Y || reg == REG_Y + 1))))
1110 {
1111 if (set)
1112 SET_HARD_REG_BIT (*set, reg);
1113 count++;
1114 }
1115 }
1116 return count;
1117 }
1118
1119
1120 /* Implement `TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS' */
1121
1122 static bool
1123 avr_allocate_stack_slots_for_args (void)
1124 {
1125 return !cfun->machine->is_naked;
1126 }
1127
1128
1129 /* Implement `TARGET_CAN_ELIMINATE'. */
1130 /* Return true if register FROM can be eliminated via register TO. */
1131
1132 static bool
1133 avr_can_eliminate (const int /*from*/, const int to)
1134 {
1135 return ((frame_pointer_needed && to == FRAME_POINTER_REGNUM)
1136 || !frame_pointer_needed);
1137 }
1138
1139
1140 /* Implement `TARGET_WARN_FUNC_RETURN'. */
1141
1142 static bool
1143 avr_warn_func_return (tree decl)
1144 {
1145 /* Naked functions are implemented entirely in assembly, including the
1146 return sequence, so suppress warnings about this. */
1147
1148 return !avr_naked_function_p (decl);
1149 }
1150
1151
1152 /* Worker function for `INITIAL_ELIMINATION_OFFSET'. */
1153 /* Compute offset between arg_pointer and frame_pointer. */
1154
1155 int
1156 avr_initial_elimination_offset (int from, int to)
1157 {
1158 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1159 return 0;
1160 else
1161 {
1162 int offset = frame_pointer_needed ? 2 : 0;
1163 int avr_pc_size = AVR_HAVE_EIJMP_EICALL ? 3 : 2;
1164
1165 // If FROM is ARG_POINTER_REGNUM, we are not in an ISR as ISRs
1166 // might not have arguments. Hence the following is not affected
1167 // by gasisr prologues.
1168 offset += avr_regs_to_save (nullptr);
1169 return (get_frame_size () + avr_outgoing_args_size ()
1170 + avr_pc_size + 1 + offset);
1171 }
1172 }
1173
1174
1175 /* Helper for the function below. */
1176
1177 static void
1178 avr_adjust_type_node (tree *node, machine_mode mode, int sat_p)
1179 {
1180 *node = make_node (FIXED_POINT_TYPE);
1181 TYPE_SATURATING (*node) = sat_p;
1182 TYPE_UNSIGNED (*node) = UNSIGNED_FIXED_POINT_MODE_P (mode);
1183 TYPE_IBIT (*node) = GET_MODE_IBIT (mode);
1184 TYPE_FBIT (*node) = GET_MODE_FBIT (mode);
1185 TYPE_PRECISION (*node) = GET_MODE_BITSIZE (mode);
1186 SET_TYPE_ALIGN (*node, 8);
1187 SET_TYPE_MODE (*node, mode);
1188
1189 layout_type (*node);
1190 }
1191
1192
1193 /* Implement `TARGET_BUILD_BUILTIN_VA_LIST'. */
1194
1195 static tree
1196 avr_build_builtin_va_list (void)
1197 {
1198 /* avr-modes.def adjusts [U]TA to be 64-bit modes with 48 fractional bits.
1199 This is more appropriate for the 8-bit machine AVR than 128-bit modes.
1200 The ADJUST_IBIT/FBIT are handled in toplev:init_adjust_machine_modes()
1201 which is auto-generated by genmodes, but the compiler assigns [U]DAmode
1202 to the long long accum modes instead of the desired [U]TAmode.
1203
1204 Fix this now, right after node setup in tree.cc:build_common_tree_nodes().
1205 This must run before c-cppbuiltin.cc:builtin_define_fixed_point_constants()
1206 which built-in defines macros like __ULLACCUM_FBIT__ that are used by
1207 libgcc to detect IBIT and FBIT. */
1208
1209 avr_adjust_type_node (&ta_type_node, TAmode, 0);
1210 avr_adjust_type_node (&uta_type_node, UTAmode, 0);
1211 avr_adjust_type_node (&sat_ta_type_node, TAmode, 1);
1212 avr_adjust_type_node (&sat_uta_type_node, UTAmode, 1);
1213
1214 unsigned_long_long_accum_type_node = uta_type_node;
1215 long_long_accum_type_node = ta_type_node;
1216 sat_unsigned_long_long_accum_type_node = sat_uta_type_node;
1217 sat_long_long_accum_type_node = sat_ta_type_node;
1218
1219 /* Dispatch to the default handler. */
1220
1221 return std_build_builtin_va_list ();
1222 }
1223
1224
1225 /* Worker function for `INCOMING_RETURN_ADDR_RTX'. */
1226 /* Return contents of MEM at frame pointer + stack size + 1 (+2 if 3-byte PC).
1227 This is the return address of the function. */
1228
1229 rtx
1230 avr_return_addr_rtx (int count, rtx tem)
1231 {
1232 rtx r;
1233
1234 /* Can only return this function's return address. Others not supported. */
1235 if (count)
1236 return NULL_RTX;
1237
1238 if (AVR_3_BYTE_PC)
1239 {
1240 r = gen_rtx_SYMBOL_REF (Pmode, ".L__stack_usage+2");
1241 warning (0, "%<builtin_return_address%> contains only 2 bytes"
1242 " of address");
1243 }
1244 else
1245 r = gen_rtx_SYMBOL_REF (Pmode, ".L__stack_usage+1");
1246
1247 cfun->machine->use_L__stack_usage = true;
1248
1249 r = gen_rtx_PLUS (Pmode, tem, r);
1250 r = gen_frame_mem (Pmode, memory_address (Pmode, r));
1251 r = gen_rtx_ROTATE (HImode, r, GEN_INT (8));
1252 return r;
1253 }
1254
1255
1256 /* Return 1 if the function epilogue is just a single "ret". */
1257
1258 int
1259 avr_simple_epilogue (void)
1260 {
1261 return (! frame_pointer_needed
1262 && get_frame_size () == 0
1263 && avr_outgoing_args_size () == 0
1264 && avr_regs_to_save (nullptr) == 0
1265 && ! cfun->machine->is_interrupt
1266 && ! cfun->machine->is_signal
1267 && ! cfun->machine->is_naked
1268 && ! TREE_THIS_VOLATILE (current_function_decl));
1269 }
1270
1271
1272 /* This function checks sequence of live registers. */
1273
1274 static int
1275 sequent_regs_live (void)
1276 {
1277 int live_seq = 0;
1278 int cur_seq = 0;
1279
1280 for (int reg = 0; reg <= LAST_CALLEE_SAVED_REG; ++reg)
1281 {
1282 if (fixed_regs[reg])
1283 {
1284 /* Don't recognize sequences that contain global register
1285 variables. */
1286
1287 if (live_seq != 0)
1288 return 0;
1289 else
1290 continue;
1291 }
1292
1293 if (!call_used_or_fixed_reg_p (reg))
1294 {
1295 if (df_regs_ever_live_p (reg))
1296 {
1297 ++live_seq;
1298 ++cur_seq;
1299 }
1300 else
1301 cur_seq = 0;
1302 }
1303 }
1304
1305 if (!frame_pointer_needed)
1306 {
1307 if (df_regs_ever_live_p (REG_Y))
1308 {
1309 ++live_seq;
1310 ++cur_seq;
1311 }
1312 else
1313 cur_seq = 0;
1314
1315 if (df_regs_ever_live_p (REG_Y + 1))
1316 {
1317 ++live_seq;
1318 ++cur_seq;
1319 }
1320 else
1321 cur_seq = 0;
1322 }
1323 else
1324 {
1325 cur_seq += 2;
1326 live_seq += 2;
1327 }
1328 return (cur_seq == live_seq) ? live_seq : 0;
1329 }
1330
1331
1332 /* Obtain the length sequence of insns. */
1333
1334 static int
1335 get_sequence_length (rtx_insn *insns)
1336 {
1337 int length = 0;
1338
1339 for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
1340 length += get_attr_length (insn);
1341
1342 return length;
1343 }
1344
1345
1346 /* Implement `INCOMING_RETURN_ADDR_RTX'. */
1347
1348 rtx
1349 avr_incoming_return_addr_rtx (void)
1350 {
1351 /* The return address is at the top of the stack. Note that the push
1352 was via post-decrement, which means the actual address is off by one. */
1353 return gen_frame_mem (HImode, plus_constant (Pmode, stack_pointer_rtx, 1));
1354 }
1355
1356
1357 /* Unset a bit in *SET. If successful, return the respective bit number.
1358 Otherwise, return -1 and *SET is unaltered. */
1359
1360 static int
1361 avr_hregs_split_reg (HARD_REG_SET *set)
1362 {
1363 for (int regno = REG_0; regno < REG_32; regno++)
1364 if (TEST_HARD_REG_BIT (*set, regno))
1365 {
1366 // Don't remove a register from *SET which might indicate that
1367 // some RAMP* register might need ISR prologue / epilogue treatment.
1368
1369 if (AVR_HAVE_RAMPX
1370 && (REG_X == regno || REG_X + 1 == regno)
1371 && TEST_HARD_REG_BIT (*set, REG_X)
1372 && TEST_HARD_REG_BIT (*set, REG_X + 1))
1373 continue;
1374
1375 if (AVR_HAVE_RAMPY
1376 && !frame_pointer_needed
1377 && (REG_Y == regno || REG_Y + 1 == regno)
1378 && TEST_HARD_REG_BIT (*set, REG_Y)
1379 && TEST_HARD_REG_BIT (*set, REG_Y + 1))
1380 continue;
1381
1382 if (AVR_HAVE_RAMPZ
1383 && (REG_Z == regno || REG_Z + 1 == regno)
1384 && TEST_HARD_REG_BIT (*set, REG_Z)
1385 && TEST_HARD_REG_BIT (*set, REG_Z + 1))
1386 continue;
1387
1388 CLEAR_HARD_REG_BIT (*set, regno);
1389 return regno;
1390 }
1391
1392 return -1;
1393 }
1394
1395
1396 /* Helper for expand_prologue. Emit a push of a byte register. */
1397
1398 static void
1399 emit_push_byte (unsigned regno, bool frame_related_p)
1400 {
1401 rtx mem, reg;
1402 rtx_insn *insn;
1403
1404 mem = gen_rtx_POST_DEC (HImode, stack_pointer_rtx);
1405 mem = gen_frame_mem (QImode, mem);
1406 reg = gen_rtx_REG (QImode, regno);
1407
1408 insn = emit_insn (gen_rtx_SET (mem, reg));
1409 if (frame_related_p)
1410 RTX_FRAME_RELATED_P (insn) = 1;
1411
1412 cfun->machine->stack_usage++;
1413 }
1414
1415
1416 /* Helper for expand_prologue. Emit a push of a SFR via register TREG.
1417 SFR is a MEM representing the memory location of the SFR.
1418 If CLR_P then clear the SFR after the push using zero_reg. */
1419
1420 static void
1421 emit_push_sfr (rtx sfr, bool frame_related_p, bool clr_p, int treg)
1422 {
1423 gcc_assert (MEM_P (sfr));
1424
1425 /* IN treg, IO(SFR) */
1426 rtx_insn *insn = emit_move_insn (all_regs_rtx[treg], sfr);
1427 if (frame_related_p)
1428 RTX_FRAME_RELATED_P (insn) = 1;
1429
1430 /* PUSH treg */
1431 emit_push_byte (treg, frame_related_p);
1432
1433 if (clr_p)
1434 {
1435 /* OUT IO(SFR), __zero_reg__ */
1436 insn = emit_move_insn (sfr, const0_rtx);
1437 if (frame_related_p)
1438 RTX_FRAME_RELATED_P (insn) = 1;
1439 }
1440 }
1441
1442 static void
1443 avr_prologue_setup_frame (HOST_WIDE_INT size, HARD_REG_SET set)
1444 {
1445 rtx_insn *insn;
1446 bool isr_p = cfun->machine->is_interrupt || cfun->machine->is_signal;
1447 int live_seq = sequent_regs_live ();
1448
1449 HOST_WIDE_INT size_max
1450 = (HOST_WIDE_INT) GET_MODE_MASK (AVR_HAVE_8BIT_SP ? QImode : Pmode);
1451
1452 bool minimize = (TARGET_CALL_PROLOGUES
1453 && size < size_max
1454 && live_seq
1455 && !isr_p
1456 && !cfun->machine->is_OS_task
1457 && !cfun->machine->is_OS_main
1458 && !AVR_TINY);
1459
1460 if (minimize
1461 && (frame_pointer_needed
1462 || avr_outgoing_args_size () > 8
1463 || (AVR_2_BYTE_PC && live_seq > 6)
1464 || live_seq > 7))
1465 {
1466 rtx pattern;
1467 int reg, offset;
1468
1469 emit_move_insn (gen_rtx_REG (HImode, REG_X),
1470 gen_int_mode (size, HImode));
1471
1472 pattern = gen_call_prologue_saves (gen_int_mode (live_seq, HImode),
1473 gen_int_mode (live_seq+size, HImode));
1474 insn = emit_insn (pattern);
1475 RTX_FRAME_RELATED_P (insn) = 1;
1476
1477 /* Describe the effect of the unspec_volatile call to prologue_saves.
1478 Note that this formulation assumes that add_reg_note pushes the
1479 notes to the front. Thus we build them in the reverse order of
1480 how we want dwarf2out to process them. */
1481
1482 /* The function does always set frame_pointer_rtx, but whether that
1483 is going to be permanent in the function is frame_pointer_needed. */
1484
1485 add_reg_note (insn, REG_CFA_ADJUST_CFA,
1486 gen_rtx_SET ((frame_pointer_needed
1487 ? frame_pointer_rtx
1488 : stack_pointer_rtx),
1489 plus_constant (Pmode, stack_pointer_rtx,
1490 -(size + live_seq))));
1491
1492 /* Note that live_seq always contains r28+r29, but the other
1493 registers to be saved are all below 18. */
1494
1495 int first_reg = (LAST_CALLEE_SAVED_REG + 1) - (live_seq - 2);
1496
1497 for (reg = REG_29, offset = -live_seq + 1;
1498 reg >= first_reg;
1499 reg = (reg == REG_28 ? LAST_CALLEE_SAVED_REG : reg - 1), ++offset)
1500 {
1501 rtx m = gen_rtx_MEM (QImode, plus_constant (Pmode, stack_pointer_rtx,
1502 offset));
1503 rtx r = gen_rtx_REG (QImode, reg);
1504 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (m, r));
1505 }
1506
1507 cfun->machine->stack_usage += size + live_seq;
1508 }
1509 else /* !minimize */
1510 {
1511 for (int reg = REG_0; reg < REG_32; ++reg)
1512 if (TEST_HARD_REG_BIT (set, reg))
1513 emit_push_byte (reg, true);
1514
1515 if (frame_pointer_needed
1516 && (!(cfun->machine->is_OS_task || cfun->machine->is_OS_main)))
1517 {
1518 /* Push frame pointer. Always be consistent about the
1519 ordering of pushes -- epilogue_restores expects the
1520 register pair to be pushed low byte first. */
1521
1522 emit_push_byte (REG_Y, true);
1523 emit_push_byte (REG_Y + 1, true);
1524 }
1525
1526 if (frame_pointer_needed
1527 && size == 0)
1528 {
1529 insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1530 RTX_FRAME_RELATED_P (insn) = 1;
1531 }
1532
1533 if (size != 0)
1534 {
1535 /* Creating a frame can be done by direct manipulation of the
1536 stack or via the frame pointer. These two methods are:
1537 fp = sp
1538 fp -= size
1539 sp = fp
1540 or
1541 sp -= size
1542 fp = sp (*)
1543 the optimal method depends on the function type, stack and
1544 frame size. To avoid a complex logic, both methods are
1545 tested and the shortest one is selected.
1546
1547 There is also the case where SIZE != 0 and no frame pointer is
1548 needed; this can occur if ACCUMULATE_OUTGOING_ARGS is on.
1549 In that case, insn (*) is not needed.
1550 We use the X register as scratch. This is save because in X
1551 is call-clobbered.
1552 In an interrupt routine, the case of SIZE != 0 together with
1553 !frame_pointer_needed can only occur if the function is not a
1554 leaf function and thus X has already been saved. */
1555
1556 int irq_state = -1;
1557 HOST_WIDE_INT size_cfa = size, neg_size;
1558 rtx_insn *fp_plus_insns;
1559
1560 gcc_assert (frame_pointer_needed
1561 || !isr_p
1562 || !crtl->is_leaf);
1563
1564 rtx my_fp = (frame_pointer_needed
1565 ? frame_pointer_rtx
1566 : gen_rtx_REG (Pmode, REG_X));
1567 rtx fp = my_fp;
1568
1569 if (AVR_HAVE_8BIT_SP)
1570 {
1571 /* The high byte (r29) does not change:
1572 Prefer SUBI (1 cycle) over SBIW (2 cycles, same size). */
1573
1574 my_fp = all_regs_rtx[FRAME_POINTER_REGNUM];
1575 }
1576
1577 /* Cut down size and avoid size = 0 so that we don't run
1578 into ICE like PR52488 in the remainder. */
1579
1580 if (size > size_max)
1581 {
1582 /* Don't error so that insane code from newlib still compiles
1583 and does not break building newlib. As PR51345 is implemented
1584 now, there are multilib variants with -msp8.
1585
1586 If user wants sanity checks he can use -Wstack-usage=
1587 or similar options.
1588
1589 For CFA we emit the original, non-saturated size so that
1590 the generic machinery is aware of the real stack usage and
1591 will print the above diagnostic as expected. */
1592
1593 size = size_max;
1594 }
1595
1596 size = trunc_int_for_mode (size, GET_MODE (my_fp));
1597 neg_size = trunc_int_for_mode (-size, GET_MODE (my_fp));
1598
1599 /************ Method 1: Adjust frame pointer ************/
1600
1601 start_sequence ();
1602
1603 /* Normally, the dwarf2out frame-related-expr interpreter does
1604 not expect to have the CFA change once the frame pointer is
1605 set up. Thus, we avoid marking the move insn below and
1606 instead indicate that the entire operation is complete after
1607 the frame pointer subtraction is done. */
1608
1609 insn = emit_move_insn (fp, stack_pointer_rtx);
1610 if (frame_pointer_needed)
1611 {
1612 RTX_FRAME_RELATED_P (insn) = 1;
1613 add_reg_note (insn, REG_CFA_ADJUST_CFA,
1614 gen_rtx_SET (fp, stack_pointer_rtx));
1615 }
1616
1617 insn = emit_move_insn (my_fp, plus_constant (GET_MODE (my_fp),
1618 my_fp, neg_size));
1619
1620 if (frame_pointer_needed)
1621 {
1622 RTX_FRAME_RELATED_P (insn) = 1;
1623 add_reg_note (insn, REG_CFA_ADJUST_CFA,
1624 gen_rtx_SET (fp, plus_constant (Pmode, fp,
1625 -size_cfa)));
1626 }
1627
1628 /* Copy to stack pointer. Note that since we've already
1629 changed the CFA to the frame pointer this operation
1630 need not be annotated if frame pointer is needed.
1631 Always move through unspec, see PR50063.
1632 For meaning of irq_state see movhi_sp_r insn. */
1633
1634 if (cfun->machine->is_interrupt
1635 || (cfun->machine->is_signal
1636 && cfun->machine->is_noblock))
1637 irq_state = 1;
1638
1639 if (TARGET_NO_INTERRUPTS
1640 || (cfun->machine->is_signal
1641 && ! cfun->machine->is_noblock)
1642 || cfun->machine->is_OS_main)
1643 irq_state = 0;
1644
1645 if (AVR_HAVE_8BIT_SP)
1646 irq_state = 2;
1647
1648 insn = emit_insn (gen_movhi_sp_r (stack_pointer_rtx,
1649 fp, GEN_INT (irq_state)));
1650 if (!frame_pointer_needed)
1651 {
1652 RTX_FRAME_RELATED_P (insn) = 1;
1653 add_reg_note (insn, REG_CFA_ADJUST_CFA,
1654 gen_rtx_SET (stack_pointer_rtx,
1655 plus_constant (Pmode,
1656 stack_pointer_rtx,
1657 -size_cfa)));
1658 }
1659
1660 fp_plus_insns = get_insns ();
1661 end_sequence ();
1662
1663 /************ Method 2: Adjust Stack pointer ************/
1664
1665 /* Stack adjustment by means of RCALL . and/or PUSH __TMP_REG__
1666 can only handle specific offsets. */
1667
1668 int n_rcall = size / (AVR_3_BYTE_PC ? 3 : 2);
1669
1670 if (avr_sp_immediate_operand (gen_int_mode (-size, HImode), HImode)
1671 // Don't use more than 3 RCALLs.
1672 && n_rcall <= 3)
1673 {
1674 rtx_insn *sp_plus_insns;
1675
1676 start_sequence ();
1677
1678 insn = emit_move_insn (stack_pointer_rtx,
1679 plus_constant (Pmode, stack_pointer_rtx,
1680 -size));
1681 RTX_FRAME_RELATED_P (insn) = 1;
1682 add_reg_note (insn, REG_CFA_ADJUST_CFA,
1683 gen_rtx_SET (stack_pointer_rtx,
1684 plus_constant (Pmode,
1685 stack_pointer_rtx,
1686 -size_cfa)));
1687 if (frame_pointer_needed)
1688 {
1689 insn = emit_move_insn (fp, stack_pointer_rtx);
1690 RTX_FRAME_RELATED_P (insn) = 1;
1691 }
1692
1693 sp_plus_insns = get_insns ();
1694 end_sequence ();
1695
1696 /************ Use shortest method ************/
1697
1698 emit_insn (get_sequence_length (sp_plus_insns)
1699 < get_sequence_length (fp_plus_insns)
1700 ? sp_plus_insns
1701 : fp_plus_insns);
1702 }
1703 else
1704 {
1705 emit_insn (fp_plus_insns);
1706 }
1707
1708 cfun->machine->stack_usage += size_cfa;
1709 } /* !minimize && size != 0 */
1710 } /* !minimize */
1711 }
1712
1713
1714 /* Output function prologue. */
1715
1716 void
1717 avr_expand_prologue (void)
1718 {
1719 HARD_REG_SET set;
1720 HOST_WIDE_INT size = get_frame_size () + avr_outgoing_args_size ();
1721
1722 cfun->machine->stack_usage = 0;
1723
1724 /* Prologue: naked. */
1725 if (cfun->machine->is_naked)
1726 {
1727 return;
1728 }
1729
1730 avr_regs_to_save (&set);
1731
1732 if (cfun->machine->is_interrupt || cfun->machine->is_signal)
1733 {
1734 int treg = AVR_TMP_REGNO;
1735 /* Enable interrupts. */
1736 if (cfun->machine->is_interrupt
1737 || cfun->machine->is_noblock)
1738 emit_insn (gen_enable_interrupt ());
1739
1740 if (cfun->machine->gasisr.maybe)
1741 {
1742 /* Let GAS PR21472 emit prologue preamble for us which handles SREG,
1743 ZERO_REG and TMP_REG and one additional, optional register for
1744 us in an optimal way. This even scans through inline asm. */
1745
1746 cfun->machine->gasisr.yes = true;
1747
1748 // The optional reg or TMP_REG if we don't need one. If we need one,
1749 // remove that reg from SET so that it's not puhed / popped twice.
1750 // We also use it below instead of TMP_REG in some places.
1751
1752 treg = avr_hregs_split_reg (&set);
1753 if (treg < 0)
1754 treg = AVR_TMP_REGNO;
1755 cfun->machine->gasisr.regno = treg;
1756
1757 // The worst case of pushes. The exact number can be inferred
1758 // at assembly time by magic expression __gcc_isr.n_pushed.
1759 cfun->machine->stack_usage += 3 + (treg != AVR_TMP_REGNO);
1760
1761 // Emit a Prologue chunk. Epilogue chunk(s) might follow.
1762 // The final Done chunk is emit by final postscan.
1763 emit_insn (gen_gasisr (GEN_INT (GASISR_Prologue), GEN_INT (treg)));
1764 }
1765 else // !TARGET_GASISR_PROLOGUES: Classic, dumb prologue preamble.
1766 {
1767 /* Push zero reg. */
1768 emit_push_byte (AVR_ZERO_REGNO, true);
1769
1770 /* Push tmp reg. */
1771 emit_push_byte (AVR_TMP_REGNO, true);
1772
1773 /* Push SREG. */
1774 /* ??? There's no dwarf2 column reserved for SREG. */
1775 emit_push_sfr (sreg_rtx, false, false /* clr */, AVR_TMP_REGNO);
1776
1777 /* Clear zero reg. */
1778 emit_move_insn (zero_reg_rtx, const0_rtx);
1779
1780 /* Prevent any attempt to delete the setting of ZERO_REG! */
1781 emit_use (zero_reg_rtx);
1782 }
1783
1784 /* Push and clear RAMPD/X/Y/Z if present and low-part register is used.
1785 ??? There are no dwarf2 columns reserved for RAMPD/X/Y/Z. */
1786
1787 if (AVR_HAVE_RAMPD)
1788 emit_push_sfr (rampd_rtx, false /* frame */, true /* clr */, treg);
1789
1790 if (AVR_HAVE_RAMPX
1791 && TEST_HARD_REG_BIT (set, REG_X)
1792 && TEST_HARD_REG_BIT (set, REG_X + 1))
1793 {
1794 emit_push_sfr (rampx_rtx, false /* frame */, true /* clr */, treg);
1795 }
1796
1797 if (AVR_HAVE_RAMPY
1798 && (frame_pointer_needed
1799 || (TEST_HARD_REG_BIT (set, REG_Y)
1800 && TEST_HARD_REG_BIT (set, REG_Y + 1))))
1801 {
1802 emit_push_sfr (rampy_rtx, false /* frame */, true /* clr */, treg);
1803 }
1804
1805 if (AVR_HAVE_RAMPZ
1806 && TEST_HARD_REG_BIT (set, REG_Z)
1807 && TEST_HARD_REG_BIT (set, REG_Z + 1))
1808 {
1809 emit_push_sfr (rampz_rtx, false /* frame */, AVR_HAVE_RAMPD, treg);
1810 }
1811 } /* is_interrupt is_signal */
1812
1813 avr_prologue_setup_frame (size, set);
1814
1815 if (flag_stack_usage_info)
1816 current_function_static_stack_size
1817 = cfun->machine->stack_usage + INCOMING_FRAME_SP_OFFSET;
1818 }
1819
1820
1821 /* Turn TVAL into an integer that represents an ISR number. When no such
1822 conversion is possible, then return 0. Unfortunately, we don't know
1823 how many IRQs the device actually has. */
1824
1825 static int
1826 avr_isr_number (tree tval)
1827 {
1828 return (TREE_CODE (tval) == INTEGER_CST
1829 && tree_fits_shwi_p (tval)
1830 && tree_to_shwi (tval) > 0)
1831 ? (int) tree_to_shwi (tval)
1832 : 0;
1833 }
1834
1835
1836 struct avr_fun_cookie
1837 {
1838 FILE *file;
1839 const char *name;
1840 };
1841
1842 /* A helper for `avr_declare_function_name' below. When the function has
1843 attributes like signal(N) or interrupt(N), then define __vector_N as
1844 a global alias for the function name. */
1845
1846 static void
1847 avr_asm_isr_alias (tree /*func*/, tree attr, void *pv)
1848 {
1849 avr_fun_cookie *cookie = (avr_fun_cookie *) pv;
1850
1851 for (tree v = TREE_VALUE (attr); v; v = TREE_CHAIN (v))
1852 {
1853 int ival = avr_isr_number (TREE_VALUE (v));
1854
1855 if (ival)
1856 {
1857 fprintf (cookie->file, ".global __vector_%d\n", ival);
1858 fprintf (cookie->file, "__vector_%d = ", ival);
1859 assemble_name (cookie->file, cookie->name);
1860 fprintf (cookie->file, "\n");
1861 }
1862 }
1863 }
1864
1865
1866 /* Worker for `ASM_DECLARE_FUNCTION_NAME'. */
1867
1868 void
1869 avr_declare_function_name (FILE *file, const char *name, tree decl)
1870 {
1871 // Default action from elfos.h.
1872 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
1873 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
1874 ASM_OUTPUT_FUNCTION_LABEL (file, name, decl);
1875
1876 avr_fun_cookie fc = { file, name };
1877
1878 avr_foreach_function_attribute (decl, "signal", avr_asm_isr_alias, &fc);
1879 avr_foreach_function_attribute (decl, "interrupt", avr_asm_isr_alias, &fc);
1880 }
1881
1882
1883 /* Implement `TARGET_ASM_FUNCTION_END_PROLOGUE'. */
1884 /* Output summary at end of function prologue. */
1885
1886 static void
1887 avr_asm_function_end_prologue (FILE *file)
1888 {
1889 if (cfun->machine->is_naked)
1890 {
1891 fputs ("/* prologue: naked */\n", file);
1892 }
1893 else
1894 {
1895 if (cfun->machine->is_interrupt
1896 || (cfun->machine->is_signal
1897 && cfun->machine->is_noblock))
1898 {
1899 fputs ("/* prologue: Interrupt */\n", file);
1900 }
1901 else if (cfun->machine->is_signal)
1902 {
1903 fputs ("/* prologue: Signal */\n", file);
1904 }
1905 else
1906 fputs ("/* prologue: function */\n", file);
1907 }
1908
1909 if (ACCUMULATE_OUTGOING_ARGS)
1910 fprintf (file, "/* outgoing args size = %d */\n",
1911 avr_outgoing_args_size ());
1912
1913 fprintf (file, "/* frame size = " HOST_WIDE_INT_PRINT_DEC " */\n",
1914 (HOST_WIDE_INT) get_frame_size ());
1915
1916 if (!cfun->machine->gasisr.yes)
1917 {
1918 fprintf (file, "/* stack size = %d */\n", cfun->machine->stack_usage);
1919 // Create symbol stack offset so all functions have it. Add 1 to stack
1920 // usage for offset so that SP + .L__stack_offset = return address.
1921 fprintf (file, ".L__stack_usage = %d\n", cfun->machine->stack_usage);
1922 }
1923 else
1924 {
1925 int used_by_gasisr = 3 + (cfun->machine->gasisr.regno != AVR_TMP_REGNO);
1926 int to = cfun->machine->stack_usage;
1927 int from = to - used_by_gasisr;
1928 // Number of pushed regs is only known at assembly-time.
1929 fprintf (file, "/* stack size = %d...%d */\n", from , to);
1930 fprintf (file, ".L__stack_usage = %d + __gcc_isr.n_pushed\n", from);
1931 }
1932 }
1933
1934
1935 /* Worker function for `EPILOGUE_USES'. */
1936
1937 bool
1938 avr_epilogue_uses (int /*regno*/)
1939 {
1940 return (reload_completed
1941 && cfun->machine
1942 && (cfun->machine->is_interrupt || cfun->machine->is_signal));
1943 }
1944
1945
1946 /* Helper for avr_expand_epilogue. Emit a pop of a byte register. */
1947
1948 static void
1949 emit_pop_byte (unsigned regno)
1950 {
1951 rtx mem = gen_rtx_PRE_INC (HImode, stack_pointer_rtx);
1952 mem = gen_frame_mem (QImode, mem);
1953 rtx reg = gen_rtx_REG (QImode, regno);
1954
1955 emit_insn (gen_rtx_SET (reg, mem));
1956 }
1957
1958
1959 /* Output RTL epilogue. */
1960
1961 void
1962 avr_expand_epilogue (bool sibcall_p)
1963 {
1964 HARD_REG_SET set;
1965 bool isr_p = cfun->machine->is_interrupt || cfun->machine->is_signal;
1966
1967 HOST_WIDE_INT size = get_frame_size () + avr_outgoing_args_size ();
1968
1969 /* epilogue: naked */
1970 if (cfun->machine->is_naked)
1971 {
1972 gcc_assert (!sibcall_p);
1973
1974 emit_jump_insn (gen_return ());
1975 return;
1976 }
1977
1978 avr_regs_to_save (&set);
1979 int live_seq = sequent_regs_live ();
1980
1981 bool minimize = (TARGET_CALL_PROLOGUES
1982 && live_seq
1983 && !isr_p
1984 && !cfun->machine->is_OS_task
1985 && !cfun->machine->is_OS_main
1986 && !AVR_TINY);
1987
1988 if (minimize
1989 && (live_seq > 4
1990 || frame_pointer_needed
1991 || size))
1992 {
1993 /* Get rid of frame. */
1994
1995 if (!frame_pointer_needed)
1996 {
1997 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1998 }
1999
2000 if (size)
2001 {
2002 emit_move_insn (frame_pointer_rtx,
2003 plus_constant (Pmode, frame_pointer_rtx, size));
2004 }
2005
2006 emit_insn (gen_epilogue_restores (gen_int_mode (live_seq, HImode)));
2007 return;
2008 }
2009
2010 if (size)
2011 {
2012 /* Try two methods to adjust stack and select shortest. */
2013
2014 int irq_state = -1;
2015
2016 gcc_assert (frame_pointer_needed
2017 || !isr_p
2018 || !crtl->is_leaf);
2019
2020 rtx my_fp = (frame_pointer_needed
2021 ? frame_pointer_rtx
2022 : gen_rtx_REG (Pmode, REG_X));
2023 rtx fp = my_fp;
2024
2025 if (AVR_HAVE_8BIT_SP)
2026 {
2027 /* The high byte (r29) does not change:
2028 Prefer SUBI (1 cycle) over SBIW (2 cycles). */
2029
2030 my_fp = all_regs_rtx[FRAME_POINTER_REGNUM];
2031 }
2032
2033 /* For rationale see comment in prologue generation. */
2034
2035 HOST_WIDE_INT size_max = (HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (my_fp));
2036 if (size > size_max)
2037 size = size_max;
2038 size = trunc_int_for_mode (size, GET_MODE (my_fp));
2039
2040 /********** Method 1: Adjust fp register **********/
2041
2042 start_sequence ();
2043
2044 if (!frame_pointer_needed)
2045 emit_move_insn (fp, stack_pointer_rtx);
2046
2047 emit_move_insn (my_fp, plus_constant (GET_MODE (my_fp), my_fp, size));
2048
2049 /* Copy to stack pointer. */
2050
2051 if (TARGET_NO_INTERRUPTS)
2052 irq_state = 0;
2053
2054 if (AVR_HAVE_8BIT_SP)
2055 irq_state = 2;
2056
2057 emit_insn (gen_movhi_sp_r (stack_pointer_rtx, fp,
2058 GEN_INT (irq_state)));
2059
2060 rtx_insn *fp_plus_insns = get_insns ();
2061 end_sequence ();
2062
2063 /********** Method 2: Adjust Stack pointer **********/
2064
2065 if (avr_sp_immediate_operand (gen_int_mode (size, HImode), HImode))
2066 {
2067 start_sequence ();
2068
2069 emit_move_insn (stack_pointer_rtx,
2070 plus_constant (Pmode, stack_pointer_rtx, size));
2071
2072 rtx_insn *sp_plus_insns = get_insns ();
2073 end_sequence ();
2074
2075 /************ Use shortest method ************/
2076
2077 emit_insn (get_sequence_length (sp_plus_insns)
2078 < get_sequence_length (fp_plus_insns)
2079 ? sp_plus_insns
2080 : fp_plus_insns);
2081 }
2082 else
2083 emit_insn (fp_plus_insns);
2084 } /* size != 0 */
2085
2086 if (frame_pointer_needed
2087 && !(cfun->machine->is_OS_task || cfun->machine->is_OS_main))
2088 {
2089 /* Restore previous frame_pointer. See avr_expand_prologue for
2090 rationale for not using pophi. */
2091
2092 emit_pop_byte (REG_Y + 1);
2093 emit_pop_byte (REG_Y);
2094 }
2095
2096 /* Restore used registers. */
2097
2098 int treg = AVR_TMP_REGNO;
2099
2100 if (isr_p
2101 && cfun->machine->gasisr.yes)
2102 {
2103 treg = cfun->machine->gasisr.regno;
2104 CLEAR_HARD_REG_BIT (set, treg);
2105 }
2106
2107 for (int reg = REG_31; reg >= REG_0; --reg)
2108 if (TEST_HARD_REG_BIT (set, reg))
2109 emit_pop_byte (reg);
2110
2111 if (isr_p)
2112 {
2113 /* Restore RAMPZ/Y/X/D using tmp_reg as scratch.
2114 The conditions to restore them must be tha same as in prologue. */
2115
2116 if (AVR_HAVE_RAMPZ
2117 && TEST_HARD_REG_BIT (set, REG_Z)
2118 && TEST_HARD_REG_BIT (set, REG_Z + 1))
2119 {
2120 emit_pop_byte (treg);
2121 emit_move_insn (rampz_rtx, all_regs_rtx[treg]);
2122 }
2123
2124 if (AVR_HAVE_RAMPY
2125 && (frame_pointer_needed
2126 || (TEST_HARD_REG_BIT (set, REG_Y)
2127 && TEST_HARD_REG_BIT (set, REG_Y + 1))))
2128 {
2129 emit_pop_byte (treg);
2130 emit_move_insn (rampy_rtx, all_regs_rtx[treg]);
2131 }
2132
2133 if (AVR_HAVE_RAMPX
2134 && TEST_HARD_REG_BIT (set, REG_X)
2135 && TEST_HARD_REG_BIT (set, REG_X + 1))
2136 {
2137 emit_pop_byte (treg);
2138 emit_move_insn (rampx_rtx, all_regs_rtx[treg]);
2139 }
2140
2141 if (AVR_HAVE_RAMPD)
2142 {
2143 emit_pop_byte (treg);
2144 emit_move_insn (rampd_rtx, all_regs_rtx[treg]);
2145 }
2146
2147 if (cfun->machine->gasisr.yes)
2148 {
2149 // Emit an Epilogue chunk.
2150 emit_insn (gen_gasisr (GEN_INT (GASISR_Epilogue),
2151 GEN_INT (cfun->machine->gasisr.regno)));
2152 }
2153 else // !TARGET_GASISR_PROLOGUES
2154 {
2155 /* Restore SREG using tmp_reg as scratch. */
2156
2157 emit_pop_byte (AVR_TMP_REGNO);
2158 emit_move_insn (sreg_rtx, tmp_reg_rtx);
2159
2160 /* Restore tmp REG. */
2161 emit_pop_byte (AVR_TMP_REGNO);
2162
2163 /* Restore zero REG. */
2164 emit_pop_byte (AVR_ZERO_REGNO);
2165 }
2166 }
2167
2168 if (!sibcall_p)
2169 emit_jump_insn (gen_return ());
2170 }
2171
2172
2173 /* Implement `TARGET_ASM_FUNCTION_BEGIN_EPILOGUE'. */
2174
2175 static void
2176 avr_asm_function_begin_epilogue (FILE *file)
2177 {
2178 app_disable ();
2179 fprintf (file, "/* epilogue start */\n");
2180 }
2181
2182
2183 /* Implement `TARGET_CANNOT_MODITY_JUMPS_P'. */
2184
2185 static bool
2186 avr_cannot_modify_jumps_p (void)
2187 {
2188 /* Naked functions must not have any instructions after
2189 their epilogue, see PR42240 */
2190
2191 return (reload_completed
2192 && cfun->machine
2193 && cfun->machine->is_naked);
2194 }
2195
2196
2197 /* Implement `TARGET_MODE_DEPENDENT_ADDRESS_P'. */
2198
2199 static bool
2200 avr_mode_dependent_address_p (const_rtx /*addr*/, addr_space_t as)
2201 {
2202 /* FIXME: Non-generic addresses are not mode-dependent in themselves.
2203 This hook just serves to hack around PR rtl-optimization/52543 by
2204 claiming that non-generic addresses were mode-dependent so that
2205 lower-subreg.cc will skip these addresses. lower-subreg.cc sets up fake
2206 RTXes to probe SET and MEM costs and assumes that MEM is always in the
2207 generic address space which is not true. */
2208
2209 return !ADDR_SPACE_GENERIC_P (as);
2210 }
2211
2212
2213 /* Return true if rtx X is a CONST_INT, CONST or SYMBOL_REF
2214 address with the `absdata' variable attribute, i.e. respective
2215 data can be read / written by LDS / STS instruction.
2216 This is used only for AVR_TINY. */
2217
2218 static bool
2219 avr_address_tiny_absdata_p (rtx x, machine_mode mode)
2220 {
2221 if (CONST == GET_CODE (x))
2222 x = XEXP (XEXP (x, 0), 0);
2223
2224 if (SYMBOL_REF_P (x))
2225 return SYMBOL_REF_FLAGS (x) & AVR_SYMBOL_FLAG_TINY_ABSDATA;
2226
2227 if (CONST_INT_P (x)
2228 && IN_RANGE (INTVAL (x), 0, 0xc0 - GET_MODE_SIZE (mode)))
2229 return true;
2230
2231 return false;
2232 }
2233
2234
2235 /* Helper function for `avr_legitimate_address_p'. */
2236
2237 static inline bool
2238 avr_reg_ok_for_addr_p (rtx reg, addr_space_t as,
2239 rtx_code outer_code, bool strict)
2240 {
2241 return (REG_P (reg)
2242 && (avr_regno_mode_code_ok_for_base_p (REGNO (reg), QImode,
2243 as, outer_code, UNKNOWN)
2244 || (!strict
2245 && REGNO (reg) >= FIRST_PSEUDO_REGISTER)));
2246 }
2247
2248
2249 /* Return nonzero if rtx X is a legitimate memory address on the target
2250 machine for a memory operand of mode MODE. */
2251
2252 static bool
2253 avr_legitimate_address_p (machine_mode mode, rtx x, bool strict)
2254 {
2255 bool ok = CONSTANT_ADDRESS_P (x);
2256
2257 switch (GET_CODE (x))
2258 {
2259 case REG:
2260 ok = avr_reg_ok_for_addr_p (x, ADDR_SPACE_GENERIC,
2261 MEM, strict);
2262
2263 if (strict
2264 && GET_MODE_SIZE (mode) > 4
2265 && REG_X == REGNO (x))
2266 {
2267 ok = false;
2268 }
2269 break;
2270
2271 case POST_INC:
2272 case PRE_DEC:
2273 ok = avr_reg_ok_for_addr_p (XEXP (x, 0), ADDR_SPACE_GENERIC,
2274 GET_CODE (x), strict);
2275 break;
2276
2277 case PLUS:
2278 {
2279 rtx reg = XEXP (x, 0);
2280 rtx op1 = XEXP (x, 1);
2281
2282 if (REG_P (reg)
2283 && CONST_INT_P (op1)
2284 && INTVAL (op1) >= 0)
2285 {
2286 bool fit = (IN_RANGE (INTVAL (op1), 0, MAX_LD_OFFSET (mode))
2287 // Reduced Tiny does not support PLUS addressing
2288 // anyway, so we are not restricted to LD offset.
2289 || AVR_TINY);
2290
2291 if (fit)
2292 {
2293 ok = (! strict
2294 || avr_reg_ok_for_addr_p (reg, ADDR_SPACE_GENERIC,
2295 PLUS, strict));
2296
2297 if (reg == frame_pointer_rtx
2298 || reg == arg_pointer_rtx)
2299 {
2300 ok = true;
2301 }
2302 }
2303 else if (frame_pointer_needed
2304 && reg == frame_pointer_rtx)
2305 {
2306 ok = true;
2307 }
2308 }
2309 }
2310 break;
2311
2312 default:
2313 break;
2314 }
2315
2316 if (AVR_TINY
2317 && CONSTANT_ADDRESS_P (x))
2318 {
2319 /* avrtiny's load / store instructions only cover addresses 0..0xbf:
2320 IN / OUT range is 0..0x3f and LDS / STS can access 0x40..0xbf. */
2321
2322 ok = avr_address_tiny_absdata_p (x, mode);
2323 }
2324
2325 if (avr_log.legitimate_address_p)
2326 {
2327 avr_edump ("\n%?: ret=%d, mode=%m strict=%d "
2328 "reload_completed=%d ra_in_progress=%d %s:",
2329 ok, mode, strict, reload_completed, ra_in_progress (),
2330 reg_renumber ? "(reg_renumber)" : "");
2331
2332 if (GET_CODE (x) == PLUS
2333 && REG_P (XEXP (x, 0))
2334 && CONST_INT_P (XEXP (x, 1))
2335 && IN_RANGE (INTVAL (XEXP (x, 1)), 0, MAX_LD_OFFSET (mode))
2336 && reg_renumber)
2337 {
2338 avr_edump ("(r%d ---> r%d)", REGNO (XEXP (x, 0)),
2339 true_regnum (XEXP (x, 0)));
2340 }
2341
2342 avr_edump ("\n%r\n", x);
2343 }
2344
2345 return ok;
2346 }
2347
2348
2349 /* Former implementation of TARGET_LEGITIMIZE_ADDRESS,
2350 now only a helper for avr_addr_space_legitimize_address. */
2351 /* Attempts to replace X with a valid
2352 memory address for an operand of mode MODE */
2353
2354 static rtx
2355 avr_legitimize_address (rtx x, rtx oldx, machine_mode mode)
2356 {
2357 bool big_offset_p = false;
2358
2359 x = oldx;
2360
2361 if (AVR_TINY)
2362 {
2363 if (CONSTANT_ADDRESS_P (x)
2364 && ! avr_address_tiny_absdata_p (x, mode))
2365 {
2366 x = force_reg (Pmode, x);
2367 }
2368 }
2369
2370 if (GET_CODE (oldx) == PLUS
2371 && REG_P (XEXP (oldx, 0)))
2372 {
2373 if (REG_P (XEXP (oldx, 1)))
2374 x = force_reg (GET_MODE (oldx), oldx);
2375 else if (CONST_INT_P (XEXP (oldx, 1)))
2376 {
2377 int offs = INTVAL (XEXP (oldx, 1));
2378 if (frame_pointer_rtx != XEXP (oldx, 0)
2379 && offs > MAX_LD_OFFSET (mode))
2380 {
2381 big_offset_p = true;
2382 x = force_reg (GET_MODE (oldx), oldx);
2383 }
2384 }
2385 }
2386
2387 if (avr_log.legitimize_address)
2388 {
2389 avr_edump ("\n%?: mode=%m\n %r\n", mode, oldx);
2390
2391 if (x != oldx)
2392 avr_edump (" %s --> %r\n", big_offset_p ? "(big offset)" : "", x);
2393 }
2394
2395 return x;
2396 }
2397
2398
2399 /* Implement `LEGITIMIZE_RELOAD_ADDRESS'. */
2400 /* This will allow register R26/27 to be used where it is no worse than normal
2401 base pointers R28/29 or R30/31. For example, if base offset is greater
2402 than 63 bytes or for R++ or --R addressing. */
2403
2404 rtx
2405 avr_legitimize_reload_address (rtx *px, machine_mode mode, int opnum,
2406 int type, int addr_type, int /*ind_levels*/,
2407 rtx (*mk_memloc)(rtx,int))
2408 {
2409 rtx x = *px;
2410
2411 if (avr_log.legitimize_reload_address)
2412 avr_edump ("\n%?:%m %r\n", mode, x);
2413
2414 if (1 && (GET_CODE (x) == POST_INC
2415 || GET_CODE (x) == PRE_DEC))
2416 {
2417 push_reload (XEXP (x, 0), XEXP (x, 0), &XEXP (x, 0), &XEXP (x, 0),
2418 POINTER_REGS, GET_MODE (x), GET_MODE (x), 0, 0,
2419 opnum, RELOAD_OTHER);
2420
2421 if (avr_log.legitimize_reload_address)
2422 avr_edump (" RCLASS.1 = %R\n IN = %r\n OUT = %r\n",
2423 POINTER_REGS, XEXP (x, 0), XEXP (x, 0));
2424
2425 return x;
2426 }
2427
2428 if (GET_CODE (x) == PLUS
2429 && REG_P (XEXP (x, 0))
2430 && reg_equiv_constant (REGNO (XEXP (x, 0))) == 0
2431 && CONST_INT_P (XEXP (x, 1))
2432 && INTVAL (XEXP (x, 1)) >= 1)
2433 {
2434 bool fit = INTVAL (XEXP (x, 1)) <= MAX_LD_OFFSET (mode);
2435
2436 if (fit)
2437 {
2438 if (reg_equiv_address (REGNO (XEXP (x, 0))) != 0)
2439 {
2440 int regno = REGNO (XEXP (x, 0));
2441 rtx mem = mk_memloc (x, regno);
2442
2443 push_reload (XEXP (mem, 0), NULL_RTX, &XEXP (mem, 0), NULL,
2444 POINTER_REGS, Pmode, VOIDmode, 0, 0,
2445 1, (enum reload_type) addr_type);
2446
2447 if (avr_log.legitimize_reload_address)
2448 avr_edump (" RCLASS.2 = %R\n IN = %r\n OUT = %r\n",
2449 POINTER_REGS, XEXP (mem, 0), NULL_RTX);
2450
2451 push_reload (mem, NULL_RTX, &XEXP (x, 0), NULL,
2452 BASE_POINTER_REGS, GET_MODE (x), VOIDmode, 0, 0,
2453 opnum, (enum reload_type) type);
2454
2455 if (avr_log.legitimize_reload_address)
2456 avr_edump (" RCLASS.2 = %R\n IN = %r\n OUT = %r\n",
2457 BASE_POINTER_REGS, mem, NULL_RTX);
2458
2459 return x;
2460 }
2461 }
2462 else if (! (frame_pointer_needed
2463 && XEXP (x, 0) == frame_pointer_rtx))
2464 {
2465 push_reload (x, NULL_RTX, px, NULL,
2466 POINTER_REGS, GET_MODE (x), VOIDmode, 0, 0,
2467 opnum, (enum reload_type) type);
2468
2469 if (avr_log.legitimize_reload_address)
2470 avr_edump (" RCLASS.3 = %R\n IN = %r\n OUT = %r\n",
2471 POINTER_REGS, x, NULL_RTX);
2472
2473 return x;
2474 }
2475 }
2476
2477 return NULL_RTX;
2478 }
2479
2480
2481 /* Helper function to print assembler resp. track instruction
2482 sequence lengths. Always return "".
2483
2484 If PLEN == NULL:
2485 Output assembler code from template TPL with operands supplied
2486 by OPERANDS. This is just forwarding to output_asm_insn.
2487
2488 If PLEN != NULL:
2489 If N_WORDS >= 0 Add N_WORDS to *PLEN.
2490 If N_WORDS < 0 Set *PLEN to -N_WORDS.
2491 Don't output anything.
2492 */
2493
2494 static const char *
2495 avr_asm_len (const char *tpl, rtx *operands, int *plen, int n_words)
2496 {
2497 if (plen == nullptr)
2498 output_asm_insn (tpl, operands);
2499 else
2500 {
2501 if (n_words < 0)
2502 *plen = -n_words;
2503 else
2504 *plen += n_words;
2505 }
2506
2507 return "";
2508 }
2509
2510
2511 /* Return a pointer register name as a string. */
2512
2513 static const char *
2514 ptrreg_to_str (int regno)
2515 {
2516 switch (regno)
2517 {
2518 case REG_X: return "X";
2519 case REG_Y: return "Y";
2520 case REG_Z: return "Z";
2521 default:
2522 output_operand_lossage ("address operand requires constraint for"
2523 " X, Y, or Z register");
2524 }
2525 return nullptr;
2526 }
2527
2528
2529 /* Return the condition name as a string to be used in a BR** instruction.
2530 Used in conditional jump constructing. */
2531
2532 static const char *
2533 avr_cond_string (rtx_code code, bool cc_overflow_unusable)
2534 {
2535 switch (code)
2536 {
2537 case NE:
2538 return "ne";
2539 case EQ:
2540 return "eq";
2541 case GE:
2542 return cc_overflow_unusable ? "pl" : "ge";
2543 case LT:
2544 return cc_overflow_unusable ? "mi" : "lt";
2545 case GEU:
2546 return "sh";
2547 case LTU:
2548 return "lo";
2549 default:
2550 gcc_unreachable ();
2551 }
2552
2553 return "";
2554 }
2555
2556
2557 /* Return true if rtx X is a CONST or SYMBOL_REF with progmem.
2558 This must be used for AVR_TINY only because on other cores
2559 the flash memory is not visible in the RAM address range and
2560 cannot be read by, say, LD instruction. */
2561
2562 static bool
2563 avr_address_tiny_pm_p (rtx x)
2564 {
2565 if (CONST == GET_CODE (x))
2566 x = XEXP (XEXP (x, 0), 0);
2567
2568 if (SYMBOL_REF_P (x))
2569 return SYMBOL_REF_FLAGS (x) & AVR_SYMBOL_FLAG_TINY_PM;
2570
2571 return false;
2572 }
2573
2574
2575 /* Implement `TARGET_PRINT_OPERAND_ADDRESS'. */
2576 /* Output ADDR to FILE as address. */
2577
2578 static void
2579 avr_print_operand_address (FILE *file, machine_mode /*mode*/, rtx addr)
2580 {
2581 if (AVR_TINY
2582 && avr_address_tiny_pm_p (addr))
2583 {
2584 addr = plus_constant (Pmode, addr, avr_arch->flash_pm_offset);
2585 }
2586
2587 switch (GET_CODE (addr))
2588 {
2589 case REG:
2590 fprintf (file, "%s", ptrreg_to_str (REGNO (addr)));
2591 break;
2592
2593 case PRE_DEC:
2594 fprintf (file, "-%s", ptrreg_to_str (REGNO (XEXP (addr, 0))));
2595 break;
2596
2597 case POST_INC:
2598 fprintf (file, "%s+", ptrreg_to_str (REGNO (XEXP (addr, 0))));
2599 break;
2600
2601 default:
2602 if (CONSTANT_ADDRESS_P (addr)
2603 && text_segment_operand (addr, VOIDmode))
2604 {
2605 rtx x = addr;
2606 if (GET_CODE (x) == CONST)
2607 x = XEXP (x, 0);
2608 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
2609 {
2610 /* Assembler gs() will implant word address. Make offset
2611 a byte offset inside gs() for assembler. This is
2612 needed because the more logical (constant+gs(sym)) is not
2613 accepted by gas. For 128K and smaller devices this is ok.
2614 For large devices it will create a trampoline to offset
2615 from symbol which may not be what the user really wanted. */
2616
2617 fprintf (file, "gs(");
2618 output_addr_const (file, XEXP (x, 0));
2619 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC ")",
2620 2 * INTVAL (XEXP (x, 1)));
2621 if (AVR_3_BYTE_PC)
2622 {
2623 location_t loc = avr_insn_location != UNKNOWN_LOCATION
2624 ? avr_insn_location
2625 : input_location;
2626 if (warning_at (loc, 0, "pointer offset from symbol may be"
2627 " incorrect"))
2628 {
2629 output_addr_const (stderr, addr);
2630 fprintf (stderr, "\n");
2631 }
2632 }
2633 }
2634 else
2635 {
2636 fprintf (file, "gs(");
2637 output_addr_const (file, addr);
2638 fprintf (file, ")");
2639 }
2640 }
2641 else
2642 output_addr_const (file, addr);
2643 }
2644 }
2645
2646
2647 /* Implement `TARGET_PRINT_OPERAND_PUNCT_VALID_P'. */
2648
2649 static bool
2650 avr_print_operand_punct_valid_p (unsigned char code)
2651 {
2652 return code == '~' || code == '!';
2653 }
2654
2655
2656 /* Implement `TARGET_PRINT_OPERAND'. */
2657 /* Output X as assembler operand to file FILE.
2658 For a description of supported %-codes, see top of avr.md. */
2659
2660 static void
2661 avr_print_operand (FILE *file, rtx x, int code)
2662 {
2663 int abcd = 0, ef = 0, ij = 0;
2664
2665 if (code >= 'A' && code <= 'D')
2666 abcd = code - 'A';
2667 else if (code == 'E' || code == 'F')
2668 ef = code - 'E';
2669 else if (code == 'I' || code == 'J')
2670 ij = code - 'I';
2671
2672 if (code == '~')
2673 {
2674 if (!AVR_HAVE_JMP_CALL)
2675 fputc ('r', file);
2676 }
2677 else if (code == '!')
2678 {
2679 if (AVR_HAVE_EIJMP_EICALL)
2680 fputc ('e', file);
2681 }
2682 else if (code == 't'
2683 || code == 'T')
2684 {
2685 static int t_regno = -1;
2686 static int t_nbits = -1;
2687
2688 if (REG_P (x) && t_regno < 0 && code == 'T')
2689 {
2690 t_regno = REGNO (x);
2691 t_nbits = GET_MODE_BITSIZE (GET_MODE (x));
2692 }
2693 else if (CONST_INT_P (x) && t_regno >= 0
2694 && IN_RANGE (INTVAL (x), 0, t_nbits - 1))
2695 {
2696 int bpos = INTVAL (x);
2697
2698 fprintf (file, "%s", reg_names[t_regno + bpos / 8]);
2699 if (code == 'T')
2700 fprintf (file, ",%d", bpos % 8);
2701
2702 t_regno = -1;
2703 }
2704 else
2705 fatal_insn ("operands to %T/%t must be reg + const_int:", x);
2706 }
2707 else if (code == 'E' || code == 'F')
2708 {
2709 rtx op = XEXP (x, 0);
2710 fprintf (file, "%s", reg_names[REGNO (op) + ef]);
2711 }
2712 else if (code == 'I' || code == 'J')
2713 {
2714 rtx op = XEXP (XEXP (x, 0), 0);
2715 fprintf (file, "%s", reg_names[REGNO (op) + ij]);
2716 }
2717 else if (code == 'i')
2718 {
2719 const int sfr0 = avr_arch->sfr_offset;
2720 bool lossage_p = false;
2721
2722 switch (GET_CODE (x))
2723 {
2724 default:
2725 lossage_p = true;
2726 break;
2727
2728 case CONST_INT:
2729 {
2730 const auto ival = INTVAL (x);
2731
2732 if (io_address_operand (x, VOIDmode))
2733 {
2734 if (AVR_HAVE_RAMPZ && ival == avr_addr.rampz)
2735 fprintf (file, "__RAMPZ__");
2736 else if (AVR_HAVE_RAMPY && ival == avr_addr.rampy)
2737 fprintf (file, "__RAMPY__");
2738 else if (AVR_HAVE_RAMPX && ival == avr_addr.rampx)
2739 fprintf (file, "__RAMPX__");
2740 else if (AVR_HAVE_RAMPD && ival == avr_addr.rampd)
2741 fprintf (file, "__RAMPD__");
2742 else if ((AVR_XMEGA || AVR_TINY) && ival == avr_addr.ccp)
2743 fprintf (file, "__CCP__");
2744 else if (ival == avr_addr.sreg) fprintf (file, "__SREG__");
2745 else if (ival == avr_addr.sp_l) fprintf (file, "__SP_L__");
2746 else if (ival == avr_addr.sp_h) fprintf (file, "__SP_H__");
2747 else
2748 fprintf (file, HOST_WIDE_INT_PRINT_HEX, ival - sfr0);
2749 }
2750 else
2751 {
2752 char buf[17];
2753 /* Printed indirectly through buffer, as
2754 output_operand_lossage is translatable but uses printf
2755 format strings, so HOST_WIDE_INT_PRINT_HEX_PURE macro can't
2756 be used there to make translation possible and how exactly
2757 can be HOST_WIDE_INT printed is host dependent. */
2758 snprintf (buf, sizeof buf, HOST_WIDE_INT_PRINT_HEX_PURE,
2759 ival);
2760 output_operand_lossage ("bad I/O address 0x%s outside of "
2761 "valid range [0x%x, 0x%x] for %%i "
2762 "operand", buf, sfr0, sfr0 + 0x3f);
2763 }
2764 }
2765 break; // CONST_INT
2766
2767 case MEM:
2768 if (io_address_operand (XEXP (x, 0), VOIDmode))
2769 avr_print_operand (file, XEXP (x, 0), 'i');
2770 else
2771 lossage_p = true;
2772 break;
2773
2774 case SYMBOL_REF:
2775 if (io_address_operand (x, VOIDmode))
2776 {
2777 rtx addr = plus_constant (HImode, x, -sfr0);
2778 avr_print_operand_address (file, VOIDmode, addr);
2779 }
2780 else
2781 lossage_p = true;
2782 break;
2783 } // switch code
2784
2785 if (lossage_p)
2786 output_operand_lossage ("%s operand cannot be used as %%i I/O "
2787 "address operand", rtx_name[GET_CODE (x)]);
2788 } // code = i
2789 else if (REG_P (x))
2790 {
2791 if (x == zero_reg_rtx)
2792 fprintf (file, "__zero_reg__");
2793 else if (code == 'r' && REGNO (x) < REG_32)
2794 fprintf (file, "%d", (int) REGNO (x));
2795 else
2796 fprintf (file, "%s", reg_names[REGNO (x) + abcd]);
2797 }
2798 else if (CONST_INT_P (x))
2799 {
2800 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) + abcd);
2801 }
2802 else if (MEM_P (x))
2803 {
2804 rtx addr = XEXP (x, 0);
2805
2806 if (code == 'm')
2807 {
2808 if (!CONSTANT_P (addr))
2809 fatal_insn ("bad address, not a constant:", addr);
2810 /* Assembler template with m-code is data - not progmem section */
2811 if (text_segment_operand (addr, VOIDmode))
2812 {
2813 location_t loc = avr_insn_location != UNKNOWN_LOCATION
2814 ? avr_insn_location
2815 : input_location;
2816 if (warning_at (loc, 0, "accessing data memory with"
2817 " program memory address"))
2818 {
2819 output_addr_const (stderr, addr);
2820 fprintf (stderr,"\n");
2821 }
2822 }
2823 output_addr_const (file, addr);
2824 }
2825 else if (code == 'o')
2826 {
2827 if (GET_CODE (addr) != PLUS)
2828 fatal_insn ("bad address, not (reg+disp):", addr);
2829
2830 avr_print_operand (file, XEXP (addr, 1), 0);
2831 }
2832 else if (code == 'b')
2833 {
2834 if (GET_CODE (addr) != PLUS)
2835 fatal_insn ("bad address, not (reg+disp):", addr);
2836
2837 avr_print_operand_address (file, VOIDmode, XEXP (addr, 0));
2838 }
2839 else if (code == 'p' || code == 'r')
2840 {
2841 if (GET_CODE (addr) != POST_INC && GET_CODE (addr) != PRE_DEC)
2842 fatal_insn ("bad address, not post_inc or pre_dec:", addr);
2843
2844 if (code == 'p')
2845 /* X, Y, Z */
2846 avr_print_operand_address (file, VOIDmode, XEXP (addr, 0));
2847 else
2848 avr_print_operand (file, XEXP (addr, 0), 0); /* r26, r28, r30 */
2849 }
2850 else if (GET_CODE (addr) == PLUS)
2851 {
2852 avr_print_operand_address (file, VOIDmode, XEXP (addr, 0));
2853 if (REGNO (XEXP (addr, 0)) == REG_X)
2854 fatal_insn ("internal compiler error. Bad address:", addr);
2855 fputc ('+', file);
2856 avr_print_operand (file, XEXP (addr, 1), code);
2857 }
2858 else
2859 avr_print_operand_address (file, VOIDmode, addr);
2860 }
2861 else if (code == 'x')
2862 {
2863 /* Constant progmem address - like used in jmp or call */
2864 if (text_segment_operand (x, VOIDmode) == 0)
2865 {
2866 location_t loc = avr_insn_location != UNKNOWN_LOCATION
2867 ? avr_insn_location
2868 : input_location;
2869 if (warning_at (loc, 0, "accessing program memory"
2870 " with data memory address"))
2871 {
2872 output_addr_const (stderr, x);
2873 fprintf (stderr, "\n");
2874 }
2875 }
2876 /* Use normal symbol for direct address no linker trampoline needed */
2877 output_addr_const (file, x);
2878 }
2879 else if (CONST_FIXED_P (x))
2880 {
2881 HOST_WIDE_INT ival = INTVAL (avr_to_int_mode (x));
2882 if (code != 0)
2883 output_operand_lossage ("Unsupported code '%c' for fixed-point:",
2884 code);
2885 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
2886 }
2887 else if (CONST_DOUBLE_P (x))
2888 {
2889 if (GET_MODE (x) == SFmode)
2890 {
2891 long val;
2892 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
2893 fprintf (file, "0x%lx", val);
2894 }
2895 else if (GET_MODE (x) == DFmode)
2896 {
2897 long l[2];
2898 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), l);
2899 fprintf (file, "0x%lx%08lx", l[1] & 0xffffffff, l[0] & 0xffffffff);
2900 }
2901 else
2902 fatal_insn ("internal compiler error. Unknown mode:", x);
2903 }
2904 else if (GET_CODE (x) == CONST_STRING)
2905 fputs (XSTR (x, 0), file);
2906 else if (code == 'j' || code == 'L')
2907 fputs (avr_cond_string (GET_CODE (x), code == 'L'), file);
2908 else if (code == 'k' || code == 'K')
2909 fputs (avr_cond_string (reverse_condition (GET_CODE (x)), code == 'K'),
2910 file);
2911 else
2912 avr_print_operand_address (file, VOIDmode, x);
2913 }
2914
2915
2916 /* Implement `TARGET_USE_BY_PIECES_INFRASTRUCTURE_P'. */
2917 /* Prefer sequence of loads/stores for moves of size upto
2918 two - two pairs of load/store instructions are always better
2919 than the 5 instruction sequence for a loop (1 instruction
2920 for loop counter setup, and 4 for the body of the loop). */
2921
2922 static bool
2923 avr_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
2924 unsigned int align,
2925 by_pieces_operation op, bool speed_p)
2926 {
2927 if (op != MOVE_BY_PIECES
2928 || (speed_p && size > MOVE_MAX_PIECES))
2929 return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
2930
2931 return size <= MOVE_MAX_PIECES;
2932 }
2933
2934
2935 /* Choose mode for jump insn:
2936 1 - relative jump in range -63 <= x <= 62 ;
2937 2 - relative jump in range -2046 <= x <= 2045 ;
2938 3 - absolute jump (only when we have JMP / CALL).
2939
2940 When jumping backwards, assume the jump offset is EXTRA words
2941 bigger than inferred from insn addresses. */
2942
2943 int
2944 avr_jump_mode (rtx x, rtx_insn *insn, int extra)
2945 {
2946 int dest_addr = INSN_ADDRESSES (INSN_UID (GET_CODE (x) == LABEL_REF
2947 ? XEXP (x, 0) : x));
2948 int cur_addr = INSN_ADDRESSES (INSN_UID (insn));
2949 int jump_distance = cur_addr - dest_addr;
2950
2951 if (IN_RANGE (jump_distance, -63, 62 - extra))
2952 return 1;
2953 else if (IN_RANGE (jump_distance, -2046, 2045 - extra))
2954 return 2;
2955 else if (AVR_HAVE_JMP_CALL)
2956 return 3;
2957
2958 return 2;
2959 }
2960
2961
2962 /* Return the asm code for conditional branch INSN, where XOP[0] is the jump
2963 target label and XOP[1] is a comparison operator of REG_CC against 0. */
2964
2965 const char *
2966 avr_cond_branch (rtx_insn *insn, rtx *xop)
2967 {
2968 machine_mode ccmode = GET_MODE (XEXP (xop[1], 0));
2969 rtx_code cond = GET_CODE (xop[1]);
2970 bool cc_overflow_unusable = ccmode != CCmode;
2971 int len = avr_jump_mode (xop[0], insn);
2972
2973 if (ccmode == CCNmode)
2974 // The N flag can only do < 0 and >= 0.
2975 gcc_assert (cond == GE || cond == LT);
2976
2977 switch (cond)
2978 {
2979 case GT:
2980 if (cc_overflow_unusable)
2981 return (len == 1 ? ("breq .+2" CR_TAB
2982 "brpl %0") :
2983 len == 2 ? ("breq .+4" CR_TAB
2984 "brmi .+2" CR_TAB
2985 "rjmp %0") :
2986 ("breq .+6" CR_TAB
2987 "brmi .+4" CR_TAB
2988 "jmp %0"));
2989
2990 else
2991 return (len == 1 ? ("breq .+2" CR_TAB
2992 "brge %0") :
2993 len == 2 ? ("breq .+4" CR_TAB
2994 "brlt .+2" CR_TAB
2995 "rjmp %0") :
2996 ("breq .+6" CR_TAB
2997 "brlt .+4" CR_TAB
2998 "jmp %0"));
2999 case GTU:
3000 return (len == 1 ? ("breq .+2" CR_TAB
3001 "brsh %0") :
3002 len == 2 ? ("breq .+4" CR_TAB
3003 "brlo .+2" CR_TAB
3004 "rjmp %0") :
3005 ("breq .+6" CR_TAB
3006 "brlo .+4" CR_TAB
3007 "jmp %0"));
3008 case LE:
3009 if (cc_overflow_unusable)
3010 return (len == 1 ? ("breq %0" CR_TAB
3011 "brmi %0") :
3012 len == 2 ? ("breq .+2" CR_TAB
3013 "brpl .+2" CR_TAB
3014 "rjmp %0") :
3015 ("breq .+2" CR_TAB
3016 "brpl .+4" CR_TAB
3017 "jmp %0"));
3018 else
3019 return (len == 1 ? ("breq %0" CR_TAB
3020 "brlt %0") :
3021 len == 2 ? ("breq .+2" CR_TAB
3022 "brge .+2" CR_TAB
3023 "rjmp %0") :
3024 ("breq .+2" CR_TAB
3025 "brge .+4" CR_TAB
3026 "jmp %0"));
3027 case LEU:
3028 return (len == 1 ? ("breq %0" CR_TAB
3029 "brlo %0") :
3030 len == 2 ? ("breq .+2" CR_TAB
3031 "brsh .+2" CR_TAB
3032 "rjmp %0") :
3033 ("breq .+2" CR_TAB
3034 "brsh .+4" CR_TAB
3035 "jmp %0"));
3036 default:
3037 switch (len)
3038 {
3039 case 1:
3040 return cc_overflow_unusable
3041 ? "br%L1 %0"
3042 : "br%j1 %0";
3043 case 2:
3044 return cc_overflow_unusable
3045 ? "br%K1 .+2" CR_TAB "rjmp %0"
3046 : "br%k1 .+2" CR_TAB "rjmp %0";
3047 default:
3048 return cc_overflow_unusable
3049 ? "br%K1 .+4" CR_TAB "jmp %0"
3050 : "br%k1 .+4" CR_TAB "jmp %0";
3051 }
3052 }
3053 return "";
3054 }
3055
3056
3057 /* Worker function for `FINAL_PRESCAN_INSN'. */
3058 /* Output insn cost for next insn. */
3059
3060 void
3061 avr_final_prescan_insn (rtx_insn *insn, rtx * /*operands*/,
3062 int /*num_operands*/)
3063 {
3064 avr_insn_location = LOCATION_LOCUS (INSN_LOCATION (insn));
3065
3066 if (avr_log.rtx_costs)
3067 {
3068 rtx set = single_set (insn);
3069
3070 if (set)
3071 fprintf (asm_out_file, "/* DEBUG: cost = %d. */\n",
3072 set_src_cost (SET_SRC (set), GET_MODE (SET_DEST (set)),
3073 optimize_insn_for_speed_p ()));
3074 else
3075 fprintf (asm_out_file, "/* DEBUG: pattern-cost = %d. */\n",
3076 rtx_cost (PATTERN (insn), VOIDmode, INSN, 0,
3077 optimize_insn_for_speed_p ()));
3078 }
3079
3080 if (avr_log.insn_addresses)
3081 fprintf (asm_out_file, ";; ADDR = %d\n",
3082 (int) INSN_ADDRESSES (INSN_UID (insn)));
3083 }
3084
3085
3086 /* Implement `TARGET_ASM_FINAL_POSTSCAN_INSN'. */
3087 /* When GAS generates (parts of) ISR prologue / epilogue for us, we must
3088 hint GAS about the end of the code to scan. There might be code located
3089 after the last epilogue. */
3090
3091 static void
3092 avr_asm_final_postscan_insn (FILE *stream, rtx_insn *insn, rtx *, int)
3093 {
3094 if (!next_real_insn (insn))
3095 avr_insn_location = UNKNOWN_LOCATION;
3096
3097 if (cfun->machine->gasisr.yes
3098 && !next_real_insn (insn))
3099 {
3100 app_disable ();
3101 fprintf (stream, "\t__gcc_isr %d,r%d\n", GASISR_Done,
3102 cfun->machine->gasisr.regno);
3103 }
3104 }
3105
3106
3107 /* Worker function for `FUNCTION_ARG_REGNO_P'. */
3108 /* Returns nonzero if REGNO is the number of a hard
3109 register in which function arguments are sometimes passed. */
3110
3111 bool
3112 avr_function_arg_regno_p (int r)
3113 {
3114 return AVR_TINY
3115 ? IN_RANGE (r, REG_20, REG_25)
3116 : IN_RANGE (r, REG_8, REG_25);
3117 }
3118
3119
3120 /* Worker function for `INIT_CUMULATIVE_ARGS'. */
3121 /* Initializing the variable cum for the state at the beginning
3122 of the argument list. */
3123
3124 void
3125 avr_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, rtx libname,
3126 tree /*fndecl*/)
3127 {
3128 cum->nregs = AVR_TINY ? 1 + REG_25 - REG_20 : 1 + REG_25 - REG_8;
3129 cum->regno = FIRST_CUM_REG;
3130 cum->has_stack_args = false;
3131 if (!libname && stdarg_p (fntype))
3132 cum->nregs = 0;
3133
3134 /* Assume the calle may be tail called */
3135
3136 cfun->machine->sibcall_fails = false;
3137 }
3138
3139
3140 /* Returns the number of registers to allocate for a function argument. */
3141
3142 static int
3143 avr_num_arg_regs (machine_mode mode, const_tree type)
3144 {
3145 int size = (mode == BLKmode
3146 ? int_size_in_bytes (type)
3147 : GET_MODE_SIZE (mode));
3148
3149 /* Align all function arguments to start in even-numbered registers.
3150 Odd-sized arguments leave holes above them. */
3151
3152 return (size + 1) & ~1;
3153 }
3154
3155
3156 /* Implement `TARGET_FUNCTION_ARG'. */
3157 /* Controls whether a function argument is passed
3158 in a register, and which register. */
3159
3160 static rtx
3161 avr_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
3162 {
3163 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3164 int bytes = avr_num_arg_regs (arg.mode, arg.type);
3165
3166 if (cum->nregs && bytes <= cum->nregs)
3167 return gen_rtx_REG (arg.mode, cum->regno - bytes);
3168
3169 cum->has_stack_args = true;
3170
3171 return NULL_RTX;
3172 }
3173
3174
3175 /* Implement `TARGET_FUNCTION_ARG_ADVANCE'. */
3176 /* Update the summarizer variable CUM to advance past an argument
3177 in the argument list. */
3178
3179 static void
3180 avr_function_arg_advance (cumulative_args_t cum_v, const function_arg_info &arg)
3181 {
3182 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3183 int bytes = avr_num_arg_regs (arg.mode, arg.type);
3184
3185 cum->nregs -= bytes;
3186 cum->regno -= bytes;
3187
3188 /* A parameter is being passed in a call-saved register. As the original
3189 contents of these regs has to be restored before leaving the function,
3190 a function must not pass arguments in call-saved regs in order to get
3191 tail-called. */
3192
3193 if (cum->regno >= REG_8
3194 && cum->nregs >= 0
3195 && !call_used_or_fixed_reg_p (cum->regno))
3196 {
3197 /* FIXME: We ship info on failing tail-call in struct machine_function.
3198 This uses internals of calls.cc:expand_call() and the way args_so_far
3199 is used. targetm.function_ok_for_sibcall() needs to be extended to
3200 pass &args_so_far, too. At present, CUMULATIVE_ARGS is target
3201 dependent so that such an extension is not wanted. */
3202
3203 cfun->machine->sibcall_fails = true;
3204 }
3205
3206 /* Test if all registers needed by the ABI are actually available. If the
3207 user has fixed a GPR needed to pass an argument, an (implicit) function
3208 call will clobber that fixed register. See PR45099 for an example. */
3209
3210 if (cum->regno >= REG_8
3211 && cum->nregs >= 0)
3212 {
3213 for (int regno = cum->regno; regno < cum->regno + bytes; regno++)
3214 if (fixed_regs[regno])
3215 warning (0, "fixed register %s used to pass parameter to function",
3216 reg_names[regno]);
3217 }
3218
3219 if (cum->nregs <= 0)
3220 {
3221 cum->nregs = 0;
3222 cum->regno = FIRST_CUM_REG;
3223 }
3224 }
3225
3226
3227 /* Implement `TARGET_FUNCTION_OK_FOR_SIBCALL' */
3228 /* Decide whether we can make a sibling call to a function. DECL is the
3229 declaration of the function being targeted by the call and EXP is the
3230 CALL_EXPR representing the call. */
3231
3232 static bool
3233 avr_function_ok_for_sibcall (tree decl_callee, tree exp_callee)
3234 {
3235 /* Tail-calling must fail if callee-saved regs are used to pass
3236 function args. We must not tail-call when `epilogue_restores'
3237 is used. Unfortunately, we cannot tell at this point if that
3238 actually will happen or not, and we cannot step back from
3239 tail-calling. Thus, we inhibit tail-calling with -mcall-prologues. */
3240
3241 if (cfun->machine->sibcall_fails
3242 || TARGET_CALL_PROLOGUES)
3243 {
3244 return false;
3245 }
3246
3247 tree fntype_callee = TREE_TYPE (CALL_EXPR_FN (exp_callee));
3248
3249 if (decl_callee)
3250 {
3251 decl_callee = TREE_TYPE (decl_callee);
3252 }
3253 else
3254 {
3255 decl_callee = fntype_callee;
3256
3257 while (FUNCTION_TYPE != TREE_CODE (decl_callee)
3258 && METHOD_TYPE != TREE_CODE (decl_callee))
3259 {
3260 decl_callee = TREE_TYPE (decl_callee);
3261 }
3262 }
3263
3264 /* Ensure that caller and callee have compatible epilogues */
3265
3266 if (cfun->machine->is_interrupt
3267 || cfun->machine->is_signal
3268 || cfun->machine->is_naked
3269 || avr_naked_function_p (decl_callee))
3270 {
3271 return false;
3272 }
3273
3274 return true;
3275 }
3276
3277
3278 /***********************************************************************
3279 Functions for outputting various mov's for a various modes
3280 ************************************************************************/
3281
3282 /* Return true if a value of mode MODE is read from flash by
3283 __load_* function from libgcc. */
3284
3285 bool
3286 avr_load_libgcc_p (rtx op)
3287 {
3288 machine_mode mode = GET_MODE (op);
3289 int n_bytes = GET_MODE_SIZE (mode);
3290
3291 return (n_bytes > 2
3292 && !AVR_HAVE_LPMX
3293 && avr_mem_flash_p (op)
3294 && MEM_ADDR_SPACE (op) == ADDR_SPACE_FLASH);
3295 }
3296
3297
3298 /* Return true if a value of mode MODE is read by __xload_* function
3299 provided it is located in __memx. */
3300
3301 bool
3302 avr_xload_libgcc_p (machine_mode mode)
3303 {
3304 int n_bytes = GET_MODE_SIZE (mode);
3305
3306 return (n_bytes > 1
3307 || avropt_n_flash > 1);
3308 }
3309
3310
3311 /* Return true if a value of mode MODE is read by __fload_* function
3312 provided it is located in __flashx. */
3313
3314 bool
3315 avr_fload_libgcc_p (machine_mode)
3316 {
3317 return (! AVR_HAVE_ELPMX
3318 && ! AVR_HAVE_LPMX);
3319 }
3320
3321
3322 /* USE_LIBGCC = true: Return true when MEM is a mem rtx for address space
3323 AS that will be loaded using a libgcc support function.
3324 USE_LIBGCC = false: Return true when MEM is a mem rtx for address space
3325 AS that will be loaded inline (without using a libgcc support function). */
3326
3327 bool
3328 avr_load_libgcc_mem_p (rtx mem, addr_space_t as, bool use_libgcc)
3329 {
3330 if (MEM_P (mem))
3331 {
3332 machine_mode mode = GET_MODE (mem);
3333 rtx addr = XEXP (mem, 0);
3334
3335 if (MEM_ADDR_SPACE (mem) != as
3336 || GET_MODE (addr) != targetm.addr_space.pointer_mode (as))
3337 return false;
3338
3339 switch (as)
3340 {
3341 default:
3342 gcc_unreachable ();
3343
3344 case ADDR_SPACE_FLASH:
3345 return avr_load_libgcc_p (mem) == use_libgcc;
3346
3347 case ADDR_SPACE_MEMX:
3348 return avr_xload_libgcc_p (mode) == use_libgcc;
3349
3350 case ADDR_SPACE_FLASHX:
3351 return avr_fload_libgcc_p (mode) == use_libgcc;
3352 }
3353 }
3354
3355 return false;
3356 }
3357
3358
3359 /* Like `avr_load_libgcc_mem_p()', but for a single_set insn with
3360 a SET_SRC according to avr_load_libgcc_mem_p. */
3361
3362 bool
3363 avr_load_libgcc_insn_p (rtx_insn *insn, addr_space_t as, bool use_libgcc)
3364 {
3365 rtx set = single_set (insn);
3366 return (set
3367 && avr_load_libgcc_mem_p (SET_SRC (set), as, use_libgcc));
3368 }
3369
3370
3371 /* Return true when INSN has a REG_UNUSED note for hard reg REG.
3372 rtlanal.cc::find_reg_note() uses == to compare XEXP (link, 0)
3373 therefore use a custom function. */
3374
3375 static bool
3376 avr_insn_has_reg_unused_note_p (rtx_insn *insn, rtx reg)
3377 {
3378 for (rtx link = REG_NOTES (insn); link; link = XEXP (link, 1))
3379 if (REG_NOTE_KIND (link) == REG_UNUSED
3380 && REG_P (XEXP (link, 0))
3381 && REGNO (reg) >= REGNO (XEXP (link, 0))
3382 && END_REGNO (reg) <= END_REGNO (XEXP (link, 0)))
3383 return true;
3384
3385 return false;
3386 }
3387
3388
3389 /* A helper for the next function.
3390 Return nonzero if REG is not used after INSN.
3391 We assume REG is a reload reg, and therefore does
3392 not live past labels. It may live past calls or jumps though. */
3393
3394 static bool
3395 _reg_unused_after (rtx_insn *insn, rtx reg, bool look_at_insn)
3396 {
3397 if (look_at_insn)
3398 {
3399 /* If the reg is set by this instruction, then it is safe for our
3400 case. Disregard the case where this is a store to memory, since
3401 we are checking a register used in the store address. */
3402 rtx set = single_set (insn);
3403 if (set && !MEM_P (SET_DEST (set))
3404 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
3405 return true;
3406
3407 /* This case occurs when fuse-add introduced a POST_INC addressing,
3408 but the address register is unused after. */
3409 if (set)
3410 {
3411 rtx mem = MEM_P (SET_SRC (set)) ? SET_SRC (set) : SET_DEST (set);
3412 if (MEM_P (mem)
3413 && reg_overlap_mentioned_p (reg, XEXP (mem, 0))
3414 && avr_insn_has_reg_unused_note_p (insn, reg))
3415 return true;
3416 }
3417 }
3418
3419 while ((insn = NEXT_INSN (insn)))
3420 {
3421 rtx set;
3422 rtx_code code = GET_CODE (insn);
3423
3424 #if 0
3425 /* If this is a label that existed before reload, then the register
3426 if dead here. However, if this is a label added by reorg, then
3427 the register may still be live here. We can't tell the difference,
3428 so we just ignore labels completely. */
3429 if (code == CODE_LABEL)
3430 return true;
3431 /* else */
3432 #endif
3433
3434 if (!INSN_P (insn))
3435 continue;
3436
3437 if (code == JUMP_INSN)
3438 return false;
3439
3440 /* If this is a sequence, we must handle them all at once.
3441 We could have for instance a call that sets the target register,
3442 and an insn in a delay slot that uses the register. In this case,
3443 we must return 0. */
3444 else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
3445 {
3446 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
3447 bool retval = false;
3448
3449 for (int i = 0; i < seq->len (); i++)
3450 {
3451 rtx_insn *this_insn = seq->insn (i);
3452 rtx set = single_set (this_insn);
3453
3454 if (CALL_P (this_insn))
3455 code = CALL_INSN;
3456 else if (JUMP_P (this_insn))
3457 {
3458 if (INSN_ANNULLED_BRANCH_P (this_insn))
3459 return false;
3460 code = JUMP_INSN;
3461 }
3462
3463 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
3464 return false;
3465 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
3466 {
3467 if (!MEM_P (SET_DEST (set)))
3468 retval = true;
3469 else
3470 return false;
3471 }
3472 if (!set
3473 && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
3474 return false;
3475 }
3476 if (retval)
3477 return true;
3478 else if (code == JUMP_INSN)
3479 return false;
3480 }
3481
3482 if (code == CALL_INSN)
3483 {
3484 rtx tem;
3485 for (tem = CALL_INSN_FUNCTION_USAGE (insn); tem; tem = XEXP (tem, 1))
3486 if (GET_CODE (XEXP (tem, 0)) == USE
3487 && REG_P (XEXP (XEXP (tem, 0), 0))
3488 && reg_overlap_mentioned_p (reg, XEXP (XEXP (tem, 0), 0)))
3489 return false;
3490 if (call_used_or_fixed_reg_p (REGNO (reg)))
3491 return true;
3492 }
3493
3494 set = single_set (insn);
3495
3496 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
3497 return false;
3498 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
3499 return !MEM_P (SET_DEST (set));
3500 if (!set && reg_overlap_mentioned_p (reg, PATTERN (insn)))
3501 return false;
3502 }
3503 return true;
3504 }
3505
3506
3507 /* Return nonzero if register REG dead after INSN. */
3508
3509 bool
3510 reg_unused_after (rtx_insn *insn, rtx reg)
3511 {
3512 return (dead_or_set_p (insn, reg)
3513 || (REG_P (reg) && _reg_unused_after (insn, reg, true)));
3514 }
3515
3516
3517 /* Return true when REGNO is set by INSN but not used by the following code.
3518 The difference to reg_unused_after() is that reg_unused_after() returns
3519 true for the entire result even when the result *IS* being used atfer. */
3520
3521 static bool
3522 avr_result_regno_unused_p (rtx_insn *insn, unsigned regno)
3523 {
3524 if (!insn || !single_set (insn) || regno >= REG_32)
3525 return false;
3526
3527 rtx dest = SET_DEST (single_set (insn));
3528 if (!REG_P (dest) || !IN_RANGE (regno, REGNO (dest), END_REGNO (dest) - 1))
3529 return false;
3530
3531 return (avr_insn_has_reg_unused_note_p (insn, all_regs_rtx[regno])
3532 || _reg_unused_after (insn, all_regs_rtx[regno], false));
3533 }
3534
3535
3536 /* Fixme: This is a hack because secondary reloads don't works as expected.
3537
3538 Find an unused d-register to be used as scratch in INSN.
3539 EXCLUDE is either NULL_RTX or some register. In the case where EXCLUDE
3540 is a register, skip all possible return values that overlap EXCLUDE.
3541 The policy for the returned register is similar to that of
3542 `reg_unused_after', i.e. the returned register may overlap the SET_DEST
3543 of INSN.
3544
3545 Return a QImode d-register or NULL_RTX if nothing found. */
3546
3547 static rtx
3548 avr_find_unused_d_reg (rtx_insn *insn, rtx exclude)
3549 {
3550 bool isr_p = (avr_interrupt_function (current_function_decl)
3551 || avr_signal_function (current_function_decl));
3552
3553 for (int regno = REG_16; regno < REG_32; regno++)
3554 {
3555 rtx reg = all_regs_rtx[regno];
3556
3557 if ((exclude
3558 && reg_overlap_mentioned_p (exclude, reg))
3559 || fixed_regs[regno])
3560 {
3561 continue;
3562 }
3563
3564 /* Try non-live register */
3565
3566 if (!df_regs_ever_live_p (regno)
3567 && (TREE_THIS_VOLATILE (current_function_decl)
3568 || cfun->machine->is_OS_task
3569 || cfun->machine->is_OS_main
3570 || (!isr_p && call_used_or_fixed_reg_p (regno))))
3571 {
3572 return reg;
3573 }
3574
3575 /* Any live register can be used if it is unused after.
3576 Prologue/epilogue will care for it as needed. */
3577
3578 if (df_regs_ever_live_p (regno)
3579 && reg_unused_after (insn, reg))
3580 {
3581 return reg;
3582 }
3583 }
3584
3585 return NULL_RTX;
3586 }
3587
3588
3589 /* Helper function for the next function in the case where only restricted
3590 version of LPM instruction is available. */
3591
3592 static const char *
3593 avr_out_lpm_no_lpmx (rtx_insn *insn, rtx *xop, int *plen)
3594 {
3595 rtx dest = xop[0];
3596 rtx addr = xop[1];
3597 int n_bytes = GET_MODE_SIZE (GET_MODE (dest));
3598 int regno_dest;
3599
3600 regno_dest = REGNO (dest);
3601
3602 /* The implicit target register of LPM. */
3603 xop[3] = lpm_reg_rtx;
3604
3605 switch (GET_CODE (addr))
3606 {
3607 default:
3608 gcc_unreachable ();
3609
3610 case REG:
3611
3612 gcc_assert (REG_Z == REGNO (addr));
3613
3614 switch (n_bytes)
3615 {
3616 default:
3617 gcc_unreachable ();
3618
3619 case 1:
3620 avr_asm_len ("%4lpm", xop, plen, 1);
3621
3622 if (regno_dest != LPM_REGNO)
3623 avr_asm_len ("mov %0,%3", xop, plen, 1);
3624
3625 return "";
3626
3627 case 2:
3628 if (REGNO (dest) == REG_Z)
3629 return avr_asm_len ("%4lpm" CR_TAB
3630 "push %3" CR_TAB
3631 "adiw %2,1" CR_TAB
3632 "%4lpm" CR_TAB
3633 "mov %B0,%3" CR_TAB
3634 "pop %A0", xop, plen, 6);
3635
3636 avr_asm_len ("%4lpm" CR_TAB
3637 "mov %A0,%3" CR_TAB
3638 "adiw %2,1" CR_TAB
3639 "%4lpm" CR_TAB
3640 "mov %B0,%3", xop, plen, 5);
3641
3642 if (!reg_unused_after (insn, addr))
3643 avr_asm_len ("sbiw %2,1", xop, plen, 1);
3644
3645 break; /* 2 */
3646
3647 /* cases 3 and 4 are only needed with ELPM but no ELPMx. */
3648
3649 case 3:
3650 if (REGNO (dest) == REG_Z - 2
3651 && !reg_unused_after (insn, all_regs_rtx[REG_31]))
3652 avr_asm_len ("push r31", xop, plen, 1);
3653
3654 avr_asm_len ("%4lpm $ mov %A0,%3 $ adiw %2,1", xop, plen, 3);
3655 avr_asm_len ("%4lpm $ mov %B0,%3 $ adiw %2,1", xop, plen, 3);
3656 avr_asm_len ("%4lpm $ mov %C0,%3", xop, plen, 2);
3657
3658 if (REGNO (dest) == REG_Z - 2)
3659 {
3660 if (!reg_unused_after (insn, all_regs_rtx[REG_31]))
3661 avr_asm_len ("pop r31", xop, plen, 1);
3662 }
3663 else if (!reg_unused_after (insn, addr))
3664 avr_asm_len ("sbiw %2,2", xop, plen, 1);
3665
3666 break; /* 3 */
3667
3668 case 4:
3669 avr_asm_len ("%4lpm $ mov %A0,%3 $ adiw %2,1", xop, plen, 3);
3670 avr_asm_len ("%4lpm $ mov %B0,%3 $ adiw %2,1", xop, plen, 3);
3671 if (REGNO (dest) != REG_Z - 2)
3672 {
3673 avr_asm_len ("%4lpm $ mov %C0,%3 $ adiw %2,1", xop, plen, 3);
3674 avr_asm_len ("%4lpm $ mov %D0,%3", xop, plen, 2);
3675 if (!reg_unused_after (insn, addr))
3676 avr_asm_len ("sbiw %2,3", xop, plen, 1);
3677 }
3678 else
3679 {
3680 avr_asm_len ("%4lpm $ push %3 $ adiw %2,1", xop, plen, 3);
3681 avr_asm_len ("%4lpm $ mov %D0,%3", xop, plen, 2);
3682 avr_asm_len ("pop $C0", xop, plen, 1);
3683 }
3684
3685 break; /* 4 */
3686 }
3687
3688 break; /* REG */
3689
3690 case POST_INC:
3691
3692 gcc_assert (REG_Z == REGNO (XEXP (addr, 0))
3693 && n_bytes <= 4);
3694
3695 for (int i = 0; i < n_bytes; ++i)
3696 {
3697 rtx reg = avr_byte (dest, i);
3698
3699 if (i > 0)
3700 avr_asm_len ("adiw %2,1", xop, plen, 1);
3701
3702 avr_asm_len ("%4lpm", xop, plen, 1);
3703
3704 if (REGNO (reg) != LPM_REGNO)
3705 avr_asm_len ("mov %0,r0", &reg, plen, 1);
3706 }
3707
3708 if (! reg_unused_after (insn, xop[2]))
3709 avr_asm_len ("adiw %2,1", xop, plen, 1);
3710
3711 break; /* POST_INC */
3712
3713 } /* switch CODE (addr) */
3714
3715 return "";
3716 }
3717
3718
3719 /* If PLEN == NULL: Ouput instructions to load a value from a memory location
3720 OP[1] in AS1 to register OP[0].
3721 If PLEN != 0 set *PLEN to the length in words of the instruction sequence.
3722 Return "". */
3723
3724 const char *
3725 avr_out_lpm (rtx_insn *insn, rtx *op, int *plen)
3726 {
3727 rtx xop[7];
3728 rtx dest = op[0];
3729 rtx src = SET_SRC (single_set (insn));
3730 int n_bytes = GET_MODE_SIZE (GET_MODE (dest));
3731 addr_space_t as = MEM_ADDR_SPACE (src);
3732
3733 if (plen)
3734 *plen = 0;
3735
3736 if (MEM_P (dest))
3737 {
3738 warning (0, "writing to address space %qs not supported",
3739 avr_addrspace[MEM_ADDR_SPACE (dest)].name);
3740
3741 return "";
3742 }
3743
3744 rtx addr = XEXP (src, 0);
3745 rtx_code code = GET_CODE (addr);
3746
3747 gcc_assert (REG_P (dest));
3748 gcc_assert (REG == code || POST_INC == code);
3749
3750 xop[0] = dest;
3751 xop[1] = addr;
3752 xop[2] = lpm_addr_reg_rtx;
3753 xop[4] = xstring_empty;
3754 xop[5] = tmp_reg_rtx;
3755 xop[6] = XEXP (rampz_rtx, 0);
3756
3757 int segment = avr_addrspace[as].segment;
3758
3759 /* Set RAMPZ as needed. */
3760
3761 if (segment)
3762 {
3763 xop[4] = GEN_INT (segment);
3764 xop[3] = avr_find_unused_d_reg (insn, lpm_addr_reg_rtx);
3765
3766 if (xop[3] != NULL_RTX)
3767 {
3768 avr_asm_len ("ldi %3,%4" CR_TAB
3769 "out %i6,%3", xop, plen, 2);
3770 }
3771 else if (segment == 1)
3772 {
3773 avr_asm_len ("clr %5" CR_TAB
3774 "inc %5" CR_TAB
3775 "out %i6,%5", xop, plen, 3);
3776 }
3777 else
3778 {
3779 avr_asm_len ("mov %5,%2" CR_TAB
3780 "ldi %2,%4" CR_TAB
3781 "out %i6,%2" CR_TAB
3782 "mov %2,%5", xop, plen, 4);
3783 }
3784
3785 xop[4] = xstring_e;
3786
3787 if (!AVR_HAVE_ELPMX)
3788 return avr_out_lpm_no_lpmx (insn, xop, plen);
3789 }
3790 else if (!AVR_HAVE_LPMX)
3791 {
3792 return avr_out_lpm_no_lpmx (insn, xop, plen);
3793 }
3794
3795 /* We have [E]LPMX: Output reading from Flash the comfortable way. */
3796
3797 switch (GET_CODE (addr))
3798 {
3799 default:
3800 gcc_unreachable ();
3801
3802 case REG:
3803
3804 gcc_assert (REG_Z == REGNO (addr));
3805
3806 switch (n_bytes)
3807 {
3808 default:
3809 gcc_unreachable ();
3810
3811 case 1:
3812 avr_asm_len ("%4lpm %0,%a2", xop, plen, 1);
3813 break;
3814
3815 case 2:
3816 if (REGNO (dest) == REG_Z)
3817 avr_asm_len ("%4lpm %5,%a2+" CR_TAB
3818 "%4lpm %B0,%a2" CR_TAB
3819 "mov %A0,%5", xop, plen, 3);
3820 else
3821 {
3822 avr_asm_len ("%4lpm %A0,%a2+" CR_TAB
3823 "%4lpm %B0,%a2", xop, plen, 2);
3824
3825 if (!reg_unused_after (insn, addr))
3826 avr_asm_len ("sbiw %2,1", xop, plen, 1);
3827 }
3828
3829 break; /* 2 */
3830
3831 case 3:
3832
3833 avr_asm_len ("%4lpm %A0,%a2+" CR_TAB
3834 "%4lpm %B0,%a2+" CR_TAB
3835 "%4lpm %C0,%a2", xop, plen, 3);
3836
3837 if (!reg_unused_after (insn, addr))
3838 avr_asm_len ("sbiw %2,2", xop, plen, 1);
3839
3840 break; /* 3 */
3841
3842 case 4:
3843
3844 avr_asm_len ("%4lpm %A0,%a2+" CR_TAB
3845 "%4lpm %B0,%a2+", xop, plen, 2);
3846
3847 if (REGNO (dest) == REG_Z - 2)
3848 avr_asm_len ("%4lpm %5,%a2+" CR_TAB
3849 "%4lpm %C0,%a2" CR_TAB
3850 "mov %D0,%5", xop, plen, 3);
3851 else
3852 {
3853 avr_asm_len ("%4lpm %C0,%a2+" CR_TAB
3854 "%4lpm %D0,%a2", xop, plen, 2);
3855
3856 if (!reg_unused_after (insn, addr))
3857 avr_asm_len ("sbiw %2,3", xop, plen, 1);
3858 }
3859
3860 break; /* 4 */
3861 } /* n_bytes */
3862
3863 break; /* REG */
3864
3865 case POST_INC:
3866
3867 gcc_assert (REG_Z == REGNO (XEXP (addr, 0))
3868 && n_bytes <= 4);
3869
3870 avr_asm_len ("%4lpm %A0,%a2+", xop, plen, 1);
3871 if (n_bytes >= 2) avr_asm_len ("%4lpm %B0,%a2+", xop, plen, 1);
3872 if (n_bytes >= 3) avr_asm_len ("%4lpm %C0,%a2+", xop, plen, 1);
3873 if (n_bytes >= 4) avr_asm_len ("%4lpm %D0,%a2+", xop, plen, 1);
3874
3875 break; /* POST_INC */
3876
3877 } /* switch CODE (addr) */
3878
3879 if (xop[4] == xstring_e && AVR_HAVE_RAMPD)
3880 {
3881 /* Reset RAMPZ to 0 so that EBI devices don't read garbage from RAM. */
3882
3883 xop[0] = zero_reg_rtx;
3884 avr_asm_len ("out %i6,%0", xop, plen, 1);
3885 }
3886
3887 return "";
3888 }
3889
3890
3891 /* Load a value from 24-bit address space __memx and return "".
3892 PLEN == 0: Output instructions.
3893 PLEN != 0: Set *PLEN to the length of the sequence in words. */
3894
3895 const char *
3896 avr_out_xload (rtx_insn * /*insn*/, rtx *op, int *plen)
3897 {
3898 rtx xop[4];
3899
3900 xop[0] = op[0];
3901 xop[1] = op[1];
3902 xop[2] = lpm_addr_reg_rtx;
3903 xop[3] = AVR_HAVE_LPMX ? op[0] : lpm_reg_rtx;
3904
3905 if (plen)
3906 *plen = 0;
3907
3908 if (reg_overlap_mentioned_p (xop[3], lpm_addr_reg_rtx))
3909 avr_asm_len ("sbrs %1,7", xop, plen, 1);
3910
3911 avr_asm_len (AVR_HAVE_LPMX ? "lpm %3,%a2" : "lpm", xop, plen, 1);
3912
3913 avr_asm_len ("sbrc %1,7" CR_TAB
3914 "ld %3,%a2", xop, plen, 2);
3915
3916 if (REGNO (xop[0]) != REGNO (xop[3]))
3917 avr_asm_len ("mov %0,%3", xop, plen, 1);
3918
3919 return "";
3920 }
3921
3922
3923 /* Load a value from 24-bit address space __flashx and return "".
3924 PLEN == 0: Output instructions.
3925 PLEN != 0: Set *PLEN to the length of the sequence in words. */
3926
3927 const char *
3928 avr_out_fload (rtx_insn * /*insn*/, rtx *xop, int *plen)
3929 {
3930 gcc_assert (AVR_HAVE_ELPMX
3931 || (! AVR_HAVE_ELPM && AVR_HAVE_LPMX));
3932 if (plen)
3933 *plen = 0;
3934
3935 if (AVR_HAVE_ELPMX)
3936 avr_asm_len ("out __RAMPZ__,%1", xop, plen, 1);
3937
3938 const int n_bytes = GET_MODE_SIZE (GET_MODE (xop[0]));
3939 const char *s_load = AVR_HAVE_ELPMX ? "elpm %0,Z" : "lpm %0,Z";
3940 const char *s_load_inc = AVR_HAVE_ELPMX ? "elpm %0,Z+" : "lpm %0,Z+";
3941 const char *s_load_tmp_inc = AVR_HAVE_ELPMX ? "elpm r0,Z+" : "lpm r0,Z+";
3942 bool use_tmp_for_r30 = false;
3943
3944 // There are nasty cases where reload assigns a register to dest that
3945 // overlaps Z, even though fmov<mode> clobbers REG_Z.
3946 for (int i = 0; i < n_bytes; ++i)
3947 {
3948 rtx b = avr_byte (xop[0], i);
3949 if (i == n_bytes - 1)
3950 avr_asm_len (s_load, &b, plen, 1);
3951 else if (REGNO (b) == REG_30)
3952 {
3953 avr_asm_len (s_load_tmp_inc, &b, plen, 1);
3954 use_tmp_for_r30 = true;
3955 }
3956 else
3957 avr_asm_len (s_load_inc, &b, plen, 1);
3958 }
3959
3960 if (use_tmp_for_r30)
3961 avr_asm_len ("mov r30,r0", xop, plen, 1);
3962
3963 if (AVR_HAVE_ELPMX && AVR_HAVE_RAMPD)
3964 avr_asm_len ("out __RAMPZ__,__zero_reg__", xop, plen, 1);
3965
3966 return "";
3967 }
3968
3969
3970 /* A helper for `output_reload_insisf' and `output_reload_inhi'. */
3971 /* Set register OP[0] to compile-time constant OP[1].
3972 CLOBBER_REG is a QI clobber register or NULL_RTX.
3973 LEN == NULL: output instructions.
3974 LEN != NULL: set *LEN to the length of the instruction sequence
3975 (in words) printed with LEN = NULL.
3976 If CLEAR_P is true, OP[0] had been cleard to Zero already.
3977 If CLEAR_P is false, nothing is known about OP[0].
3978
3979 The effect on cc0 is as follows:
3980
3981 Load 0 to any register except ZERO_REG : NONE
3982 Load ld register with any value : NONE
3983 Anything else: : CLOBBER */
3984
3985 void
3986 output_reload_in_const (rtx *op, rtx clobber_reg, int *len, bool clear_p)
3987 {
3988 rtx src = op[1];
3989 rtx dest = op[0];
3990 rtx xval, xdest[4];
3991 int ival[4];
3992 int clobber_val = 1234;
3993 bool cooked_clobber_p = false;
3994 bool set_p = false;
3995 machine_mode mode = GET_MODE (dest);
3996 int n_bytes = GET_MODE_SIZE (mode);
3997
3998 gcc_assert (REG_P (dest)
3999 && CONSTANT_P (src));
4000
4001 if (len)
4002 *len = 0;
4003
4004 /* (REG:SI 14) is special: It's neither in LD_REGS nor in NO_LD_REGS
4005 but has some subregs that are in LD_REGS. Use the MSB (REG:QI 17). */
4006
4007 if (REGNO (dest) < REG_16
4008 && END_REGNO (dest) > REG_16)
4009 {
4010 clobber_reg = all_regs_rtx[END_REGNO (dest) - 1];
4011 }
4012
4013 /* We might need a clobber reg but don't have one. Look at the value to
4014 be loaded more closely. A clobber is only needed if it is a symbol
4015 or contains a byte that is neither 0, -1 or a power of 2. */
4016
4017 if (NULL_RTX == clobber_reg
4018 && !test_hard_reg_class (LD_REGS, dest)
4019 && (! (CONST_INT_P (src) || CONST_FIXED_P (src) || CONST_DOUBLE_P (src))
4020 || !avr_popcount_each_byte (src, n_bytes,
4021 (1 << 0) | (1 << 1) | (1 << 8))))
4022 {
4023 /* We have no clobber register but need one. Cook one up.
4024 That's cheaper than loading from constant pool. */
4025
4026 cooked_clobber_p = true;
4027 clobber_reg = all_regs_rtx[REG_Z + 1];
4028 avr_asm_len ("mov __tmp_reg__,%0", &clobber_reg, len, 1);
4029 }
4030
4031 /* Now start filling DEST from LSB to MSB. */
4032
4033 for (int n = 0; n < n_bytes; n++)
4034 {
4035 bool done_byte = false;
4036 rtx xop[3];
4037
4038 /* Crop the n-th destination byte. */
4039
4040 xdest[n] = avr_byte (dest, n);
4041 int ldreg_p = test_hard_reg_class (LD_REGS, xdest[n]);
4042
4043 if (!CONST_INT_P (src)
4044 && !CONST_FIXED_P (src)
4045 && !CONST_DOUBLE_P (src))
4046 {
4047 static const char *const asm_code[][2] =
4048 {
4049 { "ldi %2,lo8(%1)" CR_TAB "mov %0,%2", "ldi %0,lo8(%1)" },
4050 { "ldi %2,hi8(%1)" CR_TAB "mov %0,%2", "ldi %0,hi8(%1)" },
4051 { "ldi %2,hlo8(%1)" CR_TAB "mov %0,%2", "ldi %0,hlo8(%1)" },
4052 { "ldi %2,hhi8(%1)" CR_TAB "mov %0,%2", "ldi %0,hhi8(%1)" }
4053 };
4054
4055 xop[0] = xdest[n];
4056 xop[1] = src;
4057 xop[2] = clobber_reg;
4058
4059 avr_asm_len (asm_code[n][ldreg_p], xop, len, ldreg_p ? 1 : 2);
4060
4061 continue;
4062 }
4063
4064 /* Crop the n-th source byte. */
4065
4066 xval = avr_byte (src, n);
4067 ival[n] = INTVAL (xval);
4068
4069 /* Look if we can reuse the low word by means of MOVW. */
4070
4071 if (n == 2
4072 && n_bytes >= 4
4073 && AVR_HAVE_MOVW)
4074 {
4075 int lo16 = avr_int16 (src, 0);
4076 int hi16 = avr_int16 (src, 2);
4077
4078 if (lo16 == hi16)
4079 {
4080 if (lo16 != 0 || ! clear_p)
4081 avr_asm_len ("movw %C0,%A0", &op[0], len, 1);
4082
4083 break;
4084 }
4085 }
4086
4087 /* Don't use CLR so that cc0 is set as expected. */
4088
4089 if (ival[n] == 0)
4090 {
4091 if (!clear_p)
4092 avr_asm_len (ldreg_p ? "ldi %0,0"
4093 : AVR_ZERO_REGNO == REGNO (xdest[n]) ? "clr %0"
4094 : "mov %0,__zero_reg__",
4095 &xdest[n], len, 1);
4096 continue;
4097 }
4098
4099 if (clobber_val == ival[n]
4100 && REGNO (clobber_reg) == REGNO (xdest[n]))
4101 {
4102 continue;
4103 }
4104
4105 /* LD_REGS can use LDI to move a constant value */
4106
4107 if (ldreg_p)
4108 {
4109 xop[0] = xdest[n];
4110 xop[1] = xval;
4111 avr_asm_len ("ldi %0,lo8(%1)", xop, len, 1);
4112 continue;
4113 }
4114
4115 /* Try to reuse value already loaded in some lower byte. */
4116
4117 for (int j = 0; j < n; j++)
4118 if (ival[j] == ival[n])
4119 {
4120 xop[0] = xdest[n];
4121 xop[1] = xdest[j];
4122
4123 avr_asm_len ("mov %0,%1", xop, len, 1);
4124 done_byte = true;
4125 break;
4126 }
4127
4128 if (done_byte)
4129 continue;
4130
4131 /* Need no clobber reg for -1: Use CLR/DEC */
4132
4133 if (ival[n] == -1)
4134 {
4135 if (!clear_p)
4136 avr_asm_len ("clr %0", &xdest[n], len, 1);
4137
4138 avr_asm_len ("dec %0", &xdest[n], len, 1);
4139 continue;
4140 }
4141 else if (ival[n] == 1)
4142 {
4143 if (!clear_p)
4144 avr_asm_len ("clr %0", &xdest[n], len, 1);
4145
4146 avr_asm_len ("inc %0", &xdest[n], len, 1);
4147 continue;
4148 }
4149
4150 /* Use T flag or INC to manage powers of 2 if we have
4151 no clobber reg. */
4152
4153 if (NULL_RTX == clobber_reg
4154 && single_one_operand (xval, QImode))
4155 {
4156 xop[0] = xdest[n];
4157 xop[1] = GEN_INT (exact_log2 (ival[n] & GET_MODE_MASK (QImode)));
4158
4159 gcc_assert (constm1_rtx != xop[1]);
4160
4161 if (!set_p)
4162 {
4163 set_p = true;
4164 avr_asm_len ("set", xop, len, 1);
4165 }
4166
4167 if (!clear_p)
4168 avr_asm_len ("clr %0", xop, len, 1);
4169
4170 avr_asm_len ("bld %0,%1", xop, len, 1);
4171 continue;
4172 }
4173
4174 /* We actually need the LD_REGS clobber reg. */
4175
4176 gcc_assert (NULL_RTX != clobber_reg);
4177
4178 xop[0] = xdest[n];
4179 xop[1] = xval;
4180 xop[2] = clobber_reg;
4181 clobber_val = ival[n];
4182
4183 avr_asm_len ("ldi %2,lo8(%1)" CR_TAB
4184 "mov %0,%2", xop, len, 2);
4185 }
4186
4187 /* If we cooked up a clobber reg above, restore it. */
4188
4189 if (cooked_clobber_p)
4190 {
4191 avr_asm_len ("mov %0,__tmp_reg__", &clobber_reg, len, 1);
4192 }
4193 }
4194
4195
4196 const char *
4197 output_movqi (rtx_insn *insn, rtx operands[], int *plen)
4198 {
4199 rtx dest = operands[0];
4200 rtx src = operands[1];
4201
4202 if (avr_mem_flash_p (src)
4203 || avr_mem_flash_p (dest))
4204 {
4205 return avr_out_lpm (insn, operands, plen);
4206 }
4207
4208 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 1);
4209
4210 if (REG_P (dest))
4211 {
4212 if (REG_P (src)) /* mov r,r */
4213 {
4214 if (test_hard_reg_class (STACK_REG, dest))
4215 return avr_asm_len ("out %0,%1", operands, plen, -1);
4216 else if (test_hard_reg_class (STACK_REG, src))
4217 return avr_asm_len ("in %0,%1", operands, plen, -1);
4218
4219 return avr_asm_len ("mov %0,%1", operands, plen, -1);
4220 }
4221 else if (CONSTANT_P (src))
4222 {
4223 output_reload_in_const (operands, NULL_RTX, plen, false);
4224 return "";
4225 }
4226 else if (MEM_P (src))
4227 return out_movqi_r_mr (insn, operands, plen); /* mov r,m */
4228 }
4229 else if (MEM_P (dest))
4230 {
4231 rtx xop[2];
4232
4233 xop[0] = dest;
4234 xop[1] = src == CONST0_RTX (GET_MODE (dest)) ? zero_reg_rtx : src;
4235
4236 return out_movqi_mr_r (insn, xop, plen);
4237 }
4238
4239 return "";
4240 }
4241
4242
4243 const char *
4244 output_movhi (rtx_insn *insn, rtx xop[], int *plen)
4245 {
4246 rtx dest = xop[0];
4247 rtx src = xop[1];
4248
4249 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 2);
4250
4251 if (avr_mem_flash_p (src)
4252 || avr_mem_flash_p (dest))
4253 {
4254 return avr_out_lpm (insn, xop, plen);
4255 }
4256
4257 if (REG_P (dest))
4258 {
4259 if (REG_P (src)) /* mov r,r */
4260 {
4261 if (test_hard_reg_class (STACK_REG, dest))
4262 {
4263 if (AVR_HAVE_8BIT_SP)
4264 return avr_asm_len ("out __SP_L__,%A1", xop, plen, -1);
4265
4266 if (AVR_XMEGA)
4267 return avr_asm_len ("out __SP_L__,%A1" CR_TAB
4268 "out __SP_H__,%B1", xop, plen, -2);
4269
4270 /* Use simple load of SP if no interrupts are used. */
4271
4272 return TARGET_NO_INTERRUPTS
4273 ? avr_asm_len ("out __SP_H__,%B1" CR_TAB
4274 "out __SP_L__,%A1", xop, plen, -2)
4275 : avr_asm_len ("in __tmp_reg__,__SREG__" CR_TAB
4276 "cli" CR_TAB
4277 "out __SP_H__,%B1" CR_TAB
4278 "out __SREG__,__tmp_reg__" CR_TAB
4279 "out __SP_L__,%A1", xop, plen, -5);
4280 }
4281 else if (test_hard_reg_class (STACK_REG, src))
4282 {
4283 return !AVR_HAVE_SPH
4284 ? avr_asm_len ("in %A0,__SP_L__" CR_TAB
4285 "clr %B0", xop, plen, -2)
4286
4287 : avr_asm_len ("in %A0,__SP_L__" CR_TAB
4288 "in %B0,__SP_H__", xop, plen, -2);
4289 }
4290
4291 return AVR_HAVE_MOVW
4292 ? avr_asm_len ("movw %0,%1", xop, plen, -1)
4293
4294 : avr_asm_len ("mov %A0,%A1" CR_TAB
4295 "mov %B0,%B1", xop, plen, -2);
4296 } /* REG_P (src) */
4297 else if (CONSTANT_P (src))
4298 {
4299 return output_reload_inhi (xop, NULL_RTX, plen);
4300 }
4301 else if (MEM_P (src))
4302 {
4303 return out_movhi_r_mr (insn, xop, plen); /* mov r,m */
4304 }
4305 }
4306 else if (MEM_P (dest))
4307 {
4308 rtx xop[2];
4309
4310 xop[0] = dest;
4311 xop[1] = src == CONST0_RTX (GET_MODE (dest)) ? zero_reg_rtx : src;
4312
4313 return out_movhi_mr_r (insn, xop, plen);
4314 }
4315
4316 fatal_insn ("invalid insn:", insn);
4317
4318 return "";
4319 }
4320
4321
4322 /* Same as out_movqi_r_mr, but TINY does not have ADIW or SBIW */
4323
4324 static const char *
4325 avr_out_movqi_r_mr_reg_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
4326 {
4327 rtx dest = op[0];
4328 rtx src = op[1];
4329 rtx x = XEXP (src, 0);
4330 rtx base = XEXP (x, 0);
4331
4332 if (plen)
4333 *plen = 0;
4334
4335 if (!reg_overlap_mentioned_p (dest, base))
4336 {
4337 avr_asm_len (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4338 "ld %0,%b1", op, plen, 3);
4339 if (!reg_unused_after (insn, base))
4340 avr_asm_len (TINY_SBIW (%I1, %J1, %o1), op, plen, 2);
4341 }
4342 else
4343 {
4344 // PR98762: The base register overlaps dest and is only partly clobbered.
4345 rtx base2 = all_regs_rtx[1 ^ REGNO (dest)];
4346
4347 if (!reg_unused_after (insn, base2))
4348 avr_asm_len ("mov __tmp_reg__,%0", &base2, plen, 1);
4349 avr_asm_len (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4350 "ld %0,%b1", op, plen, 3);
4351 if (!reg_unused_after (insn, base2))
4352 avr_asm_len ("mov %0,__tmp_reg__", &base2, plen, 1);
4353 }
4354
4355 return "";
4356 }
4357
4358 static const char *
4359 out_movqi_r_mr (rtx_insn *insn, rtx op[], int *plen)
4360 {
4361 rtx dest = op[0];
4362 rtx src = op[1];
4363 rtx x = XEXP (src, 0);
4364
4365 if (CONSTANT_ADDRESS_P (x))
4366 {
4367 int n_words = AVR_TINY ? 1 : 2;
4368 return io_address_operand (x, QImode)
4369 ? avr_asm_len ("in %0,%i1", op, plen, -1)
4370 : avr_asm_len ("lds %0,%m1", op, plen, -n_words);
4371 }
4372
4373 if (GET_CODE (x) == PLUS
4374 && REG_P (XEXP (x, 0))
4375 && CONST_INT_P (XEXP (x, 1)))
4376 {
4377 /* memory access by reg+disp */
4378
4379 if (AVR_TINY)
4380 return avr_out_movqi_r_mr_reg_disp_tiny (insn, op, plen);
4381
4382 if (plen)
4383 *plen = 0;
4384
4385 int disp = INTVAL (XEXP (x, 1));
4386 rtx base = XEXP (x, 0);
4387 rtx base2 = all_regs_rtx[1 ^ REGNO (dest)];
4388 bool partial_clobber = (reg_overlap_mentioned_p (dest, base)
4389 && ! reg_unused_after (insn, base2));
4390
4391 if (disp - GET_MODE_SIZE (GET_MODE (src)) >= 63)
4392 {
4393 // PR117744: The base register overlaps dest and is
4394 // only partially clobbered.
4395 if (partial_clobber)
4396 avr_asm_len ("mov __tmp_reg__,%0", &base2, plen, 1);
4397
4398 if (REGNO (XEXP (x, 0)) != REG_Y)
4399 fatal_insn ("incorrect insn:",insn);
4400
4401 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (src)))
4402 avr_asm_len ("adiw r28,%o1-63" CR_TAB
4403 "ldd %0,Y+63" CR_TAB
4404 "sbiw r28,%o1-63", op, plen, 3);
4405 else
4406 avr_asm_len ("subi r28,lo8(-%o1)" CR_TAB
4407 "sbci r29,hi8(-%o1)" CR_TAB
4408 "ld %0,Y" CR_TAB
4409 "subi r28,lo8(%o1)" CR_TAB
4410 "sbci r29,hi8(%o1)", op, plen, 5);
4411
4412 if (partial_clobber)
4413 avr_asm_len ("mov __tmp_reg__,%0", &base2, plen, 1);
4414
4415 return "";
4416 }
4417 else if (REGNO (XEXP (x, 0)) == REG_X)
4418 {
4419 /* This is a paranoid case LEGITIMIZE_RELOAD_ADDRESS must exclude
4420 it but I have this situation with extremal optimizing options. */
4421
4422 // PR117744: The base register overlaps dest and is
4423 // only partially clobbered.
4424 bool clobber_r26 = (partial_clobber
4425 && REGNO (base) == (REGNO (base) & ~1));
4426 if (partial_clobber
4427 && ! clobber_r26)
4428 avr_asm_len ("mov __tmp_reg__,%0", &base2, plen, 1);
4429
4430 avr_asm_len ("adiw r26,%o1" CR_TAB
4431 "ld %0,X", op, plen, 2);
4432
4433 if (clobber_r26)
4434 avr_asm_len ("subi r26,lo8(%o1)", op, plen, 1);
4435 else if (partial_clobber)
4436 avr_asm_len ("mov %0,__tmp_reg__", &base2, plen, 1);
4437 else if (! reg_unused_after (insn, base))
4438 avr_asm_len ("sbiw r26,%o1", op, plen, 1);
4439
4440 return "";
4441 }
4442
4443 return avr_asm_len ("ldd %0,%1", op, plen, -1);
4444 }
4445
4446 return avr_asm_len ("ld %0,%1", op, plen, -1);
4447 }
4448
4449
4450 /* Same as movhi_r_mr, but TINY does not have ADIW, SBIW and LDD */
4451
4452 static const char *
4453 avr_out_movhi_r_mr_reg_no_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
4454 {
4455 rtx dest = op[0];
4456 rtx src = op[1];
4457 rtx base = XEXP (src, 0);
4458
4459 int reg_dest = true_regnum (dest);
4460 int reg_base = true_regnum (base);
4461
4462 if (reg_dest == reg_base) /* R = (R) */
4463 return avr_asm_len ("ld __tmp_reg__,%1+" CR_TAB
4464 "ld %B0,%1" CR_TAB
4465 "mov %A0,__tmp_reg__", op, plen, -3);
4466
4467 avr_asm_len ("ld %A0,%1+" CR_TAB
4468 "ld %B0,%1", op, plen, -2);
4469
4470 if (!reg_unused_after (insn, base))
4471 avr_asm_len (TINY_SBIW (%E1, %F1, 1), op, plen, 2);
4472
4473 return "";
4474 }
4475
4476
4477 /* Same as movhi_r_mr, but TINY does not have ADIW, SBIW and LDD */
4478
4479 static const char *
4480 avr_out_movhi_r_mr_reg_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
4481 {
4482 rtx dest = op[0];
4483 rtx src = op[1];
4484 rtx base = XEXP (src, 0);
4485
4486 int reg_dest = true_regnum (dest);
4487 int reg_base = true_regnum (XEXP (base, 0));
4488
4489 if (reg_base == reg_dest)
4490 {
4491 return avr_asm_len (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4492 "ld __tmp_reg__,%b1+" CR_TAB
4493 "ld %B0,%b1" CR_TAB
4494 "mov %A0,__tmp_reg__", op, plen, -5);
4495 }
4496 else
4497 {
4498 avr_asm_len (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4499 "ld %A0,%b1+" CR_TAB
4500 "ld %B0,%b1", op, plen, -4);
4501
4502 if (!reg_unused_after (insn, XEXP (base, 0)))
4503 avr_asm_len (TINY_SBIW (%I1, %J1, %o1+1), op, plen, 2);
4504
4505 return "";
4506 }
4507 }
4508
4509
4510 /* Same as movhi_r_mr, but TINY does not have ADIW, SBIW and LDD */
4511
4512 static const char *
4513 avr_out_movhi_r_mr_pre_dec_tiny (rtx_insn *insn, rtx op[], int *plen)
4514 {
4515 rtx dest = op[0];
4516 rtx src = op[1];
4517 rtx base = XEXP (src, 0);
4518
4519 /* "volatile" forces reading low byte first, even if less efficient,
4520 for correct operation with 16-bit I/O registers. */
4521 bool mem_volatile_p = MEM_VOLATILE_P (src);
4522
4523 if (reg_overlap_mentioned_p (dest, XEXP (base, 0)))
4524 fatal_insn ("incorrect insn:", insn);
4525
4526 if (!mem_volatile_p)
4527 return avr_asm_len ("ld %B0,%1" CR_TAB
4528 "ld %A0,%1", op, plen, -2);
4529
4530 return avr_asm_len (TINY_SBIW (%I1, %J1, 2) CR_TAB
4531 "ld %A0,%p1+" CR_TAB
4532 "ld %B0,%p1" CR_TAB
4533 TINY_SBIW (%I1, %J1, 1), op, plen, -6);
4534 }
4535
4536
4537 static const char *
4538 out_movhi_r_mr (rtx_insn *insn, rtx op[], int *plen)
4539 {
4540 rtx dest = op[0];
4541 rtx src = op[1];
4542 rtx base = XEXP (src, 0);
4543 int reg_dest = true_regnum (dest);
4544 int reg_base = true_regnum (base);
4545 /* "volatile" forces reading low byte first, even if less efficient,
4546 for correct operation with 16-bit I/O registers. */
4547 bool mem_volatile_p = MEM_VOLATILE_P (src);
4548
4549 if (reg_base > 0)
4550 {
4551 if (AVR_TINY)
4552 return avr_out_movhi_r_mr_reg_no_disp_tiny (insn, op, plen);
4553
4554 if (reg_dest == reg_base) /* R = (R) */
4555 return avr_asm_len ("ld __tmp_reg__,%1+" CR_TAB
4556 "ld %B0,%1" CR_TAB
4557 "mov %A0,__tmp_reg__", op, plen, -3);
4558
4559 if (reg_base != REG_X)
4560 return avr_asm_len ("ld %A0,%1" CR_TAB
4561 "ldd %B0,%1+1", op, plen, -2);
4562
4563 avr_asm_len ("ld %A0,X+" CR_TAB
4564 "ld %B0,X", op, plen, -2);
4565
4566 if (!reg_unused_after (insn, base))
4567 avr_asm_len ("sbiw r26,1", op, plen, 1);
4568
4569 return "";
4570 }
4571 else if (GET_CODE (base) == PLUS) /* (R + i) */
4572 {
4573 int disp = INTVAL (XEXP (base, 1));
4574 int reg_base = true_regnum (XEXP (base, 0));
4575
4576 if (AVR_TINY)
4577 return avr_out_movhi_r_mr_reg_disp_tiny (insn, op, plen);
4578
4579 if (disp > MAX_LD_OFFSET (GET_MODE (src)))
4580 {
4581 if (REGNO (XEXP (base, 0)) != REG_Y)
4582 fatal_insn ("incorrect insn:",insn);
4583
4584 return disp <= 63 + MAX_LD_OFFSET (GET_MODE (src))
4585 ? avr_asm_len ("adiw r28,%o1-62" CR_TAB
4586 "ldd %A0,Y+62" CR_TAB
4587 "ldd %B0,Y+63" CR_TAB
4588 "sbiw r28,%o1-62", op, plen, -4)
4589
4590 : avr_asm_len ("subi r28,lo8(-%o1)" CR_TAB
4591 "sbci r29,hi8(-%o1)" CR_TAB
4592 "ld %A0,Y" CR_TAB
4593 "ldd %B0,Y+1" CR_TAB
4594 "subi r28,lo8(%o1)" CR_TAB
4595 "sbci r29,hi8(%o1)", op, plen, -6);
4596 }
4597
4598 /* This is a paranoid case. LEGITIMIZE_RELOAD_ADDRESS must exclude
4599 it but I have this situation with extremal
4600 optimization options. */
4601
4602 if (reg_base == REG_X)
4603 {
4604 if (reg_base == reg_dest)
4605 return avr_asm_len ("adiw r26,%o1" CR_TAB
4606 "ld __tmp_reg__,X+" CR_TAB
4607 "ld %B0,X" CR_TAB
4608 "mov %A0,__tmp_reg__", op, plen, -4);
4609
4610 avr_asm_len ("adiw r26,%o1" CR_TAB
4611 "ld %A0,X+" CR_TAB
4612 "ld %B0,X", op, plen, -3);
4613
4614 if (!reg_unused_after (insn, XEXP (base, 0)))
4615 avr_asm_len ("sbiw r26,%o1+1", op, plen, 1);
4616
4617 return "";
4618 }
4619
4620 return reg_base == reg_dest
4621 ? avr_asm_len ("ldd __tmp_reg__,%A1" CR_TAB
4622 "ldd %B0,%B1" CR_TAB
4623 "mov %A0,__tmp_reg__", op, plen, -3)
4624
4625 : avr_asm_len ("ldd %A0,%A1" CR_TAB
4626 "ldd %B0,%B1", op, plen, -2);
4627 }
4628 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
4629 {
4630 if (AVR_TINY)
4631 return avr_out_movhi_r_mr_pre_dec_tiny (insn, op, plen);
4632
4633 if (reg_overlap_mentioned_p (dest, XEXP (base, 0)))
4634 fatal_insn ("incorrect insn:", insn);
4635
4636 if (!mem_volatile_p)
4637 return avr_asm_len ("ld %B0,%1" CR_TAB
4638 "ld %A0,%1", op, plen, -2);
4639
4640 return REGNO (XEXP (base, 0)) == REG_X
4641 ? avr_asm_len ("sbiw r26,2" CR_TAB
4642 "ld %A0,X+" CR_TAB
4643 "ld %B0,X" CR_TAB
4644 "sbiw r26,1", op, plen, -4)
4645
4646 : avr_asm_len ("sbiw %r1,2" CR_TAB
4647 "ld %A0,%p1" CR_TAB
4648 "ldd %B0,%p1+1", op, plen, -3);
4649 }
4650 else if (GET_CODE (base) == POST_INC) /* (R++) */
4651 {
4652 if (reg_overlap_mentioned_p (dest, XEXP (base, 0)))
4653 fatal_insn ("incorrect insn:", insn);
4654
4655 return avr_asm_len ("ld %A0,%1" CR_TAB
4656 "ld %B0,%1", op, plen, -2);
4657 }
4658 else if (CONSTANT_ADDRESS_P (base))
4659 {
4660 int n_words = AVR_TINY ? 2 : 4;
4661 return io_address_operand (base, HImode)
4662 ? avr_asm_len ("in %A0,%i1" CR_TAB
4663 "in %B0,%i1+1", op, plen, -2)
4664
4665 : avr_asm_len ("lds %A0,%m1" CR_TAB
4666 "lds %B0,%m1+1", op, plen, -n_words);
4667 }
4668
4669 fatal_insn ("unknown move insn:",insn);
4670 return "";
4671 }
4672
4673 static const char *
4674 avr_out_movsi_r_mr_reg_no_disp_tiny (rtx_insn *insn, rtx op[], int *l)
4675 {
4676 rtx dest = op[0];
4677 rtx src = op[1];
4678 rtx base = XEXP (src, 0);
4679 int reg_dest = true_regnum (dest);
4680 int reg_base = true_regnum (base);
4681
4682 if (reg_dest == reg_base)
4683 {
4684 /* "ld r26,-X" is undefined */
4685 return *l = 9, (TINY_ADIW (%E1, %F1, 3) CR_TAB
4686 "ld %D0,%1" CR_TAB
4687 "ld %C0,-%1" CR_TAB
4688 "ld __tmp_reg__,-%1" CR_TAB
4689 TINY_SBIW (%E1, %F1, 1) CR_TAB
4690 "ld %A0,%1" CR_TAB
4691 "mov %B0,__tmp_reg__");
4692 }
4693 else if (reg_dest == reg_base - 2)
4694 {
4695 return *l = 5, ("ld %A0,%1+" CR_TAB
4696 "ld %B0,%1+" CR_TAB
4697 "ld __tmp_reg__,%1+" CR_TAB
4698 "ld %D0,%1" CR_TAB
4699 "mov %C0,__tmp_reg__");
4700 }
4701 else if (reg_unused_after (insn, base))
4702 {
4703 return *l = 4, ("ld %A0,%1+" CR_TAB
4704 "ld %B0,%1+" CR_TAB
4705 "ld %C0,%1+" CR_TAB
4706 "ld %D0,%1");
4707 }
4708 else
4709 {
4710 return *l = 6, ("ld %A0,%1+" CR_TAB
4711 "ld %B0,%1+" CR_TAB
4712 "ld %C0,%1+" CR_TAB
4713 "ld %D0,%1" CR_TAB
4714 TINY_SBIW (%E1, %F1, 3));
4715 }
4716 }
4717
4718
4719 static const char *
4720 avr_out_movsi_r_mr_reg_disp_tiny (rtx_insn *insn, rtx op[], int *l)
4721 {
4722 rtx dest = op[0];
4723 rtx src = op[1];
4724 rtx base = XEXP (src, 0);
4725 int reg_dest = true_regnum (dest);
4726 int reg_base = true_regnum (XEXP (base, 0));
4727
4728 if (reg_dest == reg_base)
4729 {
4730 /* "ld r26,-X" is undefined */
4731 return *l = 9, (TINY_ADIW (%I1, %J1, %o1+3) CR_TAB
4732 "ld %D0,%b1" CR_TAB
4733 "ld %C0,-%b1" CR_TAB
4734 "ld __tmp_reg__,-%b1" CR_TAB
4735 TINY_SBIW (%I1, %J1, 1) CR_TAB
4736 "ld %A0,%b1" CR_TAB
4737 "mov %B0,__tmp_reg__");
4738 }
4739 else if (reg_dest == reg_base - 2)
4740 {
4741 return *l = 7, (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4742 "ld %A0,%b1+" CR_TAB
4743 "ld %B0,%b1+" CR_TAB
4744 "ld __tmp_reg__,%b1+" CR_TAB
4745 "ld %D0,%b1" CR_TAB
4746 "mov %C0,__tmp_reg__");
4747 }
4748 else if (reg_unused_after (insn, XEXP (base, 0)))
4749 {
4750 return *l = 6, (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4751 "ld %A0,%b1+" CR_TAB
4752 "ld %B0,%b1+" CR_TAB
4753 "ld %C0,%b1+" CR_TAB
4754 "ld %D0,%b1");
4755 }
4756 else
4757 {
4758 return *l = 8, (TINY_ADIW (%I1, %J1, %o1) CR_TAB
4759 "ld %A0,%b1+" CR_TAB
4760 "ld %B0,%b1+" CR_TAB
4761 "ld %C0,%b1+" CR_TAB
4762 "ld %D0,%b1" CR_TAB
4763 TINY_SBIW (%I1, %J1, %o1+3));
4764 }
4765 }
4766
4767 static const char *
4768 out_movsi_r_mr (rtx_insn *insn, rtx op[], int *l)
4769 {
4770 rtx dest = op[0];
4771 rtx src = op[1];
4772 rtx base = XEXP (src, 0);
4773 int reg_dest = true_regnum (dest);
4774 int reg_base = true_regnum (base);
4775 int tmp;
4776
4777 if (!l)
4778 l = &tmp;
4779
4780 if (reg_base > 0)
4781 {
4782 if (AVR_TINY)
4783 return avr_out_movsi_r_mr_reg_no_disp_tiny (insn, op, l);
4784
4785 if (reg_base == REG_X) /* (R26) */
4786 {
4787 if (reg_dest == REG_X)
4788 /* "ld r26,-X" is undefined */
4789 return *l=7, ("adiw r26,3" CR_TAB
4790 "ld r29,X" CR_TAB
4791 "ld r28,-X" CR_TAB
4792 "ld __tmp_reg__,-X" CR_TAB
4793 "sbiw r26,1" CR_TAB
4794 "ld r26,X" CR_TAB
4795 "mov r27,__tmp_reg__");
4796 else if (reg_dest == REG_X - 2)
4797 return *l=5, ("ld %A0,X+" CR_TAB
4798 "ld %B0,X+" CR_TAB
4799 "ld __tmp_reg__,X+" CR_TAB
4800 "ld %D0,X" CR_TAB
4801 "mov %C0,__tmp_reg__");
4802 else if (reg_unused_after (insn, base))
4803 return *l=4, ("ld %A0,X+" CR_TAB
4804 "ld %B0,X+" CR_TAB
4805 "ld %C0,X+" CR_TAB
4806 "ld %D0,X");
4807 else
4808 return *l=5, ("ld %A0,X+" CR_TAB
4809 "ld %B0,X+" CR_TAB
4810 "ld %C0,X+" CR_TAB
4811 "ld %D0,X" CR_TAB
4812 "sbiw r26,3");
4813 }
4814 else
4815 {
4816 if (reg_dest == reg_base)
4817 return *l=5, ("ldd %D0,%1+3" CR_TAB
4818 "ldd %C0,%1+2" CR_TAB
4819 "ldd __tmp_reg__,%1+1" CR_TAB
4820 "ld %A0,%1" CR_TAB
4821 "mov %B0,__tmp_reg__");
4822 else if (reg_base == reg_dest + 2)
4823 return *l=5, ("ld %A0,%1" CR_TAB
4824 "ldd %B0,%1+1" CR_TAB
4825 "ldd __tmp_reg__,%1+2" CR_TAB
4826 "ldd %D0,%1+3" CR_TAB
4827 "mov %C0,__tmp_reg__");
4828 else
4829 return *l=4, ("ld %A0,%1" CR_TAB
4830 "ldd %B0,%1+1" CR_TAB
4831 "ldd %C0,%1+2" CR_TAB
4832 "ldd %D0,%1+3");
4833 }
4834 }
4835 else if (GET_CODE (base) == PLUS) /* (R + i) */
4836 {
4837 int disp = INTVAL (XEXP (base, 1));
4838
4839 if (AVR_TINY)
4840 return avr_out_movsi_r_mr_reg_disp_tiny (insn, op, l);
4841
4842 if (disp > MAX_LD_OFFSET (GET_MODE (src)))
4843 {
4844 if (REGNO (XEXP (base, 0)) != REG_Y)
4845 fatal_insn ("incorrect insn:",insn);
4846
4847 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (src)))
4848 return *l = 6, ("adiw r28,%o1-60" CR_TAB
4849 "ldd %A0,Y+60" CR_TAB
4850 "ldd %B0,Y+61" CR_TAB
4851 "ldd %C0,Y+62" CR_TAB
4852 "ldd %D0,Y+63" CR_TAB
4853 "sbiw r28,%o1-60");
4854
4855 return *l = 8, ("subi r28,lo8(-%o1)" CR_TAB
4856 "sbci r29,hi8(-%o1)" CR_TAB
4857 "ld %A0,Y" CR_TAB
4858 "ldd %B0,Y+1" CR_TAB
4859 "ldd %C0,Y+2" CR_TAB
4860 "ldd %D0,Y+3" CR_TAB
4861 "subi r28,lo8(%o1)" CR_TAB
4862 "sbci r29,hi8(%o1)");
4863 }
4864
4865 reg_base = true_regnum (XEXP (base, 0));
4866 if (reg_base == REG_X)
4867 {
4868 /* R = (X + d) */
4869 if (reg_dest == REG_X)
4870 {
4871 *l = 7;
4872 /* "ld r26,-X" is undefined */
4873 return ("adiw r26,%o1+3" CR_TAB
4874 "ld r29,X" CR_TAB
4875 "ld r28,-X" CR_TAB
4876 "ld __tmp_reg__,-X" CR_TAB
4877 "sbiw r26,1" CR_TAB
4878 "ld r26,X" CR_TAB
4879 "mov r27,__tmp_reg__");
4880 }
4881 *l = 6;
4882 if (reg_dest == REG_X - 2)
4883 return ("adiw r26,%o1" CR_TAB
4884 "ld r24,X+" CR_TAB
4885 "ld r25,X+" CR_TAB
4886 "ld __tmp_reg__,X+" CR_TAB
4887 "ld r27,X" CR_TAB
4888 "mov r26,__tmp_reg__");
4889
4890 return ("adiw r26,%o1" CR_TAB
4891 "ld %A0,X+" CR_TAB
4892 "ld %B0,X+" CR_TAB
4893 "ld %C0,X+" CR_TAB
4894 "ld %D0,X" CR_TAB
4895 "sbiw r26,%o1+3");
4896 }
4897 if (reg_dest == reg_base)
4898 return *l=5, ("ldd %D0,%D1" CR_TAB
4899 "ldd %C0,%C1" CR_TAB
4900 "ldd __tmp_reg__,%B1" CR_TAB
4901 "ldd %A0,%A1" CR_TAB
4902 "mov %B0,__tmp_reg__");
4903 else if (reg_dest == reg_base - 2)
4904 return *l=5, ("ldd %A0,%A1" CR_TAB
4905 "ldd %B0,%B1" CR_TAB
4906 "ldd __tmp_reg__,%C1" CR_TAB
4907 "ldd %D0,%D1" CR_TAB
4908 "mov %C0,__tmp_reg__");
4909 return *l=4, ("ldd %A0,%A1" CR_TAB
4910 "ldd %B0,%B1" CR_TAB
4911 "ldd %C0,%C1" CR_TAB
4912 "ldd %D0,%D1");
4913 }
4914 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
4915 return *l=4, ("ld %D0,%1" CR_TAB
4916 "ld %C0,%1" CR_TAB
4917 "ld %B0,%1" CR_TAB
4918 "ld %A0,%1");
4919 else if (GET_CODE (base) == POST_INC) /* (R++) */
4920 return *l=4, ("ld %A0,%1" CR_TAB
4921 "ld %B0,%1" CR_TAB
4922 "ld %C0,%1" CR_TAB
4923 "ld %D0,%1");
4924 else if (CONSTANT_ADDRESS_P (base))
4925 {
4926 if (io_address_operand (base, SImode))
4927 {
4928 *l = 4;
4929 return ("in %A0,%i1" CR_TAB
4930 "in %B0,%i1+1" CR_TAB
4931 "in %C0,%i1+2" CR_TAB
4932 "in %D0,%i1+3");
4933 }
4934 else
4935 {
4936 *l = AVR_TINY ? 4 : 8;
4937 return ("lds %A0,%m1" CR_TAB
4938 "lds %B0,%m1+1" CR_TAB
4939 "lds %C0,%m1+2" CR_TAB
4940 "lds %D0,%m1+3");
4941 }
4942 }
4943
4944 fatal_insn ("unknown move insn:",insn);
4945 return "";
4946 }
4947
4948 static const char *
4949 avr_out_movsi_mr_r_reg_no_disp_tiny (rtx_insn *insn, rtx op[], int *l)
4950 {
4951 rtx dest = op[0];
4952 rtx src = op[1];
4953 rtx base = XEXP (dest, 0);
4954 int reg_base = true_regnum (base);
4955 int reg_src = true_regnum (src);
4956
4957 if (reg_base == reg_src)
4958 {
4959 /* "ld r26,-X" is undefined */
4960 if (reg_unused_after (insn, base))
4961 {
4962 return *l = 7, ("mov __tmp_reg__,%B1" CR_TAB
4963 "st %0,%A1" CR_TAB
4964 TINY_ADIW (%E0, %F0, 1) CR_TAB
4965 "st %0+,__tmp_reg__" CR_TAB
4966 "st %0+,%C1" CR_TAB
4967 "st %0+,%D1");
4968 }
4969 else
4970 {
4971 return *l = 9, ("mov __tmp_reg__,%B1" CR_TAB
4972 "st %0,%A1" CR_TAB
4973 TINY_ADIW (%E0, %F0, 1) CR_TAB
4974 "st %0+,__tmp_reg__" CR_TAB
4975 "st %0+,%C1" CR_TAB
4976 "st %0+,%D1" CR_TAB
4977 TINY_SBIW (%E0, %F0, 3));
4978 }
4979 }
4980 else if (reg_base == reg_src + 2)
4981 {
4982 if (reg_unused_after (insn, base))
4983 return *l = 7, ("mov __zero_reg__,%C1" CR_TAB
4984 "mov __tmp_reg__,%D1" CR_TAB
4985 "st %0+,%A1" CR_TAB
4986 "st %0+,%B1" CR_TAB
4987 "st %0+,__zero_reg__" CR_TAB
4988 "st %0,__tmp_reg__" CR_TAB
4989 "clr __zero_reg__");
4990 else
4991 return *l = 9, ("mov __zero_reg__,%C1" CR_TAB
4992 "mov __tmp_reg__,%D1" CR_TAB
4993 "st %0+,%A1" CR_TAB
4994 "st %0+,%B1" CR_TAB
4995 "st %0+,__zero_reg__" CR_TAB
4996 "st %0,__tmp_reg__" CR_TAB
4997 "clr __zero_reg__" CR_TAB
4998 TINY_SBIW (%E0, %F0, 3));
4999 }
5000
5001 return *l = 6, ("st %0+,%A1" CR_TAB
5002 "st %0+,%B1" CR_TAB
5003 "st %0+,%C1" CR_TAB
5004 "st %0,%D1" CR_TAB
5005 TINY_SBIW (%E0, %F0, 3));
5006 }
5007
5008 static const char *
5009 avr_out_movsi_mr_r_reg_disp_tiny (rtx op[], int *l)
5010 {
5011 rtx dest = op[0];
5012 rtx src = op[1];
5013 rtx base = XEXP (dest, 0);
5014 int reg_base = REGNO (XEXP (base, 0));
5015 int reg_src = true_regnum (src);
5016
5017 if (reg_base == reg_src)
5018 {
5019 *l = 11;
5020 return ("mov __tmp_reg__,%A1" CR_TAB
5021 "mov __zero_reg__,%B1" CR_TAB
5022 TINY_ADIW (%I0, %J0, %o0) CR_TAB
5023 "st %b0+,__tmp_reg__" CR_TAB
5024 "st %b0+,__zero_reg__" CR_TAB
5025 "st %b0+,%C1" CR_TAB
5026 "st %b0,%D1" CR_TAB
5027 "clr __zero_reg__" CR_TAB
5028 TINY_SBIW (%I0, %J0, %o0+3));
5029 }
5030 else if (reg_src == reg_base - 2)
5031 {
5032 // This awkward case can occur when ext-dce turns zero-extend:SI(HI)
5033 // into a paradoxical subreg, which register allocation may turn into
5034 // something like *(R28:HI + 7) = R26:SI. There is actually no need
5035 // to store the upper 2 bytes of R26:SI as they are unused rubbish.
5036 // See PR116390.
5037 *l = 6;
5038 return (TINY_ADIW (%I0, %J0, %o0) CR_TAB
5039 "st %b0+,%A1" CR_TAB
5040 "st %b0,%B1" CR_TAB
5041 TINY_SBIW (%I0, %J0, %o0+1));
5042 }
5043 *l = 8;
5044 return (TINY_ADIW (%I0, %J0, %o0) CR_TAB
5045 "st %b0+,%A1" CR_TAB
5046 "st %b0+,%B1" CR_TAB
5047 "st %b0+,%C1" CR_TAB
5048 "st %b0,%D1" CR_TAB
5049 TINY_SBIW (%I0, %J0, %o0+3));
5050 }
5051
5052 static const char *
5053 out_movsi_mr_r (rtx_insn *insn, rtx op[], int *l)
5054 {
5055 rtx dest = op[0];
5056 rtx src = op[1];
5057 rtx base = XEXP (dest, 0);
5058 int reg_base = true_regnum (base);
5059 int reg_src = true_regnum (src);
5060 int tmp;
5061
5062 if (!l)
5063 l = &tmp;
5064
5065 if (CONSTANT_ADDRESS_P (base))
5066 {
5067 if (io_address_operand (base, SImode))
5068 {
5069 return *l=4,("out %i0,%A1" CR_TAB
5070 "out %i0+1,%B1" CR_TAB
5071 "out %i0+2,%C1" CR_TAB
5072 "out %i0+3,%D1");
5073 }
5074 else
5075 {
5076 *l = AVR_TINY ? 4 : 8;
5077 return ("sts %m0,%A1" CR_TAB
5078 "sts %m0+1,%B1" CR_TAB
5079 "sts %m0+2,%C1" CR_TAB
5080 "sts %m0+3,%D1");
5081 }
5082 }
5083
5084 if (reg_base > 0) /* (r) */
5085 {
5086 if (AVR_TINY)
5087 return avr_out_movsi_mr_r_reg_no_disp_tiny (insn, op, l);
5088
5089 if (reg_base == REG_X) /* (R26) */
5090 {
5091 if (reg_src == REG_X)
5092 {
5093 /* "st X+,r26" is undefined */
5094 if (reg_unused_after (insn, base))
5095 return *l=6, ("mov __tmp_reg__,r27" CR_TAB
5096 "st X,r26" CR_TAB
5097 "adiw r26,1" CR_TAB
5098 "st X+,__tmp_reg__" CR_TAB
5099 "st X+,r28" CR_TAB
5100 "st X,r29");
5101 else
5102 return *l=7, ("mov __tmp_reg__,r27" CR_TAB
5103 "st X,r26" CR_TAB
5104 "adiw r26,1" CR_TAB
5105 "st X+,__tmp_reg__" CR_TAB
5106 "st X+,r28" CR_TAB
5107 "st X,r29" CR_TAB
5108 "sbiw r26,3");
5109 }
5110 else if (reg_base == reg_src + 2)
5111 {
5112 if (reg_unused_after (insn, base))
5113 return *l=7, ("mov __zero_reg__,%C1" CR_TAB
5114 "mov __tmp_reg__,%D1" CR_TAB
5115 "st %0+,%A1" CR_TAB
5116 "st %0+,%B1" CR_TAB
5117 "st %0+,__zero_reg__" CR_TAB
5118 "st %0,__tmp_reg__" CR_TAB
5119 "clr __zero_reg__");
5120 else
5121 return *l=8, ("mov __zero_reg__,%C1" CR_TAB
5122 "mov __tmp_reg__,%D1" CR_TAB
5123 "st %0+,%A1" CR_TAB
5124 "st %0+,%B1" CR_TAB
5125 "st %0+,__zero_reg__" CR_TAB
5126 "st %0,__tmp_reg__" CR_TAB
5127 "clr __zero_reg__" CR_TAB
5128 "sbiw r26,3");
5129 }
5130 return *l=5, ("st %0+,%A1" CR_TAB
5131 "st %0+,%B1" CR_TAB
5132 "st %0+,%C1" CR_TAB
5133 "st %0,%D1" CR_TAB
5134 "sbiw r26,3");
5135 }
5136 else
5137 return *l=4, ("st %0,%A1" CR_TAB
5138 "std %0+1,%B1" CR_TAB
5139 "std %0+2,%C1" CR_TAB
5140 "std %0+3,%D1");
5141 }
5142 else if (GET_CODE (base) == PLUS) /* (R + i) */
5143 {
5144 int disp = INTVAL (XEXP (base, 1));
5145
5146 if (AVR_TINY)
5147 return avr_out_movsi_mr_r_reg_disp_tiny (op, l);
5148
5149 reg_base = REGNO (XEXP (base, 0));
5150 if (disp > MAX_LD_OFFSET (GET_MODE (dest)))
5151 {
5152 if (reg_base != REG_Y)
5153 fatal_insn ("incorrect insn:",insn);
5154
5155 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (dest)))
5156 return *l = 6, ("adiw r28,%o0-60" CR_TAB
5157 "std Y+60,%A1" CR_TAB
5158 "std Y+61,%B1" CR_TAB
5159 "std Y+62,%C1" CR_TAB
5160 "std Y+63,%D1" CR_TAB
5161 "sbiw r28,%o0-60");
5162
5163 return *l = 8, ("subi r28,lo8(-%o0)" CR_TAB
5164 "sbci r29,hi8(-%o0)" CR_TAB
5165 "st Y,%A1" CR_TAB
5166 "std Y+1,%B1" CR_TAB
5167 "std Y+2,%C1" CR_TAB
5168 "std Y+3,%D1" CR_TAB
5169 "subi r28,lo8(%o0)" CR_TAB
5170 "sbci r29,hi8(%o0)");
5171 }
5172 if (reg_base == REG_X)
5173 {
5174 /* (X + d) = R */
5175 if (reg_src == REG_X)
5176 {
5177 *l = 9;
5178 return ("mov __tmp_reg__,r26" CR_TAB
5179 "mov __zero_reg__,r27" CR_TAB
5180 "adiw r26,%o0" CR_TAB
5181 "st X+,__tmp_reg__" CR_TAB
5182 "st X+,__zero_reg__" CR_TAB
5183 "st X+,r28" CR_TAB
5184 "st X,r29" CR_TAB
5185 "clr __zero_reg__" CR_TAB
5186 "sbiw r26,%o0+3");
5187 }
5188 else if (reg_src == REG_X - 2)
5189 {
5190 *l = 9;
5191 return ("mov __tmp_reg__,r26" CR_TAB
5192 "mov __zero_reg__,r27" CR_TAB
5193 "adiw r26,%o0" CR_TAB
5194 "st X+,r24" CR_TAB
5195 "st X+,r25" CR_TAB
5196 "st X+,__tmp_reg__" CR_TAB
5197 "st X,__zero_reg__" CR_TAB
5198 "clr __zero_reg__" CR_TAB
5199 "sbiw r26,%o0+3");
5200 }
5201 *l = 6;
5202 return ("adiw r26,%o0" CR_TAB
5203 "st X+,%A1" CR_TAB
5204 "st X+,%B1" CR_TAB
5205 "st X+,%C1" CR_TAB
5206 "st X,%D1" CR_TAB
5207 "sbiw r26,%o0+3");
5208 }
5209 return *l=4, ("std %A0,%A1" CR_TAB
5210 "std %B0,%B1" CR_TAB
5211 "std %C0,%C1" CR_TAB
5212 "std %D0,%D1");
5213 }
5214 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
5215 return *l=4, ("st %0,%D1" CR_TAB
5216 "st %0,%C1" CR_TAB
5217 "st %0,%B1" CR_TAB
5218 "st %0,%A1");
5219 else if (GET_CODE (base) == POST_INC) /* (R++) */
5220 return *l=4, ("st %0,%A1" CR_TAB
5221 "st %0,%B1" CR_TAB
5222 "st %0,%C1" CR_TAB
5223 "st %0,%D1");
5224 fatal_insn ("unknown move insn:",insn);
5225 return "";
5226 }
5227
5228 const char *
5229 output_movsisf (rtx_insn *insn, rtx operands[], int *l)
5230 {
5231 int dummy;
5232 rtx dest = operands[0];
5233 rtx src = operands[1];
5234 int *real_l = l;
5235
5236 if (avr_mem_flash_p (src)
5237 || avr_mem_flash_p (dest))
5238 {
5239 return avr_out_lpm (insn, operands, real_l);
5240 }
5241
5242 if (!l)
5243 l = &dummy;
5244
5245 gcc_assert (GET_MODE_SIZE (GET_MODE (dest)) == 4);
5246
5247 if (REG_P (dest))
5248 {
5249 if (REG_P (src)) /* mov r,r */
5250 {
5251 if (true_regnum (dest) > true_regnum (src))
5252 {
5253 if (AVR_HAVE_MOVW)
5254 {
5255 *l = 2;
5256 return ("movw %C0,%C1" CR_TAB
5257 "movw %A0,%A1");
5258 }
5259 *l = 4;
5260 return ("mov %D0,%D1" CR_TAB
5261 "mov %C0,%C1" CR_TAB
5262 "mov %B0,%B1" CR_TAB
5263 "mov %A0,%A1");
5264 }
5265 else
5266 {
5267 if (AVR_HAVE_MOVW)
5268 {
5269 *l = 2;
5270 return ("movw %A0,%A1" CR_TAB
5271 "movw %C0,%C1");
5272 }
5273 *l = 4;
5274 return ("mov %A0,%A1" CR_TAB
5275 "mov %B0,%B1" CR_TAB
5276 "mov %C0,%C1" CR_TAB
5277 "mov %D0,%D1");
5278 }
5279 }
5280 else if (CONSTANT_P (src))
5281 {
5282 return output_reload_insisf (operands, NULL_RTX, real_l);
5283 }
5284 else if (MEM_P (src))
5285 return out_movsi_r_mr (insn, operands, real_l); /* mov r,m */
5286 }
5287 else if (MEM_P (dest))
5288 {
5289 const char *templ;
5290
5291 if (src == CONST0_RTX (GET_MODE (dest)))
5292 operands[1] = zero_reg_rtx;
5293
5294 templ = out_movsi_mr_r (insn, operands, real_l);
5295
5296 if (!real_l)
5297 output_asm_insn (templ, operands);
5298
5299 operands[1] = src;
5300 return "";
5301 }
5302 fatal_insn ("invalid insn:", insn);
5303 return "";
5304 }
5305
5306
5307 /* Handle loads of 24-bit types from memory to register. */
5308
5309 static const char *
5310 avr_out_load_psi_reg_no_disp_tiny (rtx_insn *insn, rtx *op, int *plen)
5311 {
5312 rtx dest = op[0];
5313 rtx src = op[1];
5314 rtx base = XEXP (src, 0);
5315 int reg_dest = true_regnum (dest);
5316 int reg_base = true_regnum (base);
5317
5318 if (reg_base == reg_dest)
5319 {
5320 return avr_asm_len (TINY_ADIW (%E1, %F1, 2) CR_TAB
5321 "ld %C0,%1" CR_TAB
5322 "ld __tmp_reg__,-%1" CR_TAB
5323 TINY_SBIW (%E1, %F1, 1) CR_TAB
5324 "ld %A0,%1" CR_TAB
5325 "mov %B0,__tmp_reg__", op, plen, -8);
5326 }
5327 else
5328 {
5329 avr_asm_len ("ld %A0,%1+" CR_TAB
5330 "ld %B0,%1+" CR_TAB
5331 "ld %C0,%1", op, plen, -3);
5332
5333 if (reg_dest != reg_base - 2
5334 && !reg_unused_after (insn, base))
5335 {
5336 avr_asm_len (TINY_SBIW (%E1, %F1, 2), op, plen, 2);
5337 }
5338 return "";
5339 }
5340 }
5341
5342 static const char *
5343 avr_out_load_psi_reg_disp_tiny (rtx_insn *insn, rtx *op, int *plen)
5344 {
5345 rtx dest = op[0];
5346 rtx src = op[1];
5347 rtx base = XEXP (src, 0);
5348 int reg_dest = true_regnum (dest);
5349 int reg_base = true_regnum (base);
5350
5351 reg_base = true_regnum (XEXP (base, 0));
5352 if (reg_base == reg_dest)
5353 {
5354 return avr_asm_len (TINY_ADIW (%I1, %J1, %o1+2) CR_TAB
5355 "ld %C0,%b1" CR_TAB
5356 "ld __tmp_reg__,-%b1" CR_TAB
5357 TINY_SBIW (%I1, %J1, 1) CR_TAB
5358 "ld %A0,%b1" CR_TAB
5359 "mov %B0,__tmp_reg__", op, plen, -8);
5360 }
5361 else
5362 {
5363 avr_asm_len (TINY_ADIW (%I1, %J1, %o1) CR_TAB
5364 "ld %A0,%b1+" CR_TAB
5365 "ld %B0,%b1+" CR_TAB
5366 "ld %C0,%b1", op, plen, -5);
5367
5368 if (reg_dest != reg_base - 2
5369 && !reg_unused_after (insn, XEXP (base, 0)))
5370 avr_asm_len (TINY_SBIW (%I1, %J1, %o1+2), op, plen, 2);
5371
5372 return "";
5373 }
5374 }
5375
5376 static const char *
5377 avr_out_load_psi (rtx_insn *insn, rtx *op, int *plen)
5378 {
5379 rtx dest = op[0];
5380 rtx src = op[1];
5381 rtx base = XEXP (src, 0);
5382 int reg_dest = true_regnum (dest);
5383 int reg_base = true_regnum (base);
5384
5385 if (reg_base > 0)
5386 {
5387 if (AVR_TINY)
5388 return avr_out_load_psi_reg_no_disp_tiny (insn, op, plen);
5389
5390 if (reg_base == REG_X) /* (R26) */
5391 {
5392 if (reg_dest == REG_X)
5393 /* "ld r26,-X" is undefined */
5394 return avr_asm_len ("adiw r26,2" CR_TAB
5395 "ld r28,X" CR_TAB
5396 "ld __tmp_reg__,-X" CR_TAB
5397 "sbiw r26,1" CR_TAB
5398 "ld r26,X" CR_TAB
5399 "mov r27,__tmp_reg__", op, plen, -6);
5400 else
5401 {
5402 avr_asm_len ("ld %A0,X+" CR_TAB
5403 "ld %B0,X+" CR_TAB
5404 "ld %C0,X", op, plen, -3);
5405
5406 if (reg_dest != REG_X - 2
5407 && !reg_unused_after (insn, base))
5408 {
5409 avr_asm_len ("sbiw r26,2", op, plen, 1);
5410 }
5411
5412 return "";
5413 }
5414 }
5415 else /* reg_base != REG_X */
5416 {
5417 if (reg_dest == reg_base)
5418 return avr_asm_len ("ldd %C0,%1+2" CR_TAB
5419 "ldd __tmp_reg__,%1+1" CR_TAB
5420 "ld %A0,%1" CR_TAB
5421 "mov %B0,__tmp_reg__", op, plen, -4);
5422 else
5423 return avr_asm_len ("ld %A0,%1" CR_TAB
5424 "ldd %B0,%1+1" CR_TAB
5425 "ldd %C0,%1+2", op, plen, -3);
5426 }
5427 }
5428 else if (GET_CODE (base) == PLUS) /* (R + i) */
5429 {
5430 int disp = INTVAL (XEXP (base, 1));
5431
5432 if (AVR_TINY)
5433 return avr_out_load_psi_reg_disp_tiny (insn, op, plen);
5434
5435 if (disp > MAX_LD_OFFSET (GET_MODE (src)))
5436 {
5437 if (REGNO (XEXP (base, 0)) != REG_Y)
5438 fatal_insn ("incorrect insn:",insn);
5439
5440 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (src)))
5441 return avr_asm_len ("adiw r28,%o1-61" CR_TAB
5442 "ldd %A0,Y+61" CR_TAB
5443 "ldd %B0,Y+62" CR_TAB
5444 "ldd %C0,Y+63" CR_TAB
5445 "sbiw r28,%o1-61", op, plen, -5);
5446
5447 return avr_asm_len ("subi r28,lo8(-%o1)" CR_TAB
5448 "sbci r29,hi8(-%o1)" CR_TAB
5449 "ld %A0,Y" CR_TAB
5450 "ldd %B0,Y+1" CR_TAB
5451 "ldd %C0,Y+2" CR_TAB
5452 "subi r28,lo8(%o1)" CR_TAB
5453 "sbci r29,hi8(%o1)", op, plen, -7);
5454 }
5455
5456 reg_base = true_regnum (XEXP (base, 0));
5457 if (reg_base == REG_X)
5458 {
5459 /* R = (X + d) */
5460 if (reg_dest == REG_X)
5461 {
5462 /* "ld r26,-X" is undefined */
5463 return avr_asm_len ("adiw r26,%o1+2" CR_TAB
5464 "ld r28,X" CR_TAB
5465 "ld __tmp_reg__,-X" CR_TAB
5466 "sbiw r26,1" CR_TAB
5467 "ld r26,X" CR_TAB
5468 "mov r27,__tmp_reg__", op, plen, -6);
5469 }
5470
5471 avr_asm_len ("adiw r26,%o1" CR_TAB
5472 "ld %A0,X+" CR_TAB
5473 "ld %B0,X+" CR_TAB
5474 "ld %C0,X", op, plen, -4);
5475
5476 if (reg_dest != REG_W
5477 && !reg_unused_after (insn, XEXP (base, 0)))
5478 avr_asm_len ("sbiw r26,%o1+2", op, plen, 1);
5479
5480 return "";
5481 }
5482
5483 if (reg_dest == reg_base)
5484 return avr_asm_len ("ldd %C0,%C1" CR_TAB
5485 "ldd __tmp_reg__,%B1" CR_TAB
5486 "ldd %A0,%A1" CR_TAB
5487 "mov %B0,__tmp_reg__", op, plen, -4);
5488
5489 return avr_asm_len ("ldd %A0,%A1" CR_TAB
5490 "ldd %B0,%B1" CR_TAB
5491 "ldd %C0,%C1", op, plen, -3);
5492 }
5493 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
5494 return avr_asm_len ("ld %C0,%1" CR_TAB
5495 "ld %B0,%1" CR_TAB
5496 "ld %A0,%1", op, plen, -3);
5497 else if (GET_CODE (base) == POST_INC) /* (R++) */
5498 return avr_asm_len ("ld %A0,%1" CR_TAB
5499 "ld %B0,%1" CR_TAB
5500 "ld %C0,%1", op, plen, -3);
5501
5502 else if (CONSTANT_ADDRESS_P (base))
5503 {
5504 int n_words = AVR_TINY ? 3 : 6;
5505 return avr_asm_len ("lds %A0,%m1" CR_TAB
5506 "lds %B0,%m1+1" CR_TAB
5507 "lds %C0,%m1+2", op, plen , -n_words);
5508 }
5509
5510 fatal_insn ("unknown move insn:",insn);
5511 return "";
5512 }
5513
5514
5515 static const char *
5516 avr_out_store_psi_reg_no_disp_tiny (rtx_insn *insn, rtx *op, int *plen)
5517 {
5518 rtx dest = op[0];
5519 rtx src = op[1];
5520 rtx base = XEXP (dest, 0);
5521 int reg_base = true_regnum (base);
5522 int reg_src = true_regnum (src);
5523
5524 if (reg_base == reg_src)
5525 {
5526 avr_asm_len ("st %0,%A1" CR_TAB
5527 "mov __tmp_reg__,%B1" CR_TAB
5528 TINY_ADIW (%E0, %F0, 1) CR_TAB /* st X+, r27 is undefined */
5529 "st %0+,__tmp_reg__" CR_TAB
5530 "st %0,%C1", op, plen, -6);
5531
5532 }
5533 else if (reg_src == reg_base - 2)
5534 {
5535 avr_asm_len ("st %0,%A1" CR_TAB
5536 "mov __tmp_reg__,%C1" CR_TAB
5537 TINY_ADIW (%E0, %F0, 1) CR_TAB
5538 "st %0+,%B1" CR_TAB
5539 "st %0,__tmp_reg__", op, plen, 6);
5540 }
5541 else
5542 {
5543 avr_asm_len ("st %0+,%A1" CR_TAB
5544 "st %0+,%B1" CR_TAB
5545 "st %0,%C1", op, plen, -3);
5546 }
5547
5548 if (!reg_unused_after (insn, base))
5549 avr_asm_len (TINY_SBIW (%E0, %F0, 2), op, plen, 2);
5550
5551 return "";
5552 }
5553
5554 static const char *
5555 avr_out_store_psi_reg_disp_tiny (rtx_insn *insn, rtx *op, int *plen)
5556 {
5557 rtx dest = op[0];
5558 rtx src = op[1];
5559 rtx base = XEXP (dest, 0);
5560 int reg_base = REGNO (XEXP (base, 0));
5561 int reg_src = true_regnum (src);
5562
5563 if (reg_src == reg_base)
5564 avr_asm_len ("mov __tmp_reg__,%A1" CR_TAB
5565 "mov __zero_reg__,%B1" CR_TAB
5566 TINY_ADIW (%I0, %J0, %o0) CR_TAB
5567 "st %b0+,__tmp_reg__" CR_TAB
5568 "st %b0+,__zero_reg__" CR_TAB
5569 "st %b0,%C1" CR_TAB
5570 "clr __zero_reg__", op, plen, -8);
5571 else if (reg_src == reg_base - 2)
5572 avr_asm_len ("mov __tmp_reg__,%C1" CR_TAB
5573 TINY_ADIW (%I0, %J0, %o0) CR_TAB
5574 "st %b0+,%A1" CR_TAB
5575 "st %b0+,%B1" CR_TAB
5576 "st %b0,__tmp_reg__", op, plen, -6);
5577 else
5578 avr_asm_len (TINY_ADIW (%I0, %J0, %o0) CR_TAB
5579 "st %b0+,%A1" CR_TAB
5580 "st %b0+,%B1" CR_TAB
5581 "st %b0,%C1", op, plen, -5);
5582
5583 if (!reg_unused_after (insn, XEXP (base, 0)))
5584 avr_asm_len (TINY_SBIW (%I0, %J0, %o0+2), op, plen, 2);
5585
5586 return "";
5587 }
5588
5589 /* Handle store of 24-bit type from register or zero to memory. */
5590
5591 static const char *
5592 avr_out_store_psi (rtx_insn *insn, rtx *op, int *plen)
5593 {
5594 rtx dest = op[0];
5595 rtx src = op[1];
5596 rtx base = XEXP (dest, 0);
5597 int reg_base = true_regnum (base);
5598
5599 if (CONSTANT_ADDRESS_P (base))
5600 {
5601 int n_words = AVR_TINY ? 3 : 6;
5602 return avr_asm_len ("sts %m0,%A1" CR_TAB
5603 "sts %m0+1,%B1" CR_TAB
5604 "sts %m0+2,%C1", op, plen, -n_words);
5605 }
5606
5607 if (reg_base > 0) /* (r) */
5608 {
5609 if (AVR_TINY)
5610 return avr_out_store_psi_reg_no_disp_tiny (insn, op, plen);
5611
5612 if (reg_base == REG_X) /* (R26) */
5613 {
5614 gcc_assert (!reg_overlap_mentioned_p (base, src));
5615
5616 avr_asm_len ("st %0+,%A1" CR_TAB
5617 "st %0+,%B1" CR_TAB
5618 "st %0,%C1", op, plen, -3);
5619
5620 if (!reg_unused_after (insn, base))
5621 avr_asm_len ("sbiw r26,2", op, plen, 1);
5622
5623 return "";
5624 }
5625 else
5626 return avr_asm_len ("st %0,%A1" CR_TAB
5627 "std %0+1,%B1" CR_TAB
5628 "std %0+2,%C1", op, plen, -3);
5629 }
5630 else if (GET_CODE (base) == PLUS) /* (R + i) */
5631 {
5632 int disp = INTVAL (XEXP (base, 1));
5633
5634 if (AVR_TINY)
5635 return avr_out_store_psi_reg_disp_tiny (insn, op, plen);
5636
5637 reg_base = REGNO (XEXP (base, 0));
5638
5639 if (disp > MAX_LD_OFFSET (GET_MODE (dest)))
5640 {
5641 if (reg_base != REG_Y)
5642 fatal_insn ("incorrect insn:",insn);
5643
5644 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (dest)))
5645 return avr_asm_len ("adiw r28,%o0-61" CR_TAB
5646 "std Y+61,%A1" CR_TAB
5647 "std Y+62,%B1" CR_TAB
5648 "std Y+63,%C1" CR_TAB
5649 "sbiw r28,%o0-61", op, plen, -5);
5650
5651 return avr_asm_len ("subi r28,lo8(-%o0)" CR_TAB
5652 "sbci r29,hi8(-%o0)" CR_TAB
5653 "st Y,%A1" CR_TAB
5654 "std Y+1,%B1" CR_TAB
5655 "std Y+2,%C1" CR_TAB
5656 "subi r28,lo8(%o0)" CR_TAB
5657 "sbci r29,hi8(%o0)", op, plen, -7);
5658 }
5659 if (reg_base == REG_X)
5660 {
5661 /* (X + d) = R */
5662 gcc_assert (!reg_overlap_mentioned_p (XEXP (base, 0), src));
5663
5664 avr_asm_len ("adiw r26,%o0" CR_TAB
5665 "st X+,%A1" CR_TAB
5666 "st X+,%B1" CR_TAB
5667 "st X,%C1", op, plen, -4);
5668
5669 if (!reg_unused_after (insn, XEXP (base, 0)))
5670 avr_asm_len ("sbiw r26,%o0+2", op, plen, 1);
5671
5672 return "";
5673 }
5674
5675 return avr_asm_len ("std %A0,%A1" CR_TAB
5676 "std %B0,%B1" CR_TAB
5677 "std %C0,%C1", op, plen, -3);
5678 }
5679 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
5680 return avr_asm_len ("st %0,%C1" CR_TAB
5681 "st %0,%B1" CR_TAB
5682 "st %0,%A1", op, plen, -3);
5683 else if (GET_CODE (base) == POST_INC) /* (R++) */
5684 return avr_asm_len ("st %0,%A1" CR_TAB
5685 "st %0,%B1" CR_TAB
5686 "st %0,%C1", op, plen, -3);
5687
5688 fatal_insn ("unknown move insn:",insn);
5689 return "";
5690 }
5691
5692
5693 /* Move around 24-bit stuff. */
5694
5695 const char *
5696 avr_out_movpsi (rtx_insn *insn, rtx *op, int *plen)
5697 {
5698 rtx dest = op[0];
5699 rtx src = op[1];
5700
5701 if (avr_mem_flash_p (src)
5702 || avr_mem_flash_p (dest))
5703 {
5704 return avr_out_lpm (insn, op, plen);
5705 }
5706
5707 if (register_operand (dest, VOIDmode))
5708 {
5709 if (register_operand (src, VOIDmode)) /* mov r,r */
5710 {
5711 if (true_regnum (dest) > true_regnum (src))
5712 {
5713 avr_asm_len ("mov %C0,%C1", op, plen, -1);
5714
5715 if (AVR_HAVE_MOVW)
5716 return avr_asm_len ("movw %A0,%A1", op, plen, 1);
5717 else
5718 return avr_asm_len ("mov %B0,%B1" CR_TAB
5719 "mov %A0,%A1", op, plen, 2);
5720 }
5721 else
5722 {
5723 if (AVR_HAVE_MOVW)
5724 avr_asm_len ("movw %A0,%A1", op, plen, -1);
5725 else
5726 avr_asm_len ("mov %A0,%A1" CR_TAB
5727 "mov %B0,%B1", op, plen, -2);
5728
5729 return avr_asm_len ("mov %C0,%C1", op, plen, 1);
5730 }
5731 }
5732 else if (CONSTANT_P (src))
5733 {
5734 return avr_out_reload_inpsi (op, NULL_RTX, plen);
5735 }
5736 else if (MEM_P (src))
5737 return avr_out_load_psi (insn, op, plen); /* mov r,m */
5738 }
5739 else if (MEM_P (dest))
5740 {
5741 rtx xop[2];
5742
5743 xop[0] = dest;
5744 xop[1] = src == CONST0_RTX (GET_MODE (dest)) ? zero_reg_rtx : src;
5745
5746 return avr_out_store_psi (insn, xop, plen);
5747 }
5748
5749 fatal_insn ("invalid insn:", insn);
5750 return "";
5751 }
5752
5753 static const char *
5754 avr_out_movqi_mr_r_reg_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
5755 {
5756 rtx dest = op[0];
5757 rtx src = op[1];
5758 rtx x = XEXP (dest, 0);
5759
5760 if (reg_overlap_mentioned_p (src, XEXP (x, 0)))
5761 {
5762 avr_asm_len ("mov __tmp_reg__,%1" CR_TAB
5763 TINY_ADIW (%I0, %J0, %o0) CR_TAB
5764 "st %b0,__tmp_reg__", op, plen, -4);
5765 }
5766 else
5767 {
5768 avr_asm_len (TINY_ADIW (%I0, %J0, %o0) CR_TAB
5769 "st %b0,%1", op, plen, -3);
5770 }
5771
5772 if (!reg_unused_after (insn, XEXP (x, 0)))
5773 avr_asm_len (TINY_SBIW (%I0, %J0, %o0), op, plen, 2);
5774
5775 return "";
5776 }
5777
5778 static const char *
5779 out_movqi_mr_r (rtx_insn *insn, rtx op[], int *plen)
5780 {
5781 rtx dest = op[0];
5782 rtx src = op[1];
5783 rtx x = XEXP (dest, 0);
5784
5785 if (CONSTANT_ADDRESS_P (x))
5786 {
5787 int n_words = AVR_TINY ? 1 : 2;
5788 return io_address_operand (x, QImode)
5789 ? avr_asm_len ("out %i0,%1", op, plen, -1)
5790 : avr_asm_len ("sts %m0,%1", op, plen, -n_words);
5791 }
5792 else if (GET_CODE (x) == PLUS
5793 && REG_P (XEXP (x, 0))
5794 && CONST_INT_P (XEXP (x, 1)))
5795 {
5796 /* memory access by reg+disp */
5797
5798 int disp = INTVAL (XEXP (x, 1));
5799
5800 if (AVR_TINY)
5801 return avr_out_movqi_mr_r_reg_disp_tiny (insn, op, plen);
5802
5803 if (disp - GET_MODE_SIZE (GET_MODE (dest)) >= 63)
5804 {
5805 if (REGNO (XEXP (x, 0)) != REG_Y)
5806 fatal_insn ("incorrect insn:",insn);
5807
5808 if (disp <= 63 + MAX_LD_OFFSET (GET_MODE (dest)))
5809 return avr_asm_len ("adiw r28,%o0-63" CR_TAB
5810 "std Y+63,%1" CR_TAB
5811 "sbiw r28,%o0-63", op, plen, -3);
5812
5813 return avr_asm_len ("subi r28,lo8(-%o0)" CR_TAB
5814 "sbci r29,hi8(-%o0)" CR_TAB
5815 "st Y,%1" CR_TAB
5816 "subi r28,lo8(%o0)" CR_TAB
5817 "sbci r29,hi8(%o0)", op, plen, -5);
5818 }
5819 else if (REGNO (XEXP (x, 0)) == REG_X)
5820 {
5821 if (reg_overlap_mentioned_p (src, XEXP (x, 0)))
5822 {
5823 avr_asm_len ("mov __tmp_reg__,%1" CR_TAB
5824 "adiw r26,%o0" CR_TAB
5825 "st X,__tmp_reg__", op, plen, -3);
5826 }
5827 else
5828 {
5829 avr_asm_len ("adiw r26,%o0" CR_TAB
5830 "st X,%1", op, plen, -2);
5831 }
5832
5833 if (!reg_unused_after (insn, XEXP (x, 0)))
5834 avr_asm_len ("sbiw r26,%o0", op, plen, 1);
5835
5836 return "";
5837 }
5838
5839 return avr_asm_len ("std %0,%1", op, plen, -1);
5840 }
5841
5842 return avr_asm_len ("st %0,%1", op, plen, -1);
5843 }
5844
5845
5846 /* Helper for the next function for XMEGA. It does the same
5847 but with low byte first. */
5848
5849 static const char *
5850 avr_out_movhi_mr_r_xmega (rtx_insn *insn, rtx op[], int *plen)
5851 {
5852 rtx dest = op[0];
5853 rtx src = op[1];
5854 rtx base = XEXP (dest, 0);
5855 int reg_base = true_regnum (base);
5856 int reg_src = true_regnum (src);
5857
5858 /* "volatile" forces writing low byte first, even if less efficient,
5859 for correct operation with 16-bit I/O registers like SP. */
5860 bool mem_volatile_p = MEM_VOLATILE_P (dest);
5861
5862 if (CONSTANT_ADDRESS_P (base))
5863 {
5864 return io_address_operand (base, HImode)
5865 ? avr_asm_len ("out %i0,%A1" CR_TAB
5866 "out %i0+1,%B1", op, plen, -2)
5867
5868 : avr_asm_len ("sts %m0,%A1" CR_TAB
5869 "sts %m0+1,%B1", op, plen, -4);
5870 }
5871
5872 if (reg_base > 0)
5873 {
5874 if (reg_base != REG_X)
5875 return avr_asm_len ("st %0,%A1" CR_TAB
5876 "std %0+1,%B1", op, plen, -2);
5877
5878 if (reg_src == REG_X)
5879 /* "st X+,r26" and "st -X,r26" are undefined. */
5880 avr_asm_len ("mov __tmp_reg__,r27" CR_TAB
5881 "st X,r26" CR_TAB
5882 "adiw r26,1" CR_TAB
5883 "st X,__tmp_reg__", op, plen, -4);
5884 else
5885 avr_asm_len ("st X+,%A1" CR_TAB
5886 "st X,%B1", op, plen, -2);
5887
5888 return reg_unused_after (insn, base)
5889 ? ""
5890 : avr_asm_len ("sbiw r26,1", op, plen, 1);
5891 }
5892 else if (GET_CODE (base) == PLUS)
5893 {
5894 int disp = INTVAL (XEXP (base, 1));
5895 reg_base = REGNO (XEXP (base, 0));
5896 if (disp > MAX_LD_OFFSET (GET_MODE (dest)))
5897 {
5898 if (reg_base != REG_Y)
5899 fatal_insn ("incorrect insn:",insn);
5900
5901 return disp <= 63 + MAX_LD_OFFSET (GET_MODE (dest))
5902 ? avr_asm_len ("adiw r28,%o0-62" CR_TAB
5903 "std Y+62,%A1" CR_TAB
5904 "std Y+63,%B1" CR_TAB
5905 "sbiw r28,%o0-62", op, plen, -4)
5906
5907 : avr_asm_len ("subi r28,lo8(-%o0)" CR_TAB
5908 "sbci r29,hi8(-%o0)" CR_TAB
5909 "st Y,%A1" CR_TAB
5910 "std Y+1,%B1" CR_TAB
5911 "subi r28,lo8(%o0)" CR_TAB
5912 "sbci r29,hi8(%o0)", op, plen, -6);
5913 }
5914
5915 if (reg_base != REG_X)
5916 return avr_asm_len ("std %A0,%A1" CR_TAB
5917 "std %B0,%B1", op, plen, -2);
5918 /* (X + d) = R */
5919 return reg_src == REG_X
5920 ? avr_asm_len ("mov __tmp_reg__,r26" CR_TAB
5921 "mov __zero_reg__,r27" CR_TAB
5922 "adiw r26,%o0" CR_TAB
5923 "st X+,__tmp_reg__" CR_TAB
5924 "st X,__zero_reg__" CR_TAB
5925 "clr __zero_reg__" CR_TAB
5926 "sbiw r26,%o0+1", op, plen, -7)
5927
5928 : avr_asm_len ("adiw r26,%o0" CR_TAB
5929 "st X+,%A1" CR_TAB
5930 "st X,%B1" CR_TAB
5931 "sbiw r26,%o0+1", op, plen, -4);
5932 }
5933 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
5934 {
5935 if (!mem_volatile_p)
5936 return avr_asm_len ("st %0,%B1" CR_TAB
5937 "st %0,%A1", op, plen, -2);
5938
5939 return REGNO (XEXP (base, 0)) == REG_X
5940 ? avr_asm_len ("sbiw r26,2" CR_TAB
5941 "st X+,%A1" CR_TAB
5942 "st X,%B1" CR_TAB
5943 "sbiw r26,1", op, plen, -4)
5944
5945 : avr_asm_len ("sbiw %r0,2" CR_TAB
5946 "st %p0,%A1" CR_TAB
5947 "std %p0+1,%B1", op, plen, -3);
5948 }
5949 else if (GET_CODE (base) == POST_INC) /* (R++) */
5950 {
5951 return avr_asm_len ("st %0,%A1" CR_TAB
5952 "st %0,%B1", op, plen, -2);
5953
5954 }
5955 fatal_insn ("unknown move insn:",insn);
5956 return "";
5957 }
5958
5959 static const char *
5960 avr_out_movhi_mr_r_reg_no_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
5961 {
5962 rtx dest = op[0];
5963 rtx src = op[1];
5964 rtx base = XEXP (dest, 0);
5965 int reg_base = true_regnum (base);
5966 int reg_src = true_regnum (src);
5967 bool mem_volatile_p = MEM_VOLATILE_P (dest);
5968
5969 if (reg_base == reg_src)
5970 {
5971 return !mem_volatile_p && reg_unused_after (insn, src)
5972 ? avr_asm_len ("mov __tmp_reg__,%B1" CR_TAB
5973 "st %0,%A1" CR_TAB
5974 TINY_ADIW (%E0, %F0, 1) CR_TAB
5975 "st %0,__tmp_reg__", op, plen, -5)
5976 : avr_asm_len ("mov __tmp_reg__,%B1" CR_TAB
5977 TINY_ADIW (%E0, %F0, 1) CR_TAB
5978 "st %0,__tmp_reg__" CR_TAB
5979 TINY_SBIW (%E0, %F0, 1) CR_TAB
5980 "st %0,%A1", op, plen, -7);
5981 }
5982
5983 return !mem_volatile_p && reg_unused_after (insn, base)
5984 ? avr_asm_len ("st %0+,%A1" CR_TAB
5985 "st %0,%B1", op, plen, -2)
5986 : avr_asm_len (TINY_ADIW (%E0, %F0, 1) CR_TAB
5987 "st %0,%B1" CR_TAB
5988 "st -%0,%A1", op, plen, -4);
5989 }
5990
5991 static const char *
5992 avr_out_movhi_mr_r_reg_disp_tiny (rtx_insn *insn, rtx op[], int *plen)
5993 {
5994 rtx dest = op[0];
5995 rtx src = op[1];
5996 rtx base = XEXP (dest, 0);
5997 int reg_base = REGNO (XEXP (base, 0));
5998 int reg_src = true_regnum (src);
5999
6000 if (reg_src == reg_base)
6001 avr_asm_len ("mov __tmp_reg__,%A1" CR_TAB
6002 "mov __zero_reg__,%B1" CR_TAB
6003 TINY_ADIW (%I0, %J0, %o0+1) CR_TAB
6004 "st %b0,__zero_reg__" CR_TAB
6005 "st -%b0,__tmp_reg__" CR_TAB
6006 "clr __zero_reg__", op, plen, -7);
6007 else
6008 avr_asm_len (TINY_ADIW (%I0, %J0, %o0+1) CR_TAB
6009 "st %b0,%B1" CR_TAB
6010 "st -%b0,%A1", op, plen, -4);
6011
6012 if (!reg_unused_after (insn, XEXP (base, 0)))
6013 avr_asm_len (TINY_SBIW (%I0, %J0, %o0), op, plen, 2);
6014
6015 return "";
6016 }
6017
6018 static const char *
6019 avr_out_movhi_mr_r_post_inc_tiny (rtx op[], int *plen)
6020 {
6021 return avr_asm_len (TINY_ADIW (%I0, %J0, 1) CR_TAB
6022 "st %p0,%B1" CR_TAB
6023 "st -%p0,%A1" CR_TAB
6024 TINY_ADIW (%I0, %J0, 2), op, plen, -6);
6025 }
6026
6027 static const char *
6028 out_movhi_mr_r (rtx_insn *insn, rtx op[], int *plen)
6029 {
6030 rtx dest = op[0];
6031 rtx src = op[1];
6032 rtx base = XEXP (dest, 0);
6033 int reg_base = true_regnum (base);
6034 int reg_src = true_regnum (src);
6035
6036 /* "volatile" forces writing high-byte first (no-xmega) resp.
6037 low-byte first (xmega) even if less efficient, for correct
6038 operation with 16-bit I/O registers like. */
6039
6040 if (AVR_XMEGA)
6041 return avr_out_movhi_mr_r_xmega (insn, op, plen);
6042
6043 bool mem_volatile_p = MEM_VOLATILE_P (dest);
6044
6045 if (CONSTANT_ADDRESS_P (base))
6046 {
6047 int n_words = AVR_TINY ? 2 : 4;
6048 return io_address_operand (base, HImode)
6049 ? avr_asm_len ("out %i0+1,%B1" CR_TAB
6050 "out %i0,%A1", op, plen, -2)
6051
6052 : avr_asm_len ("sts %m0+1,%B1" CR_TAB
6053 "sts %m0,%A1", op, plen, -n_words);
6054 }
6055
6056 if (reg_base > 0)
6057 {
6058 if (AVR_TINY)
6059 return avr_out_movhi_mr_r_reg_no_disp_tiny (insn, op, plen);
6060
6061 if (reg_base != REG_X)
6062 return avr_asm_len ("std %0+1,%B1" CR_TAB
6063 "st %0,%A1", op, plen, -2);
6064
6065 if (reg_src == REG_X)
6066 /* "st X+,r26" and "st -X,r26" are undefined. */
6067 return !mem_volatile_p && reg_unused_after (insn, src)
6068 ? avr_asm_len ("mov __tmp_reg__,r27" CR_TAB
6069 "st X,r26" CR_TAB
6070 "adiw r26,1" CR_TAB
6071 "st X,__tmp_reg__", op, plen, -4)
6072
6073 : avr_asm_len ("mov __tmp_reg__,r27" CR_TAB
6074 "adiw r26,1" CR_TAB
6075 "st X,__tmp_reg__" CR_TAB
6076 "sbiw r26,1" CR_TAB
6077 "st X,r26", op, plen, -5);
6078
6079 return !mem_volatile_p && reg_unused_after (insn, base)
6080 ? avr_asm_len ("st X+,%A1" CR_TAB
6081 "st X,%B1", op, plen, -2)
6082 : avr_asm_len ("adiw r26,1" CR_TAB
6083 "st X,%B1" CR_TAB
6084 "st -X,%A1", op, plen, -3);
6085 }
6086 else if (GET_CODE (base) == PLUS)
6087 {
6088 int disp = INTVAL (XEXP (base, 1));
6089
6090 if (AVR_TINY)
6091 return avr_out_movhi_mr_r_reg_disp_tiny (insn, op, plen);
6092
6093 reg_base = REGNO (XEXP (base, 0));
6094 if (disp > MAX_LD_OFFSET (GET_MODE (dest)))
6095 {
6096 if (reg_base != REG_Y)
6097 fatal_insn ("incorrect insn:",insn);
6098
6099 return disp <= 63 + MAX_LD_OFFSET (GET_MODE (dest))
6100 ? avr_asm_len ("adiw r28,%o0-62" CR_TAB
6101 "std Y+63,%B1" CR_TAB
6102 "std Y+62,%A1" CR_TAB
6103 "sbiw r28,%o0-62", op, plen, -4)
6104
6105 : avr_asm_len ("subi r28,lo8(-%o0)" CR_TAB
6106 "sbci r29,hi8(-%o0)" CR_TAB
6107 "std Y+1,%B1" CR_TAB
6108 "st Y,%A1" CR_TAB
6109 "subi r28,lo8(%o0)" CR_TAB
6110 "sbci r29,hi8(%o0)", op, plen, -6);
6111 }
6112
6113 if (reg_base != REG_X)
6114 return avr_asm_len ("std %B0,%B1" CR_TAB
6115 "std %A0,%A1", op, plen, -2);
6116 /* (X + d) = R */
6117 return reg_src == REG_X
6118 ? avr_asm_len ("mov __tmp_reg__,r26" CR_TAB
6119 "mov __zero_reg__,r27" CR_TAB
6120 "adiw r26,%o0+1" CR_TAB
6121 "st X,__zero_reg__" CR_TAB
6122 "st -X,__tmp_reg__" CR_TAB
6123 "clr __zero_reg__" CR_TAB
6124 "sbiw r26,%o0", op, plen, -7)
6125
6126 : avr_asm_len ("adiw r26,%o0+1" CR_TAB
6127 "st X,%B1" CR_TAB
6128 "st -X,%A1" CR_TAB
6129 "sbiw r26,%o0", op, plen, -4);
6130 }
6131 else if (GET_CODE (base) == PRE_DEC) /* (--R) */
6132 {
6133 return avr_asm_len ("st %0,%B1" CR_TAB
6134 "st %0,%A1", op, plen, -2);
6135 }
6136 else if (GET_CODE (base) == POST_INC) /* (R++) */
6137 {
6138 if (!mem_volatile_p)
6139 return avr_asm_len ("st %0,%A1" CR_TAB
6140 "st %0,%B1", op, plen, -2);
6141
6142 if (AVR_TINY)
6143 return avr_out_movhi_mr_r_post_inc_tiny (op, plen);
6144
6145 return REGNO (XEXP (base, 0)) == REG_X
6146 ? avr_asm_len ("adiw r26,1" CR_TAB
6147 "st X,%B1" CR_TAB
6148 "st -X,%A1" CR_TAB
6149 "adiw r26,2", op, plen, -4)
6150
6151 : avr_asm_len ("std %p0+1,%B1" CR_TAB
6152 "st %p0,%A1" CR_TAB
6153 "adiw %r0,2", op, plen, -3);
6154 }
6155 fatal_insn ("unknown move insn:",insn);
6156 return "";
6157 }
6158
6159
6160 /* Output code for the "set_some" insn that sets some QImode GPRs.
6161 $0 is a parallel; for its layout see the description of the next function.
6162 $0[5] ... $0[8] are SETs of QImode registers to const_int values. All
6163 of them are bytes in the register described by $3 and $4. SET $0[5]
6164 is mandatory, but all the following ones are optional.
6165 $1 is a QImode scratch d-register or const0_rtx.
6166 $2 is the known 8-bit value held in $1 before the insn starts. When the
6167 code below clobbers $1, then it must restore $1 to $2 at the end.
6168 $3 The register number of a GPR.
6169 $4 The modesize of $3 in 1...4.
6170 PLEN == 0: Output instructions.
6171 PLEN != 0: Set *PLEN to the length of the sequence in words. */
6172
6173 const char *
6174 avr_out_set_some (rtx_insn *insn, rtx *xop, int *plen)
6175 {
6176 const int vlen = XVECLEN (xop[0], 0);
6177 const int sets_start = 5;
6178 gcc_assert (vlen > sets_start);
6179
6180 if (plen)
6181 *plen = 0;
6182
6183 rtx op[4];
6184 rtx &dest = op[0], &src = op[1], &scratch = op[2], &oldval = op[3];
6185 scratch = REG_P (xop[1]) ? xop[1] : NULL_RTX;
6186 oldval = NULL_RTX;
6187
6188 /* There are 3 ways to get a scratch, starting withe the most preferred ones:
6189 1) avr_find_unused_d_reg() need not to be restored, and it takes care
6190 of fixed regs. This is an unlikely case, e.g. with -fno-peephole2.
6191 2) "set_some" provides a scratch register with a known content.
6192 This scratch need not be saved but has to be restored to its value.
6193 3) A last resort approach saves and restores some upper register.
6194 Notice that "set_some" will only be emit when avr-fuse-move is fed
6195 with mov insn(s) that don't have a scratch reg but need one;
6196 hence "set_some" won't have a scratch reg at its disposal, either. */
6197
6198 bool knows_way_p = false;
6199
6200 for (int i = sets_start; i < vlen; ++i)
6201 {
6202 rtx xset = XVECEXP (xop[0], 0, i);
6203
6204 gcc_assert (GET_CODE (xset) == SET
6205 && REG_P (dest = XEXP (xset, 0))
6206 && CONST_INT_P (src = XEXP (xset, 1)));
6207
6208 if (src == const0_rtx)
6209 avr_asm_len ("clr %0", op, plen, 1);
6210 else if (src == const1_rtx)
6211 avr_asm_len ("clr %0" CR_TAB
6212 "inc %0", op, plen, 2);
6213 else if (src == constm1_rtx)
6214 avr_asm_len ("clr %0" CR_TAB
6215 "dec %0", op, plen, 2);
6216 else
6217 {
6218 if (! knows_way_p)
6219 {
6220 knows_way_p = true;
6221
6222 static const machine_mode size_to_mode[4 + 1] =
6223 {
6224 VOIDmode, QImode, HImode, PSImode, SImode
6225 };
6226
6227 const int ex_regno = INTVAL (xop[3]);
6228 const int ex_modesize = INTVAL (xop[4]);
6229 rtx exclude = gen_rtx_REG (size_to_mode[ex_modesize], ex_regno);
6230 rtx dreg = avr_find_unused_d_reg (insn, exclude);
6231
6232 if (dreg)
6233 {
6234 // Way 1
6235 scratch = dreg;
6236 oldval = NULL_RTX;
6237 }
6238 else if (scratch)
6239 {
6240 // Way 2
6241 if (! reg_unused_after (insn, scratch))
6242 oldval = xop[2];
6243 }
6244 else
6245 {
6246 // Way 3
6247 scratch = all_regs_rtx[REG_24];
6248 oldval = tmp_reg_rtx;
6249 avr_asm_len ("mov %3,%2", op, plen, 1);
6250 }
6251 } // decide about way
6252
6253 avr_asm_len ("ldi %2,%1" CR_TAB
6254 "mov %0,%2", op, plen, 2);
6255 } // needs a scratch
6256 } // for $0[5] ... $0[8].
6257
6258 if (oldval)
6259 avr_asm_len (REG_P (oldval)
6260 ? "mov %2,%3"
6261 : "ldi %2,%3", op, plen, 1);
6262 return "";
6263 }
6264
6265
6266 /* Implements the `set_some_operation' predicate.
6267 PARA is a parallel with the following elements:
6268 [0] is a USE of an 8-bit scratch d-register or const0_rtx.
6269 [1] is the known value held in [0]. When [0] is used as a scratch,
6270 then its value has to be restored to [1] after the respective insn.
6271 [2] is the regno of a GPR, and
6272 [3] is the mode size of that GPR. All SETs [5]... of PARA will set
6273 bytes of that GPR, but in many cases not all of them.
6274 [4] In a clobber of REG_CC.
6275 [5] [6] [7] [8] SETs of an 8-bit register to a const_int value, where
6276 all destinations are sub-bytes of [2]. Element [5] is mandatory,
6277 and the following elements are optional. */
6278
6279 bool
6280 avr_set_some_operation (rtx para)
6281 {
6282 const int sets_start = 5;
6283 const int n_sets = XVECLEN (para, 0) - sets_start;
6284
6285 if (! IN_RANGE (n_sets, 1, 4))
6286 return false;
6287
6288 if (GET_CODE (XVECEXP (para, 0, 4)) != CLOBBER
6289 || GET_CODE (XVECEXP (para, 0, 0)) != USE
6290 || GET_CODE (XVECEXP (para, 0, 1)) != USE
6291 || GET_CODE (XVECEXP (para, 0, 2)) != USE
6292 || GET_CODE (XVECEXP (para, 0, 3)) != USE)
6293 return false;
6294
6295 for (int i = sets_start; i < XVECLEN (para, 0); ++i)
6296 {
6297 rtx xset = XVECEXP (para, 0, i);
6298 if (! xset
6299 || GET_CODE (xset) != SET
6300 || ! register_operand (XEXP (xset, 0), QImode)
6301 || ! const_int_operand (XEXP (xset, 1), QImode))
6302 return false;
6303 }
6304
6305 return true;
6306 }
6307
6308
6309 /* Implement `TARGET_FRAME_POINTER_REQUIRED'. */
6310 /* Return 1 if frame pointer for current function required. */
6311
6312 static bool
6313 avr_frame_pointer_required_p (void)
6314 {
6315 return (cfun->calls_alloca
6316 || cfun->calls_setjmp
6317 || cfun->has_nonlocal_label
6318 || crtl->args.info.has_stack_args
6319 || get_frame_size () > 0);
6320 }
6321
6322
6323 /* Returns the condition of the branch following INSN, where INSN is some
6324 comparison. If the next insn is not a branch or the condition code set
6325 by INSN might be used by more insns than the next one, return UNKNOWN.
6326 For now, just look at the next insn, which misses some opportunities like
6327 following jumps. */
6328
6329 static rtx_code
6330 compare_condition (rtx_insn *insn)
6331 {
6332 rtx set;
6333 rtx_insn *next = next_real_nondebug_insn (insn);
6334
6335 if (next
6336 && JUMP_P (next)
6337 // If SREG does not die in the next insn, it is used in more than one
6338 // branch. This can happen due to pass .avr-ifelse optimizations.
6339 && dead_or_set_regno_p (next, REG_CC)
6340 // Branches are (set (pc) (if_then_else (COND (...)))).
6341 && (set = single_set (next))
6342 && GET_CODE (SET_SRC (set)) == IF_THEN_ELSE)
6343 {
6344 return GET_CODE (XEXP (SET_SRC (set), 0));
6345 }
6346
6347 return UNKNOWN;
6348 }
6349
6350
6351 /* Returns true if INSN is a tst insn that only tests the sign. */
6352
6353 static bool
6354 compare_sign_p (rtx_insn *insn)
6355 {
6356 rtx_code cond = compare_condition (insn);
6357 return (cond == GE || cond == LT);
6358 }
6359
6360
6361 /* Returns true if INSN is a compare insn with the EQ or NE condition. */
6362
6363 static bool
6364 compare_eq_p (rtx_insn *insn)
6365 {
6366 rtx_code cond = compare_condition (insn);
6367 return (cond == EQ || cond == NE);
6368 }
6369
6370
6371 /* Implement `TARGET_CANONICALIZE_COMPARISON'. */
6372 /* Basically tries to convert "difficult" comparisons like GT[U]
6373 and LE[U] to simple ones. Some asymmetric comparisons can be
6374 transformed to EQ or NE against zero. */
6375
6376 static void
6377 avr_canonicalize_comparison (int *icode, rtx *op0, rtx *op1, bool op0_fixed)
6378 {
6379 rtx_code code = (rtx_code) *icode;
6380 machine_mode mode = GET_MODE (*op0);
6381
6382 bool signed_p = code == GT || code == LE;
6383 bool unsigned_p = code == GTU || code == LEU;
6384 bool difficult_p = signed_p || unsigned_p;
6385
6386 if (// Only do integers and fixed-points.
6387 (! SCALAR_INT_MODE_P (mode)
6388 && ! ALL_SCALAR_FIXED_POINT_MODE_P (mode))
6389 // Only do comparisons against a register.
6390 || ! register_operand (*op0, mode))
6391 return;
6392
6393 // Canonicalize "difficult" reg-reg comparisons.
6394
6395 if (! op0_fixed
6396 && difficult_p
6397 && register_operand (*op1, mode))
6398 {
6399 std::swap (*op0, *op1);
6400 *icode = (int) swap_condition (code);
6401 return;
6402 }
6403
6404 // Canonicalize comparisons against compile-time constants.
6405
6406 if (CONST_INT_P (*op1)
6407 || CONST_FIXED_P (*op1))
6408 {
6409 // INT_MODE of the same size.
6410 scalar_int_mode imode = int_mode_for_mode (mode).require ();
6411
6412 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (imode);
6413 unsigned HOST_WIDE_INT maxval = signed_p ? mask >> 1 : mask;
6414
6415 // Convert value *op1 to imode.
6416 rtx xval = simplify_gen_subreg (imode, *op1, mode, 0);
6417
6418 // Canonicalize difficult comparisons against const.
6419 if (difficult_p
6420 && (UINTVAL (xval) & mask) != maxval)
6421 {
6422 // Convert *op0 > *op1 to *op0 >= 1 + *op1.
6423 // Convert *op0 <= *op1 to *op0 < 1 + *op1.
6424 xval = simplify_binary_operation (PLUS, imode, xval, const1_rtx);
6425
6426 // Convert value back to its original mode.
6427 *op1 = simplify_gen_subreg (mode, xval, imode, 0);
6428
6429 // Map > to >= and <= to <.
6430 *icode = (int) avr_normalize_condition (code);
6431
6432 return;
6433 }
6434
6435 // Some asymmetric comparisons can be turned into EQ or NE.
6436 if (code == LTU && xval == const1_rtx)
6437 {
6438 *icode = (int) EQ;
6439 *op1 = CONST0_RTX (mode);
6440 return;
6441 }
6442
6443 if (code == GEU && xval == const1_rtx)
6444 {
6445 *icode = (int) NE;
6446 *op1 = CONST0_RTX (mode);
6447 return;
6448 }
6449 }
6450 }
6451
6452
6453 /* Try to turn a GEU or LTU comparison of register XOP[1] into an
6454 NE / EQ comparison of the higher bytes of XOP[1] against 0.
6455 XOP[1] has scalar int or scalar fixed-point mode of 2, 3 or 4 bytes.
6456 XOP[2] is a compile-time constant, and XOP[0] = XOP[1] <comp> XOP[2]
6457 is the comparison operator. XOP[3] is the branch label, and XOP[4]
6458 is a QImode scratch operand.
6459 When XOP[1] (viewed as a CONST_INT) is an integral power of 256,
6460 then a GTU or LTU comparison can be turned into a NE or EQ comparison
6461 of the high bytes against zero. For example, the C code
6462
6463 if (x >= 1)
6464 ccc = 0;
6465
6466 where x is an unsigned _Accum may be compiled as:
6467
6468 or r24,r25 ; *cmpsi_lsr
6469 breq .L1 ; branch
6470 sts ccc,__zero_reg__ ; movqi_insn
6471 .L1:
6472
6473 In the case of success, the operands will be such that they comprise
6474 a *cmp<mode>_lsr insn, where mode is HI, PSI or SI, and XOP[0] will be
6475 a NE or EQ branch condition. Otherwise, XOP[] is unchanged. */
6476
6477 void
6478 avr_maybe_cmp_lsr (rtx *xop)
6479 {
6480 rtx_code comp = GET_CODE (xop[0]);
6481
6482 if ((comp == GEU || comp == LTU)
6483 && (CONST_INT_P (xop[2]) || CONST_FIXED_P (xop[2])))
6484 {
6485 rtx xreg = avr_to_int_mode (xop[1]);
6486 rtx xval = avr_to_int_mode (xop[2]);
6487 machine_mode imode = GET_MODE (xreg);
6488 auto uval = UINTVAL (xval) & GET_MODE_MASK (imode);
6489 int shift = exact_log2 (uval);
6490
6491 if (shift == 8 || shift == 16 || shift == 24)
6492 {
6493 // Operands such that the compare becomes *cmp<mode>_lsr.
6494 xop[1] = gen_rtx_LSHIFTRT (imode, xreg, GEN_INT (shift));
6495 xop[2] = const0_rtx;
6496 xop[4] = gen_rtx_SCRATCH (QImode);
6497 // Branch condition.
6498 xop[0] = gen_rtx_fmt_ee (comp == GEU ? NE : EQ,
6499 VOIDmode, xop[1], xop[2]);
6500 }
6501 }
6502 }
6503
6504
6505 /* Output an EQ / NE compare of HI, PSI or SI register XOP[0] against 0,
6506 where only the bits starting at XOP[1] are relevant. XOP[1] is a
6507 const_int that is 8, 16 or 24. Return "".
6508 PLEN == 0: Output instructions.
6509 PLEN != 0: Set *PLEN to the length of the sequence in words. */
6510
6511 const char *
6512 avr_out_cmp_lsr (rtx_insn *insn, rtx *xop, int *plen)
6513 {
6514 rtx xreg = xop[0];
6515 const int n_bytes = GET_MODE_SIZE (GET_MODE (xreg));
6516 const int shift = INTVAL (xop[1]);
6517 const rtx_code cond = compare_condition (insn);
6518
6519 gcc_assert (shift == 8 || shift == 16 || shift == 24);
6520 gcc_assert (shift < 8 * n_bytes);
6521 gcc_assert (cond == UNKNOWN || cond == NE || cond == EQ);
6522
6523 const bool used_p = ! reg_unused_after (insn, xreg);
6524
6525 if (plen)
6526 *plen = 0;
6527
6528 if (shift / 8 == n_bytes - 1)
6529 {
6530 rtx xmsb = avr_byte (xreg, n_bytes - 1);
6531 avr_asm_len ("tst %0", &xmsb, plen, 1);
6532 }
6533 else if (n_bytes == 4
6534 && shift <= 16
6535 && AVR_HAVE_ADIW
6536 && REGNO (xreg) >= REG_22
6537 // The sequence also works when xreg is unused after,
6538 // but SBIW is slower than OR.
6539 && used_p)
6540 {
6541 avr_asm_len ("sbiw %C0,0", &xreg, plen, 1);
6542 if (shift == 8)
6543 avr_asm_len ("cpc %B0,__zero_reg__", &xreg, plen, 1);
6544 }
6545 else
6546 {
6547 rtx op[2] = { avr_byte (xreg, shift / 8), tmp_reg_rtx };
6548 if (used_p)
6549 {
6550 avr_asm_len ("mov %1,%0", op, plen, 1);
6551 op[0] = tmp_reg_rtx;
6552 }
6553
6554 for (int i = 1 + shift / 8; i < n_bytes; ++i)
6555 {
6556 op[1] = avr_byte (xreg, i);
6557 avr_asm_len ("or %0,%1", op, plen, 1);
6558 }
6559 }
6560
6561 return "";
6562 }
6563
6564
6565 /* Output compare instruction
6566
6567 compare (XOP[0], XOP[1])
6568
6569 for a register XOP[0] and a compile-time constant XOP[1]. Return "".
6570 XOP[2] is an 8-bit scratch register as needed.
6571
6572 PLEN == NULL: Output instructions.
6573 PLEN != NULL: Set *PLEN to the length (in words) of the sequence.
6574 Don't output anything. */
6575
6576 const char *
6577 avr_out_compare (rtx_insn *insn, rtx *xop, int *plen)
6578 {
6579 /* Register to compare and value to compare against. */
6580 rtx xreg = xop[0];
6581 rtx xval = xop[1];
6582
6583 /* Number of bytes to operate on. */
6584 int n_bytes = GET_MODE_SIZE (GET_MODE (xreg));
6585
6586 /* Value (0..0xff) held in clobber register xop[2] or -1 if unknown. */
6587 int clobber_val = -1;
6588
6589 /* Map fixed mode operands to integer operands with the same binary
6590 representation. They are easier to handle in the remainder. */
6591
6592 if (CONST_FIXED_P (xval))
6593 {
6594 xreg = avr_to_int_mode (xop[0]);
6595 xval = avr_to_int_mode (xop[1]);
6596 }
6597
6598 gcc_assert (REG_P (xreg));
6599 gcc_assert ((CONST_INT_P (xval) && n_bytes <= 4)
6600 || (const_double_operand (xval, VOIDmode) && n_bytes == 8));
6601
6602 if (plen)
6603 *plen = 0;
6604
6605 const rtx_code cond = compare_condition (insn);
6606 const bool eqne_p = cond == EQ || cond == NE;
6607
6608 /* Comparisons == +/-1 and != +/-1 can be done similar to camparing
6609 against 0 by ORing the bytes. This is one instruction shorter.
6610 Notice that 64-bit comparisons are always against reg:ALL8 18 (ACC_A)
6611 and therefore don't use this. */
6612
6613 if (eqne_p
6614 && ! test_hard_reg_class (LD_REGS, xreg)
6615 && reg_unused_after (insn, xreg))
6616 {
6617 if (xval == const1_rtx)
6618 {
6619 avr_asm_len ("dec %A0" CR_TAB
6620 "or %A0,%B0", xop, plen, 2);
6621
6622 if (n_bytes >= 3)
6623 avr_asm_len ("or %A0,%C0", xop, plen, 1);
6624
6625 if (n_bytes >= 4)
6626 avr_asm_len ("or %A0,%D0", xop, plen, 1);
6627
6628 return "";
6629 }
6630 else if (xval == constm1_rtx)
6631 {
6632 if (n_bytes >= 4)
6633 avr_asm_len ("and %A0,%D0", xop, plen, 1);
6634
6635 if (n_bytes >= 3)
6636 avr_asm_len ("and %A0,%C0", xop, plen, 1);
6637
6638 return avr_asm_len ("and %A0,%B0" CR_TAB
6639 "com %A0", xop, plen, 2);
6640 }
6641 }
6642
6643 /* Comparisons == and != may change the order in which the sub-bytes are
6644 being compared. Start with the high 16 bits so we can use SBIW. */
6645
6646 if (n_bytes == 4
6647 && eqne_p
6648 && AVR_HAVE_ADIW
6649 && REGNO (xreg) >= REG_22
6650 && (xval == const0_rtx
6651 || (IN_RANGE (avr_int16 (xval, 2), 0, 63)
6652 && reg_unused_after (insn, xreg))))
6653 {
6654 xop[2] = avr_word (xval, 2);
6655 return avr_asm_len ("sbiw %C0,%2" CR_TAB
6656 "sbci %B0,hi8(%1)" CR_TAB
6657 "sbci %A0,lo8(%1)", xop, plen, 3);
6658 }
6659
6660 bool changed[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
6661
6662 /* The >= and < comparisons may skip the lower bytes when the according bytes
6663 of the constant are all zeros. In that case, the comparison may start
6664 at a byte other than the LSB. */
6665
6666 const int start = ((cond == GEU || cond == LTU || cond == GE || cond == LT)
6667 && INTVAL (xval) != 0)
6668 ? ctz_hwi (INTVAL (xval)) / 8
6669 : 0;
6670
6671 for (int i = start; i < n_bytes; i++)
6672 {
6673 /* We compare byte-wise. */
6674 xop[0] = avr_byte (xreg, i);
6675 xop[1] = avr_byte (xval, i);
6676
6677 /* 8-bit value to compare with this byte. */
6678 unsigned int val8 = avr_uint8 (xval, i);
6679
6680 /* Word registers >= R24 can use SBIW/ADIW with 0..63. */
6681
6682 if (i == start
6683 && i % 2 == 0
6684 && n_bytes - start >= 2
6685 && avr_adiw_reg_p (xop[0]))
6686 {
6687 int val16 = avr_int16 (xval, i);
6688
6689 if (IN_RANGE (val16, 0, 63)
6690 && (val8 == 0
6691 || reg_unused_after (insn, xreg)))
6692 {
6693 avr_asm_len ("sbiw %0,%1", xop, plen, 1);
6694 changed[i] = changed[i + 1] = val8 != 0;
6695 i++;
6696 continue;
6697 }
6698
6699 if (IN_RANGE (val16, -63, -1)
6700 && eqne_p
6701 && n_bytes - start == 2
6702 && reg_unused_after (insn, xreg))
6703 {
6704 return avr_asm_len ("adiw %0,%n1", xop, plen, 1);
6705 }
6706 }
6707
6708 /* Otherwise, compare a single byte with CP / CPC or equivalent. */
6709
6710 const bool ldreg_p = test_hard_reg_class (LD_REGS, xop[0]);
6711
6712 if (ldreg_p && i == start)
6713 {
6714 avr_asm_len ("cpi %0,%1", xop, plen, 1);
6715 continue;
6716 }
6717
6718 /* When byte comparisons for an EQ or NE comparison look like
6719 compare (x[i], C)
6720 compare (x[j], C)
6721 then we can instead use
6722 compare (x[i], C)
6723 compare (x[j], x[i])
6724 which is shorter, and the outcome of the comparison is the same. */
6725
6726 if (eqne_p)
6727 {
6728 bool found = false;
6729
6730 for (int j = start; j < i && ! found; ++j)
6731 if (val8 == avr_uint8 (xval, j)
6732 // Make sure that we didn't clobber x[j] with SBCI.
6733 && ! changed[j])
6734 {
6735 rtx op[] = { xop[0], avr_byte (xreg, j) };
6736 avr_asm_len ("cpc %0,%1", op, plen, 1);
6737 found = true;
6738 }
6739
6740 if (found)
6741 continue;
6742 }
6743
6744 /* Upper registers can SBCI which has the same effect like CPC on
6745 SREG, but it writes back the result of the subtraction to %0.
6746 Therefore, SBCI can only replace CPC when %0 is unused after,
6747 or when the comparison is against 0x..0000. */
6748
6749 if (ldreg_p && i > start)
6750 {
6751 bool zero_p = true;
6752
6753 for (int j = start; j <= i; ++j)
6754 zero_p &= avr_uint8 (xval, j) == 0;
6755
6756 if (zero_p || reg_unused_after (insn, xreg))
6757 {
6758 avr_asm_len ("sbci %0,%1", xop, plen, 1);
6759 changed[i] = !zero_p;
6760 continue;
6761 }
6762 }
6763
6764 /* Comparing against 0 is easy, but only invoke __zero_reg__ when
6765 nothing else has been found. This is better for small ISRs. */
6766
6767 if (val8 == 0)
6768 {
6769 avr_asm_len (i == start
6770 ? "cp %0,__zero_reg__"
6771 : "cpc %0,__zero_reg__", xop, plen, 1);
6772 continue;
6773 }
6774
6775 /* Must load the value into the scratch register. */
6776
6777 gcc_assert (REG_P (xop[2]));
6778
6779 if (clobber_val != (int) val8)
6780 avr_asm_len ("ldi %2,%1", xop, plen, 1);
6781 clobber_val = (int) val8;
6782
6783 avr_asm_len (i == start
6784 ? "cp %0,%2"
6785 : "cpc %0,%2", xop, plen, 1);
6786 }
6787
6788 return "";
6789 }
6790
6791
6792 /* Prepare operands of compare_const_di2 to be used with avr_out_compare. */
6793
6794 const char *
6795 avr_out_compare64 (rtx_insn *insn, rtx *op, int *plen)
6796 {
6797 rtx xop[3] = { gen_rtx_REG (DImode, ACC_A), op[0], op[1] };
6798
6799 return avr_out_compare (insn, xop, plen);
6800 }
6801
6802
6803 /* Output test instruction for HImode. */
6804
6805 const char *
6806 avr_out_tsthi (rtx_insn *insn, rtx *op, int *plen)
6807 {
6808 if (compare_sign_p (insn))
6809 {
6810 avr_asm_len ("tst %B0", op, plen, -1);
6811 }
6812 else if (reg_unused_after (insn, op[0])
6813 && compare_eq_p (insn))
6814 {
6815 /* Faster than sbiw if we can clobber the operand. */
6816 avr_asm_len ("or %A0,%B0", op, plen, -1);
6817 }
6818 else
6819 {
6820 avr_out_compare (insn, op, plen);
6821 }
6822
6823 return "";
6824 }
6825
6826
6827 /* Output test instruction for PSImode. */
6828
6829 const char *
6830 avr_out_tstpsi (rtx_insn *insn, rtx *op, int *plen)
6831 {
6832 if (compare_sign_p (insn))
6833 {
6834 avr_asm_len ("tst %C0", op, plen, -1);
6835 }
6836 else if (reg_unused_after (insn, op[0])
6837 && compare_eq_p (insn))
6838 {
6839 /* Faster than sbiw if we can clobber the operand. */
6840 avr_asm_len ("or %A0,%B0" CR_TAB
6841 "or %A0,%C0", op, plen, -2);
6842 }
6843 else
6844 {
6845 avr_out_compare (insn, op, plen);
6846 }
6847
6848 return "";
6849 }
6850
6851
6852 /* Output test instruction for SImode. */
6853
6854 const char *
6855 avr_out_tstsi (rtx_insn *insn, rtx *op, int *plen)
6856 {
6857 if (compare_sign_p (insn))
6858 {
6859 avr_asm_len ("tst %D0", op, plen, -1);
6860 }
6861 else if (reg_unused_after (insn, op[0])
6862 && compare_eq_p (insn))
6863 {
6864 /* Faster than sbiw if we can clobber the operand. */
6865 avr_asm_len ("or %A0,%B0" CR_TAB
6866 "or %A0,%C0" CR_TAB
6867 "or %A0,%D0", op, plen, -3);
6868 }
6869 else
6870 {
6871 avr_out_compare (insn, op, plen);
6872 }
6873
6874 return "";
6875 }
6876
6877
6878 /* Output a comparison of a zero- or sign-extended register against a
6879 plain register. CODE is SIGN_EXTEND or ZERO_EXTEND. Return "".
6880
6881 PLEN != 0: Set *PLEN to the code length in words. Don't output anything.
6882 PLEN == 0: Print instructions. */
6883
6884 const char *
6885 avr_out_cmp_ext (rtx xop[], rtx_code code, int *plen)
6886 {
6887 // The smaller reg is the one that's to be extended. Get its index as z.
6888 int z = GET_MODE_SIZE (GET_MODE (xop[1])) < GET_MODE_SIZE (GET_MODE (xop[0]));
6889 rtx zreg = xop[z];
6890 rtx reg = xop[1 - z];
6891 machine_mode mode = GET_MODE (reg);
6892 machine_mode zmode = GET_MODE (zreg);
6893 rtx zex;
6894
6895 if (plen)
6896 *plen = 0;
6897
6898 // zex holds the extended bytes above zreg. This is 0 for ZERO_EXTEND,
6899 // and 0 or -1 for SIGN_EXTEND.
6900
6901 if (code == SIGN_EXTEND)
6902 {
6903 // Sign-extend the high-byte of zreg to tmp_reg.
6904 int zmsb = GET_MODE_SIZE (zmode) - 1;
6905 rtx xzmsb = avr_byte (zreg, zmsb);
6906
6907 avr_asm_len ("mov __tmp_reg__,%0" CR_TAB
6908 "rol __tmp_reg__" CR_TAB
6909 "sbc __tmp_reg__,__tmp_reg__", &xzmsb, plen, 3);
6910 zex = tmp_reg_rtx;
6911 }
6912 else if (code == ZERO_EXTEND)
6913 {
6914 zex = zero_reg_rtx;
6915 }
6916 else
6917 gcc_unreachable ();
6918
6919 // Now output n_bytes bytes of the very comparison.
6920
6921 int n_bytes = GET_MODE_SIZE (mode);
6922
6923 avr_asm_len ("cp %0,%1", xop, plen, 1);
6924
6925 for (int b = 1; b < n_bytes; ++b)
6926 {
6927 rtx regs[2];
6928 regs[1 - z] = avr_byte (reg, b);
6929 regs[z] = b < GET_MODE_SIZE (zmode) ? avr_byte (zreg, b) : zex;
6930
6931 avr_asm_len ("cpc %0,%1", regs, plen, 1);
6932 }
6933
6934 return "";
6935 }
6936
6937
6938 /* Generate asm equivalent for various shifts. This only handles cases
6939 that are not already carefully hand-optimized in ?sh<mode>3_out.
6940
6941 OPERANDS[0] resp. %0 in TEMPL is the operand to be shifted.
6942 OPERANDS[2] is the shift count as CONST_INT, MEM or REG.
6943 OPERANDS[3] is a QImode scratch register from LD regs if
6944 available and SCRATCH, otherwise (no scratch available)
6945
6946 TEMPL is an assembler template that shifts by one position.
6947 T_LEN is the length of this template.
6948 PLEN != 0: Set *PLEN to the length of the sequence in words.
6949 PLEN == 0: Output instructions. */
6950
6951 void
6952 out_shift_with_cnt (const char *templ, rtx_insn *insn, rtx operands[],
6953 int *plen, int t_len)
6954 {
6955 bool second_label = true;
6956 bool saved_in_tmp = false;
6957 bool use_zero_reg = false;
6958 rtx op[5];
6959
6960 op[0] = operands[0];
6961 op[1] = operands[1];
6962 op[2] = operands[2];
6963 op[3] = operands[3];
6964
6965 if (plen)
6966 *plen = 0;
6967
6968 if (CONST_INT_P (operands[2]))
6969 {
6970 /* Operand 3 is a scratch register if this is a
6971 parallel with three elements i.e. a set,
6972 a clobber of a scratch, and clobber of REG_CC.
6973 If a scratch reg is not available, then the parallel
6974 will contain only a set and clobber of REG_CC. */
6975 bool scratch = (GET_CODE (PATTERN (insn)) == PARALLEL
6976 && XVECLEN (PATTERN (insn), 0) == 3
6977 && REG_P (operands[3]));
6978 int count = INTVAL (operands[2]);
6979 int max_len = 10; /* If larger than this, always use a loop. */
6980
6981 if (count <= 0)
6982 return;
6983
6984 if (count < 8 && !scratch)
6985 use_zero_reg = true;
6986
6987 if (optimize_size)
6988 max_len = t_len + (scratch ? 3 : (use_zero_reg ? 4 : 5));
6989
6990 if (t_len * count <= max_len)
6991 {
6992 /* Output shifts inline with no loop - faster. */
6993
6994 while (count-- > 0)
6995 avr_asm_len (templ, op, plen, t_len);
6996
6997 return;
6998 }
6999
7000 if (scratch)
7001 {
7002 avr_asm_len ("ldi %3,%2", op, plen, 1);
7003 }
7004 else if (use_zero_reg)
7005 {
7006 /* Hack to save one word: use __zero_reg__ as loop counter.
7007 Set one bit, then shift in a loop until it is 0 again. */
7008
7009 op[3] = zero_reg_rtx;
7010
7011 avr_asm_len ("set" CR_TAB
7012 "bld %3,%2-1", op, plen, 2);
7013 }
7014 else
7015 {
7016 /* No scratch register available, use one from LD_REGS (saved in
7017 __tmp_reg__) that doesn't overlap with registers to shift. */
7018
7019 op[3] = all_regs_rtx[((REGNO (op[0]) - 1) & 15) + REG_16];
7020 op[4] = tmp_reg_rtx;
7021 saved_in_tmp = true;
7022
7023 avr_asm_len ("mov %4,%3" CR_TAB
7024 "ldi %3,%2", op, plen, 2);
7025 }
7026
7027 second_label = false;
7028 }
7029 else if (MEM_P (op[2]))
7030 {
7031 rtx op_mov[2];
7032
7033 op_mov[0] = op[3] = tmp_reg_rtx;
7034 op_mov[1] = op[2];
7035
7036 out_movqi_r_mr (insn, op_mov, plen);
7037 }
7038 else if (register_operand (op[2], QImode))
7039 {
7040 op[3] = op[2];
7041
7042 if (!reg_unused_after (insn, op[2])
7043 || reg_overlap_mentioned_p (op[0], op[2]))
7044 {
7045 op[3] = tmp_reg_rtx;
7046 avr_asm_len ("mov %3,%2", op, plen, 1);
7047 }
7048 }
7049 else
7050 fatal_insn ("bad shift insn:", insn);
7051
7052 if (second_label)
7053 avr_asm_len ("rjmp 2f", op, plen, 1);
7054
7055 avr_asm_len ("1:", op, plen, 0);
7056 avr_asm_len (templ, op, plen, t_len);
7057
7058 if (second_label)
7059 avr_asm_len ("2:", op, plen, 0);
7060
7061 avr_asm_len (use_zero_reg ? "lsr %3" : "dec %3", op, plen, 1);
7062 avr_asm_len (second_label ? "brpl 1b" : "brne 1b", op, plen, 1);
7063
7064 if (saved_in_tmp)
7065 avr_asm_len ("mov %3,%4", op, plen, 1);
7066 }
7067
7068
7069 /* 8bit shift left ((char)x << i) */
7070
7071 const char *
7072 ashlqi3_out (rtx_insn *insn, rtx operands[], int *plen)
7073 {
7074 if (CONST_INT_P (operands[2]))
7075 {
7076 int reg0 = REGNO (operands[0]);
7077 int reg1 = REGNO (operands[1]);
7078 bool ldreg_p = test_hard_reg_class (LD_REGS, operands[0]);
7079 int offs = INTVAL (operands[2]);
7080
7081 if (plen)
7082 *plen = 0;
7083
7084 if (offs <= 3
7085 || (offs <= 5 && ! ldreg_p))
7086 {
7087 for (int i = 0; i < offs; ++i)
7088 avr_asm_len ("lsl %0", operands, plen, 1);
7089 return "";
7090 }
7091
7092 switch (offs)
7093 {
7094 default:
7095 if (offs < 8)
7096 break;
7097 return avr_asm_len ("clr %0", operands, plen, 1);
7098
7099 case 1:
7100 case 2:
7101 case 3:
7102 gcc_unreachable ();
7103
7104 case 4:
7105 return avr_asm_len ("swap %0" CR_TAB
7106 "andi %0,0xf0", operands, plen, 2);
7107 case 5:
7108 return avr_asm_len ("swap %0" CR_TAB
7109 "lsl %0" CR_TAB
7110 "andi %0,0xe0", operands, plen, 3);
7111 case 6:
7112 if (ldreg_p && reg0 == reg1)
7113 return avr_asm_len ("swap %0" CR_TAB
7114 "lsl %0" CR_TAB
7115 "lsl %0" CR_TAB
7116 "andi %0,0xc0", operands, plen, 4);
7117 if (ldreg_p && reg0 != reg1 && AVR_HAVE_MUL)
7118 return avr_asm_len ("ldi %0,1<<6" CR_TAB
7119 "mul %0,%1" CR_TAB
7120 "mov %0,r0" CR_TAB
7121 "clr __zero_reg__", operands, plen, 4);
7122 return reg0 != reg1
7123 ? avr_asm_len ("clr %0" CR_TAB
7124 "bst %1,0" CR_TAB
7125 "bld %0,6" CR_TAB
7126 "bst %1,1" CR_TAB
7127 "bld %0,7", operands, plen, 5)
7128 : avr_asm_len ("lsl %0" CR_TAB
7129 "lsl %0" CR_TAB
7130 "lsl %0" CR_TAB
7131 "lsl %0" CR_TAB
7132 "lsl %0" CR_TAB
7133 "lsl %0", operands, plen, 6);
7134 case 7:
7135 return avr_asm_len ("bst %1,0" CR_TAB
7136 "clr %0" CR_TAB
7137 "bld %0,7", operands, plen, 3);
7138 }
7139 }
7140 else if (CONSTANT_P (operands[2]))
7141 fatal_insn ("internal compiler error. Incorrect shift:", insn);
7142
7143 out_shift_with_cnt ("lsl %0",
7144 insn, operands, plen, 1);
7145 return "";
7146 }
7147
7148
7149 /* Output a 16-bit left shift XOP[0] = XOP[1] << XOP[2] using MUL.
7150 XOP[3] is an upper 8-bit scratch register. This function is currently
7151 only used for offsets 5 and 6 but works for offsets 1...7 as well. */
7152
7153 static const char*
7154 avr_out_ashlhi3_mul (rtx *xop, bool scratch_p, int *plen)
7155 {
7156 gcc_assert (scratch_p && AVR_HAVE_MUL);
7157
7158 // Takes 7 words and 9 cycles.
7159 return avr_asm_len ("ldi %3,1<<%2" CR_TAB
7160 "mul %B1,%3" CR_TAB
7161 "mov %B0,r0" CR_TAB
7162 "mul %A1,%3" CR_TAB
7163 "mov %A0,r0" CR_TAB
7164 "or %B0,r1" CR_TAB
7165 "clr __zero_reg__", xop, plen, -7);
7166 }
7167
7168
7169 /* 16bit shift left ((short)x << i) */
7170
7171 const char *
7172 ashlhi3_out (rtx_insn *insn, rtx operands[], int *plen)
7173 {
7174 if (CONST_INT_P (operands[2]))
7175 {
7176 bool scratch = (GET_CODE (PATTERN (insn)) == PARALLEL
7177 && XVECLEN (PATTERN (insn), 0) == 3
7178 && REG_P (operands[3]));
7179 bool ldi_ok = test_hard_reg_class (LD_REGS, operands[0]);
7180 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
7181 int size;
7182 int reg0 = REGNO (operands[0]);
7183 int reg1 = REGNO (operands[1]);
7184 bool use_mul_p = reg1 != reg0 || (scratch && AVR_HAVE_MUL);
7185
7186 if (plen)
7187 *plen = 0;
7188
7189 switch (INTVAL (operands[2]))
7190 {
7191 default:
7192 if (INTVAL (operands[2]) < 16)
7193 break;
7194
7195 return avr_asm_len ("clr %B0" CR_TAB
7196 "clr %A0", operands, plen, 2);
7197 case 4:
7198 if (avr_optimize_size_max_p () && scratch)
7199 break; /* 5 */
7200 if (ldi_ok)
7201 return avr_asm_len ("swap %A0" CR_TAB
7202 "swap %B0" CR_TAB
7203 "andi %B0,0xf0" CR_TAB
7204 "eor %B0,%A0" CR_TAB
7205 "andi %A0,0xf0" CR_TAB
7206 "eor %B0,%A0", operands, plen, 6);
7207 if (scratch)
7208 return avr_asm_len ("swap %A0" CR_TAB
7209 "swap %B0" CR_TAB
7210 "ldi %3,0xf0" CR_TAB
7211 "and %B0,%3" CR_TAB
7212 "eor %B0,%A0" CR_TAB
7213 "and %A0,%3" CR_TAB
7214 "eor %B0,%A0", operands, plen, 7);
7215 break; /* optimize_size ? 6 : 8 */
7216
7217 case 5:
7218 size = (scratch ? 5 : 6) + (reg1 != reg0) * (2 - AVR_HAVE_MOVW);
7219 if (avr_optimize_size_max_p () && (size < 7 || !use_mul_p))
7220 {
7221 if (reg0 != reg1)
7222 {
7223 if (AVR_HAVE_MOVW)
7224 avr_asm_len ("movw %0,%1", operands, plen, 1);
7225 else
7226 avr_asm_len ("mov %A0,%A1" CR_TAB
7227 "mov %B0,%B1", operands, plen, 2);
7228 }
7229 break; // scratch ? 5 : 6
7230 }
7231
7232 if (use_mul_p)
7233 return avr_out_ashlhi3_mul (operands, scratch, plen); // 7
7234
7235 if (optimize_size)
7236 break; /* scratch ? 5 : 6 */
7237 if (ldi_ok)
7238 return avr_asm_len ("lsl %A0" CR_TAB
7239 "rol %B0" CR_TAB
7240 "swap %A0" CR_TAB
7241 "swap %B0" CR_TAB
7242 "andi %B0,0xf0" CR_TAB
7243 "eor %B0,%A0" CR_TAB
7244 "andi %A0,0xf0" CR_TAB
7245 "eor %B0,%A0", operands, plen, 8);
7246 if (scratch)
7247 return avr_asm_len ("lsl %A0" CR_TAB
7248 "rol %B0" CR_TAB
7249 "swap %A0" CR_TAB
7250 "swap %B0" CR_TAB
7251 "ldi %3,0xf0" CR_TAB
7252 "and %B0,%3" CR_TAB
7253 "eor %B0,%A0" CR_TAB
7254 "and %A0,%3" CR_TAB
7255 "eor %B0,%A0", operands, plen, 9);
7256 break; /* 10 */
7257
7258 case 6:
7259 size = (scratch ? 5 : 6) + (reg1 != reg0) * (2 - AVR_HAVE_MOVW);
7260 if (avr_optimize_size_max_p () && (size < 7 || !use_mul_p))
7261 {
7262 if (reg0 != reg1)
7263 {
7264 if (AVR_HAVE_MOVW)
7265 avr_asm_len ("movw %0,%1", operands, plen, 1);
7266 else
7267 avr_asm_len ("mov %A0,%A1" CR_TAB
7268 "mov %B0,%B1", operands, plen, 2);
7269 }
7270 break; // scratch ? 5 : 6
7271 }
7272
7273 if (use_mul_p)
7274 return avr_out_ashlhi3_mul (operands, scratch, plen); // 7
7275
7276 if (optimize_size)
7277 break; /* scratch ? 5 : 6 */
7278 return avr_asm_len ("clr __tmp_reg__" CR_TAB
7279 "lsr %B0" CR_TAB
7280 "ror %A0" CR_TAB
7281 "ror __tmp_reg__" CR_TAB
7282 "lsr %B0" CR_TAB
7283 "ror %A0" CR_TAB
7284 "ror __tmp_reg__" CR_TAB
7285 "mov %B0,%A0" CR_TAB
7286 "mov %A0,__tmp_reg__", operands, plen, 9);
7287 case 7:
7288 return reg1_unused_after
7289 ? avr_asm_len ("lsr %B1" CR_TAB
7290 "mov %B0,%A1" CR_TAB
7291 "clr %A0" CR_TAB
7292 "ror %B0" CR_TAB
7293 "ror %A0", operands, plen, 5)
7294 : avr_asm_len ("bst %B1,0" CR_TAB
7295 "mov %B0,%A1" CR_TAB
7296 "clr %A0" CR_TAB
7297 "ror %B0" CR_TAB
7298 "ror %A0" CR_TAB
7299 "bld %B0,7", operands, plen, 6);
7300 case 8:
7301 return avr_asm_len ("mov %B0,%A1" CR_TAB
7302 "clr %A0", operands, plen, 2);
7303 case 9:
7304 return avr_asm_len ("mov %B0,%A1" CR_TAB
7305 "clr %A0" CR_TAB
7306 "lsl %B0", operands, plen, 3);
7307 case 10:
7308 return avr_asm_len ("mov %B0,%A1" CR_TAB
7309 "clr %A0" CR_TAB
7310 "lsl %B0" CR_TAB
7311 "lsl %B0", operands, plen, 4);
7312 case 11:
7313 return avr_asm_len ("mov %B0,%A1" CR_TAB
7314 "clr %A0" CR_TAB
7315 "lsl %B0" CR_TAB
7316 "lsl %B0" CR_TAB
7317 "lsl %B0", operands, plen, 5);
7318 case 12:
7319 if (ldi_ok)
7320 return avr_asm_len ("mov %B0,%A1" CR_TAB
7321 "clr %A0" CR_TAB
7322 "swap %B0" CR_TAB
7323 "andi %B0,0xf0", operands, plen, 4);
7324 if (scratch)
7325 return avr_asm_len ("mov %B0,%A1" CR_TAB
7326 "clr %A0" CR_TAB
7327 "swap %B0" CR_TAB
7328 "ldi %3,0xf0" CR_TAB
7329 "and %B0,%3", operands, plen, 5);
7330
7331 return avr_asm_len ("mov %B0,%A1" CR_TAB
7332 "clr %A0" CR_TAB
7333 "lsl %B0" CR_TAB
7334 "lsl %B0" CR_TAB
7335 "lsl %B0" CR_TAB
7336 "lsl %B0", operands, plen, 6);
7337 case 13:
7338 if (ldi_ok)
7339 return avr_asm_len ("mov %B0,%A0" CR_TAB
7340 "clr %A0" CR_TAB
7341 "swap %B0" CR_TAB
7342 "lsl %B0" CR_TAB
7343 "andi %B0,0xe0", operands, plen, 5);
7344 if (AVR_HAVE_MUL && scratch)
7345 return avr_asm_len ("ldi %3,0x20" CR_TAB
7346 "mul %A0,%3" CR_TAB
7347 "mov %B0,r0" CR_TAB
7348 "clr %A0" CR_TAB
7349 "clr __zero_reg__", operands, plen, 5);
7350 if (optimize_size && scratch)
7351 break; /* 5 */
7352
7353 if (scratch)
7354 return avr_asm_len ("mov %B0,%A0" CR_TAB
7355 "clr %A0" CR_TAB
7356 "swap %B0" CR_TAB
7357 "lsl %B0" CR_TAB
7358 "ldi %3,0xe0" CR_TAB
7359 "and %B0,%3", operands, plen, 6);
7360 if (AVR_HAVE_MUL)
7361 return avr_asm_len ("set" CR_TAB
7362 "bld r1,5" CR_TAB
7363 "mul %A0,r1" CR_TAB
7364 "mov %B0,r0" CR_TAB
7365 "clr %A0" CR_TAB
7366 "clr __zero_reg__", operands, plen, 6);
7367 return avr_asm_len ("mov %B0,%A0" CR_TAB
7368 "clr %A0" CR_TAB
7369 "lsl %B0" CR_TAB
7370 "lsl %B0" CR_TAB
7371 "lsl %B0" CR_TAB
7372 "lsl %B0" CR_TAB
7373 "lsl %B0", operands, plen, 7);
7374 case 14:
7375 if (AVR_HAVE_MUL && ldi_ok)
7376 return avr_asm_len ("ldi %B0,0x40" CR_TAB
7377 "mul %A0,%B0" CR_TAB
7378 "mov %B0,r0" CR_TAB
7379 "clr %A0" CR_TAB
7380 "clr __zero_reg__", operands, plen, 5);
7381 if (AVR_HAVE_MUL && scratch)
7382 return avr_asm_len ("ldi %3,0x40" CR_TAB
7383 "mul %A0,%3" CR_TAB
7384 "mov %B0,r0" CR_TAB
7385 "clr %A0" CR_TAB
7386 "clr __zero_reg__", operands, plen, 5);
7387 if (optimize_size && ldi_ok)
7388 return avr_asm_len ("mov %B0,%A0" CR_TAB
7389 "ldi %A0,6" "\n1:\t"
7390 "lsl %B0" CR_TAB
7391 "dec %A0" CR_TAB
7392 "brne 1b", operands, plen, 5);
7393 if (optimize_size && scratch)
7394 break; /* 5 */
7395
7396 return avr_asm_len ("clr %B0" CR_TAB
7397 "lsr %A0" CR_TAB
7398 "ror %B0" CR_TAB
7399 "lsr %A0" CR_TAB
7400 "ror %B0" CR_TAB
7401 "clr %A0", operands, plen, 6);
7402 case 15:
7403 return avr_asm_len ("bst %A1,0" CR_TAB
7404 "clr %A0" CR_TAB
7405 "clr %B0" CR_TAB
7406 "bld %B0,7", operands, plen, 4);
7407 } // switch
7408 }
7409
7410 out_shift_with_cnt ("lsl %A0" CR_TAB
7411 "rol %B0", insn, operands, plen, 2);
7412 return "";
7413 }
7414
7415
7416 /* 24-bit shift left */
7417
7418 const char *
7419 avr_out_ashlpsi3 (rtx_insn *insn, rtx *op, int *plen)
7420 {
7421 if (plen)
7422 *plen = 0;
7423
7424 if (CONST_INT_P (op[2]))
7425 {
7426 int reg0 = REGNO (op[0]);
7427 int reg1 = REGNO (op[1]);
7428 bool reg1_unused_after = reg_unused_after (insn, op[1]);
7429
7430 switch (INTVAL (op[2]))
7431 {
7432 default:
7433 if (INTVAL (op[2]) < 24)
7434 break;
7435
7436 return avr_asm_len ("clr %A0" CR_TAB
7437 "clr %B0" CR_TAB
7438 "clr %C0", op, plen, 3);
7439 case 8:
7440 return reg0 >= reg1
7441 ? avr_asm_len ("mov %C0,%B1" CR_TAB
7442 "mov %B0,%A1" CR_TAB
7443 "clr %A0", op, plen, 3)
7444 : avr_asm_len ("clr %A0" CR_TAB
7445 "mov %B0,%A1" CR_TAB
7446 "mov %C0,%B1", op, plen, 3);
7447 case 15:
7448 avr_asm_len (reg1_unused_after
7449 ? "lsr %B1"
7450 : "bst %B1,0", op, plen, 1);
7451 if (reg0 + 2 != reg1)
7452 avr_asm_len ("mov %C0,%A1", op, plen, 1);
7453 avr_asm_len ("clr %A0" CR_TAB
7454 "clr %B0" CR_TAB
7455 "ror %C0" CR_TAB
7456 "ror %B0", op, plen, 5);
7457 return reg1_unused_after
7458 ? ""
7459 : avr_asm_len ("bld %C0,7", op, plen, 1);
7460
7461 case 16:
7462 if (reg0 + 2 != reg1)
7463 avr_asm_len ("mov %C0,%A1", op, plen, 1);
7464
7465 return avr_asm_len ("clr %B0" CR_TAB
7466 "clr %A0", op, plen, 2);
7467 case 23:
7468 return avr_asm_len ("bst %A1,0" CR_TAB
7469 "clr %A0" CR_TAB
7470 "clr %B0" CR_TAB
7471 "clr %C0" CR_TAB
7472 "bld %C0,7", op, plen, 5);
7473 }
7474 }
7475
7476 out_shift_with_cnt ("lsl %A0" CR_TAB
7477 "rol %B0" CR_TAB
7478 "rol %C0", insn, op, plen, 3);
7479 return "";
7480 }
7481
7482
7483 /* 32bit shift left ((long)x << i) */
7484
7485 const char *
7486 ashlsi3_out (rtx_insn *insn, rtx operands[], int *plen)
7487 {
7488 if (CONST_INT_P (operands[2]))
7489 {
7490 int off = INTVAL (operands[2]);
7491 int reg0 = true_regnum (operands[0]);
7492 int reg1 = true_regnum (operands[1]);
7493 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
7494 bool scratch_p = (GET_CODE (PATTERN (insn)) == PARALLEL
7495 && XVECLEN (PATTERN (insn), 0) == 3
7496 && REG_P (operands[3]));
7497 if (plen)
7498 *plen = 0;
7499
7500 switch (off)
7501 {
7502 default:
7503 if (off < 32)
7504 break;
7505
7506 return AVR_HAVE_MOVW
7507 ? avr_asm_len ("clr %D0" CR_TAB
7508 "clr %C0" CR_TAB
7509 "movw %A0,%C0", operands, plen, 3)
7510 : avr_asm_len ("clr %D0" CR_TAB
7511 "clr %C0" CR_TAB
7512 "clr %B0" CR_TAB
7513 "clr %A0", operands, plen, 4);
7514 case 8:
7515 return reg0 >= reg1
7516 ? avr_asm_len ("mov %D0,%C1" CR_TAB
7517 "mov %C0,%B1" CR_TAB
7518 "mov %B0,%A1" CR_TAB
7519 "clr %A0", operands, plen, 4)
7520 : avr_asm_len ("clr %A0" CR_TAB
7521 "mov %B0,%A1" CR_TAB
7522 "mov %C0,%B1" CR_TAB
7523 "mov %D0,%C1", operands, plen, 4);
7524 case 15:
7525 avr_asm_len (reg1_unused_after
7526 ? "lsr %C1"
7527 : "bst %C1,0", operands, plen, 1);
7528 if (reg0 + 2 != reg1)
7529 {
7530 if (AVR_HAVE_MOVW)
7531 avr_asm_len ("movw %C0,%A1", operands, plen, 1);
7532 else
7533 avr_asm_len ("mov %C0,%A1" CR_TAB
7534 "mov %D0,%B1", operands, plen, 2);
7535 }
7536 avr_asm_len ("clr %A0" CR_TAB
7537 "clr %B0" CR_TAB
7538 "ror %D0" CR_TAB
7539 "ror %C0" CR_TAB
7540 "ror %B0", operands, plen, 5);
7541 return reg1_unused_after
7542 ? ""
7543 : avr_asm_len ("bld %D0,7", operands, plen, 1);
7544
7545 case 16:
7546 if (reg0 + 2 == reg1)
7547 return avr_asm_len ("clr %B0" CR_TAB
7548 "clr %A0", operands, plen, 2);
7549 return AVR_HAVE_MOVW
7550 ? avr_asm_len ("movw %C0,%A1" CR_TAB
7551 "clr %B0" CR_TAB
7552 "clr %A0", operands, plen, 3)
7553 : avr_asm_len ("mov %C0,%A1" CR_TAB
7554 "mov %D0,%B1" CR_TAB
7555 "clr %B0" CR_TAB
7556 "clr %A0", operands, plen, 4);
7557 case 30:
7558 if (AVR_HAVE_MUL && scratch_p)
7559 return avr_asm_len ("ldi %3,1<<6" CR_TAB
7560 "mul %3,%A1" CR_TAB
7561 "mov %D0,r0" CR_TAB
7562 "clr __zero_reg__" CR_TAB
7563 "clr %C0" CR_TAB
7564 "clr %B0" CR_TAB
7565 "clr %A0", operands, plen, 7);
7566 // Fallthrough
7567
7568 case 28:
7569 case 29:
7570 {
7571 const bool ld_reg0_p = avr_ld_regno_p (reg0 + 3); // %D0
7572 const bool ld_reg1_p = avr_ld_regno_p (reg1 + 0); // %A1
7573 if (ld_reg0_p
7574 || (ld_reg1_p && reg1_unused_after)
7575 || scratch_p)
7576 {
7577 if (ld_reg0_p)
7578 avr_asm_len ("mov %D0,%A1" CR_TAB
7579 "swap %D0" CR_TAB
7580 "andi %D0,0xf0", operands, plen, 3);
7581 else if (ld_reg1_p && reg1_unused_after)
7582 avr_asm_len ("swap %A1" CR_TAB
7583 "andi %A1,0xf0" CR_TAB
7584 "mov %D0,%A1", operands, plen, 3);
7585 else
7586 avr_asm_len ("mov %D0,%A1" CR_TAB
7587 "swap %D0" CR_TAB
7588 "ldi %3,0xf0" CR_TAB
7589 "and %D0,%3", operands, plen, 4);
7590 for (int i = 28; i < off; ++i)
7591 avr_asm_len ("lsl %D0", operands, plen, 1);
7592 return avr_asm_len ("clr %C0" CR_TAB
7593 "clr %B0" CR_TAB
7594 "clr %A0", operands, plen, 3);
7595 }
7596 }
7597 // Fallthrough
7598
7599 case 24:
7600 case 25:
7601 case 26:
7602 case 27:
7603 avr_asm_len ("mov %D0,%A1", operands, plen, 1);
7604 for (int i = 24; i < off; ++i)
7605 avr_asm_len ("lsl %D0", operands, plen, 1);
7606 return avr_asm_len ("clr %C0" CR_TAB
7607 "clr %B0" CR_TAB
7608 "clr %A0", operands, plen, 3);
7609 case 31:
7610 return AVR_HAVE_MOVW
7611 ? avr_asm_len ("bst %A1,0" CR_TAB
7612 "clr %A0" CR_TAB
7613 "clr %B0" CR_TAB
7614 "movw %C0,%A0" CR_TAB
7615 "bld %D0,7", operands, plen, 5)
7616 : avr_asm_len ("bst %A1,0" CR_TAB
7617 "clr %A0" CR_TAB
7618 "clr %B0" CR_TAB
7619 "clr %D0" CR_TAB
7620 "clr %C0" CR_TAB
7621 "bld %D0,7", operands, plen, 6);
7622 }
7623 }
7624
7625 out_shift_with_cnt ("lsl %A0" CR_TAB
7626 "rol %B0" CR_TAB
7627 "rol %C0" CR_TAB
7628 "rol %D0", insn, operands, plen, 4);
7629 return "";
7630 }
7631
7632
7633 /* 8-bit arithmetic shift right (int8_t) x >> i. */
7634
7635 const char *
7636 ashrqi3_out (rtx_insn *insn, rtx operands[], int *plen)
7637 {
7638 if (CONST_INT_P (operands[2]))
7639 {
7640 if (plen)
7641 *plen = 0;
7642
7643 const int offs = INTVAL (operands[2]);
7644 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
7645
7646 if (IN_RANGE (offs, 0, 5))
7647 {
7648 for (int i = 0; i < offs; ++i)
7649 avr_asm_len ("asr %0", operands, plen, 1);
7650 return "";
7651 }
7652 else if (offs == 6)
7653 {
7654 return reg1_unused_after
7655 ? avr_asm_len ("bst %1,6" CR_TAB
7656 "lsl %1" CR_TAB
7657 "sbc %0,%0" CR_TAB
7658 "bld %0,0", operands, plen, 4)
7659 : avr_asm_len ("mov %0,%1" CR_TAB
7660 "bst %0,6" CR_TAB
7661 "lsl %0" CR_TAB
7662 "sbc %0,%0" CR_TAB
7663 "bld %0,0", operands, plen, 5);
7664 }
7665 else if (offs >= 7)
7666 {
7667 rtx xop[2] = { operands[0], operands[1] };
7668 if (! reg_unused_after (insn, xop[1]))
7669 {
7670 avr_asm_len ("mov %0,%1", xop, plen, 1);
7671 xop[1] = xop[0];
7672 }
7673 return avr_asm_len ("lsl %1" CR_TAB
7674 "sbc %0,%0", xop, plen, 2);
7675 }
7676 }
7677 else if (CONSTANT_P (operands[2]))
7678 fatal_insn ("internal compiler error. Incorrect shift:", insn);
7679
7680 out_shift_with_cnt ("asr %0",
7681 insn, operands, plen, 1);
7682 return "";
7683 }
7684
7685
7686 /* 16bit arithmetic shift right ((signed short)x >> i) */
7687
7688 const char *
7689 ashrhi3_out (rtx_insn *insn, rtx operands[], int *plen)
7690 {
7691 if (CONST_INT_P (operands[2]))
7692 {
7693 bool scratch = (GET_CODE (PATTERN (insn)) == PARALLEL
7694 && XVECLEN (PATTERN (insn), 0) == 3
7695 && REG_P (operands[3]));
7696 bool ldi_ok = test_hard_reg_class (LD_REGS, operands[0]);
7697 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
7698
7699 if (plen)
7700 *plen = 0;
7701
7702 switch (INTVAL (operands[2]))
7703 {
7704 case 4:
7705 case 5:
7706 /* XXX try to optimize this too? */
7707 break;
7708
7709 case 6:
7710 if (optimize_size)
7711 break; /* scratch ? 5 : 6 */
7712 return avr_asm_len ("mov __tmp_reg__,%A0" CR_TAB
7713 "mov %A0,%B0" CR_TAB
7714 "lsl __tmp_reg__" CR_TAB
7715 "rol %A0" CR_TAB
7716 "sbc %B0,%B0" CR_TAB
7717 "lsl __tmp_reg__" CR_TAB
7718 "rol %A0" CR_TAB
7719 "rol %B0", operands, plen, 8);
7720 case 7:
7721 return reg1_unused_after
7722 ? avr_asm_len ("lsl %A1" CR_TAB
7723 "mov %A0,%B1" CR_TAB
7724 "rol %A0" CR_TAB
7725 "sbc %B0,%B0", operands, plen, 4)
7726 : avr_asm_len ("mov %A0,%A1" CR_TAB
7727 "lsl %A0" CR_TAB
7728 "mov %A0,%B1" CR_TAB
7729 "rol %A0" CR_TAB
7730 "sbc %B0,%B0", operands, plen, 5);
7731 case 8:
7732 {
7733 int reg0 = true_regnum (operands[0]);
7734 int reg1 = true_regnum (operands[1]);
7735
7736 return reg0 == reg1
7737 ? avr_asm_len ("mov %A0,%B0" CR_TAB
7738 "lsl %B0" CR_TAB
7739 "sbc %B0,%B0", operands, plen, 3)
7740 : avr_asm_len ("mov %A0,%B1" CR_TAB
7741 "clr %B0" CR_TAB
7742 "sbrc %A0,7" CR_TAB
7743 "dec %B0", operands, plen, 4);
7744 }
7745
7746 case 9:
7747 return avr_asm_len ("mov %A0,%B0" CR_TAB
7748 "lsl %B0" CR_TAB
7749 "sbc %B0,%B0" CR_TAB
7750 "asr %A0", operands, plen, 4);
7751 case 10:
7752 return avr_asm_len ("mov %A0,%B0" CR_TAB
7753 "lsl %B0" CR_TAB
7754 "sbc %B0,%B0" CR_TAB
7755 "asr %A0" CR_TAB
7756 "asr %A0", operands, plen, 5);
7757 case 11:
7758 if (AVR_HAVE_MUL && ldi_ok)
7759 return avr_asm_len ("ldi %A0,0x20" CR_TAB
7760 "muls %B0,%A0" CR_TAB
7761 "mov %A0,r1" CR_TAB
7762 "sbc %B0,%B0" CR_TAB
7763 "clr __zero_reg__", operands, plen, 5);
7764 if (optimize_size && scratch)
7765 break; /* 5 */
7766 return avr_asm_len ("mov %A0,%B0" CR_TAB
7767 "lsl %B0" CR_TAB
7768 "sbc %B0,%B0" CR_TAB
7769 "asr %A0" CR_TAB
7770 "asr %A0" CR_TAB
7771 "asr %A0", operands, plen, 6);
7772 case 12:
7773 if (AVR_HAVE_MUL && ldi_ok)
7774 return avr_asm_len ("ldi %A0,0x10" CR_TAB
7775 "muls %B0,%A0" CR_TAB
7776 "mov %A0,r1" CR_TAB
7777 "sbc %B0,%B0" CR_TAB
7778 "clr __zero_reg__", operands, plen, 5);
7779 if (optimize_size && scratch)
7780 break; /* 5 */
7781 return avr_asm_len ("mov %A0,%B0" CR_TAB
7782 "lsl %B0" CR_TAB
7783 "sbc %B0,%B0" CR_TAB
7784 "asr %A0" CR_TAB
7785 "asr %A0" CR_TAB
7786 "asr %A0" CR_TAB
7787 "asr %A0", operands, plen, 7);
7788 case 13:
7789 if (AVR_HAVE_MUL && ldi_ok)
7790 return avr_asm_len ("ldi %A0,0x08" CR_TAB
7791 "muls %B0,%A0" CR_TAB
7792 "mov %A0,r1" CR_TAB
7793 "sbc %B0,%B0" CR_TAB
7794 "clr __zero_reg__", operands, plen, 5);
7795 if (optimize_size)
7796 break; /* scratch ? 5 : 7 */
7797 return avr_asm_len ("mov %A0,%B0" CR_TAB
7798 "lsl %B0" CR_TAB
7799 "sbc %B0,%B0" CR_TAB
7800 "asr %A0" CR_TAB
7801 "asr %A0" CR_TAB
7802 "asr %A0" CR_TAB
7803 "asr %A0" CR_TAB
7804 "asr %A0", operands, plen, 8);
7805 case 14:
7806 return reg1_unused_after
7807 ? avr_asm_len ("bst %B1,6" CR_TAB
7808 "lsl %B1" CR_TAB
7809 "sbc %B0,%B0" CR_TAB
7810 "sbc %A0,%A0" CR_TAB
7811 "bld %A0,0", operands, plen, 5)
7812 : avr_asm_len ("mov %B0,%B1" CR_TAB
7813 "bst %B0,6" CR_TAB
7814 "lsl %B0" CR_TAB
7815 "sbc %B0,%B0" CR_TAB
7816 "sbc %A0,%A0" CR_TAB
7817 "bld %A0,0", operands, plen, 6);
7818 default:
7819 if (INTVAL (operands[2]) < 16)
7820 break;
7821
7822 /* fall through */
7823
7824 case 15:
7825 {
7826 rtx xop[2] = { operands[0], operands[1] };
7827 if (! reg_unused_after (insn, xop[1]))
7828 {
7829 avr_asm_len ("mov %B0,%B1", xop, plen, 1);
7830 xop[1] = xop[0];
7831 }
7832 return avr_asm_len ("lsl %B1" CR_TAB
7833 "sbc %A0,%A0" CR_TAB
7834 "mov %B0,%A0", xop, plen, 3);
7835 }
7836 } // switch
7837 }
7838
7839 out_shift_with_cnt ("asr %B0" CR_TAB
7840 "ror %A0", insn, operands, plen, 2);
7841 return "";
7842 }
7843
7844
7845 /* 24-bit arithmetic shift right */
7846
7847 const char *
7848 avr_out_ashrpsi3 (rtx_insn *insn, rtx *op, int *plen)
7849 {
7850 int dest = REGNO (op[0]);
7851 int src = REGNO (op[1]);
7852
7853 if (CONST_INT_P (op[2]))
7854 {
7855 if (plen)
7856 *plen = 0;
7857
7858 bool reg1_unused_after = reg_unused_after (insn, op[1]);
7859
7860 switch (INTVAL (op[2]))
7861 {
7862 case 8:
7863 return dest <= src
7864 ? avr_asm_len ("mov %A0,%B1" CR_TAB
7865 "mov %B0,%C1" CR_TAB
7866 "clr %C0" CR_TAB
7867 "sbrc %B0,7" CR_TAB
7868 "dec %C0", op, plen, 5)
7869 : avr_asm_len ("clr %C0" CR_TAB
7870 "sbrc %C1,7" CR_TAB
7871 "dec %C0" CR_TAB
7872 "mov %B0,%C1" CR_TAB
7873 "mov %A0,%B1", op, plen, 5);
7874 case 15:
7875 avr_asm_len (reg1_unused_after
7876 ? "lsl %B1"
7877 : "bst %B1,7", op, plen, 1);
7878 if (dest != src + 2)
7879 avr_asm_len ("mov %A0,%C1", op, plen, 1);
7880 avr_asm_len ("rol %A0" CR_TAB
7881 "sbc %B0,%B0" CR_TAB
7882 "sbc %C0,%C0", op, plen, 3);
7883 return reg1_unused_after
7884 ? ""
7885 : avr_asm_len ("bld %A0,0", op, plen, 1);
7886
7887 case 16:
7888 if (dest != src + 2)
7889 avr_asm_len ("mov %A0,%C1", op, plen, 1);
7890 return reg1_unused_after && dest != src + 2
7891 ? avr_asm_len ("rol %C1" CR_TAB
7892 "sbc %B0,%B0" CR_TAB
7893 "sbc %C0,%C0", op, plen, 3)
7894 : avr_asm_len ("clr %B0" CR_TAB
7895 "sbrc %A0,7" CR_TAB
7896 "com %B0" CR_TAB
7897 "mov %C0,%B0", op, plen, 4);
7898 case 22:
7899 {
7900 rtx xop[2] = { op[0], op[1] };
7901 if (! reg1_unused_after)
7902 {
7903 avr_asm_len ("mov %C0,%C1", xop, plen, 1);
7904 xop[1] = xop[0];
7905 }
7906 return avr_asm_len ("bst %C1,6" CR_TAB
7907 "lsl %C1" CR_TAB
7908 "sbc %C0,%C0" CR_TAB
7909 "sbc %B0,%B0" CR_TAB
7910 "sbc %A0,%A0" CR_TAB
7911 "bld %A0,0", xop, plen, 6);
7912 }
7913
7914 default:
7915 if (INTVAL (op[2]) < 24)
7916 break;
7917
7918 /* fall through */
7919
7920 case 23:
7921 {
7922 rtx xop[2] = { op[0], op[1] };
7923 if (! reg1_unused_after)
7924 {
7925 avr_asm_len ("mov %C0,%C1", xop, plen, 1);
7926 xop[1] = xop[0];
7927 }
7928 return avr_asm_len ("lsl %C1" CR_TAB
7929 "sbc %A0,%A0" CR_TAB
7930 "mov %B0,%A0" CR_TAB
7931 "mov %C0,%A0", xop, plen, 4);
7932 }
7933 } /* switch */
7934 }
7935
7936 out_shift_with_cnt ("asr %C0" CR_TAB
7937 "ror %B0" CR_TAB
7938 "ror %A0", insn, op, plen, 3);
7939 return "";
7940 }
7941
7942
7943 /* 32-bit arithmetic shift right ((signed long)x >> i) */
7944
7945 const char *
7946 ashrsi3_out (rtx_insn *insn, rtx operands[], int *plen)
7947 {
7948 if (CONST_INT_P (operands[2]))
7949 {
7950 if (plen)
7951 *plen = 0;
7952
7953 int reg0 = true_regnum (operands[0]);
7954 int reg1 = true_regnum (operands[1]);
7955 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
7956
7957 switch (INTVAL (operands[2]))
7958 {
7959 case 8:
7960 return reg0 <= reg1
7961 ? avr_asm_len ("mov %A0,%B1" CR_TAB
7962 "mov %B0,%C1" CR_TAB
7963 "mov %C0,%D1" CR_TAB
7964 "clr %D0" CR_TAB
7965 "sbrc %C0,7" CR_TAB
7966 "dec %D0", operands, plen, 6)
7967 : avr_asm_len ("clr %D0" CR_TAB
7968 "sbrc %D1,7" CR_TAB
7969 "dec %D0" CR_TAB
7970 "mov %C0,%D1" CR_TAB
7971 "mov %B0,%C1" CR_TAB
7972 "mov %A0,%B1", operands, plen, 6);
7973 case 15:
7974 avr_asm_len (reg1_unused_after
7975 ? "lsl %B1"
7976 : "bst %B1,7", operands, plen, 1);
7977 if (reg0 != reg1 + 2)
7978 {
7979 if (AVR_HAVE_MOVW)
7980 avr_asm_len ("movw %A0,%C1", operands, plen, 1);
7981 else
7982 avr_asm_len ("mov %A0,%C1" CR_TAB
7983 "mov %B0,%D1", operands, plen, 2);
7984 }
7985 avr_asm_len ("rol %A0" CR_TAB
7986 "rol %B0" CR_TAB
7987 "sbc %C0,%C0" CR_TAB
7988 "sbc %D0,%D0", operands, plen, 4);
7989 return reg1_unused_after
7990 ? ""
7991 : avr_asm_len ("bld %A0,0", operands, plen, 1);
7992
7993 case 16:
7994 if (reg0 == reg1 + 2)
7995 return avr_asm_len ("clr %D0" CR_TAB
7996 "sbrc %B0,7" CR_TAB
7997 "com %D0" CR_TAB
7998 "mov %C0,%D0", operands, plen, 4);
7999 if (AVR_HAVE_MOVW)
8000 avr_asm_len ("movw %A0,%C1", operands, plen, 1);
8001 else
8002 avr_asm_len ("mov %B0,%D1" CR_TAB
8003 "mov %A0,%C1", operands, plen, 2);
8004 return reg1_unused_after
8005 ? avr_asm_len ("lsl %D1" CR_TAB
8006 "sbc %D0,%D0" CR_TAB
8007 "mov %C0,%D0", operands, plen, 3)
8008 : avr_asm_len ("clr %D0" CR_TAB
8009 "sbrc %B0,7" CR_TAB
8010 "com %D0" CR_TAB
8011 "mov %C0,%D0", operands, plen, 4);
8012 case 24:
8013 return reg1_unused_after
8014 ? avr_asm_len ("mov %A0,%D1" CR_TAB
8015 "lsl %D1" CR_TAB
8016 "sbc %D0,%D0" CR_TAB
8017 "mov %B0,%D0" CR_TAB
8018 "mov %C0,%D0", operands, plen, 5)
8019 : avr_asm_len ("mov %A0,%D1" CR_TAB
8020 "clr %D0" CR_TAB
8021 "sbrc %A0,7" CR_TAB
8022 "com %D0" CR_TAB
8023 "mov %B0,%D0" CR_TAB
8024 "mov %C0,%D0", operands, plen, 6);
8025 case 30:
8026 {
8027 rtx xop[2] = { operands[0], operands[1] };
8028 if (! reg1_unused_after)
8029 {
8030 avr_asm_len ("mov %D0,%D1", xop, plen, 1);
8031 xop[1] = xop[0];
8032 }
8033 avr_asm_len ("bst %D1,6" CR_TAB
8034 "lsl %D1" CR_TAB
8035 "sbc %A0,%A0" CR_TAB
8036 "sbc %B0,%B0", xop, plen, 4);
8037 return AVR_HAVE_MOVW
8038 ? avr_asm_len ("movw %C0,%A0" CR_TAB
8039 "bld %A0,0", xop, plen, 2)
8040 : avr_asm_len ("mov %C0,%A0" CR_TAB
8041 "mov %D0,%A0" CR_TAB
8042 "bld %A0,0", xop, plen, 3);
8043 }
8044
8045 default:
8046 if (INTVAL (operands[2]) < 32)
8047 break;
8048
8049 /* fall through */
8050
8051 case 31:
8052 {
8053 rtx xop[2] = { operands[0], operands[1] };
8054 if (! reg1_unused_after)
8055 {
8056 avr_asm_len ("mov %D0,%D1", xop, plen, 1);
8057 xop[1] = xop[0];
8058 }
8059 return AVR_HAVE_MOVW
8060 ? avr_asm_len ("lsl %D1" CR_TAB
8061 "sbc %A0,%A0" CR_TAB
8062 "mov %B0,%A0" CR_TAB
8063 "movw %C0,%A0", xop, plen, 4)
8064 : avr_asm_len ("lsl %D1" CR_TAB
8065 "sbc %A0,%A0" CR_TAB
8066 "mov %B0,%A0" CR_TAB
8067 "mov %C0,%A0" CR_TAB
8068 "mov %D0,%A0", xop, plen, 5);
8069 }
8070 } // switch
8071 }
8072
8073 out_shift_with_cnt ("asr %D0" CR_TAB
8074 "ror %C0" CR_TAB
8075 "ror %B0" CR_TAB
8076 "ror %A0", insn, operands, plen, 4);
8077 return "";
8078 }
8079
8080 /* 8-bit logic shift right ((unsigned char)x >> i) */
8081
8082 const char *
8083 lshrqi3_out (rtx_insn *insn, rtx operands[], int *plen)
8084 {
8085 if (CONST_INT_P (operands[2]))
8086 {
8087 int reg0 = REGNO (operands[0]);
8088 int reg1 = REGNO (operands[1]);
8089 bool ldreg_p = test_hard_reg_class (LD_REGS, operands[0]);
8090 int offs = INTVAL (operands[2]);
8091
8092 if (plen)
8093 *plen = 0;
8094
8095 if (offs <= 3
8096 || (offs <= 5 && ! ldreg_p))
8097 {
8098 for (int i = 0; i < offs; ++i)
8099 avr_asm_len ("lsr %0", operands, plen, 1);
8100 return "";
8101 }
8102
8103 switch (INTVAL (operands[2]))
8104 {
8105 default:
8106 if (INTVAL (operands[2]) < 8)
8107 break;
8108 return avr_asm_len ("clr %0", operands, plen, 1);
8109
8110 case 1:
8111 case 2:
8112 case 3:
8113 gcc_unreachable ();
8114
8115 case 4:
8116 return avr_asm_len ("swap %0" CR_TAB
8117 "andi %0,0x0f", operands, plen, 2);
8118 case 5:
8119 return avr_asm_len ("swap %0" CR_TAB
8120 "lsr %0" CR_TAB
8121 "andi %0,0x7", operands, plen, 3);
8122 case 6:
8123 if (ldreg_p && reg0 == reg1)
8124 return avr_asm_len ("swap %0" CR_TAB
8125 "lsr %0" CR_TAB
8126 "lsr %0" CR_TAB
8127 "andi %0,0x3", operands, plen, 4);
8128 if (ldreg_p && reg0 != reg1 && AVR_HAVE_MUL)
8129 return avr_asm_len ("ldi %0,1<<2" CR_TAB
8130 "mul %0,%1" CR_TAB
8131 "mov %0,r1" CR_TAB
8132 "clr __zero_reg__", operands, plen, 4);
8133 return reg0 != reg1
8134 ? avr_asm_len ("clr %0" CR_TAB
8135 "bst %1,6" CR_TAB
8136 "bld %0,0" CR_TAB
8137 "bst %1,7" CR_TAB
8138 "bld %0,1", operands, plen, 5)
8139 : avr_asm_len ("lsr %0" CR_TAB
8140 "lsr %0" CR_TAB
8141 "lsr %0" CR_TAB
8142 "lsr %0" CR_TAB
8143 "lsr %0" CR_TAB
8144 "lsr %0", operands, plen, 6);
8145 case 7:
8146 return avr_asm_len ("bst %1,7" CR_TAB
8147 "clr %0" CR_TAB
8148 "bld %0,0", operands, plen, 3);
8149 }
8150 }
8151 else if (CONSTANT_P (operands[2]))
8152 fatal_insn ("internal compiler error. Incorrect shift:", insn);
8153
8154 out_shift_with_cnt ("lsr %0",
8155 insn, operands, plen, 1);
8156 return "";
8157 }
8158
8159
8160 /* 16-bit logic shift right ((unsigned short)x >> i) */
8161
8162 const char *
8163 lshrhi3_out (rtx_insn *insn, rtx operands[], int *plen)
8164 {
8165 if (CONST_INT_P (operands[2]))
8166 {
8167 bool scratch = (GET_CODE (PATTERN (insn)) == PARALLEL
8168 && XVECLEN (PATTERN (insn), 0) == 3
8169 && REG_P (operands[3]));
8170 bool ldi_ok = test_hard_reg_class (LD_REGS, operands[0]);
8171 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
8172
8173 if (plen)
8174 *plen = 0;
8175
8176 switch (INTVAL (operands[2]))
8177 {
8178 default:
8179 if (INTVAL (operands[2]) < 16)
8180 break;
8181 return avr_asm_len ("clr %B0" CR_TAB
8182 "clr %A0", operands, plen, 2);
8183
8184 case 4:
8185 if (optimize_size && scratch)
8186 break; /* 5 */
8187 if (ldi_ok)
8188 return avr_asm_len ("swap %B0" CR_TAB
8189 "swap %A0" CR_TAB
8190 "andi %A0,0x0f" CR_TAB
8191 "eor %A0,%B0" CR_TAB
8192 "andi %B0,0x0f" CR_TAB
8193 "eor %A0,%B0", operands, plen, 6);
8194 if (scratch)
8195 return avr_asm_len ("swap %B0" CR_TAB
8196 "swap %A0" CR_TAB
8197 "ldi %3,0x0f" CR_TAB
8198 "and %A0,%3" CR_TAB
8199 "eor %A0,%B0" CR_TAB
8200 "and %B0,%3" CR_TAB
8201 "eor %A0,%B0", operands, plen, 7);
8202 break; /* optimize_size ? 6 : 8 */
8203
8204 case 5:
8205 if (optimize_size)
8206 break; /* scratch ? 5 : 6 */
8207 if (ldi_ok)
8208 return avr_asm_len ("lsr %B0" CR_TAB
8209 "ror %A0" CR_TAB
8210 "swap %B0" CR_TAB
8211 "swap %A0" CR_TAB
8212 "andi %A0,0x0f" CR_TAB
8213 "eor %A0,%B0" CR_TAB
8214 "andi %B0,0x0f" CR_TAB
8215 "eor %A0,%B0", operands, plen, 8);
8216 if (scratch)
8217 return avr_asm_len ("lsr %B0" CR_TAB
8218 "ror %A0" CR_TAB
8219 "swap %B0" CR_TAB
8220 "swap %A0" CR_TAB
8221 "ldi %3,0x0f" CR_TAB
8222 "and %A0,%3" CR_TAB
8223 "eor %A0,%B0" CR_TAB
8224 "and %B0,%3" CR_TAB
8225 "eor %A0,%B0", operands, plen, 9);
8226 break; /* 10 */
8227
8228 case 6:
8229 if (optimize_size)
8230 break; /* scratch ? 5 : 6 */
8231 return avr_asm_len ("clr __tmp_reg__" CR_TAB
8232 "lsl %A0" CR_TAB
8233 "rol %B0" CR_TAB
8234 "rol __tmp_reg__" CR_TAB
8235 "lsl %A0" CR_TAB
8236 "rol %B0" CR_TAB
8237 "rol __tmp_reg__" CR_TAB
8238 "mov %A0,%B0" CR_TAB
8239 "mov %B0,__tmp_reg__", operands, plen, 9);
8240 case 7:
8241 return reg1_unused_after
8242 ? avr_asm_len ("lsl %A1" CR_TAB
8243 "mov %A0,%B1" CR_TAB
8244 "rol %A0" CR_TAB
8245 "sbc %B0,%B0" CR_TAB
8246 "neg %B0", operands, plen, 5)
8247 : avr_asm_len ("bst %A1,7" CR_TAB
8248 "mov %A0,%B1" CR_TAB
8249 "rol %A0" CR_TAB
8250 "sbc %B0,%B0" CR_TAB
8251 "neg %B0" CR_TAB
8252 "bld %A0,0", operands, plen, 6);
8253 case 8:
8254 return avr_asm_len ("mov %A0,%B1" CR_TAB
8255 "clr %B0", operands, plen, 2);
8256 case 9:
8257 return avr_asm_len ("mov %A0,%B1" CR_TAB
8258 "clr %B0" CR_TAB
8259 "lsr %A0", operands, plen, 3);
8260 case 10:
8261 return avr_asm_len ("mov %A0,%B1" CR_TAB
8262 "clr %B0" CR_TAB
8263 "lsr %A0" CR_TAB
8264 "lsr %A0", operands, plen, 4);
8265 case 11:
8266 return avr_asm_len ("mov %A0,%B1" CR_TAB
8267 "clr %B0" CR_TAB
8268 "lsr %A0" CR_TAB
8269 "lsr %A0" CR_TAB
8270 "lsr %A0", operands, plen, 5);
8271 case 12:
8272 if (ldi_ok)
8273 return avr_asm_len ("mov %A0,%B1" CR_TAB
8274 "clr %B0" CR_TAB
8275 "swap %A0" CR_TAB
8276 "andi %A0,0x0f", operands, plen, 4);
8277 return scratch
8278 ? avr_asm_len ("mov %A0,%B1" CR_TAB
8279 "clr %B0" CR_TAB
8280 "swap %A0" CR_TAB
8281 "ldi %3,0x0f" CR_TAB
8282 "and %A0,%3", operands, plen, 5)
8283 : avr_asm_len ("mov %A0,%B1" CR_TAB
8284 "clr %B0" CR_TAB
8285 "lsr %A0" CR_TAB
8286 "lsr %A0" CR_TAB
8287 "lsr %A0" CR_TAB
8288 "lsr %A0", operands, plen, 6);
8289 case 13:
8290 if (ldi_ok)
8291 return avr_asm_len ("mov %A0,%B0" CR_TAB
8292 "clr %B0" CR_TAB
8293 "swap %A0" CR_TAB
8294 "lsr %A0" CR_TAB
8295 "andi %A0,0x07", operands, plen, 5);
8296 if (AVR_HAVE_MUL && scratch)
8297 return avr_asm_len ("ldi %3,0x08" CR_TAB
8298 "mul %B0,%3" CR_TAB
8299 "mov %A0,r1" CR_TAB
8300 "clr %B0" CR_TAB
8301 "clr __zero_reg__", operands, plen, 5);
8302 if (optimize_size && scratch)
8303 break; /* 5 */
8304 if (scratch)
8305 return avr_asm_len ("mov %A0,%B0" CR_TAB
8306 "clr %B0" CR_TAB
8307 "swap %A0" CR_TAB
8308 "lsr %A0" CR_TAB
8309 "ldi %3,0x07" CR_TAB
8310 "and %A0,%3", operands, plen, 6);
8311 return AVR_HAVE_MUL
8312 ? avr_asm_len ("set" CR_TAB
8313 "bld r1,3" CR_TAB
8314 "mul %B0,r1" CR_TAB
8315 "mov %A0,r1" CR_TAB
8316 "clr %B0" CR_TAB
8317 "clr __zero_reg__", operands, plen, 6)
8318 : avr_asm_len ("mov %A0,%B0" CR_TAB
8319 "clr %B0" CR_TAB
8320 "lsr %A0" CR_TAB
8321 "lsr %A0" CR_TAB
8322 "lsr %A0" CR_TAB
8323 "lsr %A0" CR_TAB
8324 "lsr %A0", operands, plen, 7);
8325 case 14:
8326 if (AVR_HAVE_MUL && ldi_ok)
8327 return avr_asm_len ("ldi %A0,0x04" CR_TAB
8328 "mul %B0,%A0" CR_TAB
8329 "mov %A0,r1" CR_TAB
8330 "clr %B0" CR_TAB
8331 "clr __zero_reg__", operands, plen, 5);
8332 if (AVR_HAVE_MUL && scratch)
8333 return avr_asm_len ("ldi %3,0x04" CR_TAB
8334 "mul %B0,%3" CR_TAB
8335 "mov %A0,r1" CR_TAB
8336 "clr %B0" CR_TAB
8337 "clr __zero_reg__", operands, plen, 5);
8338 if (optimize_size && ldi_ok)
8339 return avr_asm_len ("mov %A0,%B0" CR_TAB
8340 "ldi %B0,6" "\n1:\t"
8341 "lsr %A0" CR_TAB
8342 "dec %B0" CR_TAB
8343 "brne 1b", operands, plen, 5);
8344 if (optimize_size && scratch)
8345 break; /* 5 */
8346 return avr_asm_len ("clr %A0" CR_TAB
8347 "lsl %B0" CR_TAB
8348 "rol %A0" CR_TAB
8349 "lsl %B0" CR_TAB
8350 "rol %A0" CR_TAB
8351 "clr %B0", operands, plen, 6);
8352 case 15:
8353 return avr_asm_len ("bst %B1,7" CR_TAB
8354 "clr %A0" CR_TAB
8355 "clr %B0" CR_TAB
8356 "bld %A0,0", operands, plen, 4);
8357 }
8358 }
8359
8360 out_shift_with_cnt ("lsr %B0" CR_TAB
8361 "ror %A0", insn, operands, plen, 2);
8362 return "";
8363 }
8364
8365
8366 /* 24-bit logic shift right */
8367
8368 const char *
8369 avr_out_lshrpsi3 (rtx_insn *insn, rtx *op, int *plen)
8370 {
8371 int dest = REGNO (op[0]);
8372 int src = REGNO (op[1]);
8373 bool src_unused_after_p = reg_unused_after (insn, op[1]);
8374
8375 if (CONST_INT_P (op[2]))
8376 {
8377 if (plen)
8378 *plen = 0;
8379
8380 switch (INTVAL (op[2]))
8381 {
8382 case 8:
8383 if (dest <= src)
8384 return avr_asm_len ("mov %A0,%B1" CR_TAB
8385 "mov %B0,%C1" CR_TAB
8386 "clr %C0", op, plen, 3);
8387 else
8388 return avr_asm_len ("clr %C0" CR_TAB
8389 "mov %B0,%C1" CR_TAB
8390 "mov %A0,%B1", op, plen, 3);
8391 case 15:
8392 avr_asm_len (src_unused_after_p
8393 ? "lsl %B1"
8394 : "bst %B1,7", op, plen, 1);
8395 if (dest != src + 2)
8396 avr_asm_len ("mov %A0,%C1", op, plen, 1);
8397 avr_asm_len ("clr %C0" CR_TAB
8398 "clr %B0" CR_TAB
8399 "rol %A0" CR_TAB
8400 "rol %B0", op, plen, 4);
8401 return src_unused_after_p
8402 ? ""
8403 : avr_asm_len ("bld %A0,0", op, plen, 1);
8404
8405 case 16:
8406 if (dest != src + 2)
8407 avr_asm_len ("mov %A0,%C1", op, plen, 1);
8408
8409 return avr_asm_len ("clr %B0" CR_TAB
8410 "clr %C0", op, plen, 2);
8411
8412 default:
8413 if (INTVAL (op[2]) < 24)
8414 break;
8415
8416 /* fall through */
8417
8418 case 23:
8419 return avr_asm_len ("bst %C1,7" CR_TAB
8420 "clr %A0" CR_TAB
8421 "clr %B0" CR_TAB
8422 "clr %C0" CR_TAB
8423 "bld %A0,0", op, plen, 5);
8424 } /* switch */
8425 }
8426
8427 out_shift_with_cnt ("lsr %C0" CR_TAB
8428 "ror %B0" CR_TAB
8429 "ror %A0", insn, op, plen, 3);
8430 return "";
8431 }
8432
8433
8434 /* 32-bit logic shift right ((unsigned int)x >> i) */
8435
8436 const char *
8437 lshrsi3_out (rtx_insn *insn, rtx operands[], int *plen)
8438 {
8439 if (CONST_INT_P (operands[2]))
8440 {
8441 int off = INTVAL (operands[2]);
8442 int reg0 = true_regnum (operands[0]);
8443 int reg1 = true_regnum (operands[1]);
8444 bool reg1_unused_after = reg_unused_after (insn, operands[1]);
8445 bool scratch_p = (GET_CODE (PATTERN (insn)) == PARALLEL
8446 && XVECLEN (PATTERN (insn), 0) == 3
8447 && REG_P (operands[3]));
8448 if (plen)
8449 *plen = 0;
8450
8451 switch (off)
8452 {
8453 default:
8454 if (off < 32)
8455 break;
8456
8457 return AVR_HAVE_MOVW
8458 ? avr_asm_len ("clr %D0" CR_TAB
8459 "clr %C0" CR_TAB
8460 "movw %A0,%C0", operands, plen, 3)
8461 : avr_asm_len ("clr %D0" CR_TAB
8462 "clr %C0" CR_TAB
8463 "clr %B0" CR_TAB
8464 "clr %A0", operands, plen, 4);
8465 case 8:
8466 return reg0 <= reg1
8467 ? avr_asm_len ("mov %A0,%B1" CR_TAB
8468 "mov %B0,%C1" CR_TAB
8469 "mov %C0,%D1" CR_TAB
8470 "clr %D0", operands, plen, 4)
8471 : avr_asm_len ("clr %D0" CR_TAB
8472 "mov %C0,%D1" CR_TAB
8473 "mov %B0,%C1" CR_TAB
8474 "mov %A0,%B1", operands, plen, 4);
8475 case 15:
8476 avr_asm_len (reg1_unused_after
8477 ? "lsl %B1"
8478 : "bst %B1,7", operands, plen, 1);
8479 if (reg0 != reg1 + 2)
8480 {
8481 if (AVR_HAVE_MOVW)
8482 avr_asm_len ("movw %A0,%C1", operands, plen, 1);
8483 else
8484 avr_asm_len ("mov %A0,%C1" CR_TAB
8485 "mov %B0,%D1", operands, plen, 2);
8486 }
8487 avr_asm_len ("clr %D0" CR_TAB
8488 "clr %C0" CR_TAB
8489 "rol %A0" CR_TAB
8490 "rol %B0" CR_TAB
8491 "rol %C0", operands, plen, 5);
8492 return reg1_unused_after
8493 ? ""
8494 : avr_asm_len ("bld %A0,0", operands, plen, 1);
8495
8496 case 16:
8497 if (reg0 == reg1 + 2)
8498 return avr_asm_len ("clr %C0" CR_TAB
8499 "clr %D0", operands, plen, 2);
8500 return AVR_HAVE_MOVW
8501 ? avr_asm_len ("movw %A0,%C1" CR_TAB
8502 "clr %C0" CR_TAB
8503 "clr %D0", operands, plen, 3)
8504 : avr_asm_len ("mov %B0,%D1" CR_TAB
8505 "mov %A0,%C1" CR_TAB
8506 "clr %C0" CR_TAB
8507 "clr %D0", operands, plen, 4);
8508 case 30:
8509 if (AVR_HAVE_MUL && scratch_p)
8510 return avr_asm_len ("ldi %3,1<<2" CR_TAB
8511 "mul %3,%D1" CR_TAB
8512 "mov %A0,r1" CR_TAB
8513 "clr __zero_reg__" CR_TAB
8514 "clr %B0" CR_TAB
8515 "clr %C0" CR_TAB
8516 "clr %D0", operands, plen, 7);
8517 // Fallthrough
8518
8519 case 29:
8520 case 28:
8521 {
8522 const bool ld_reg0_p = avr_ld_regno_p (reg0 + 0); // %A0
8523 const bool ld_reg1_p = avr_ld_regno_p (reg1 + 3); // %D1
8524 if (ld_reg0_p
8525 || (ld_reg1_p && reg1_unused_after)
8526 || scratch_p)
8527 {
8528 if (ld_reg0_p)
8529 avr_asm_len ("mov %A0,%D1" CR_TAB
8530 "swap %A0" CR_TAB
8531 "andi %A0,0x0f", operands, plen, 3);
8532 else if (ld_reg1_p && reg1_unused_after)
8533 avr_asm_len ("swap %D1" CR_TAB
8534 "andi %D1,0x0f" CR_TAB
8535 "mov %A0,%D1", operands, plen, 3);
8536 else
8537 avr_asm_len ("mov %A0,%D1" CR_TAB
8538 "swap %A0" CR_TAB
8539 "ldi %3,0x0f" CR_TAB
8540 "and %A0,%3", operands, plen, 4);
8541 for (int i = 28; i < off; ++i)
8542 avr_asm_len ("lsr %A0", operands, plen, 1);
8543 return avr_asm_len ("clr %B0" CR_TAB
8544 "clr %C0" CR_TAB
8545 "clr %D0", operands, plen, 3);
8546 }
8547 }
8548 // Fallthrough
8549
8550 case 27:
8551 case 26:
8552 case 25:
8553 case 24:
8554 avr_asm_len ("mov %A0,%D1", operands, plen, 1);
8555 for (int i = 24; i < off; ++i)
8556 avr_asm_len ("lsr %A0", operands, plen, 1);
8557 return avr_asm_len ("clr %B0" CR_TAB
8558 "clr %C0" CR_TAB
8559 "clr %D0", operands, plen, 3);
8560 case 31:
8561 return AVR_HAVE_MOVW
8562 ? avr_asm_len ("bst %D1,7" CR_TAB
8563 "clr %A0" CR_TAB
8564 "clr %B0" CR_TAB
8565 "movw %C0,%A0" CR_TAB
8566 "bld %A0,0", operands, plen, 5)
8567 : avr_asm_len ("bst %D1,7" CR_TAB
8568 "clr %A0" CR_TAB
8569 "clr %B0" CR_TAB
8570 "clr %C0" CR_TAB
8571 "clr %D0" CR_TAB
8572 "bld %A0,0", operands, plen, 6);
8573 } // switch
8574 }
8575
8576 out_shift_with_cnt ("lsr %D0" CR_TAB
8577 "ror %C0" CR_TAB
8578 "ror %B0" CR_TAB
8579 "ror %A0", insn, operands, plen, 4);
8580 return "";
8581 }
8582
8583
8584 /* When INSN is a PARALLEL with two SETs, a SET of REG_CC and a SET of a
8585 GPR, then return the second SET and set *CCMODE to the first SET's mode.
8586 Otherwise, return single_set and set *CCMODE to VOIDmode. */
8587
8588 static rtx
8589 avr_cc_set (rtx_insn *insn, machine_mode *ccmode)
8590 {
8591 // single_set() not only depends on the anatomy of an insn but also
8592 // on REG_UNUSED notes, thus we have to analyze by hand so that the
8593 // result only depends on the pattern.
8594
8595 rtx pat = PATTERN (insn);
8596
8597 if (GET_CODE (pat) == PARALLEL
8598 && XVECLEN (pat, 0) == 2
8599 && GET_CODE (XVECEXP (pat, 0, 0)) == SET
8600 && GET_CODE (XVECEXP (pat, 0, 1)) == SET)
8601 {
8602 rtx ccset = XVECEXP (pat, 0, 0);
8603 *ccmode = GET_MODE (SET_DEST (ccset));
8604 return XVECEXP (pat, 0, 1);
8605 }
8606
8607 *ccmode = VOIDmode;
8608 return single_set (insn);
8609 }
8610
8611
8612 /* Output addition of registers YOP[0] and YOP[1]
8613
8614 YOP[0] += extend (YOP[1])
8615
8616 or subtraction of registers YOP[0] and YOP[2]
8617
8618 YOP[0] -= extend (YOP[2])
8619
8620 where the integer modes satisfy SI >= YOP[0].mode >= YOP[1/2].mode >= QI,
8621 and the extension may be sign-extend, zero-extend or reg (no extend).
8622 INSN is either a single_set or a true parallel insn. In the latter case,
8623 INSN has two SETs: A SET of REG_CC and a SET like in the single_set case.
8624 Returns "".
8625
8626 If PLEN == NULL output the instructions.
8627 If PLEN != NULL set *PLEN to the length of the sequence in words. */
8628
8629 const char *
8630 avr_out_plus_ext (rtx_insn *insn, rtx *yop, int *plen)
8631 {
8632 rtx regs[2];
8633 machine_mode ccmode;
8634
8635 /* Ouch! Whether or not an insn is a single_set does not only depend
8636 on the anatomy of the pattern, but also on REG_UNUSED notes.
8637 Hence we have to dig by hand... */
8638
8639 const rtx src = SET_SRC (avr_cc_set (insn, &ccmode));
8640 const rtx_code add = GET_CODE (src);
8641 gcc_assert (GET_CODE (src) == PLUS || GET_CODE (src) == MINUS);
8642
8643 // Use XOP[] in the remainder with XOP[0] = YOP[0] and XOP[1] = YOP[1/2].
8644 rtx xop[2] = { yop[0], yop[add == PLUS ? 1 : 2] };
8645 const rtx xreg = XEXP (src, add == PLUS ? 1 : 0);
8646 const rtx xext = XEXP (src, add == PLUS ? 0 : 1);
8647 const rtx_code ext = GET_CODE (xext);
8648
8649 gcc_assert (REG_P (xreg)
8650 && (ext == ZERO_EXTEND || ext == SIGN_EXTEND || ext == REG));
8651
8652 const int n_bytes0 = GET_MODE_SIZE (GET_MODE (xop[0]));
8653 const int n_bytes1 = GET_MODE_SIZE (GET_MODE (xop[1]));
8654 rtx msb1 = all_regs_rtx[n_bytes1 - 1 + REGNO (xop[1])];
8655 // Prefer SBCI *,0 over SBC *,__zero_reg__.
8656 const bool sbci_p = add == MINUS && n_bytes0 > n_bytes1
8657 ? test_hard_reg_class (LD_REGS, avr_byte (xop[0], n_bytes1))
8658 : false;
8659
8660 const char *const s_ADD = add == PLUS ? "add %0,%1" : "sub %0,%1";
8661 const char *const s_ADC = add == PLUS ? "adc %0,%1" : "sbc %0,%1";
8662 const char *const s_DEC = add == PLUS
8663 ? "adc %0,__zero_reg__" CR_TAB "sbrc %1,7" CR_TAB "dec %0"
8664 : sbci_p
8665 ? "sbci %0,0" CR_TAB "sbrc %1,7" CR_TAB "inc %0"
8666 : "sbc %0,__zero_reg__" CR_TAB "sbrc %1,7" CR_TAB "inc %0";
8667
8668 // A register that containts 8 copies of $1.msb.
8669 rtx ext_reg = ext == ZERO_EXTEND ? zero_reg_rtx : NULL_RTX;
8670
8671 if (plen)
8672 *plen = 0;
8673
8674 if (ext == SIGN_EXTEND
8675 && (n_bytes0 > 1 + n_bytes1
8676 || reg_overlap_mentioned_p (msb1, xop[0])
8677 // The insn also wants to set SREG.N and SREG.Z.
8678 || ccmode == CCZNmode))
8679 {
8680 // Sign-extending more than one byte: Set tmp_reg to 0 or -1
8681 // depending on $1.msb. Same for the pathological case where
8682 // $0 and $1 overlap.
8683
8684 regs[0] = ext_reg = tmp_reg_rtx;
8685 regs[1] = msb1;
8686
8687 avr_asm_len ("mov %0,%1" CR_TAB
8688 "lsl %0" CR_TAB
8689 "sbc %0,%0", regs, plen, 3);
8690 }
8691
8692 // Adding the bytes of $1 is just plain additions / subtractions.
8693 // Same for the extended bytes when we have ext_reg.
8694
8695 avr_asm_len (s_ADD, xop, plen, 1);
8696
8697 for (int i = 1; i < n_bytes0; ++i)
8698 {
8699 regs[0] = all_regs_rtx[i + REGNO (xop[0])];
8700 regs[1] = i < n_bytes1 ? all_regs_rtx[i + REGNO (xop[1])] : ext_reg;
8701
8702 if (! regs[1])
8703 {
8704 // Extending just 1 byte: This is one instruction shorter
8705 // than sign-extending $1.msb to tmp_reg.
8706
8707 regs[1] = msb1;
8708 avr_asm_len (s_DEC, regs, plen, 3);
8709 }
8710 else if (sbci_p && regs[1] == zero_reg_rtx)
8711 avr_asm_len ("sbci %0,0", regs, plen, 1);
8712 else
8713 avr_asm_len (s_ADC, regs, plen, 1);
8714 }
8715
8716 return "";
8717 }
8718
8719
8720 /* Output code for addition of a sign-bit
8721
8722 YOP[0] += YOP[1] <CMP> 0
8723
8724 or such a subtraction:
8725
8726 YOP[0] -= YOP[2] <CMP> 0
8727
8728 where CMP is in { GE, LT }.
8729 If PLEN == NULL output the instructions.
8730 If PLEN != NULL set *PLEN to the length of the sequence in words. */
8731
8732 const char *
8733 avr_out_add_msb (rtx_insn *insn, rtx *yop, rtx_code cmp, int *plen)
8734 {
8735 const rtx_code add = GET_CODE (SET_SRC (single_set (insn)));
8736 const machine_mode mode = GET_MODE (yop[0]);
8737 const int n_bytes = GET_MODE_SIZE (mode);
8738 rtx sigop = yop[add == PLUS ? 1 : 2];
8739 rtx msb = avr_byte (sigop, GET_MODE_SIZE (GET_MODE (sigop)) - 1);
8740 rtx op[3] = { yop[0], msb, nullptr };
8741
8742 if (plen)
8743 *plen = 0;
8744
8745 if (n_bytes == 1
8746 || (n_bytes == 2 && avr_adiw_reg_p (op[0])))
8747 {
8748 avr_asm_len (cmp == LT
8749 ? "sbrc %1,7"
8750 : "sbrs %1,7", op, plen, 1);
8751 const char *s_add = add == PLUS
8752 ? n_bytes == 1 ? "inc %0" : "adiw %0,1"
8753 : n_bytes == 1 ? "dec %0" : "sbiw %0,1";
8754 return avr_asm_len (s_add, op, plen, 1);
8755 }
8756
8757 bool labl_p = false;
8758 const char *s_code0 = nullptr;
8759
8760 // Default code provided SREG.C = MSBit.
8761 const char *s_code = add == PLUS
8762 ? "adc %2,__zero_reg__"
8763 : "sbc %2,__zero_reg__";
8764
8765 if (cmp == LT)
8766 {
8767 if (reg_unused_after (insn, sigop)
8768 && ! reg_overlap_mentioned_p (msb, op[0]))
8769 avr_asm_len ("lsl %1", op, plen, 1);
8770 else
8771 avr_asm_len ("mov __tmp_reg__,%1" CR_TAB
8772 "lsl __tmp_reg__", op, plen, 2);
8773 }
8774 else if (test_hard_reg_class (LD_REGS, msb))
8775 {
8776 avr_asm_len ("cpi %1,0x80", op, plen, 1);
8777 }
8778 else if (test_hard_reg_class (LD_REGS, op[0]))
8779 {
8780 labl_p = true;
8781 avr_asm_len ("tst %1" CR_TAB
8782 "brmi 0f", op, plen, 2);
8783 s_code0 = add == PLUS ? "subi %2,-1" : "subi %2,1";
8784 s_code = add == PLUS ? "sbci %2,-1" : "sbci %2,0";
8785 }
8786 else
8787 {
8788 labl_p = true;
8789 avr_asm_len ("tst %1" CR_TAB
8790 "brmi 0f" CR_TAB
8791 "sec", op, plen, 3);
8792 }
8793
8794 for (int i = 0; i < n_bytes; ++i)
8795 {
8796 op[2] = avr_byte (op[0], i);
8797 avr_asm_len (i == 0 && s_code0
8798 ? s_code0
8799 : s_code, op, plen, 1);
8800 }
8801
8802 return labl_p
8803 ? avr_asm_len ("0:", op, plen, 0)
8804 : "";
8805 }
8806
8807
8808 /* Output addition of register XOP[0] and compile time constant XOP[2].
8809 XINSN is a single_set insn or an insn pattern.
8810 CODE == PLUS: perform addition by using ADD instructions or
8811 CODE == MINUS: perform addition by using SUB instructions:
8812
8813 XOP[0] = XOP[0] + XOP[2]
8814
8815 Or perform addition/subtraction with register XOP[2] depending on CODE:
8816
8817 XOP[0] = XOP[0] +/- XOP[2]
8818
8819 If PLEN == NULL, print assembler instructions to perform the operation;
8820 otherwise, set *PLEN to the length of the instruction sequence (in words)
8821 printed with PLEN == NULL. XOP[3] is an 8-bit scratch register or NULL_RTX.
8822
8823 CODE_SAT == UNKNOWN: Perform ordinary, non-saturating operation.
8824 CODE_SAT != UNKNOWN: Perform operation and saturate according to CODE_SAT.
8825 If CODE_SAT != UNKNOWN then SIGN contains the sign of the summand resp.
8826 the subtrahend in the original insn, provided it is a compile time constant.
8827 In all other cases, SIGN is 0.
8828
8829 If OUT_LABEL is true, print the final 0: label which is needed for
8830 saturated addition / subtraction. The only case where OUT_LABEL = false
8831 is useful is for saturated addition / subtraction performed during
8832 fixed-point rounding, cf. `avr_out_round'. */
8833
8834 static void
8835 avr_out_plus_1 (rtx xinsn, rtx *xop, int *plen, rtx_code code,
8836 rtx_code code_sat, int sign, bool out_label)
8837 {
8838 rtx_insn *insn = xinsn && INSN_P (xinsn)
8839 ? as_a <rtx_insn *> (xinsn)
8840 : nullptr;
8841
8842 /* MODE of the operation. */
8843 machine_mode mode = GET_MODE (xop[0]);
8844
8845 /* INT_MODE of the same size. */
8846 scalar_int_mode imode = int_mode_for_mode (mode).require ();
8847
8848 /* Number of bytes to operate on. */
8849 int n_bytes = GET_MODE_SIZE (mode);
8850
8851 int regno0 = REGNO (xop[0]);
8852 if (optimize && code_sat == UNKNOWN)
8853 while (n_bytes && avr_result_regno_unused_p (insn, regno0 + n_bytes - 1))
8854 n_bytes -= 1;
8855
8856 /* Value (0..0xff) held in clobber register op[3] or -1 if unknown. */
8857 int clobber_val = -1;
8858
8859 /* op[0]: 8-bit destination register
8860 op[1]: 8-bit const int
8861 op[2]: 8-bit scratch register */
8862 rtx op[3];
8863
8864 /* Started the operation? Before starting the operation we may skip
8865 adding 0. This is no more true after the operation started because
8866 carry must be taken into account. */
8867 bool started = false;
8868
8869 /* Value to add. There are two ways to add VAL: R += VAL and R -= -VAL. */
8870 rtx xval = xop[2];
8871
8872 /* Output a BRVC instruction. Only needed with saturation. */
8873 bool out_brvc = true;
8874
8875 if (plen)
8876 *plen = 0;
8877
8878 if (REG_P (xop[2]))
8879 {
8880 if (optimize
8881 && REGNO (xop[0]) != REGNO (xop[2])
8882 && reg_overlap_mentioned_p (xop[0], xop[2]))
8883 {
8884 /* PR118878: Paradoxical SUBREGs may result in overlapping
8885 registers. The assumption is that the overlapping part
8886 is unused garbage. */
8887 gcc_assert (n_bytes <= 4);
8888 int delta = (int) REGNO (xop[0]) - (int) REGNO (xop[2]);
8889 n_bytes = std::min (n_bytes, std::abs (delta));
8890 }
8891
8892 for (int i = 0; i < n_bytes; i++)
8893 {
8894 /* We operate byte-wise on the destination. */
8895 op[0] = avr_byte (xop[0], i);
8896 op[1] = avr_byte (xop[2], i);
8897
8898 if (i == 0)
8899 avr_asm_len (code == PLUS ? "add %0,%1" : "sub %0,%1",
8900 op, plen, 1);
8901 else
8902 avr_asm_len (code == PLUS ? "adc %0,%1" : "sbc %0,%1",
8903 op, plen, 1);
8904 }
8905
8906 if (MINUS == code
8907 && REGNO (xop[0]) == REGNO (xop[2]))
8908 return;
8909
8910 goto saturate;
8911 }
8912
8913 if (CONST_FIXED_P (xval))
8914 xval = avr_to_int_mode (xval);
8915
8916 /* Adding/Subtracting zero is a no-op. */
8917
8918 if (xval == const0_rtx)
8919 return;
8920
8921 if (MINUS == code)
8922 xval = simplify_unary_operation (NEG, imode, xval, imode);
8923
8924 op[2] = xop[3];
8925
8926 if (SS_PLUS == code_sat && MINUS == code
8927 && sign < 0
8928 && 0x80 == avr_uint8 (xval, n_bytes - 1))
8929 {
8930 /* We compute x + 0x80 by means of SUB instructions. We negated the
8931 constant subtrahend above and are left with x - (-128) so that we
8932 need something like SUBI r,128 which does not exist because SUBI sets
8933 V according to the sign of the subtrahend. Notice the only case
8934 where this must be done is when NEG overflowed in case [2s] because
8935 the V computation needs the right sign of the subtrahend. */
8936
8937 rtx msb = avr_byte (xop[0], n_bytes - 1);
8938
8939 avr_asm_len ("subi %0,128" CR_TAB
8940 "brmi 0f", &msb, plen, 2);
8941 out_brvc = false;
8942
8943 goto saturate;
8944 }
8945
8946 for (int i = 0; i < n_bytes; i++)
8947 {
8948 /* We operate byte-wise on the destination. */
8949 rtx reg8 = avr_byte (xop[0], i);
8950 rtx xval8 = avr_byte (xval, i);
8951
8952 /* 8-bit value to operate with this byte. */
8953 unsigned int val8 = UINTVAL (xval8) & GET_MODE_MASK (QImode);
8954
8955 /* Registers R16..R31 can operate with immediate. */
8956 bool ld_reg_p = test_hard_reg_class (LD_REGS, reg8);
8957
8958 op[0] = reg8;
8959 op[1] = gen_int_mode (val8, QImode);
8960
8961 /* To get usable cc0 no low-bytes must have been skipped. */
8962
8963 if (!started
8964 && i % 2 == 0
8965 && i + 2 <= n_bytes
8966 && avr_adiw_reg_p (reg8))
8967 {
8968 unsigned int val16 = avr_uint16 (xval, i);
8969
8970 /* Registers R24, X, Y, Z can use ADIW/SBIW with constants < 64
8971 i.e. operate word-wise. */
8972
8973 if (val16 < 64)
8974 {
8975 if (val16 != 0)
8976 {
8977 started = true;
8978 avr_asm_len (code == PLUS ? "adiw %0,%1" : "sbiw %0,%1",
8979 op, plen, 1);
8980 }
8981
8982 i++;
8983 continue;
8984 }
8985 }
8986
8987 if (AVR_TINY
8988 && optimize
8989 && i == 0
8990 && n_bytes == 2
8991 // When that pass adjusts the frame pointer, then we know that
8992 // reg Y points to ordinary memory, and the only side-effect
8993 // of -Y and Y+ is the side effect on Y.
8994 && avropt_fuse_add >= 2
8995 && frame_pointer_needed
8996 && REGNO (xop[0]) == FRAME_POINTER_REGNUM)
8997 {
8998 if (insn
8999 && _reg_unused_after (insn, xop[0], false))
9000 return;
9001
9002 if (AVR_HAVE_8BIT_SP)
9003 {
9004 avr_asm_len ("subi %A0,%n2", xop, plen, 1);
9005 return;
9006 }
9007 else if (xop[2] == const1_rtx || xop[2] == constm1_rtx)
9008 {
9009 avr_asm_len (xop[2] == const1_rtx
9010 ? "ld __tmp_reg__,%a0+"
9011 : "ld __tmp_reg__,-%a0", xop, plen, 1);
9012 return;
9013 }
9014 }
9015
9016 if (val8 == 0)
9017 {
9018 if (started)
9019 avr_asm_len (code == PLUS
9020 ? "adc %0,__zero_reg__"
9021 : ld_reg_p ? "sbci %0,0" : "sbc %0,__zero_reg__",
9022 op, plen, 1);
9023 continue;
9024 }
9025 else if ((val8 == 1 || val8 == 0xff)
9026 && UNKNOWN == code_sat
9027 && !started
9028 && i == n_bytes - 1)
9029 {
9030 avr_asm_len ((code == PLUS) ^ (val8 == 1) ? "dec %0" : "inc %0",
9031 op, plen, 1);
9032 break;
9033 }
9034
9035 switch (code)
9036 {
9037 case PLUS:
9038
9039 gcc_assert (plen != nullptr || (op[2] && REG_P (op[2])));
9040
9041 if (plen != nullptr && UNKNOWN != code_sat)
9042 {
9043 /* This belongs to the x + 0x80 corner case. The code with
9044 ADD instruction is not smaller, thus make this case
9045 expensive so that the caller won't pick it. */
9046
9047 *plen += 10;
9048 break;
9049 }
9050
9051 if (clobber_val != (int) val8)
9052 avr_asm_len ("ldi %2,%1", op, plen, 1);
9053 clobber_val = (int) val8;
9054
9055 avr_asm_len (started ? "adc %0,%2" : "add %0,%2", op, plen, 1);
9056
9057 break; /* PLUS */
9058
9059 case MINUS:
9060
9061 if (ld_reg_p)
9062 avr_asm_len (started ? "sbci %0,%1" : "subi %0,%1", op, plen, 1);
9063 else
9064 {
9065 gcc_assert (plen != nullptr || REG_P (op[2]));
9066
9067 if (clobber_val != (int) val8)
9068 avr_asm_len ("ldi %2,%1", op, plen, 1);
9069 clobber_val = (int) val8;
9070
9071 avr_asm_len (started ? "sbc %0,%2" : "sub %0,%2", op, plen, 1);
9072 }
9073
9074 break; /* MINUS */
9075
9076 default:
9077 /* Unknown code */
9078 gcc_unreachable ();
9079 }
9080
9081 started = true;
9082
9083 } /* for all sub-bytes */
9084
9085 saturate:
9086
9087 if (UNKNOWN == code_sat)
9088 return;
9089
9090 /* Vanilla addition/subtraction is done. We are left with saturation.
9091
9092 We have to compute A = A <op> B where A is a register and
9093 B is a register or a non-zero compile time constant CONST.
9094 A is register class "r" if unsigned && B is REG. Otherwise, A is in "d".
9095 B stands for the original operand $2 in XINSN. In the case of B = CONST,
9096 SIGN in { -1, 1 } is the sign of B. Otherwise, SIGN is 0.
9097
9098 CODE is the instruction flavor we use in the asm sequence to perform <op>.
9099
9100
9101 unsigned
9102 operation | code | sat if | b is | sat value | case
9103 -----------------+-------+----------+--------------+-----------+-------
9104 + as a + b | add | C == 1 | const, reg | u+ = 0xff | [1u]
9105 + as a - (-b) | sub | C == 0 | const | u+ = 0xff | [2u]
9106 - as a - b | sub | C == 1 | const, reg | u- = 0 | [3u]
9107 - as a + (-b) | add | C == 0 | const | u- = 0 | [4u]
9108
9109
9110 signed
9111 operation | code | sat if | b is | sat value | case
9112 -----------------+-------+----------+--------------+-----------+-------
9113 + as a + b | add | V == 1 | const, reg | s+ | [1s]
9114 + as a - (-b) | sub | V == 1 | const | s+ | [2s]
9115 - as a - b | sub | V == 1 | const, reg | s- | [3s]
9116 - as a + (-b) | add | V == 1 | const | s- | [4s]
9117
9118 s+ = b < 0 ? -0x80 : 0x7f
9119 s- = b < 0 ? 0x7f : -0x80
9120
9121 The cases a - b actually perform a - (-(-b)) if B is CONST. */
9122
9123 op[0] = avr_byte (xop[0], n_bytes - 1);
9124 op[1] = n_bytes > 1 ? avr_byte (xop[0], n_bytes - 2) : NULL_RTX;
9125
9126 bool need_copy = true;
9127 int len_call = 1 + AVR_HAVE_JMP_CALL;
9128
9129 switch (code_sat)
9130 {
9131 default:
9132 gcc_unreachable ();
9133
9134 case SS_PLUS:
9135 case SS_MINUS:
9136
9137 if (out_brvc)
9138 avr_asm_len ("brvc 0f", op, plen, 1);
9139
9140 if (reg_overlap_mentioned_p (xop[0], xop[2]))
9141 {
9142 /* [1s,reg] */
9143
9144 if (n_bytes == 1)
9145 avr_asm_len ("ldi %0,0x7f" CR_TAB
9146 "adc %0,__zero_reg__", op, plen, 2);
9147 else
9148 avr_asm_len ("ldi %0,0x7f" CR_TAB
9149 "ldi %1,0xff" CR_TAB
9150 "adc %1,__zero_reg__" CR_TAB
9151 "adc %0,__zero_reg__", op, plen, 4);
9152 }
9153 else if (sign == 0 && PLUS == code)
9154 {
9155 /* [1s,reg] */
9156
9157 op[2] = avr_byte (xop[2], n_bytes - 1);
9158
9159 if (n_bytes == 1)
9160 avr_asm_len ("ldi %0,0x80" CR_TAB
9161 "sbrs %2,7" CR_TAB
9162 "dec %0", op, plen, 3);
9163 else
9164 avr_asm_len ("ldi %0,0x80" CR_TAB
9165 "cp %2,%0" CR_TAB
9166 "sbc %1,%1" CR_TAB
9167 "sbci %0,0", op, plen, 4);
9168 }
9169 else if (sign == 0 && MINUS == code)
9170 {
9171 /* [3s,reg] */
9172
9173 op[2] = avr_byte (xop[2], n_bytes - 1);
9174
9175 if (n_bytes == 1)
9176 avr_asm_len ("ldi %0,0x7f" CR_TAB
9177 "sbrs %2,7" CR_TAB
9178 "inc %0", op, plen, 3);
9179 else
9180 avr_asm_len ("ldi %0,0x7f" CR_TAB
9181 "cp %0,%2" CR_TAB
9182 "sbc %1,%1" CR_TAB
9183 "sbci %0,-1", op, plen, 4);
9184 }
9185 else if ((sign < 0) ^ (SS_MINUS == code_sat))
9186 {
9187 /* [1s,const,B < 0] [2s,B < 0] */
9188 /* [3s,const,B > 0] [4s,B > 0] */
9189
9190 if (n_bytes == 8)
9191 {
9192 avr_asm_len ("%~call __clr_8", op, plen, len_call);
9193 need_copy = false;
9194 }
9195
9196 avr_asm_len ("ldi %0,0x80", op, plen, 1);
9197 if (n_bytes > 1 && need_copy)
9198 avr_asm_len ("clr %1", op, plen, 1);
9199 }
9200 else if ((sign > 0) ^ (SS_MINUS == code_sat))
9201 {
9202 /* [1s,const,B > 0] [2s,B > 0] */
9203 /* [3s,const,B < 0] [4s,B < 0] */
9204
9205 if (n_bytes == 8)
9206 {
9207 avr_asm_len ("sec" CR_TAB
9208 "%~call __sbc_8", op, plen, 1 + len_call);
9209 need_copy = false;
9210 }
9211
9212 avr_asm_len ("ldi %0,0x7f", op, plen, 1);
9213 if (n_bytes > 1 && need_copy)
9214 avr_asm_len ("ldi %1,0xff", op, plen, 1);
9215 }
9216 else
9217 gcc_unreachable ();
9218
9219 break;
9220
9221 case US_PLUS:
9222 /* [1u] : [2u] */
9223
9224 avr_asm_len (PLUS == code ? "brcc 0f" : "brcs 0f", op, plen, 1);
9225
9226 if (n_bytes == 8)
9227 {
9228 if (MINUS == code)
9229 avr_asm_len ("sec", op, plen, 1);
9230 avr_asm_len ("%~call __sbc_8", op, plen, len_call);
9231
9232 need_copy = false;
9233 }
9234 else
9235 {
9236 if (MINUS == code && !test_hard_reg_class (LD_REGS, op[0]))
9237 avr_asm_len ("sec" CR_TAB
9238 "sbc %0,%0", op, plen, 2);
9239 else
9240 avr_asm_len (PLUS == code ? "sbc %0,%0" : "ldi %0,0xff",
9241 op, plen, 1);
9242 }
9243 break; /* US_PLUS */
9244
9245 case US_MINUS:
9246 /* [4u] : [3u] */
9247
9248 avr_asm_len (PLUS == code ? "brcs 0f" : "brcc 0f", op, plen, 1);
9249
9250 if (n_bytes == 8)
9251 {
9252 avr_asm_len ("%~call __clr_8", op, plen, len_call);
9253 need_copy = false;
9254 }
9255 else
9256 avr_asm_len ("clr %0", op, plen, 1);
9257
9258 break;
9259 }
9260
9261 /* We set the MSB in the unsigned case and the 2 MSBs in the signed case.
9262 Now copy the right value to the LSBs. */
9263
9264 if (need_copy && n_bytes > 1)
9265 {
9266 if (US_MINUS == code_sat || US_PLUS == code_sat)
9267 {
9268 avr_asm_len ("mov %1,%0", op, plen, 1);
9269
9270 if (n_bytes > 2)
9271 {
9272 op[0] = xop[0];
9273 if (AVR_HAVE_MOVW)
9274 avr_asm_len ("movw %0,%1", op, plen, 1);
9275 else
9276 avr_asm_len ("mov %A0,%1" CR_TAB
9277 "mov %B0,%1", op, plen, 2);
9278 }
9279 }
9280 else if (n_bytes > 2)
9281 {
9282 op[0] = xop[0];
9283 avr_asm_len ("mov %A0,%1" CR_TAB
9284 "mov %B0,%1", op, plen, 2);
9285 }
9286 }
9287
9288 if (need_copy && n_bytes == 8)
9289 {
9290 if (AVR_HAVE_MOVW)
9291 avr_asm_len ("movw %r0+2,%0" CR_TAB
9292 "movw %r0+4,%0", xop, plen, 2);
9293 else
9294 avr_asm_len ("mov %r0+2,%0" CR_TAB
9295 "mov %r0+3,%0" CR_TAB
9296 "mov %r0+4,%0" CR_TAB
9297 "mov %r0+5,%0", xop, plen, 4);
9298 }
9299
9300 if (out_label)
9301 avr_asm_len ("0:", op, plen, 0);
9302 }
9303
9304
9305 /* Output addition/subtraction of register XOP[0] and a constant XOP[2] that
9306 is not a compile-time constant:
9307
9308 XOP[0] = XOP[0] +/- XOP[2]
9309
9310 This is a helper for the function below. The only insns that need this
9311 are additions/subtraction for pointer modes, i.e. HImode and PSImode. */
9312
9313 static const char *
9314 avr_out_plus_symbol (rtx *xop, rtx_code code, int *plen)
9315 {
9316 machine_mode mode = GET_MODE (xop[0]);
9317
9318 /* Only pointer modes want to add symbols. */
9319
9320 gcc_assert (mode == HImode || mode == PSImode);
9321
9322 if (mode == HImode
9323 && const_0mod256_operand (xop[2], HImode))
9324 return avr_asm_len (PLUS == code
9325 ? "subi %B0,hi8(-(%2))"
9326 : "subi %B0,hi8(%2)", xop, plen, -1);
9327
9328 avr_asm_len (PLUS == code
9329 ? "subi %A0,lo8(-(%2))" CR_TAB "sbci %B0,hi8(-(%2))"
9330 : "subi %A0,lo8(%2)" CR_TAB "sbci %B0,hi8(%2)",
9331 xop, plen, -2);
9332
9333 if (PSImode == mode)
9334 avr_asm_len (PLUS == code
9335 ? "sbci %C0,hlo8(-(%2))"
9336 : "sbci %C0,hlo8(%2)", xop, plen, 1);
9337 return "";
9338 }
9339
9340
9341 /* Prepare operands of addition/subtraction to be used with avr_out_plus_1.
9342
9343 INSN is a single_set insn or an insn pattern with a binary operation as
9344 SET_SRC that is one of: PLUS, SS_PLUS, US_PLUS, MINUS, SS_MINUS, US_MINUS.
9345
9346 XOP are the operands of INSN. In the case of 64-bit operations with
9347 constant XOP[] has just one element: The summand/subtrahend in XOP[0].
9348 The non-saturating insns up to 32 bits may or may not supply a "d" class
9349 scratch as XOP[3].
9350
9351 If PLEN == NULL output the instructions.
9352 If PLEN != NULL set *PLEN to the length of the sequence in words.
9353
9354 PLEN defaults to NULL.
9355
9356 OUT_LABEL defaults to TRUE. For a description, see AVR_OUT_PLUS_1.
9357
9358 Return "" */
9359
9360 const char *
9361 avr_out_plus (rtx insn, rtx *xop, int *plen, bool out_label)
9362 {
9363 int len_plus, len_minus;
9364 rtx op[4];
9365 rtx xpattern = INSN_P (insn) ? single_set (as_a <rtx_insn *> (insn)) : insn;
9366 rtx xdest = SET_DEST (xpattern);
9367 machine_mode mode = GET_MODE (xdest);
9368 scalar_int_mode imode = int_mode_for_mode (mode).require ();
9369 int n_bytes = GET_MODE_SIZE (mode);
9370 rtx_code code_sat = GET_CODE (SET_SRC (xpattern));
9371 rtx_code code
9372 = (PLUS == code_sat || SS_PLUS == code_sat || US_PLUS == code_sat
9373 ? PLUS : MINUS);
9374
9375 /* PLUS and MINUS don't saturate: Use modular wrap-around. */
9376
9377 if (PLUS == code_sat || MINUS == code_sat)
9378 code_sat = UNKNOWN;
9379
9380 if (n_bytes <= 4 && REG_P (xop[2]))
9381 {
9382 avr_out_plus_1 (insn, xop, plen, code, code_sat, 0, out_label);
9383 return "";
9384 }
9385
9386 if (n_bytes == 8)
9387 {
9388 op[0] = gen_rtx_REG (DImode, ACC_A);
9389 op[1] = gen_rtx_REG (DImode, ACC_A);
9390 op[2] = avr_to_int_mode (xop[0]);
9391 }
9392 else
9393 {
9394 if (!REG_P (xop[2])
9395 && !CONST_INT_P (xop[2])
9396 && !CONST_FIXED_P (xop[2]))
9397 {
9398 return avr_out_plus_symbol (xop, code, plen);
9399 }
9400
9401 op[0] = avr_to_int_mode (xop[0]);
9402 op[1] = avr_to_int_mode (xop[1]);
9403 op[2] = avr_to_int_mode (xop[2]);
9404 }
9405
9406 /* Saturations and 64-bit operations don't have a clobber operand.
9407 For the other cases, the caller will provide a proper XOP[3]. */
9408
9409 xpattern = INSN_P (insn) ? PATTERN (insn) : insn;
9410 op[3] = PARALLEL == GET_CODE (xpattern) ? xop[3] : NULL_RTX;
9411
9412 /* Saturation will need the sign of the original operand. */
9413
9414 int sign = avr_int8 (op[2], n_bytes - 1) < 0 ? -1 : 1;
9415
9416 /* If we subtract and the subtrahend is a constant, then negate it
9417 so that avr_out_plus_1 can be used. */
9418
9419 if (MINUS == code)
9420 op[2] = simplify_unary_operation (NEG, imode, op[2], imode);
9421
9422 /* Work out the shortest sequence. */
9423
9424 avr_out_plus_1 (insn, op, &len_minus, MINUS, code_sat, sign, out_label);
9425 avr_out_plus_1 (insn, op, &len_plus, PLUS, code_sat, sign, out_label);
9426
9427 if (plen)
9428 *plen = (len_minus <= len_plus) ? len_minus : len_plus;
9429 else if (len_minus <= len_plus)
9430 avr_out_plus_1 (insn, op, nullptr, MINUS, code_sat, sign, out_label);
9431 else
9432 avr_out_plus_1 (insn, op, nullptr, PLUS, code_sat, sign, out_label);
9433
9434 return "";
9435 }
9436
9437
9438 /* Output an addition with a compile-time constant that sets SREG.N:
9439
9440 XOP[0] += XOP[1]
9441
9442 where XOP[0] is a HI, PSI or SI register, and XOP[1] is a register or a
9443 compile-time constant. XOP[2] is SCRATCH or a QI clobber reg. Return "".
9444
9445 If PLEN == NULL output the instructions.
9446 If PLEN != NULL set *PLEN to the length of the sequence in words. */
9447
9448 const char *
9449 avr_out_plus_set_N (rtx *xop, int *plen)
9450 {
9451 gcc_assert (xop[1] != const0_rtx);
9452
9453 // The output function for vanilla additions, avr_out_plus_1, can be
9454 // used because it always issues an operation on the MSB (except when
9455 // the addend is zero).
9456
9457 rtx op[] = { xop[0], xop[0], xop[1], xop[2] };
9458
9459 if (REG_P (xop[1]))
9460 {
9461 avr_out_plus_1 (NULL_RTX, op, plen, PLUS, UNKNOWN, 0, false);
9462 }
9463 else
9464 {
9465 int len_plus, len_minus;
9466
9467 avr_out_plus_1 (NULL_RTX, op, &len_plus, PLUS, UNKNOWN, 0, false);
9468 avr_out_plus_1 (NULL_RTX, op, &len_minus, MINUS, UNKNOWN, 0, false);
9469
9470 avr_out_plus_1 (NULL_RTX, op, plen, len_minus < len_plus ? MINUS : PLUS,
9471 UNKNOWN, 0, false);
9472 }
9473
9474 return "";
9475 }
9476
9477
9478 /* Output an instruction sequence for addition of REG in XOP[0] and CONST_INT
9479 in XOP[1] in such a way that SREG.Z and SREG.N are set according to the
9480 result. The mode is HI, PSI or SI. XOP[2] might be a d-regs clobber
9481 register. If XOP[2] is SCRATCH, then the addition can be performed
9482 without a clobber reg. Return "".
9483
9484 If PLEN == NULL, then output the instructions.
9485 If PLEN != NULL, then set *PLEN to the length of the sequence in words. */
9486
9487 const char *
9488 avr_out_plus_set_ZN (rtx *xop, int *plen)
9489 {
9490 if (plen)
9491 *plen = 0;
9492
9493 // Register to compare and value to compare against.
9494 rtx xreg = xop[0];
9495 rtx xval = xop[1];
9496
9497 machine_mode mode = GET_MODE (xreg);
9498
9499 // Number of bytes to operate on.
9500 int n_bytes = GET_MODE_SIZE (mode);
9501
9502 if (n_bytes == 2
9503 && avr_adiw_reg_p (xreg)
9504 && IN_RANGE (INTVAL (xval), 1, 63))
9505 {
9506 // Add 16-bit value in [1..63] to a w register.
9507 return avr_asm_len ("adiw %0,%1", xop, plen, 1);
9508 }
9509
9510 // Addition won't work; subtract the negative of XVAL instead.
9511 xval = simplify_unary_operation (NEG, mode, xval, mode);
9512
9513 // Value (0..0xff) held in clobber register xop[2] or -1 if unknown.
9514 int clobber_val = -1;
9515
9516 // [0] = Current sub-register.
9517 // [1] = Current partial xval.
9518 // [2] = 8-bit clobber d-register or SCRATCH.
9519 rtx op[3];
9520 op[2] = xop[2];
9521
9522 // Work byte-wise from LSB to MSB. The lower two bytes might be
9523 // SBIW'ed in one go.
9524 for (int i = 0; i < n_bytes; ++i)
9525 {
9526 op[0] = avr_byte (xreg, i);
9527
9528 if (i == 0
9529 && n_bytes >= 2
9530 && avr_adiw_reg_p (op[0]))
9531 {
9532 op[1] = avr_word (xval, 0);
9533 if (IN_RANGE (INTVAL (op[1]), 0, 63))
9534 {
9535 // SBIW can handle the lower 16 bits.
9536 avr_asm_len ("sbiw %0,%1", op, plen, 1);
9537
9538 // Next byte has already been handled: Skip it.
9539 ++i;
9540 continue;
9541 }
9542 }
9543
9544 op[1] = avr_byte (xval, i);
9545
9546 if (test_hard_reg_class (LD_REGS, op[0]))
9547 {
9548 // d-regs can subtract immediates.
9549 avr_asm_len (i == 0
9550 ? "subi %0,%1"
9551 : "sbci %0,%1", op, plen, 1);
9552 }
9553 else
9554 {
9555 int val8 = 0xff & INTVAL (op[1]);
9556 if (val8 == 0)
9557 {
9558 // Any register can subtract 0.
9559 avr_asm_len (i == 0
9560 ? "sub %0,__zero_reg__"
9561 : "sbc %0,__zero_reg__", op, plen, 1);
9562 }
9563 else
9564 {
9565 // Use d-register to hold partial xval.
9566
9567 if (val8 != clobber_val)
9568 {
9569 // Load partial xval to QI clobber reg and memoize for later.
9570 gcc_assert (REG_P (op[2]));
9571 avr_asm_len ("ldi %2,%1", op, plen, 1);
9572 clobber_val = val8;
9573 }
9574
9575 avr_asm_len (i == 0
9576 ? "sub %0,%2"
9577 : "sbc %0,%2", op, plen, 1);
9578 }
9579 }
9580 } // Loop bytes.
9581
9582 return "";
9583 }
9584
9585
9586 /* A helper worker for `op8_ZN_operator'. Allow
9587
9588 OP0 <code> OP1
9589
9590 QImode operations that set SREG.N and SREG.Z in a usable way.
9591 these are:
9592
9593 * OP0 is a QImode register, and
9594 * OP1 is a QImode register or CONST_INT, and
9595
9596 the allowed operations is one of:
9597
9598 * SHIFTs with a const_int offset in { 1, 2, 3 }.
9599 * MINUS and XOR with a register operand
9600 * IOR and AND with a register operand, or d-reg + const_int
9601 * PLUS with a register operand, or d-reg + const_int,
9602 or a const_int in { -2, -1, 1, 2 }. */
9603
9604 bool
9605 avr_op8_ZN_operator (rtx op)
9606 {
9607 const rtx_code code = GET_CODE (op);
9608 rtx op0 = XEXP (op, 0);
9609 rtx op1 = XEXP (op, 1);
9610
9611 if (! register_operand (op0, QImode)
9612 || ! (register_operand (op1, QImode)
9613 || const_int_operand (op1, QImode)))
9614 return false;
9615
9616 const bool reg1_p = REG_P (op1);
9617 const bool ld_reg0_p = test_hard_reg_class (LD_REGS, op0);
9618
9619 switch (code)
9620 {
9621 default:
9622 break;
9623
9624 case ASHIFT:
9625 case ASHIFTRT:
9626 case LSHIFTRT:
9627 return const_1_to_3_operand (op1, QImode);
9628
9629 case MINUS:
9630 case XOR:
9631 return reg1_p;
9632
9633 case IOR:
9634 case AND:
9635 return reg1_p || ld_reg0_p;
9636
9637 case PLUS:
9638 return reg1_p || ld_reg0_p || abs1_abs2_operand (op1, QImode);
9639 }
9640
9641 return false;
9642 }
9643
9644
9645 /* Output a QImode instruction sequence for
9646
9647 XOP[0] = XOP[0] <CODE> XOP[2]
9648
9649 where XOP[0] is a register, and the possible operands and CODEs
9650 are according to `avr_op8_ZN_operator' from above. Return "".
9651
9652 If PLEN == NULL, then output the instructions.
9653 If PLEN != NULL, then set *PLEN to the length of the sequence in words. */
9654
9655 const char *
9656 avr_out_op8_set_ZN (rtx_code code, rtx *xop, int *plen)
9657 {
9658 const bool reg2_p = REG_P (xop[2]);
9659 const int ival = CONST_INT_P (xop[2]) ? (int) INTVAL (xop[2]) : 0;
9660
9661 gcc_assert (op8_ZN_operator (gen_rtx_fmt_ee (code, QImode, xop[0], xop[2]),
9662 QImode));
9663 if (plen)
9664 *plen = 0;
9665
9666 const char *tpl = nullptr;
9667 int times = 1;
9668
9669 if (code == ASHIFT)
9670 tpl = "lsl %0", times = ival;
9671 else if (code == LSHIFTRT)
9672 tpl = "lsr %0", times = ival;
9673 else if (code == ASHIFTRT)
9674 tpl = "asr %0", times = ival;
9675 else if (code == MINUS)
9676 tpl = "sub %0,%2";
9677 else if (code == XOR)
9678 tpl = "eor %0,%2";
9679 else if (code == AND)
9680 tpl = reg2_p ? "and %0,%2" : "andi %0,lo8(%2)";
9681 else if (code == IOR)
9682 tpl = reg2_p ? "or %0,%2" : "ori %0,lo8(%2)";
9683 else if (code == PLUS)
9684 {
9685 if (ival
9686 && ! test_hard_reg_class (LD_REGS, xop[0]))
9687 {
9688 tpl = ival > 0 ? "inc %0" : "dec %0";
9689 times = std::abs (ival);
9690 }
9691 else
9692 tpl = reg2_p ? "add %0,%2" : "subi %0,lo8(%n2)";
9693 }
9694 else
9695 gcc_unreachable ();
9696
9697 for (int i = 0; i < times; ++i)
9698 avr_asm_len (tpl, xop, plen, 1);
9699
9700 return "";
9701 }
9702
9703
9704 /* Used in the "length" attribute of insn "*op8.for.cczn.<code>". */
9705
9706 int
9707 avr_len_op8_set_ZN (rtx_code code, rtx *xop)
9708 {
9709 int len;
9710 (void) avr_out_op8_set_ZN (code, xop, &len);
9711
9712 return len;
9713 }
9714
9715
9716 /* Output bit operation (IOR, AND, XOR) with register XOP[0] and compile
9717 time constant XOP[2]:
9718
9719 XOP[0] = XOP[0] <op> XOP[2]
9720
9721 and return "". If PLEN == NULL, print assembler instructions to perform the
9722 operation; otherwise, set *PLEN to the length of the instruction sequence
9723 (in words) printed with PLEN == NULL. XOP[3] is either an 8-bit clobber
9724 register or SCRATCH if no clobber register is needed for the operation.
9725 XINSN is an INSN_P or a pattern of an insn. */
9726
9727 const char *
9728 avr_out_bitop (rtx xinsn, rtx *xop, int *plen)
9729 {
9730 rtx_insn *insn = xinsn && INSN_P (xinsn)
9731 ? as_a <rtx_insn *> (xinsn)
9732 : nullptr;
9733 rtx xpattern = insn ? single_set (insn) : xinsn;
9734
9735 /* CODE and MODE of the operation. */
9736 rtx_code code = GET_CODE (SET_SRC (xpattern));
9737 machine_mode mode = GET_MODE (xop[0]);
9738
9739 /* Number of bytes to operate on. */
9740 int n_bytes = GET_MODE_SIZE (mode);
9741
9742 /* Value of T-flag (0 or 1) or -1 if unknow. */
9743 int set_t = -1;
9744
9745 /* Value (0..0xff) held in clobber register op[3] or -1 if unknown. */
9746 int clobber_val = -1;
9747
9748 /* op[0]: 8-bit destination register
9749 op[1]: 8-bit const int
9750 op[2]: 8-bit clobber register, SCRATCH or NULL_RTX.
9751 op[3]: 8-bit register containing 0xff or NULL_RTX */
9752 rtx op[4];
9753
9754 op[2] = QImode == mode ? NULL_RTX : xop[3];
9755 op[3] = NULL_RTX;
9756
9757 if (plen)
9758 *plen = 0;
9759
9760 for (int i = 0; i < n_bytes; i++)
9761 {
9762 /* We operate byte-wise on the destination. */
9763 rtx reg8 = avr_byte (xop[0], i);
9764
9765 if (optimize
9766 && avr_result_regno_unused_p (insn, REGNO (reg8)))
9767 continue;
9768
9769 /* 8-bit value to operate with this byte. */
9770 unsigned int val8 = avr_uint8 (xop[2], i);
9771
9772 /* Number of bits set in the current byte of the constant. */
9773 int pop8 = popcount_hwi (val8);
9774
9775 /* Registers R16..R31 can operate with immediate. */
9776 bool ld_reg_p = test_hard_reg_class (LD_REGS, reg8);
9777
9778 op[0] = reg8;
9779 op[1] = GEN_INT (val8);
9780
9781 switch (code)
9782 {
9783 case IOR:
9784
9785 if (pop8 == 0)
9786 continue;
9787 else if (ld_reg_p)
9788 avr_asm_len ("ori %0,%1", op, plen, 1);
9789 else if (pop8 == 1)
9790 {
9791 if (set_t != 1)
9792 avr_asm_len ("set", op, plen, 1);
9793 set_t = 1;
9794
9795 op[1] = GEN_INT (exact_log2 (val8));
9796 avr_asm_len ("bld %0,%1", op, plen, 1);
9797 }
9798 else if (pop8 == 8)
9799 {
9800 if (op[3] != NULL_RTX)
9801 avr_asm_len ("mov %0,%3", op, plen, 1);
9802 else
9803 avr_asm_len ("clr %0" CR_TAB
9804 "dec %0", op, plen, 2);
9805
9806 op[3] = op[0];
9807 }
9808 else
9809 {
9810 if (clobber_val != (int) val8)
9811 avr_asm_len ("ldi %2,%1", op, plen, 1);
9812 clobber_val = (int) val8;
9813
9814 avr_asm_len ("or %0,%2", op, plen, 1);
9815 }
9816
9817 continue; /* IOR */
9818
9819 case AND:
9820
9821 if (pop8 == 8)
9822 continue;
9823 else if (pop8 == 0)
9824 avr_asm_len ("clr %0", op, plen, 1);
9825 else if (ld_reg_p)
9826 avr_asm_len ("andi %0,%1", op, plen, 1);
9827 else if (pop8 == 7)
9828 {
9829 if (set_t != 0)
9830 avr_asm_len ("clt", op, plen, 1);
9831 set_t = 0;
9832
9833 op[1] = GEN_INT (exact_log2 (GET_MODE_MASK (QImode) & ~val8));
9834 avr_asm_len ("bld %0,%1", op, plen, 1);
9835 }
9836 else
9837 {
9838 if (clobber_val != (int) val8)
9839 avr_asm_len ("ldi %2,%1", op, plen, 1);
9840 clobber_val = (int) val8;
9841
9842 avr_asm_len ("and %0,%2", op, plen, 1);
9843 }
9844
9845 continue; /* AND */
9846
9847 case XOR:
9848
9849 if (pop8 == 0)
9850 continue;
9851 else if (pop8 == 8)
9852 avr_asm_len ("com %0", op, plen, 1);
9853 else if (ld_reg_p && val8 == (1 << 7))
9854 avr_asm_len ("subi %0,%1", op, plen, 1);
9855 else
9856 {
9857 if (clobber_val != (int) val8)
9858 avr_asm_len ("ldi %2,%1", op, plen, 1);
9859 clobber_val = (int) val8;
9860
9861 avr_asm_len ("eor %0,%2", op, plen, 1);
9862 }
9863
9864 continue; /* XOR */
9865
9866 default:
9867 /* Unknown rtx_code */
9868 gcc_unreachable ();
9869 }
9870 } /* for all sub-bytes */
9871
9872 return "";
9873 }
9874
9875
9876 /* Emit code for
9877
9878 XOP[0] = XOP[0] <xior> (XOP[1] <shift> BITOFF)
9879
9880 where XOP[0] and XOP[1] are hard registers with integer mode,
9881 <xior> is XOR or IOR, and <shift> is LSHIFTRT or ASHIFT with a
9882 non-negative shift offset BITOFF. This function emits the operation
9883 in terms of byte-wise operations in QImode. */
9884
9885 void
9886 avr_emit_xior_with_shift (rtx_insn *insn, rtx *xop, int bitoff)
9887 {
9888 rtx src = SET_SRC (single_set (insn));
9889 rtx_code xior = GET_CODE (src);
9890 gcc_assert (xior == XOR || xior == IOR);
9891 gcc_assert (bitoff % 8 == 0);
9892
9893 // Work out the shift offset in bytes; negative for shift right.
9894 rtx_code shift = GET_CODE (XEXP (src, 0));
9895 int byteoff = 0?0
9896 : shift == ASHIFT ? bitoff / 8
9897 : shift == LSHIFTRT ? -bitoff / 8
9898 // Not a shift but something like REG or ZERO_EXTEND:
9899 // Use xop[1] as is, without shifting it.
9900 : 0;
9901
9902 // Work out which hard REGNOs belong to the operands.
9903 int size0 = GET_MODE_SIZE (GET_MODE (xop[0]));
9904 int size1 = GET_MODE_SIZE (GET_MODE (xop[1]));
9905 int regno0_lo = REGNO (xop[0]), regno0_hi = regno0_lo + size0 - 1;
9906 int regno1_lo = REGNO (xop[1]), regno1_hi = regno1_lo + size1 - 1;
9907 int regoff = regno0_lo - regno1_lo + byteoff;
9908
9909 // The order of insns matters in the rare case when xop[1] overlaps xop[0].
9910 int beg = regoff > 0 ? regno1_hi : regno1_lo;
9911 int end = regoff > 0 ? regno1_lo : regno1_hi;
9912 int inc = regoff > 0 ? -1 : 1;
9913
9914 rtx (*gen)(rtx,rtx,rtx) = xior == XOR ? gen_xorqi3 : gen_iorqi3;
9915
9916 for (int i = beg; i != end + inc; i += inc)
9917 {
9918 if (IN_RANGE (i + regoff, regno0_lo, regno0_hi))
9919 {
9920 rtx reg0 = all_regs_rtx[i + regoff];
9921 rtx reg1 = all_regs_rtx[i];
9922 emit_insn (gen (reg0, reg0, reg1));
9923 }
9924 }
9925 }
9926
9927
9928 /* Output sign extension from XOP[1] to XOP[0] and return "".
9929 If PLEN == NULL, print assembler instructions to perform the operation;
9930 otherwise, set *PLEN to the length of the instruction sequence (in words)
9931 as printed with PLEN == NULL. */
9932
9933 const char *
9934 avr_out_sign_extend (rtx_insn *insn, rtx *xop, int *plen)
9935 {
9936 // Size in bytes of source resp. destination operand.
9937 unsigned n_src = GET_MODE_SIZE (GET_MODE (xop[1]));
9938 unsigned n_dest = GET_MODE_SIZE (GET_MODE (xop[0]));
9939 rtx r_msb = all_regs_rtx[REGNO (xop[1]) + n_src - 1];
9940
9941 if (plen)
9942 *plen = 0;
9943
9944 // Copy destination to source
9945
9946 if (REGNO (xop[0]) != REGNO (xop[1]))
9947 {
9948 gcc_assert (n_src <= 2);
9949
9950 if (n_src == 2)
9951 avr_asm_len (AVR_HAVE_MOVW
9952 ? "movw %0,%1"
9953 : "mov %B0,%B1", xop, plen, 1);
9954 if (n_src == 1 || !AVR_HAVE_MOVW)
9955 avr_asm_len ("mov %A0,%A1", xop, plen, 1);
9956 }
9957
9958 // Set Carry to the sign bit MSB.7...
9959
9960 if (REGNO (xop[0]) == REGNO (xop[1])
9961 || !reg_unused_after (insn, r_msb))
9962 {
9963 avr_asm_len ("mov __tmp_reg__,%0", &r_msb, plen, 1);
9964 r_msb = tmp_reg_rtx;
9965 }
9966
9967 avr_asm_len ("lsl %0", &r_msb, plen, 1);
9968
9969 // ...and propagate it to all the new sign bits
9970
9971 for (unsigned n = n_src; n < n_dest; n++)
9972 avr_asm_len ("sbc %0,%0", &all_regs_rtx[REGNO (xop[0]) + n], plen, 1);
9973
9974 return "";
9975 }
9976
9977
9978 /* PLEN == NULL: Output code to add CONST_INT OP[0] to SP.
9979 PLEN != NULL: Set *PLEN to the length of that sequence.
9980 Return "". */
9981
9982 const char *
9983 avr_out_addto_sp (rtx *op, int *plen)
9984 {
9985 int pc_len = AVR_2_BYTE_PC ? 2 : 3;
9986 int addend = INTVAL (op[0]);
9987
9988 if (plen)
9989 *plen = 0;
9990
9991 if (addend < 0)
9992 {
9993 if (flag_verbose_asm || flag_print_asm_name)
9994 avr_asm_len (ASM_COMMENT_START "SP -= %n0", op, plen, 0);
9995
9996 while (addend <= -pc_len)
9997 {
9998 addend += pc_len;
9999 avr_asm_len ("rcall .", op, plen, 1);
10000 }
10001
10002 while (addend++ < 0)
10003 avr_asm_len ("push __tmp_reg__", op, plen, 1);
10004 }
10005 else if (addend > 0)
10006 {
10007 if (flag_verbose_asm || flag_print_asm_name)
10008 avr_asm_len (ASM_COMMENT_START "SP += %0", op, plen, 0);
10009
10010 while (addend-- > 0)
10011 avr_asm_len ("pop __tmp_reg__", op, plen, 1);
10012 }
10013
10014 return "";
10015 }
10016
10017
10018 /* Output instructions to insert an inverted bit into OP[0]: $0.$1 = ~$2.$3.
10019 If PLEN = NULL then output the respective instruction sequence which
10020 is a combination of BST / BLD and some instruction(s) to invert the bit.
10021 If PLEN != NULL then store the length of the sequence (in words) in *PLEN.
10022 Return "". */
10023
10024 const char *
10025 avr_out_insert_notbit (rtx_insn *insn, rtx op[], int *plen)
10026 {
10027 if (INTVAL (op[1]) == 7
10028 && test_hard_reg_class (LD_REGS, op[0]))
10029 {
10030 /* If the inserted bit number is 7 and we have a d-reg, then invert
10031 the bit after the insertion by means of SUBI *,0x80. */
10032
10033 if (INTVAL (op[3]) == 7
10034 && REGNO (op[0]) == REGNO (op[2]))
10035 {
10036 avr_asm_len ("subi %0,0x80", op, plen, -1);
10037 }
10038 else
10039 {
10040 avr_asm_len ("bst %2,%3" CR_TAB
10041 "bld %0,%1" CR_TAB
10042 "subi %0,0x80", op, plen, -3);
10043 }
10044 }
10045 else if (test_hard_reg_class (LD_REGS, op[0])
10046 && (INTVAL (op[1]) != INTVAL (op[3])
10047 || !reg_overlap_mentioned_p (op[0], op[2])))
10048 {
10049 /* If the destination bit is in a d-reg we can jump depending
10050 on the source bit and use ANDI / ORI. This just applies if we
10051 have not an early-clobber situation with the bit. */
10052
10053 avr_asm_len ("andi %0,~(1<<%1)" CR_TAB
10054 "sbrs %2,%3" CR_TAB
10055 "ori %0,1<<%1", op, plen, -3);
10056 }
10057 else
10058 {
10059 /* Otherwise, invert the bit by means of COM before we store it with
10060 BST and then undo the COM if needed. */
10061
10062 avr_asm_len ("com %2" CR_TAB
10063 "bst %2,%3", op, plen, -2);
10064
10065 if (!reg_unused_after (insn, op[2])
10066 // A simple 'reg_unused_after' is not enough because that function
10067 // assumes that the destination register is overwritten completely
10068 // and hence is in order for our purpose. This is not the case
10069 // with BLD which just changes one bit of the destination.
10070 || reg_overlap_mentioned_p (op[0], op[2]))
10071 {
10072 /* Undo the COM from above. */
10073 avr_asm_len ("com %2", op, plen, 1);
10074 }
10075
10076 avr_asm_len ("bld %0,%1", op, plen, 1);
10077 }
10078
10079 return "";
10080 }
10081
10082
10083 /* Output instructions for XOP[0] = (XOP[1] <Shift> XOP[2]) & XOP[3] where
10084 - XOP[0] and XOP[1] have the same mode which is one of: QI, HI, PSI, SI.
10085 - XOP[3] is an exact const_int power of 2.
10086 - XOP[2] and XOP[3] are const_int.
10087 - <Shift> is any of: ASHIFT, LSHIFTRT, ASHIFTRT.
10088 - The result depends on XOP[1].
10089 or XOP[0] = XOP[1] & XOP[2] where
10090 - XOP[0] and XOP[1] have the same mode which is one of: QI, HI, PSI, SI.
10091 - XOP[2] is an exact const_int power of 2.
10092 Returns "".
10093 PLEN != 0: Set *PLEN to the code length in words. Don't output anything.
10094 PLEN == 0: Output instructions. */
10095
10096 const char*
10097 avr_out_insv (rtx_insn *insn, rtx xop[], int *plen)
10098 {
10099 machine_mode mode = GET_MODE (xop[0]);
10100 int n_bytes = GET_MODE_SIZE (mode);
10101 rtx xsrc = SET_SRC (single_set (insn));
10102
10103 gcc_assert (AND == GET_CODE (xsrc));
10104
10105 rtx xop2 = xop[2];
10106 rtx xop3 = xop[3];
10107
10108 if (REG_P (XEXP (xsrc, 0)))
10109 {
10110 // This function can also handle AND with an exact power of 2,
10111 // which can be regarded as a XOP[1] shift with offset 0.
10112 rtx xshift = gen_rtx_ASHIFT (mode, xop[1], const0_rtx);
10113 xsrc = gen_rtx_AND (mode, xshift, xop[2]);
10114 xop3 = xop[2];
10115 xop2 = const0_rtx;
10116 }
10117
10118 // Any of ASHIFT, LSHIFTRT, ASHIFTRT.
10119 rtx_code code = GET_CODE (XEXP (xsrc, 0));
10120 int shift = code == ASHIFT ? INTVAL (xop2) : -INTVAL (xop2);
10121
10122 // Determines the position of the output bit.
10123 unsigned mask = GET_MODE_MASK (mode) & INTVAL (xop3);
10124
10125 // Position of the output / input bit, respectively.
10126 int obit = exact_log2 (mask);
10127 int ibit = obit - shift;
10128
10129 gcc_assert (IN_RANGE (obit, 0, GET_MODE_BITSIZE (mode) - 1));
10130 gcc_assert (IN_RANGE (ibit, 0, GET_MODE_BITSIZE (mode) - 1));
10131
10132 // In the remainder, use the sub-bytes that hold the bits.
10133 rtx op[4] =
10134 {
10135 // Output
10136 avr_byte (xop[0], obit / 8), GEN_INT (obit & 7),
10137 // Input
10138 avr_byte (xop[1], ibit / 8), GEN_INT (ibit & 7)
10139 };
10140 obit &= 7;
10141 ibit &= 7;
10142
10143 // The length of the default sequence at the end of this function.
10144 // We only emit anything other than the default when we find a sequence
10145 // that is strictly shorter than the default sequence; which is:
10146 // BST + <CLR-result-bytes> + BLD.
10147 const int len0 = 2 + n_bytes - (n_bytes == 4 && AVR_HAVE_MOVW);
10148
10149 // Finding something shorter than the default sequence implies that there
10150 // must be at most 2 instructions that deal with the bytes containing the
10151 // relevant bits. In addition, we need N_BYTES - 1 instructions to clear
10152 // the remaining result bytes.
10153
10154 const int n_clr = n_bytes - 1;
10155 bool clr_p = false;
10156 bool andi_p = false;
10157
10158 if (plen)
10159 *plen = 0;
10160
10161 if (REGNO (op[0]) == REGNO (op[2])
10162 // Output reg allows ANDI.
10163 && test_hard_reg_class (LD_REGS, op[0]))
10164 {
10165 if (1 + n_clr < len0
10166 // Same byte and bit: A single ANDI will do.
10167 && obit == ibit)
10168 {
10169 clr_p = andi_p = true;
10170 }
10171 else if (2 + n_clr < len0
10172 // |obit - ibit| = 4: SWAP + ANDI will do.
10173 && (obit == ibit + 4 || obit == ibit - 4))
10174 {
10175 avr_asm_len ("swap %0", op, plen, 1);
10176 clr_p = andi_p = true;
10177 }
10178 else if (2 + n_clr < len0
10179 // LSL + ANDI will do.
10180 && obit == ibit + 1)
10181 {
10182 avr_asm_len ("lsl %0", op, plen, 1);
10183 clr_p = andi_p = true;
10184 }
10185 else if (2 + n_clr < len0
10186 // LSR + ANDI will do.
10187 && obit == ibit - 1)
10188 {
10189 avr_asm_len ("lsr %0", op, plen, 1);
10190 clr_p = andi_p = true;
10191 }
10192 }
10193
10194 if (REGNO (op[0]) != REGNO (op[2])
10195 && obit == ibit)
10196 {
10197 if (2 + n_clr < len0
10198 // Same bit but different byte: MOV + ANDI will do.
10199 && test_hard_reg_class (LD_REGS, op[0]))
10200 {
10201 avr_asm_len ("mov %0,%2", op, plen, 1);
10202 clr_p = andi_p = true;
10203 }
10204 else if (2 + n_clr < len0
10205 // Same bit but different byte: We can use ANDI + MOV,
10206 // but only if the input byte is LD_REGS and unused after.
10207 && test_hard_reg_class (LD_REGS, op[2])
10208 && reg_unused_after (insn, op[2]))
10209 {
10210 avr_asm_len ("andi %2,1<<%3" CR_TAB
10211 "mov %0,%2", op, plen, 2);
10212 clr_p = true;
10213 }
10214 }
10215
10216 // Output remaining instructions of the shorter sequence.
10217
10218 if (andi_p)
10219 avr_asm_len ("andi %0,1<<%1", op, plen, 1);
10220
10221 if (clr_p)
10222 {
10223 for (int b = 0; b < n_bytes; ++b)
10224 {
10225 rtx byte = avr_byte (xop[0], b);
10226 if (REGNO (byte) != REGNO (op[0]))
10227 avr_asm_len ("clr %0", &byte, plen, 1);
10228 }
10229
10230 // CLR_P means we found a shorter sequence, so we are done now.
10231 return "";
10232 }
10233
10234 // No shorter sequence found, just emit BST, CLR*, BLD sequence.
10235
10236 avr_asm_len ("bst %2,%3", op, plen, -1);
10237
10238 if (n_bytes == 4 && AVR_HAVE_MOVW)
10239 avr_asm_len ("clr %A0" CR_TAB
10240 "clr %B0" CR_TAB
10241 "movw %C0,%A0", xop, plen, 3);
10242 else
10243 for (int b = 0; b < n_bytes; ++b)
10244 {
10245 rtx byte = avr_byte (xop[0], b);
10246 avr_asm_len ("clr %0", &byte, plen, 1);
10247 }
10248
10249 return avr_asm_len ("bld %0,%1", op, plen, 1);
10250 }
10251
10252
10253 /* Output instructions to extract a bit to 8-bit register OP[0].
10254 The input OP[1] is a register or an 8-bit MEM in the lower I/O range.
10255 OP[2] is the const_int bit position. Return "".
10256
10257 PLEN != 0: Set *PLEN to the code length in words. Don't output anything.
10258 PLEN == 0: Output instructions. */
10259
10260 const char *
10261 avr_out_extr (rtx_insn *insn, rtx op[], int *plen)
10262 {
10263 rtx xop[] = { op[0], op[1], op[2] };
10264 rtx dest = xop[0];
10265 rtx src = xop[1];
10266 int bit = INTVAL (xop[2]);
10267
10268 if (GET_MODE (src) != QImode)
10269 {
10270 src = xop[1] = avr_byte (src, bit / 8);
10271 bit %= 8;
10272 xop[2] = GEN_INT (bit);
10273 }
10274
10275 if (MEM_P (src))
10276 {
10277 xop[1] = XEXP (src, 0); // address
10278 gcc_assert (low_io_address_operand (xop[1], Pmode));
10279
10280 return avr_asm_len ("clr %0" CR_TAB
10281 "sbic %i1,%2" CR_TAB
10282 "inc %0", xop, plen, -3);
10283 }
10284
10285 gcc_assert (REG_P (src));
10286
10287 bool ld_dest_p = test_hard_reg_class (LD_REGS, dest);
10288 bool ld_src_p = test_hard_reg_class (LD_REGS, src);
10289
10290 if (ld_dest_p
10291 && REGNO (src) == REGNO (dest))
10292 {
10293 if (bit == 0)
10294 return avr_asm_len ("andi %0,1", xop, plen, -1);
10295 if (bit == 1)
10296 return avr_asm_len ("lsr %0" CR_TAB
10297 "andi %0,1", xop, plen, -2);
10298 if (bit == 4)
10299 return avr_asm_len ("swap %0" CR_TAB
10300 "andi %0,1", xop, plen, -2);
10301 }
10302
10303 if (bit == 0
10304 && REGNO (src) != REGNO (dest))
10305 {
10306 if (ld_dest_p)
10307 return avr_asm_len ("mov %0,%1" CR_TAB
10308 "andi %0,1", xop, plen, -2);
10309 if (ld_src_p
10310 && reg_unused_after (insn, src))
10311 return avr_asm_len ("andi %1,1" CR_TAB
10312 "mov %0,%1", xop, plen, -2);
10313 }
10314
10315 return avr_asm_len ("bst %1,%2" CR_TAB
10316 "clr %0" CR_TAB
10317 "bld %0,0", xop, plen, -3);
10318 }
10319
10320
10321 /* Output instructions to extract a negated bit to 8-bit register OP[0].
10322 The input OP[1] is an 8-bit register or MEM in the lower I/O range.
10323 OP[2] is the const_int bit position. Return "".
10324
10325 PLEN != 0: Set *PLEN to the code length in words. Don't output anything.
10326 PLEN == 0: Output instructions. */
10327
10328 const char *
10329 avr_out_extr_not (rtx_insn * /* insn */, rtx op[], int *plen)
10330 {
10331 rtx xop[] = { op[0], op[1], op[2] };
10332 rtx dest = xop[0];
10333 rtx src = xop[1];
10334 int bit = INTVAL (xop[2]);
10335
10336 if (MEM_P (src))
10337 {
10338 xop[1] = XEXP (src, 0); // address
10339 gcc_assert (low_io_address_operand (xop[1], Pmode));
10340
10341 return avr_asm_len ("clr %0" CR_TAB
10342 "sbis %i1,%2" CR_TAB
10343 "inc %0", xop, plen, -3);
10344 }
10345
10346 gcc_assert (REG_P (src));
10347
10348 bool ld_src_p = test_hard_reg_class (LD_REGS, src);
10349
10350 if (ld_src_p
10351 && REGNO (src) == REGNO (dest))
10352 {
10353 if (bit == 0)
10354 return avr_asm_len ("inc %0" CR_TAB
10355 "andi %0,1", xop, plen, -2);
10356 if (bit == 1)
10357 return avr_asm_len ("lsr %0" CR_TAB
10358 "inc %0" CR_TAB
10359 "andi %0,1", xop, plen, -3);
10360 if (bit == 4)
10361 return avr_asm_len ("swap %0" CR_TAB
10362 "inc %0" CR_TAB
10363 "andi %0,1", xop, plen, -3);
10364 }
10365
10366 if (bit == 7
10367 && ld_src_p)
10368 return avr_asm_len ("cpi %1,0x80" CR_TAB
10369 "sbc %0,%0" CR_TAB
10370 "neg %0", xop, plen, -3);
10371
10372 if (REGNO (src) != REGNO (dest))
10373 return avr_asm_len ("clr %0" CR_TAB
10374 "sbrs %1,%2" CR_TAB
10375 "inc %0", xop, plen, -3);
10376
10377 return avr_asm_len ("clr __tmp_reg__" CR_TAB
10378 "sbrs %1,%2" CR_TAB
10379 "inc __tmp_reg__" CR_TAB
10380 "mov %0,__tmp_reg__", xop, plen, -4);
10381 }
10382
10383
10384 /* Outputs instructions needed for fixed point type conversion.
10385 This includes converting between any fixed point type, as well
10386 as converting to any integer type. Conversion between integer
10387 types is not supported.
10388
10389 Converting signed fractional types requires a bit shift if converting
10390 to or from any unsigned fractional type because the decimal place is
10391 shifted by 1 bit. When the destination is a signed fractional, the sign
10392 is stored in either the carry or T bit. */
10393
10394 const char *
10395 avr_out_fract (rtx_insn *insn, rtx operands[], bool intsigned, int *plen)
10396 {
10397 rtx xop[6];
10398 rtx_code shift = UNKNOWN;
10399 bool sign_in_carry = false;
10400 bool msb_in_carry = false;
10401 bool lsb_in_tmp_reg = false;
10402 bool lsb_in_carry = false;
10403 bool frac_rounded = false;
10404 const char *code_ashift = "lsl %0";
10405
10406
10407 #define MAY_CLOBBER(RR) \
10408 /* Shorthand used below. */ \
10409 ((sign_bytes \
10410 && IN_RANGE (RR, dest.regno_msb - sign_bytes + 1, dest.regno_msb)) \
10411 || (offset && IN_RANGE (RR, dest.regno, dest.regno_msb)) \
10412 || (reg_unused_after (insn, all_regs_rtx[RR]) \
10413 && !IN_RANGE (RR, dest.regno, dest.regno_msb)))
10414
10415 struct
10416 {
10417 /* bytes : Length of operand in bytes.
10418 ibyte : Length of integral part in bytes.
10419 fbyte, fbit : Length of fractional part in bytes, bits. */
10420
10421 bool sbit;
10422 unsigned fbit, bytes, ibyte, fbyte;
10423 unsigned regno, regno_msb;
10424 } dest, src, *val[2] = { &dest, &src };
10425
10426 if (plen)
10427 *plen = 0;
10428
10429 /* Step 0: Determine information on source and destination operand we
10430 ====== will need in the remainder. */
10431
10432 for (size_t i = 0; i < ARRAY_SIZE (val); i++)
10433 {
10434 machine_mode mode;
10435
10436 xop[i] = operands[i];
10437
10438 mode = GET_MODE (xop[i]);
10439
10440 val[i]->bytes = GET_MODE_SIZE (mode);
10441 val[i]->regno = REGNO (xop[i]);
10442 val[i]->regno_msb = REGNO (xop[i]) + val[i]->bytes - 1;
10443
10444 if (SCALAR_INT_MODE_P (mode))
10445 {
10446 val[i]->sbit = intsigned;
10447 val[i]->fbit = 0;
10448 }
10449 else if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
10450 {
10451 val[i]->sbit = SIGNED_SCALAR_FIXED_POINT_MODE_P (mode);
10452 val[i]->fbit = GET_MODE_FBIT (mode);
10453 }
10454 else
10455 fatal_insn ("unsupported fixed-point conversion", insn);
10456
10457 val[i]->fbyte = (1 + val[i]->fbit) / BITS_PER_UNIT;
10458 val[i]->ibyte = val[i]->bytes - val[i]->fbyte;
10459 }
10460
10461 // Byte offset of the decimal point taking into account different place
10462 // of the decimal point in input and output and different register numbers
10463 // of input and output.
10464 int offset = dest.regno - src.regno + dest.fbyte - src.fbyte;
10465
10466 // Number of destination bytes that will come from sign / zero extension.
10467 int sign_bytes = (dest.ibyte - src.ibyte) * (dest.ibyte > src.ibyte);
10468
10469 // Number of bytes at the low end to be filled with zeros.
10470 int zero_bytes = (dest.fbyte - src.fbyte) * (dest.fbyte > src.fbyte);
10471
10472 // Do we have a 16-Bit register that is cleared?
10473 rtx clrw = NULL_RTX;
10474
10475 bool sign_extend = src.sbit && sign_bytes;
10476
10477 if (dest.fbit % 8 == 0 && src.fbit % 8 == 7)
10478 shift = ASHIFT;
10479 else if (dest.fbit % 8 == 7 && src.fbit % 8 == 0)
10480 shift = ASHIFTRT;
10481 else if (dest.fbit % 8 == src.fbit % 8)
10482 shift = UNKNOWN;
10483 else
10484 gcc_unreachable ();
10485
10486 /* If we need to round the fraction part, we might need to save/round it
10487 before clobbering any of it in Step 1. Also, we might want to do
10488 the rounding now to make use of LD_REGS. */
10489 if (SCALAR_INT_MODE_P (GET_MODE (xop[0]))
10490 && SCALAR_ACCUM_MODE_P (GET_MODE (xop[1]))
10491 && !TARGET_FRACT_CONV_TRUNC)
10492 {
10493 bool overlap
10494 = (src.regno <=
10495 (offset ? dest.regno_msb - sign_bytes : dest.regno + zero_bytes - 1)
10496 && dest.regno - offset -1 >= dest.regno);
10497 unsigned s0 = dest.regno - offset -1;
10498 bool use_src = true;
10499 unsigned sn;
10500 unsigned copied_msb = src.regno_msb;
10501 bool have_carry = false;
10502
10503 if (src.ibyte > dest.ibyte)
10504 copied_msb -= src.ibyte - dest.ibyte;
10505
10506 for (sn = s0; sn <= copied_msb; sn++)
10507 if (!IN_RANGE (sn, dest.regno, dest.regno_msb)
10508 && !reg_unused_after (insn, all_regs_rtx[sn]))
10509 use_src = false;
10510 if (use_src && TEST_HARD_REG_BIT (reg_class_contents[LD_REGS], s0))
10511 {
10512 avr_asm_len ("tst %0" CR_TAB "brpl 0f",
10513 &all_regs_rtx[src.regno_msb], plen, 2);
10514 sn = src.regno;
10515 if (sn < s0)
10516 {
10517 if (TEST_HARD_REG_BIT (reg_class_contents[LD_REGS], sn))
10518 avr_asm_len ("cpi %0,1", &all_regs_rtx[sn], plen, 1);
10519 else
10520 avr_asm_len ("sec" CR_TAB
10521 "cpc %0,__zero_reg__",
10522 &all_regs_rtx[sn], plen, 2);
10523 have_carry = true;
10524 }
10525 while (++sn < s0)
10526 avr_asm_len ("cpc %0,__zero_reg__", &all_regs_rtx[sn], plen, 1);
10527
10528 avr_asm_len (have_carry ? "sbci %0,128" : "subi %0,129",
10529 &all_regs_rtx[s0], plen, 1);
10530 for (sn = src.regno + src.fbyte; sn <= copied_msb; sn++)
10531 avr_asm_len ("sbci %0,255", &all_regs_rtx[sn], plen, 1);
10532 avr_asm_len ("\n0:", nullptr, plen, 0);
10533 frac_rounded = true;
10534 }
10535 else if (use_src && overlap)
10536 {
10537 avr_asm_len ("clr __tmp_reg__" CR_TAB
10538 "sbrc %1,0" CR_TAB
10539 "dec __tmp_reg__", xop, plen, 3);
10540 sn = src.regno;
10541 if (sn < s0)
10542 {
10543 avr_asm_len ("add %0,__tmp_reg__", &all_regs_rtx[sn], plen, 1);
10544 have_carry = true;
10545 }
10546
10547 while (++sn < s0)
10548 avr_asm_len ("adc %0,__tmp_reg__", &all_regs_rtx[sn], plen, 1);
10549
10550 if (have_carry)
10551 avr_asm_len ("clt" CR_TAB
10552 "bld __tmp_reg__,7" CR_TAB
10553 "adc %0,__tmp_reg__",
10554 &all_regs_rtx[s0], plen, 3);
10555 else
10556 avr_asm_len ("lsr __tmp_reg" CR_TAB
10557 "add %0,__tmp_reg__",
10558 &all_regs_rtx[s0], plen, 2);
10559 for (sn = src.regno + src.fbyte; sn <= copied_msb; sn++)
10560 avr_asm_len ("adc %0,__zero_reg__", &all_regs_rtx[sn], plen, 1);
10561 frac_rounded = true;
10562 }
10563 else if (overlap)
10564 {
10565 bool use_src
10566 = (TEST_HARD_REG_BIT (reg_class_contents[LD_REGS], s0)
10567 && (IN_RANGE (s0, dest.regno, dest.regno_msb)
10568 || reg_unused_after (insn, all_regs_rtx[s0])));
10569 xop[2] = all_regs_rtx[s0];
10570 unsigned sn = src.regno;
10571 if (!use_src || sn == s0)
10572 avr_asm_len ("mov __tmp_reg__,%2", xop, plen, 1);
10573 /* We need to consider to-be-discarded bits
10574 if the value is negative. */
10575 if (sn < s0)
10576 {
10577 avr_asm_len ("tst %0" CR_TAB
10578 "brpl 0f",
10579 &all_regs_rtx[src.regno_msb], plen, 2);
10580 /* Test to-be-discarded bytes for any nozero bits.
10581 ??? Could use OR or SBIW to test two registers at once. */
10582 if (sn < s0)
10583 avr_asm_len ("cp %0,__zero_reg__", &all_regs_rtx[sn], plen, 1);
10584
10585 while (++sn < s0)
10586 avr_asm_len ("cpc %0,__zero_reg__", &all_regs_rtx[sn], plen, 1);
10587 /* Set bit 0 in __tmp_reg__ if any of the lower bits was set. */
10588 if (use_src)
10589 avr_asm_len ("breq 0f" CR_TAB
10590 "ori %2,1"
10591 "\n0:\t" "mov __tmp_reg__,%2",
10592 xop, plen, 3);
10593 else
10594 avr_asm_len ("breq 0f" CR_TAB
10595 "set" CR_TAB
10596 "bld __tmp_reg__,0\n0:",
10597 xop, plen, 3);
10598 }
10599 lsb_in_tmp_reg = true;
10600 }
10601 }
10602
10603 /* Step 1: Clear bytes at the low end and copy payload bits from source
10604 ====== to destination. */
10605
10606 int step = offset < 0 ? 1 : -1;
10607 unsigned d0 = offset < 0 ? dest.regno : dest.regno_msb;
10608
10609 // We cleared at least that number of registers.
10610 int clr_n = 0;
10611
10612 for (; d0 >= dest.regno && d0 <= dest.regno_msb; d0 += step)
10613 {
10614 // Next regno of destination is needed for MOVW
10615 unsigned d1 = d0 + step;
10616
10617 // Current and next regno of source
10618 signed s0 = d0 - offset;
10619 signed s1 = s0 + step;
10620
10621 // Must current resp. next regno be CLRed? This applies to the low
10622 // bytes of the destination that have no associated source bytes.
10623 bool clr0 = s0 < (signed) src.regno;
10624 bool clr1 = s1 < (signed) src.regno && d1 >= dest.regno;
10625
10626 // First gather what code to emit (if any) and additional step to
10627 // apply if a MOVW is in use. xop[2] is destination rtx and xop[3]
10628 // is the source rtx for the current loop iteration.
10629 const char *code = nullptr;
10630 int stepw = 0;
10631
10632 if (clr0)
10633 {
10634 if (AVR_HAVE_MOVW && clr1 && clrw)
10635 {
10636 xop[2] = all_regs_rtx[d0 & ~1];
10637 xop[3] = clrw;
10638 code = "movw %2,%3";
10639 stepw = step;
10640 }
10641 else
10642 {
10643 xop[2] = all_regs_rtx[d0];
10644 code = "clr %2";
10645
10646 if (++clr_n >= 2
10647 && !clrw
10648 && d0 % 2 == (step > 0))
10649 {
10650 clrw = all_regs_rtx[d0 & ~1];
10651 }
10652 }
10653 }
10654 else if (offset && s0 <= (signed) src.regno_msb)
10655 {
10656 int movw = AVR_HAVE_MOVW && offset % 2 == 0
10657 && d0 % 2 == (offset > 0)
10658 && d1 <= dest.regno_msb && d1 >= dest.regno
10659 && s1 <= (signed) src.regno_msb && s1 >= (signed) src.regno;
10660
10661 xop[2] = all_regs_rtx[d0 & ~movw];
10662 xop[3] = all_regs_rtx[s0 & ~movw];
10663 code = movw ? "movw %2,%3" : "mov %2,%3";
10664 stepw = step * movw;
10665 }
10666
10667 if (code)
10668 {
10669 if (sign_extend && shift != ASHIFT && !sign_in_carry
10670 && (d0 == src.regno_msb || d0 + stepw == src.regno_msb))
10671 {
10672 /* We are going to override the sign bit. If we sign-extend,
10673 store the sign in the Carry flag. This is not needed if
10674 the destination will be ASHIFT in the remainder because
10675 the ASHIFT will set Carry without extra instruction. */
10676
10677 avr_asm_len ("lsl %0", &all_regs_rtx[src.regno_msb], plen, 1);
10678 sign_in_carry = true;
10679 }
10680
10681 unsigned src_msb = dest.regno_msb - sign_bytes - offset + 1;
10682
10683 if (!sign_extend && shift == ASHIFTRT && !msb_in_carry
10684 && src.ibyte > dest.ibyte
10685 && (d0 == src_msb || d0 + stepw == src_msb))
10686 {
10687 /* We are going to override the MSB. If we shift right,
10688 store the MSB in the Carry flag. This is only needed if
10689 we don't sign-extend becaue with sign-extension the MSB
10690 (the sign) will be produced by the sign extension. */
10691
10692 avr_asm_len ("lsr %0", &all_regs_rtx[src_msb], plen, 1);
10693 msb_in_carry = true;
10694 }
10695
10696 unsigned src_lsb = dest.regno - offset -1;
10697
10698 if (shift == ASHIFT && src.fbyte > dest.fbyte && !lsb_in_carry
10699 && !lsb_in_tmp_reg
10700 && (d0 == src_lsb || d0 + stepw == src_lsb))
10701 {
10702 /* We are going to override the new LSB; store it into carry. */
10703
10704 avr_asm_len ("lsl %0", &all_regs_rtx[src_lsb], plen, 1);
10705 code_ashift = "rol %0";
10706 lsb_in_carry = true;
10707 }
10708
10709 avr_asm_len (code, xop, plen, 1);
10710 d0 += stepw;
10711 }
10712 }
10713
10714 /* Step 2: Shift destination left by 1 bit position. This might be needed
10715 ====== for signed input and unsigned output. */
10716
10717 if (shift == ASHIFT && src.fbyte > dest.fbyte && !lsb_in_carry)
10718 {
10719 unsigned s0 = dest.regno - offset -1;
10720
10721 /* n1169 4.1.4 says:
10722 "Conversions from a fixed-point to an integer type round toward zero."
10723 Hence, converting a fract type to integer only gives a non-zero result
10724 for -1. */
10725 if (SCALAR_INT_MODE_P (GET_MODE (xop[0]))
10726 && SCALAR_FRACT_MODE_P (GET_MODE (xop[1]))
10727 && !TARGET_FRACT_CONV_TRUNC)
10728 {
10729 gcc_assert (s0 == src.regno_msb);
10730 /* Check if the input is -1. We do that by checking if negating
10731 the input causes an integer overflow. */
10732 unsigned sn = src.regno;
10733 avr_asm_len ("cp __zero_reg__,%0", &all_regs_rtx[sn++], plen, 1);
10734 while (sn <= s0)
10735 avr_asm_len ("cpc __zero_reg__,%0", &all_regs_rtx[sn++], plen, 1);
10736
10737 /* Overflow goes with set carry. Clear carry otherwise. */
10738 avr_asm_len ("brvs 0f" CR_TAB
10739 "clc\n0:", nullptr, plen, 2);
10740 }
10741 /* Likewise, when converting from accumulator types to integer, we
10742 need to round up negative values. */
10743 else if (SCALAR_INT_MODE_P (GET_MODE (xop[0]))
10744 && SCALAR_ACCUM_MODE_P (GET_MODE (xop[1]))
10745 && !TARGET_FRACT_CONV_TRUNC
10746 && !frac_rounded)
10747 {
10748 bool have_carry = false;
10749
10750 xop[2] = all_regs_rtx[s0];
10751 if (!lsb_in_tmp_reg && !MAY_CLOBBER (s0))
10752 avr_asm_len ("mov __tmp_reg__,%2", xop, plen, 1);
10753 avr_asm_len ("tst %0" CR_TAB
10754 "brpl 0f", &all_regs_rtx[src.regno_msb], plen, 2);
10755 if (!lsb_in_tmp_reg)
10756 {
10757 unsigned sn = src.regno;
10758 if (sn < s0)
10759 {
10760 avr_asm_len ("cp __zero_reg__,%0", &all_regs_rtx[sn],
10761 plen, 1);
10762 have_carry = true;
10763 }
10764 while (++sn < s0)
10765 avr_asm_len ("cpc __zero_reg__,%0", &all_regs_rtx[sn], plen, 1);
10766 lsb_in_tmp_reg = !MAY_CLOBBER (s0);
10767 }
10768 /* Add in C and the rounding value 127. */
10769 /* If the destination msb is a sign byte, and in LD_REGS,
10770 grab it as a temporary. */
10771 if (sign_bytes
10772 && TEST_HARD_REG_BIT (reg_class_contents[LD_REGS],
10773 dest.regno_msb))
10774 {
10775 xop[3] = all_regs_rtx[dest.regno_msb];
10776 avr_asm_len ("ldi %3,127", xop, plen, 1);
10777 avr_asm_len ((have_carry && lsb_in_tmp_reg ? "adc __tmp_reg__,%3"
10778 : have_carry ? "adc %2,%3"
10779 : lsb_in_tmp_reg ? "add __tmp_reg__,%3"
10780 : "add %2,%3"),
10781 xop, plen, 1);
10782 }
10783 else
10784 {
10785 /* Fall back to use __zero_reg__ as a temporary. */
10786 avr_asm_len ("dec __zero_reg__", nullptr, plen, 1);
10787 if (have_carry)
10788 avr_asm_len ("clt" CR_TAB
10789 "bld __zero_reg__,7", nullptr, plen, 2);
10790 else
10791 avr_asm_len ("lsr __zero_reg__", nullptr, plen, 1);
10792 avr_asm_len (have_carry && lsb_in_tmp_reg
10793 ? "adc __tmp_reg__,__zero_reg__"
10794 : have_carry ? "adc %2,__zero_reg__"
10795 : lsb_in_tmp_reg ? "add __tmp_reg__,__zero_reg__"
10796 : "add %2,__zero_reg__",
10797 xop, plen, 1);
10798 avr_asm_len ("eor __zero_reg__,__zero_reg__", nullptr, plen, 1);
10799 }
10800
10801 for (d0 = dest.regno + zero_bytes;
10802 d0 <= dest.regno_msb - sign_bytes; d0++)
10803 avr_asm_len ("adc %0,__zero_reg__", &all_regs_rtx[d0], plen, 1);
10804
10805 avr_asm_len (lsb_in_tmp_reg
10806 ? "\n0:\t" "lsl __tmp_reg__"
10807 : "\n0:\t" "lsl %2",
10808 xop, plen, 1);
10809 }
10810 else if (MAY_CLOBBER (s0))
10811 avr_asm_len ("lsl %0", &all_regs_rtx[s0], plen, 1);
10812 else
10813 avr_asm_len ("mov __tmp_reg__,%0" CR_TAB
10814 "lsl __tmp_reg__", &all_regs_rtx[s0], plen, 2);
10815
10816 code_ashift = "rol %0";
10817 lsb_in_carry = true;
10818 }
10819
10820 if (shift == ASHIFT)
10821 {
10822 for (d0 = dest.regno + zero_bytes;
10823 d0 <= dest.regno_msb - sign_bytes; d0++)
10824 {
10825 avr_asm_len (code_ashift, &all_regs_rtx[d0], plen, 1);
10826 code_ashift = "rol %0";
10827 }
10828
10829 lsb_in_carry = false;
10830 sign_in_carry = true;
10831 }
10832
10833 /* Step 4a: Store MSB in carry if we don't already have it or will produce
10834 ======= it in sign-extension below. */
10835
10836 if (!sign_extend && shift == ASHIFTRT && !msb_in_carry
10837 && src.ibyte > dest.ibyte)
10838 {
10839 unsigned s0 = dest.regno_msb - sign_bytes - offset + 1;
10840
10841 if (MAY_CLOBBER (s0))
10842 avr_asm_len ("lsr %0", &all_regs_rtx[s0], plen, 1);
10843 else
10844 avr_asm_len ("mov __tmp_reg__,%0" CR_TAB
10845 "lsr __tmp_reg__", &all_regs_rtx[s0], plen, 2);
10846
10847 msb_in_carry = true;
10848 }
10849
10850 /* Step 3: Sign-extend or zero-extend the destination as needed.
10851 ====== */
10852
10853 if (sign_extend && !sign_in_carry)
10854 {
10855 unsigned s0 = src.regno_msb;
10856
10857 if (MAY_CLOBBER (s0))
10858 avr_asm_len ("lsl %0", &all_regs_rtx[s0], plen, 1);
10859 else
10860 avr_asm_len ("mov __tmp_reg__,%0" CR_TAB
10861 "lsl __tmp_reg__", &all_regs_rtx[s0], plen, 2);
10862
10863 sign_in_carry = true;
10864 }
10865
10866 gcc_assert (sign_in_carry + msb_in_carry + lsb_in_carry <= 1);
10867
10868 unsigned copies = 0;
10869 rtx movw = sign_extend ? NULL_RTX : clrw;
10870
10871 for (d0 = dest.regno_msb - sign_bytes + 1; d0 <= dest.regno_msb; d0++)
10872 {
10873 if (AVR_HAVE_MOVW && movw
10874 && d0 % 2 == 0 && d0 + 1 <= dest.regno_msb)
10875 {
10876 xop[2] = all_regs_rtx[d0];
10877 xop[3] = movw;
10878 avr_asm_len ("movw %2,%3", xop, plen, 1);
10879 d0++;
10880 }
10881 else
10882 {
10883 avr_asm_len (sign_extend ? "sbc %0,%0" : "clr %0",
10884 &all_regs_rtx[d0], plen, 1);
10885
10886 if (++copies >= 2 && !movw && d0 % 2 == 1)
10887 movw = all_regs_rtx[d0-1];
10888 }
10889 } /* for */
10890
10891
10892 /* Step 4: Right shift the destination. This might be needed for
10893 ====== conversions from unsigned to signed. */
10894
10895 if (shift == ASHIFTRT)
10896 {
10897 const char *code_ashiftrt = "lsr %0";
10898
10899 if (sign_extend || msb_in_carry)
10900 code_ashiftrt = "ror %0";
10901
10902 if (src.sbit && src.ibyte == dest.ibyte)
10903 code_ashiftrt = "asr %0";
10904
10905 for (d0 = dest.regno_msb - sign_bytes;
10906 d0 >= dest.regno + zero_bytes - 1 && d0 >= dest.regno; d0--)
10907 {
10908 avr_asm_len (code_ashiftrt, &all_regs_rtx[d0], plen, 1);
10909 code_ashiftrt = "ror %0";
10910 }
10911 }
10912
10913 #undef MAY_CLOBBER
10914
10915 return "";
10916 }
10917
10918
10919 /* Output fixed-point rounding. XOP[0] = XOP[1] is the operand to round.
10920 XOP[2] is the rounding point, a CONST_INT. The function prints the
10921 instruction sequence if PLEN = NULL and computes the length in words
10922 of the sequence if PLEN != NULL. Most of this function deals with
10923 preparing operands for calls to `avr_out_plus' and `avr_out_bitop'. */
10924
10925 const char *
10926 avr_out_round (rtx_insn * /*insn*/, rtx *xop, int *plen)
10927 {
10928 scalar_mode mode = as_a <scalar_mode> (GET_MODE (xop[0]));
10929 scalar_int_mode imode = int_mode_for_mode (mode).require ();
10930 // The smallest fractional bit not cleared by the rounding is 2^(-RP).
10931 int fbit = (int) GET_MODE_FBIT (mode);
10932 double_int i_add = double_int_zero.set_bit (fbit-1 - INTVAL (xop[2]));
10933 wide_int wi_add = wi::set_bit_in_zero (fbit-1 - INTVAL (xop[2]),
10934 GET_MODE_PRECISION (imode));
10935 // Lengths of PLUS and AND parts.
10936 int len_add = 0, *plen_add = plen ? &len_add : nullptr;
10937 int len_and = 0, *plen_and = plen ? &len_and : nullptr;
10938
10939 // Add-Saturate 1/2 * 2^(-RP). Don't print the label "0:" when printing
10940 // the saturated addition so that we can emit the "rjmp 1f" before the
10941 // "0:" below.
10942
10943 rtx xadd = const_fixed_from_double_int (i_add, mode);
10944 rtx xpattern, xsrc, op[4];
10945
10946 xsrc = SIGNED_FIXED_POINT_MODE_P (mode)
10947 ? gen_rtx_SS_PLUS (mode, xop[1], xadd)
10948 : gen_rtx_US_PLUS (mode, xop[1], xadd);
10949 xpattern = gen_rtx_SET (xop[0], xsrc);
10950
10951 op[0] = xop[0];
10952 op[1] = xop[1];
10953 op[2] = xadd;
10954 avr_out_plus (xpattern, op, plen_add, false /* Don't print "0:" */);
10955
10956 avr_asm_len ("rjmp 1f" CR_TAB
10957 "0:", nullptr, plen_add, 1);
10958
10959 // Keep all bits from RP and higher: ... 2^(-RP)
10960 // Clear all bits from RP+1 and lower: 2^(-RP-1) ...
10961 // Rounding point ^^^^^^^
10962 // Added above ^^^^^^^^^
10963 rtx xreg = simplify_gen_subreg (imode, xop[0], mode, 0);
10964 rtx xmask = immed_wide_int_const (-wi_add - wi_add, imode);
10965
10966 xpattern = gen_rtx_SET (xreg, gen_rtx_AND (imode, xreg, xmask));
10967
10968 op[0] = xreg;
10969 op[1] = xreg;
10970 op[2] = xmask;
10971 op[3] = gen_rtx_SCRATCH (QImode);
10972 avr_out_bitop (xpattern, op, plen_and);
10973 avr_asm_len ("1:", nullptr, plen, 0);
10974
10975 if (plen)
10976 *plen = len_add + len_and;
10977
10978 return "";
10979 }
10980
10981
10982 /* Create RTL split patterns for byte sized rotate expressions. This
10983 produces a series of move instructions and considers overlap situations.
10984 Overlapping non-HImode operands need a scratch register. */
10985
10986 bool
10987 avr_rotate_bytes (rtx operands[])
10988 {
10989 machine_mode mode = GET_MODE (operands[0]);
10990 bool overlapped = reg_overlap_mentioned_p (operands[0], operands[1]);
10991 bool same_reg = rtx_equal_p (operands[0], operands[1]);
10992 int num = INTVAL (operands[2]);
10993 rtx scratch = operands[3];
10994 /* Work out if byte or word move is needed. Odd byte rotates need QImode.
10995 Word move if no scratch is needed, otherwise use size of scratch. */
10996 machine_mode move_mode = QImode;
10997
10998 if (num & 0xf)
10999 move_mode = QImode;
11000 else if ((mode == SImode && !same_reg) || !overlapped)
11001 move_mode = HImode;
11002 else
11003 move_mode = GET_MODE (scratch);
11004
11005 /* Force DI rotate to use QI moves since other DI moves are currently split
11006 into QI moves so forward propagation works better. */
11007 if (mode == DImode)
11008 move_mode = QImode;
11009 /* Make scratch smaller if needed. */
11010 if (SCRATCH != GET_CODE (scratch)
11011 && HImode == GET_MODE (scratch)
11012 && QImode == move_mode)
11013 scratch = simplify_gen_subreg (move_mode, scratch, HImode, 0);
11014
11015 int move_size = GET_MODE_SIZE (move_mode);
11016 /* Number of bytes/words to rotate. */
11017 int offset = (num >> 3) / move_size;
11018 /* Number of moves needed. */
11019 int size = GET_MODE_SIZE (mode) / move_size;
11020 /* Himode byte swap is special case to avoid a scratch register. */
11021 if (mode == HImode && same_reg)
11022 {
11023 /* HImode byte swap, using xor. This is as quick as using scratch. */
11024 rtx src, dst;
11025 src = simplify_gen_subreg (move_mode, operands[1], mode, 0);
11026 dst = simplify_gen_subreg (move_mode, operands[0], mode, 1);
11027 if (!rtx_equal_p (dst, src))
11028 {
11029 emit_move_insn (dst, gen_rtx_XOR (QImode, dst, src));
11030 emit_move_insn (src, gen_rtx_XOR (QImode, src, dst));
11031 emit_move_insn (dst, gen_rtx_XOR (QImode, dst, src));
11032 }
11033 }
11034 else
11035 {
11036 #define MAX_SIZE 8 /* GET_MODE_SIZE (DImode) / GET_MODE_SIZE (QImode) */
11037 /* Create linked list of moves to determine move order. */
11038 struct {
11039 rtx src, dst;
11040 int links;
11041 } move[MAX_SIZE + 8];
11042 int blocked, moves;
11043
11044 gcc_assert (size <= MAX_SIZE);
11045 /* Generate list of subreg moves. */
11046 for (int i = 0; i < size; i++)
11047 {
11048 int from = i;
11049 int to = (from + offset) % size;
11050 move[i].src = simplify_gen_subreg (move_mode, operands[1],
11051 mode, from * move_size);
11052 move[i].dst = simplify_gen_subreg (move_mode, operands[0],
11053 mode, to * move_size);
11054 move[i].links = -1;
11055 }
11056 /* Mark dependence where a dst of one move is the src of another move.
11057 The first move is a conflict as it must wait until second is
11058 performed. We ignore moves to self - we catch this later. */
11059 if (overlapped)
11060 for (int i = 0; i < size; i++)
11061 if (reg_overlap_mentioned_p (move[i].dst, operands[1]))
11062 for (int j = 0; j < size; j++)
11063 if (j != i && rtx_equal_p (move[j].src, move[i].dst))
11064 {
11065 /* The dst of move i is the src of move j. */
11066 move[i].links = j;
11067 break;
11068 }
11069
11070 blocked = -1;
11071 moves = 0;
11072 /* Go through move list and perform non-conflicting moves. As each
11073 non-overlapping move is made, it may remove other conflicts
11074 so the process is repeated until no conflicts remain. */
11075 do
11076 {
11077 blocked = -1;
11078 moves = 0;
11079 /* Emit move where dst is not also a src or we have used that
11080 src already. */
11081 for (int i = 0; i < size; i++)
11082 if (move[i].src != NULL_RTX)
11083 {
11084 if (move[i].links == -1
11085 || move[move[i].links].src == NULL_RTX)
11086 {
11087 moves++;
11088 /* Ignore NOP moves to self. */
11089 if (!rtx_equal_p (move[i].dst, move[i].src))
11090 emit_move_insn (move[i].dst, move[i].src);
11091
11092 /* Remove conflict from list. */
11093 move[i].src = NULL_RTX;
11094 }
11095 else
11096 blocked = i;
11097 }
11098
11099 /* Check for deadlock. This is when no moves occurred and we have
11100 at least one blocked move. */
11101 if (moves == 0 && blocked != -1)
11102 {
11103 /* Need to use scratch register to break deadlock.
11104 Add move to put dst of blocked move into scratch.
11105 When this move occurs, it will break chain deadlock.
11106 The scratch register is substituted for real move. */
11107
11108 gcc_assert (SCRATCH != GET_CODE (scratch));
11109
11110 move[size].src = move[blocked].dst;
11111 move[size].dst = scratch;
11112 /* Scratch move is never blocked. */
11113 move[size].links = -1;
11114 /* Make sure we have valid link. */
11115 gcc_assert (move[blocked].links != -1);
11116 /* Replace src of blocking move with scratch reg. */
11117 move[move[blocked].links].src = scratch;
11118 /* Make dependent on scratch move occurring. */
11119 move[blocked].links = size;
11120 size=size+1;
11121 }
11122 }
11123 while (blocked != -1);
11124 }
11125 return true;
11126 }
11127
11128
11129 /* Worker function for `ADJUST_INSN_LENGTH'. */
11130 /* Modifies the length assigned to instruction INSN
11131 LEN is the initially computed length of the insn. */
11132
11133 int
11134 avr_adjust_insn_length (rtx_insn *insn, int len)
11135 {
11136 rtx *op = recog_data.operand;
11137
11138 /* As we pretend jump tables in .text, fix branch offsets crossing jump
11139 tables now. */
11140
11141 if (JUMP_TABLE_DATA_P (insn))
11142 return 0;
11143
11144 /* Some complex insns don't need length adjustment and therefore
11145 the length need not/must not be adjusted for these insns.
11146 It is easier to state this in an insn attribute "adjust_len" than
11147 to clutter up code here... */
11148
11149 if (!NONDEBUG_INSN_P (insn) || recog_memoized (insn) == -1)
11150 {
11151 return len;
11152 }
11153
11154 /* Read from insn attribute "adjust_len" if/how length is to be adjusted. */
11155
11156 attr_adjust_len adjust_len = get_attr_adjust_len (insn);
11157
11158 if (adjust_len == ADJUST_LEN_NO)
11159 {
11160 /* Nothing to adjust: The length from attribute "length" is fine.
11161 This is the default. */
11162
11163 return len;
11164 }
11165
11166 /* Extract insn's operands. */
11167
11168 extract_constrain_insn_cached (insn);
11169
11170 /* Dispatch to right function. */
11171
11172 switch (adjust_len)
11173 {
11174 case ADJUST_LEN_RELOAD_IN16: output_reload_inhi (op, op[2], &len); break;
11175 case ADJUST_LEN_RELOAD_IN24: avr_out_reload_inpsi (op, op[2], &len); break;
11176 case ADJUST_LEN_RELOAD_IN32: output_reload_insisf (op, op[2], &len); break;
11177
11178 case ADJUST_LEN_OUT_BITOP: avr_out_bitop (insn, op, &len); break;
11179 case ADJUST_LEN_EXTR_NOT: avr_out_extr_not (insn, op, &len); break;
11180 case ADJUST_LEN_EXTR: avr_out_extr (insn, op, &len); break;
11181 case ADJUST_LEN_INSV: avr_out_insv (insn, op, &len); break;
11182 case ADJUST_LEN_SET_SOME: avr_out_set_some (insn, op, &len); break;
11183
11184 case ADJUST_LEN_PLUS: avr_out_plus (insn, op, &len); break;
11185 case ADJUST_LEN_PLUS_EXT: avr_out_plus_ext (insn, op, &len); break;
11186 case ADJUST_LEN_ADDTO_SP: avr_out_addto_sp (op, &len); break;
11187
11188 case ADJUST_LEN_MOV8: output_movqi (insn, op, &len); break;
11189 case ADJUST_LEN_MOV16: output_movhi (insn, op, &len); break;
11190 case ADJUST_LEN_MOV24: avr_out_movpsi (insn, op, &len); break;
11191 case ADJUST_LEN_MOV32: output_movsisf (insn, op, &len); break;
11192 case ADJUST_LEN_CPYMEM: avr_out_cpymem (insn, op, &len); break;
11193 case ADJUST_LEN_XLOAD: avr_out_xload (insn, op, &len); break;
11194 case ADJUST_LEN_FLOAD: avr_out_fload (insn, op, &len); break;
11195 case ADJUST_LEN_SEXT: avr_out_sign_extend (insn, op, &len); break;
11196 case ADJUST_LEN_SEXTR: avr_out_sextr (insn, op, &len); break;
11197
11198 case ADJUST_LEN_SFRACT: avr_out_fract (insn, op, true, &len); break;
11199 case ADJUST_LEN_UFRACT: avr_out_fract (insn, op, false, &len); break;
11200 case ADJUST_LEN_ROUND: avr_out_round (insn, op, &len); break;
11201
11202 case ADJUST_LEN_TSTHI: avr_out_tsthi (insn, op, &len); break;
11203 case ADJUST_LEN_TSTPSI: avr_out_tstpsi (insn, op, &len); break;
11204 case ADJUST_LEN_TSTSI: avr_out_tstsi (insn, op, &len); break;
11205 case ADJUST_LEN_COMPARE: avr_out_compare (insn, op, &len); break;
11206 case ADJUST_LEN_COMPARE64: avr_out_compare64 (insn, op, &len); break;
11207 case ADJUST_LEN_CMP_UEXT: avr_out_cmp_ext (op, ZERO_EXTEND, &len); break;
11208 case ADJUST_LEN_CMP_SEXT: avr_out_cmp_ext (op, SIGN_EXTEND, &len); break;
11209 case ADJUST_LEN_CMP_LSR: avr_out_cmp_lsr (insn, op, &len); break;
11210
11211 case ADJUST_LEN_LSHRQI: lshrqi3_out (insn, op, &len); break;
11212 case ADJUST_LEN_LSHRHI: lshrhi3_out (insn, op, &len); break;
11213 case ADJUST_LEN_LSHRSI: lshrsi3_out (insn, op, &len); break;
11214
11215 case ADJUST_LEN_ASHRQI: ashrqi3_out (insn, op, &len); break;
11216 case ADJUST_LEN_ASHRHI: ashrhi3_out (insn, op, &len); break;
11217 case ADJUST_LEN_ASHRSI: ashrsi3_out (insn, op, &len); break;
11218
11219 case ADJUST_LEN_ASHLQI: ashlqi3_out (insn, op, &len); break;
11220 case ADJUST_LEN_ASHLHI: ashlhi3_out (insn, op, &len); break;
11221 case ADJUST_LEN_ASHLSI: ashlsi3_out (insn, op, &len); break;
11222
11223 case ADJUST_LEN_ASHLPSI: avr_out_ashlpsi3 (insn, op, &len); break;
11224 case ADJUST_LEN_ASHRPSI: avr_out_ashrpsi3 (insn, op, &len); break;
11225 case ADJUST_LEN_LSHRPSI: avr_out_lshrpsi3 (insn, op, &len); break;
11226
11227 case ADJUST_LEN_CALL: len = AVR_HAVE_JMP_CALL ? 2 : 1; break;
11228
11229 case ADJUST_LEN_INSERT_BITS: avr_out_insert_bits (op, &len); break;
11230 case ADJUST_LEN_ADD_SET_ZN: avr_out_plus_set_ZN (op, &len); break;
11231 case ADJUST_LEN_ADD_SET_N: avr_out_plus_set_N (op, &len); break;
11232
11233 case ADJUST_LEN_ADD_GE0: avr_out_add_msb (insn, op, GE, &len); break;
11234 case ADJUST_LEN_ADD_LT0: avr_out_add_msb (insn, op, LT, &len); break;
11235
11236 case ADJUST_LEN_INSV_NOTBIT: avr_out_insert_notbit (insn, op, &len); break;
11237
11238 default:
11239 gcc_unreachable ();
11240 }
11241
11242 return len;
11243 }
11244
11245
11246 /* Implement `TARGET_ASM_INTEGER'. */
11247 /* Target hook for assembling integer objects. The AVR version needs
11248 special handling for references to certain labels. */
11249
11250 static bool
11251 avr_assemble_integer (rtx x, unsigned int size, int aligned_p)
11252 {
11253 if (size == POINTER_SIZE / BITS_PER_UNIT && aligned_p
11254 && text_segment_operand (x, VOIDmode))
11255 {
11256 fputs ("\t.word\tgs(", asm_out_file);
11257 output_addr_const (asm_out_file, x);
11258 fputs (")\n", asm_out_file);
11259
11260 return true;
11261 }
11262 else if (GET_MODE (x) == PSImode)
11263 {
11264 /* This needs binutils 2.23+, see PR binutils/13503 */
11265
11266 fputs ("\t.byte\tlo8(", asm_out_file);
11267 output_addr_const (asm_out_file, x);
11268 fputs (")" ASM_COMMENT_START "need binutils PR13503\n", asm_out_file);
11269
11270 fputs ("\t.byte\thi8(", asm_out_file);
11271 output_addr_const (asm_out_file, x);
11272 fputs (")" ASM_COMMENT_START "need binutils PR13503\n", asm_out_file);
11273
11274 fputs ("\t.byte\thh8(", asm_out_file);
11275 output_addr_const (asm_out_file, x);
11276 fputs (")" ASM_COMMENT_START "need binutils PR13503\n", asm_out_file);
11277
11278 return true;
11279 }
11280 else if (CONST_FIXED_P (x))
11281 {
11282 /* varasm fails to handle big fixed modes that don't fit in hwi. */
11283
11284 for (unsigned n = 0; n < size; n++)
11285 default_assemble_integer (avr_byte (x, n), 1, aligned_p);
11286
11287 return true;
11288 }
11289
11290 if (AVR_TINY
11291 && avr_address_tiny_pm_p (x))
11292 {
11293 x = plus_constant (Pmode, x, avr_arch->flash_pm_offset);
11294 }
11295
11296 return default_assemble_integer (x, size, aligned_p);
11297 }
11298
11299
11300 /* Implement `TARGET_CLASS_MAX_NREGS'. Reasons described in comments for
11301 avr_hard_regno_nregs. */
11302
11303 static unsigned char
11304 avr_class_max_nregs (reg_class_t rclass, machine_mode mode)
11305 {
11306 if (rclass == CC_REG && GET_MODE_CLASS (mode) == MODE_CC)
11307 return 1;
11308
11309 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
11310 }
11311
11312
11313 /* Implement `TARGET_CLASS_LIKELY_SPILLED_P'. */
11314 /* Return value is nonzero if pseudos that have been
11315 assigned to registers of class CLASS would likely be spilled
11316 because registers of CLASS are needed for spill registers. */
11317
11318 static bool
11319 avr_class_likely_spilled_p (reg_class_t c)
11320 {
11321 return (c != ALL_REGS
11322 && (AVR_TINY ? 1 : c != ADDW_REGS));
11323 }
11324
11325
11326 /* Valid attributes:
11327 progmem - Put data to program memory.
11328 signal - Make a function to be hardware interrupt.
11329 After function prologue interrupts remain disabled.
11330 interrupt - Make a function to be hardware interrupt. Before function
11331 prologue interrupts are enabled by means of SEI.
11332 noblock - The function is an ISR that starts with a SEI instruction.
11333 naked - Don't generate function prologue/epilogue and RET
11334 instruction. */
11335
11336 /* Handle a "progmem" attribute; arguments as in
11337 struct attribute_spec.handler. */
11338
11339 static tree
11340 avr_handle_progmem_attribute (tree *node, tree name, tree args,
11341 int /*flags*/, bool *no_add_attrs)
11342 {
11343 if (DECL_P (*node))
11344 {
11345 if (TREE_CODE (*node) == TYPE_DECL)
11346 {
11347 /* This is really a decl attribute, not a type attribute,
11348 but try to handle it for GCC 3.0 backwards compatibility. */
11349
11350 tree type = TREE_TYPE (*node);
11351 tree attr = tree_cons (name, args, TYPE_ATTRIBUTES (type));
11352 tree newtype = build_type_attribute_variant (type, attr);
11353
11354 TYPE_MAIN_VARIANT (newtype) = TYPE_MAIN_VARIANT (type);
11355 TREE_TYPE (*node) = newtype;
11356 *no_add_attrs = true;
11357 }
11358 else if (TREE_STATIC (*node) || DECL_EXTERNAL (*node))
11359 {
11360 *no_add_attrs = false;
11361 }
11362 else
11363 {
11364 warning (OPT_Wattributes, "%qE attribute ignored",
11365 name);
11366 *no_add_attrs = true;
11367 }
11368 }
11369
11370 return NULL_TREE;
11371 }
11372
11373
11374 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
11375 struct attribute_spec.handler. */
11376
11377 static tree
11378 avr_handle_fndecl_attribute (tree *node, tree name, tree /*args*/,
11379 int /*flags*/, bool *no_add_attrs)
11380 {
11381 if (TREE_CODE (*node) != FUNCTION_DECL)
11382 {
11383 warning (OPT_Wattributes, "%qE attribute only applies to functions",
11384 name);
11385 *no_add_attrs = true;
11386 }
11387
11388 return NULL_TREE;
11389 }
11390
11391 static tree
11392 avr_handle_fntype_attribute (tree *node, tree name, tree /*args*/,
11393 int /*flags*/, bool *no_add_attrs)
11394 {
11395 if (TREE_CODE (*node) != FUNCTION_TYPE)
11396 {
11397 warning (OPT_Wattributes, "%qE attribute only applies to functions",
11398 name);
11399 *no_add_attrs = true;
11400 }
11401
11402 return NULL_TREE;
11403 }
11404
11405 static tree
11406 avr_handle_absdata_attribute (tree *node, tree name, tree /* args */,
11407 int /* flags */, bool *no_add)
11408 {
11409 location_t loc = DECL_SOURCE_LOCATION (*node);
11410
11411 if (AVR_TINY)
11412 {
11413 if (TREE_CODE (*node) != VAR_DECL
11414 || (!TREE_STATIC (*node) && !DECL_EXTERNAL (*node)))
11415 {
11416 warning_at (loc, OPT_Wattributes, "%qE attribute only applies to"
11417 " variables in static storage", name);
11418 *no_add = true;
11419 }
11420 }
11421 else
11422 {
11423 warning_at (loc, OPT_Wattributes, "%qE attribute only supported"
11424 " for reduced Tiny cores", name);
11425 *no_add = true;
11426 }
11427
11428 return NULL_TREE;
11429 }
11430
11431 static tree
11432 avr_handle_addr_attribute (tree *node, tree name, tree args,
11433 int /*flags*/, bool *no_add)
11434 {
11435 bool io_p = startswith (IDENTIFIER_POINTER (name), "io");
11436 HOST_WIDE_INT io_start = avr_arch->sfr_offset;
11437 HOST_WIDE_INT io_end = strcmp (IDENTIFIER_POINTER (name), "io_low") == 0
11438 ? io_start + 0x1f
11439 : io_start + 0x3f;
11440 location_t loc = DECL_SOURCE_LOCATION (*node);
11441
11442 if (!VAR_P (*node))
11443 {
11444 warning_at (loc, OPT_Wattributes, "%qE attribute only applies to "
11445 "variables", name);
11446 *no_add = true;
11447 return NULL_TREE;
11448 }
11449
11450 if (args != NULL_TREE)
11451 {
11452 if (TREE_CODE (TREE_VALUE (args)) == NON_LVALUE_EXPR)
11453 TREE_VALUE (args) = TREE_OPERAND (TREE_VALUE (args), 0);
11454 tree arg = TREE_VALUE (args);
11455 if (TREE_CODE (arg) != INTEGER_CST)
11456 {
11457 warning_at (loc, OPT_Wattributes, "%qE attribute allows only an "
11458 "integer constant argument", name);
11459 *no_add = true;
11460 }
11461 else if (io_p
11462 && (!tree_fits_shwi_p (arg)
11463 || ! IN_RANGE (TREE_INT_CST_LOW (arg), io_start, io_end)))
11464 {
11465 warning_at (loc, OPT_Wattributes, "%qE attribute address out of range"
11466 " 0x%x%s0x%x", name, (int) io_start, "...", (int) io_end);
11467 *no_add = true;
11468 }
11469 else
11470 {
11471 tree attribs = DECL_ATTRIBUTES (*node);
11472 const char *names[] = { "io", "io_low", "address", nullptr };
11473 for (const char **p = names; *p; p++)
11474 {
11475 tree other = lookup_attribute (*p, attribs);
11476 if (other && TREE_VALUE (other))
11477 {
11478 warning_at (loc, OPT_Wattributes,
11479 "both %s and %qE attribute provide address",
11480 *p, name);
11481 *no_add = true;
11482 break;
11483 }
11484 }
11485 }
11486 }
11487
11488 if (*no_add == false && io_p && !TREE_THIS_VOLATILE (*node))
11489 warning_at (loc, OPT_Wattributes, "%qE attribute on non-volatile variable",
11490 name);
11491
11492 // Optimizers must not draw any conclusions from "static int addr;" etc.
11493 // because the contents of `addr' are not given by its initializer but
11494 // by the contents at the address as specified by the attribute.
11495 if (VAR_P (*node) && ! *no_add)
11496 TREE_THIS_VOLATILE (*node) = 1;
11497
11498 return NULL_TREE;
11499 }
11500
11501 rtx
11502 avr_eval_addr_attrib (rtx x)
11503 {
11504 if (SYMBOL_REF_P (x)
11505 && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_ADDRESS))
11506 {
11507 tree decl = SYMBOL_REF_DECL (x);
11508 tree attr = NULL_TREE;
11509
11510 if (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_IO)
11511 {
11512 attr = lookup_attribute ("io", DECL_ATTRIBUTES (decl));
11513 if (!attr || !TREE_VALUE (attr))
11514 attr = lookup_attribute ("io_low", DECL_ATTRIBUTES (decl));
11515 }
11516 if (!attr || !TREE_VALUE (attr))
11517 attr = lookup_attribute ("address", DECL_ATTRIBUTES (decl));
11518 gcc_assert (attr && TREE_VALUE (attr) && TREE_VALUE (TREE_VALUE (attr)));
11519 return GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))));
11520 }
11521 return x;
11522 }
11523
11524
11525 /* AVR attributes. */
11526 TARGET_GNU_ATTRIBUTES (avr_attribute_table,
11527 {
11528 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
11529 affects_type_identity, handler, exclude } */
11530 { "progmem", 0, 0, false, false, false, false,
11531 avr_handle_progmem_attribute, NULL },
11532 { "signal", 0, -1, true, false, false, false,
11533 avr_handle_fndecl_attribute, NULL },
11534 { "interrupt", 0, -1, true, false, false, false,
11535 avr_handle_fndecl_attribute, NULL },
11536 { "noblock", 0, 0, true, false, false, false,
11537 avr_handle_fndecl_attribute, NULL },
11538 { "no_gccisr", 0, 0, true, false, false, false,
11539 avr_handle_fndecl_attribute, NULL },
11540 { "naked", 0, 0, false, true, true, false,
11541 avr_handle_fntype_attribute, NULL },
11542 { "OS_task", 0, 0, false, true, true, false,
11543 avr_handle_fntype_attribute, NULL },
11544 { "OS_main", 0, 0, false, true, true, false,
11545 avr_handle_fntype_attribute, NULL },
11546 { "io", 0, 1, true, false, false, false,
11547 avr_handle_addr_attribute, NULL },
11548 { "io_low", 0, 1, true, false, false, false,
11549 avr_handle_addr_attribute, NULL },
11550 { "address", 1, 1, true, false, false, false,
11551 avr_handle_addr_attribute, NULL },
11552 { "absdata", 0, 0, true, false, false, false,
11553 avr_handle_absdata_attribute, NULL }
11554 });
11555
11556
11557 /* Return true if we support address space AS for the architecture in effect
11558 and false, otherwise. If LOC is not UNKNOWN_LOCATION then also issue
11559 a respective error. */
11560
11561 bool
11562 avr_addr_space_supported_p (addr_space_t as, location_t loc)
11563 {
11564 if (AVR_TINY)
11565 {
11566 if (loc != UNKNOWN_LOCATION)
11567 error_at (loc, "address spaces are not supported for reduced "
11568 "Tiny devices");
11569 return false;
11570 }
11571 else if (avr_addrspace[as].segment >= avropt_n_flash)
11572 {
11573 if (loc != UNKNOWN_LOCATION)
11574 error_at (loc, "address space %qs not supported for devices with "
11575 "flash size up to %d KiB", avr_addrspace[as].name,
11576 64 * avropt_n_flash);
11577 return false;
11578 }
11579
11580 return true;
11581 }
11582
11583
11584 /* Implement `TARGET_ADDR_SPACE_DIAGNOSE_USAGE'. */
11585
11586 static void
11587 avr_addr_space_diagnose_usage (addr_space_t as, location_t loc)
11588 {
11589 (void) avr_addr_space_supported_p (as, loc);
11590 }
11591
11592
11593 /* Implement `TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID'. Zero is a valid
11594 address in all address spaces. Even in ADDR_SPACE_FLASH1 etc..,
11595 a zero address is valid and means 0x<RAMPZ val>0000, where RAMPZ is
11596 set to the appropriate segment value. */
11597
11598 static bool
11599 avr_addr_space_zero_address_valid (addr_space_t)
11600 {
11601 return true;
11602 }
11603
11604
11605 /* Look if DECL shall be placed in program memory space by
11606 means of attribute `progmem' or some address-space qualifier.
11607 Return non-zero if DECL is data that must end up in Flash and
11608 zero if the data lives in RAM (.bss, .data, .rodata, ...).
11609
11610 Return 2 if DECL is located in 24-bit flash address-space
11611 Return 1 if DECL is located in 16-bit flash address-space
11612 Return -1 if attribute `progmem' occurs in DECL or ATTRIBUTES
11613 Return 0 otherwise */
11614
11615 int
11616 avr_progmem_p (tree decl, tree attributes)
11617 {
11618 if (TREE_CODE (decl) != VAR_DECL)
11619 return 0;
11620
11621 if (avr_decl_memx_p (decl)
11622 || avr_decl_flashx_p (decl))
11623 return 2;
11624
11625 if (avr_decl_flash_p (decl))
11626 return 1;
11627
11628 if (NULL_TREE
11629 != lookup_attribute ("progmem", attributes))
11630 return -1;
11631
11632 tree a = decl;
11633
11634 do
11635 a = TREE_TYPE (a);
11636 while (TREE_CODE (a) == ARRAY_TYPE);
11637
11638 if (a == error_mark_node)
11639 return 0;
11640
11641 if (NULL_TREE != lookup_attribute ("progmem", TYPE_ATTRIBUTES (a)))
11642 return -1;
11643
11644 return 0;
11645 }
11646
11647
11648 /* Return true if DECL has attribute `absdata' set. This function should
11649 only be used for AVR_TINY. */
11650
11651 static bool
11652 avr_decl_absdata_p (tree decl, tree attributes)
11653 {
11654 return (VAR_P (decl)
11655 && NULL_TREE != lookup_attribute ("absdata", attributes));
11656 }
11657
11658
11659 /* Scan type TYP for pointer references to address space ASn.
11660 Return ADDR_SPACE_GENERIC (i.e. 0) if all pointers targeting
11661 the AS are also declared to be CONST.
11662 Otherwise, return the respective address space, i.e. a value != 0. */
11663
11664 static addr_space_t
11665 avr_nonconst_pointer_addrspace (tree typ)
11666 {
11667 while (ARRAY_TYPE == TREE_CODE (typ))
11668 typ = TREE_TYPE (typ);
11669
11670 if (POINTER_TYPE_P (typ))
11671 {
11672 tree target = TREE_TYPE (typ);
11673
11674 /* Pointer to function: Test the function's return type. */
11675
11676 if (FUNCTION_TYPE == TREE_CODE (target))
11677 return avr_nonconst_pointer_addrspace (TREE_TYPE (target));
11678
11679 /* "Ordinary" pointers... */
11680
11681 while (TREE_CODE (target) == ARRAY_TYPE)
11682 target = TREE_TYPE (target);
11683
11684 /* Pointers to non-generic address space must be const. */
11685
11686 addr_space_t as = TYPE_ADDR_SPACE (target);
11687
11688 if (!ADDR_SPACE_GENERIC_P (as)
11689 && !TYPE_READONLY (target)
11690 && avr_addr_space_supported_p (as))
11691 {
11692 return as;
11693 }
11694
11695 /* Scan pointer's target type. */
11696
11697 return avr_nonconst_pointer_addrspace (target);
11698 }
11699
11700 return ADDR_SPACE_GENERIC;
11701 }
11702
11703
11704 /* Sanity check NODE so that all pointers targeting non-generic address spaces
11705 go along with CONST qualifier. Writing to these address spaces should
11706 be detected and complained about as early as possible. */
11707
11708 static bool
11709 avr_pgm_check_var_decl (tree node)
11710 {
11711 const char *reason = nullptr;
11712
11713 addr_space_t as = ADDR_SPACE_GENERIC;
11714
11715 gcc_assert (as == 0);
11716
11717 if (avr_log.progmem)
11718 avr_edump ("%?: %t\n", node);
11719
11720 switch (TREE_CODE (node))
11721 {
11722 default:
11723 break;
11724
11725 case VAR_DECL:
11726 if (as = avr_nonconst_pointer_addrspace (TREE_TYPE (node)), as)
11727 reason = _("variable");
11728 break;
11729
11730 case PARM_DECL:
11731 if (as = avr_nonconst_pointer_addrspace (TREE_TYPE (node)), as)
11732 reason = _("function parameter");
11733 break;
11734
11735 case FIELD_DECL:
11736 if (as = avr_nonconst_pointer_addrspace (TREE_TYPE (node)), as)
11737 reason = _("structure field");
11738 break;
11739
11740 case FUNCTION_DECL:
11741 if (as = avr_nonconst_pointer_addrspace (TREE_TYPE (TREE_TYPE (node))),
11742 as)
11743 reason = _("return type of function");
11744 break;
11745
11746 case POINTER_TYPE:
11747 if (as = avr_nonconst_pointer_addrspace (node), as)
11748 reason = _("pointer");
11749 break;
11750 }
11751
11752 if (reason)
11753 {
11754 if (TYPE_P (node))
11755 error ("pointer targeting address space %qs must be const in %qT",
11756 avr_addrspace[as].name, node);
11757 else
11758 error ("pointer targeting address space %qs must be const"
11759 " in %s %q+D",
11760 avr_addrspace[as].name, reason, node);
11761 }
11762
11763 return reason == nullptr;
11764 }
11765
11766
11767 /* Helper for `avr_insert_attributes'. Print an error when there are invalid
11768 attributes named NAME, where NAME is in { "signal", "interrupt" }. */
11769
11770 static void
11771 avr_handle_isr_attribute (tree node, tree *attrs, const char *name)
11772 {
11773 bool seen = false;
11774
11775 for (tree list = lookup_attribute (name, *attrs); list;
11776 list = lookup_attribute (name, TREE_CHAIN (list)))
11777 {
11778 seen = true;
11779 for (tree v = TREE_VALUE (list); v; v = TREE_CHAIN (v))
11780 {
11781 int num = avr_isr_number (TREE_VALUE (v));
11782 if (! num)
11783 error ("attribute %qs expects a constant positive integer"
11784 " argument", name);
11785 if (TARGET_CVT
11786 && num >= 4)
11787 error ("vector number %d of %q+D is out of range 1%s3 for"
11788 " compact vector table", num, node, "...");
11789 }
11790 }
11791
11792 if (seen
11793 && ! lookup_attribute ("used", *attrs))
11794 {
11795 *attrs = tree_cons (get_identifier ("used"), NULL, *attrs);
11796 }
11797 }
11798
11799
11800 /* Helper for `avr_insert_attributes'.
11801 Return the section name from attribute "section" in attribute list ATTRS.
11802 When no "section" attribute is present, then return nullptr. */
11803
11804 static const char *
11805 avr_attrs_section_name (tree attrs)
11806 {
11807 if (tree a_sec = lookup_attribute ("section", attrs))
11808 if (TREE_VALUE (a_sec))
11809 if (tree t_section_name = TREE_VALUE (TREE_VALUE (a_sec)))
11810 if (TREE_CODE (t_section_name) == STRING_CST)
11811 return TREE_STRING_POINTER (t_section_name);
11812
11813 return nullptr;
11814 }
11815
11816
11817 /* Implement `TARGET_INSERT_ATTRIBUTES'. */
11818
11819 static void
11820 avr_insert_attributes (tree node, tree *attributes)
11821 {
11822 if (VAR_P (node)
11823 && ! TREE_STATIC (node)
11824 && ! DECL_EXTERNAL (node))
11825 {
11826 const char *names[] = { "io", "io_low", "address", nullptr };
11827 for (const char **p = names; *p; ++p)
11828 if (lookup_attribute (*p, *attributes))
11829 error ("variable %q+D with attribute %qs must be located in "
11830 "static storage", node, *p);
11831 }
11832
11833 avr_pgm_check_var_decl (node);
11834
11835 if (TARGET_MAIN_IS_OS_TASK
11836 && TREE_CODE (node) == FUNCTION_DECL
11837 && MAIN_NAME_P (DECL_NAME (node))
11838 // FIXME: We'd like to also test `flag_hosted' which is only
11839 // available in the C-ish fronts, hence no such test for now.
11840 // Instead, we test the return type of "main" which is not exactly
11841 // the same but good enough.
11842 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (node)))
11843 && NULL == lookup_attribute ("OS_task", *attributes))
11844 {
11845 *attributes = tree_cons (get_identifier ("OS_task"),
11846 NULL, *attributes);
11847 }
11848
11849 const char *section_name = avr_attrs_section_name (*attributes);
11850
11851 // When the function is in an .initN or .finiN section, then add "used"
11852 // since such functions are never called.
11853 if (section_name
11854 && strlen (section_name) == strlen (".init*")
11855 && IN_RANGE (section_name[5], '0', '9')
11856 && (startswith (section_name, ".init")
11857 || startswith (section_name, ".fini"))
11858 && !lookup_attribute ("used", *attributes))
11859 {
11860 *attributes = tree_cons (get_identifier ("used"), NULL, *attributes);
11861 }
11862
11863 #if defined WITH_AVRLIBC
11864 if (avropt_call_main == 0
11865 && TREE_CODE (node) == FUNCTION_DECL
11866 && MAIN_NAME_P (DECL_NAME (node)))
11867 {
11868 bool in_init9_p = section_name && !strcmp (section_name, ".init9");
11869
11870 if (section_name && !in_init9_p)
11871 {
11872 warning (OPT_Wattributes, "%<section(\"%s\")%> attribute on main"
11873 " function inhibits %<-mno-call-main%>", section_name);
11874 }
11875 else
11876 {
11877 if (!lookup_attribute ("noreturn", *attributes))
11878 *attributes = tree_cons (get_identifier ("noreturn"),
11879 NULL_TREE, *attributes);
11880 // Put main into section .init9 so that it is executed even
11881 // though it's not called.
11882 if (!in_init9_p)
11883 {
11884 tree init9 = build_string (1 + strlen (".init9"), ".init9");
11885 tree arg = build_tree_list (NULL_TREE, init9);
11886 *attributes = tree_cons (get_identifier ("section"),
11887 arg, *attributes);
11888 }
11889 if (!lookup_attribute ("used", *attributes))
11890 *attributes = tree_cons (get_identifier ("used"),
11891 NULL_TREE, *attributes);
11892 }
11893 } // -mno-call-main
11894 #endif // AVR-LibC
11895
11896 avr_handle_isr_attribute (node, attributes, "signal");
11897 avr_handle_isr_attribute (node, attributes, "interrupt");
11898
11899 /* Add the section attribute if the variable is in progmem. */
11900
11901 if (VAR_P (node)
11902 && (TREE_STATIC (node) || DECL_EXTERNAL (node))
11903 && avr_progmem_p (node, *attributes))
11904 {
11905 tree node0 = node;
11906
11907 /* For C++, we have to peel arrays in order to get correct
11908 determination of readonlyness. */
11909
11910 do
11911 node0 = TREE_TYPE (node0);
11912 while (TREE_CODE (node0) == ARRAY_TYPE);
11913
11914 if (error_mark_node == node0)
11915 return;
11916
11917 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (node));
11918
11919 if (!TYPE_READONLY (node0)
11920 && !TREE_READONLY (node))
11921 {
11922 const char *reason = "__attribute__((progmem))";
11923
11924 if (!ADDR_SPACE_GENERIC_P (as))
11925 reason = avr_addrspace[as].name;
11926
11927 if (avr_log.progmem)
11928 avr_edump ("\n%?: %t\n%t\n", node, node0);
11929
11930 error ("variable %q+D must be const in order to be put into"
11931 " read-only section by means of %qs", node, reason);
11932 }
11933 }
11934 }
11935
11936 #ifdef HAVE_LD_AVR_AVRXMEGA2_FLMAP
11937 static const bool have_avrxmega2_flmap = true;
11938 #else
11939 static const bool have_avrxmega2_flmap = false;
11940 #endif
11941
11942 #ifdef HAVE_LD_AVR_AVRXMEGA4_FLMAP
11943 static const bool have_avrxmega4_flmap = true;
11944 #else
11945 static const bool have_avrxmega4_flmap = false;
11946 #endif
11947
11948 #ifdef HAVE_LD_AVR_AVRXMEGA3_RODATA_IN_FLASH
11949 static const bool have_avrxmega3_rodata_in_flash = true;
11950 #else
11951 static const bool have_avrxmega3_rodata_in_flash = false;
11952 #endif
11953
11954
11955 static bool
11956 avr_rodata_in_flash_p ()
11957 {
11958 switch (avr_arch_index)
11959 {
11960 default:
11961 break;
11962
11963 case ARCH_AVRTINY:
11964 return true;
11965
11966 case ARCH_AVRXMEGA3:
11967 return have_avrxmega3_rodata_in_flash;
11968
11969 case ARCH_AVRXMEGA2:
11970 return avropt_flmap && have_avrxmega2_flmap && avropt_rodata_in_ram != 1;
11971
11972 case ARCH_AVRXMEGA4:
11973 return avropt_flmap && have_avrxmega4_flmap && avropt_rodata_in_ram != 1;
11974 }
11975
11976 return false;
11977 }
11978
11979
11980 /* Implement `ASM_OUTPUT_ALIGNED_DECL_LOCAL'. */
11981 /* Implement `ASM_OUTPUT_ALIGNED_DECL_COMMON'. */
11982 /* Track need of __do_clear_bss. */
11983
11984 void
11985 avr_asm_output_aligned_decl_common (FILE *stream, tree /* decl */,
11986 const char *name,
11987 unsigned HOST_WIDE_INT size,
11988 unsigned int align, bool local_p)
11989 {
11990 /* __gnu_lto_slim is just a marker for the linker injected by toplev.cc.
11991 There is no need to trigger __do_clear_bss code for them. */
11992
11993 if (!startswith (name, "__gnu_lto"))
11994 avr_need_clear_bss_p = true;
11995
11996 if (local_p)
11997 ASM_OUTPUT_ALIGNED_LOCAL (stream, name, size, align);
11998 else
11999 ASM_OUTPUT_ALIGNED_COMMON (stream, name, size, align);
12000 }
12001
12002
12003 /* Implement `ASM_OUTPUT_ALIGNED_BSS'. */
12004
12005 void
12006 avr_asm_asm_output_aligned_bss (FILE *file, tree decl, const char *name,
12007 unsigned HOST_WIDE_INT size, int align,
12008 void (*default_func)
12009 (FILE *, tree, const char *,
12010 unsigned HOST_WIDE_INT, int))
12011 {
12012 if (!startswith (name, "__gnu_lto"))
12013 avr_need_clear_bss_p = true;
12014
12015 default_func (file, decl, name, size, align);
12016 }
12017
12018
12019 /* Unnamed section callback for data_section
12020 to track need of __do_copy_data. */
12021
12022 static void
12023 avr_output_data_section_asm_op (const char *data)
12024 {
12025 avr_need_copy_data_p = true;
12026
12027 /* Dispatch to default. */
12028 output_section_asm_op (data);
12029 }
12030
12031
12032 /* Unnamed section callback for bss_section
12033 to track need of __do_clear_bss. */
12034
12035 static void
12036 avr_output_bss_section_asm_op (const char *data)
12037 {
12038 avr_need_clear_bss_p = true;
12039
12040 /* Dispatch to default. */
12041 output_section_asm_op (data);
12042 }
12043
12044
12045 /* Unnamed section callback for progmem*.data sections. */
12046
12047 static void
12048 avr_output_progmem_section_asm_op (const char *data)
12049 {
12050 fprintf (asm_out_file, "\t.section\t%s,\"a\",@progbits\n", data);
12051 }
12052
12053
12054 /* A noswitch section callback to output symbol definitions for
12055 attributes "io", "io_low" and "address". */
12056
12057 static bool
12058 avr_output_addr_attrib (tree decl, const char *name,
12059 unsigned HOST_WIDE_INT /* size */,
12060 unsigned HOST_WIDE_INT /* align */)
12061 {
12062 gcc_assert (DECL_RTL_SET_P (decl));
12063
12064 FILE *stream = asm_out_file;
12065 bool local_p = ! DECL_WEAK (decl) && ! TREE_PUBLIC (decl);
12066 rtx symbol, mem = DECL_RTL (decl);
12067
12068 if (mem != NULL_RTX && MEM_P (mem)
12069 && SYMBOL_REF_P ((symbol = XEXP (mem, 0)))
12070 && (SYMBOL_REF_FLAGS (symbol) & (SYMBOL_FLAG_IO | SYMBOL_FLAG_ADDRESS)))
12071 {
12072 if (! local_p)
12073 {
12074 fprintf (stream, "\t%s\t", DECL_WEAK (decl) ? ".weak" : ".globl");
12075 assemble_name (stream, name);
12076 fprintf (stream, "\n");
12077 }
12078
12079 if (SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_ADDRESS)
12080 {
12081 assemble_name (stream, name);
12082 fprintf (stream, " = %ld\n",
12083 (long) INTVAL (avr_eval_addr_attrib (symbol)));
12084 }
12085 else if (local_p)
12086 {
12087 const char *names[] = { "io", "io_low", "address", nullptr };
12088 for (const char **p = names; *p; ++p)
12089 if (lookup_attribute (*p, DECL_ATTRIBUTES (decl)))
12090 {
12091 error ("static attribute %qs declaration for %q+D needs an "
12092 "address", *p, decl);
12093 break;
12094 }
12095 }
12096
12097 return true;
12098 }
12099
12100 gcc_unreachable ();
12101
12102 return false;
12103 }
12104
12105
12106 /* Implement `TARGET_ASM_INIT_SECTIONS'. */
12107
12108 static void
12109 avr_asm_init_sections (void)
12110 {
12111 /* Override section callbacks to keep track of `avr_need_clear_bss_p',
12112 `avr_need_copy_data_p' and `avr_has_rodata_p'.
12113 Track also .rodata for the case when .rodata is located in RAM. */
12114
12115 if (! avr_rodata_in_flash_p ())
12116 readonly_data_section->unnamed.callback = avr_output_data_section_asm_op;
12117 data_section->unnamed.callback = avr_output_data_section_asm_op;
12118 bss_section->unnamed.callback = avr_output_bss_section_asm_op;
12119 tls_comm_section->noswitch.callback = avr_output_addr_attrib;
12120 }
12121
12122
12123 /* Implement `TARGET_ASM_NAMED_SECTION'. */
12124 /* Track need of __do_clear_bss, __do_copy_data for named sections. */
12125
12126 static void
12127 avr_asm_named_section (const char *name, unsigned int flags, tree decl)
12128 {
12129 if (flags & AVR_SECTION_PROGMEM
12130 // Only use section .progmem*.data if there is no attribute section.
12131 && ! (decl
12132 && DECL_SECTION_NAME (decl)
12133 && symtab_node::get (decl)
12134 && ! symtab_node::get (decl)->implicit_section))
12135 {
12136 addr_space_t as = (flags & AVR_SECTION_PROGMEM) / SECTION_MACH_DEP;
12137 const char *old_prefix = ".rodata";
12138 const char *new_prefix = avr_addrspace[as].section_name;
12139
12140 if (startswith (name, old_prefix))
12141 {
12142 const char *sname = ACONCAT ((new_prefix,
12143 name + strlen (old_prefix), nullptr));
12144 default_elf_asm_named_section (sname, flags, decl);
12145 return;
12146 }
12147
12148 default_elf_asm_named_section (new_prefix, flags, decl);
12149 return;
12150 }
12151
12152 if (!avr_need_copy_data_p)
12153 avr_need_copy_data_p = (startswith (name, ".data")
12154 || startswith (name, ".gnu.linkonce.d"));
12155
12156 if (!avr_has_rodata_p)
12157 avr_has_rodata_p = (startswith (name, ".rodata")
12158 || startswith (name, ".gnu.linkonce.r"));
12159
12160 if (!avr_need_clear_bss_p)
12161 avr_need_clear_bss_p = startswith (name, ".bss");
12162
12163 default_elf_asm_named_section (name, flags, decl);
12164 }
12165
12166
12167 /* Implement `TARGET_SECTION_TYPE_FLAGS'. */
12168
12169 static unsigned int
12170 avr_section_type_flags (tree decl, const char *name, int reloc)
12171 {
12172 unsigned int flags = default_section_type_flags (decl, name, reloc);
12173
12174 if (startswith (name, ".noinit"))
12175 {
12176 if (decl && VAR_P (decl)
12177 && DECL_INITIAL (decl) == NULL_TREE)
12178 flags |= SECTION_BSS; /* @nobits */
12179 else
12180 warning (0, "only uninitialized variables can be placed in the "
12181 "%<.noinit%> section");
12182 }
12183
12184 if (decl && DECL_P (decl)
12185 && avr_progmem_p (decl, DECL_ATTRIBUTES (decl)))
12186 {
12187 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
12188
12189 /* Attribute progmem puts data in generic address space.
12190 Set section flags as if it was in __flash to get the right
12191 section prefix in the remainder. */
12192
12193 if (ADDR_SPACE_GENERIC_P (as))
12194 as = ADDR_SPACE_FLASH;
12195
12196 flags |= as * SECTION_MACH_DEP;
12197 flags &= ~SECTION_WRITE;
12198 flags &= ~SECTION_BSS;
12199 flags &= ~SECTION_NOTYPE;
12200 }
12201
12202 return flags;
12203 }
12204
12205
12206 /* A helper for the next function. NODE is a decl that is associated with
12207 a symbol. Return TRUE if the respective object may be accessed by LDS.
12208 There might still be other reasons for why LDS is not appropriate.
12209 This function is only appropriate for AVR_TINY. */
12210
12211 static bool
12212 avr_decl_maybe_lds_p (tree node)
12213 {
12214 if (!node
12215 || TREE_CODE (node) != VAR_DECL
12216 || DECL_SECTION_NAME (node) != nullptr)
12217 return false;
12218
12219 /* Don't use LDS for objects that go to .rodata. The current default
12220 linker description file still locates .rodata in RAM, but this is not
12221 a must. A better linker script would just keep .rodata in flash and
12222 add an offset of 0x4000 to the VMA. Hence avoid LDS for such data. */
12223
12224 if (TREE_READONLY (node))
12225 return false;
12226
12227 // C++ requires peeling arrays.
12228
12229 do
12230 node = TREE_TYPE (node);
12231 while (ARRAY_TYPE == TREE_CODE (node));
12232
12233 return (node != error_mark_node
12234 && !TYPE_READONLY (node));
12235 }
12236
12237
12238 /* Implement `TARGET_ENCODE_SECTION_INFO'. */
12239
12240 static void
12241 avr_encode_section_info (tree decl, rtx rtl, int new_decl_p)
12242 {
12243 tree addr_attr = NULL_TREE;
12244
12245 /* In avr_handle_progmem_attribute, DECL_INITIAL is not yet
12246 readily available, see PR34734. So we postpone the warning
12247 about uninitialized data in program memory section until here. */
12248
12249 if (new_decl_p
12250 && decl && DECL_P (decl)
12251 && !DECL_EXTERNAL (decl)
12252 && avr_progmem_p (decl, DECL_ATTRIBUTES (decl)))
12253 {
12254 if (!TREE_READONLY (decl))
12255 {
12256 // This might happen with C++ if stuff needs constructing.
12257 error ("variable %q+D with dynamic initialization put "
12258 "into program memory area", decl);
12259 }
12260 else if (NULL_TREE == DECL_INITIAL (decl))
12261 {
12262 // Don't warn for (implicit) aliases like in PR80462.
12263 tree asmname = DECL_ASSEMBLER_NAME (decl);
12264 varpool_node *node = varpool_node::get_for_asmname (asmname);
12265 bool alias_p = node && node->alias;
12266
12267 if (!alias_p)
12268 warning (OPT_Wuninitialized, "uninitialized variable %q+D put "
12269 "into program memory area", decl);
12270 }
12271 }
12272
12273 default_encode_section_info (decl, rtl, new_decl_p);
12274
12275 if (decl && DECL_P (decl)
12276 && TREE_CODE (decl) != FUNCTION_DECL
12277 && MEM_P (rtl)
12278 && SYMBOL_REF_P (XEXP (rtl, 0)))
12279 {
12280 rtx sym = XEXP (rtl, 0);
12281 tree type = TREE_TYPE (decl);
12282 tree attr = DECL_ATTRIBUTES (decl);
12283 if (type == error_mark_node)
12284 return;
12285
12286 addr_space_t as = TYPE_ADDR_SPACE (type);
12287
12288 /* PSTR strings are in generic space but located in flash:
12289 patch address space. */
12290
12291 if (!AVR_TINY && avr_progmem_p (decl, attr) == -1)
12292 as = ADDR_SPACE_FLASH;
12293
12294 AVR_SYMBOL_SET_ADDR_SPACE (sym, as);
12295
12296 tree io_low_attr = lookup_attribute ("io_low", attr);
12297 tree io_attr = lookup_attribute ("io", attr);
12298 tree address_attr = lookup_attribute ("address", attr);
12299
12300 if (io_low_attr
12301 && TREE_VALUE (io_low_attr) && TREE_VALUE (TREE_VALUE (io_low_attr)))
12302 addr_attr = io_low_attr;
12303 else if (io_attr
12304 && TREE_VALUE (io_attr) && TREE_VALUE (TREE_VALUE (io_attr)))
12305 addr_attr = io_attr;
12306 else
12307 addr_attr = address_attr;
12308
12309 if (io_low_attr
12310 || (io_attr && addr_attr
12311 && low_io_address_operand
12312 (GEN_INT (TREE_INT_CST_LOW
12313 (TREE_VALUE (TREE_VALUE (addr_attr)))), QImode)))
12314 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_IO_LOW;
12315 if (io_attr || io_low_attr)
12316 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_IO;
12317 /* If we have an (io) address attribute specification, but the variable
12318 is external, treat the address as only a tentative definition
12319 to be used to determine if an io port is in the lower range, but
12320 don't use the exact value for constant propagation. */
12321 if (addr_attr && !DECL_EXTERNAL (decl))
12322 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_ADDRESS;
12323
12324 if (io_attr || io_low_attr || address_attr)
12325 {
12326 if (DECL_INITIAL (decl))
12327 {
12328 /* Initializers are not yet parsed in TARGET_INSERT_ATTRIBUTES,
12329 hence deny initializers now. The values of symbols with an
12330 address attribute are determined by the attribute, not by
12331 some initializer. */
12332
12333 error ("variable %q+D with attribute %qs must not have an "
12334 "initializer", decl,
12335 io_low_attr ? "io_low" : io_attr ? "io" : "address");
12336 }
12337 else
12338 {
12339 /* PR112952: The only way to output a variable declaration in a
12340 custom manner is by means of a noswitch section callback.
12341 There are only three noswitch sections: comm_section,
12342 lcomm_section and tls_comm_section. And there is no way to
12343 wire a custom noswitch section to a decl. As lcomm_section
12344 is bypassed with -fdata-sections -fno-common, there is no
12345 other way than making use of tls_comm_section. As we are
12346 using that section anyway, also use it in the public case. */
12347
12348 DECL_COMMON (decl) = 1;
12349 set_decl_section_name (decl, (const char *) nullptr);
12350 set_decl_tls_model (decl, (tls_model) 2);
12351 }
12352 }
12353 }
12354
12355 if (AVR_TINY
12356 && decl
12357 && VAR_P (decl)
12358 && MEM_P (rtl)
12359 && SYMBOL_REF_P (XEXP (rtl, 0)))
12360 {
12361 rtx sym = XEXP (rtl, 0);
12362 bool progmem_p = avr_progmem_p (decl, DECL_ATTRIBUTES (decl)) == -1;
12363
12364 if (progmem_p)
12365 {
12366 // Tag symbols for addition of 0x4000 (avr_arch->flash_pm_offset).
12367 SYMBOL_REF_FLAGS (sym) |= AVR_SYMBOL_FLAG_TINY_PM;
12368 }
12369
12370 if (avr_decl_absdata_p (decl, DECL_ATTRIBUTES (decl))
12371 || (TARGET_ABSDATA
12372 && !progmem_p
12373 && !addr_attr
12374 && avr_decl_maybe_lds_p (decl))
12375 || (addr_attr
12376 // If addr_attr is non-null, it has an argument. Peek into it.
12377 && TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (addr_attr))) < 0xc0))
12378 {
12379 // May be accessed by LDS / STS.
12380 SYMBOL_REF_FLAGS (sym) |= AVR_SYMBOL_FLAG_TINY_ABSDATA;
12381 }
12382
12383 if (progmem_p
12384 && avr_decl_absdata_p (decl, DECL_ATTRIBUTES (decl)))
12385 {
12386 error ("%q+D has incompatible attributes %qs and %qs",
12387 decl, "progmem", "absdata");
12388 }
12389 }
12390 }
12391
12392
12393 /* Implement `TARGET_ASM_SELECT_SECTION' */
12394
12395 static section *
12396 avr_asm_select_section (tree decl, int reloc, unsigned HOST_WIDE_INT align)
12397 {
12398 section *sect = default_elf_select_section (decl, reloc, align);
12399
12400 if (decl && DECL_P (decl)
12401 && avr_progmem_p (decl, DECL_ATTRIBUTES (decl)))
12402 {
12403 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
12404
12405 /* __progmem__ goes in generic space but shall be allocated to
12406 .progmem.data */
12407
12408 if (ADDR_SPACE_GENERIC_P (as))
12409 as = ADDR_SPACE_FLASH;
12410
12411 if (sect->common.flags & SECTION_NAMED)
12412 {
12413 const char *name = sect->named.name;
12414 const char *old_prefix = ".rodata";
12415 const char *new_prefix = avr_addrspace[as].section_name;
12416
12417 if (startswith (name, old_prefix))
12418 {
12419 const char *sname = ACONCAT ((new_prefix,
12420 name + strlen (old_prefix), NULL));
12421 return get_section (sname,
12422 sect->common.flags & ~SECTION_DECLARED,
12423 sect->named.decl);
12424 }
12425 }
12426
12427 if (!progmem_section[as])
12428 {
12429 progmem_section[as]
12430 = get_unnamed_section (0, avr_output_progmem_section_asm_op,
12431 avr_addrspace[as].section_name);
12432 }
12433
12434 return progmem_section[as];
12435 }
12436
12437 return sect;
12438 }
12439
12440
12441 /* Implement `TARGET_ASM_FILE_START'. */
12442 /* Outputs some text at the start of each assembler file. */
12443
12444 static void
12445 avr_file_start (void)
12446 {
12447 int sfr_offset = avr_arch->sfr_offset;
12448
12449 if (avr_arch->asm_only)
12450 error ("architecture %qs supported for assembler only", avropt_mmcu);
12451
12452 default_file_start ();
12453
12454 /* Print I/O addresses of some SFRs used with IN and OUT. */
12455
12456 if (AVR_HAVE_SPH)
12457 fprintf (asm_out_file, "__SP_H__ = 0x%02x\n", avr_addr.sp_h - sfr_offset);
12458
12459 fprintf (asm_out_file, "__SP_L__ = 0x%02x\n", avr_addr.sp_l - sfr_offset);
12460 fprintf (asm_out_file, "__SREG__ = 0x%02x\n", avr_addr.sreg - sfr_offset);
12461 if (AVR_HAVE_RAMPZ)
12462 fprintf (asm_out_file, "__RAMPZ__ = 0x%02x\n", avr_addr.rampz - sfr_offset);
12463 if (AVR_HAVE_RAMPY)
12464 fprintf (asm_out_file, "__RAMPY__ = 0x%02x\n", avr_addr.rampy - sfr_offset);
12465 if (AVR_HAVE_RAMPX)
12466 fprintf (asm_out_file, "__RAMPX__ = 0x%02x\n", avr_addr.rampx - sfr_offset);
12467 if (AVR_HAVE_RAMPD)
12468 fprintf (asm_out_file, "__RAMPD__ = 0x%02x\n", avr_addr.rampd - sfr_offset);
12469 if (AVR_XMEGA || AVR_TINY)
12470 fprintf (asm_out_file, "__CCP__ = 0x%02x\n", avr_addr.ccp - sfr_offset);
12471 fprintf (asm_out_file, "__tmp_reg__ = %d\n", AVR_TMP_REGNO);
12472 fprintf (asm_out_file, "__zero_reg__ = %d\n", AVR_ZERO_REGNO);
12473 }
12474
12475
12476 /* Implement `TARGET_ASM_FILE_END'. */
12477 /* Outputs to the stdio stream FILE some
12478 appropriate text to go at the end of an assembler file. */
12479
12480 static void
12481 avr_file_end (void)
12482 {
12483 /* Output these only if there is anything in the
12484 .data* / .rodata* / .gnu.linkonce.* resp. .bss* or COMMON
12485 input section(s) - some code size can be saved by not
12486 linking in the initialization code from libgcc if resp.
12487 sections are empty, see PR18145. */
12488
12489 if (avr_need_copy_data_p
12490 || (avr_has_rodata_p && ! avr_rodata_in_flash_p ()))
12491 fputs (".global __do_copy_data\n", asm_out_file);
12492
12493 if (avr_need_clear_bss_p)
12494 fputs (".global __do_clear_bss\n", asm_out_file);
12495 }
12496
12497
12498 /* Worker function for `ADJUST_REG_ALLOC_ORDER'. */
12499 /* Choose the order in which to allocate hard registers for
12500 pseudo-registers local to a basic block.
12501
12502 Store the desired register order in the array `reg_alloc_order'.
12503 Element 0 should be the register to allocate first; element 1, the
12504 next register; and so on. */
12505
12506 void
12507 avr_adjust_reg_alloc_order (void)
12508 {
12509 static const int order_0[] =
12510 {
12511 24, 25,
12512 18, 19, 20, 21, 22, 23,
12513 30, 31,
12514 26, 27, 28, 29,
12515 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
12516 0, 1,
12517 32, 33, 34, 35
12518 };
12519 static const int tiny_order_0[] = {
12520 20, 21,
12521 22, 23,
12522 24, 25,
12523 30, 31,
12524 26, 27,
12525 28, 29,
12526 19, 18,
12527 16, 17,
12528 32, 33, 34, 35,
12529 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
12530 };
12531 static const int order_1[] =
12532 {
12533 18, 19, 20, 21, 22, 23, 24, 25,
12534 30, 31,
12535 26, 27, 28, 29,
12536 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
12537 0, 1,
12538 32, 33, 34, 35
12539 };
12540 static const int tiny_order_1[] = {
12541 22, 23,
12542 24, 25,
12543 30, 31,
12544 26, 27,
12545 28, 29,
12546 21, 20, 19, 18,
12547 16, 17,
12548 32, 33, 34, 35,
12549 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
12550 };
12551 static const int order_2[] =
12552 {
12553 25, 24, 23, 22, 21, 20, 19, 18,
12554 30, 31,
12555 26, 27, 28, 29,
12556 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
12557 1, 0,
12558 32, 33, 34, 35
12559 };
12560
12561 /* Select specific register allocation order.
12562 Tiny Core (ATtiny4/5/9/10/20/40) devices have only 16 registers,
12563 so different allocation order should be used. */
12564
12565 const int *order = (TARGET_ORDER_1 ? (AVR_TINY ? tiny_order_1 : order_1)
12566 : TARGET_ORDER_2 ? (AVR_TINY ? tiny_order_0 : order_2)
12567 : (AVR_TINY ? tiny_order_0 : order_0));
12568
12569 for (size_t i = 0; i < ARRAY_SIZE (order_0); ++i)
12570 reg_alloc_order[i] = order[i];
12571 }
12572
12573
12574 /* Implement `TARGET_REGISTER_MOVE_COST' */
12575
12576 static int
12577 avr_register_move_cost (machine_mode /*mode*/, reg_class_t from, reg_class_t to)
12578 {
12579 return (from == STACK_REG ? 6
12580 : to == STACK_REG ? 12
12581 : 2);
12582 }
12583
12584
12585 /* Implement `TARGET_MEMORY_MOVE_COST' */
12586
12587 static int
12588 avr_memory_move_cost (machine_mode mode, reg_class_t /*rclass*/, bool /*in*/)
12589 {
12590 return (mode == QImode ? 2
12591 : mode == HImode ? 4
12592 : mode == SImode ? 8
12593 : mode == SFmode ? 8
12594 : 16);
12595 }
12596
12597
12598 /* Cost for mul highpart. X is a LSHIFTRT, i.e. the outer TRUNCATE is
12599 already stripped off. */
12600
12601 static int
12602 avr_mul_highpart_cost (rtx x, int)
12603 {
12604 if (AVR_HAVE_MUL
12605 && LSHIFTRT == GET_CODE (x)
12606 && MULT == GET_CODE (XEXP (x, 0))
12607 && CONST_INT_P (XEXP (x, 1)))
12608 {
12609 // This is the wider mode.
12610 machine_mode mode = GET_MODE (x);
12611
12612 // The middle-end might still have PR81444, i.e. it is calling the cost
12613 // functions with strange modes. Fix this now by also considering
12614 // PSImode (should actually be SImode instead).
12615 if (HImode == mode || PSImode == mode || SImode == mode)
12616 {
12617 return COSTS_N_INSNS (2);
12618 }
12619 }
12620
12621 return 10000;
12622 }
12623
12624
12625 /* Return the expected cost of a conditional branch like
12626 (set (pc)
12627 (if_then_else (X)
12628 (label_ref *)
12629 (pc)))
12630 where X is some comparison operator. */
12631
12632 static int
12633 avr_cbranch_cost (rtx x)
12634 {
12635 bool difficult_p = difficult_comparison_operator (x, VOIDmode);
12636
12637 if (reload_completed)
12638 {
12639 // After reload, we basically just have plain branches.
12640 return COSTS_N_INSNS (1 + difficult_p);
12641 }
12642
12643 rtx xreg = XEXP (x, 0);
12644 rtx xval = XEXP (x, 1);
12645 machine_mode mode = GET_MODE (xreg);
12646 if (mode == VOIDmode)
12647 mode = GET_MODE (xval);
12648 int size = GET_MODE_SIZE (mode);
12649
12650 if (GET_CODE (xreg) == ZERO_EXTEND
12651 || GET_CODE (xval) == ZERO_EXTEND)
12652 {
12653 // *cbranch<HISI:mode>.<code><QIPSI:mode>.0/1, code = zero_extend.
12654 return COSTS_N_INSNS (size + 1);
12655 }
12656
12657 if (GET_CODE (xreg) == SIGN_EXTEND
12658 || GET_CODE (xval) == SIGN_EXTEND)
12659 {
12660 // *cbranch<HISI:mode>.<code><QIPSI:mode>.0/1, code = sign_extend.
12661 // Make it a bit cheaper than it actually is (less reg pressure).
12662 return COSTS_N_INSNS (size + 1 + 1);
12663 }
12664
12665 if (GET_CODE (xreg) == ZERO_EXTRACT
12666 && XEXP (xreg, 1) == const1_rtx)
12667 {
12668 // Branch on a single bit, with an additional edge due to less
12669 // register pressure.
12670 return (int) COSTS_N_INSNS (1.5);
12671 }
12672
12673 bool reg_p = register_operand (xreg, mode);
12674 bool reg_or_0_p = reg_or_0_operand (xval, mode);
12675
12676 return COSTS_N_INSNS (size
12677 // For the branch
12678 + 1 + difficult_p
12679 // Combine might propagate constants other than zero
12680 // into the 2nd operand. Make that more expensive.
12681 + 1 * (!reg_p || !reg_or_0_p));
12682 }
12683
12684
12685 /* Mutually recursive subroutine of `avr_rtx_cost' for calculating the
12686 cost of an RTX operand given its context. X is the rtx of the
12687 operand, MODE is its mode, and OUTER is the rtx_code of this
12688 operand's parent operator. */
12689
12690 static int
12691 avr_operand_rtx_cost (rtx x, machine_mode mode, rtx_code outer,
12692 int opno, bool speed)
12693 {
12694 rtx_code code = GET_CODE (x);
12695
12696 switch (code)
12697 {
12698 case REG:
12699 case SUBREG:
12700 return 0;
12701
12702 case CONST_INT:
12703 case CONST_FIXED:
12704 case CONST_DOUBLE:
12705 return COSTS_N_INSNS (GET_MODE_SIZE (mode));
12706
12707 default:
12708 break;
12709 }
12710
12711 int total = 0;
12712 avr_rtx_costs (x, mode, outer, opno, &total, speed);
12713 return total;
12714 }
12715
12716
12717 /* Return the default shift costs for an n-byte shift with a constant
12718 bit offset in terms of cycles (speed) or in terms of words (!speed). */
12719
12720 static int
12721 avr_default_shift_costs (int n_bytes, int offset, bool speed)
12722 {
12723 int c_space = 3 + n_bytes;
12724 int c_speed = offset <= 4
12725 ? (3 + n_bytes) * offset
12726 // For larger offsets, don't make the speed costs more costly than
12727 // an unrolled shift, because we cannot rollback from an unrolled shift.
12728 : n_bytes * offset;
12729
12730 return COSTS_N_INSNS (speed ? c_speed : c_space);
12731 }
12732
12733
12734 /* Worker function for AVR backend's rtx_cost function.
12735 X is rtx expression whose cost is to be calculated.
12736 Return true if the complete cost has been computed.
12737 Return false if subexpressions should be scanned.
12738 In either case, *TOTAL contains the cost result. */
12739
12740 static bool
12741 avr_rtx_costs_1 (rtx x, machine_mode mode, int outer_code,
12742 int /*opno*/, int *total, bool speed)
12743 {
12744 const rtx_code code = GET_CODE (x);
12745 const int n_bytes = GET_MODE_SIZE (mode);
12746 const HOST_WIDE_INT val1 = BINARY_P (x) && CONST_INT_P (XEXP (x, 1))
12747 ? INTVAL (XEXP (x, 1))
12748 : -1;
12749
12750 if (avropt_pr118012)
12751 {
12752 if ((code == IOR || code == XOR || code == PLUS)
12753 && GET_CODE (XEXP (x, 0)) == ASHIFT
12754 && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
12755 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == AND
12756 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == const1_rtx)
12757 {
12758 *total = COSTS_N_INSNS (2 + n_bytes);
12759 return true;
12760 }
12761 }
12762
12763 // Insns with nonzero_bits() == 1 in the condition.
12764 if (avropt_use_nonzero_bits
12765 && mode == QImode
12766 && (code == AND || code == IOR || code == XOR)
12767 && REG_P (XEXP (x, 1)))
12768 {
12769 // "*nzb=1.<code>.lsr_split"
12770 // "*nzb=1.<code>.lsr.not_split"
12771 bool is_nzb = (GET_CODE (XEXP (x, 0)) == LSHIFTRT
12772 && (REG_P (XEXP (XEXP (x, 0), 0))
12773 || GET_CODE (XEXP (XEXP (x, 0), 0)) == XOR)
12774 && const_0_to_7_operand (XEXP (XEXP (x, 0), 1), QImode));
12775 // "*nzb=1.<code>.zerox_split"
12776 // "*nzb=1.<code>.zerox.not_split"
12777 is_nzb |= (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
12778 && (REG_P (XEXP (XEXP (x, 0), 0))
12779 || GET_CODE (XEXP (XEXP (x, 0), 0)) == XOR)
12780 && const1_operand (XEXP (XEXP (x, 0), 1), QImode)
12781 && const_0_to_7_operand (XEXP (XEXP (x, 0), 2), QImode));
12782 // "*nzb=1.<code>.ge0_split"
12783 is_nzb |= (GET_CODE (XEXP (x, 0)) == GE
12784 && REG_P (XEXP (XEXP (x, 0), 0))
12785 && const0_operand (XEXP (XEXP (x, 0), 1), QImode));
12786 if (is_nzb)
12787 {
12788 *total = COSTS_N_INSNS (code == XOR ? 3 : 2);
12789 return true;
12790 }
12791 }
12792
12793 // Insn "*nzb=1.ior.ashift_split" with nonzero_bits() == 1 in the condition.
12794 if (avropt_use_nonzero_bits
12795 && mode == QImode
12796 && code == IOR
12797 && REG_P (XEXP (x, 1))
12798 && GET_CODE (XEXP (x, 0)) == ASHIFT
12799 && REG_P (XEXP (XEXP (x, 0), 0))
12800 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
12801 {
12802 *total = COSTS_N_INSNS (2);
12803 return true;
12804 }
12805
12806
12807 switch (code)
12808 {
12809 case CONST_INT:
12810 case CONST_FIXED:
12811 case CONST_DOUBLE:
12812 case SYMBOL_REF:
12813 case CONST:
12814 case LABEL_REF:
12815 /* Immediate constants are as cheap as registers. */
12816 *total = 0;
12817 return true;
12818
12819 case MEM:
12820 *total = COSTS_N_INSNS (n_bytes);
12821 return true;
12822
12823 case NEG:
12824 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
12825 && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT)
12826 {
12827 // Just a sign_extract of bit 0?
12828 rtx y = XEXP (XEXP (x, 0), 0);
12829 if (XEXP (y, 1) == const1_rtx
12830 && XEXP (y, 2) == const0_rtx)
12831 {
12832 *total = COSTS_N_INSNS (1 + n_bytes
12833 - (AVR_HAVE_MOVW && n_bytes == 4));
12834 return true;
12835 }
12836 }
12837
12838 switch (mode)
12839 {
12840 case E_QImode:
12841 case E_SFmode:
12842 *total = COSTS_N_INSNS (1);
12843 break;
12844
12845 case E_HImode:
12846 case E_PSImode:
12847 case E_SImode:
12848 *total = COSTS_N_INSNS (2 * n_bytes - 1);
12849 break;
12850
12851 default:
12852 return false;
12853 }
12854 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
12855 return true;
12856
12857 case ABS:
12858 switch (mode)
12859 {
12860 case E_QImode:
12861 case E_SFmode:
12862 *total = COSTS_N_INSNS (1);
12863 break;
12864
12865 default:
12866 return false;
12867 }
12868 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
12869 return true;
12870
12871 case NOT:
12872 *total = COSTS_N_INSNS (n_bytes);
12873 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
12874 return true;
12875
12876 case ZERO_EXTEND:
12877 *total = COSTS_N_INSNS (n_bytes
12878 - GET_MODE_SIZE (GET_MODE (XEXP (x, 0))));
12879 *total += avr_operand_rtx_cost (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
12880 code, 0, speed);
12881 return true;
12882
12883 case SIGN_EXTEND:
12884 *total = COSTS_N_INSNS (n_bytes + 2
12885 - GET_MODE_SIZE (GET_MODE (XEXP (x, 0))));
12886 *total += avr_operand_rtx_cost (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
12887 code, 0, speed);
12888 return true;
12889
12890 case PLUS:
12891 // uint16_t += 2 * uint8_t;
12892 if (mode == HImode
12893 && GET_CODE (XEXP (x, 0)) == ASHIFT
12894 && REG_P (XEXP (x, 1))
12895 && XEXP (XEXP (x, 0), 1) == const1_rtx
12896 && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
12897 {
12898 *total = COSTS_N_INSNS (4);
12899 return true;
12900 }
12901
12902 // *usum_widenqihi
12903 if (mode == HImode
12904 && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
12905 && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
12906 {
12907 *total = COSTS_N_INSNS (3);
12908 return true;
12909 }
12910 // *aligned_add_symbol
12911 if (mode == HImode
12912 && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
12913 && const_0mod256_operand (XEXP (x, 1), HImode))
12914 {
12915 *total = COSTS_N_INSNS (1.5);
12916 return true;
12917 }
12918
12919 // *add<PSISI:mode>3.zero_extend.<QIPSI:mode>
12920 // *addhi3_zero_extend
12921 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
12922 && REG_P (XEXP (x, 1)))
12923 {
12924 *total = COSTS_N_INSNS (n_bytes);
12925 return true;
12926 }
12927 if (REG_P (XEXP (x, 0))
12928 && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
12929 {
12930 *total = COSTS_N_INSNS (n_bytes);
12931 return true;
12932 }
12933
12934 // *add<HISI:mode>3.sign_extend.<QIPSI:mode>
12935 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
12936 && REG_P (XEXP (x, 1)))
12937 {
12938 int size2 = GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0), 0)));
12939 *total = COSTS_N_INSNS (2 + n_bytes
12940 + (n_bytes > 1 + size2));
12941 return true;
12942 }
12943
12944 switch (mode)
12945 {
12946 case E_QImode:
12947 if (AVR_HAVE_MUL
12948 && MULT == GET_CODE (XEXP (x, 0))
12949 && register_operand (XEXP (x, 1), QImode))
12950 {
12951 /* multiply-add */
12952 *total = COSTS_N_INSNS (speed ? 4 : 3);
12953 /* multiply-add with constant: will be split and load constant. */
12954 if (CONST_INT_P (XEXP (XEXP (x, 0), 1)))
12955 *total = COSTS_N_INSNS (1) + *total;
12956 return true;
12957 }
12958 *total = COSTS_N_INSNS (1);
12959 if (!CONST_INT_P (XEXP (x, 1)))
12960 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1, speed);
12961 break;
12962
12963 case E_HImode:
12964 if (AVR_HAVE_MUL
12965 && (MULT == GET_CODE (XEXP (x, 0))
12966 || ASHIFT == GET_CODE (XEXP (x, 0)))
12967 && register_operand (XEXP (x, 1), HImode)
12968 && (ZERO_EXTEND == GET_CODE (XEXP (XEXP (x, 0), 0))
12969 || SIGN_EXTEND == GET_CODE (XEXP (XEXP (x, 0), 0))))
12970 {
12971 /* multiply-add */
12972 *total = COSTS_N_INSNS (speed ? 5 : 4);
12973 /* multiply-add with constant: will be split and load constant. */
12974 if (CONST_INT_P (XEXP (XEXP (x, 0), 1)))
12975 *total = COSTS_N_INSNS (1) + *total;
12976 return true;
12977 }
12978 if (!CONST_INT_P (XEXP (x, 1)))
12979 {
12980 *total = COSTS_N_INSNS (2);
12981 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
12982 speed);
12983 }
12984 else
12985 *total = COSTS_N_INSNS (2);
12986 break;
12987
12988 case E_PSImode:
12989 if (!CONST_INT_P (XEXP (x, 1)))
12990 {
12991 *total = COSTS_N_INSNS (3);
12992 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
12993 speed);
12994 }
12995 else
12996 *total = COSTS_N_INSNS (3);
12997 break;
12998
12999 case E_SImode:
13000 if (!CONST_INT_P (XEXP (x, 1)))
13001 {
13002 *total = COSTS_N_INSNS (4);
13003 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13004 speed);
13005 }
13006 else
13007 *total = COSTS_N_INSNS (4);
13008 break;
13009
13010 default:
13011 return false;
13012 }
13013 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13014 return true;
13015
13016 case MINUS:
13017 // *udiff_widenqihi
13018 if (mode == HImode
13019 && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
13020 && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
13021 {
13022 *total = COSTS_N_INSNS (2);
13023 return true;
13024 }
13025 // *sub<HISI:mode>3.zero_extend.<QIPSI:mode>
13026 if (REG_P (XEXP (x, 0))
13027 && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
13028 {
13029 *total = COSTS_N_INSNS (n_bytes);
13030 return true;
13031 }
13032 // *sub<HISI:mode>3.sign_extend.<QIPSI:mode>
13033 if (REG_P (XEXP (x, 0))
13034 && GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
13035 {
13036 int size2 = GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 1), 0)));
13037 *total = COSTS_N_INSNS (2 + n_bytes
13038 + (n_bytes > 1 + size2));
13039 return true;
13040 }
13041
13042 if (AVR_HAVE_MUL
13043 && QImode == mode
13044 && register_operand (XEXP (x, 0), QImode)
13045 && MULT == GET_CODE (XEXP (x, 1)))
13046 {
13047 /* multiply-sub */
13048 *total = COSTS_N_INSNS (speed ? 4 : 3);
13049 /* multiply-sub with constant: will be split and load constant. */
13050 if (CONST_INT_P (XEXP (XEXP (x, 1), 1)))
13051 *total = COSTS_N_INSNS (1) + *total;
13052 return true;
13053 }
13054 if (AVR_HAVE_MUL
13055 && HImode == mode
13056 && register_operand (XEXP (x, 0), HImode)
13057 && (MULT == GET_CODE (XEXP (x, 1))
13058 || ASHIFT == GET_CODE (XEXP (x, 1)))
13059 && (ZERO_EXTEND == GET_CODE (XEXP (XEXP (x, 1), 0))
13060 || SIGN_EXTEND == GET_CODE (XEXP (XEXP (x, 1), 0))))
13061 {
13062 /* multiply-sub */
13063 *total = COSTS_N_INSNS (speed ? 5 : 4);
13064 /* multiply-sub with constant: will be split and load constant. */
13065 if (CONST_INT_P (XEXP (XEXP (x, 1), 1)))
13066 *total = COSTS_N_INSNS (1) + *total;
13067 return true;
13068 }
13069 /* FALLTHRU */
13070 case AND:
13071 case IOR:
13072 if (IOR == code
13073 && HImode == mode
13074 && ASHIFT == GET_CODE (XEXP (x, 0)))
13075 {
13076 *total = COSTS_N_INSNS (2);
13077 // Just a rough estimate. If we see no sign- or zero-extend,
13078 // then increase the cost a little bit.
13079 if (REG_P (XEXP (XEXP (x, 0), 0)))
13080 *total += COSTS_N_INSNS (1);
13081 if (REG_P (XEXP (x, 1)))
13082 *total += COSTS_N_INSNS (1);
13083 return true;
13084 }
13085 if (IOR == code
13086 && AND == GET_CODE (XEXP (x, 0))
13087 && AND == GET_CODE (XEXP (x, 1))
13088 && single_zero_operand (XEXP (XEXP (x, 0), 1), mode))
13089 {
13090 // Open-coded bit transfer.
13091 *total = COSTS_N_INSNS (2);
13092 return true;
13093 }
13094 if (AND == code
13095 && single_one_operand (XEXP (x, 1), mode)
13096 && (ASHIFT == GET_CODE (XEXP (x, 0))
13097 || ASHIFTRT == GET_CODE (XEXP (x, 0))
13098 || LSHIFTRT == GET_CODE (XEXP (x, 0))))
13099 {
13100 // "*insv.any_shift.<mode>
13101 *total = COSTS_N_INSNS (1 + n_bytes);
13102 return true;
13103 }
13104 *total = COSTS_N_INSNS (n_bytes);
13105 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13106 if (!CONST_INT_P (XEXP (x, 1)))
13107 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1, speed);
13108 return true;
13109
13110 case XOR:
13111 *total = COSTS_N_INSNS (n_bytes);
13112 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13113 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1, speed);
13114 return true;
13115
13116 case MULT:
13117 if (avropt_pr118012)
13118 {
13119 if (GET_CODE (XEXP (x, 0)) == AND
13120 && XEXP (XEXP (x, 0), 1) == const1_rtx)
13121 {
13122 // Try to defeat PR118012. The MUL variant is actually very
13123 // expensive, but combine is given a pattern to transform this
13124 // into something less toxic. Though this might not work
13125 // for SImode, and we still have a completely ridiculous
13126 // 32-bit multiplication instead of a simple bit test on
13127 // devices that don't even have MUL. This is because on
13128 // AVR_TINY, we'll get a libcall which we cannot undo.
13129 // (On other devices that don't have MUL, the libcall is
13130 // bypassed by providing mulsi3, cf. insn mulsi3_[call_]pr118012.
13131 *total = 0;
13132 return true;
13133 }
13134 } // PR118012
13135
13136 switch (mode)
13137 {
13138 case E_QImode:
13139 if (AVR_HAVE_MUL)
13140 *total = COSTS_N_INSNS (!speed ? 3 : 4);
13141 else if (!speed)
13142 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 2 : 1);
13143 else
13144 return false;
13145 break;
13146
13147 case E_HImode:
13148 if (AVR_HAVE_MUL)
13149 {
13150 rtx op0 = XEXP (x, 0);
13151 rtx op1 = XEXP (x, 1);
13152 rtx_code code0 = GET_CODE (op0);
13153 rtx_code code1 = GET_CODE (op1);
13154 bool ex0 = SIGN_EXTEND == code0 || ZERO_EXTEND == code0;
13155 bool ex1 = SIGN_EXTEND == code1 || ZERO_EXTEND == code1;
13156
13157 if (ex0
13158 && (u8_operand (op1, HImode)
13159 || s8_operand (op1, HImode)))
13160 {
13161 *total = COSTS_N_INSNS (!speed ? 4 : 6);
13162 return true;
13163 }
13164 if (ex0
13165 && register_operand (op1, HImode))
13166 {
13167 *total = COSTS_N_INSNS (!speed ? 5 : 8);
13168 return true;
13169 }
13170 else if (ex0 || ex1)
13171 {
13172 *total = COSTS_N_INSNS (!speed ? 3 : 5);
13173 return true;
13174 }
13175 else if (register_operand (op0, HImode)
13176 && (u8_operand (op1, HImode)
13177 || s8_operand (op1, HImode)))
13178 {
13179 *total = COSTS_N_INSNS (!speed ? 6 : 9);
13180 return true;
13181 }
13182 else
13183 *total = COSTS_N_INSNS (!speed ? 7 : 10);
13184 }
13185 else if (!speed)
13186 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 2 : 1);
13187 else
13188 return false;
13189 break;
13190
13191 case E_PSImode:
13192 if (!speed)
13193 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 2 : 1);
13194 else
13195 *total = 10;
13196 break;
13197
13198 case E_SImode:
13199 case E_DImode:
13200 if (AVR_HAVE_MUL)
13201 {
13202 if (!speed)
13203 {
13204 /* Add some additional costs besides CALL like moves etc. */
13205
13206 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 5 : 4);
13207 }
13208 else
13209 {
13210 /* Just a rough estimate. Even with -O2 we don't want bulky
13211 code expanded inline. */
13212
13213 *total = COSTS_N_INSNS (25);
13214 }
13215 }
13216 else
13217 {
13218 if (speed)
13219 *total = COSTS_N_INSNS (300);
13220 else
13221 /* Add some additional costs besides CALL like moves etc. */
13222 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 5 : 4);
13223 }
13224
13225 if (mode == DImode)
13226 *total *= 2;
13227
13228 return true;
13229
13230 default:
13231 return false;
13232 }
13233 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13234 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1, speed);
13235 return true;
13236
13237 case DIV:
13238 case MOD:
13239 case UDIV:
13240 case UMOD:
13241 if (!speed)
13242 *total = COSTS_N_INSNS (AVR_HAVE_JMP_CALL ? 2 : 1);
13243 else
13244 *total = COSTS_N_INSNS (15 * n_bytes);
13245 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13246 /* For div/mod with const-int divisor we have at least the cost of
13247 loading the divisor. */
13248 if (CONST_INT_P (XEXP (x, 1)))
13249 *total += COSTS_N_INSNS (n_bytes);
13250 /* Add some overall penaly for clobbering and moving around registers */
13251 *total += COSTS_N_INSNS (2);
13252 return true;
13253
13254 case ROTATE:
13255 switch (mode)
13256 {
13257 case E_QImode:
13258 if (val1 == 4)
13259 *total = COSTS_N_INSNS (1);
13260 break;
13261
13262 case E_HImode:
13263 if (val1 == 8)
13264 *total = COSTS_N_INSNS (3);
13265 break;
13266
13267 case E_SImode:
13268 if (CONST_INT_P (XEXP (x, 1)))
13269 switch (val1)
13270 {
13271 case 8:
13272 case 24:
13273 *total = COSTS_N_INSNS (5);
13274 break;
13275 case 16:
13276 *total = COSTS_N_INSNS (AVR_HAVE_MOVW ? 4 : 6);
13277 break;
13278 }
13279 break;
13280
13281 default:
13282 return false;
13283 }
13284 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13285 return true;
13286
13287 case ASHIFT:
13288 switch (mode)
13289 {
13290 case E_QImode: // ashlqi3
13291 if (speed
13292 && XEXP (x, 0) == const1_rtx
13293 && GET_CODE (XEXP (x, 1)) == AND)
13294 {
13295 // "*mask1_0x01"
13296 // Leave the space costs alone as they are smaller than 7 here.
13297 *total = COSTS_N_INSNS (7);
13298 return true;
13299 }
13300
13301 if (!CONST_INT_P (XEXP (x, 1)))
13302 {
13303 *total = COSTS_N_INSNS (!speed ? 4 : 17);
13304 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13305 speed);
13306 }
13307 else
13308 {
13309 if (val1 == 7)
13310 *total = COSTS_N_INSNS (3);
13311 else if (val1 == 6)
13312 *total = COSTS_N_INSNS (5 - AVR_HAVE_MUL);
13313 else if (val1 >= 0 && val1 <= 5)
13314 *total = COSTS_N_INSNS (val1);
13315 else
13316 *total = COSTS_N_INSNS (1);
13317 }
13318 break;
13319
13320 case E_HImode: // ashlhi3
13321 if (AVR_HAVE_MUL)
13322 {
13323 if (const_2_to_7_operand (XEXP (x, 1), HImode)
13324 && (SIGN_EXTEND == GET_CODE (XEXP (x, 0))
13325 || ZERO_EXTEND == GET_CODE (XEXP (x, 0))))
13326 {
13327 *total = COSTS_N_INSNS (!speed ? 4 : 6);
13328 return true;
13329 }
13330 }
13331
13332 if (const1_rtx == (XEXP (x, 1))
13333 && SIGN_EXTEND == GET_CODE (XEXP (x, 0)))
13334 {
13335 *total = COSTS_N_INSNS (2);
13336 return true;
13337 }
13338
13339 if (!CONST_INT_P (XEXP (x, 1)))
13340 {
13341 *total = COSTS_N_INSNS (!speed ? 5 : 41);
13342 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13343 speed);
13344 }
13345 else
13346 switch (val1)
13347 {
13348 case 0:
13349 *total = 0;
13350 break;
13351 case 1:
13352 case 8:
13353 *total = COSTS_N_INSNS (2);
13354 break;
13355 case 9:
13356 *total = COSTS_N_INSNS (3);
13357 break;
13358 case 2:
13359 case 3:
13360 case 10:
13361 case 15:
13362 *total = COSTS_N_INSNS (4);
13363 break;
13364 case 7:
13365 case 11:
13366 case 12:
13367 *total = COSTS_N_INSNS (5);
13368 break;
13369 case 4:
13370 *total = COSTS_N_INSNS (!speed ? 5 : 8);
13371 break;
13372 case 6:
13373 *total = COSTS_N_INSNS (!speed ? 5 : 9);
13374 break;
13375 case 5:
13376 *total = COSTS_N_INSNS (!speed ? 5 : 10);
13377 break;
13378 default:
13379 *total = avr_default_shift_costs (n_bytes, val1, speed);
13380 break;
13381 }
13382 break;
13383
13384 case E_PSImode: // ashlpsi3
13385 if (!CONST_INT_P (XEXP (x, 1)))
13386 {
13387 *total = COSTS_N_INSNS (!speed ? 6 : 73);
13388 }
13389 else
13390 switch (val1)
13391 {
13392 case 0:
13393 *total = 0;
13394 break;
13395 case 1:
13396 case 8:
13397 case 16:
13398 *total = COSTS_N_INSNS (3);
13399 break;
13400 case 9:
13401 case 15:
13402 *total = COSTS_N_INSNS (6);
13403 break;
13404 case 23:
13405 *total = COSTS_N_INSNS (5);
13406 break;
13407 default:
13408 *total = avr_default_shift_costs (n_bytes, val1, speed);
13409 break;
13410 }
13411 break;
13412
13413 case E_SImode: // ashlsi3
13414 if (!CONST_INT_P (XEXP (x, 1)))
13415 {
13416 *total = COSTS_N_INSNS (!speed ? 7 : 113);
13417 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13418 speed);
13419 }
13420 else
13421 switch (val1)
13422 {
13423 case 0:
13424 *total = 0;
13425 break;
13426 case 1:
13427 case 8:
13428 *total = COSTS_N_INSNS (4);
13429 break;
13430 case 15:
13431 *total = COSTS_N_INSNS (8 - AVR_HAVE_MOVW);
13432 break;
13433 case 16:
13434 *total = COSTS_N_INSNS (4 - AVR_HAVE_MOVW);
13435 break;
13436 case 24:
13437 case 25:
13438 case 26:
13439 case 27:
13440 *total = COSTS_N_INSNS (4 + val1 - 24);
13441 break;
13442 case 28:
13443 case 29:
13444 *total = COSTS_N_INSNS (6 + val1 - 28);
13445 break;
13446 case 30:
13447 *total = COSTS_N_INSNS (!speed && AVR_HAVE_MUL ? 7 : 8);
13448 break;
13449 case 31:
13450 *total = COSTS_N_INSNS (6);
13451 break;
13452 case 2:
13453 *total = COSTS_N_INSNS (!speed ? 7 : 8);
13454 break;
13455 default:
13456 *total = avr_default_shift_costs (n_bytes, val1, speed);
13457 break;
13458 }
13459 break;
13460
13461 default:
13462 return false;
13463 }
13464 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13465 return true;
13466
13467 case ASHIFTRT:
13468 switch (mode)
13469 {
13470 case E_QImode: // ashrqi3
13471 if (!CONST_INT_P (XEXP (x, 1)))
13472 {
13473 *total = COSTS_N_INSNS (!speed ? 4 : 17);
13474 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13475 speed);
13476 }
13477 else
13478 {
13479 if (val1 == 6)
13480 *total = COSTS_N_INSNS (4);
13481 else if (val1 == 7)
13482 *total = COSTS_N_INSNS (2);
13483 else if (val1 >= 0 && val1 <= 5)
13484 *total = COSTS_N_INSNS (val1);
13485 else
13486 *total = COSTS_N_INSNS (1);
13487 }
13488 break;
13489
13490 case E_HImode: // ashrhi3
13491 if (CONST_INT_P (XEXP (x, 0))
13492 && INTVAL (XEXP (x, 0)) == 128
13493 && GET_CODE (XEXP (x, 1)) == AND)
13494 {
13495 // "*mask1_0x80"
13496 *total = COSTS_N_INSNS (7);
13497 return true;
13498 }
13499
13500 if (!CONST_INT_P (XEXP (x, 1)))
13501 {
13502 *total = COSTS_N_INSNS (!speed ? 5 : 41);
13503 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13504 speed);
13505 }
13506 else
13507 switch (val1)
13508 {
13509 case 0:
13510 *total = 0;
13511 break;
13512 case 1:
13513 *total = COSTS_N_INSNS (2);
13514 break;
13515 case 15:
13516 *total = COSTS_N_INSNS (3);
13517 break;
13518 case 2:
13519 case 7:
13520 case 8:
13521 case 9:
13522 *total = COSTS_N_INSNS (4);
13523 break;
13524 case 10:
13525 case 14:
13526 *total = COSTS_N_INSNS (5);
13527 break;
13528 case 11:
13529 *total = COSTS_N_INSNS (!speed ? 5 : 6);
13530 break;
13531 case 12:
13532 *total = COSTS_N_INSNS (!speed ? 5 : 7);
13533 break;
13534 case 6:
13535 case 13:
13536 *total = COSTS_N_INSNS (!speed ? 5 : 8);
13537 break;
13538 default:
13539 *total = avr_default_shift_costs (n_bytes, val1, speed);
13540 break;
13541 }
13542 break;
13543
13544 case E_PSImode: // ashrpsi3
13545 if (!CONST_INT_P (XEXP (x, 1)))
13546 {
13547 *total = COSTS_N_INSNS (!speed ? 6 : 73);
13548 }
13549 else
13550 switch (val1)
13551 {
13552 case 0:
13553 *total = 0;
13554 break;
13555 case 1:
13556 *total = COSTS_N_INSNS (3);
13557 break;
13558 case 8:
13559 case 15:
13560 *total = COSTS_N_INSNS (5);
13561 break;
13562 case 16:
13563 *total = COSTS_N_INSNS (4);
13564 break;
13565 case 22:
13566 *total = COSTS_N_INSNS (6);
13567 break;
13568 case 23:
13569 *total = COSTS_N_INSNS (4);
13570 break;
13571 default:
13572 *total = avr_default_shift_costs (n_bytes, val1, speed);
13573 break;
13574 }
13575 break;
13576
13577 case E_SImode: // ashrsi3
13578 if (!CONST_INT_P (XEXP (x, 1)))
13579 {
13580 *total = COSTS_N_INSNS (!speed ? 7 : 113);
13581 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13582 speed);
13583 }
13584 else
13585 switch (val1)
13586 {
13587 case 0:
13588 *total = 0;
13589 break;
13590 case 1:
13591 *total = COSTS_N_INSNS (4);
13592 break;
13593 case 8:
13594 *total = COSTS_N_INSNS (6);
13595 break;
13596 case 15:
13597 *total = COSTS_N_INSNS (6 - AVR_HAVE_MOVW);
13598 break;
13599 case 16:
13600 *total = COSTS_N_INSNS (4 - AVR_HAVE_MOVW);
13601 break;
13602 case 24:
13603 *total = COSTS_N_INSNS (5);
13604 break;
13605 case 2:
13606 *total = COSTS_N_INSNS (!speed ? 7 : 8);
13607 break;
13608 case 30:
13609 *total = COSTS_N_INSNS (7 - AVR_HAVE_MOVW);
13610 break;
13611 case 31:
13612 *total = COSTS_N_INSNS (AVR_HAVE_MOVW ? 4 : 5);
13613 break;
13614 default:
13615 *total = avr_default_shift_costs (n_bytes, val1, speed);
13616 break;
13617 }
13618 break;
13619
13620 default:
13621 return false;
13622 }
13623 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13624 return true;
13625
13626 case LSHIFTRT:
13627 if (outer_code == TRUNCATE)
13628 {
13629 *total = avr_mul_highpart_cost (x, speed);
13630 return true;
13631 }
13632
13633 switch (mode)
13634 {
13635 case E_QImode: // lshrqi3
13636 if (!CONST_INT_P (XEXP (x, 1)))
13637 {
13638 *total = COSTS_N_INSNS (!speed ? 4 : 17);
13639 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13640 speed);
13641 }
13642 else
13643 {
13644 if (val1 == 7)
13645 *total = COSTS_N_INSNS (3);
13646 else if (val1 == 6)
13647 *total = COSTS_N_INSNS (5 - AVR_HAVE_MUL);
13648 else if (val1 >= 0 && val1 <= 5)
13649 *total = COSTS_N_INSNS (val1);
13650 else
13651 *total = COSTS_N_INSNS (1);
13652 }
13653 break;
13654
13655 case E_HImode: // lshrhi3
13656 if (!CONST_INT_P (XEXP (x, 1)))
13657 {
13658 *total = COSTS_N_INSNS (!speed ? 5 : 41);
13659 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13660 speed);
13661 }
13662 else
13663 switch (val1)
13664 {
13665 case 0:
13666 *total = 0;
13667 break;
13668 case 1:
13669 case 8:
13670 *total = COSTS_N_INSNS (2);
13671 break;
13672 case 9:
13673 *total = COSTS_N_INSNS (3);
13674 break;
13675 case 2:
13676 case 10:
13677 case 15:
13678 *total = COSTS_N_INSNS (4);
13679 break;
13680 case 7:
13681 case 11:
13682 *total = COSTS_N_INSNS (5);
13683 break;
13684 case 3:
13685 case 12:
13686 case 13:
13687 case 14:
13688 *total = COSTS_N_INSNS (!speed ? 5 : 6);
13689 break;
13690 case 4:
13691 *total = COSTS_N_INSNS (!speed ? 5 : 7);
13692 break;
13693 case 5:
13694 case 6:
13695 *total = COSTS_N_INSNS (!speed ? 5 : 9);
13696 break;
13697 default:
13698 *total = avr_default_shift_costs (n_bytes, val1, speed);
13699 break;
13700 }
13701 break;
13702
13703 case E_PSImode: // lshrpsi3
13704 if (!CONST_INT_P (XEXP (x, 1)))
13705 {
13706 *total = COSTS_N_INSNS (!speed ? 6 : 73);
13707 }
13708 else
13709 switch (val1)
13710 {
13711 case 0:
13712 *total = 0;
13713 break;
13714 case 1:
13715 case 8:
13716 case 16:
13717 *total = COSTS_N_INSNS (3);
13718 break;
13719 case 15:
13720 *total = COSTS_N_INSNS (6);
13721 break;
13722 case 23:
13723 *total = COSTS_N_INSNS (5);
13724 break;
13725 default:
13726 *total = avr_default_shift_costs (n_bytes, val1, speed);
13727 break;
13728 }
13729 break;
13730
13731 case E_SImode: // lshrsi3
13732 if (!CONST_INT_P (XEXP (x, 1)))
13733 {
13734 *total = COSTS_N_INSNS (!speed ? 7 : 113);
13735 *total += avr_operand_rtx_cost (XEXP (x, 1), mode, code, 1,
13736 speed);
13737 }
13738 else
13739 switch (val1)
13740 {
13741 case 0:
13742 *total = 0;
13743 break;
13744 case 1:
13745 case 8:
13746 *total = COSTS_N_INSNS (4);
13747 break;
13748 case 2:
13749 *total = COSTS_N_INSNS (!speed ? 7 : 8);
13750 break;
13751 case 15:
13752 *total = COSTS_N_INSNS (8 - AVR_HAVE_MOVW);
13753 break;
13754 case 16:
13755 *total = COSTS_N_INSNS (4 - AVR_HAVE_MOVW);
13756 break;
13757 case 24:
13758 case 25:
13759 case 26:
13760 case 27:
13761 *total = COSTS_N_INSNS (4 + val1 - 24);
13762 break;
13763 case 28:
13764 case 29:
13765 *total = COSTS_N_INSNS (6 + val1 - 28);
13766 break;
13767 case 30:
13768 *total = COSTS_N_INSNS (!speed && AVR_HAVE_MUL ? 7 : 8);
13769 break;
13770 case 31:
13771 *total = COSTS_N_INSNS (6);
13772 break;
13773 default:
13774 *total = avr_default_shift_costs (n_bytes, val1, speed);
13775 break;
13776 }
13777 break;
13778
13779 default:
13780 return false;
13781 }
13782 *total += avr_operand_rtx_cost (XEXP (x, 0), mode, code, 0, speed);
13783 return true;
13784
13785 case GE:
13786 if (mode == QImode
13787 && REG_P (XEXP (x, 0))
13788 && XEXP (x, 1) == const0_rtx)
13789 {
13790 *total = COSTS_N_INSNS (3);
13791 return true;
13792 }
13793 break;
13794
13795 case ZERO_EXTRACT:
13796 if (mode == QImode
13797 && REG_P (XEXP (x, 0))
13798 && XEXP (x, 1) == const1_rtx
13799 && CONST_INT_P (XEXP (x, 2)))
13800 {
13801 int bpos = INTVAL (XEXP (x, 2));
13802 *total = COSTS_N_INSNS (bpos == 0 ? 1 : bpos == 1 ? 2 : 3);
13803 return true;
13804 }
13805 break;
13806
13807 case COMPARE:
13808 switch (GET_MODE (XEXP (x, 0)))
13809 {
13810 case E_QImode:
13811 *total = COSTS_N_INSNS (1);
13812 if (!CONST_INT_P (XEXP (x, 1)))
13813 *total += avr_operand_rtx_cost (XEXP (x, 1), QImode, code,
13814 1, speed);
13815 break;
13816
13817 case E_HImode:
13818 *total = COSTS_N_INSNS (2);
13819 if (!CONST_INT_P (XEXP (x, 1)))
13820 *total += avr_operand_rtx_cost (XEXP (x, 1), HImode, code,
13821 1, speed);
13822 else if (INTVAL (XEXP (x, 1)) != 0)
13823 *total += COSTS_N_INSNS (1);
13824 break;
13825
13826 case E_PSImode:
13827 *total = COSTS_N_INSNS (3);
13828 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
13829 *total += COSTS_N_INSNS (2);
13830 break;
13831
13832 case E_SImode:
13833 *total = COSTS_N_INSNS (4);
13834 if (!CONST_INT_P (XEXP (x, 1)))
13835 *total += avr_operand_rtx_cost (XEXP (x, 1), SImode, code,
13836 1, speed);
13837 else if (INTVAL (XEXP (x, 1)) != 0)
13838 *total += COSTS_N_INSNS (3);
13839 break;
13840
13841 default:
13842 return false;
13843 }
13844 *total += avr_operand_rtx_cost (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
13845 code, 0, speed);
13846 return true;
13847
13848 case TRUNCATE:
13849 if (LSHIFTRT == GET_CODE (XEXP (x, 0)))
13850 {
13851 *total = avr_mul_highpart_cost (XEXP (x, 0), speed);
13852 return true;
13853 }
13854 break;
13855
13856 case IF_THEN_ELSE:
13857 if (outer_code == SET
13858 && XEXP (x, 2) == pc_rtx
13859 && ordered_comparison_operator (XEXP (x, 0), VOIDmode))
13860 {
13861 *total = avr_cbranch_cost (XEXP (x, 0));
13862 return true;
13863 }
13864
13865 default:
13866 break;
13867 }
13868 return false;
13869 }
13870
13871
13872 /* Implement `TARGET_RTX_COSTS'. */
13873
13874 static bool
13875 avr_rtx_costs (rtx x, machine_mode mode, int outer_code,
13876 int opno, int *total, bool speed)
13877 {
13878 bool done = avr_rtx_costs_1 (x, mode, outer_code, opno, total, speed);
13879
13880 if (avr_log.rtx_costs)
13881 {
13882 avr_edump ("\n%?=%b (%s) total=%d, outer=%C:\n%r\n",
13883 done, speed ? "speed" : "size", *total, outer_code, x);
13884 }
13885
13886 return done;
13887 }
13888
13889
13890 /* Implement `TARGET_INSN_COST'. */
13891 /* For some insns, it is not enough to look at the cost of the SET_SRC.
13892 In that case, have a look at the entire insn, e.g. during insn combine. */
13893
13894 static int
13895 avr_insn_cost (rtx_insn *insn, bool speed)
13896 {
13897 const int unknown_cost = -1;
13898 int cost = unknown_cost;
13899
13900 rtx set = single_set (insn);
13901
13902 if (set
13903 && ZERO_EXTRACT == GET_CODE (SET_DEST (set)))
13904 {
13905 // Try find anything that would flip the extracted bit.
13906 bool not_bit_p = false;
13907
13908 subrtx_iterator::array_type array;
13909 FOR_EACH_SUBRTX (iter, array, SET_SRC (set), NONCONST)
13910 {
13911 rtx_code code = GET_CODE (*iter);
13912 not_bit_p |= code == NOT || code == XOR || code == GE;
13913 }
13914
13915 // Don't go too deep into the analysis. In almost all cases,
13916 // using BLD/BST is the best we can do for single-bit moves,
13917 // even considering CSE.
13918 cost = COSTS_N_INSNS (2 + not_bit_p);
13919 }
13920
13921 if (cost != unknown_cost)
13922 {
13923 if (avr_log.rtx_costs)
13924 avr_edump ("\n%? (%s) insn_cost=%d\n%r\n",
13925 speed ? "speed" : "size", cost, insn);
13926 return cost;
13927 }
13928
13929 // Resort to what rtlanal.cc::insn_cost() implements as a default
13930 // when targetm.insn_cost() is not implemented.
13931
13932 return pattern_cost (PATTERN (insn), speed);
13933 }
13934
13935
13936 /* Implement `TARGET_ADDRESS_COST'. */
13937
13938 static int
13939 avr_address_cost (rtx x, machine_mode mode, addr_space_t /*as*/,
13940 bool /*speed*/)
13941 {
13942 int cost = 4;
13943
13944 if (GET_CODE (x) == PLUS
13945 && CONST_INT_P (XEXP (x, 1))
13946 && (REG_P (XEXP (x, 0))
13947 || SUBREG_P (XEXP (x, 0))))
13948 {
13949 if (INTVAL (XEXP (x, 1)) > MAX_LD_OFFSET(mode))
13950 cost = 18;
13951 }
13952 else if (CONSTANT_ADDRESS_P (x))
13953 {
13954 if (io_address_operand (x, QImode))
13955 cost = 2;
13956
13957 if (AVR_TINY
13958 && avr_address_tiny_absdata_p (x, QImode))
13959 cost = 2;
13960 }
13961
13962 if (avr_log.address_cost)
13963 avr_edump ("\n%?: %d = %r\n", cost, x);
13964
13965 return cost;
13966 }
13967
13968
13969 /* Test for extra memory constraint 'Q'.
13970 It's a memory address based on Y or Z pointer with valid displacement. */
13971
13972 bool
13973 extra_constraint_Q (rtx x)
13974 {
13975 bool ok = false;
13976 rtx plus = XEXP (x, 0);
13977
13978 if (GET_CODE (plus) == PLUS
13979 && REG_P (XEXP (plus, 0))
13980 && CONST_INT_P (XEXP (plus, 1))
13981 && (INTVAL (XEXP (plus, 1))
13982 <= MAX_LD_OFFSET (GET_MODE (x))))
13983 {
13984 rtx xx = XEXP (plus, 0);
13985 int regno = REGNO (xx);
13986
13987 ok = (/* allocate pseudos */
13988 regno >= FIRST_PSEUDO_REGISTER
13989 /* strictly check */
13990 || regno == REG_Z || regno == REG_Y
13991 /* XXX frame & arg pointer checks */
13992 || xx == frame_pointer_rtx
13993 || xx == arg_pointer_rtx);
13994
13995 if (avr_log.constraints)
13996 avr_edump ("\n%?=%d reload_completed=%d ra_in_progress=%d\n %r\n",
13997 ok, reload_completed, ra_in_progress (), x);
13998 }
13999
14000 return ok;
14001 }
14002
14003
14004 /* Convert condition code CONDITION to the valid AVR condition code. */
14005
14006 rtx_code
14007 avr_normalize_condition (rtx_code condition)
14008 {
14009 switch (condition)
14010 {
14011 case GT:
14012 return GE;
14013 case GTU:
14014 return GEU;
14015 case LE:
14016 return LT;
14017 case LEU:
14018 return LTU;
14019 default:
14020 gcc_unreachable ();
14021 }
14022 }
14023
14024
14025 /* Returns register number for function return value.*/
14026
14027 static inline unsigned int
14028 avr_ret_register (void)
14029 {
14030 return REG_24;
14031 }
14032
14033
14034 /* Implement `TARGET_FUNCTION_VALUE_REGNO_P'. */
14035
14036 static bool
14037 avr_function_value_regno_p (const unsigned int regno)
14038 {
14039 return regno == avr_ret_register ();
14040 }
14041
14042
14043 /* Implement `TARGET_LIBCALL_VALUE'. */
14044 /* Create an RTX representing the place where a
14045 library function returns a value of mode MODE. */
14046
14047 static rtx
14048 avr_libcall_value (machine_mode mode, const_rtx /*func*/)
14049 {
14050 int offs = GET_MODE_SIZE (mode);
14051
14052 if (offs <= 4)
14053 offs = (offs + 1) & ~1;
14054
14055 return gen_rtx_REG (mode, avr_ret_register () + 2 - offs);
14056 }
14057
14058
14059 /* Implement `TARGET_FUNCTION_VALUE'. */
14060 /* Create an RTX representing the place where a
14061 function returns a value of data type VALTYPE. */
14062
14063 static rtx
14064 avr_function_value (const_tree type, const_tree /*fn_decl_or_type*/,
14065 bool /*outgoing*/)
14066 {
14067 if (TYPE_MODE (type) != BLKmode)
14068 return avr_libcall_value (TYPE_MODE (type), NULL_RTX);
14069
14070 unsigned int offs = int_size_in_bytes (type);
14071 if (offs < 2)
14072 offs = 2;
14073 if (offs > 2 && offs < GET_MODE_SIZE (SImode))
14074 offs = GET_MODE_SIZE (SImode);
14075 else if (offs > GET_MODE_SIZE (SImode) && offs < GET_MODE_SIZE (DImode))
14076 offs = GET_MODE_SIZE (DImode);
14077
14078 return gen_rtx_REG (BLKmode, avr_ret_register () + 2 - offs);
14079 }
14080
14081
14082 bool
14083 test_hard_reg_class (reg_class rclass, rtx x)
14084 {
14085 int regno = true_regnum (x);
14086 if (regno < 0)
14087 return false;
14088
14089 if (TEST_HARD_REG_CLASS (rclass, regno))
14090 return true;
14091
14092 return false;
14093 }
14094
14095
14096 /* Helper for `jump_over_one_insn_p': Test if INSN is a 2-word instruction
14097 and thus is suitable to be skipped by CPSE, SBRC, etc. */
14098
14099 static bool
14100 avr_2word_insn_p (rtx_insn *insn)
14101 {
14102 if (TARGET_SKIP_BUG || !insn || get_attr_length (insn) != 2)
14103 {
14104 return false;
14105 }
14106
14107 switch (INSN_CODE (insn))
14108 {
14109 default:
14110 return (recog_memoized (insn) >= 0
14111 // Transparent calls may be skipped.
14112 && (get_attr_type (insn) == TYPE_XCALL
14113 || get_attr_adjust_len (insn) == ADJUST_LEN_CALL));
14114
14115 case CODE_FOR_movqi_insn:
14116 case CODE_FOR_movuqq_insn:
14117 case CODE_FOR_movqq_insn:
14118 {
14119 rtx set = single_set (insn);
14120 rtx src = SET_SRC (set);
14121 rtx dest = SET_DEST (set);
14122
14123 /* Factor out LDS and STS from movqi_insn. */
14124
14125 if (MEM_P (dest)
14126 && (REG_P (src) || src == CONST0_RTX (GET_MODE (dest))))
14127 {
14128 return CONSTANT_ADDRESS_P (XEXP (dest, 0));
14129 }
14130 else if (REG_P (dest)
14131 && MEM_P (src))
14132 {
14133 return CONSTANT_ADDRESS_P (XEXP (src, 0));
14134 }
14135
14136 return false;
14137 }
14138
14139 case CODE_FOR_call_insn:
14140 case CODE_FOR_call_value_insn:
14141 return true;
14142 }
14143 }
14144
14145
14146 bool
14147 jump_over_one_insn_p (rtx_insn *insn, rtx dest)
14148 {
14149 int uid = INSN_UID (GET_CODE (dest) == LABEL_REF
14150 ? XEXP (dest, 0)
14151 : dest);
14152 int jump_addr = INSN_ADDRESSES (INSN_UID (insn));
14153 int dest_addr = INSN_ADDRESSES (uid);
14154 int jump_offset = dest_addr - jump_addr - get_attr_length (insn);
14155
14156 return (jump_offset == 1
14157 || (jump_offset == 2
14158 && avr_2word_insn_p (next_active_insn (insn))));
14159 }
14160
14161
14162 /* Implement `TARGET_HARD_REGNO_NREGS'. CCmode is four units for historical
14163 reasons. If this hook is not defined, TARGET_HARD_REGNO_NREGS
14164 reports that CCmode requires four registers.
14165 Define this hook to allow CCmode to fit in a single REG_CC. For
14166 other modes and regs, return the number of words in mode (i.e whatever
14167 the default implementation of the hook returned). */
14168
14169 static unsigned int
14170 avr_hard_regno_nregs (unsigned int regno, machine_mode mode)
14171 {
14172 if (regno == REG_CC && GET_MODE_CLASS (mode) == MODE_CC)
14173 return 1;
14174
14175 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
14176 }
14177
14178
14179 /* Implement `TARGET_HARD_REGNO_MODE_OK'. On the enhanced core, anything
14180 larger than 1 byte must start in even numbered register for "movw" to
14181 work (this way we don't have to check for odd registers everywhere). */
14182
14183 static bool
14184 avr_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
14185 {
14186 if (regno == REG_CC)
14187 return GET_MODE_CLASS (mode) == MODE_CC;
14188
14189 /* NOTE: 8-bit values must not be disallowed for R28 or R29.
14190 Disallowing QI et al. in these regs might lead to code like
14191 (set (subreg:QI (reg:HI 28) n) ...)
14192 which will result in wrong code because reload does not
14193 handle SUBREGs of hard regsisters like this.
14194 This could be fixed in reload. However, it appears
14195 that fixing reload is not wanted by reload people. */
14196
14197 /* Any GENERAL_REGS register can hold 8-bit values. */
14198
14199 if (GET_MODE_SIZE (mode) == 1)
14200 return true;
14201
14202 /* FIXME: Ideally, the following test is not needed.
14203 However, it turned out that it can reduce the number
14204 of spill fails. AVR and it's poor endowment with
14205 address registers is extreme stress test for reload. */
14206
14207 if (GET_MODE_SIZE (mode) >= 4
14208 && regno >= REG_X
14209 // This problem only concerned the old reload.
14210 && ! avropt_lra_p)
14211 return false;
14212
14213 /* All modes larger than 8 bits should start in an even register. */
14214
14215 return !(regno & 1);
14216 }
14217
14218
14219 /* Implement `TARGET_HARD_REGNO_CALL_PART_CLOBBERED'. */
14220
14221 static bool
14222 avr_hard_regno_call_part_clobbered (unsigned, unsigned regno,
14223 machine_mode mode)
14224 {
14225 /* FIXME: This hook gets called with MODE:REGNO combinations that don't
14226 represent valid hard registers like, e.g. HI:29. Returning TRUE
14227 for such registers can lead to performance degradation as mentioned
14228 in PR53595. Thus, report invalid hard registers as FALSE. */
14229
14230 if (!avr_hard_regno_mode_ok (regno, mode))
14231 return false;
14232
14233 /* Return true if any of the following boundaries is crossed:
14234 17/18 or 19/20 (if AVR_TINY), 27/28 and 29/30. */
14235
14236 return ((regno <= LAST_CALLEE_SAVED_REG
14237 && regno + GET_MODE_SIZE (mode) > 1 + LAST_CALLEE_SAVED_REG)
14238 || (regno < REG_Y && regno + GET_MODE_SIZE (mode) > REG_Y)
14239 || (regno < REG_Z && regno + GET_MODE_SIZE (mode) > REG_Z));
14240 }
14241
14242
14243 /* Implement `MODE_CODE_BASE_REG_CLASS'. */
14244
14245 reg_class
14246 avr_mode_code_base_reg_class (machine_mode /*mode*/, addr_space_t as,
14247 rtx_code outer_code, rtx_code /*index_code*/)
14248 {
14249 if (!ADDR_SPACE_GENERIC_P (as))
14250 {
14251 return POINTER_Z_REGS;
14252 }
14253
14254 if (AVR_TINY)
14255 // We allow all offsets for all pointer regs. Pass .avr-fuse-add
14256 // will rectify it (register allocation cannot do it).
14257 return POINTER_REGS;
14258
14259 if (!avropt_strict_X)
14260 return reload_completed ? BASE_POINTER_REGS : POINTER_REGS;
14261
14262 return PLUS == outer_code ? BASE_POINTER_REGS : POINTER_REGS;
14263 }
14264
14265
14266 /* Implement `REGNO_MODE_CODE_OK_FOR_BASE_P'. */
14267
14268 bool
14269 avr_regno_mode_code_ok_for_base_p (int regno, machine_mode /*mode*/,
14270 addr_space_t as, rtx_code outer_code,
14271 rtx_code /*index_code*/)
14272 {
14273 bool ok = false;
14274
14275 if (!ADDR_SPACE_GENERIC_P (as))
14276 {
14277 if (regno < FIRST_PSEUDO_REGISTER
14278 && regno == REG_Z)
14279 {
14280 return true;
14281 }
14282
14283 if (reg_renumber)
14284 {
14285 regno = reg_renumber[regno];
14286
14287 if (regno == REG_Z)
14288 {
14289 return true;
14290 }
14291 }
14292
14293 return false;
14294 }
14295
14296 if (regno < FIRST_PSEUDO_REGISTER
14297 && (regno == REG_X
14298 || regno == REG_Y
14299 || regno == REG_Z
14300 || regno == ARG_POINTER_REGNUM))
14301 {
14302 ok = true;
14303 }
14304 else if (reg_renumber)
14305 {
14306 regno = reg_renumber[regno];
14307
14308 if (regno == REG_X
14309 || regno == REG_Y
14310 || regno == REG_Z
14311 || regno == ARG_POINTER_REGNUM)
14312 {
14313 ok = true;
14314 }
14315 }
14316
14317 if (avropt_strict_X
14318 // On Reduced Tiny, all registers are equal in that they do not
14319 // support PLUS addressing; respective addresses will be fake,
14320 // even for the frame pointer. They must be handled in the
14321 // printers by add-store-sub sequences -- or may be split after
14322 // reload by `avr_split_tiny_move'.
14323 && ! AVR_TINY
14324 && PLUS == outer_code
14325 && regno == REG_X)
14326 {
14327 ok = false;
14328 }
14329
14330 return ok;
14331 }
14332
14333
14334 /* Reload the constant OP[1] into the HI register OP[0].
14335 CLOBBER_REG is a QI clobber reg needed to move vast majority of consts
14336 into a NO_LD_REGS register. If CLOBBER_REG is NULL_RTX we either don't
14337 need a clobber reg or have to cook one up.
14338
14339 PLEN == NULL: Output instructions.
14340 PLEN != NULL: Output nothing. Set *PLEN to number of words occupied
14341 by the insns printed.
14342
14343 Return "". */
14344
14345 const char *
14346 output_reload_inhi (rtx *op, rtx clobber_reg, int *plen)
14347 {
14348 output_reload_in_const (op, clobber_reg, plen, false);
14349 return "";
14350 }
14351
14352
14353 /* Reload a SI or SF compile time constant OP[1] into the register OP[0].
14354 CLOBBER_REG is a QI clobber reg needed to move vast majority of consts
14355 into a NO_LD_REGS register. If CLOBBER_REG is NULL_RTX we either don't
14356 need a clobber reg or have to cook one up.
14357
14358 LEN == NULL: Output instructions.
14359
14360 LEN != NULL: Output nothing. Set *LEN to number of words occupied
14361 by the insns printed.
14362
14363 Return "". */
14364
14365 const char *
14366 output_reload_insisf (rtx *op, rtx clobber_reg, int *len)
14367 {
14368 if (AVR_HAVE_MOVW
14369 && !test_hard_reg_class (LD_REGS, op[0])
14370 && (CONST_INT_P (op[1])
14371 || CONST_FIXED_P (op[1])
14372 || CONST_DOUBLE_P (op[1])))
14373 {
14374 /* In some cases it is better to clear the destination beforehand, e.g.
14375
14376 CLR R2 CLR R3 MOVW R4,R2 INC R2
14377
14378 is shorther than
14379
14380 CLR R2 INC R2 CLR R3 CLR R4 CLR R5
14381
14382 We find it too tedious to work that out in the print function.
14383 Instead, we call the print function twice to get the lengths of
14384 both methods and use the shortest one. */
14385
14386 int len_clr, len_noclr;
14387 output_reload_in_const (op, clobber_reg, &len_clr, true);
14388 output_reload_in_const (op, clobber_reg, &len_noclr, false);
14389
14390 if (len_noclr - len_clr == 4)
14391 {
14392 /* Default needs 4 CLR instructions: clear register beforehand. */
14393
14394 avr_asm_len ("mov %A0,__zero_reg__" CR_TAB
14395 "mov %B0,__zero_reg__" CR_TAB
14396 "movw %C0,%A0", &op[0], len, 3);
14397
14398 output_reload_in_const (op, clobber_reg, len, true);
14399
14400 if (len)
14401 *len += 3;
14402
14403 return "";
14404 }
14405 }
14406
14407 /* Default: destination not pre-cleared. */
14408
14409 output_reload_in_const (op, clobber_reg, len, false);
14410 return "";
14411 }
14412
14413
14414 const char *
14415 avr_out_reload_inpsi (rtx *op, rtx clobber_reg, int *len)
14416 {
14417 output_reload_in_const (op, clobber_reg, len, false);
14418 return "";
14419 }
14420
14421
14422 /* Worker function for `ASM_OUTPUT_ADDR_VEC'. */
14423 /* Emit jump tables out-of-line so that branches crossing the table
14424 get shorter offsets. If we have JUMP + CALL, then put the tables
14425 in a dedicated non-.text section so that CALLs get better chance to
14426 be relaxed to RCALLs.
14427
14428 We emit the tables by hand because `function_rodata_section' does not
14429 work as expected, cf. PR71151, and we do *NOT* want the table to be
14430 in .rodata, hence setting JUMP_TABLES_IN_TEXT_SECTION = 0 is of limited
14431 use; and setting it to 1 attributes table lengths to branch offsets...
14432 Moreover, fincal.c keeps switching section before each table entry
14433 which we find too fragile as to rely on section caching. */
14434
14435 void
14436 avr_output_addr_vec (rtx_insn *labl, rtx table)
14437 {
14438 FILE *stream = asm_out_file;
14439
14440 // AVR-SD: On functional safety devices, each executed instruction must
14441 // be followed by a valid opcode. This is because instruction validation
14442 // runs at fetch and decode for the next instruction and while the 2-stage
14443 // pipeline is executing the current one. There is no multilib option for
14444 // these devices, so take all multilib variants that contain AVR-SD.
14445 const bool maybe_sd = (AVR_HAVE_JMP_CALL
14446 && (avr_arch_index == ARCH_AVRXMEGA2
14447 || avr_arch_index == ARCH_AVRXMEGA3));
14448 bool uses_subsection = false;
14449
14450 app_disable ();
14451
14452 // Switch to appropriate (sub)section.
14453
14454 if (DECL_SECTION_NAME (current_function_decl)
14455 && symtab_node::get (current_function_decl)
14456 && ! symtab_node::get (current_function_decl)->implicit_section)
14457 {
14458 // .subsection will emit the code after the function and in the
14459 // section as chosen by the user.
14460
14461 switch_to_section (current_function_section ());
14462 fprintf (stream, "\t.subsection\t1\n");
14463 uses_subsection = true;
14464 }
14465 else
14466 {
14467 // Since PR63223 there is no restriction where to put the table; it
14468 // may even reside above 128 KiB. We put it in a section as high as
14469 // possible and avoid progmem in order not to waste flash <= 64 KiB.
14470
14471 const char *sec_name = ".jumptables.gcc";
14472
14473 // The table belongs to its host function, therefore use fine
14474 // grained sections so that, if that function is removed by
14475 // --gc-sections, the child table(s) may also be removed. */
14476
14477 tree asm_name = DECL_ASSEMBLER_NAME (current_function_decl);
14478 const char *fname = IDENTIFIER_POINTER (asm_name);
14479 fname = targetm.strip_name_encoding (fname);
14480 sec_name = ACONCAT ((sec_name, ".", fname, nullptr));
14481
14482 fprintf (stream, "\t.section\t%s,\"%s\",@progbits\n", sec_name,
14483 AVR_HAVE_JMP_CALL ? "a" : "ax");
14484 }
14485
14486 ASM_OUTPUT_ALIGN (stream, 1);
14487
14488 if (maybe_sd && uses_subsection)
14489 {
14490 // Insert a valid opcode prior to the first gs() label.
14491 // Any valid opcode will do. Use CLH since it disassembles
14492 // more nicely than NOP = 0x0000. This is all GCC can do.
14493 // Other cases, like inserting CLH after the vector table and
14494 // after the last instruction, are handled by other parts of
14495 // the toolchain.
14496 fprintf (stream, "\tclh\n");
14497 }
14498
14499 // Output the label that precedes the table.
14500
14501 char s_labl[40];
14502 targetm.asm_out.generate_internal_label (s_labl, "L",
14503 CODE_LABEL_NUMBER (labl));
14504 ASM_OUTPUT_TYPE_DIRECTIVE (stream, s_labl,
14505 AVR_HAVE_JMP_CALL ? "object" : "function");
14506
14507 targetm.asm_out.internal_label (stream, "L", CODE_LABEL_NUMBER (labl));
14508
14509 // Output the table's content.
14510
14511 int vlen = XVECLEN (table, 0);
14512
14513 for (int idx = 0; idx < vlen; idx++)
14514 {
14515 int value = CODE_LABEL_NUMBER (XEXP (XVECEXP (table, 0, idx), 0));
14516
14517 if (AVR_HAVE_JMP_CALL)
14518 fprintf (stream, "\t.word gs(.L%d)\n", value);
14519 else
14520 fprintf (stream, "\trjmp .L%d\n", value);
14521 }
14522
14523 // Switch back to original section. As we clobbered the section above,
14524 // forget the current section before switching back.
14525
14526 in_section = nullptr;
14527 switch_to_section (current_function_section ());
14528 }
14529
14530
14531 /* Implement `TARGET_CONDITIONAL_REGISTER_USAGE'. */
14532
14533 static void
14534 avr_conditional_register_usage (void)
14535 {
14536 if (AVR_TINY)
14537 {
14538 const int tiny_reg_alloc_order[] = {
14539 24, 25,
14540 22, 23,
14541 30, 31,
14542 26, 27,
14543 28, 29,
14544 21, 20, 19, 18,
14545 16, 17,
14546 32, 33, 34, 35,
14547 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
14548 };
14549
14550 /* Set R0-R17 as fixed registers. Reset R0-R17 in call used register list
14551 - R0-R15 are not available in Tiny Core devices
14552 - R16 and R17 are fixed registers. */
14553
14554 for (size_t i = REG_0; i <= REG_17; i++)
14555 {
14556 fixed_regs[i] = 1;
14557 call_used_regs[i] = 1;
14558 }
14559
14560 /* Set R18 to R21 as callee saved registers
14561 - R18, R19, R20 and R21 are the callee saved registers in
14562 Tiny Core devices */
14563
14564 for (size_t i = REG_18; i <= LAST_CALLEE_SAVED_REG; i++)
14565 {
14566 call_used_regs[i] = 0;
14567 }
14568
14569 /* Update register allocation order for Tiny Core devices */
14570
14571 for (size_t i = 0; i < ARRAY_SIZE (tiny_reg_alloc_order); i++)
14572 {
14573 reg_alloc_order[i] = tiny_reg_alloc_order[i];
14574 }
14575
14576 CLEAR_HARD_REG_SET (reg_class_contents[(int) NO_LD_REGS]);
14577 }
14578 }
14579
14580
14581 /* Implement `TARGET_HARD_REGNO_SCRATCH_OK'. */
14582 /* Returns true if SCRATCH are safe to be allocated as a scratch
14583 registers (for a define_peephole2) in the current function. */
14584
14585 static bool
14586 avr_hard_regno_scratch_ok (unsigned int regno)
14587 {
14588 /* Interrupt functions can only use registers that have already been saved
14589 by the prologue, even if they would normally be call-clobbered. */
14590
14591 if ((cfun->machine->is_interrupt || cfun->machine->is_signal)
14592 && !df_regs_ever_live_p (regno))
14593 return false;
14594
14595 /* Don't allow hard registers that might be part of the frame pointer.
14596 Some places in the compiler just test for [HARD_]FRAME_POINTER_REGNUM
14597 and don't care for a frame pointer that spans more than one register. */
14598
14599 if ((!reload_completed || frame_pointer_needed)
14600 && (regno == REG_Y || regno == REG_Y + 1))
14601 {
14602 return false;
14603 }
14604
14605 return true;
14606 }
14607
14608
14609 /* Worker function for `HARD_REGNO_RENAME_OK'. */
14610 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
14611
14612 bool
14613 avr_hard_regno_rename_ok (unsigned int old_reg, unsigned int new_reg)
14614 {
14615 /* Interrupt functions can only use registers that have already been
14616 saved by the prologue, even if they would normally be
14617 call-clobbered. */
14618
14619 if ((cfun->machine->is_interrupt || cfun->machine->is_signal)
14620 && !df_regs_ever_live_p (new_reg))
14621 return false;
14622
14623 /* Don't allow hard registers that might be part of the frame pointer.
14624 Some places in the compiler just test for [HARD_]FRAME_POINTER_REGNUM
14625 and don't care for a frame pointer that spans more than one register. */
14626
14627 if ((!reload_completed || frame_pointer_needed)
14628 && (old_reg == REG_Y || old_reg == REG_Y + 1
14629 || new_reg == REG_Y || new_reg == REG_Y + 1))
14630 {
14631 return false;
14632 }
14633
14634 return true;
14635 }
14636
14637
14638 /* Output a branch that tests a single bit of a register (QI, HI, SI or DImode)
14639 or memory location in the I/O space (QImode only).
14640
14641 Operand 0: comparison operator (must be EQ or NE, compare bit to zero).
14642 Operand 1: register operand to test, or CONST_INT memory address.
14643 Operand 2: bit number.
14644 Operand 3: label to jump to if the test is true. */
14645
14646 const char *
14647 avr_out_sbxx_branch (rtx_insn *insn, rtx operands[])
14648 {
14649 rtx_code comp = GET_CODE (operands[0]);
14650 bool long_jump = get_attr_length (insn) >= 4;
14651 bool reverse = long_jump || jump_over_one_insn_p (insn, operands[3]);
14652
14653 // PR116953: jump_over_one_insn_p may call extract on the next insn,
14654 // clobbering recog_data.operand. Thus, restore recog_data.
14655 extract_constrain_insn_cached (insn);
14656
14657 if (comp == GE)
14658 comp = EQ;
14659 else if (comp == LT)
14660 comp = NE;
14661
14662 if (reverse)
14663 comp = reverse_condition (comp);
14664
14665 switch (GET_CODE (operands[1]))
14666 {
14667 default:
14668 gcc_unreachable ();
14669
14670 case CONST_INT:
14671 case CONST:
14672 case SYMBOL_REF:
14673
14674 if (low_io_address_operand (operands[1], QImode))
14675 {
14676 if (comp == EQ)
14677 output_asm_insn ("sbis %i1,%2", operands);
14678 else
14679 output_asm_insn ("sbic %i1,%2", operands);
14680 }
14681 else
14682 {
14683 gcc_assert (io_address_operand (operands[1], QImode));
14684 output_asm_insn ("in __tmp_reg__,%i1", operands);
14685 if (comp == EQ)
14686 output_asm_insn ("sbrs __tmp_reg__,%2", operands);
14687 else
14688 output_asm_insn ("sbrc __tmp_reg__,%2", operands);
14689 }
14690
14691 break; /* CONST_INT */
14692
14693 case REG:
14694
14695 if (comp == EQ)
14696 output_asm_insn ("sbrs %T1%T2", operands);
14697 else
14698 output_asm_insn ("sbrc %T1%T2", operands);
14699
14700 break; /* REG */
14701 } /* switch */
14702
14703 if (long_jump)
14704 return ("rjmp .+4" CR_TAB
14705 "jmp %x3");
14706
14707 if (!reverse)
14708 return "rjmp %x3";
14709
14710 return "";
14711 }
14712
14713
14714 /* Output code for XOP[0] = sign_extract (XOP[1].0) and return "".
14715 PLEN == 0: Output instructions.
14716 PLEN != 0: Set *PLEN to the length of the sequence in words. */
14717
14718 const char *
14719 avr_out_sextr (rtx_insn *insn, rtx *xop, int *plen)
14720 {
14721 rtx dest = xop[0];
14722 rtx src = xop[1];
14723 int bit = INTVAL (xop[2]);
14724 int n_bytes = GET_MODE_SIZE (GET_MODE (dest));
14725
14726 gcc_assert (bit == 0);
14727
14728 if (reg_unused_after (insn, src))
14729 avr_asm_len ("lsr %1", xop, plen, -1);
14730 else
14731 avr_asm_len ("mov %0,%1" CR_TAB
14732 "lsr %0", xop, plen, -2);
14733
14734 for (int i = 0; i < n_bytes; ++i)
14735 {
14736 rtx b = avr_byte (dest, i);
14737 avr_asm_len ("sbc %0,%0", &b, plen, 1);
14738 if (i == 1 && n_bytes == 4 && AVR_HAVE_MOVW)
14739 return avr_asm_len ("movw %C0,%A0", xop, plen, 1);
14740 }
14741
14742 return "";
14743 }
14744
14745
14746 /*
14747 if (bits.bitno <eqne> 0)
14748 dest = op0;
14749 else
14750 dest = op0 <pix> op1;
14751
14752 Performed as:
14753
14754 dest = op0;
14755 if (bits.bitno <eqne> 0)
14756 goto LL;
14757 dest o= op1;
14758 LL:; */
14759
14760 void
14761 avr_emit_skip_pixop (rtx_code pix, rtx dest, rtx op0, rtx op1,
14762 rtx_code eqne, rtx bits, int bitno)
14763 {
14764 gcc_assert (eqne == EQ);
14765
14766 const machine_mode mode = GET_MODE (dest);
14767
14768 // Get rid of early-clobbers.
14769
14770 if (reg_overlap_mentioned_p (dest, bits))
14771 bits = copy_to_mode_reg (GET_MODE (bits), bits);
14772
14773 if (reg_overlap_mentioned_p (dest, op1))
14774 op1 = copy_to_mode_reg (mode, op1);
14775
14776 // xorqi3 has "register_operand" for op1.
14777 if (mode == QImode && pix == XOR)
14778 op1 = force_reg (QImode, op1);
14779
14780 emit_move_insn (dest, op0);
14781
14782 // Skip if bits.bitno <eqne> bitno.
14783 rtx xlabel = gen_label_rtx ();
14784 rtx zerox = gen_rtx_ZERO_EXTRACT (QImode, bits, const1_rtx, GEN_INT (bitno));
14785 rtx cond = gen_rtx_fmt_ee (eqne, VOIDmode, zerox, const0_rtx);
14786 emit (gen_sbrx_branchqi_split (cond, bits, const0_rtx, xlabel));
14787
14788 // Payload: plus, ior, xor for HI, PSI, SI have a scratch:QI;
14789 // QI and plus:HI don't.
14790 rtx src = gen_rtx_fmt_ee (pix, mode, dest, op1);
14791 rtx set = gen_rtx_SET (dest, src);
14792 rtx clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (QImode));
14793 bool no_scratch = mode == QImode || (mode == HImode && pix == PLUS);
14794 emit (no_scratch
14795 ? set
14796 : gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
14797
14798 emit_label (xlabel);
14799 }
14800
14801
14802 /*
14803 if (bits.bitno <eqne> 0)
14804 dest = src;
14805 else
14806 dest = 0;
14807
14808 Performed as:
14809
14810 dest = src;
14811 if (bits.bitno <eqne> 0)
14812 goto LL;
14813 dest = 0;
14814 LL:; */
14815
14816 void
14817 avr_emit_skip_clear (rtx dest, rtx src, rtx_code eqne, rtx bits, int bitno)
14818 {
14819 const machine_mode mode = GET_MODE (dest);
14820
14821 // Get rid of early-clobber.
14822 if (reg_overlap_mentioned_p (dest, bits))
14823 bits = copy_to_mode_reg (GET_MODE (bits), bits);
14824
14825 emit_move_insn (dest, src);
14826
14827 // Skip if bits.bitno <eqne> bitno.
14828 rtx xlabel = gen_label_rtx ();
14829 rtx zerox = gen_rtx_ZERO_EXTRACT (QImode, bits, const1_rtx, GEN_INT (bitno));
14830 rtx cond = gen_rtx_fmt_ee (eqne, VOIDmode, zerox, const0_rtx);
14831 emit (gen_sbrx_branchqi_split (cond, bits, const0_rtx, xlabel));
14832
14833 // Payload: dest = 0;
14834 emit_move_insn (dest, CONST0_RTX (mode));
14835
14836 emit_label (xlabel);
14837 }
14838
14839
14840 /* Worker function for `TARGET_ASM_CONSTRUCTOR'. */
14841
14842 static void
14843 avr_asm_out_ctor (rtx symbol, int priority)
14844 {
14845 fputs ("\t.global __do_global_ctors\n", asm_out_file);
14846 default_ctor_section_asm_out_constructor (symbol, priority);
14847 }
14848
14849
14850 /* Worker function for `TARGET_ASM_DESTRUCTOR'. */
14851
14852 static void
14853 avr_asm_out_dtor (rtx symbol, int priority)
14854 {
14855 fputs ("\t.global __do_global_dtors\n", asm_out_file);
14856 default_dtor_section_asm_out_destructor (symbol, priority);
14857 }
14858
14859
14860 /* Implement `TARGET_RETURN_IN_MEMORY'. */
14861
14862 static bool
14863 avr_return_in_memory (const_tree type, const_tree /*fntype*/)
14864 {
14865 HOST_WIDE_INT size = int_size_in_bytes (type);
14866 HOST_WIDE_INT ret_size_limit = AVR_TINY ? 4 : 8;
14867
14868 /* In avr, there are 8 return registers. But, for Tiny Core
14869 (ATtiny4/5/9/10/20/40) devices, only 4 registers are available.
14870 Return true if size is unknown or greater than the limit. */
14871
14872 return size == -1 || size > ret_size_limit;
14873 }
14874
14875
14876 /* Implement `CASE_VALUES_THRESHOLD'. */
14877 /* Supply the default for --param case-values-threshold=0 */
14878
14879 static unsigned int
14880 avr_case_values_threshold (void)
14881 {
14882 /* The exact break-even point between a jump table and an if-else tree
14883 depends on several factors not available here like, e.g. if 8-bit
14884 comparisons can be used in the if-else tree or not, on the
14885 range of the case values, if the case value can be reused, on the
14886 register allocation, etc. '7' appears to be a good choice. */
14887
14888 return 7;
14889 }
14890
14891
14892 /* Implement `TARGET_ADDR_SPACE_ADDRESS_MODE'. */
14893
14894 static scalar_int_mode
14895 avr_addr_space_address_mode (addr_space_t as)
14896 {
14897 return avr_addrspace[as].pointer_size == 3 ? PSImode : HImode;
14898 }
14899
14900
14901 /* Implement `TARGET_ADDR_SPACE_POINTER_MODE'. */
14902
14903 static scalar_int_mode
14904 avr_addr_space_pointer_mode (addr_space_t as)
14905 {
14906 return avr_addr_space_address_mode (as);
14907 }
14908
14909
14910 /* Helper for following function. */
14911
14912 static bool
14913 avr_reg_ok_for_pgm_addr (rtx reg, bool strict)
14914 {
14915 gcc_assert (REG_P (reg));
14916
14917 if (strict)
14918 {
14919 return REGNO (reg) == REG_Z;
14920 }
14921
14922 /* Avoid combine to propagate hard regs. */
14923
14924 if (can_create_pseudo_p ()
14925 && REGNO (reg) < REG_Z)
14926 {
14927 return false;
14928 }
14929
14930 return true;
14931 }
14932
14933
14934 /* Implement `TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P'. */
14935
14936 static bool
14937 avr_addr_space_legitimate_address_p (machine_mode mode, rtx x, bool strict,
14938 addr_space_t as, code_helper = ERROR_MARK)
14939 {
14940 bool ok = false;
14941
14942 switch (as)
14943 {
14944 default:
14945 gcc_unreachable ();
14946
14947 case ADDR_SPACE_GENERIC:
14948 return avr_legitimate_address_p (mode, x, strict);
14949
14950 case ADDR_SPACE_FLASH:
14951 case ADDR_SPACE_FLASH1:
14952 case ADDR_SPACE_FLASH2:
14953 case ADDR_SPACE_FLASH3:
14954 case ADDR_SPACE_FLASH4:
14955 case ADDR_SPACE_FLASH5:
14956
14957 switch (GET_CODE (x))
14958 {
14959 case REG:
14960 ok = avr_reg_ok_for_pgm_addr (x, strict);
14961 break;
14962
14963 case POST_INC:
14964 ok = avr_reg_ok_for_pgm_addr (XEXP (x, 0), strict);
14965 break;
14966
14967 default:
14968 break;
14969 }
14970
14971 break; /* FLASH */
14972
14973 case ADDR_SPACE_MEMX:
14974 case ADDR_SPACE_FLASHX:
14975 if (REG_P (x))
14976 ok = (!strict
14977 && can_create_pseudo_p ());
14978
14979 if (LO_SUM == GET_CODE (x))
14980 {
14981 rtx hi = XEXP (x, 0);
14982 rtx lo = XEXP (x, 1);
14983
14984 ok = (REG_P (hi)
14985 && (!strict || REGNO (hi) < FIRST_PSEUDO_REGISTER)
14986 && REG_P (lo)
14987 && REGNO (lo) == REG_Z);
14988 }
14989
14990 break; /* MEMX, FLASHX */
14991 }
14992
14993 if (avr_log.legitimate_address_p)
14994 {
14995 avr_edump ("\n%?: ret=%b, mode=%m strict=%d "
14996 "reload_completed=%d ra_in_progress=%d %s:",
14997 ok, mode, strict, reload_completed, ra_in_progress (),
14998 reg_renumber ? "(reg_renumber)" : "");
14999
15000 if (GET_CODE (x) == PLUS
15001 && REG_P (XEXP (x, 0))
15002 && CONST_INT_P (XEXP (x, 1))
15003 && IN_RANGE (INTVAL (XEXP (x, 1)), 0, MAX_LD_OFFSET (mode))
15004 && reg_renumber)
15005 {
15006 avr_edump ("(r%d ---> r%d)", REGNO (XEXP (x, 0)),
15007 true_regnum (XEXP (x, 0)));
15008 }
15009
15010 avr_edump ("\n%r\n", x);
15011 }
15012
15013 return ok;
15014 }
15015
15016
15017 /* Implement `TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS'. */
15018
15019 static rtx
15020 avr_addr_space_legitimize_address (rtx x, rtx old_x,
15021 machine_mode mode, addr_space_t as)
15022 {
15023 if (ADDR_SPACE_GENERIC_P (as))
15024 return avr_legitimize_address (x, old_x, mode);
15025
15026 if (avr_log.legitimize_address)
15027 {
15028 avr_edump ("\n%?: mode=%m\n %r\n", mode, old_x);
15029 }
15030
15031 return old_x;
15032 }
15033
15034
15035 /* Implement `TARGET_ADDR_SPACE_CONVERT'. */
15036
15037 static rtx
15038 avr_addr_space_convert (rtx src, tree type_old, tree type_new)
15039 {
15040 addr_space_t as_old = TYPE_ADDR_SPACE (TREE_TYPE (type_old));
15041 addr_space_t as_new = TYPE_ADDR_SPACE (TREE_TYPE (type_new));
15042 int size_old = GET_MODE_SIZE (targetm.addr_space.pointer_mode (as_old));
15043 int size_new = GET_MODE_SIZE (targetm.addr_space.pointer_mode (as_new));
15044
15045 if (avr_log.progmem)
15046 avr_edump ("\n%!: op = %r\nfrom = %t\nto = %t\n",
15047 src, type_old, type_new);
15048
15049 /* Up-casting from 16-bit to 24-bit pointer. */
15050
15051 if (size_old == 2 && size_new == 3)
15052 {
15053 rtx sym = src;
15054 rtx reg = gen_reg_rtx (PSImode);
15055
15056 while (CONST == GET_CODE (sym) || PLUS == GET_CODE (sym))
15057 sym = XEXP (sym, 0);
15058
15059 /* Look at symbol flags: avr_encode_section_info set the flags
15060 also if attribute progmem was seen so that we get the right
15061 promotion for, e.g. PSTR-like strings that reside in generic space
15062 but are located in flash. In that case we patch the incoming
15063 address space. */
15064
15065 if (SYMBOL_REF_P (sym)
15066 && ADDR_SPACE_FLASH == AVR_SYMBOL_GET_ADDR_SPACE (sym))
15067 {
15068 as_old = ADDR_SPACE_FLASH;
15069 }
15070
15071 /* Linearize memory: RAM has bit 23 set. When as_new = __flashx then
15072 this is basically UB since __flashx mistreats RAM addresses, but there
15073 is no way to bail out. (Though -Waddr-space-convert will tell.)
15074 ...but PR121277 is confusing, in particular when NULL is coming in. */
15075
15076 int msb = ADDR_SPACE_GENERIC_P (as_old)
15077 ? as_new == ADDR_SPACE_MEMX ? 0x80 : 0x00
15078 : avr_addrspace[as_old].segment;
15079
15080 src = force_reg (Pmode, src);
15081
15082 emit_insn (msb == 0
15083 ? gen_zero_extendhipsi2 (reg, src)
15084 : gen_n_extendhipsi2 (reg, gen_int_mode (msb, QImode), src));
15085
15086 return reg;
15087 }
15088
15089 /* Down-casting from 24-bit to 16-bit throws away the high byte. */
15090
15091 if (size_old == 3 && size_new == 2)
15092 {
15093 rtx new_src = gen_reg_rtx (Pmode);
15094
15095 src = force_reg (PSImode, src);
15096
15097 emit_move_insn (new_src,
15098 simplify_gen_subreg (Pmode, src, PSImode, 0));
15099 return new_src;
15100 }
15101
15102 return src;
15103 }
15104
15105
15106 /* Implement `TARGET_ADDR_SPACE_SUBSET_P'. */
15107
15108 static bool
15109 avr_addr_space_subset_p (addr_space_t /*subset*/, addr_space_t /*superset*/)
15110 {
15111 /* Allow any kind of pointer mess. */
15112
15113 return true;
15114 }
15115
15116
15117 /* Helps the next function. */
15118
15119 static bool
15120 avr_addr_space_contains (addr_space_t super, addr_space_t sub)
15121 {
15122 return (super == sub
15123 || super == ADDR_SPACE_MEMX
15124 || (super == ADDR_SPACE_FLASHX
15125 && sub != ADDR_SPACE_MEMX && ! ADDR_SPACE_GENERIC_P (sub)));
15126 }
15127
15128
15129 /* Implement `TARGET_CONVERT_TO_TYPE'. */
15130
15131 static tree
15132 avr_convert_to_type (tree type, tree expr)
15133 {
15134 /* Print a diagnose for pointer conversion that changes the address
15135 space of the pointer target to a non-enclosing address space,
15136 provided -Waddr-space-convert is on.
15137
15138 FIXME: Filter out cases where the target object is known to
15139 be located in the right memory, like in
15140
15141 (const __flash*) PSTR ("text")
15142
15143 Also try to distinguish between explicit casts requested by
15144 the user and implicit casts like
15145
15146 void f (const __flash char*);
15147
15148 void g (const char *p)
15149 {
15150 f ((const __flash*) p);
15151 }
15152
15153 under the assumption that an explicit casts means that the user
15154 knows what he is doing, e.g. interface with PSTR or old style
15155 code with progmem and pgm_read_xxx.
15156 */
15157
15158 if (avropt_warn_addr_space_convert
15159 && expr != error_mark_node
15160 && POINTER_TYPE_P (type)
15161 && POINTER_TYPE_P (TREE_TYPE (expr)))
15162 {
15163 addr_space_t as_old = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
15164 addr_space_t as_new = TYPE_ADDR_SPACE (TREE_TYPE (type));
15165
15166 if (avr_log.progmem)
15167 avr_edump ("%?: type = %t\nexpr = %t\n\n", type, expr);
15168
15169 if (! avr_addr_space_contains (as_new, as_old))
15170 {
15171 location_t loc = EXPR_LOCATION (expr);
15172 const char *name_old = avr_addrspace[as_old].name;
15173 const char *name_new = avr_addrspace[as_new].name;
15174
15175 // Be relaxed when NULL is used, and when 0x0 stands for
15176 // address 0x0.
15177 bool nowarn = (expr == null_pointer_node
15178 && (as_new == ADDR_SPACE_FLASHX
15179 || as_new == ADDR_SPACE_FLASH));
15180 if (!nowarn)
15181 warning (OPT_Waddr_space_convert,
15182 "conversion from address space %qs to address space %qs",
15183 ADDR_SPACE_GENERIC_P (as_old) ? "generic" : name_old,
15184 ADDR_SPACE_GENERIC_P (as_new) ? "generic" : name_new);
15185
15186 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
15187 }
15188 }
15189
15190 return NULL_TREE;
15191 }
15192
15193
15194 /* Implement `TARGET_LEGITIMATE_COMBINED_INSN'. */
15195 /* PR78883: Filter out paradoxical SUBREGs of MEM which are not handled
15196 properly by following passes. As INSN_SCHEDULING is off and hence
15197 general_operand accepts such expressions, ditch them now. */
15198
15199 static bool
15200 avr_legitimate_combined_insn (rtx_insn *insn)
15201 {
15202 subrtx_iterator::array_type array;
15203
15204 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
15205 {
15206 const_rtx op = *iter;
15207
15208 if (SUBREG_P (op)
15209 && MEM_P (SUBREG_REG (op))
15210 && (GET_MODE_SIZE (GET_MODE (op))
15211 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
15212 {
15213 return false;
15214 }
15215 }
15216
15217 return true;
15218 }
15219
15220
15221 /* PR63633: The middle-end might come up with hard regs as input operands.
15222
15223 RMASK is a bit mask representing a subset of hard registers R0...R31:
15224 Rn is an element of that set iff bit n of RMASK is set.
15225 OPMASK describes a subset of OP[]: If bit n of OPMASK is 1 then
15226 OP[n] has to be fixed; otherwise OP[n] is left alone.
15227
15228 For each element of OPMASK which is a hard register overlapping RMASK,
15229 replace OP[n] with a newly created pseudo register
15230
15231 HREG == 0: Also emit a move insn that copies the contents of that
15232 hard register into the new pseudo.
15233
15234 HREG != 0: Also set HREG[n] to the hard register. */
15235
15236 static void
15237 avr_fix_operands (rtx *op, rtx *hreg, unsigned opmask, unsigned rmask)
15238 {
15239 for (; opmask; opmask >>= 1, op++)
15240 {
15241 rtx reg = *op;
15242
15243 if (hreg)
15244 *hreg = NULL_RTX;
15245
15246 if ((opmask & 1)
15247 && REG_P (reg)
15248 && REGNO (reg) < FIRST_PSEUDO_REGISTER
15249 // This hard-reg overlaps other prohibited hard regs?
15250 && (rmask & regmask (GET_MODE (reg), REGNO (reg))))
15251 {
15252 *op = gen_reg_rtx (GET_MODE (reg));
15253 if (hreg == nullptr)
15254 emit_move_insn (*op, reg);
15255 else
15256 *hreg = reg;
15257 }
15258
15259 if (hreg)
15260 hreg++;
15261 }
15262 }
15263
15264
15265 void
15266 avr_fix_inputs (rtx *op, unsigned opmask, unsigned rmask)
15267 {
15268 avr_fix_operands (op, nullptr, opmask, rmask);
15269 }
15270
15271
15272 /* Helper for the function below: If bit n of MASK is set and
15273 HREG[n] != NULL, then emit a move insn to copy OP[n] to HREG[n].
15274 Otherwise do nothing for that n. Return TRUE. */
15275
15276 static bool
15277 avr_move_fixed_operands (rtx *op, rtx *hreg, unsigned mask)
15278 {
15279 for (; mask; mask >>= 1, op++, hreg++)
15280 if ((mask & 1)
15281 && *hreg)
15282 emit_move_insn (*hreg, *op);
15283
15284 return true;
15285 }
15286
15287
15288 /* PR63633: The middle-end might come up with hard regs as output operands.
15289
15290 GEN is a sequence generating function like gen_mulsi3 with 3 operands OP[].
15291 RMASK is a bit mask representing a subset of hard registers R0...R31:
15292 Rn is an element of that set iff bit n of RMASK is set.
15293 OPMASK describes a subset of OP[]: If bit n of OPMASK is 1 then
15294 OP[n] has to be fixed; otherwise OP[n] is left alone.
15295
15296 Emit the insn sequence as generated by GEN() with all elements of OPMASK
15297 which are hard registers overlapping RMASK replaced by newly created
15298 pseudo registers. After the sequence has been emitted, emit insns that
15299 move the contents of respective pseudos to their hard regs. */
15300
15301 bool
15302 avr_emit3_fix_outputs (rtx (*gen)(rtx,rtx,rtx), rtx *op,
15303 unsigned opmask, unsigned rmask)
15304 {
15305 const int n = 3;
15306 rtx hreg[n];
15307
15308 /* It is letigimate for GEN to call this function, and in order not to
15309 get self-recursive we use the following static kludge. This is the
15310 only way not to duplicate all expanders and to avoid ugly and
15311 hard-to-maintain C-code instead of the much more appreciated RTL
15312 representation as supplied by define_expand. */
15313 static bool lock = false;
15314
15315 gcc_assert (opmask < (1u << n));
15316
15317 if (lock)
15318 return false;
15319
15320 avr_fix_operands (op, hreg, opmask, rmask);
15321
15322 lock = true;
15323 emit_insn (gen (op[0], op[1], op[2]));
15324 lock = false;
15325
15326 return avr_move_fixed_operands (op, hreg, opmask);
15327 }
15328
15329
15330 /* A helper for the insn condition of "*nzb=1.<code>.lsr[.not]_split"
15331 where <code> is AND, IOR or XOR. Return true when
15332
15333 OP[0] <code>= OP[1] >> OP[2]
15334
15335 can be performed by means of the code of "*nzb=1.<code>.zerox", i.e.
15336
15337 OP[0] <code>= OP[1].OP[2]
15338
15339 For example, when OP[0] is in { 0, 1 }, then R24 &= R10.4
15340 can be performed by means of SBRS R10,4 $ CLR R24.
15341 Notice that the constraint of OP[3] is "0". */
15342
15343 bool
15344 avr_nonzero_bits_lsr_operands_p (rtx_code code, rtx *op)
15345 {
15346 if (reload_completed)
15347 return false;
15348
15349 const auto offs = INTVAL (op[2]);
15350 const auto op1_non0 = nonzero_bits (op[1], QImode);
15351 const auto op3_non0 = nonzero_bits (op[3], QImode);
15352
15353 switch (code)
15354 {
15355 default:
15356 gcc_unreachable ();
15357
15358 case IOR:
15359 case XOR:
15360 return op1_non0 >> offs == 1;
15361
15362 case AND:
15363 return op3_non0 == 1;
15364 }
15365
15366 return false;
15367 }
15368
15369
15370 /* Worker function for cpymemhi expander.
15371 XOP[0] Destination as MEM:BLK
15372 XOP[1] Source " "
15373 XOP[2] # Bytes to copy
15374
15375 Return TRUE if the expansion is accomplished.
15376 Return FALSE if the operand compination is not supported. */
15377
15378 bool
15379 avr_emit_cpymemhi (rtx *xop)
15380 {
15381 machine_mode loop_mode;
15382 addr_space_t as = MEM_ADDR_SPACE (xop[1]);
15383 rtx loop_reg, addr1, insn;
15384 rtx a_hi8 = NULL_RTX;
15385
15386 if (avr_mem_flash_p (xop[0]))
15387 return false;
15388
15389 if (!CONST_INT_P (xop[2]))
15390 return false;
15391
15392 HOST_WIDE_INT count = INTVAL (xop[2]);
15393 if (count <= 0)
15394 return false;
15395
15396 rtx a_src = XEXP (xop[1], 0);
15397 rtx a_dest = XEXP (xop[0], 0);
15398
15399 if (as == ADDR_SPACE_FLASHX
15400 && ! AVR_HAVE_ELPM)
15401 {
15402 a_src = copy_to_mode_reg (Pmode, avr_word (a_src, 0));
15403 as = ADDR_SPACE_FLASH;
15404 }
15405
15406 machine_mode addr_mode = GET_MODE (a_src);
15407
15408 if (addr_mode == PSImode)
15409 {
15410 gcc_assert (as == ADDR_SPACE_MEMX || as == ADDR_SPACE_FLASHX);
15411
15412 loop_mode = (count < 0x100) ? QImode : HImode;
15413 loop_reg = gen_rtx_REG (loop_mode, 24);
15414 emit_move_insn (loop_reg, gen_int_mode (count, loop_mode));
15415
15416 addr1 = simplify_gen_subreg (HImode, a_src, PSImode, 0);
15417 a_hi8 = simplify_gen_subreg (QImode, a_src, PSImode, 2);
15418 }
15419 else
15420 {
15421 int segment = avr_addrspace[as].segment;
15422
15423 if (segment
15424 && avropt_n_flash > 1)
15425 {
15426 a_hi8 = GEN_INT (segment);
15427 emit_move_insn (rampz_rtx, a_hi8 = copy_to_mode_reg (QImode, a_hi8));
15428 }
15429 else if (!ADDR_SPACE_GENERIC_P (as))
15430 {
15431 as = ADDR_SPACE_FLASH;
15432 }
15433
15434 addr1 = a_src;
15435
15436 loop_mode = (count <= 0x100) ? QImode : HImode;
15437 loop_reg = copy_to_mode_reg (loop_mode, gen_int_mode (count, loop_mode));
15438 }
15439
15440 rtx xas = GEN_INT (as);
15441
15442 /* FIXME: Register allocator might come up with spill fails if it is left
15443 on its own. Thus, we allocate the pointer registers by hand:
15444 Z = source address
15445 X = destination address */
15446
15447 emit_move_insn (lpm_addr_reg_rtx, addr1);
15448 emit_move_insn (gen_rtx_REG (HImode, REG_X), a_dest);
15449
15450 /* FIXME: Register allocator does a bad job and might spill address
15451 register(s) inside the loop leading to additional move instruction
15452 to/from stack which could clobber tmp_reg. Thus, do *not* emit
15453 load and store as separate insns. Instead, we perform the copy
15454 by means of one monolithic insn. */
15455
15456 gcc_assert (TMP_REGNO == LPM_REGNO);
15457
15458 if (addr_mode == HImode)
15459 {
15460 /* Load instruction ([E]LPM or LD) is known at compile time:
15461 Do the copy-loop inline. */
15462
15463 rtx (*fun) (rtx, rtx, rtx)
15464 = QImode == loop_mode ? gen_cpymem_qi : gen_cpymem_hi;
15465
15466 insn = fun (xas, loop_reg, loop_reg);
15467 }
15468 else
15469 {
15470 rtx (*fun) (rtx, rtx)
15471 = QImode == loop_mode ? gen_cpymemx_qi : gen_cpymemx_hi;
15472
15473 emit_move_insn (gen_rtx_REG (QImode, REG_23), a_hi8);
15474
15475 insn = fun (xas, GEN_INT (avr_addr.rampz));
15476 }
15477
15478 set_mem_addr_space (SET_SRC (XVECEXP (insn, 0, 0)), as);
15479 emit_insn (insn);
15480
15481 return true;
15482 }
15483
15484
15485 /* Print assembler for cpymem_qi, cpymem_hi insns...
15486 $0 : Address Space
15487 $1, $2 : Loop register
15488 Z : Source address
15489 X : Destination address
15490 */
15491
15492 const char *
15493 avr_out_cpymem (rtx_insn * /*insn*/, rtx *op, int *plen)
15494 {
15495 addr_space_t as = (addr_space_t) INTVAL (op[0]);
15496 machine_mode loop_mode = GET_MODE (op[1]);
15497 bool sbiw_p = avr_adiw_reg_p (op[1]);
15498 rtx xop[3] = { op[0], op[1], tmp_reg_rtx };
15499
15500 if (plen)
15501 *plen = 0;
15502
15503 /* Loop label */
15504
15505 avr_asm_len ("0:", xop, plen, 0);
15506
15507 /* Load with post-increment */
15508
15509 switch (as)
15510 {
15511 default:
15512 gcc_unreachable ();
15513
15514 case ADDR_SPACE_GENERIC:
15515
15516 avr_asm_len ("ld %2,Z+", xop, plen, 1);
15517 break;
15518
15519 case ADDR_SPACE_FLASH:
15520
15521 if (AVR_HAVE_LPMX)
15522 avr_asm_len ("lpm %2,Z+", xop, plen, 1);
15523 else
15524 avr_asm_len ("lpm" CR_TAB
15525 "adiw r30,1", xop, plen, 2);
15526 break;
15527
15528 case ADDR_SPACE_FLASH1:
15529 case ADDR_SPACE_FLASH2:
15530 case ADDR_SPACE_FLASH3:
15531 case ADDR_SPACE_FLASH4:
15532 case ADDR_SPACE_FLASH5:
15533
15534 if (AVR_HAVE_ELPMX)
15535 avr_asm_len ("elpm %2,Z+", xop, plen, 1);
15536 else
15537 avr_asm_len ("elpm" CR_TAB
15538 "adiw r30,1", xop, plen, 2);
15539 break;
15540 }
15541
15542 /* Store with post-increment */
15543
15544 avr_asm_len ("st X+,%2", xop, plen, 1);
15545
15546 /* Decrement loop-counter and set Z-flag */
15547
15548 if (QImode == loop_mode)
15549 {
15550 avr_asm_len ("dec %1", xop, plen, 1);
15551 }
15552 else if (sbiw_p)
15553 {
15554 avr_asm_len ("sbiw %1,1", xop, plen, 1);
15555 }
15556 else
15557 {
15558 avr_asm_len ("subi %A1,1" CR_TAB
15559 "sbci %B1,0", xop, plen, 2);
15560 }
15561
15562 // Loop until zero.
15563 avr_asm_len ("brne 0b", xop, plen, 1);
15564
15565
15566 // Restore RAMPZ on EBI devices.
15567 if (as >= ADDR_SPACE_FLASH1
15568 && AVR_HAVE_ELPM && AVR_HAVE_RAMPD)
15569 avr_asm_len ("out %i0,__zero_reg__", &rampz_rtx, plen, 1);
15570
15571 return "";
15572 }
15573
15574
15575 /* Helper for __builtin_avr_delay_cycles */
15576
15577 static rtx
15578 avr_mem_clobber (void)
15579 {
15580 rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
15581 MEM_VOLATILE_P (mem) = 1;
15582 return mem;
15583 }
15584
15585
15586 static void
15587 avr_expand_delay_cycles (rtx operands0)
15588 {
15589 unsigned HOST_WIDE_INT cycles = UINTVAL (operands0) & GET_MODE_MASK (SImode);
15590 unsigned HOST_WIDE_INT cycles_used;
15591 unsigned HOST_WIDE_INT loop_count;
15592
15593 if (IN_RANGE (cycles, 83886082, 0xFFFFFFFF))
15594 {
15595 loop_count = ((cycles - 9) / 6) + 1;
15596 cycles_used = ((loop_count - 1) * 6) + 9;
15597 emit_insn (gen_delay_cycles_4 (gen_int_mode (loop_count, SImode),
15598 avr_mem_clobber ()));
15599 cycles -= cycles_used;
15600 }
15601
15602 if (IN_RANGE (cycles, 262145, 83886081))
15603 {
15604 loop_count = ((cycles - 7) / 5) + 1;
15605 if (loop_count > 0xFFFFFF)
15606 loop_count = 0xFFFFFF;
15607 cycles_used = ((loop_count - 1) * 5) + 7;
15608 emit_insn (gen_delay_cycles_3 (gen_int_mode (loop_count, SImode),
15609 avr_mem_clobber ()));
15610 cycles -= cycles_used;
15611 }
15612
15613 if (IN_RANGE (cycles, 768, 262144))
15614 {
15615 loop_count = ((cycles - 5) / 4) + 1;
15616 if (loop_count > 0xFFFF)
15617 loop_count = 0xFFFF;
15618 cycles_used = ((loop_count - 1) * 4) + 5;
15619 emit_insn (gen_delay_cycles_2 (gen_int_mode (loop_count, HImode),
15620 avr_mem_clobber ()));
15621 cycles -= cycles_used;
15622 }
15623
15624 if (IN_RANGE (cycles, 6, 767))
15625 {
15626 loop_count = cycles / 3;
15627 if (loop_count > 255)
15628 loop_count = 255;
15629 cycles_used = loop_count * 3;
15630 emit_insn (gen_delay_cycles_1 (gen_int_mode (loop_count, QImode),
15631 avr_mem_clobber ()));
15632 cycles -= cycles_used;
15633 }
15634
15635 while (cycles >= 2)
15636 {
15637 emit_insn (gen_nopv (GEN_INT (2)));
15638 cycles -= 2;
15639 }
15640
15641 if (cycles == 1)
15642 {
15643 emit_insn (gen_nopv (GEN_INT (1)));
15644 cycles--;
15645 }
15646 }
15647
15648
15649 static void
15650 avr_expand_nops (rtx operands0)
15651 {
15652 unsigned HOST_WIDE_INT n_nops = UINTVAL (operands0) & GET_MODE_MASK (HImode);
15653
15654 while (n_nops--)
15655 {
15656 emit_insn (gen_nopv (const1_rtx));
15657 }
15658 }
15659
15660
15661 /* Compute the image of x under f, i.e. perform x --> f(x) */
15662
15663 static int
15664 avr_map (unsigned int f, int x)
15665 {
15666 return x < 8 ? (f >> (4 * x)) & 0xf : 0;
15667 }
15668
15669
15670 /* Return some metrics of map A. */
15671
15672 enum
15673 {
15674 /* Number of fixed points in { 0 ... 7 } */
15675 MAP_FIXED_0_7,
15676
15677 /* Size of preimage of non-fixed points in { 0 ... 7 } */
15678 MAP_NONFIXED_0_7,
15679
15680 /* Mask representing the fixed points in { 0 ... 7 } */
15681 MAP_MASK_FIXED_0_7,
15682
15683 /* Size of the preimage of { 0 ... 7 } */
15684 MAP_PREIMAGE_0_7,
15685
15686 /* Mask that represents the preimage of { f } */
15687 MAP_MASK_PREIMAGE_F
15688 };
15689
15690 static unsigned
15691 avr_map_metric (unsigned int a, int mode)
15692 {
15693 unsigned metric = 0;
15694
15695 for (unsigned i = 0; i < 8; i++)
15696 {
15697 unsigned ai = avr_map (a, i);
15698
15699 if (mode == MAP_FIXED_0_7)
15700 metric += ai == i;
15701 else if (mode == MAP_NONFIXED_0_7)
15702 metric += ai < 8 && ai != i;
15703 else if (mode == MAP_MASK_FIXED_0_7)
15704 metric |= ((unsigned) (ai == i)) << i;
15705 else if (mode == MAP_PREIMAGE_0_7)
15706 metric += ai < 8;
15707 else if (mode == MAP_MASK_PREIMAGE_F)
15708 metric |= ((unsigned) (ai == 0xf)) << i;
15709 else
15710 gcc_unreachable ();
15711 }
15712
15713 return metric;
15714 }
15715
15716
15717 /* Return true if IVAL has a 0xf in its hexadecimal representation
15718 and false, otherwise. Only nibbles 0..7 are taken into account.
15719 Used as constraint helper for C0f and Cxf. */
15720
15721 bool
15722 avr_has_nibble_0xf (rtx ival)
15723 {
15724 unsigned int map = UINTVAL (ival) & GET_MODE_MASK (SImode);
15725 return avr_map_metric (map, MAP_MASK_PREIMAGE_F) != 0;
15726 }
15727
15728
15729 /* We have a set of bits that are mapped by a function F.
15730 Try to decompose F by means of a second function G so that
15731
15732 F = F o G^-1 o G
15733
15734 and
15735
15736 cost (F o G^-1) + cost (G) < cost (F)
15737
15738 Example: Suppose builtin insert_bits supplies us with the map
15739 F = 0x3210ffff. Instead of doing 4 bit insertions to get the high
15740 nibble of the result, we can just as well rotate the bits before inserting
15741 them and use the map 0x7654ffff which is cheaper than the original map.
15742 For this example G = G^-1 = 0x32107654 and F o G^-1 = 0x7654ffff. */
15743
15744 typedef struct
15745 {
15746 /* tree code of binary function G */
15747 tree_code code;
15748
15749 /* The constant second argument of G */
15750 int arg;
15751
15752 /* G^-1, the inverse of G (*, arg) */
15753 unsigned ginv;
15754
15755 /* The cost of applying G (*, arg) */
15756 int cost;
15757
15758 /* The composition F o G^-1 (*, arg) for some function F */
15759 unsigned int map;
15760
15761 /* For debug purpose only */
15762 const char *str;
15763 } avr_map_op_t;
15764
15765 static const avr_map_op_t avr_map_op[] =
15766 {
15767 { LROTATE_EXPR, 0, 0x76543210, 0, 0, "id" },
15768 { LROTATE_EXPR, 1, 0x07654321, 2, 0, "<<<" },
15769 { LROTATE_EXPR, 2, 0x10765432, 4, 0, "<<<" },
15770 { LROTATE_EXPR, 3, 0x21076543, 4, 0, "<<<" },
15771 { LROTATE_EXPR, 4, 0x32107654, 1, 0, "<<<" },
15772 { LROTATE_EXPR, 5, 0x43210765, 3, 0, "<<<" },
15773 { LROTATE_EXPR, 6, 0x54321076, 5, 0, "<<<" },
15774 { LROTATE_EXPR, 7, 0x65432107, 3, 0, "<<<" },
15775 { RSHIFT_EXPR, 1, 0x6543210c, 1, 0, ">>" },
15776 { RSHIFT_EXPR, 1, 0x7543210c, 1, 0, ">>" },
15777 { RSHIFT_EXPR, 2, 0x543210cc, 2, 0, ">>" },
15778 { RSHIFT_EXPR, 2, 0x643210cc, 2, 0, ">>" },
15779 { RSHIFT_EXPR, 2, 0x743210cc, 2, 0, ">>" },
15780 { LSHIFT_EXPR, 1, 0xc7654321, 1, 0, "<<" },
15781 { LSHIFT_EXPR, 2, 0xcc765432, 2, 0, "<<" }
15782 };
15783
15784
15785 /* Try to decompose F as F = (F o G^-1) o G as described above.
15786 The result is a struct representing F o G^-1 and G.
15787 If result.cost < 0 then such a decomposition does not exist. */
15788
15789 static avr_map_op_t
15790 avr_map_decompose (unsigned int f, const avr_map_op_t *g, bool val_const_p)
15791 {
15792 bool val_used_p = avr_map_metric (f, MAP_MASK_PREIMAGE_F) != 0;
15793 avr_map_op_t f_ginv = *g;
15794 unsigned int ginv = g->ginv;
15795
15796 f_ginv.cost = -1;
15797
15798 /* Step 1: Computing F o G^-1 */
15799
15800 for (int i = 7; i >= 0; i--)
15801 {
15802 int x = avr_map (f, i);
15803
15804 if (x <= 7)
15805 {
15806 x = avr_map (ginv, x);
15807
15808 /* The bit is no element of the image of G: no avail (cost = -1) */
15809
15810 if (x > 7)
15811 return f_ginv;
15812 }
15813
15814 f_ginv.map = (f_ginv.map << 4) + x;
15815 }
15816
15817 /* Step 2: Compute the cost of the operations.
15818 The overall cost of doing an operation prior to the insertion is
15819 the cost of the insertion plus the cost of the operation. */
15820
15821 /* Step 2a: Compute cost of F o G^-1 */
15822
15823 if (avr_map_metric (f_ginv.map, MAP_NONFIXED_0_7) == 0)
15824 /* The mapping consists only of fixed points and can be folded
15825 to AND/OR logic in the remainder. Reasonable cost is 3. */
15826 f_ginv.cost = 2 + (val_used_p && !val_const_p);
15827 else
15828 {
15829 rtx xop[4];
15830
15831 /* Get the cost of the insn by calling the output worker with some
15832 fake values. Mimic effect of reloading xop[3]: Unused operands
15833 are mapped to 0 and used operands are reloaded to xop[0]. */
15834
15835 xop[0] = all_regs_rtx[REG_24];
15836 xop[1] = gen_int_mode (f_ginv.map, SImode);
15837 xop[2] = all_regs_rtx[REG_25];
15838 xop[3] = val_used_p ? xop[0] : const0_rtx;
15839
15840 avr_out_insert_bits (xop, &f_ginv.cost);
15841
15842 f_ginv.cost += val_const_p && val_used_p ? 1 : 0;
15843 }
15844
15845 /* Step 2b: Add cost of G */
15846
15847 f_ginv.cost += g->cost;
15848
15849 if (avr_log.builtin)
15850 avr_edump (" %s%d=%d", g->str, g->arg, f_ginv.cost);
15851
15852 return f_ginv;
15853 }
15854
15855
15856 /* Insert bits from XOP[1] into XOP[0] according to MAP.
15857 XOP[0] and XOP[1] don't overlap.
15858 If FIXP_P = true: Move all bits according to MAP using BLD/BST sequences.
15859 If FIXP_P = false: Just move the bit if its position in the destination
15860 is different to its source position. */
15861
15862 static void
15863 avr_move_bits (rtx *xop, unsigned int map, bool fixp_p, int *plen)
15864 {
15865 /* T-flag contains this bit of the source, i.e. of XOP[1] */
15866 int t_bit_src = -1;
15867
15868 /* We order the operations according to the requested source bit b. */
15869
15870 for (int b = 0; b < 8; b++)
15871 for (int bit_dest = 0; bit_dest < 8; bit_dest++)
15872 {
15873 int bit_src = avr_map (map, bit_dest);
15874
15875 if (b != bit_src
15876 || bit_src >= 8
15877 /* Same position: No need to copy as requested by FIXP_P. */
15878 || (bit_dest == bit_src && !fixp_p))
15879 continue;
15880
15881 if (t_bit_src != bit_src)
15882 {
15883 /* Source bit is not yet in T: Store it to T. */
15884
15885 t_bit_src = bit_src;
15886
15887 xop[3] = GEN_INT (bit_src);
15888 avr_asm_len ("bst %T1%T3", xop, plen, 1);
15889 }
15890
15891 /* Load destination bit with T. */
15892
15893 xop[3] = GEN_INT (bit_dest);
15894 avr_asm_len ("bld %T0%T3", xop, plen, 1);
15895 }
15896 }
15897
15898
15899 /* PLEN == 0: Print assembler code for `insert_bits'.
15900 PLEN != 0: Compute code length in bytes.
15901
15902 OP[0]: Result
15903 OP[1]: The mapping composed of nibbles. If nibble no. N is
15904 0: Bit N of result is copied from bit OP[2].0
15905 ... ...
15906 7: Bit N of result is copied from bit OP[2].7
15907 0xf: Bit N of result is copied from bit OP[3].N
15908 OP[2]: Bits to be inserted
15909 OP[3]: Target value */
15910
15911 const char *
15912 avr_out_insert_bits (rtx *op, int *plen)
15913 {
15914 unsigned int map = UINTVAL (op[1]) & GET_MODE_MASK (SImode);
15915 bool fixp_p = true;
15916 rtx xop[4];
15917
15918 xop[0] = op[0];
15919 xop[1] = op[2];
15920 xop[2] = op[3];
15921
15922 gcc_assert (REG_P (xop[2]) || CONST_INT_P (xop[2]));
15923
15924 if (plen)
15925 *plen = 0;
15926 else if (flag_print_asm_name)
15927 fprintf (asm_out_file, ASM_COMMENT_START "map = 0x%08x\n", map);
15928
15929 /* If MAP has fixed points it might be better to initialize the result
15930 with the bits to be inserted instead of moving all bits by hand. */
15931
15932 unsigned mask_fixed = avr_map_metric (map, MAP_MASK_FIXED_0_7);
15933
15934 if (REGNO (xop[0]) == REGNO (xop[1]))
15935 {
15936 /* Avoid early-clobber conflicts */
15937
15938 avr_asm_len ("mov __tmp_reg__,%1", xop, plen, 1);
15939 xop[1] = tmp_reg_rtx;
15940 fixp_p = false;
15941 }
15942
15943 if (avr_map_metric (map, MAP_MASK_PREIMAGE_F))
15944 {
15945 /* XOP[2] is used and reloaded to XOP[0] already */
15946
15947 int n_fix = 0, n_nofix = 0;
15948
15949 gcc_assert (REG_P (xop[2]));
15950
15951 /* Get the code size of the bit insertions; once with all bits
15952 moved and once with fixed points omitted. */
15953
15954 avr_move_bits (xop, map, true, &n_fix);
15955 avr_move_bits (xop, map, false, &n_nofix);
15956
15957 if (fixp_p && n_fix - n_nofix > 3)
15958 {
15959 xop[3] = gen_int_mode (~mask_fixed, QImode);
15960
15961 avr_asm_len ("eor %0,%1" CR_TAB
15962 "andi %0,%3" CR_TAB
15963 "eor %0,%1", xop, plen, 3);
15964 fixp_p = false;
15965 }
15966 }
15967 else
15968 {
15969 /* XOP[2] is unused */
15970
15971 if (fixp_p && mask_fixed)
15972 {
15973 avr_asm_len ("mov %0,%1", xop, plen, 1);
15974 fixp_p = false;
15975 }
15976 }
15977
15978 /* Move/insert remaining bits. */
15979
15980 avr_move_bits (xop, map, fixp_p, plen);
15981
15982 return "";
15983 }
15984
15985
15986 /* IDs for all the AVR builtins. */
15987
15988 enum avr_builtin_id
15989 {
15990 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME, ATTRS) \
15991 AVR_BUILTIN_ ## NAME,
15992 #include "builtins.def"
15993 #undef DEF_BUILTIN
15994
15995 AVR_BUILTIN_COUNT
15996 };
15997
15998 struct GTY(()) avr_builtin_description
15999 {
16000 enum insn_code icode;
16001 int n_args;
16002 tree fndecl;
16003 };
16004
16005
16006 /* Notice that avr_bdesc[] and avr_builtin_id are initialized in such a way
16007 that a built-in's ID can be used to access the built-in by means of
16008 avr_bdesc[ID] */
16009
16010 static GTY(()) avr_builtin_description
16011 avr_bdesc[AVR_BUILTIN_COUNT] =
16012 {
16013 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, LIBNAME, ATTRS) \
16014 { (enum insn_code) CODE_FOR_ ## ICODE, N_ARGS, NULL_TREE },
16015 #include "builtins.def"
16016 #undef DEF_BUILTIN
16017 };
16018
16019
16020 /* Implement `TARGET_BUILTIN_DECL'. */
16021
16022 static tree
16023 avr_builtin_decl (unsigned id, bool /*initialize_p*/)
16024 {
16025 if (id < AVR_BUILTIN_COUNT)
16026 return avr_bdesc[id].fndecl;
16027
16028 return error_mark_node;
16029 }
16030
16031
16032 static void
16033 avr_init_builtin_int24 (void)
16034 {
16035 for (int i = 0; i < NUM_INT_N_ENTS; ++i)
16036 if (int_n_data[i].bitsize == 24)
16037 {
16038 tree uint24_type = int_n_trees[i].unsigned_type;
16039 lang_hooks.types.register_builtin_type (uint24_type, "__uint24");
16040 break;
16041 }
16042 }
16043
16044
16045 /* Return a function signature type similar to strlen, but where
16046 the address is qualified by named address-space AS. */
16047
16048 static tree
16049 avr_ftype_strlen (addr_space_t as)
16050 {
16051 tree const_AS_char_node
16052 = build_qualified_type (char_type_node,
16053 TYPE_QUAL_CONST | ENCODE_QUAL_ADDR_SPACE (as));
16054 tree const_AS_ptr_type_node
16055 = build_pointer_type_for_mode (const_AS_char_node,
16056 avr_addr_space_pointer_mode (as), false);
16057 tree size_ftype_const_AS_char_ptr
16058 = build_function_type_list (size_type_node, const_AS_ptr_type_node, NULL);
16059
16060 return size_ftype_const_AS_char_ptr;
16061 }
16062
16063
16064 /* Implement `TARGET_INIT_BUILTINS' */
16065 /* Set up all builtin functions for this target. */
16066
16067 static void
16068 avr_init_builtins (void)
16069 {
16070 tree void_ftype_void
16071 = build_function_type_list (void_type_node, NULL_TREE);
16072 tree uintQI_ftype_uintQI
16073 = build_function_type_list (unsigned_intQI_type_node,
16074 unsigned_intQI_type_node,
16075 NULL_TREE);
16076 tree uintQI_ftype_uintQI_uintQI
16077 = build_function_type_list (unsigned_intQI_type_node,
16078 unsigned_intQI_type_node,
16079 unsigned_intQI_type_node,
16080 NULL_TREE);
16081 tree uintHI_ftype_uintQI_uintQI
16082 = build_function_type_list (unsigned_intHI_type_node,
16083 unsigned_intQI_type_node,
16084 unsigned_intQI_type_node,
16085 NULL_TREE);
16086 tree intHI_ftype_intQI_intQI
16087 = build_function_type_list (intHI_type_node,
16088 intQI_type_node,
16089 intQI_type_node,
16090 NULL_TREE);
16091 tree intHI_ftype_intQI_uintQI
16092 = build_function_type_list (intHI_type_node,
16093 intQI_type_node,
16094 unsigned_intQI_type_node,
16095 NULL_TREE);
16096 tree void_ftype_uintSI
16097 = build_function_type_list (void_type_node,
16098 unsigned_intSI_type_node,
16099 NULL_TREE);
16100
16101 tree uintQI_ftype_uintSI_uintQI_uintQI
16102 = build_function_type_list (unsigned_intQI_type_node,
16103 unsigned_intSI_type_node,
16104 unsigned_intQI_type_node,
16105 unsigned_intQI_type_node,
16106 NULL_TREE);
16107
16108 tree const_memx_void_node
16109 = build_qualified_type (void_type_node,
16110 TYPE_QUAL_CONST
16111 | ENCODE_QUAL_ADDR_SPACE (ADDR_SPACE_MEMX));
16112
16113 tree const_memx_ptr_type_node
16114 = build_pointer_type_for_mode (const_memx_void_node, PSImode, false);
16115
16116 tree intQI_ftype_const_memx_ptr
16117 = build_function_type_list (intQI_type_node,
16118 const_memx_ptr_type_node,
16119 NULL);
16120
16121 tree strlen_flash_node = avr_ftype_strlen (ADDR_SPACE_FLASH);
16122 tree strlen_flashx_node = avr_ftype_strlen (ADDR_SPACE_FLASHX);
16123 tree strlen_memx_node = avr_ftype_strlen (ADDR_SPACE_MEMX);
16124
16125 #define ITYP(T) \
16126 lang_hooks.types.type_for_size (TYPE_PRECISION (T), TYPE_UNSIGNED (T))
16127
16128 #define FX_FTYPE_FX(fx) \
16129 tree fx##r_ftype_##fx##r \
16130 = build_function_type_list (node_##fx##r, node_##fx##r, NULL); \
16131 tree fx##k_ftype_##fx##k \
16132 = build_function_type_list (node_##fx##k, node_##fx##k, NULL)
16133
16134 #define FX_FTYPE_FX_INT(fx) \
16135 tree fx##r_ftype_##fx##r_int \
16136 = build_function_type_list (node_##fx##r, node_##fx##r, \
16137 integer_type_node, NULL); \
16138 tree fx##k_ftype_##fx##k_int \
16139 = build_function_type_list (node_##fx##k, node_##fx##k, \
16140 integer_type_node, NULL)
16141
16142 #define INT_FTYPE_FX(fx) \
16143 tree int_ftype_##fx##r \
16144 = build_function_type_list (integer_type_node, node_##fx##r, NULL); \
16145 tree int_ftype_##fx##k \
16146 = build_function_type_list (integer_type_node, node_##fx##k, NULL)
16147
16148 #define INTX_FTYPE_FX(fx) \
16149 tree int##fx##r_ftype_##fx##r \
16150 = build_function_type_list (ITYP (node_##fx##r), node_##fx##r, NULL); \
16151 tree int##fx##k_ftype_##fx##k \
16152 = build_function_type_list (ITYP (node_##fx##k), node_##fx##k, NULL)
16153
16154 #define FX_FTYPE_INTX(fx) \
16155 tree fx##r_ftype_int##fx##r \
16156 = build_function_type_list (node_##fx##r, ITYP (node_##fx##r), NULL); \
16157 tree fx##k_ftype_int##fx##k \
16158 = build_function_type_list (node_##fx##k, ITYP (node_##fx##k), NULL)
16159
16160 tree node_hr = short_fract_type_node;
16161 tree node_nr = fract_type_node;
16162 tree node_lr = long_fract_type_node;
16163 tree node_llr = long_long_fract_type_node;
16164
16165 tree node_uhr = unsigned_short_fract_type_node;
16166 tree node_unr = unsigned_fract_type_node;
16167 tree node_ulr = unsigned_long_fract_type_node;
16168 tree node_ullr = unsigned_long_long_fract_type_node;
16169
16170 tree node_hk = short_accum_type_node;
16171 tree node_nk = accum_type_node;
16172 tree node_lk = long_accum_type_node;
16173 tree node_llk = long_long_accum_type_node;
16174
16175 tree node_uhk = unsigned_short_accum_type_node;
16176 tree node_unk = unsigned_accum_type_node;
16177 tree node_ulk = unsigned_long_accum_type_node;
16178 tree node_ullk = unsigned_long_long_accum_type_node;
16179
16180
16181 /* For absfx builtins. */
16182
16183 FX_FTYPE_FX (h);
16184 FX_FTYPE_FX (n);
16185 FX_FTYPE_FX (l);
16186 FX_FTYPE_FX (ll);
16187
16188 /* For roundfx builtins. */
16189
16190 FX_FTYPE_FX_INT (h);
16191 FX_FTYPE_FX_INT (n);
16192 FX_FTYPE_FX_INT (l);
16193 FX_FTYPE_FX_INT (ll);
16194
16195 FX_FTYPE_FX_INT (uh);
16196 FX_FTYPE_FX_INT (un);
16197 FX_FTYPE_FX_INT (ul);
16198 FX_FTYPE_FX_INT (ull);
16199
16200 /* For countlsfx builtins. */
16201
16202 INT_FTYPE_FX (h);
16203 INT_FTYPE_FX (n);
16204 INT_FTYPE_FX (l);
16205 INT_FTYPE_FX (ll);
16206
16207 INT_FTYPE_FX (uh);
16208 INT_FTYPE_FX (un);
16209 INT_FTYPE_FX (ul);
16210 INT_FTYPE_FX (ull);
16211
16212 /* For bitsfx builtins. */
16213
16214 INTX_FTYPE_FX (h);
16215 INTX_FTYPE_FX (n);
16216 INTX_FTYPE_FX (l);
16217 INTX_FTYPE_FX (ll);
16218
16219 INTX_FTYPE_FX (uh);
16220 INTX_FTYPE_FX (un);
16221 INTX_FTYPE_FX (ul);
16222 INTX_FTYPE_FX (ull);
16223
16224 /* For fxbits builtins. */
16225
16226 FX_FTYPE_INTX (h);
16227 FX_FTYPE_INTX (n);
16228 FX_FTYPE_INTX (l);
16229 FX_FTYPE_INTX (ll);
16230
16231 FX_FTYPE_INTX (uh);
16232 FX_FTYPE_INTX (un);
16233 FX_FTYPE_INTX (ul);
16234 FX_FTYPE_INTX (ull);
16235
16236 tree attr_const = tree_cons (get_identifier ("const"), NULL, NULL);
16237
16238 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME, ATTRS) \
16239 { \
16240 int id = AVR_BUILTIN_ ## NAME; \
16241 const char *Name = "__builtin_avr_" #NAME; \
16242 char *name = (char *) alloca (1 + strlen (Name)); \
16243 \
16244 gcc_assert (id < AVR_BUILTIN_COUNT); \
16245 avr_bdesc[id].fndecl \
16246 = add_builtin_function (avr_tolower (name, Name), TYPE, id, \
16247 BUILT_IN_MD, LIBNAME, ATTRS); \
16248 }
16249 #include "builtins.def"
16250 #undef DEF_BUILTIN
16251
16252 avr_init_builtin_int24 ();
16253 }
16254
16255
16256 /* Subroutine of avr_expand_builtin to expand vanilla builtins
16257 with non-void result and 1 ... 3 arguments. */
16258
16259 static rtx
16260 avr_default_expand_builtin (enum insn_code icode, tree exp, rtx target)
16261 {
16262 rtx pat, xop[3];
16263 int n_args = call_expr_nargs (exp);
16264 machine_mode tmode = insn_data[icode].operand[0].mode;
16265
16266 gcc_assert (n_args >= 1 && n_args <= 3);
16267
16268 if (target == NULL_RTX
16269 || GET_MODE (target) != tmode
16270 || !insn_data[icode].operand[0].predicate (target, tmode))
16271 {
16272 target = gen_reg_rtx (tmode);
16273 }
16274
16275 for (int n = 0; n < n_args; n++)
16276 {
16277 tree arg = CALL_EXPR_ARG (exp, n);
16278 rtx op = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
16279 machine_mode opmode = GET_MODE (op);
16280 machine_mode mode = insn_data[icode].operand[n + 1].mode;
16281
16282 if ((opmode == SImode || opmode == VOIDmode) && mode == HImode)
16283 {
16284 opmode = HImode;
16285 op = gen_lowpart (HImode, op);
16286 }
16287
16288 /* In case the insn wants input operands in modes different from
16289 the result, abort. */
16290
16291 gcc_assert (opmode == mode || opmode == VOIDmode);
16292
16293 if (!insn_data[icode].operand[n + 1].predicate (op, mode))
16294 op = copy_to_mode_reg (mode, op);
16295
16296 xop[n] = op;
16297 }
16298
16299 switch (n_args)
16300 {
16301 case 1: pat = GEN_FCN (icode) (target, xop[0]); break;
16302 case 2: pat = GEN_FCN (icode) (target, xop[0], xop[1]); break;
16303 case 3: pat = GEN_FCN (icode) (target, xop[0], xop[1], xop[2]); break;
16304
16305 default:
16306 gcc_unreachable ();
16307 }
16308
16309 if (pat == NULL_RTX)
16310 return NULL_RTX;
16311
16312 emit_insn (pat);
16313
16314 return target;
16315 }
16316
16317
16318 /* Implement `TARGET_EXPAND_BUILTIN'. */
16319 /* Expand an expression EXP that calls a built-in function,
16320 with result going to TARGET if that's convenient
16321 (and in mode MODE if that's convenient).
16322 SUBTARGET may be used as the target for computing one of EXP's operands.
16323 IGNORE is nonzero if the value is to be ignored. */
16324
16325 static rtx
16326 avr_expand_builtin (tree exp, rtx target, rtx /*subtarget*/,
16327 machine_mode mode, int ignore)
16328 {
16329 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16330 const char *bname = IDENTIFIER_POINTER (DECL_NAME (fndecl));
16331 unsigned int id = DECL_MD_FUNCTION_CODE (fndecl);
16332 const avr_builtin_description *d = &avr_bdesc[id];
16333 tree arg0;
16334 rtx op0;
16335
16336 gcc_assert (id < AVR_BUILTIN_COUNT);
16337
16338 switch (id)
16339 {
16340 case AVR_BUILTIN_NOP:
16341 emit_insn (gen_nopv (GEN_INT (1)));
16342 return 0;
16343
16344 case AVR_BUILTIN_DELAY_CYCLES:
16345 {
16346 arg0 = CALL_EXPR_ARG (exp, 0);
16347 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
16348
16349 if (!CONST_INT_P (op0))
16350 error ("%s expects a compile time integer constant", bname);
16351 else
16352 avr_expand_delay_cycles (op0);
16353
16354 return NULL_RTX;
16355 }
16356
16357 case AVR_BUILTIN_NOPS:
16358 {
16359 arg0 = CALL_EXPR_ARG (exp, 0);
16360 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
16361
16362 if (!CONST_INT_P (op0))
16363 error ("%s expects a compile time integer constant", bname);
16364 else
16365 avr_expand_nops (op0);
16366
16367 return NULL_RTX;
16368 }
16369
16370 case AVR_BUILTIN_MASK1:
16371 {
16372 arg0 = CALL_EXPR_ARG (exp, 0);
16373 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
16374 int ival = CONST_INT_P (op0) ? 0xff & INTVAL (op0) : 0;
16375
16376 if (ival != 0x01 && ival != 0x7f && ival != 0x80 && ival != 0xfe)
16377 {
16378 error ("%s expects a compile time integer constant of 0x01, "
16379 "0x7f, 0x80 or 0xfe as first argument", bname);
16380 return target;
16381 }
16382
16383 break;
16384 }
16385
16386 case AVR_BUILTIN_INSERT_BITS:
16387 {
16388 arg0 = CALL_EXPR_ARG (exp, 0);
16389 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
16390
16391 if (!CONST_INT_P (op0))
16392 {
16393 error ("%s expects a compile time long integer constant"
16394 " as first argument", bname);
16395 return target;
16396 }
16397
16398 break;
16399 }
16400
16401 case AVR_BUILTIN_ROUNDHR: case AVR_BUILTIN_ROUNDUHR:
16402 case AVR_BUILTIN_ROUNDR: case AVR_BUILTIN_ROUNDUR:
16403 case AVR_BUILTIN_ROUNDLR: case AVR_BUILTIN_ROUNDULR:
16404 case AVR_BUILTIN_ROUNDLLR: case AVR_BUILTIN_ROUNDULLR:
16405
16406 case AVR_BUILTIN_ROUNDHK: case AVR_BUILTIN_ROUNDUHK:
16407 case AVR_BUILTIN_ROUNDK: case AVR_BUILTIN_ROUNDUK:
16408 case AVR_BUILTIN_ROUNDLK: case AVR_BUILTIN_ROUNDULK:
16409 case AVR_BUILTIN_ROUNDLLK: case AVR_BUILTIN_ROUNDULLK:
16410
16411 /* Warn about odd rounding. Rounding points >= FBIT will have
16412 no effect. */
16413
16414 if (TREE_CODE (CALL_EXPR_ARG (exp, 1)) != INTEGER_CST)
16415 break;
16416
16417 int rbit = (int) TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1));
16418
16419 if (rbit >= (int) GET_MODE_FBIT (mode))
16420 {
16421 warning (OPT_Wextra, "rounding to %d bits has no effect for "
16422 "fixed-point value with %d fractional bits",
16423 rbit, GET_MODE_FBIT (mode));
16424
16425 return expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, mode,
16426 EXPAND_NORMAL);
16427 }
16428 else if (rbit <= - (int) GET_MODE_IBIT (mode))
16429 {
16430 warning (0, "rounding result will always be 0");
16431 return CONST0_RTX (mode);
16432 }
16433
16434 /* The rounding points RP satisfies now: -IBIT < RP < FBIT.
16435
16436 TR 18037 only specifies results for RP > 0. However, the
16437 remaining cases of -IBIT < RP <= 0 can easily be supported
16438 without any additional overhead. */
16439
16440 break; /* round */
16441 }
16442
16443 /* No fold found and no insn: Call support function from libgcc. */
16444
16445 if (d->icode == CODE_FOR_nothing
16446 && DECL_ASSEMBLER_NAME (get_callee_fndecl (exp)) != NULL_TREE)
16447 {
16448 return expand_call (exp, target, ignore);
16449 }
16450
16451 /* No special treatment needed: vanilla expand. */
16452
16453 gcc_assert (d->icode != CODE_FOR_nothing);
16454 gcc_assert (d->n_args == call_expr_nargs (exp));
16455
16456 if (d->n_args == 0)
16457 {
16458 emit_insn ((GEN_FCN (d->icode)) (target));
16459 return NULL_RTX;
16460 }
16461
16462 return avr_default_expand_builtin (d->icode, exp, target);
16463 }
16464
16465
16466 /* Helper for `avr_fold_builtin' that folds absfx (FIXED_CST). */
16467
16468 static tree
16469 avr_fold_absfx (tree tval)
16470 {
16471 if (FIXED_CST != TREE_CODE (tval))
16472 return NULL_TREE;
16473
16474 /* Our fixed-points have no padding: Use double_int payload directly. */
16475
16476 FIXED_VALUE_TYPE fval = TREE_FIXED_CST (tval);
16477 unsigned int bits = GET_MODE_BITSIZE (fval.mode);
16478 double_int ival = fval.data.sext (bits);
16479
16480 if (!ival.is_negative ())
16481 return tval;
16482
16483 /* ISO/IEC TR 18037, 7.18a.6.2: The absfx functions are saturating. */
16484
16485 fval.data = (ival == double_int::min_value (bits, false).sext (bits))
16486 ? double_int::max_value (bits, false)
16487 : -ival;
16488
16489 return build_fixed (TREE_TYPE (tval), fval);
16490 }
16491
16492
16493 /* Implement `TARGET_FOLD_BUILTIN'. */
16494
16495 static tree
16496 avr_fold_builtin (tree fndecl, int /*n_args*/, tree *arg, bool /*ignore*/)
16497 {
16498 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
16499 tree val_type = TREE_TYPE (TREE_TYPE (fndecl));
16500
16501 if (!optimize)
16502 return NULL_TREE;
16503
16504 switch (fcode)
16505 {
16506 default:
16507 break;
16508
16509 case AVR_BUILTIN_SWAP:
16510 {
16511 return fold_build2 (LROTATE_EXPR, val_type, arg[0],
16512 build_int_cst (val_type, 4));
16513 }
16514
16515 case AVR_BUILTIN_STRLEN_FLASH:
16516 case AVR_BUILTIN_STRLEN_FLASHX:
16517 case AVR_BUILTIN_STRLEN_MEMX:
16518 if (tree len = c_strlen (arg[0], 0))
16519 return len;
16520 break;
16521
16522 case AVR_BUILTIN_ABSHR:
16523 case AVR_BUILTIN_ABSR:
16524 case AVR_BUILTIN_ABSLR:
16525 case AVR_BUILTIN_ABSLLR:
16526
16527 case AVR_BUILTIN_ABSHK:
16528 case AVR_BUILTIN_ABSK:
16529 case AVR_BUILTIN_ABSLK:
16530 case AVR_BUILTIN_ABSLLK:
16531 /* GCC is not good with folding ABS for fixed-point. Do it by hand. */
16532
16533 return avr_fold_absfx (arg[0]);
16534
16535 case AVR_BUILTIN_BITSHR: case AVR_BUILTIN_HRBITS:
16536 case AVR_BUILTIN_BITSHK: case AVR_BUILTIN_HKBITS:
16537 case AVR_BUILTIN_BITSUHR: case AVR_BUILTIN_UHRBITS:
16538 case AVR_BUILTIN_BITSUHK: case AVR_BUILTIN_UHKBITS:
16539
16540 case AVR_BUILTIN_BITSR: case AVR_BUILTIN_RBITS:
16541 case AVR_BUILTIN_BITSK: case AVR_BUILTIN_KBITS:
16542 case AVR_BUILTIN_BITSUR: case AVR_BUILTIN_URBITS:
16543 case AVR_BUILTIN_BITSUK: case AVR_BUILTIN_UKBITS:
16544
16545 case AVR_BUILTIN_BITSLR: case AVR_BUILTIN_LRBITS:
16546 case AVR_BUILTIN_BITSLK: case AVR_BUILTIN_LKBITS:
16547 case AVR_BUILTIN_BITSULR: case AVR_BUILTIN_ULRBITS:
16548 case AVR_BUILTIN_BITSULK: case AVR_BUILTIN_ULKBITS:
16549
16550 case AVR_BUILTIN_BITSLLR: case AVR_BUILTIN_LLRBITS:
16551 case AVR_BUILTIN_BITSLLK: case AVR_BUILTIN_LLKBITS:
16552 case AVR_BUILTIN_BITSULLR: case AVR_BUILTIN_ULLRBITS:
16553 case AVR_BUILTIN_BITSULLK: case AVR_BUILTIN_ULLKBITS:
16554
16555 gcc_assert (TYPE_PRECISION (val_type)
16556 == TYPE_PRECISION (TREE_TYPE (arg[0])));
16557
16558 return build1 (VIEW_CONVERT_EXPR, val_type, arg[0]);
16559
16560 case AVR_BUILTIN_MASK1:
16561 {
16562 tree tmask = arg[0];
16563 tree toffs = arg[1];
16564
16565 if (TREE_CODE (tmask) == INTEGER_CST
16566 && TREE_CODE (toffs) == INTEGER_CST)
16567 {
16568 switch (0xff & TREE_INT_CST_LOW (tmask))
16569 {
16570 case 0x01:
16571 case 0xfe:
16572 return fold_build2 (LROTATE_EXPR, val_type, arg[0], toffs);
16573
16574 case 0x80:
16575 case 0x7f:
16576 return fold_build2 (RROTATE_EXPR, val_type, arg[0], toffs);
16577 }
16578 }
16579 break;
16580 } // AVR_BUILTIN_MASK1
16581
16582 case AVR_BUILTIN_INSERT_BITS:
16583 {
16584 tree tbits = arg[1];
16585 tree tval = arg[2];
16586 tree map_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
16587 bool changed = false;
16588 avr_map_op_t best_g;
16589
16590 if (TREE_CODE (arg[0]) != INTEGER_CST)
16591 {
16592 /* No constant as first argument: Don't fold this and run into
16593 error in avr_expand_builtin. */
16594
16595 break;
16596 }
16597
16598 tree tmap = wide_int_to_tree (map_type, wi::to_wide (arg[0]));
16599 unsigned int map = TREE_INT_CST_LOW (tmap);
16600
16601 if (TREE_CODE (tval) != INTEGER_CST
16602 && avr_map_metric (map, MAP_MASK_PREIMAGE_F) == 0)
16603 {
16604 /* There are no F in the map, i.e. 3rd operand is unused.
16605 Replace that argument with some constant to render
16606 respective input unused. */
16607
16608 tval = build_int_cst (val_type, 0);
16609 changed = true;
16610 }
16611
16612 if (TREE_CODE (tbits) != INTEGER_CST
16613 && avr_map_metric (map, MAP_PREIMAGE_0_7) == 0)
16614 {
16615 /* Similar for the bits to be inserted. If they are unused,
16616 we can just as well pass 0. */
16617
16618 tbits = build_int_cst (val_type, 0);
16619 }
16620
16621 if (TREE_CODE (tbits) == INTEGER_CST)
16622 {
16623 /* Inserting bits known at compile time is easy and can be
16624 performed by AND and OR with appropriate masks. */
16625
16626 int bits = TREE_INT_CST_LOW (tbits);
16627 int mask_ior = 0, mask_and = 0xff;
16628
16629 for (size_t i = 0; i < 8; i++)
16630 {
16631 int mi = avr_map (map, i);
16632
16633 if (mi < 8)
16634 {
16635 if (bits & (1 << mi)) mask_ior |= (1 << i);
16636 else mask_and &= ~(1 << i);
16637 }
16638 }
16639
16640 tval = fold_build2 (BIT_IOR_EXPR, val_type, tval,
16641 build_int_cst (val_type, mask_ior));
16642 return fold_build2 (BIT_AND_EXPR, val_type, tval,
16643 build_int_cst (val_type, mask_and));
16644 }
16645
16646 if (changed)
16647 return build_call_expr (fndecl, 3, tmap, tbits, tval);
16648
16649 /* If bits don't change their position, we can use vanilla logic
16650 to merge the two arguments... */
16651
16652 if (avr_map_metric (map, MAP_NONFIXED_0_7) == 0
16653 // ...except when we are copying just one bit. In that
16654 // case, BLD/BST is better than XOR/AND/XOR, see PR90622.
16655 && avr_map_metric (map, MAP_FIXED_0_7) != 1)
16656 {
16657 int mask_f = avr_map_metric (map, MAP_MASK_PREIMAGE_F);
16658 tree tres, tmask = build_int_cst (val_type, mask_f ^ 0xff);
16659
16660 tres = fold_build2 (BIT_XOR_EXPR, val_type, tbits, tval);
16661 tres = fold_build2 (BIT_AND_EXPR, val_type, tres, tmask);
16662 return fold_build2 (BIT_XOR_EXPR, val_type, tres, tval);
16663 }
16664
16665 /* Try to decomposing map to reduce overall cost. */
16666
16667 if (avr_log.builtin)
16668 avr_edump ("\n%?: %x\n%?: ROL cost: ", map);
16669
16670 best_g = avr_map_op[0];
16671 best_g.cost = 1000;
16672
16673 for (size_t i = 0; i < ARRAY_SIZE (avr_map_op); i++)
16674 {
16675 avr_map_op_t g
16676 = avr_map_decompose (map, avr_map_op + i,
16677 TREE_CODE (tval) == INTEGER_CST);
16678
16679 if (g.cost >= 0 && g.cost < best_g.cost)
16680 best_g = g;
16681 }
16682
16683 if (avr_log.builtin)
16684 avr_edump ("\n");
16685
16686 if (best_g.arg == 0)
16687 /* No optimization found */
16688 break;
16689
16690 /* Apply operation G to the 2nd argument. */
16691
16692 if (avr_log.builtin)
16693 avr_edump ("%?: using OP(%s%d, %x) cost %d\n",
16694 best_g.str, best_g.arg, best_g.map, best_g.cost);
16695
16696 /* Do right-shifts arithmetically: They copy the MSB instead of
16697 shifting in a non-usable value (0) as with logic right-shift. */
16698
16699 tbits = fold_convert (signed_char_type_node, tbits);
16700 tbits = fold_build2 (best_g.code, signed_char_type_node, tbits,
16701 build_int_cst (val_type, best_g.arg));
16702 tbits = fold_convert (val_type, tbits);
16703
16704 /* Use map o G^-1 instead of original map to undo the effect of G. */
16705
16706 tmap = wide_int_to_tree (map_type, best_g.map);
16707
16708 return build_call_expr (fndecl, 3, tmap, tbits, tval);
16709 } /* AVR_BUILTIN_INSERT_BITS */
16710 }
16711
16712 return NULL_TREE;
16713 }
16714
16715
16716 /* Implement `TARGET_MD_ASM_ADJUST'. */
16717 /* Prepend to CLOBBERS hard registers that are automatically clobbered
16718 for an asm. We do this for CC_REGNUM to maintain source compatibility
16719 with the original cc0-based compiler. */
16720
16721 static rtx_insn *
16722 avr_md_asm_adjust (vec<rtx> & /*outputs*/, vec<rtx> & /*inputs*/,
16723 vec<machine_mode> & /*input_modes*/,
16724 vec<const char *> & /*constraints*/,
16725 vec<rtx> & /*uses*/,
16726 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs,
16727 location_t /*loc*/)
16728 {
16729 clobbers.safe_push (cc_reg_rtx);
16730 SET_HARD_REG_BIT (clobbered_regs, REG_CC);
16731 return nullptr;
16732 }
16733
16734
16735 /* Implement `TARGET_C_MODE_FOR_FLOATING_TYPE'. Return SFmode or DFmode
16736 for TI_{LONG_,}DOUBLE_TYPE which is for {long,} double type, go with
16737 the default one for the others. */
16738
16739 static machine_mode
16740 avr_c_mode_for_floating_type (tree_index ti)
16741 {
16742 if (ti == TI_DOUBLE_TYPE)
16743 return avropt_double == 32 ? SFmode : DFmode;
16744 if (ti == TI_LONG_DOUBLE_TYPE)
16745 return avropt_long_double == 32 ? SFmode : DFmode;
16746 return default_mode_for_floating_type (ti);
16747 }
16748
16749
16750 /* Worker function for `FLOAT_LIB_COMPARE_RETURNS_BOOL'. */
16751
16752 bool
16753 avr_float_lib_compare_returns_bool (machine_mode mode, rtx_code)
16754 {
16755 if (mode == DFmode)
16756 {
16757 #if WITH_DOUBLE_COMPARISON == 2
16758 return true;
16759 #endif
16760 }
16761
16762 // This is the GCC default and also what AVR-LibC implements.
16763 return false;
16764 }
16765
16766
16767 /* Implement `TARGET_UNWIND_WORD_MODE'. */
16768
16769 static scalar_int_mode
16770 avr_unwind_word_mode ()
16771 {
16772 return Pmode;
16773 }
16774
16775
16776 /* Implement `TARGET_LRA_P'. */
16777
16778 static bool
16779 avr_use_lra_p ()
16780 {
16781 return avropt_lra_p;
16782 }
16783
16784
16785
16786 /* Initialize the GCC target structure. */
16787
16788 #undef TARGET_ASM_ALIGNED_HI_OP
16789 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
16790 #undef TARGET_ASM_ALIGNED_SI_OP
16791 #define TARGET_ASM_ALIGNED_SI_OP "\t.long\t"
16792 #undef TARGET_ASM_UNALIGNED_HI_OP
16793 #define TARGET_ASM_UNALIGNED_HI_OP "\t.word\t"
16794 #undef TARGET_ASM_UNALIGNED_SI_OP
16795 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
16796 #undef TARGET_ASM_INTEGER
16797 #define TARGET_ASM_INTEGER avr_assemble_integer
16798 #undef TARGET_ASM_FILE_START
16799 #define TARGET_ASM_FILE_START avr_file_start
16800 #undef TARGET_ASM_FILE_END
16801 #define TARGET_ASM_FILE_END avr_file_end
16802
16803 #undef TARGET_ASM_FUNCTION_END_PROLOGUE
16804 #define TARGET_ASM_FUNCTION_END_PROLOGUE avr_asm_function_end_prologue
16805 #undef TARGET_ASM_FUNCTION_BEGIN_EPILOGUE
16806 #define TARGET_ASM_FUNCTION_BEGIN_EPILOGUE avr_asm_function_begin_epilogue
16807
16808 #undef TARGET_FUNCTION_VALUE
16809 #define TARGET_FUNCTION_VALUE avr_function_value
16810 #undef TARGET_LIBCALL_VALUE
16811 #define TARGET_LIBCALL_VALUE avr_libcall_value
16812 #undef TARGET_FUNCTION_VALUE_REGNO_P
16813 #define TARGET_FUNCTION_VALUE_REGNO_P avr_function_value_regno_p
16814
16815 #undef TARGET_ATTRIBUTE_TABLE
16816 #define TARGET_ATTRIBUTE_TABLE avr_attribute_table
16817 #undef TARGET_INSERT_ATTRIBUTES
16818 #define TARGET_INSERT_ATTRIBUTES avr_insert_attributes
16819 #undef TARGET_SECTION_TYPE_FLAGS
16820 #define TARGET_SECTION_TYPE_FLAGS avr_section_type_flags
16821
16822 #undef TARGET_ASM_NAMED_SECTION
16823 #define TARGET_ASM_NAMED_SECTION avr_asm_named_section
16824 #undef TARGET_ASM_INIT_SECTIONS
16825 #define TARGET_ASM_INIT_SECTIONS avr_asm_init_sections
16826 #undef TARGET_ENCODE_SECTION_INFO
16827 #define TARGET_ENCODE_SECTION_INFO avr_encode_section_info
16828 #undef TARGET_ASM_SELECT_SECTION
16829 #define TARGET_ASM_SELECT_SECTION avr_asm_select_section
16830
16831 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
16832 #define TARGET_ASM_FINAL_POSTSCAN_INSN avr_asm_final_postscan_insn
16833
16834 #undef TARGET_INSN_COST
16835 #define TARGET_INSN_COST avr_insn_cost
16836 #undef TARGET_REGISTER_MOVE_COST
16837 #define TARGET_REGISTER_MOVE_COST avr_register_move_cost
16838 #undef TARGET_MEMORY_MOVE_COST
16839 #define TARGET_MEMORY_MOVE_COST avr_memory_move_cost
16840 #undef TARGET_RTX_COSTS
16841 #define TARGET_RTX_COSTS avr_rtx_costs
16842 #undef TARGET_ADDRESS_COST
16843 #define TARGET_ADDRESS_COST avr_address_cost
16844 #undef TARGET_FUNCTION_ARG
16845 #define TARGET_FUNCTION_ARG avr_function_arg
16846 #undef TARGET_FUNCTION_ARG_ADVANCE
16847 #define TARGET_FUNCTION_ARG_ADVANCE avr_function_arg_advance
16848
16849 #undef TARGET_SET_CURRENT_FUNCTION
16850 #define TARGET_SET_CURRENT_FUNCTION avr_set_current_function
16851
16852 #undef TARGET_RETURN_IN_MEMORY
16853 #define TARGET_RETURN_IN_MEMORY avr_return_in_memory
16854
16855 #undef TARGET_STRICT_ARGUMENT_NAMING
16856 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
16857
16858 #undef TARGET_CONDITIONAL_REGISTER_USAGE
16859 #define TARGET_CONDITIONAL_REGISTER_USAGE avr_conditional_register_usage
16860
16861 #undef TARGET_HARD_REGNO_NREGS
16862 #define TARGET_HARD_REGNO_NREGS avr_hard_regno_nregs
16863
16864 #undef TARGET_HARD_REGNO_MODE_OK
16865 #define TARGET_HARD_REGNO_MODE_OK avr_hard_regno_mode_ok
16866 #undef TARGET_HARD_REGNO_SCRATCH_OK
16867 #define TARGET_HARD_REGNO_SCRATCH_OK avr_hard_regno_scratch_ok
16868 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
16869 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
16870 avr_hard_regno_call_part_clobbered
16871
16872 #undef TARGET_CASE_VALUES_THRESHOLD
16873 #define TARGET_CASE_VALUES_THRESHOLD avr_case_values_threshold
16874
16875 #undef TARGET_FRAME_POINTER_REQUIRED
16876 #define TARGET_FRAME_POINTER_REQUIRED avr_frame_pointer_required_p
16877 #undef TARGET_CAN_ELIMINATE
16878 #define TARGET_CAN_ELIMINATE avr_can_eliminate
16879
16880 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
16881 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS avr_allocate_stack_slots_for_args
16882
16883 #undef TARGET_WARN_FUNC_RETURN
16884 #define TARGET_WARN_FUNC_RETURN avr_warn_func_return
16885
16886 #undef TARGET_CLASS_LIKELY_SPILLED_P
16887 #define TARGET_CLASS_LIKELY_SPILLED_P avr_class_likely_spilled_p
16888
16889 #undef TARGET_CLASS_MAX_NREGS
16890 #define TARGET_CLASS_MAX_NREGS avr_class_max_nregs
16891
16892 #undef TARGET_OPTION_OVERRIDE
16893 #define TARGET_OPTION_OVERRIDE avr_option_override
16894
16895 #undef TARGET_CANNOT_MODIFY_JUMPS_P
16896 #define TARGET_CANNOT_MODIFY_JUMPS_P avr_cannot_modify_jumps_p
16897
16898 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
16899 #define TARGET_FUNCTION_OK_FOR_SIBCALL avr_function_ok_for_sibcall
16900
16901 #undef TARGET_INIT_BUILTINS
16902 #define TARGET_INIT_BUILTINS avr_init_builtins
16903
16904 #undef TARGET_BUILTIN_DECL
16905 #define TARGET_BUILTIN_DECL avr_builtin_decl
16906
16907 #undef TARGET_EXPAND_BUILTIN
16908 #define TARGET_EXPAND_BUILTIN avr_expand_builtin
16909
16910 #undef TARGET_FOLD_BUILTIN
16911 #define TARGET_FOLD_BUILTIN avr_fold_builtin
16912
16913 #undef TARGET_SCALAR_MODE_SUPPORTED_P
16914 #define TARGET_SCALAR_MODE_SUPPORTED_P avr_scalar_mode_supported_p
16915
16916 #undef TARGET_BUILD_BUILTIN_VA_LIST
16917 #define TARGET_BUILD_BUILTIN_VA_LIST avr_build_builtin_va_list
16918
16919 #undef TARGET_FIXED_POINT_SUPPORTED_P
16920 #define TARGET_FIXED_POINT_SUPPORTED_P hook_bool_void_true
16921
16922 #undef TARGET_CONVERT_TO_TYPE
16923 #define TARGET_CONVERT_TO_TYPE avr_convert_to_type
16924
16925 #undef TARGET_LRA_P
16926 #define TARGET_LRA_P avr_use_lra_p
16927
16928 #undef TARGET_ADDR_SPACE_SUBSET_P
16929 #define TARGET_ADDR_SPACE_SUBSET_P avr_addr_space_subset_p
16930
16931 #undef TARGET_ADDR_SPACE_CONVERT
16932 #define TARGET_ADDR_SPACE_CONVERT avr_addr_space_convert
16933
16934 #undef TARGET_ADDR_SPACE_ADDRESS_MODE
16935 #define TARGET_ADDR_SPACE_ADDRESS_MODE avr_addr_space_address_mode
16936
16937 #undef TARGET_ADDR_SPACE_POINTER_MODE
16938 #define TARGET_ADDR_SPACE_POINTER_MODE avr_addr_space_pointer_mode
16939
16940 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
16941 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P \
16942 avr_addr_space_legitimate_address_p
16943
16944 #undef TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS
16945 #define TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS avr_addr_space_legitimize_address
16946
16947 #undef TARGET_ADDR_SPACE_DIAGNOSE_USAGE
16948 #define TARGET_ADDR_SPACE_DIAGNOSE_USAGE avr_addr_space_diagnose_usage
16949
16950 #undef TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID
16951 #define TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID avr_addr_space_zero_address_valid
16952
16953 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
16954 #define TARGET_MODE_DEPENDENT_ADDRESS_P avr_mode_dependent_address_p
16955
16956 #undef TARGET_PRINT_OPERAND
16957 #define TARGET_PRINT_OPERAND avr_print_operand
16958 #undef TARGET_PRINT_OPERAND_ADDRESS
16959 #define TARGET_PRINT_OPERAND_ADDRESS avr_print_operand_address
16960 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
16961 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P avr_print_operand_punct_valid_p
16962
16963 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
16964 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
16965 avr_use_by_pieces_infrastructure_p
16966
16967 #undef TARGET_LEGITIMATE_COMBINED_INSN
16968 #define TARGET_LEGITIMATE_COMBINED_INSN avr_legitimate_combined_insn
16969
16970 #undef TARGET_STARTING_FRAME_OFFSET
16971 #define TARGET_STARTING_FRAME_OFFSET avr_starting_frame_offset
16972
16973 #undef TARGET_MD_ASM_ADJUST
16974 #define TARGET_MD_ASM_ADJUST avr_md_asm_adjust
16975
16976 #undef TARGET_CAN_INLINE_P
16977 #define TARGET_CAN_INLINE_P avr_can_inline_p
16978
16979 #undef TARGET_CANONICALIZE_COMPARISON
16980 #define TARGET_CANONICALIZE_COMPARISON avr_canonicalize_comparison
16981
16982 #undef TARGET_UNWIND_WORD_MODE
16983 #define TARGET_UNWIND_WORD_MODE avr_unwind_word_mode
16984
16985 /* According to the opening comment in PR86772, the following applies:
16986 "If the port does not (and never will in the future) need to mitigate
16987 against unsafe speculation." */
16988 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
16989 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
16990
16991 #undef TARGET_C_MODE_FOR_FLOATING_TYPE
16992 #define TARGET_C_MODE_FOR_FLOATING_TYPE avr_c_mode_for_floating_type
16993
16994 gcc_target targetm = TARGET_INITIALIZER;
16995
16996
16997 #include "gt-avr.h"