Debian Codegraph @

gcc/config/pru/pru.cc

raw
1 /* Target machine subroutines for TI PRU.
2 Copyright (C) 2014-2025 Free Software Foundation, Inc.
3 Dimitar Dimitrov <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 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 "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "df.h"
33 #include "memmodel.h"
34 #include "tm_p.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "emit-rtl.h"
38 #include "recog.h"
39 #include "diagnostic-core.h"
40 #include "output.h"
41 #include "insn-attr.h"
42 #include "flags.h"
43 #include "explow.h"
44 #include "calls.h"
45 #include "varasm.h"
46 #include "expr.h"
47 #include "toplev.h"
48 #include "langhooks.h"
49 #include "cfgrtl.h"
50 #include "stor-layout.h"
51 #include "dumpfile.h"
52 #include "builtins.h"
53 #include "pru-protos.h"
54
55 /* This file should be included last. */
56 #include "target-def.h"
57
58 #define INIT_ARRAY_ENTRY_BYTES 2
59
60 /* Global PRU CTABLE entries, filled in by pragmas, and used for fast
61 addressing via LBCO/SBCO instructions. */
62 struct pru_ctable_entry pru_ctable[32];
63
64 /* Forward function declarations. */
65 static bool prologue_saved_reg_p (int);
66 static void pru_reorg_loop (rtx_insn *);
67
68 struct GTY (()) machine_function
69 {
70 /* Current frame information, to be filled in by pru_compute_frame_layout
71 with register save masks, and offsets for the current function. */
72
73 /* Mask of registers to save. */
74 HARD_REG_SET save_mask;
75 /* Number of bytes that the entire frame takes up. */
76 int total_size;
77 /* Number of bytes that variables take up. */
78 int var_size;
79 /* Number of bytes that outgoing arguments take up. */
80 int out_args_size;
81 /* Number of bytes needed to store registers in frame. */
82 int save_reg_size;
83 /* Offset from new stack pointer to store registers. */
84 int save_regs_offset;
85 /* True if final frame layout is already calculated. */
86 bool initialized;
87 /* Number of doloop tags used so far. */
88 int doloop_tags;
89 /* True if the last tag was allocated to a doloop_end. */
90 bool doloop_tag_from_end;
91 };
92
93 /* Stack layout and calling conventions.
94
95 The PRU ABI defines r4 as Argument Pointer. GCC implements the same
96 semantics, but represents it with HARD_FRAME_POINTER_REGNUM and
97 names it FP. The stack layout is shown below:
98
99 ---------------------- high address
100 | incoming args
101 ------call-boundary---
102 | pretend_args ^
103 FP ---------------- | total
104 | save_regs | frame
105 --------------- | size
106 | local vars |
107 --------------- |
108 | outgoing args V
109 SP ---------------------- low address
110
111 */
112
113 #define PRU_STACK_ALIGN(LOC) ROUND_UP ((LOC), STACK_BOUNDARY / BITS_PER_UNIT)
114
115 /* Implement TARGET_COMPUTE_FRAME_LAYOUT. */
116 static void
117 pru_compute_frame_layout (void)
118 {
119 int regno;
120 HARD_REG_SET *save_mask;
121 int total_size;
122 int var_size;
123 int out_args_size;
124 int save_reg_size;
125
126 gcc_assert (!cfun->machine->initialized);
127
128 save_mask = &cfun->machine->save_mask;
129 CLEAR_HARD_REG_SET (*save_mask);
130
131 var_size = PRU_STACK_ALIGN ((HOST_WIDE_INT) get_frame_size ());
132 out_args_size = PRU_STACK_ALIGN ((HOST_WIDE_INT) crtl->outgoing_args_size);
133 total_size = var_size + out_args_size;
134
135 /* Calculate space needed for gp registers. */
136 save_reg_size = 0;
137 for (regno = 0; regno <= LAST_GP_REGNUM; regno++)
138 if (prologue_saved_reg_p (regno))
139 {
140 SET_HARD_REG_BIT (*save_mask, regno);
141 save_reg_size += 1;
142 }
143
144 save_reg_size = PRU_STACK_ALIGN (save_reg_size);
145 total_size += save_reg_size;
146 total_size += PRU_STACK_ALIGN (crtl->args.pretend_args_size);
147
148 /* Save other computed information. */
149 cfun->machine->total_size = total_size;
150 cfun->machine->var_size = var_size;
151 cfun->machine->out_args_size = out_args_size;
152 cfun->machine->save_reg_size = save_reg_size;
153 cfun->machine->initialized = reload_completed;
154 cfun->machine->save_regs_offset = out_args_size + var_size;
155 }
156
157 /* Emit efficient RTL equivalent of ADD3 with the given const_int for
158 frame-related registers.
159 op0 - Destination register.
160 op1 - First addendum operand (a register).
161 addendum - Second addendum operand (a constant).
162 kind - Note kind. REG_NOTE_MAX if no note must be added.
163 */
164 static rtx
165 pru_add3_frame_adjust (rtx op0, rtx op1, int addendum,
166 const enum reg_note kind)
167 {
168 rtx insn;
169
170 rtx op0_adjust = gen_rtx_SET (op0, plus_constant (Pmode, op1, addendum));
171
172 if (UBYTE_INT (addendum) || UBYTE_INT (-addendum))
173 insn = emit_insn (op0_adjust);
174 else
175 {
176 /* Help the compiler to cope with an arbitrary integer constant.
177 Reload has finished so we can't expect the compiler to
178 auto-allocate a temporary register. But we know that call-saved
179 registers are not live yet, so we utilize them. */
180 rtx tmpreg = gen_rtx_REG (Pmode, PROLOGUE_TEMP_REGNUM);
181 if (addendum < 0)
182 {
183 emit_insn (gen_rtx_SET (tmpreg, gen_int_mode (-addendum, Pmode)));
184 insn = emit_insn (gen_sub3_insn (op0, op1, tmpreg));
185 }
186 else
187 {
188 emit_insn (gen_rtx_SET (tmpreg, gen_int_mode (addendum, Pmode)));
189 insn = emit_insn (gen_add3_insn (op0, op1, tmpreg));
190 }
191 }
192
193 /* Attach a note indicating what happened. */
194 if (kind != REG_NOTE_MAX)
195 add_reg_note (insn, kind, copy_rtx (op0_adjust));
196
197 RTX_FRAME_RELATED_P (insn) = 1;
198
199 return insn;
200 }
201
202 /* Add a const_int to the stack pointer register. */
203 static rtx
204 pru_add_to_sp (int addendum, const enum reg_note kind)
205 {
206 return pru_add3_frame_adjust (stack_pointer_rtx, stack_pointer_rtx,
207 addendum, kind);
208 }
209
210 /* Helper function used during prologue/epilogue. Emits a single LBBO/SBBO
211 instruction for load/store of the next group of consecutive registers. */
212 static int
213 xbbo_next_reg_cluster (int regno_start, int *sp_offset, bool do_store)
214 {
215 int regno, nregs, i;
216 rtx addr;
217 rtx_insn *insn;
218
219 nregs = 0;
220
221 /* Skip the empty slots. */
222 for (; regno_start <= LAST_GP_REGNUM;)
223 if (TEST_HARD_REG_BIT (cfun->machine->save_mask, regno_start))
224 break;
225 else
226 regno_start++;
227
228 /* Find the largest consecutive group of registers to save. */
229 for (regno = regno_start; regno <= LAST_GP_REGNUM;)
230 if (TEST_HARD_REG_BIT (cfun->machine->save_mask, regno))
231 {
232 regno++;
233 nregs++;
234 }
235 else
236 break;
237
238 if (!nregs)
239 return -1;
240
241 gcc_assert (UBYTE_INT (*sp_offset));
242
243 /* Ok, save this bunch. */
244 addr = plus_constant (Pmode, stack_pointer_rtx, *sp_offset);
245
246 if (do_store)
247 insn = targetm.gen_store_multiple (gen_frame_mem (BLKmode, addr),
248 gen_rtx_REG (QImode, regno_start),
249 GEN_INT (nregs));
250 else
251 insn = targetm.gen_load_multiple (gen_rtx_REG (QImode, regno_start),
252 gen_frame_mem (BLKmode, addr),
253 GEN_INT (nregs));
254
255 gcc_assert (reload_completed);
256 gcc_assert (insn);
257 emit_insn (insn);
258
259 /* Tag as frame-related. */
260 RTX_FRAME_RELATED_P (insn) = 1;
261
262 if (!do_store)
263 {
264 /* Tag epilogue unwind notes. */
265 for (i = regno_start; i < (regno_start + nregs); i++)
266 add_reg_note (insn, REG_CFA_RESTORE, gen_rtx_REG (QImode, i));
267 }
268
269 /* Increment and save offset in anticipation of the next register group. */
270 *sp_offset += nregs * UNITS_PER_WORD;
271
272 return regno_start + nregs;
273 }
274
275 /* Emit function prologue. */
276 void
277 pru_expand_prologue (void)
278 {
279 int regno_start;
280 int total_frame_size;
281 int sp_offset; /* Offset from base_reg to final stack value. */
282 int save_regs_base; /* Offset from base_reg to register save area. */
283 int save_offset; /* Temporary offset to currently saved register group. */
284
285 total_frame_size = cfun->machine->total_size;
286
287 if (flag_stack_usage_info)
288 current_function_static_stack_size = total_frame_size;
289
290 /* Decrement the stack pointer. */
291 if (!UBYTE_INT (total_frame_size))
292 {
293 /* We need an intermediary point, this will point at the spill block. */
294 pru_add_to_sp (cfun->machine->save_regs_offset - total_frame_size,
295 REG_NOTE_MAX);
296 save_regs_base = 0;
297 sp_offset = -cfun->machine->save_regs_offset;
298 }
299 else if (total_frame_size)
300 {
301 pru_add_to_sp (- total_frame_size, REG_NOTE_MAX);
302 save_regs_base = cfun->machine->save_regs_offset;
303 sp_offset = 0;
304 }
305 else
306 save_regs_base = sp_offset = 0;
307
308 regno_start = 0;
309 save_offset = save_regs_base;
310 do
311 regno_start = xbbo_next_reg_cluster (regno_start, &save_offset, true);
312 while (regno_start >= 0);
313
314 /* Set FP before adjusting SP. This way fp_offset has
315 better chance to fit in UBYTE. */
316 if (frame_pointer_needed)
317 {
318 int fp_offset = total_frame_size
319 - crtl->args.pretend_args_size
320 + sp_offset;
321
322 pru_add3_frame_adjust (hard_frame_pointer_rtx, stack_pointer_rtx,
323 fp_offset, REG_NOTE_MAX);
324 }
325
326 if (sp_offset)
327 pru_add_to_sp (sp_offset, REG_FRAME_RELATED_EXPR);
328
329 /* If we are profiling, make sure no instructions are scheduled before
330 the call to mcount. */
331 if (crtl->profile)
332 emit_insn (gen_blockage ());
333 }
334
335 /* Emit function epilogue. */
336 void
337 pru_expand_epilogue (bool sibcall_p)
338 {
339 int total_frame_size;
340 int sp_adjust, save_offset;
341 int regno_start;
342
343 if (!sibcall_p && pru_can_use_return_insn ())
344 {
345 emit_jump_insn (gen_return ());
346 return;
347 }
348
349 emit_insn (gen_blockage ());
350
351 total_frame_size = cfun->machine->total_size;
352
353 if (frame_pointer_needed)
354 {
355 /* Recover the stack pointer. */
356 pru_add3_frame_adjust (stack_pointer_rtx, hard_frame_pointer_rtx,
357 - cfun->machine->save_reg_size,
358 REG_CFA_ADJUST_CFA);
359
360 save_offset = 0;
361 sp_adjust = total_frame_size - cfun->machine->save_regs_offset;
362 }
363 else if (!UBYTE_INT (total_frame_size))
364 {
365 pru_add_to_sp (cfun->machine->save_regs_offset, REG_CFA_ADJUST_CFA);
366 save_offset = 0;
367 sp_adjust = total_frame_size - cfun->machine->save_regs_offset;
368 }
369 else
370 {
371 save_offset = cfun->machine->save_regs_offset;
372 sp_adjust = total_frame_size;
373 }
374
375 regno_start = 0;
376 do
377 regno_start = xbbo_next_reg_cluster (regno_start, &save_offset, false);
378 while (regno_start >= 0);
379
380 /* Emit a blockage insn here to keep these insns from being moved to
381 an earlier spot in the epilogue.
382
383 This is necessary as we must not cut the stack back before all the
384 restores are finished. */
385 emit_insn (gen_blockage ());
386
387 if (sp_adjust)
388 pru_add_to_sp (sp_adjust, REG_CFA_ADJUST_CFA);
389
390 if (!sibcall_p)
391 emit_jump_insn (gen_simple_return ());
392 }
393
394 /* Implement RETURN_ADDR_RTX. Note, we do not support moving
395 back to a previous frame. */
396 rtx
397 pru_get_return_address (int count)
398 {
399 if (count != 0)
400 return NULL_RTX;
401
402 /* Return r3.w2. */
403 return get_hard_reg_initial_val (HImode, RA_REGNUM);
404 }
405
406 /* Implement FUNCTION_PROFILER macro. */
407 void
408 pru_function_profiler (FILE *file, int)
409 {
410 fprintf (file, "\tmov\tr1, ra\n");
411 fprintf (file, "\tcall\t_mcount\n");
412 fprintf (file, "\tmov\tra, r1\n");
413 }
414
415 /* Dump stack layout. */
416 static void
417 pru_dump_frame_layout (FILE *file)
418 {
419 fprintf (file, "\t%s Current Frame Info\n", ASM_COMMENT_START);
420 fprintf (file, "\t%s total_size = %d\n", ASM_COMMENT_START,
421 cfun->machine->total_size);
422 fprintf (file, "\t%s var_size = %d\n", ASM_COMMENT_START,
423 cfun->machine->var_size);
424 fprintf (file, "\t%s out_args_size = %d\n", ASM_COMMENT_START,
425 cfun->machine->out_args_size);
426 fprintf (file, "\t%s save_reg_size = %d\n", ASM_COMMENT_START,
427 cfun->machine->save_reg_size);
428 fprintf (file, "\t%s initialized = %d\n", ASM_COMMENT_START,
429 cfun->machine->initialized);
430 fprintf (file, "\t%s save_regs_offset = %d\n", ASM_COMMENT_START,
431 cfun->machine->save_regs_offset);
432 fprintf (file, "\t%s is_leaf = %d\n", ASM_COMMENT_START,
433 crtl->is_leaf);
434 fprintf (file, "\t%s frame_pointer_needed = %d\n", ASM_COMMENT_START,
435 frame_pointer_needed);
436 fprintf (file, "\t%s pretend_args_size = %d\n", ASM_COMMENT_START,
437 crtl->args.pretend_args_size);
438 }
439
440 /* Return true if REGNO should be saved in the prologue. */
441 static bool
442 prologue_saved_reg_p (int regno)
443 {
444 gcc_assert (GP_REG_P (regno));
445
446 /* Do not save the register if function will not return. */
447 if (TREE_THIS_VOLATILE (current_function_decl))
448 return false;
449
450 if (df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno))
451 return true;
452
453 /* 32-bit FP. */
454 if (frame_pointer_needed
455 && regno >= HARD_FRAME_POINTER_REGNUM
456 && regno < HARD_FRAME_POINTER_REGNUM + GET_MODE_SIZE (Pmode))
457 return true;
458
459 /* 16-bit RA. */
460 if (regno == RA_REGNUM && df_regs_ever_live_p (RA_REGNUM))
461 return true;
462 if (regno == RA_REGNUM + 1 && df_regs_ever_live_p (RA_REGNUM + 1))
463 return true;
464
465 return false;
466 }
467
468 /* Implement TARGET_CAN_ELIMINATE. */
469 static bool
470 pru_can_eliminate (const int, const int to)
471 {
472 if (to == STACK_POINTER_REGNUM)
473 return !frame_pointer_needed;
474 return true;
475 }
476
477 /* Implement INITIAL_ELIMINATION_OFFSET macro. */
478 int
479 pru_initial_elimination_offset (int from, int to)
480 {
481 int offset;
482
483 /* Set OFFSET to the offset from the stack pointer. */
484 switch (from)
485 {
486 case FRAME_POINTER_REGNUM:
487 offset = cfun->machine->out_args_size;
488 break;
489
490 case ARG_POINTER_REGNUM:
491 offset = cfun->machine->total_size;
492 offset -= crtl->args.pretend_args_size;
493 break;
494
495 default:
496 gcc_unreachable ();
497 }
498
499 /* If we are asked for the frame pointer offset, then adjust OFFSET
500 by the offset from the frame pointer to the stack pointer. */
501 if (to == HARD_FRAME_POINTER_REGNUM)
502 offset -= cfun->machine->total_size - crtl->args.pretend_args_size;
503
504
505 return offset;
506 }
507
508 /* Return nonzero if this function is known to have a null epilogue.
509 This allows the optimizer to omit jumps to jumps if no stack
510 was created. */
511 int
512 pru_can_use_return_insn (void)
513 {
514 if (!reload_completed || crtl->profile)
515 return 0;
516
517 return cfun->machine->total_size == 0;
518 }
519
520 /* Implement `TARGET_CLASS_LIKELY_SPILLED_P'. The original intention
521 of the default implementation is kept, but is adjusted for PRU.
522 Return TRUE if the given class C contains a single SImode
523 (as opposed to word_mode!) register. */
524
525 static bool
526 pru_class_likely_spilled_p (reg_class_t c)
527 {
528 return (reg_class_size[(int) c] <= GET_MODE_SIZE (SImode));
529 }
530
531 /* Implement TARGET_HARD_REGNO_MODE_OK. */
532
533 static bool
534 pru_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
535 {
536 switch (GET_MODE_SIZE (mode))
537 {
538 case 1: return true;
539 case 2: return (regno % 4) <= 2;
540 case 4: return (regno % 4) == 0;
541 case 8: return (regno % 4) == 0;
542 case 16: return (regno % 4) == 0; /* Not sure why TImode is used. */
543 case 32: return (regno % 4) == 0; /* Not sure why CTImode is used. */
544 default:
545 /* TODO: Find out why VOIDmode and BLKmode are passed. */
546 gcc_assert (mode == BLKmode || mode == VOIDmode);
547 return (regno % 4) == 0;
548 }
549 }
550
551 /* Implement `TARGET_HARD_REGNO_SCRATCH_OK'.
552 Returns true if REGNO is safe to be allocated as a scratch
553 register (for a define_peephole2) in the current function. */
554
555 static bool
556 pru_hard_regno_scratch_ok (unsigned int regno)
557 {
558 /* Don't allow hard registers that might be part of the frame pointer.
559 Some places in the compiler just test for [HARD_]FRAME_POINTER_REGNUM
560 and don't handle a frame pointer that spans more than one register.
561 TODO: Fix those faulty places. */
562
563 if ((!reload_completed || frame_pointer_needed)
564 && (IN_RANGE (regno, HARD_FRAME_POINTER_REGNUM,
565 HARD_FRAME_POINTER_REGNUM + 3)
566 || IN_RANGE (regno, FRAME_POINTER_REGNUM,
567 FRAME_POINTER_REGNUM + 3)))
568 return false;
569
570 return true;
571 }
572
573
574 /* Worker function for `HARD_REGNO_RENAME_OK'.
575 Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
576
577 int
578 pru_hard_regno_rename_ok (unsigned int old_reg,
579 unsigned int new_reg)
580 {
581 /* Don't allow hard registers that might be part of the frame pointer.
582 Some places in the compiler just test for [HARD_]FRAME_POINTER_REGNUM
583 and don't care for a frame pointer that spans more than one register.
584 TODO: Fix those faulty places. */
585 if ((!reload_completed || frame_pointer_needed)
586 && (IN_RANGE (old_reg, HARD_FRAME_POINTER_REGNUM,
587 HARD_FRAME_POINTER_REGNUM + 3)
588 || IN_RANGE (old_reg, FRAME_POINTER_REGNUM,
589 FRAME_POINTER_REGNUM + 3)
590 || IN_RANGE (new_reg, HARD_FRAME_POINTER_REGNUM,
591 HARD_FRAME_POINTER_REGNUM + 3)
592 || IN_RANGE (new_reg, FRAME_POINTER_REGNUM,
593 FRAME_POINTER_REGNUM + 3)))
594 return 0;
595
596 return 1;
597 }
598
599 /* Allocate a chunk of memory for per-function machine-dependent data. */
600 static struct machine_function *
601 pru_init_machine_status (void)
602 {
603 return ggc_cleared_alloc<machine_function> ();
604 }
605
606 /* Implement TARGET_OPTION_OVERRIDE. */
607 static void
608 pru_option_override (void)
609 {
610 #ifdef SUBTARGET_OVERRIDE_OPTIONS
611 SUBTARGET_OVERRIDE_OPTIONS;
612 #endif
613
614 /* Check for unsupported options. */
615 if (flag_pic == 1)
616 warning (OPT_fpic, "%<-fpic%> is not supported");
617 if (flag_pic == 2)
618 warning (OPT_fPIC, "%<-fPIC%> is not supported");
619 if (flag_pie == 1)
620 warning (OPT_fpie, "%<-fpie%> is not supported");
621 if (flag_pie == 2)
622 warning (OPT_fPIE, "%<-fPIE%> is not supported");
623
624 /* QBxx conditional branching cannot cope with block reordering. */
625 if (flag_reorder_blocks_and_partition)
626 {
627 inform (input_location, "%<-freorder-blocks-and-partition%> "
628 "not supported on this architecture");
629 flag_reorder_blocks_and_partition = 0;
630 flag_reorder_blocks = 1;
631 }
632
633 /* Function to allocate machine-dependent function status. */
634 init_machine_status = &pru_init_machine_status;
635
636 /* Save the initial options in case the user does function specific
637 options. */
638 target_option_default_node = target_option_current_node
639 = build_target_option_node (&global_options, &global_options_set);
640 }
641
642 /* Compute a (partial) cost for rtx X. Return true if the complete
643 cost has been computed, and false if subexpressions should be
644 scanned. In either case, *TOTAL contains the cost result. */
645 static bool
646 pru_rtx_costs (rtx x, machine_mode mode, int outer_code, int, int *total, bool)
647 {
648 const int code = GET_CODE (x);
649
650 switch (code)
651 {
652 case CONST_INT:
653 if ((mode == VOIDmode && UBYTE_INT (INTVAL (x)))
654 || (mode != VOIDmode && const_ubyte_operand (x, mode)))
655 {
656 *total = COSTS_N_INSNS (0);
657 return true;
658 }
659 else if ((mode == VOIDmode && UHWORD_INT (INTVAL (x)))
660 || (mode != VOIDmode && const_uhword_operand (x, mode)))
661 {
662 *total = COSTS_N_INSNS (1);
663 return true;
664 }
665 else if (outer_code == MEM && ctable_addr_operand (x, VOIDmode))
666 {
667 *total = COSTS_N_INSNS (0);
668 return true;
669 }
670 else
671 {
672 *total = COSTS_N_INSNS (2);
673 return true;
674 }
675
676 case LABEL_REF:
677 case SYMBOL_REF:
678 case CONST:
679 {
680 *total = COSTS_N_INSNS (1);
681 return true;
682 }
683 case CONST_DOUBLE:
684 {
685 *total = COSTS_N_INSNS (2);
686 return true;
687 }
688 case CONST_WIDE_INT:
689 {
690 /* PRU declares no vector or very large integer types. */
691 gcc_unreachable ();
692 return true;
693 }
694 case SET:
695 {
696 int factor;
697
698 /* A SET doesn't have a mode, so let's look at the SET_DEST to get
699 the mode for the factor. */
700 mode = GET_MODE (SET_DEST (x));
701
702 /* SI move has the same cost as a QI move. Moves larger than
703 64 bits are costly. */
704 factor = CEIL (GET_MODE_SIZE (mode), GET_MODE_SIZE (SImode));
705 *total = factor * COSTS_N_INSNS (1);
706
707 return false;
708 }
709
710 case MULT:
711 {
712 /* Factor in that "mul" requires fixed registers, which
713 would likely require register moves. */
714 *total = COSTS_N_INSNS (7);
715 return false;
716 }
717 case PLUS:
718 {
719 rtx op0 = XEXP (x, 0);
720 rtx op1 = XEXP (x, 1);
721 machine_mode op1_mode = GET_MODE (op1);
722
723 /* Generic RTL address expressions do not enforce mode for
724 offsets, yet our UBYTE constraint requires it. Fix it here. */
725 if (op1_mode == VOIDmode && CONST_INT_P (op1) && outer_code == MEM)
726 op1_mode = Pmode;
727 if (outer_code == MEM
728 && ((REG_P (op0) && reg_or_ubyte_operand (op1, op1_mode))
729 || ctable_addr_operand (op0, VOIDmode)
730 || ctable_addr_operand (op1, VOIDmode)
731 || (ctable_base_operand (op0, VOIDmode) && REG_P (op1))
732 || (ctable_base_operand (op1, VOIDmode) && REG_P (op0))))
733 {
734 /* CTABLE or REG base addressing - PLUS comes for free. */
735 *total = COSTS_N_INSNS (0);
736 return true;
737 }
738 else
739 {
740 *total = COSTS_N_INSNS (1);
741 return false;
742 }
743 }
744 case SIGN_EXTEND:
745 {
746 *total = COSTS_N_INSNS (3);
747 return false;
748 }
749 case ASHIFTRT:
750 {
751 rtx op1 = XEXP (x, 1);
752 if (const_1_operand (op1, VOIDmode))
753 *total = COSTS_N_INSNS (3);
754 else
755 *total = COSTS_N_INSNS (7);
756 return false;
757 }
758 case ZERO_EXTRACT:
759 {
760 rtx op2 = XEXP (x, 2);
761 if ((outer_code == EQ || outer_code == NE)
762 && CONST_INT_P (op2)
763 && INTVAL (op2) == 1)
764 {
765 /* Branch if bit is set/clear is a single instruction. */
766 *total = COSTS_N_INSNS (0);
767 return true;
768 }
769 else
770 {
771 *total = COSTS_N_INSNS (2);
772 return false;
773 }
774 }
775 case ZERO_EXTEND:
776 {
777 /* 64-bit zero extensions actually have a cost because they
778 require setting a register to zero.
779 32-bit and smaller are free. */
780 int factor = (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (SImode)) ? 0 : 1;
781 *total = factor * COSTS_N_INSNS (1);
782 return false;
783 }
784
785 default:
786 {
787 /* PRU ALU is 32 bit, despite GCC's UNITS_PER_WORD=1. */
788 int factor = CEIL (GET_MODE_SIZE (mode), GET_MODE_SIZE (SImode));
789 *total = factor * COSTS_N_INSNS (1);
790 return false;
791 }
792 }
793 }
794
795 /* Calculate the cost of an addressing mode that contains ADDR.
796 ADDR must be a valid address. */
797
798 static int
799 pru_address_cost (rtx addr, machine_mode, addr_space_t as, bool)
800 {
801 if (as != ADDR_SPACE_GENERIC)
802 /* All currently implemented special address spaces for PRU
803 are much more efficient than generic memory I/O. */
804 return 0;
805 else if (ctable_addr_operand (addr, VOIDmode)
806 || (GET_CODE (addr) == PLUS
807 && ctable_base_operand (XEXP (addr, 1), VOIDmode)))
808 /* Using CTABLE instructions reduces register pressure,
809 so give it precedence. */
810 return 1;
811 else
812 /* Same two instructions (LBBO/SBBO) are used for any valid
813 addressing mode. */
814 return 2;
815 }
816
817 /* Insn costs on PRU are straightforward because:
818 - Insns emit 0, 1 or more instructions.
819 - All instructions are 32-bit length.
820 - All instructions execute in 1 cycle (sans memory access delays).
821 The "length" attribute maps nicely to the insn cost. */
822
823 static int
824 pru_insn_cost (rtx_insn *insn, bool speed)
825 {
826 /* Use generic cost calculation for unrecognized insns. */
827 if (recog_memoized (insn) < 0)
828 return pattern_cost (insn, speed);
829
830 unsigned int len = get_attr_length (insn);
831
832 gcc_assert ((len % 4) == 0);
833
834 int cost = COSTS_N_INSNS (len / 4);
835 /* Some insns have zero length (e.g. blockage, pruloop_end).
836 In such cases give the minimum cost, because a return of
837 0 would incorrectly indicate that the insn cost is unknown. */
838 if (cost == 0)
839 cost = 1;
840
841 /* Writes are usually posted, so they take 1 cycle. Reads
842 from DMEM usually take 3 cycles.
843 See TI document SPRACE8A, Device-Specific PRU Read Latency Values. */
844 if (speed && get_attr_type (insn) == TYPE_LD)
845 cost += COSTS_N_INSNS (2);
846
847 return cost;
848 }
849
850 static GTY(()) rtx eqdf_libfunc;
851 static GTY(()) rtx nedf_libfunc;
852 static GTY(()) rtx ledf_libfunc;
853 static GTY(()) rtx ltdf_libfunc;
854 static GTY(()) rtx gedf_libfunc;
855 static GTY(()) rtx gtdf_libfunc;
856 static GTY(()) rtx eqsf_libfunc;
857 static GTY(()) rtx nesf_libfunc;
858 static GTY(()) rtx lesf_libfunc;
859 static GTY(()) rtx ltsf_libfunc;
860 static GTY(()) rtx gesf_libfunc;
861 static GTY(()) rtx gtsf_libfunc;
862
863 /* Implement the TARGET_INIT_LIBFUNCS macro. We use this to rename library
864 functions to match the PRU ABI. */
865
866 static void
867 pru_init_libfuncs (void)
868 {
869 /* Double-precision floating-point arithmetic. */
870 set_optab_libfunc (add_optab, DFmode, "__pruabi_addd");
871 set_optab_libfunc (sdiv_optab, DFmode, "__pruabi_divd");
872 set_optab_libfunc (smul_optab, DFmode, "__pruabi_mpyd");
873 set_optab_libfunc (neg_optab, DFmode, "__pruabi_negd");
874 set_optab_libfunc (sub_optab, DFmode, "__pruabi_subd");
875
876 /* Single-precision floating-point arithmetic. */
877 set_optab_libfunc (add_optab, SFmode, "__pruabi_addf");
878 set_optab_libfunc (sdiv_optab, SFmode, "__pruabi_divf");
879 set_optab_libfunc (smul_optab, SFmode, "__pruabi_mpyf");
880 set_optab_libfunc (neg_optab, SFmode, "__pruabi_negf");
881 set_optab_libfunc (sub_optab, SFmode, "__pruabi_subf");
882
883 /* Floating-point comparisons. */
884 eqsf_libfunc = init_one_libfunc ("__pruabi_eqf");
885 nesf_libfunc = init_one_libfunc ("__pruabi_neqf");
886 lesf_libfunc = init_one_libfunc ("__pruabi_lef");
887 ltsf_libfunc = init_one_libfunc ("__pruabi_ltf");
888 gesf_libfunc = init_one_libfunc ("__pruabi_gef");
889 gtsf_libfunc = init_one_libfunc ("__pruabi_gtf");
890 eqdf_libfunc = init_one_libfunc ("__pruabi_eqd");
891 nedf_libfunc = init_one_libfunc ("__pruabi_neqd");
892 ledf_libfunc = init_one_libfunc ("__pruabi_led");
893 ltdf_libfunc = init_one_libfunc ("__pruabi_ltd");
894 gedf_libfunc = init_one_libfunc ("__pruabi_ged");
895 gtdf_libfunc = init_one_libfunc ("__pruabi_gtd");
896
897 /* In PRU ABI, much like other TI processors, floating point
898 comparisons return non-standard values. This quirk is handled
899 by disabling the optab library functions, and handling the
900 comparison during RTL expansion. */
901 set_optab_libfunc (eq_optab, SFmode, NULL);
902 set_optab_libfunc (ne_optab, SFmode, NULL);
903 set_optab_libfunc (gt_optab, SFmode, NULL);
904 set_optab_libfunc (ge_optab, SFmode, NULL);
905 set_optab_libfunc (lt_optab, SFmode, NULL);
906 set_optab_libfunc (le_optab, SFmode, NULL);
907 set_optab_libfunc (eq_optab, DFmode, NULL);
908 set_optab_libfunc (ne_optab, DFmode, NULL);
909 set_optab_libfunc (gt_optab, DFmode, NULL);
910 set_optab_libfunc (ge_optab, DFmode, NULL);
911 set_optab_libfunc (lt_optab, DFmode, NULL);
912 set_optab_libfunc (le_optab, DFmode, NULL);
913
914 /* The isunordered function appears to be supported only by GCC. */
915 set_optab_libfunc (unord_optab, SFmode, "__pruabi_unordf");
916 set_optab_libfunc (unord_optab, DFmode, "__pruabi_unordd");
917
918 /* Floating-point to integer conversions. */
919 set_conv_libfunc (sfix_optab, SImode, DFmode, "__pruabi_fixdi");
920 set_conv_libfunc (ufix_optab, SImode, DFmode, "__pruabi_fixdu");
921 set_conv_libfunc (sfix_optab, DImode, DFmode, "__pruabi_fixdlli");
922 set_conv_libfunc (ufix_optab, DImode, DFmode, "__pruabi_fixdull");
923 set_conv_libfunc (sfix_optab, SImode, SFmode, "__pruabi_fixfi");
924 set_conv_libfunc (ufix_optab, SImode, SFmode, "__pruabi_fixfu");
925 set_conv_libfunc (sfix_optab, DImode, SFmode, "__pruabi_fixflli");
926 set_conv_libfunc (ufix_optab, DImode, SFmode, "__pruabi_fixfull");
927
928 /* Conversions between floating types. */
929 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__pruabi_cvtdf");
930 set_conv_libfunc (sext_optab, DFmode, SFmode, "__pruabi_cvtfd");
931
932 /* Integer to floating-point conversions. */
933 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__pruabi_fltid");
934 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__pruabi_fltud");
935 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__pruabi_fltllid");
936 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__pruabi_fltulld");
937 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__pruabi_fltif");
938 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__pruabi_fltuf");
939 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__pruabi_fltllif");
940 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__pruabi_fltullf");
941
942 /* Long long. */
943 set_optab_libfunc (ashr_optab, DImode, "__pruabi_asrll");
944 set_optab_libfunc (smul_optab, DImode, "__pruabi_mpyll");
945 set_optab_libfunc (ashl_optab, DImode, "__pruabi_lslll");
946 set_optab_libfunc (lshr_optab, DImode, "__pruabi_lsrll");
947
948 set_optab_libfunc (sdiv_optab, SImode, "__pruabi_divi");
949 set_optab_libfunc (udiv_optab, SImode, "__pruabi_divu");
950 set_optab_libfunc (smod_optab, SImode, "__pruabi_remi");
951 set_optab_libfunc (umod_optab, SImode, "__pruabi_remu");
952 set_optab_libfunc (sdivmod_optab, SImode, "__pruabi_divremi");
953 set_optab_libfunc (udivmod_optab, SImode, "__pruabi_divremu");
954 set_optab_libfunc (sdiv_optab, DImode, "__pruabi_divlli");
955 set_optab_libfunc (udiv_optab, DImode, "__pruabi_divull");
956 set_optab_libfunc (smod_optab, DImode, "__pruabi_remlli");
957 set_optab_libfunc (umod_optab, DImode, "__pruabi_remull");
958 set_optab_libfunc (udivmod_optab, DImode, "__pruabi_divremull");
959 }
960
961 /* Given a comparison CODE, return a similar comparison but without
962 the "equals" condition. In other words, it strips GE/GEU/LE/LEU
963 and instead returns GT/GTU/LT/LTU. */
964
965 enum rtx_code
966 pru_noteq_condition (enum rtx_code code)
967 {
968 switch (code)
969 {
970 case GT: return GT;
971 case GTU: return GTU;
972 case GE: return GT;
973 case GEU: return GTU;
974 case LT: return LT;
975 case LTU: return LTU;
976 case LE: return LT;
977 case LEU: return LTU;
978 default:
979 gcc_unreachable ();
980 }
981 }
982
983 /* Emit comparison instruction if necessary, returning the expression
984 that holds the compare result in the proper mode. Return the comparison
985 that should be used in the jump insn. */
986
987 rtx
988 pru_expand_fp_compare (rtx comparison, machine_mode mode)
989 {
990 enum rtx_code code = GET_CODE (comparison);
991 rtx op0 = XEXP (comparison, 0);
992 rtx op1 = XEXP (comparison, 1);
993 rtx cmp;
994 enum rtx_code jump_code = code;
995 machine_mode op_mode = GET_MODE (op0);
996 rtx_insn *insns;
997 rtx libfunc;
998
999 gcc_assert (op_mode == DFmode || op_mode == SFmode);
1000
1001 /* FP exceptions are not raised by PRU's softfp implementation. So the
1002 following transformations are safe. */
1003 if (code == UNGE)
1004 {
1005 code = LT;
1006 jump_code = EQ;
1007 }
1008 else if (code == UNLE)
1009 {
1010 code = GT;
1011 jump_code = EQ;
1012 }
1013 else
1014 jump_code = NE;
1015
1016 switch (code)
1017 {
1018 case EQ:
1019 libfunc = op_mode == DFmode ? eqdf_libfunc : eqsf_libfunc;
1020 break;
1021 case NE:
1022 libfunc = op_mode == DFmode ? nedf_libfunc : nesf_libfunc;
1023 break;
1024 case GT:
1025 libfunc = op_mode == DFmode ? gtdf_libfunc : gtsf_libfunc;
1026 break;
1027 case GE:
1028 libfunc = op_mode == DFmode ? gedf_libfunc : gesf_libfunc;
1029 break;
1030 case LT:
1031 libfunc = op_mode == DFmode ? ltdf_libfunc : ltsf_libfunc;
1032 break;
1033 case LE:
1034 libfunc = op_mode == DFmode ? ledf_libfunc : lesf_libfunc;
1035 break;
1036 default:
1037 gcc_unreachable ();
1038 }
1039 start_sequence ();
1040
1041 cmp = emit_library_call_value (libfunc, 0, LCT_CONST, SImode,
1042 op0, op_mode, op1, op_mode);
1043 insns = get_insns ();
1044 end_sequence ();
1045
1046 emit_libcall_block (insns, cmp, cmp,
1047 gen_rtx_fmt_ee (code, SImode, op0, op1));
1048
1049 return gen_rtx_fmt_ee (jump_code, mode, cmp, const0_rtx);
1050 }
1051
1052 /* Return the sign bit position for given OP's mode. */
1053 static int
1054 sign_bit_position (const rtx op)
1055 {
1056 const int sz = GET_MODE_SIZE (GET_MODE (op));
1057
1058 return sz * 8 - 1;
1059 }
1060
1061 /* Parse the given CVAL integer value, and extract the "filling" byte
1062 range of consecutive 0xff byte values. Rest of bytes must be 0x00.
1063 There must be only one range in the given value. This range would
1064 typically be used to calculate the parameters of
1065 PRU instructions ZERO and FILL.
1066
1067 The parameter MODE determines the maximum byte range to consider
1068 in the given input constant.
1069
1070 Example input:
1071 cval = 0xffffffffffffff00 = -256
1072 mode = SImode
1073 Return value:
1074 start = 1
1075 nbytes = 3
1076
1077 On error, return a range with -1 for START and NBYTES. */
1078 pru_byterange
1079 pru_calc_byterange (HOST_WIDE_INT cval, machine_mode mode)
1080 {
1081 const pru_byterange invalid_range = { -1, -1 };
1082 pru_byterange r = invalid_range;
1083 enum { ST_FFS, ST_INRANGE, ST_TRAILING_ZEROS } st = ST_FFS;
1084 int i;
1085
1086 for (i = 0; i < GET_MODE_SIZE (mode); i++)
1087 {
1088 const int b = cval & ((1U << BITS_PER_UNIT) - 1);
1089 cval >>= BITS_PER_UNIT;
1090
1091 if (b == 0x00 && (st == ST_FFS || st == ST_TRAILING_ZEROS))
1092 /* No action. */;
1093 else if (b == 0x00 && st == ST_INRANGE)
1094 st = ST_TRAILING_ZEROS;
1095 else if (b == 0xff && st == ST_FFS)
1096 {
1097 st = ST_INRANGE;
1098 r.start = i;
1099 r.nbytes = 1;
1100 }
1101 else if (b == 0xff && st == ST_INRANGE)
1102 r.nbytes++;
1103 else
1104 return invalid_range;
1105 }
1106
1107 if (st != ST_TRAILING_ZEROS && st != ST_INRANGE)
1108 return invalid_range;
1109 return r;
1110 }
1111
1112 /* Branches and compares. */
1113
1114 /* PRU's ALU does not support signed comparison operations. That's why we
1115 emulate them. By first checking the sign bit and handling every possible
1116 operand sign combination, we can simulate signed comparisons in just
1117 5 instructions. See table below.
1118
1119 .-------------------.---------------------------------------------------.
1120 | Operand sign bit | Mapping the signed comparison to an unsigned one |
1121 |---------+---------+------------+------------+------------+------------|
1122 | OP1.b31 | OP2.b31 | OP1 < OP2 | OP1 <= OP2 | OP1 > OP2 | OP1 >= OP2 |
1123 |---------+---------+------------+------------+------------+------------|
1124 | 0 | 0 | OP1 < OP2 | OP1 <= OP2 | OP1 > OP2 | OP1 >= OP2 |
1125 |---------+---------+------------+------------+------------+------------|
1126 | 0 | 1 | false | false | true | true |
1127 |---------+---------+------------+------------+------------+------------|
1128 | 1 | 0 | true | true | false | false |
1129 |---------+---------+------------+------------+------------+------------|
1130 | 1 | 1 | OP1 < OP2 | OP1 <= OP2 | OP1 > OP2 | OP1 >= OP2 |
1131 `---------'---------'------------'------------'------------+------------'
1132
1133
1134 Given the table above, here is an example for a concrete op:
1135 LT:
1136 qbbc OP1_POS, OP1, 31
1137 OP1_NEG: qbbc BRANCH_TAKEN_LABEL, OP2, 31
1138 OP1_NEG_OP2_NEG: qblt BRANCH_TAKEN_LABEL, OP2, OP1
1139 ; jmp OUT -> can be eliminated because we'll take the
1140 ; following branch. OP2.b31 is guaranteed to be 1
1141 ; by the time we get here.
1142 OP1_POS: qbbs OUT, OP2, 31
1143 OP1_POS_OP2_POS: qblt BRANCH_TAKEN_LABEL, OP2, OP1
1144 #if FAR_JUMP
1145 jmp OUT
1146 BRANCH_TAKEN_LABEL: jmp REAL_BRANCH_TAKEN_LABEL
1147 #endif
1148 OUT:
1149
1150 */
1151
1152 /* Output asm code for a signed-compare LT/LE conditional branch. */
1153 static const char *
1154 pru_output_ltle_signed_cbranch (rtx *operands, bool is_near)
1155 {
1156 static char buf[1024];
1157 enum rtx_code code = GET_CODE (operands[0]);
1158 rtx op1;
1159 rtx op2;
1160 const char *cmp_opstr;
1161 int bufi = 0;
1162
1163 op1 = operands[1];
1164 op2 = operands[2];
1165
1166 gcc_assert (GET_CODE (op1) == REG && GET_CODE (op2) == REG);
1167
1168 /* Determine the comparison operators for positive and negative operands. */
1169 if (code == LT)
1170 cmp_opstr = "qblt";
1171 else if (code == LE)
1172 cmp_opstr = "qble";
1173 else
1174 gcc_unreachable ();
1175
1176 if (is_near)
1177 bufi = snprintf (buf, sizeof (buf),
1178 "qbbc\t.+12, %%1, %d\n\t"
1179 "qbbc\t%%l3, %%2, %d\n\t" /* OP1_NEG. */
1180 "%s\t%%l3, %%2, %%1\n\t" /* OP1_NEG_OP2_NEG. */
1181 "qbbs\t.+8, %%2, %d\n\t" /* OP1_POS. */
1182 "%s\t%%l3, %%2, %%1", /* OP1_POS_OP2_POS. */
1183 sign_bit_position (op1),
1184 sign_bit_position (op2),
1185 cmp_opstr,
1186 sign_bit_position (op2),
1187 cmp_opstr);
1188 else
1189 bufi = snprintf (buf, sizeof (buf),
1190 "qbbc\t.+12, %%1, %d\n\t"
1191 "qbbc\t.+20, %%2, %d\n\t" /* OP1_NEG. */
1192 "%s\t.+16, %%2, %%1\n\t" /* OP1_NEG_OP2_NEG. */
1193 "qbbs\t.+16, %%2, %d\n\t" /* OP1_POS. */
1194 "%s\t.+8, %%2, %%1\n\t" /* OP1_POS_OP2_POS. */
1195 "jmp\t.+8\n\t" /* jmp OUT. */
1196 "jmp\t%%%%label(%%l3)", /* BRANCH_TAKEN_LABEL. */
1197 sign_bit_position (op1),
1198 sign_bit_position (op2),
1199 cmp_opstr,
1200 sign_bit_position (op2),
1201 cmp_opstr);
1202
1203 gcc_assert (bufi > 0);
1204 gcc_assert ((unsigned int) bufi < sizeof (buf));
1205
1206 return buf;
1207 }
1208
1209 /* Output asm code for a signed-compare GT/GE conditional branch. */
1210 static const char *
1211 pru_output_gtge_signed_cbranch (rtx *operands, bool is_near)
1212 {
1213 static char buf[1024];
1214 enum rtx_code code = GET_CODE (operands[0]);
1215 rtx op1;
1216 rtx op2;
1217 const char *cmp_opstr;
1218 int bufi = 0;
1219
1220 op1 = operands[1];
1221 op2 = operands[2];
1222
1223 gcc_assert (GET_CODE (op1) == REG && GET_CODE (op2) == REG);
1224
1225 /* Determine the comparison operators for positive and negative operands. */
1226 if (code == GT)
1227 cmp_opstr = "qbgt";
1228 else if (code == GE)
1229 cmp_opstr = "qbge";
1230 else
1231 gcc_unreachable ();
1232
1233 if (is_near)
1234 bufi = snprintf (buf, sizeof (buf),
1235 "qbbs\t.+12, %%1, %d\n\t"
1236 "qbbs\t%%l3, %%2, %d\n\t" /* OP1_POS. */
1237 "%s\t%%l3, %%2, %%1\n\t" /* OP1_POS_OP2_POS. */
1238 "qbbc\t.+8, %%2, %d\n\t" /* OP1_NEG. */
1239 "%s\t%%l3, %%2, %%1", /* OP1_NEG_OP2_NEG. */
1240 sign_bit_position (op1),
1241 sign_bit_position (op2),
1242 cmp_opstr,
1243 sign_bit_position (op2),
1244 cmp_opstr);
1245 else
1246 bufi = snprintf (buf, sizeof (buf),
1247 "qbbs\t.+12, %%1, %d\n\t"
1248 "qbbs\t.+20, %%2, %d\n\t" /* OP1_POS. */
1249 "%s\t.+16, %%2, %%1\n\t" /* OP1_POS_OP2_POS. */
1250 "qbbc\t.+16, %%2, %d\n\t" /* OP1_NEG. */
1251 "%s\t.+8, %%2, %%1\n\t" /* OP1_NEG_OP2_NEG. */
1252 "jmp\t.+8\n\t" /* jmp OUT. */
1253 "jmp\t%%%%label(%%l3)", /* BRANCH_TAKEN_LABEL. */
1254 sign_bit_position (op1),
1255 sign_bit_position (op2),
1256 cmp_opstr,
1257 sign_bit_position (op2),
1258 cmp_opstr);
1259
1260 gcc_assert (bufi > 0);
1261 gcc_assert ((unsigned int) bufi < sizeof (buf));
1262
1263 return buf;
1264 }
1265
1266 /* Output asm code for a signed-compare conditional branch.
1267
1268 If IS_NEAR is true, then QBBx instructions may be used for reaching
1269 the destination label. Otherwise JMP is used, at the expense of
1270 increased code size. */
1271 const char *
1272 pru_output_signed_cbranch (rtx *operands, bool is_near)
1273 {
1274 enum rtx_code code = GET_CODE (operands[0]);
1275
1276 if (code == LT || code == LE)
1277 return pru_output_ltle_signed_cbranch (operands, is_near);
1278 else if (code == GT || code == GE)
1279 return pru_output_gtge_signed_cbranch (operands, is_near);
1280 else
1281 gcc_unreachable ();
1282 }
1283
1284 /* Optimized version of pru_output_signed_cbranch for constant second
1285 operand. */
1286
1287 const char *
1288 pru_output_signed_cbranch_ubyteop2 (rtx *operands, bool is_near)
1289 {
1290 static char buf[1024];
1291 enum rtx_code code = GET_CODE (operands[0]);
1292 int regop_sign_bit_pos = sign_bit_position (operands[1]);
1293 const char *cmp_opstr;
1294 const char *rcmp_opstr;
1295
1296 /* We must swap operands due to PRU's demand OP1 to be the immediate. */
1297 code = swap_condition (code);
1298
1299 /* Determine normal and reversed comparison operators for both positive
1300 operands. This enables us to go completely unsigned.
1301
1302 NOTE: We cannot use the R print modifier because we convert signed
1303 comparison operators to unsigned ones. */
1304 switch (code)
1305 {
1306 case LT: cmp_opstr = "qblt"; rcmp_opstr = "qbge"; break;
1307 case LE: cmp_opstr = "qble"; rcmp_opstr = "qbgt"; break;
1308 case GT: cmp_opstr = "qbgt"; rcmp_opstr = "qble"; break;
1309 case GE: cmp_opstr = "qbge"; rcmp_opstr = "qblt"; break;
1310 default: gcc_unreachable ();
1311 }
1312
1313 /* OP2 is a constant unsigned byte - utilize this info to generate
1314 optimized code. We can "remove half" of the op table above because
1315 we know that OP2.b31 = 0 (remember that 0 <= OP2 <= 255). */
1316 if (code == LT || code == LE)
1317 {
1318 if (is_near)
1319 snprintf (buf, sizeof (buf),
1320 "qbbs\t.+8, %%1, %d\n\t"
1321 "%s\t%%l3, %%1, %%u2",
1322 regop_sign_bit_pos,
1323 cmp_opstr);
1324 else
1325 snprintf (buf, sizeof (buf),
1326 "qbbs\t.+12, %%1, %d\n\t"
1327 "%s\t.+8, %%1, %%u2\n\t"
1328 "jmp\t%%%%label(%%l3)",
1329 regop_sign_bit_pos,
1330 rcmp_opstr);
1331 }
1332 else if (code == GT || code == GE)
1333 {
1334 if (is_near)
1335 snprintf (buf, sizeof (buf),
1336 "qbbs\t%%l3, %%1, %d\n\t"
1337 "%s\t%%l3, %%1, %%u2",
1338 regop_sign_bit_pos,
1339 cmp_opstr);
1340 else
1341 snprintf (buf, sizeof (buf),
1342 "qbbs\t.+8, %%1, %d\n\t"
1343 "%s\t.+8, %%1, %%u2\n\t"
1344 "jmp\t%%%%label(%%l3)",
1345 regop_sign_bit_pos,
1346 rcmp_opstr);
1347 }
1348 else
1349 gcc_unreachable ();
1350
1351 return buf;
1352 }
1353
1354 /* Optimized version of pru_output_signed_cbranch_ubyteop2 for constant
1355 zero second operand. */
1356
1357 const char *
1358 pru_output_signed_cbranch_zeroop2 (rtx *operands, bool is_near)
1359 {
1360 static char buf[1024];
1361 enum rtx_code code = GET_CODE (operands[0]);
1362 int regop_sign_bit_pos = sign_bit_position (operands[1]);
1363
1364 /* OP2 is a constant zero - utilize this info to simply check the
1365 OP1 sign bit when comparing for LT or GE. */
1366 if (code == LT)
1367 {
1368 if (is_near)
1369 snprintf (buf, sizeof (buf),
1370 "qbbs\t%%l3, %%1, %d\n\t",
1371 regop_sign_bit_pos);
1372 else
1373 snprintf (buf, sizeof (buf),
1374 "qbbc\t.+8, %%1, %d\n\t"
1375 "jmp\t%%%%label(%%l3)",
1376 regop_sign_bit_pos);
1377 }
1378 else if (code == GE)
1379 {
1380 if (is_near)
1381 snprintf (buf, sizeof (buf),
1382 "qbbc\t%%l3, %%1, %d\n\t",
1383 regop_sign_bit_pos);
1384 else
1385 snprintf (buf, sizeof (buf),
1386 "qbbs\t.+8, %%1, %d\n\t"
1387 "jmp\t%%%%label(%%l3)",
1388 regop_sign_bit_pos);
1389 }
1390 else
1391 gcc_unreachable ();
1392
1393 return buf;
1394 }
1395
1396 /* Addressing Modes. */
1397
1398 /* Return true if register REGNO is a valid base register.
1399 STRICT_P is true if REG_OK_STRICT is in effect. */
1400
1401 bool
1402 pru_regno_ok_for_base_p (int regno, bool strict_p)
1403 {
1404 if (!HARD_REGISTER_NUM_P (regno) && !strict_p)
1405 return true;
1406
1407 /* The fake registers will be eliminated to either the stack or
1408 hard frame pointer, both of which are usually valid base registers.
1409 Reload deals with the cases where the eliminated form isn't valid. */
1410 return (GP_REG_P (regno)
1411 || regno == FRAME_POINTER_REGNUM
1412 || regno == ARG_POINTER_REGNUM);
1413 }
1414
1415 /* Return true if given xbbo constant OFFSET is valid. */
1416 static bool
1417 pru_valid_const_ubyte_offset (machine_mode mode, HOST_WIDE_INT offset)
1418 {
1419 bool valid = UBYTE_INT (offset);
1420
1421 /* Reload can split multi word accesses, so make sure we can address
1422 the second word in a DI. */
1423 if (valid && GET_MODE_SIZE (mode) > GET_MODE_SIZE (SImode))
1424 valid = UBYTE_INT (offset + GET_MODE_SIZE (mode) - 1);
1425
1426 return valid;
1427 }
1428
1429 /* Recognize a CTABLE base address. Return CTABLE entry index, or -1 if
1430 base was not found in the pragma-filled pru_ctable. */
1431 int
1432 pru_get_ctable_exact_base_index (unsigned HOST_WIDE_INT caddr)
1433 {
1434 unsigned int i;
1435
1436 for (i = 0; i < ARRAY_SIZE (pru_ctable); i++)
1437 {
1438 if (pru_ctable[i].valid && pru_ctable[i].base == caddr)
1439 return i;
1440 }
1441 return -1;
1442 }
1443
1444
1445 /* Check if the given address can be addressed via CTABLE_BASE + UBYTE_OFFS,
1446 and return the base CTABLE index if possible. */
1447 int
1448 pru_get_ctable_base_index (unsigned HOST_WIDE_INT caddr)
1449 {
1450 unsigned int i;
1451
1452 for (i = 0; i < ARRAY_SIZE (pru_ctable); i++)
1453 {
1454 if (pru_ctable[i].valid && IN_RANGE (caddr,
1455 pru_ctable[i].base,
1456 pru_ctable[i].base + 0xff))
1457 return i;
1458 }
1459 return -1;
1460 }
1461
1462
1463 /* Return the offset from some CTABLE base for this address. */
1464 int
1465 pru_get_ctable_base_offset (unsigned HOST_WIDE_INT caddr)
1466 {
1467 int i;
1468
1469 i = pru_get_ctable_base_index (caddr);
1470 gcc_assert (i >= 0);
1471
1472 return caddr - pru_ctable[i].base;
1473 }
1474
1475 /* Return true if the address expression formed by BASE + OFFSET is
1476 valid.
1477
1478 Note that the following address is not handled here:
1479 base CTABLE constant base + UBYTE constant offset
1480 The constants will be folded. The ctable_addr_operand predicate will take
1481 care of the validation. The CTABLE base+offset split will happen during
1482 operand printing. */
1483 static bool
1484 pru_valid_addr_expr_p (machine_mode mode, rtx base, rtx offset, bool strict_p)
1485 {
1486 if (!strict_p && GET_CODE (base) == SUBREG)
1487 base = SUBREG_REG (base);
1488 if (!strict_p && GET_CODE (offset) == SUBREG)
1489 offset = SUBREG_REG (offset);
1490
1491 if (REG_P (base)
1492 && pru_regno_ok_for_base_p (REGNO (base), strict_p)
1493 && ((CONST_INT_P (offset)
1494 && pru_valid_const_ubyte_offset (mode, INTVAL (offset)))
1495 || (REG_P (offset)
1496 && pru_regno_ok_for_index_p (REGNO (offset), strict_p))))
1497 /* base register + register offset
1498 * OR base register + UBYTE constant offset. */
1499 return true;
1500 else if (REG_P (base)
1501 && pru_regno_ok_for_index_p (REGNO (base), strict_p)
1502 && ctable_base_operand (offset, VOIDmode))
1503 /* base CTABLE constant base + register offset
1504 * Note: GCC always puts the register as a first operand of PLUS. */
1505 return true;
1506 else
1507 return false;
1508 }
1509
1510 /* Return register number (either for r30 or r31) which maps to the
1511 corresponding symbol OP's name in the __regio_symbol address namespace.
1512
1513 If no mapping can be established (i.e. symbol name is invalid), then
1514 return -1. */
1515 int pru_symref2ioregno (rtx op)
1516 {
1517 if (!SYMBOL_REF_P (op))
1518 return -1;
1519
1520 const char *name = XSTR (op, 0);
1521 if (!strcmp (name, "__R30"))
1522 return R30_REGNUM;
1523 else if (!strcmp (name, "__R31"))
1524 return R31_REGNUM;
1525 else
1526 return -1;
1527 }
1528
1529 /* Implement TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P. */
1530 static bool
1531 pru_addr_space_legitimate_address_p (machine_mode mode, rtx operand,
1532 bool strict_p, addr_space_t as,
1533 code_helper = ERROR_MARK)
1534 {
1535 if (as == ADDR_SPACE_REGIO)
1536 {
1537 /* Address space constraints for __regio_symbol have been checked in
1538 TARGET_INSERT_ATTRIBUTES, and some more checks will be done
1539 during RTL expansion of "mov<mode>". */
1540 return true;
1541 }
1542 else if (as != ADDR_SPACE_GENERIC)
1543 {
1544 gcc_unreachable ();
1545 }
1546
1547 switch (GET_CODE (operand))
1548 {
1549 /* Direct. */
1550 case SYMBOL_REF:
1551 case LABEL_REF:
1552 case CONST:
1553 case CONST_WIDE_INT:
1554 return false;
1555
1556 case CONST_INT:
1557 return ctable_addr_operand (operand, VOIDmode);
1558
1559 /* Register indirect. */
1560 case REG:
1561 return pru_regno_ok_for_base_p (REGNO (operand), strict_p);
1562
1563 /* Register indirect with displacement. */
1564 case PLUS:
1565 {
1566 rtx op0 = XEXP (operand, 0);
1567 rtx op1 = XEXP (operand, 1);
1568
1569 return pru_valid_addr_expr_p (mode, op0, op1, strict_p);
1570 }
1571
1572 default:
1573 break;
1574 }
1575 return false;
1576 }
1577
1578 /* Output assembly language related definitions. */
1579
1580 /* Implement TARGET_ASM_CONSTRUCTOR. */
1581 static void
1582 pru_elf_asm_constructor (rtx symbol, int priority)
1583 {
1584 char buf[23];
1585 section *s;
1586
1587 if (priority == DEFAULT_INIT_PRIORITY)
1588 snprintf (buf, sizeof (buf), ".init_array");
1589 else
1590 {
1591 /* While priority is known to be in range [0, 65535], so 18 bytes
1592 would be enough, the compiler might not know that. To avoid
1593 -Wformat-truncation false positive, use a larger size. */
1594 snprintf (buf, sizeof (buf), ".init_array.%.5u", priority);
1595 }
1596 s = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL);
1597 switch_to_section (s);
1598 assemble_aligned_integer (INIT_ARRAY_ENTRY_BYTES, symbol);
1599 }
1600
1601 /* Implement TARGET_ASM_DESTRUCTOR. */
1602 static void
1603 pru_elf_asm_destructor (rtx symbol, int priority)
1604 {
1605 char buf[23];
1606 section *s;
1607
1608 if (priority == DEFAULT_INIT_PRIORITY)
1609 snprintf (buf, sizeof (buf), ".fini_array");
1610 else
1611 {
1612 /* While priority is known to be in range [0, 65535], so 18 bytes
1613 would be enough, the compiler might not know that. To avoid
1614 -Wformat-truncation false positive, use a larger size. */
1615 snprintf (buf, sizeof (buf), ".fini_array.%.5u", priority);
1616 }
1617 s = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL);
1618 switch_to_section (s);
1619 assemble_aligned_integer (INIT_ARRAY_ENTRY_BYTES, symbol);
1620 }
1621
1622 /* Map rtx_code to unsigned PRU branch op suffix. Callers must
1623 handle sign comparison themselves for signed operations. */
1624 static const char *
1625 pru_comparison_str (enum rtx_code cond)
1626 {
1627 switch (cond)
1628 {
1629 case NE: return "ne";
1630 case EQ: return "eq";
1631 case GEU: return "ge";
1632 case GTU: return "gt";
1633 case LEU: return "le";
1634 case LTU: return "lt";
1635 default: gcc_unreachable ();
1636 }
1637 }
1638
1639 /* Access some RTX as INT_MODE. If X is a CONST_FIXED we can get
1640 the bit representation of X by "casting" it to CONST_INT. */
1641
1642 static rtx
1643 pru_to_int_mode (rtx x)
1644 {
1645 machine_mode mode = GET_MODE (x);
1646
1647 return VOIDmode == mode
1648 ? x
1649 : simplify_gen_subreg (int_mode_for_mode (mode).require (), x, mode, 0);
1650 }
1651
1652 /* Translate between the MachineDescription notion
1653 of 8-bit consecutive registers, to the PRU
1654 assembler syntax of REGWORD[.SUBREG]. */
1655 static const char *
1656 pru_asm_regname (rtx op)
1657 {
1658 static char canon_reg_names[3][LAST_GP_REGNUM][8];
1659 int speci, regi;
1660
1661 gcc_assert (REG_P (op));
1662
1663 if (!canon_reg_names[0][0][0])
1664 {
1665 for (regi = 0; regi < LAST_GP_REGNUM; regi++)
1666 for (speci = 0; speci < 3; speci++)
1667 {
1668 const int sz = (speci == 0) ? 1 : ((speci == 1) ? 2 : 4);
1669 if ((regi + sz) > (32 * 4))
1670 continue; /* Invalid entry. */
1671
1672 /* Construct the lookup table. */
1673 const char *suffix = "";
1674
1675 switch ((sz << 8) | (regi % 4))
1676 {
1677 case (1 << 8) | 0: suffix = ".b0"; break;
1678 case (1 << 8) | 1: suffix = ".b1"; break;
1679 case (1 << 8) | 2: suffix = ".b2"; break;
1680 case (1 << 8) | 3: suffix = ".b3"; break;
1681 case (2 << 8) | 0: suffix = ".w0"; break;
1682 case (2 << 8) | 1: suffix = ".w1"; break;
1683 case (2 << 8) | 2: suffix = ".w2"; break;
1684 case (4 << 8) | 0: suffix = ""; break;
1685 default:
1686 /* Invalid entry. */
1687 continue;
1688 }
1689 sprintf (&canon_reg_names[speci][regi][0],
1690 "r%d%s", regi / 4, suffix);
1691 }
1692 }
1693
1694 switch (GET_MODE_SIZE (GET_MODE (op)))
1695 {
1696 case 1: speci = 0; break;
1697 case 2: speci = 1; break;
1698 case 4: speci = 2; break;
1699 case 8: speci = 2; break; /* Existing GCC test cases are not using %F. */
1700 default: gcc_unreachable ();
1701 }
1702 regi = REGNO (op);
1703 gcc_assert (regi < LAST_GP_REGNUM);
1704 gcc_assert (canon_reg_names[speci][regi][0]);
1705
1706 return &canon_reg_names[speci][regi][0];
1707 }
1708
1709 /* Print the operand OP to file stream FILE modified by LETTER.
1710 LETTER can be one of:
1711
1712 b: prints the register byte start (used by LBBO/SBBO).
1713 B: prints 'c' or 'b' for CTABLE or REG base in a memory address.
1714 F: Full 32-bit register.
1715 H: Higher 16-bits of a const_int operand.
1716 L: Lower 16-bits of a const_int operand.
1717 N: prints next 32-bit register (upper 32bits of a 64bit REG couple).
1718 P: prints swapped condition.
1719 Q: prints swapped and reversed condition.
1720 R: prints reversed condition.
1721 S: print operand mode size (but do not print the operand itself).
1722 T: print exact_log2 () for const_int operands.
1723 u: print QI constant integer as unsigned. No transformation for regs.
1724 V: print exact_log2 () of negated const_int operands.
1725 w: Lower 32-bits of a const_int operand.
1726 W: Upper 32-bits of a const_int operand.
1727 */
1728 static void
1729 pru_print_operand (FILE *file, rtx op, int letter)
1730 {
1731 switch (letter)
1732 {
1733 case 'S':
1734 fprintf (file, "%d", GET_MODE_SIZE (GET_MODE (op)));
1735 return;
1736
1737 default:
1738 break;
1739 }
1740
1741 if (comparison_operator (op, VOIDmode))
1742 {
1743 enum rtx_code cond = GET_CODE (op);
1744 gcc_assert (!pru_signed_cmp_operator (op, VOIDmode));
1745
1746 switch (letter)
1747 {
1748 case 0:
1749 fprintf (file, "%s", pru_comparison_str (cond));
1750 return;
1751 case 'P':
1752 fprintf (file, "%s", pru_comparison_str (swap_condition (cond)));
1753 return;
1754 case 'Q':
1755 cond = swap_condition (cond);
1756 /* Fall through. */
1757 case 'R':
1758 fprintf (file, "%s", pru_comparison_str (reverse_condition (cond)));
1759 return;
1760 }
1761 }
1762
1763 switch (GET_CODE (op))
1764 {
1765 case REG:
1766 if (letter == 0 || letter == 'u')
1767 {
1768 fprintf (file, "%s", pru_asm_regname (op));
1769 return;
1770 }
1771 else if (letter == 'b')
1772 {
1773 if (REGNO (op) > LAST_NONIO_GP_REGNUM)
1774 {
1775 output_operand_lossage ("I/O register operand for '%%%c'",
1776 letter);
1777 return;
1778 }
1779 fprintf (file, "r%d.b%d", REGNO (op) / 4, REGNO (op) % 4);
1780 return;
1781 }
1782 else if (letter == 'F' || letter == 'N')
1783 {
1784 if (REGNO (op) > LAST_NONIO_GP_REGNUM - 1)
1785 {
1786 output_operand_lossage ("I/O register operand for '%%%c'",
1787 letter);
1788 return;
1789 }
1790 if (REGNO (op) % 4 != 0)
1791 {
1792 output_operand_lossage ("non 32 bit register operand for '%%%c'",
1793 letter);
1794 return;
1795 }
1796 fprintf (file, "r%d", REGNO (op) / 4 + (letter == 'N' ? 1 : 0));
1797 return;
1798 }
1799 break;
1800
1801 case CONST_INT:
1802 if (letter == 'H')
1803 {
1804 HOST_WIDE_INT val = INTVAL (op);
1805 val = (val >> 16) & 0xFFFF;
1806 output_addr_const (file, gen_int_mode (val, SImode));
1807 return;
1808 }
1809 else if (letter == 'L')
1810 {
1811 HOST_WIDE_INT val = INTVAL (op);
1812 val &= 0xFFFF;
1813 output_addr_const (file, gen_int_mode (val, SImode));
1814 return;
1815 }
1816 else if (letter == 'T')
1817 {
1818 /* The predicate should have already validated the 1-high-bit
1819 requirement. Use CTZ here to deal with constant's sign
1820 extension. */
1821 HOST_WIDE_INT val = wi::ctz (INTVAL (op));
1822 if (val < 0 || val > 31)
1823 {
1824 output_operand_lossage ("invalid operand for '%%%c'", letter);
1825 return;
1826 }
1827 output_addr_const (file, gen_int_mode (val, SImode));
1828 return;
1829 }
1830 else if (letter == 'V')
1831 {
1832 HOST_WIDE_INT val = wi::ctz (~INTVAL (op));
1833 if (val < 0 || val > 31)
1834 {
1835 output_operand_lossage ("invalid operand for '%%%c'", letter);
1836 return;
1837 }
1838 output_addr_const (file, gen_int_mode (val, SImode));
1839 return;
1840 }
1841 else if (letter == 'w')
1842 {
1843 HOST_WIDE_INT val = INTVAL (op) & 0xffffffff;
1844 output_addr_const (file, gen_int_mode (val, SImode));
1845 return;
1846 }
1847 else if (letter == 'W')
1848 {
1849 HOST_WIDE_INT val = (INTVAL (op) >> 32) & 0xffffffff;
1850 output_addr_const (file, gen_int_mode (val, SImode));
1851 return;
1852 }
1853 else if (letter == 'u')
1854 {
1855 /* Workaround GCC's representation of QI constants in sign-extended
1856 form, and PRU's assembler insistence on unsigned constant
1857 integers. See the notes about O constraint. */
1858 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) & 0xff);
1859 return;
1860 }
1861 else if (letter == 'c')
1862 {
1863 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
1864 return;
1865 }
1866 else if (letter == 'n')
1867 {
1868 fprintf (file, HOST_WIDE_INT_PRINT_DEC, -INTVAL (op));
1869 return;
1870 }
1871 /* Else, fall through. */
1872
1873 case CONST:
1874 case LABEL_REF:
1875 case SYMBOL_REF:
1876 if (letter == 0 || letter == 'c')
1877 {
1878 output_addr_const (file, op);
1879 return;
1880 }
1881 break;
1882
1883 case CONST_FIXED:
1884 {
1885 HOST_WIDE_INT ival = INTVAL (pru_to_int_mode (op));
1886 if (letter != 0)
1887 output_operand_lossage ("unsupported code '%c' for fixed-point:",
1888 letter);
1889 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
1890 return;
1891 }
1892 break;
1893
1894 case CONST_DOUBLE:
1895 if (letter == 0)
1896 {
1897 long val;
1898
1899 if (GET_MODE (op) != SFmode)
1900 {
1901 output_operand_lossage ("double constants not supported");
1902 return;
1903 }
1904 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), val);
1905 fprintf (file, "0x%lx", val);
1906 return;
1907 }
1908 else if (letter == 'w' || letter == 'W')
1909 {
1910 long t[2];
1911 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (op), t);
1912 fprintf (file, "0x%lx", t[letter == 'w' ? 0 : 1]);
1913 return;
1914 }
1915 else
1916 {
1917 output_operand_lossage ("invalid operand for '%%%c'", letter);
1918 return;
1919 }
1920 break;
1921
1922 case SUBREG:
1923 /* Subregs should not appear at so late stage. */
1924 gcc_unreachable ();
1925 break;
1926
1927 case MEM:
1928 if (letter == 0)
1929 {
1930 output_address (VOIDmode, op);
1931 return;
1932 }
1933 else if (letter == 'B')
1934 {
1935 rtx base = XEXP (op, 0);
1936 if (GET_CODE (base) == PLUS)
1937 {
1938 rtx op0 = XEXP (base, 0);
1939 rtx op1 = XEXP (base, 1);
1940
1941 /* PLUS cannot have two constant operands, so first one
1942 of them must be a REG, hence we must check for an
1943 exact base address. */
1944 if (ctable_base_operand (op1, VOIDmode))
1945 {
1946 fprintf (file, "c");
1947 return;
1948 }
1949 else if (REG_P (op0))
1950 {
1951 fprintf (file, "b");
1952 return;
1953 }
1954 else
1955 gcc_unreachable ();
1956 }
1957 else if (REG_P (base))
1958 {
1959 fprintf (file, "b");
1960 return;
1961 }
1962 else if (ctable_addr_operand (base, VOIDmode))
1963 {
1964 fprintf (file, "c");
1965 return;
1966 }
1967 else
1968 gcc_unreachable ();
1969 }
1970 break;
1971
1972 case CODE_LABEL:
1973 if (letter == 0)
1974 {
1975 output_addr_const (file, op);
1976 return;
1977 }
1978 break;
1979
1980 default:
1981 break;
1982 }
1983
1984 output_operand_lossage ("unsupported operand %s for code '%c'",
1985 GET_RTX_NAME (GET_CODE (op)), letter);
1986 }
1987
1988 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
1989 static void
1990 pru_print_operand_address (FILE *file, machine_mode mode, rtx op)
1991 {
1992 if (CONSTANT_ADDRESS_P (op) && text_segment_operand (op, VOIDmode))
1993 {
1994 output_operand_lossage ("unexpected text address:");
1995 return;
1996 }
1997
1998 switch (GET_CODE (op))
1999 {
2000 case CONST:
2001 case LABEL_REF:
2002 case CONST_WIDE_INT:
2003 case SYMBOL_REF:
2004 break;
2005
2006 case CONST_INT:
2007 {
2008 unsigned HOST_WIDE_INT caddr = INTVAL (op);
2009 int base = pru_get_ctable_base_index (caddr);
2010 int offs = pru_get_ctable_base_offset (caddr);
2011 if (base < 0)
2012 {
2013 output_operand_lossage ("unsupported constant address:");
2014 return;
2015 }
2016 fprintf (file, "%d, %d", base, offs);
2017 return;
2018 }
2019 break;
2020
2021 case PLUS:
2022 {
2023 int base;
2024 rtx op0 = XEXP (op, 0);
2025 rtx op1 = XEXP (op, 1);
2026
2027 if (REG_P (op0) && CONST_INT_P (op1)
2028 && pru_get_ctable_exact_base_index (INTVAL (op1)) >= 0)
2029 {
2030 base = pru_get_ctable_exact_base_index (INTVAL (op1));
2031 fprintf (file, "%d, %s", base, pru_asm_regname (op0));
2032 return;
2033 }
2034 else if (REG_P (op1) && CONST_INT_P (op0)
2035 && pru_get_ctable_exact_base_index (INTVAL (op0)) >= 0)
2036 {
2037 /* Not a valid RTL. */
2038 gcc_unreachable ();
2039 }
2040 else if (REG_P (op0) && CONSTANT_P (op1))
2041 {
2042 fprintf (file, "%s, ", pru_asm_regname (op0));
2043 output_addr_const (file, op1);
2044 return;
2045 }
2046 else if (REG_P (op1) && CONSTANT_P (op0))
2047 {
2048 /* Not a valid RTL. */
2049 gcc_unreachable ();
2050 }
2051 else if (REG_P (op1) && REG_P (op0))
2052 {
2053 fprintf (file, "%s, %s", pru_asm_regname (op0),
2054 pru_asm_regname (op1));
2055 return;
2056 }
2057 }
2058 break;
2059
2060 case REG:
2061 fprintf (file, "%s, 0", pru_asm_regname (op));
2062 return;
2063
2064 case MEM:
2065 {
2066 rtx base = XEXP (op, 0);
2067 pru_print_operand_address (file, mode, base);
2068 return;
2069 }
2070 default:
2071 break;
2072 }
2073
2074 output_operand_lossage ("unsupported memory expression:");
2075 }
2076
2077 /* Implement TARGET_ASM_FUNCTION_PROLOGUE. */
2078 static void
2079 pru_asm_function_prologue (FILE *file)
2080 {
2081 if (flag_verbose_asm || flag_debug_asm)
2082 pru_dump_frame_layout (file);
2083 }
2084
2085 /* Implement `TARGET_ASM_INTEGER'.
2086 Target hook for assembling integer objects. PRU version needs
2087 special handling for references to pmem. Code copied from AVR. */
2088
2089 static bool
2090 pru_assemble_integer (rtx x, unsigned int size, int aligned_p)
2091 {
2092 if (size == POINTER_SIZE / BITS_PER_UNIT
2093 && aligned_p
2094 && text_segment_operand (x, VOIDmode))
2095 {
2096 fputs ("\t.4byte\t%pmem(", asm_out_file);
2097 output_addr_const (asm_out_file, x);
2098 fputs (")\n", asm_out_file);
2099
2100 return true;
2101 }
2102 else if (size == INIT_ARRAY_ENTRY_BYTES
2103 && aligned_p
2104 && text_segment_operand (x, VOIDmode))
2105 {
2106 fputs ("\t.2byte\t%pmem(", asm_out_file);
2107 output_addr_const (asm_out_file, x);
2108 fputs (")\n", asm_out_file);
2109
2110 return true;
2111 }
2112 else
2113 {
2114 return default_assemble_integer (x, size, aligned_p);
2115 }
2116 }
2117
2118 /* Implement TARGET_SECTION_TYPE_FLAGS. */
2119
2120 static unsigned int
2121 pru_section_type_flags (tree decl, const char *name, int reloc)
2122 {
2123 unsigned int flags = default_section_type_flags (decl, name, reloc);
2124
2125 /* The .pru_irq_map section is not meant to be loaded into the target
2126 memory. Instead its contents are read by the host remoteproc loader.
2127 To prevent being marked as a loadable (allocated) section, the
2128 .pru_irq_map section is intercepted and marked as a debug section. */
2129 if (!strcmp (name, ".pru_irq_map"))
2130 flags = SECTION_DEBUG | SECTION_RETAIN;
2131
2132 return flags;
2133 }
2134
2135 /* Implement TARGET_ASM_FILE_START. */
2136
2137 static void
2138 pru_file_start (void)
2139 {
2140 default_file_start ();
2141
2142 /* Compiler will take care of placing %label, so there is no
2143 need to confuse users with this warning. */
2144 fprintf (asm_out_file, "\t.set no_warn_regname_label\n");
2145 }
2146
2147 /* Scan type TYP for pointer references to address space other than
2148 ADDR_SPACE_GENERIC. Return true if such reference is found.
2149 Much of this code was taken from the avr port. */
2150
2151 static bool
2152 pru_nongeneric_pointer_addrspace (tree typ)
2153 {
2154 while (ARRAY_TYPE == TREE_CODE (typ))
2155 typ = TREE_TYPE (typ);
2156
2157 if (POINTER_TYPE_P (typ))
2158 {
2159 addr_space_t as;
2160 tree target = TREE_TYPE (typ);
2161
2162 /* Pointer to function: Test the function's return type. */
2163 if (FUNCTION_TYPE == TREE_CODE (target))
2164 return pru_nongeneric_pointer_addrspace (TREE_TYPE (target));
2165
2166 /* "Ordinary" pointers... */
2167
2168 while (TREE_CODE (target) == ARRAY_TYPE)
2169 target = TREE_TYPE (target);
2170
2171 as = TYPE_ADDR_SPACE (target);
2172
2173 if (!ADDR_SPACE_GENERIC_P (as))
2174 return true;
2175
2176 /* Scan pointer's target type. */
2177 return pru_nongeneric_pointer_addrspace (target);
2178 }
2179
2180 return false;
2181 }
2182
2183 /* Implement `TARGET_INSERT_ATTRIBUTES'. For PRU it's used as a hook to
2184 provide better diagnostics for some invalid usages of the __regio_symbol
2185 address space.
2186
2187 Any escapes of the following checks are supposed to be caught
2188 during the "mov<mode>" pattern expansion. */
2189
2190 static void
2191 pru_insert_attributes (tree node, tree *)
2192 {
2193
2194 /* Validate __regio_symbol variable declarations. */
2195 if (VAR_P (node))
2196 {
2197 const char *name = DECL_NAME (node)
2198 ? IDENTIFIER_POINTER (DECL_NAME (node))
2199 : "<unknown>";
2200 tree typ = TREE_TYPE (node);
2201 addr_space_t as = TYPE_ADDR_SPACE (typ);
2202
2203 if (as == ADDR_SPACE_GENERIC)
2204 return;
2205
2206 if (AGGREGATE_TYPE_P (typ))
2207 {
2208 error ("aggregate types are prohibited in "
2209 "%<__regio_symbol%> address space");
2210 /* Don't bother anymore. Below checks would pile
2211 meaningless errors, which would confuse user. */
2212 return;
2213 }
2214 if (DECL_INITIAL (node) != NULL_TREE)
2215 error ("variables in %<__regio_symbol%> address space "
2216 "cannot have initial value");
2217 if (DECL_REGISTER (node))
2218 error ("variables in %<__regio_symbol%> address space "
2219 "cannot be declared %<register%>");
2220 if (!TYPE_VOLATILE (typ))
2221 error ("variables in %<__regio_symbol%> address space "
2222 "must be declared %<volatile%>");
2223 if (!DECL_EXTERNAL (node))
2224 error ("variables in %<__regio_symbol%> address space "
2225 "must be declared %<extern%>");
2226 if (TYPE_MODE (typ) != SImode)
2227 error ("only 32-bit access is supported "
2228 "for %<__regio_symbol%> address space");
2229 if (strcmp (name, "__R30") != 0 && strcmp (name, "__R31") != 0)
2230 error ("register name %qs not recognized "
2231 "in %<__regio_symbol%> address space", name);
2232 }
2233
2234 tree typ = NULL_TREE;
2235
2236 switch (TREE_CODE (node))
2237 {
2238 case FUNCTION_DECL:
2239 typ = TREE_TYPE (TREE_TYPE (node));
2240 break;
2241 case TYPE_DECL:
2242 case RESULT_DECL:
2243 case VAR_DECL:
2244 case FIELD_DECL:
2245 case PARM_DECL:
2246 typ = TREE_TYPE (node);
2247 break;
2248 case POINTER_TYPE:
2249 typ = node;
2250 break;
2251 default:
2252 break;
2253 }
2254 if (typ != NULL_TREE && pru_nongeneric_pointer_addrspace (typ))
2255 error ("pointers to %<__regio_symbol%> address space are prohibited");
2256 }
2257
2258 /* Function argument related. */
2259
2260 /* Return the number of bytes needed for storing an argument with
2261 the given MODE and TYPE. */
2262 static int
2263 pru_function_arg_size (machine_mode mode, const_tree type)
2264 {
2265 HOST_WIDE_INT param_size;
2266
2267 if (mode == BLKmode)
2268 param_size = int_size_in_bytes (type);
2269 else
2270 param_size = GET_MODE_SIZE (mode);
2271
2272 /* Convert to words (round up). */
2273 param_size = (UNITS_PER_WORD - 1 + param_size) / UNITS_PER_WORD;
2274 gcc_assert (param_size >= 0);
2275
2276 return param_size;
2277 }
2278
2279 /* Check if argument with the given size must be
2280 passed/returned in a register.
2281
2282 Reference:
2283 https://e2e.ti.com/support/development_tools/compiler/f/343/p/650176/2393029
2284
2285 Arguments other than 8/16/24/32/64bits are passed on stack. */
2286 static bool
2287 pru_arg_in_reg_bysize (size_t sz)
2288 {
2289 return sz == 1 || sz == 2 || sz == 3 || sz == 4 || sz == 8;
2290 }
2291
2292 /* Helper function to get the starting storage HW register for an argument,
2293 or -1 if it must be passed on stack. The cum_v state is not changed. */
2294 static int
2295 pru_function_arg_regi (cumulative_args_t cum_v,
2296 machine_mode mode, const_tree type,
2297 bool named)
2298 {
2299 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2300 size_t argsize = pru_function_arg_size (mode, type);
2301 size_t i, bi;
2302 int regi = -1;
2303
2304 if (!pru_arg_in_reg_bysize (argsize))
2305 return -1;
2306
2307 if (!named)
2308 return -1;
2309
2310 /* Find the first available slot that fits. Yes, that's the PRU ABI. */
2311 for (i = 0; regi < 0 && i < ARRAY_SIZE (cum->regs_used); i++)
2312 {
2313 /* VLAs and vector types are not defined in the PRU ABI. Let's
2314 handle them the same as their same-sized counterparts. This way
2315 we do not need to treat BLKmode differently, and need only to check
2316 the size. */
2317 gcc_assert (argsize == 1 || argsize == 2 || argsize == 3
2318 || argsize == 4 || argsize == 8);
2319
2320 /* Ensure SI and DI arguments are stored in full registers only. */
2321 if ((argsize >= 4) && (i % 4) != 0)
2322 continue;
2323
2324 /* Structures with size 24 bits are passed starting at a full
2325 register boundary. */
2326 if (argsize == 3 && (i % 4) != 0)
2327 continue;
2328
2329 /* rX.w0/w1/w2 are OK. But avoid spreading the second byte
2330 into a different full register. */
2331 if (argsize == 2 && (i % 4) == 3)
2332 continue;
2333
2334 for (bi = 0;
2335 bi < argsize && (bi + i) < ARRAY_SIZE (cum->regs_used);
2336 bi++)
2337 {
2338 if (cum->regs_used[bi + i])
2339 break;
2340 }
2341 if (bi == argsize)
2342 regi = FIRST_ARG_REGNUM + i;
2343 }
2344
2345 return regi;
2346 }
2347
2348 /* Mark CUM_V that a function argument will occupy HW register slot starting
2349 at REGI. The number of consecutive 8-bit HW registers marked as occupied
2350 depends on the MODE and TYPE of the argument. */
2351 static void
2352 pru_function_arg_regi_mark_slot (int regi,
2353 cumulative_args_t cum_v,
2354 machine_mode mode, const_tree type,
2355 bool named)
2356 {
2357 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2358 HOST_WIDE_INT param_size = pru_function_arg_size (mode, type);
2359
2360 gcc_assert (named);
2361
2362 /* Mark all byte sub-registers occupied by argument as used. */
2363 while (param_size--)
2364 {
2365 gcc_assert (regi >= FIRST_ARG_REGNUM && regi <= LAST_ARG_REGNUM);
2366 gcc_assert (!cum->regs_used[regi - FIRST_ARG_REGNUM]);
2367 cum->regs_used[regi - FIRST_ARG_REGNUM] = true;
2368 regi++;
2369 }
2370 }
2371
2372 /* Define where to put the arguments to a function. Value is zero to
2373 push the argument on the stack, or a hard register in which to
2374 store the argument.
2375
2376 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2377 the preceding args and about the function being called.
2378 ARG is a description of the argument. */
2379
2380 static rtx
2381 pru_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
2382 {
2383 rtx return_rtx = NULL_RTX;
2384 int regi = pru_function_arg_regi (cum_v, arg.mode, arg.type, arg.named);
2385
2386 if (regi >= 0)
2387 return_rtx = gen_rtx_REG (arg.mode, regi);
2388
2389 return return_rtx;
2390 }
2391
2392 /* Implement TARGET_ARG_PARTIAL_BYTES. PRU never splits any arguments
2393 between registers and memory, so we can return 0. */
2394
2395 static int
2396 pru_arg_partial_bytes (cumulative_args_t, const function_arg_info &)
2397 {
2398 return 0;
2399 }
2400
2401 /* Update the data in CUM to advance over argument ARG. */
2402
2403 static void
2404 pru_function_arg_advance (cumulative_args_t cum_v,
2405 const function_arg_info &arg)
2406 {
2407 int regi = pru_function_arg_regi (cum_v, arg.mode, arg.type, arg.named);
2408
2409 if (regi >= 0)
2410 pru_function_arg_regi_mark_slot (regi, cum_v, arg.mode,
2411 arg.type, arg.named);
2412 }
2413
2414 /* Implement TARGET_FUNCTION_VALUE. */
2415 static rtx
2416 pru_function_value (const_tree ret_type, const_tree, bool)
2417 {
2418 return gen_rtx_REG (TYPE_MODE (ret_type), FIRST_RETVAL_REGNUM);
2419 }
2420
2421 /* Implement TARGET_LIBCALL_VALUE. */
2422 static rtx
2423 pru_libcall_value (machine_mode mode, const_rtx)
2424 {
2425 return gen_rtx_REG (mode, FIRST_RETVAL_REGNUM);
2426 }
2427
2428 /* Implement TARGET_FUNCTION_VALUE_REGNO_P. */
2429 static bool
2430 pru_function_value_regno_p (const unsigned int regno)
2431 {
2432 return regno == FIRST_RETVAL_REGNUM;
2433 }
2434
2435 /* Implement TARGET_RETURN_IN_MEMORY. */
2436 bool
2437 pru_return_in_memory (const_tree type, const_tree)
2438 {
2439 bool in_memory = (!pru_arg_in_reg_bysize (int_size_in_bytes (type))
2440 || int_size_in_bytes (type) == -1);
2441
2442 return in_memory;
2443 }
2444
2445 /* Implement TARGET_CAN_USE_DOLOOP_P. */
2446
2447 static bool
2448 pru_can_use_doloop_p (const widest_int &, const widest_int &iterations_max,
2449 unsigned int loop_depth, bool)
2450 {
2451 /* Considering limitations in the hardware, only use doloop
2452 for innermost loops which must be entered from the top. */
2453 if (loop_depth > 1)
2454 return false;
2455 /* PRU internal loop counter is 16bits wide. Remember that iterations_max
2456 holds the maximum number of loop latch executions, while PRU loop
2457 instruction needs the count of loop body executions. */
2458 if (iterations_max == 0 || wi::geu_p (iterations_max, 0xffff))
2459 return false;
2460
2461 return true;
2462 }
2463
2464 /* NULL if INSN insn is valid within a low-overhead loop.
2465 Otherwise return why doloop cannot be applied. */
2466
2467 static const char *
2468 pru_invalid_within_doloop (const rtx_insn *insn)
2469 {
2470 if (CALL_P (insn))
2471 return "Function call in the loop.";
2472
2473 if (JUMP_P (insn) && INSN_CODE (insn) == CODE_FOR_return)
2474 return "Return from a call instruction in the loop.";
2475
2476 if (NONDEBUG_INSN_P (insn)
2477 && INSN_CODE (insn) < 0
2478 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
2479 || asm_noperands (PATTERN (insn)) >= 0))
2480 return "Loop contains asm statement.";
2481
2482 return NULL;
2483 }
2484
2485
2486 /* Figure out where to put LABEL, which is the label for a repeat loop.
2487 The loop ends just before LAST_INSN. If SHARED, insns other than the
2488 "repeat" might use LABEL to jump to the loop's continuation point.
2489
2490 Return the last instruction in the adjusted loop. */
2491
2492 static rtx_insn *
2493 pru_insert_loop_label_last (rtx_insn *last_insn, rtx_code_label *label,
2494 bool shared)
2495 {
2496 rtx_insn *next, *prev;
2497 int count = 0, code, icode;
2498
2499 if (dump_file)
2500 fprintf (dump_file, "considering end of repeat loop at insn %d\n",
2501 INSN_UID (last_insn));
2502
2503 /* Set PREV to the last insn in the loop. */
2504 prev = PREV_INSN (last_insn);
2505
2506 /* Set NEXT to the next insn after the loop label. */
2507 next = last_insn;
2508 if (!shared)
2509 while (prev != 0)
2510 {
2511 code = GET_CODE (prev);
2512 if (code == CALL_INSN || code == CODE_LABEL || code == BARRIER)
2513 break;
2514
2515 if (INSN_P (prev))
2516 {
2517 if (GET_CODE (PATTERN (prev)) == SEQUENCE)
2518 prev = as_a <rtx_insn *> (XVECEXP (PATTERN (prev), 0, 1));
2519
2520 /* Other insns that should not be in the last two opcodes. */
2521 icode = recog_memoized (prev);
2522 if (icode < 0
2523 || icode == CODE_FOR_pruloophi
2524 || icode == CODE_FOR_pruloopsi)
2525 break;
2526
2527 count++;
2528 next = prev;
2529 if (dump_file)
2530 print_rtl_single (dump_file, next);
2531 if (count == 2)
2532 break;
2533 }
2534 prev = PREV_INSN (prev);
2535 }
2536
2537 /* Insert the nops. */
2538 if (dump_file && count < 2)
2539 fprintf (dump_file, "Adding %d nop%s inside loop\n\n",
2540 2 - count, count == 1 ? "" : "s");
2541
2542 for (; count < 2; count++)
2543 emit_insn_before (gen_nop (), last_insn);
2544
2545 /* Insert the label. */
2546 emit_label_before (label, last_insn);
2547
2548 return last_insn;
2549 }
2550
2551 /* If IS_END is false, expand a canonical doloop_begin RTL into the
2552 PRU-specific doloop_begin_internal. Otherwise expand doloop_end to
2553 doloop_end_internal. */
2554 void
2555 pru_emit_doloop (rtx *operands, int is_end)
2556 {
2557 rtx tag;
2558
2559 if (cfun->machine->doloop_tags == 0
2560 || cfun->machine->doloop_tag_from_end == is_end)
2561 {
2562 cfun->machine->doloop_tags++;
2563 cfun->machine->doloop_tag_from_end = is_end;
2564 }
2565
2566 tag = GEN_INT (cfun->machine->doloop_tags - 1);
2567 machine_mode opmode = GET_MODE (operands[0]);
2568 gcc_assert (opmode == HImode || opmode == SImode);
2569
2570 if (is_end)
2571 emit_jump_insn (gen_doloop_end_internal (opmode, operands[0],
2572 operands[1], tag));
2573 else
2574 emit_insn (gen_doloop_begin_internal (opmode, operands[0],
2575 operands[0], tag));
2576 }
2577
2578
2579 /* Code for converting doloop_begins and doloop_ends into valid
2580 PRU instructions. Idea and code snippets borrowed from mep port.
2581
2582 A doloop_begin is just a placeholder:
2583
2584 $count = unspec ($count)
2585
2586 where $count is initially the number of iterations.
2587 doloop_end has the form:
2588
2589 if (--$count == 0) goto label
2590
2591 The counter variable is private to the doloop insns, nothing else
2592 relies on its value.
2593
2594 There are three cases, in decreasing order of preference:
2595
2596 1. A loop has exactly one doloop_begin and one doloop_end.
2597 The doloop_end branches to the first instruction after
2598 the doloop_begin.
2599
2600 In this case we can replace the doloop_begin with a LOOP
2601 instruction and remove the doloop_end. I.e.:
2602
2603 $count1 = unspec ($count1)
2604 label:
2605 ...
2606 if (--$count2 != 0) goto label
2607
2608 becomes:
2609
2610 LOOP end_label,$count1
2611 label:
2612 ...
2613 end_label:
2614 # end loop
2615
2616 2. As for (1), except there are several doloop_ends. One of them
2617 (call it X) falls through to a label L. All the others fall
2618 through to branches to L.
2619
2620 In this case, we remove X and replace the other doloop_ends
2621 with branches to the LOOP label. For example:
2622
2623 $count1 = unspec ($count1)
2624 label:
2625 ...
2626 if (--$count1 != 0) goto label
2627 end_label:
2628 ...
2629 if (--$count2 != 0) goto label
2630 goto end_label
2631
2632 becomes:
2633
2634 LOOP end_label,$count1
2635 label:
2636 ...
2637 end_label:
2638 # end repeat
2639 ...
2640 goto end_label
2641
2642 3. The fallback case. Replace doloop_begins with:
2643
2644 $count = $count
2645
2646 Replace doloop_ends with the equivalent of:
2647
2648 $count = $count - 1
2649 if ($count != 0) goto loop_label
2650
2651 */
2652
2653 /* A structure describing one doloop_begin. */
2654 struct pru_doloop_begin {
2655 /* The next doloop_begin with the same tag. */
2656 struct pru_doloop_begin *next;
2657
2658 /* The instruction itself. */
2659 rtx_insn *insn;
2660
2661 /* The initial counter value. */
2662 rtx loop_count;
2663
2664 /* The counter register. */
2665 rtx counter;
2666 };
2667
2668 /* A structure describing a doloop_end. */
2669 struct pru_doloop_end {
2670 /* The next doloop_end with the same loop tag. */
2671 struct pru_doloop_end *next;
2672
2673 /* The instruction itself. */
2674 rtx_insn *insn;
2675
2676 /* The first instruction after INSN when the branch isn't taken. */
2677 rtx_insn *fallthrough;
2678
2679 /* The location of the counter value. Since doloop_end_internal is a
2680 jump instruction, it has to allow the counter to be stored anywhere
2681 (any non-fixed register). */
2682 rtx counter;
2683
2684 /* The target label (the place where the insn branches when the counter
2685 isn't zero). */
2686 rtx label;
2687
2688 /* A scratch register. Only available when COUNTER isn't stored
2689 in a general register. */
2690 rtx scratch;
2691 };
2692
2693
2694 /* One do-while loop. */
2695 struct pru_doloop {
2696 /* All the doloop_begins for this loop (in no particular order). */
2697 struct pru_doloop_begin *begin;
2698
2699 /* All the doloop_ends. When there is more than one, arrange things
2700 so that the first one is the most likely to be X in case (2) above. */
2701 struct pru_doloop_end *end;
2702 };
2703
2704
2705 /* Return true if LOOP can be converted into LOOP form
2706 (that is, if it matches cases (1) or (2) above). */
2707
2708 static bool
2709 pru_repeat_loop_p (struct pru_doloop *loop)
2710 {
2711 struct pru_doloop_end *end;
2712 rtx_insn *fallthrough;
2713
2714 /* There must be exactly one doloop_begin and at least one doloop_end. */
2715 if (loop->begin == 0 || loop->end == 0 || loop->begin->next != 0)
2716 return false;
2717
2718 /* The first doloop_end (X) must branch back to the insn after
2719 the doloop_begin. */
2720 if (prev_real_insn (as_a<rtx_insn *> (loop->end->label)) != loop->begin->insn)
2721 return false;
2722
2723 /* Check that the first doloop_end (X) can actually reach
2724 doloop_begin () with U8_PCREL relocation for LOOP instruction. */
2725 if (get_attr_length (loop->end->insn) != 4)
2726 return false;
2727
2728 /* All the other doloop_ends must branch to the same place as X.
2729 When the branch isn't taken, they must jump to the instruction
2730 after X. */
2731 fallthrough = loop->end->fallthrough;
2732 for (end = loop->end->next; end != 0; end = end->next)
2733 if (end->label != loop->end->label
2734 || !simplejump_p (end->fallthrough)
2735 || fallthrough
2736 != next_real_insn (JUMP_LABEL_AS_INSN (end->fallthrough)))
2737 return false;
2738
2739 return true;
2740 }
2741
2742
2743 /* The main repeat reorg function. See comment above for details. */
2744
2745 static void
2746 pru_reorg_loop (rtx_insn *insns)
2747 {
2748 rtx_insn *insn;
2749 struct pru_doloop *loops, *loop;
2750 struct pru_doloop_begin *begin;
2751 struct pru_doloop_end *end;
2752 size_t tmpsz;
2753
2754 /* Quick exit if we haven't created any loops. */
2755 if (cfun->machine->doloop_tags == 0)
2756 return;
2757
2758 /* Create an array of pru_doloop structures. */
2759 tmpsz = sizeof (loops[0]) * cfun->machine->doloop_tags;
2760 loops = (struct pru_doloop *) alloca (tmpsz);
2761 memset (loops, 0, sizeof (loops[0]) * cfun->machine->doloop_tags);
2762
2763 /* Search the function for do-while insns and group them by loop tag. */
2764 for (insn = insns; insn; insn = NEXT_INSN (insn))
2765 if (INSN_P (insn))
2766 switch (recog_memoized (insn))
2767 {
2768 case CODE_FOR_doloop_begin_internalhi:
2769 case CODE_FOR_doloop_begin_internalsi:
2770 insn_extract (insn);
2771 loop = &loops[INTVAL (recog_data.operand[2])];
2772
2773 tmpsz = sizeof (struct pru_doloop_begin);
2774 begin = (struct pru_doloop_begin *) alloca (tmpsz);
2775 begin->next = loop->begin;
2776 begin->insn = insn;
2777 begin->loop_count = recog_data.operand[1];
2778 begin->counter = recog_data.operand[0];
2779
2780 loop->begin = begin;
2781 break;
2782
2783 case CODE_FOR_doloop_end_internalhi:
2784 case CODE_FOR_doloop_end_internalsi:
2785 insn_extract (insn);
2786 loop = &loops[INTVAL (recog_data.operand[2])];
2787
2788 tmpsz = sizeof (struct pru_doloop_end);
2789 end = (struct pru_doloop_end *) alloca (tmpsz);
2790 end->insn = insn;
2791 end->fallthrough = next_real_insn (insn);
2792 end->counter = recog_data.operand[0];
2793 end->label = recog_data.operand[1];
2794 end->scratch = recog_data.operand[3];
2795
2796 /* If this insn falls through to an unconditional jump,
2797 give it a lower priority than the others. */
2798 if (loop->end != 0 && simplejump_p (end->fallthrough))
2799 {
2800 end->next = loop->end->next;
2801 loop->end->next = end;
2802 }
2803 else
2804 {
2805 end->next = loop->end;
2806 loop->end = end;
2807 }
2808 break;
2809 }
2810
2811 /* Convert the insns for each loop in turn. */
2812 for (loop = loops; loop < loops + cfun->machine->doloop_tags; loop++)
2813 if (pru_repeat_loop_p (loop))
2814 {
2815 /* Case (1) or (2). */
2816 rtx_code_label *repeat_label;
2817 rtx label_ref;
2818 rtx loop_rtx;
2819
2820 /* Create a new label for the repeat insn. */
2821 repeat_label = gen_label_rtx ();
2822
2823 /* Replace the doloop_begin with a repeat. We get rid
2824 of the iteration register because LOOP instruction
2825 will utilize an internal for the PRU core LOOP register. */
2826 label_ref = gen_rtx_LABEL_REF (VOIDmode, repeat_label);
2827 machine_mode loop_mode = GET_MODE (loop->begin->loop_count);
2828 if (loop_mode == VOIDmode)
2829 {
2830 gcc_assert (CONST_INT_P (loop->begin->loop_count));
2831 gcc_assert (UBYTE_INT ( INTVAL (loop->begin->loop_count)));
2832 loop_mode = SImode;
2833 }
2834 gcc_assert (loop_mode == HImode || loop_mode == SImode);
2835 loop_rtx = gen_pruloop (loop_mode, loop->begin->loop_count, label_ref);
2836 emit_insn_before (loop_rtx, loop->begin->insn);
2837
2838 delete_insn (loop->begin->insn);
2839
2840 /* Insert the repeat label before the first doloop_end.
2841 Fill the gap with nops if LOOP insn is less than 2
2842 instructions away than loop->end. */
2843 pru_insert_loop_label_last (loop->end->insn, repeat_label,
2844 loop->end->next != 0);
2845
2846 /* Emit a pruloop_end (to improve the readability of the output). */
2847 emit_insn_before (gen_pruloop_end (), loop->end->insn);
2848
2849 /* HACK: TODO: This is usually not needed, but is required for
2850 a few rare cases where a JUMP that breaks the loop
2851 references the LOOP_END address. In other words, since
2852 we're missing a real "loop_end" instruction, a loop "break"
2853 may accidentally reference the loop end itself, and thus
2854 continuing the cycle. */
2855 for (insn = NEXT_INSN (loop->end->insn);
2856 insn != next_real_insn (loop->end->insn);
2857 insn = NEXT_INSN (insn))
2858 {
2859 if (LABEL_P (insn) && LABEL_NUSES (insn) > 0)
2860 emit_insn_before (gen_nop_loop_guard (), loop->end->insn);
2861 }
2862
2863 /* Delete the first doloop_end. */
2864 delete_insn (loop->end->insn);
2865
2866 /* Replace the others with branches to REPEAT_LABEL. */
2867 for (end = loop->end->next; end != 0; end = end->next)
2868 {
2869 rtx_insn *newjmp;
2870 newjmp = emit_jump_insn_before (gen_jump (repeat_label), end->insn);
2871 JUMP_LABEL (newjmp) = repeat_label;
2872 delete_insn (end->insn);
2873 delete_insn (end->fallthrough);
2874 }
2875 }
2876 else
2877 {
2878 /* Case (3). First replace all the doloop_begins with setting
2879 the HW register used for loop counter. */
2880 for (begin = loop->begin; begin != 0; begin = begin->next)
2881 {
2882 insn = gen_move_insn (copy_rtx (begin->counter),
2883 copy_rtx (begin->loop_count));
2884 emit_insn_before (insn, begin->insn);
2885 delete_insn (begin->insn);
2886 }
2887
2888 /* Replace all the doloop_ends with decrement-and-branch sequences. */
2889 for (end = loop->end; end != 0; end = end->next)
2890 {
2891 rtx reg;
2892
2893 start_sequence ();
2894
2895 /* Load the counter value into a general register. */
2896 reg = end->counter;
2897 if (!REG_P (reg) || REGNO (reg) > LAST_NONIO_GP_REGNUM)
2898 {
2899 reg = end->scratch;
2900 emit_move_insn (copy_rtx (reg), copy_rtx (end->counter));
2901 }
2902
2903 /* Decrement the counter. */
2904 emit_insn (gen_add3_insn (copy_rtx (reg), copy_rtx (reg),
2905 constm1_rtx));
2906
2907 /* Copy it back to its original location. */
2908 if (reg != end->counter)
2909 emit_move_insn (copy_rtx (end->counter), copy_rtx (reg));
2910
2911 /* Jump back to the start label. */
2912 insn = emit_jump_insn (gen_cbranchsi4 (gen_rtx_NE (VOIDmode, reg,
2913 const0_rtx),
2914 reg,
2915 const0_rtx,
2916 end->label));
2917
2918 JUMP_LABEL (insn) = end->label;
2919 LABEL_NUSES (end->label)++;
2920
2921 /* Emit the whole sequence before the doloop_end. */
2922 insn = get_insns ();
2923 end_sequence ();
2924 emit_insn_before (insn, end->insn);
2925
2926 /* Delete the doloop_end. */
2927 delete_insn (end->insn);
2928 }
2929 }
2930 }
2931
2932 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
2933 static void
2934 pru_reorg (void)
2935 {
2936 rtx_insn *insns = get_insns ();
2937
2938 compute_bb_for_insn ();
2939 df_analyze ();
2940
2941 /* Need correct insn lengths for allowing LOOP instruction
2942 emitting due to U8_PCREL limitations. */
2943 shorten_branches (get_insns ());
2944
2945 /* The generic reorg_loops () is not suitable for PRU because
2946 it doesn't handle doloop_begin/end tying. And we need our
2947 doloop_begin emitted before reload. It is difficult to coalesce
2948 UBYTE constant initial loop values into the LOOP insn during
2949 machine reorg phase. */
2950 pru_reorg_loop (insns);
2951
2952 df_finish_pass (false);
2953 }
2954
2955 /* Enumerate all PRU-specific builtins. */
2956 enum pru_builtin
2957 {
2958 PRU_BUILTIN_DELAY_CYCLES,
2959 PRU_BUILTIN_HALT,
2960 PRU_BUILTIN_LMBD,
2961 PRU_BUILTIN_max
2962 };
2963
2964 static GTY(()) tree pru_builtins [(int) PRU_BUILTIN_max];
2965
2966 /* Implement TARGET_INIT_BUILTINS. */
2967
2968 static void
2969 pru_init_builtins (void)
2970 {
2971 tree void_ftype_longlong
2972 = build_function_type_list (void_type_node,
2973 long_long_integer_type_node,
2974 NULL);
2975 tree uint_ftype_uint_uint
2976 = build_function_type_list (unsigned_type_node,
2977 unsigned_type_node,
2978 unsigned_type_node,
2979 NULL);
2980
2981 tree void_ftype_void
2982 = build_function_type_list (void_type_node,
2983 void_type_node,
2984 NULL);
2985
2986 pru_builtins[PRU_BUILTIN_DELAY_CYCLES]
2987 = add_builtin_function ("__delay_cycles", void_ftype_longlong,
2988 PRU_BUILTIN_DELAY_CYCLES, BUILT_IN_MD, NULL,
2989 NULL_TREE);
2990
2991 pru_builtins[PRU_BUILTIN_HALT]
2992 = add_builtin_function ("__halt", void_ftype_void,
2993 PRU_BUILTIN_HALT, BUILT_IN_MD, NULL,
2994 NULL_TREE);
2995
2996 pru_builtins[PRU_BUILTIN_LMBD]
2997 = add_builtin_function ("__lmbd", uint_ftype_uint_uint,
2998 PRU_BUILTIN_LMBD, BUILT_IN_MD, NULL,
2999 NULL_TREE);
3000 }
3001
3002 /* Implement TARGET_BUILTIN_DECL. */
3003
3004 static tree
3005 pru_builtin_decl (unsigned code, bool)
3006 {
3007 switch (code)
3008 {
3009 case PRU_BUILTIN_DELAY_CYCLES:
3010 case PRU_BUILTIN_HALT:
3011 case PRU_BUILTIN_LMBD:
3012 return pru_builtins[code];
3013 default:
3014 return error_mark_node;
3015 }
3016 }
3017
3018 /* Emit a sequence of one or more delay_cycles_X insns, in order to generate
3019 code that delays exactly ARG cycles. */
3020
3021 static rtx
3022 pru_expand_delay_cycles (rtx arg)
3023 {
3024 HOST_WIDE_INT c, n;
3025
3026 if (GET_CODE (arg) != CONST_INT)
3027 {
3028 error ("%<__delay_cycles%> only takes constant arguments");
3029 return NULL_RTX;
3030 }
3031
3032 c = INTVAL (arg);
3033
3034 gcc_assert (HOST_BITS_PER_WIDE_INT > 32);
3035 if (c < 0)
3036 {
3037 error ("%<__delay_cycles%> only takes non-negative cycle counts");
3038 return NULL_RTX;
3039 }
3040
3041 emit_insn (gen_delay_cycles_start (arg));
3042
3043 /* For 32-bit loops, there's 2 + 2x cycles. */
3044 if (c > 2 * 0xffff + 1)
3045 {
3046 n = (c - 2) / 2;
3047 c -= (n * 2) + 2;
3048 if ((unsigned long long) n > 0xffffffffULL)
3049 {
3050 error ("%<__delay_cycles%> is limited to 32-bit loop counts");
3051 return NULL_RTX;
3052 }
3053 emit_insn (gen_delay_cycles_2x_plus2_si (GEN_INT (n)));
3054 }
3055
3056 /* For 16-bit loops, there's 1 + 2x cycles. */
3057 if (c > 2)
3058 {
3059 n = (c - 1) / 2;
3060 c -= (n * 2) + 1;
3061
3062 emit_insn (gen_delay_cycles_2x_plus1_hi (GEN_INT (n)));
3063 }
3064
3065 while (c > 0)
3066 {
3067 emit_insn (gen_delay_cycles_1 ());
3068 c -= 1;
3069 }
3070
3071 emit_insn (gen_delay_cycles_end (arg));
3072
3073 return NULL_RTX;
3074 }
3075
3076
3077 /* Implement TARGET_EXPAND_BUILTIN. Expand an expression EXP that calls
3078 a built-in function, with result going to TARGET if that's convenient
3079 (and in mode MODE if that's convenient).
3080 SUBTARGET may be used as the target for computing one of EXP's operands.
3081 IGNORE is nonzero if the value is to be ignored. */
3082
3083 static rtx
3084 pru_expand_builtin (tree exp, rtx target, rtx, machine_mode mode, int)
3085 {
3086 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3087 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
3088
3089 switch (fcode)
3090 {
3091 case PRU_BUILTIN_DELAY_CYCLES:
3092 {
3093 rtx arg1 = expand_normal (CALL_EXPR_ARG (exp, 0));
3094 return pru_expand_delay_cycles (arg1);
3095 }
3096 break;
3097 case PRU_BUILTIN_HALT:
3098 {
3099 emit_insn (gen_pru_halt ());
3100 return NULL_RTX;
3101 }
3102 break;
3103 case PRU_BUILTIN_LMBD:
3104 {
3105 rtx arg1 = expand_normal (CALL_EXPR_ARG (exp, 0));
3106 rtx arg2 = expand_normal (CALL_EXPR_ARG (exp, 1));
3107
3108 if (target == NULL_RTX || GET_MODE (target) != mode)
3109 {
3110 target = gen_reg_rtx (mode);
3111 }
3112
3113 emit_insn (gen_pru_lmbd (mode, target, arg1, arg2));
3114 return target;
3115 }
3116 break;
3117 default:
3118 internal_error ("bad builtin code");
3119 }
3120
3121 return NULL_RTX;
3122 }
3123
3124 /* Remember the last target of pru_set_current_function. */
3125 static GTY(()) tree pru_previous_fndecl;
3126
3127 /* Establish appropriate back-end context for processing the function
3128 FNDECL. The argument might be NULL to indicate processing at top
3129 level, outside of any function scope. */
3130 static void
3131 pru_set_current_function (tree fndecl)
3132 {
3133 tree old_tree = (pru_previous_fndecl
3134 ? DECL_FUNCTION_SPECIFIC_TARGET (pru_previous_fndecl)
3135 : NULL_TREE);
3136
3137 tree new_tree = (fndecl
3138 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
3139 : NULL_TREE);
3140
3141 if (fndecl && fndecl != pru_previous_fndecl)
3142 {
3143 pru_previous_fndecl = fndecl;
3144 if (old_tree == new_tree)
3145 ;
3146
3147 else if (new_tree)
3148 {
3149 cl_target_option_restore (&global_options, &global_options_set,
3150 TREE_TARGET_OPTION (new_tree));
3151 target_reinit ();
3152 }
3153
3154 else if (old_tree)
3155 {
3156 struct cl_target_option *def
3157 = TREE_TARGET_OPTION (target_option_current_node);
3158
3159 cl_target_option_restore (&global_options, &global_options_set, def);
3160 target_reinit ();
3161 }
3162 }
3163 }
3164
3165 /* Implement TARGET_UNWIND_WORD_MODE.
3166
3167 Since PRU is really a 32-bit CPU, the default word_mode is not suitable. */
3168 static scalar_int_mode
3169 pru_unwind_word_mode (void)
3170 {
3171 return SImode;
3172 }
3173
3174
3175 /* Initialize the GCC target structure. */
3176 #undef TARGET_ASM_FUNCTION_PROLOGUE
3177 #define TARGET_ASM_FUNCTION_PROLOGUE pru_asm_function_prologue
3178 #undef TARGET_ASM_INTEGER
3179 #define TARGET_ASM_INTEGER pru_assemble_integer
3180 #undef TARGET_SECTION_TYPE_FLAGS
3181 #define TARGET_SECTION_TYPE_FLAGS pru_section_type_flags
3182
3183 #undef TARGET_ASM_FILE_START
3184 #define TARGET_ASM_FILE_START pru_file_start
3185
3186 #undef TARGET_INSERT_ATTRIBUTES
3187 #define TARGET_INSERT_ATTRIBUTES pru_insert_attributes
3188
3189 #undef TARGET_INIT_BUILTINS
3190 #define TARGET_INIT_BUILTINS pru_init_builtins
3191 #undef TARGET_EXPAND_BUILTIN
3192 #define TARGET_EXPAND_BUILTIN pru_expand_builtin
3193 #undef TARGET_BUILTIN_DECL
3194 #define TARGET_BUILTIN_DECL pru_builtin_decl
3195
3196 #undef TARGET_COMPUTE_FRAME_LAYOUT
3197 #define TARGET_COMPUTE_FRAME_LAYOUT pru_compute_frame_layout
3198
3199 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
3200 #define TARGET_FUNCTION_OK_FOR_SIBCALL hook_bool_tree_tree_true
3201
3202 #undef TARGET_CAN_ELIMINATE
3203 #define TARGET_CAN_ELIMINATE pru_can_eliminate
3204
3205 #undef TARGET_CLASS_LIKELY_SPILLED_P
3206 #define TARGET_CLASS_LIKELY_SPILLED_P pru_class_likely_spilled_p
3207
3208 #undef TARGET_HARD_REGNO_MODE_OK
3209 #define TARGET_HARD_REGNO_MODE_OK pru_hard_regno_mode_ok
3210
3211 #undef TARGET_HARD_REGNO_SCRATCH_OK
3212 #define TARGET_HARD_REGNO_SCRATCH_OK pru_hard_regno_scratch_ok
3213
3214 #undef TARGET_FUNCTION_ARG
3215 #define TARGET_FUNCTION_ARG pru_function_arg
3216
3217 #undef TARGET_FUNCTION_ARG_ADVANCE
3218 #define TARGET_FUNCTION_ARG_ADVANCE pru_function_arg_advance
3219
3220 #undef TARGET_ARG_PARTIAL_BYTES
3221 #define TARGET_ARG_PARTIAL_BYTES pru_arg_partial_bytes
3222
3223 #undef TARGET_FUNCTION_VALUE
3224 #define TARGET_FUNCTION_VALUE pru_function_value
3225
3226 #undef TARGET_LIBCALL_VALUE
3227 #define TARGET_LIBCALL_VALUE pru_libcall_value
3228
3229 #undef TARGET_FUNCTION_VALUE_REGNO_P
3230 #define TARGET_FUNCTION_VALUE_REGNO_P pru_function_value_regno_p
3231
3232 #undef TARGET_RETURN_IN_MEMORY
3233 #define TARGET_RETURN_IN_MEMORY pru_return_in_memory
3234
3235 #undef TARGET_MUST_PASS_IN_STACK
3236 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
3237
3238 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
3239 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P \
3240 pru_addr_space_legitimate_address_p
3241
3242 #undef TARGET_INIT_LIBFUNCS
3243 #define TARGET_INIT_LIBFUNCS pru_init_libfuncs
3244 #undef TARGET_LIBFUNC_GNU_PREFIX
3245 #define TARGET_LIBFUNC_GNU_PREFIX true
3246
3247 #undef TARGET_RTX_COSTS
3248 #define TARGET_RTX_COSTS pru_rtx_costs
3249
3250 #undef TARGET_ADDRESS_COST
3251 #define TARGET_ADDRESS_COST pru_address_cost
3252
3253 #undef TARGET_INSN_COST
3254 #define TARGET_INSN_COST pru_insn_cost
3255
3256 #undef TARGET_PRINT_OPERAND
3257 #define TARGET_PRINT_OPERAND pru_print_operand
3258
3259 #undef TARGET_PRINT_OPERAND_ADDRESS
3260 #define TARGET_PRINT_OPERAND_ADDRESS pru_print_operand_address
3261
3262 #undef TARGET_MIN_ANCHOR_OFFSET
3263 #define TARGET_MIN_ANCHOR_OFFSET 0
3264
3265 #undef TARGET_MAX_ANCHOR_OFFSET
3266 #define TARGET_MAX_ANCHOR_OFFSET 255
3267
3268 #undef TARGET_OPTION_OVERRIDE
3269 #define TARGET_OPTION_OVERRIDE pru_option_override
3270
3271 #undef TARGET_SET_CURRENT_FUNCTION
3272 #define TARGET_SET_CURRENT_FUNCTION pru_set_current_function
3273
3274 #undef TARGET_MACHINE_DEPENDENT_REORG
3275 #define TARGET_MACHINE_DEPENDENT_REORG pru_reorg
3276
3277 #undef TARGET_CAN_USE_DOLOOP_P
3278 #define TARGET_CAN_USE_DOLOOP_P pru_can_use_doloop_p
3279
3280 #undef TARGET_INVALID_WITHIN_DOLOOP
3281 #define TARGET_INVALID_WITHIN_DOLOOP pru_invalid_within_doloop
3282
3283 #undef TARGET_UNWIND_WORD_MODE
3284 #define TARGET_UNWIND_WORD_MODE pru_unwind_word_mode
3285
3286 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
3287 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
3288
3289 struct gcc_target targetm = TARGET_INITIALIZER;
3290
3291 #include "gt-pru.h"