[AArch64] Improve scheduling model for X-Gene
[official-gcc.git] / gcc / config / mn10300 / mn10300.c
blob8351a5ef9595d860f611019156f0bae2d98bf851
1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2 Copyright (C) 1996-2017 Free Software Foundation, Inc.
3 Contributed by Jeff Law (law@cygnus.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "attribs.h"
30 #include "cfghooks.h"
31 #include "cfgloop.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 "alias.h"
41 #include "stor-layout.h"
42 #include "varasm.h"
43 #include "calls.h"
44 #include "output.h"
45 #include "insn-attr.h"
46 #include "reload.h"
47 #include "explow.h"
48 #include "expr.h"
49 #include "tm-constrs.h"
50 #include "cfgrtl.h"
51 #include "dumpfile.h"
52 #include "builtins.h"
54 /* This file should be included last. */
55 #include "target-def.h"
57 /* This is used in the am33_2.0-linux-gnu port, in which global symbol
58 names are not prefixed by underscores, to tell whether to prefix a
59 label with a plus sign or not, so that the assembler can tell
60 symbol names from register names. */
61 int mn10300_protect_label;
63 /* Selected processor type for tuning. */
64 enum processor_type mn10300_tune_cpu = PROCESSOR_DEFAULT;
66 #define CC_FLAG_Z 1
67 #define CC_FLAG_N 2
68 #define CC_FLAG_C 4
69 #define CC_FLAG_V 8
71 static int cc_flags_for_mode(machine_mode);
72 static int cc_flags_for_code(enum rtx_code);
74 /* Implement TARGET_OPTION_OVERRIDE. */
75 static void
76 mn10300_option_override (void)
78 if (TARGET_AM33)
79 target_flags &= ~MASK_MULT_BUG;
80 else
82 /* Disable scheduling for the MN10300 as we do
83 not have timing information available for it. */
84 flag_schedule_insns = 0;
85 flag_schedule_insns_after_reload = 0;
87 /* Force enable splitting of wide types, as otherwise it is trivial
88 to run out of registers. Indeed, this works so well that register
89 allocation problems are now more common *without* optimization,
90 when this flag is not enabled by default. */
91 flag_split_wide_types = 1;
94 if (mn10300_tune_string)
96 if (strcasecmp (mn10300_tune_string, "mn10300") == 0)
97 mn10300_tune_cpu = PROCESSOR_MN10300;
98 else if (strcasecmp (mn10300_tune_string, "am33") == 0)
99 mn10300_tune_cpu = PROCESSOR_AM33;
100 else if (strcasecmp (mn10300_tune_string, "am33-2") == 0)
101 mn10300_tune_cpu = PROCESSOR_AM33_2;
102 else if (strcasecmp (mn10300_tune_string, "am34") == 0)
103 mn10300_tune_cpu = PROCESSOR_AM34;
104 else
105 error ("-mtune= expects mn10300, am33, am33-2, or am34");
109 static void
110 mn10300_file_start (void)
112 default_file_start ();
114 if (TARGET_AM33_2)
115 fprintf (asm_out_file, "\t.am33_2\n");
116 else if (TARGET_AM33)
117 fprintf (asm_out_file, "\t.am33\n");
120 /* Note: This list must match the liw_op attribute in mn10300.md. */
122 static const char *liw_op_names[] =
124 "add", "cmp", "sub", "mov",
125 "and", "or", "xor",
126 "asr", "lsr", "asl",
127 "none", "max"
130 /* Print operand X using operand code CODE to assembly language output file
131 FILE. */
133 void
134 mn10300_print_operand (FILE *file, rtx x, int code)
136 switch (code)
138 case 'W':
140 unsigned int liw_op = UINTVAL (x);
142 gcc_assert (TARGET_ALLOW_LIW);
143 gcc_assert (liw_op < LIW_OP_MAX);
144 fputs (liw_op_names[liw_op], file);
145 break;
148 case 'b':
149 case 'B':
151 enum rtx_code cmp = GET_CODE (x);
152 machine_mode mode = GET_MODE (XEXP (x, 0));
153 const char *str;
154 int have_flags;
156 if (code == 'B')
157 cmp = reverse_condition (cmp);
158 have_flags = cc_flags_for_mode (mode);
160 switch (cmp)
162 case NE:
163 str = "ne";
164 break;
165 case EQ:
166 str = "eq";
167 break;
168 case GE:
169 /* bge is smaller than bnc. */
170 str = (have_flags & CC_FLAG_V ? "ge" : "nc");
171 break;
172 case LT:
173 str = (have_flags & CC_FLAG_V ? "lt" : "ns");
174 break;
175 case GT:
176 str = "gt";
177 break;
178 case LE:
179 str = "le";
180 break;
181 case GEU:
182 str = "cc";
183 break;
184 case GTU:
185 str = "hi";
186 break;
187 case LEU:
188 str = "ls";
189 break;
190 case LTU:
191 str = "cs";
192 break;
193 case ORDERED:
194 str = "lge";
195 break;
196 case UNORDERED:
197 str = "uo";
198 break;
199 case LTGT:
200 str = "lg";
201 break;
202 case UNEQ:
203 str = "ue";
204 break;
205 case UNGE:
206 str = "uge";
207 break;
208 case UNGT:
209 str = "ug";
210 break;
211 case UNLE:
212 str = "ule";
213 break;
214 case UNLT:
215 str = "ul";
216 break;
217 default:
218 gcc_unreachable ();
221 gcc_checking_assert ((cc_flags_for_code (cmp) & ~have_flags) == 0);
222 fputs (str, file);
224 break;
226 case 'C':
227 /* This is used for the operand to a call instruction;
228 if it's a REG, enclose it in parens, else output
229 the operand normally. */
230 if (REG_P (x))
232 fputc ('(', file);
233 mn10300_print_operand (file, x, 0);
234 fputc (')', file);
236 else
237 mn10300_print_operand (file, x, 0);
238 break;
240 case 'D':
241 switch (GET_CODE (x))
243 case MEM:
244 fputc ('(', file);
245 output_address (GET_MODE (x), XEXP (x, 0));
246 fputc (')', file);
247 break;
249 case REG:
250 fprintf (file, "fd%d", REGNO (x) - 18);
251 break;
253 default:
254 gcc_unreachable ();
256 break;
258 /* These are the least significant word in a 64bit value. */
259 case 'L':
260 switch (GET_CODE (x))
262 case MEM:
263 fputc ('(', file);
264 output_address (GET_MODE (x), XEXP (x, 0));
265 fputc (')', file);
266 break;
268 case REG:
269 fprintf (file, "%s", reg_names[REGNO (x)]);
270 break;
272 case SUBREG:
273 fprintf (file, "%s", reg_names[subreg_regno (x)]);
274 break;
276 case CONST_DOUBLE:
278 long val[2];
280 switch (GET_MODE (x))
282 case E_DFmode:
283 REAL_VALUE_TO_TARGET_DOUBLE
284 (*CONST_DOUBLE_REAL_VALUE (x), val);
285 fprintf (file, "0x%lx", val[0]);
286 break;;
287 case E_SFmode:
288 REAL_VALUE_TO_TARGET_SINGLE
289 (*CONST_DOUBLE_REAL_VALUE (x), val[0]);
290 fprintf (file, "0x%lx", val[0]);
291 break;;
292 case E_VOIDmode:
293 case E_DImode:
294 mn10300_print_operand_address (file,
295 GEN_INT (CONST_DOUBLE_LOW (x)));
296 break;
297 default:
298 break;
300 break;
303 case CONST_INT:
305 rtx low, high;
306 split_double (x, &low, &high);
307 fprintf (file, "%ld", (long)INTVAL (low));
308 break;
311 default:
312 gcc_unreachable ();
314 break;
316 /* Similarly, but for the most significant word. */
317 case 'H':
318 switch (GET_CODE (x))
320 case MEM:
321 fputc ('(', file);
322 x = adjust_address (x, SImode, 4);
323 output_address (GET_MODE (x), XEXP (x, 0));
324 fputc (')', file);
325 break;
327 case REG:
328 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
329 break;
331 case SUBREG:
332 fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
333 break;
335 case CONST_DOUBLE:
337 long val[2];
339 switch (GET_MODE (x))
341 case E_DFmode:
342 REAL_VALUE_TO_TARGET_DOUBLE
343 (*CONST_DOUBLE_REAL_VALUE (x), val);
344 fprintf (file, "0x%lx", val[1]);
345 break;;
346 case E_SFmode:
347 gcc_unreachable ();
348 case E_VOIDmode:
349 case E_DImode:
350 mn10300_print_operand_address (file,
351 GEN_INT (CONST_DOUBLE_HIGH (x)));
352 break;
353 default:
354 break;
356 break;
359 case CONST_INT:
361 rtx low, high;
362 split_double (x, &low, &high);
363 fprintf (file, "%ld", (long)INTVAL (high));
364 break;
367 default:
368 gcc_unreachable ();
370 break;
372 case 'A':
373 fputc ('(', file);
374 if (REG_P (XEXP (x, 0)))
375 output_address (VOIDmode, gen_rtx_PLUS (SImode,
376 XEXP (x, 0), const0_rtx));
377 else
378 output_address (VOIDmode, XEXP (x, 0));
379 fputc (')', file);
380 break;
382 case 'N':
383 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
384 fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
385 break;
387 case 'U':
388 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
389 fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
390 break;
392 /* For shift counts. The hardware ignores the upper bits of
393 any immediate, but the assembler will flag an out of range
394 shift count as an error. So we mask off the high bits
395 of the immediate here. */
396 case 'S':
397 if (CONST_INT_P (x))
399 fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
400 break;
402 /* FALL THROUGH */
404 default:
405 switch (GET_CODE (x))
407 case MEM:
408 fputc ('(', file);
409 output_address (GET_MODE (x), XEXP (x, 0));
410 fputc (')', file);
411 break;
413 case PLUS:
414 output_address (VOIDmode, x);
415 break;
417 case REG:
418 fprintf (file, "%s", reg_names[REGNO (x)]);
419 break;
421 case SUBREG:
422 fprintf (file, "%s", reg_names[subreg_regno (x)]);
423 break;
425 /* This will only be single precision.... */
426 case CONST_DOUBLE:
428 unsigned long val;
430 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
431 fprintf (file, "0x%lx", val);
432 break;
435 case CONST_INT:
436 case SYMBOL_REF:
437 case CONST:
438 case LABEL_REF:
439 case CODE_LABEL:
440 case UNSPEC:
441 mn10300_print_operand_address (file, x);
442 break;
443 default:
444 gcc_unreachable ();
446 break;
450 /* Output assembly language output for the address ADDR to FILE. */
452 void
453 mn10300_print_operand_address (FILE *file, rtx addr)
455 switch (GET_CODE (addr))
457 case POST_INC:
458 mn10300_print_operand (file, XEXP (addr, 0), 0);
459 fputc ('+', file);
460 break;
462 case POST_MODIFY:
463 mn10300_print_operand (file, XEXP (addr, 0), 0);
464 fputc ('+', file);
465 fputc (',', file);
466 mn10300_print_operand (file, XEXP (addr, 1), 0);
467 break;
469 case REG:
470 mn10300_print_operand (file, addr, 0);
471 break;
472 case PLUS:
474 rtx base = XEXP (addr, 0);
475 rtx index = XEXP (addr, 1);
477 if (REG_P (index) && !REG_OK_FOR_INDEX_P (index))
479 rtx x = base;
480 base = index;
481 index = x;
483 gcc_assert (REG_P (index) && REG_OK_FOR_INDEX_P (index));
485 gcc_assert (REG_OK_FOR_BASE_P (base));
487 mn10300_print_operand (file, index, 0);
488 fputc (',', file);
489 mn10300_print_operand (file, base, 0);
490 break;
492 case SYMBOL_REF:
493 output_addr_const (file, addr);
494 break;
495 default:
496 output_addr_const (file, addr);
497 break;
501 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.
503 Used for PIC-specific UNSPECs. */
505 static bool
506 mn10300_asm_output_addr_const_extra (FILE *file, rtx x)
508 if (GET_CODE (x) == UNSPEC)
510 switch (XINT (x, 1))
512 case UNSPEC_PIC:
513 /* GLOBAL_OFFSET_TABLE or local symbols, no suffix. */
514 output_addr_const (file, XVECEXP (x, 0, 0));
515 break;
516 case UNSPEC_GOT:
517 output_addr_const (file, XVECEXP (x, 0, 0));
518 fputs ("@GOT", file);
519 break;
520 case UNSPEC_GOTOFF:
521 output_addr_const (file, XVECEXP (x, 0, 0));
522 fputs ("@GOTOFF", file);
523 break;
524 case UNSPEC_PLT:
525 output_addr_const (file, XVECEXP (x, 0, 0));
526 fputs ("@PLT", file);
527 break;
528 case UNSPEC_GOTSYM_OFF:
529 assemble_name (file, GOT_SYMBOL_NAME);
530 fputs ("-(", file);
531 output_addr_const (file, XVECEXP (x, 0, 0));
532 fputs ("-.)", file);
533 break;
534 default:
535 return false;
537 return true;
539 else
540 return false;
543 /* Count the number of FP registers that have to be saved. */
544 static int
545 fp_regs_to_save (void)
547 int i, n = 0;
549 if (! TARGET_AM33_2)
550 return 0;
552 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
553 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
554 ++n;
556 return n;
559 /* Print a set of registers in the format required by "movm" and "ret".
560 Register K is saved if bit K of MASK is set. The data and address
561 registers can be stored individually, but the extended registers cannot.
562 We assume that the mask already takes that into account. For instance,
563 bits 14 to 17 must have the same value. */
565 void
566 mn10300_print_reg_list (FILE *file, int mask)
568 int need_comma;
569 int i;
571 need_comma = 0;
572 fputc ('[', file);
574 for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
575 if ((mask & (1 << i)) != 0)
577 if (need_comma)
578 fputc (',', file);
579 fputs (reg_names [i], file);
580 need_comma = 1;
583 if ((mask & 0x3c000) != 0)
585 gcc_assert ((mask & 0x3c000) == 0x3c000);
586 if (need_comma)
587 fputc (',', file);
588 fputs ("exreg1", file);
589 need_comma = 1;
592 fputc (']', file);
595 /* If the MDR register is never clobbered, we can use the RETF instruction
596 which takes the address from the MDR register. This is 3 cycles faster
597 than having to load the address from the stack. */
599 bool
600 mn10300_can_use_retf_insn (void)
602 /* Don't bother if we're not optimizing. In this case we won't
603 have proper access to df_regs_ever_live_p. */
604 if (!optimize)
605 return false;
607 /* EH returns alter the saved return address; MDR is not current. */
608 if (crtl->calls_eh_return)
609 return false;
611 /* Obviously not if MDR is ever clobbered. */
612 if (df_regs_ever_live_p (MDR_REG))
613 return false;
615 /* ??? Careful not to use this during expand_epilogue etc. */
616 gcc_assert (!in_sequence_p ());
617 return leaf_function_p ();
620 bool
621 mn10300_can_use_rets_insn (void)
623 return !mn10300_initial_offset (ARG_POINTER_REGNUM, STACK_POINTER_REGNUM);
626 /* Returns the set of live, callee-saved registers as a bitmask. The
627 callee-saved extended registers cannot be stored individually, so
628 all of them will be included in the mask if any one of them is used.
629 Also returns the number of bytes in the registers in the mask if
630 BYTES_SAVED is not NULL. */
632 unsigned int
633 mn10300_get_live_callee_saved_regs (unsigned int * bytes_saved)
635 int mask;
636 int i;
637 unsigned int count;
639 count = mask = 0;
640 for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
641 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
643 mask |= (1 << i);
644 ++ count;
647 if ((mask & 0x3c000) != 0)
649 for (i = 0x04000; i < 0x40000; i <<= 1)
650 if ((mask & i) == 0)
651 ++ count;
653 mask |= 0x3c000;
656 if (bytes_saved)
657 * bytes_saved = count * UNITS_PER_WORD;
659 return mask;
662 static rtx
663 F (rtx r)
665 RTX_FRAME_RELATED_P (r) = 1;
666 return r;
669 /* Generate an instruction that pushes several registers onto the stack.
670 Register K will be saved if bit K in MASK is set. The function does
671 nothing if MASK is zero.
673 To be compatible with the "movm" instruction, the lowest-numbered
674 register must be stored in the lowest slot. If MASK is the set
675 { R1,...,RN }, where R1...RN are ordered least first, the generated
676 instruction will have the form:
678 (parallel
679 (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
680 (set (mem:SI (plus:SI (reg:SI 9)
681 (const_int -1*4)))
682 (reg:SI RN))
684 (set (mem:SI (plus:SI (reg:SI 9)
685 (const_int -N*4)))
686 (reg:SI R1))) */
688 static void
689 mn10300_gen_multiple_store (unsigned int mask)
691 /* The order in which registers are stored, from SP-4 through SP-N*4. */
692 static const unsigned int store_order[8] = {
693 /* e2, e3: never saved */
694 FIRST_EXTENDED_REGNUM + 4,
695 FIRST_EXTENDED_REGNUM + 5,
696 FIRST_EXTENDED_REGNUM + 6,
697 FIRST_EXTENDED_REGNUM + 7,
698 /* e0, e1, mdrq, mcrh, mcrl, mcvf: never saved. */
699 FIRST_DATA_REGNUM + 2,
700 FIRST_DATA_REGNUM + 3,
701 FIRST_ADDRESS_REGNUM + 2,
702 FIRST_ADDRESS_REGNUM + 3,
703 /* d0, d1, a0, a1, mdr, lir, lar: never saved. */
706 rtx x, elts[9];
707 unsigned int i;
708 int count;
710 if (mask == 0)
711 return;
713 for (i = count = 0; i < ARRAY_SIZE(store_order); ++i)
715 unsigned regno = store_order[i];
717 if (((mask >> regno) & 1) == 0)
718 continue;
720 ++count;
721 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
722 x = gen_frame_mem (SImode, x);
723 x = gen_rtx_SET (x, gen_rtx_REG (SImode, regno));
724 elts[count] = F(x);
726 /* Remove the register from the mask so that... */
727 mask &= ~(1u << regno);
730 /* ... we can make sure that we didn't try to use a register
731 not listed in the store order. */
732 gcc_assert (mask == 0);
734 /* Create the instruction that updates the stack pointer. */
735 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
736 x = gen_rtx_SET (stack_pointer_rtx, x);
737 elts[0] = F(x);
739 /* We need one PARALLEL element to update the stack pointer and
740 an additional element for each register that is stored. */
741 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (count + 1, elts));
742 F (emit_insn (x));
745 static inline unsigned int
746 popcount (unsigned int mask)
748 unsigned int count = 0;
750 while (mask)
752 ++ count;
753 mask &= ~ (mask & - mask);
755 return count;
758 void
759 mn10300_expand_prologue (void)
761 HOST_WIDE_INT size = mn10300_frame_size ();
762 unsigned int mask;
764 mask = mn10300_get_live_callee_saved_regs (NULL);
765 /* If we use any of the callee-saved registers, save them now. */
766 mn10300_gen_multiple_store (mask);
768 if (flag_stack_usage_info)
769 current_function_static_stack_size = size + popcount (mask) * 4;
771 if (TARGET_AM33_2 && fp_regs_to_save ())
773 int num_regs_to_save = fp_regs_to_save (), i;
774 HOST_WIDE_INT xsize;
775 enum
777 save_sp_merge,
778 save_sp_no_merge,
779 save_sp_partial_merge,
780 save_a0_merge,
781 save_a0_no_merge
782 } strategy;
783 unsigned int strategy_size = (unsigned)-1, this_strategy_size;
784 rtx reg;
786 if (flag_stack_usage_info)
787 current_function_static_stack_size += num_regs_to_save * 4;
789 /* We have several different strategies to save FP registers.
790 We can store them using SP offsets, which is beneficial if
791 there are just a few registers to save, or we can use `a0' in
792 post-increment mode (`a0' is the only call-clobbered address
793 register that is never used to pass information to a
794 function). Furthermore, if we don't need a frame pointer, we
795 can merge the two SP adds into a single one, but this isn't
796 always beneficial; sometimes we can just split the two adds
797 so that we don't exceed a 16-bit constant size. The code
798 below will select which strategy to use, so as to generate
799 smallest code. Ties are broken in favor or shorter sequences
800 (in terms of number of instructions). */
802 #define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
803 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
804 #define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
805 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
807 /* We add 0 * (S) in two places to promote to the type of S,
808 so that all arms of the conditional have the same type. */
809 #define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
810 (((S) >= (L)) ? 0 * (S) + (SIZE1) * (N) \
811 : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
812 + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
813 : 0 * (S) + (ELSE))
814 #define SIZE_FMOV_SP_(S,N) \
815 (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
816 SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
817 (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
818 #define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
820 /* Consider alternative save_sp_merge only if we don't need the
821 frame pointer and size is nonzero. */
822 if (! frame_pointer_needed && size)
824 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
825 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
826 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
827 this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
829 if (this_strategy_size < strategy_size)
831 strategy = save_sp_merge;
832 strategy_size = this_strategy_size;
836 /* Consider alternative save_sp_no_merge unconditionally. */
837 /* Insn: add -4 * num_regs_to_save, sp. */
838 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
839 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
840 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
841 if (size)
843 /* Insn: add -size, sp. */
844 this_strategy_size += SIZE_ADD_SP (-size);
847 if (this_strategy_size < strategy_size)
849 strategy = save_sp_no_merge;
850 strategy_size = this_strategy_size;
853 /* Consider alternative save_sp_partial_merge only if we don't
854 need a frame pointer and size is reasonably large. */
855 if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
857 /* Insn: add -128, sp. */
858 this_strategy_size = SIZE_ADD_SP (-128);
859 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
860 this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
861 num_regs_to_save);
862 if (size)
864 /* Insn: add 128-size, sp. */
865 this_strategy_size += SIZE_ADD_SP (128 - size);
868 if (this_strategy_size < strategy_size)
870 strategy = save_sp_partial_merge;
871 strategy_size = this_strategy_size;
875 /* Consider alternative save_a0_merge only if we don't need a
876 frame pointer, size is nonzero and the user hasn't
877 changed the calling conventions of a0. */
878 if (! frame_pointer_needed && size
879 && call_really_used_regs [FIRST_ADDRESS_REGNUM]
880 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
882 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
883 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
884 /* Insn: mov sp, a0. */
885 this_strategy_size++;
886 if (size)
888 /* Insn: add size, a0. */
889 this_strategy_size += SIZE_ADD_AX (size);
891 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
892 this_strategy_size += 3 * num_regs_to_save;
894 if (this_strategy_size < strategy_size)
896 strategy = save_a0_merge;
897 strategy_size = this_strategy_size;
901 /* Consider alternative save_a0_no_merge if the user hasn't
902 changed the calling conventions of a0. */
903 if (call_really_used_regs [FIRST_ADDRESS_REGNUM]
904 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
906 /* Insn: add -4 * num_regs_to_save, sp. */
907 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
908 /* Insn: mov sp, a0. */
909 this_strategy_size++;
910 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
911 this_strategy_size += 3 * num_regs_to_save;
912 if (size)
914 /* Insn: add -size, sp. */
915 this_strategy_size += SIZE_ADD_SP (-size);
918 if (this_strategy_size < strategy_size)
920 strategy = save_a0_no_merge;
921 strategy_size = this_strategy_size;
925 /* Emit the initial SP add, common to all strategies. */
926 switch (strategy)
928 case save_sp_no_merge:
929 case save_a0_no_merge:
930 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
931 stack_pointer_rtx,
932 GEN_INT (-4 * num_regs_to_save))));
933 xsize = 0;
934 break;
936 case save_sp_partial_merge:
937 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
938 stack_pointer_rtx,
939 GEN_INT (-128))));
940 xsize = 128 - 4 * num_regs_to_save;
941 size -= xsize;
942 break;
944 case save_sp_merge:
945 case save_a0_merge:
946 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
947 stack_pointer_rtx,
948 GEN_INT (-(size + 4 * num_regs_to_save)))));
949 /* We'll have to adjust FP register saves according to the
950 frame size. */
951 xsize = size;
952 /* Since we've already created the stack frame, don't do it
953 again at the end of the function. */
954 size = 0;
955 break;
957 default:
958 gcc_unreachable ();
961 /* Now prepare register a0, if we have decided to use it. */
962 switch (strategy)
964 case save_sp_merge:
965 case save_sp_no_merge:
966 case save_sp_partial_merge:
967 reg = 0;
968 break;
970 case save_a0_merge:
971 case save_a0_no_merge:
972 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
973 F (emit_insn (gen_movsi (reg, stack_pointer_rtx)));
974 if (xsize)
975 F (emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize))));
976 reg = gen_rtx_POST_INC (SImode, reg);
977 break;
979 default:
980 gcc_unreachable ();
983 /* Now actually save the FP registers. */
984 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
985 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
987 rtx addr;
989 if (reg)
990 addr = reg;
991 else
993 /* If we aren't using `a0', use an SP offset. */
994 if (xsize)
996 addr = gen_rtx_PLUS (SImode,
997 stack_pointer_rtx,
998 GEN_INT (xsize));
1000 else
1001 addr = stack_pointer_rtx;
1003 xsize += 4;
1006 F (emit_insn (gen_movsf (gen_rtx_MEM (SFmode, addr),
1007 gen_rtx_REG (SFmode, i))));
1011 /* Now put the frame pointer into the frame pointer register. */
1012 if (frame_pointer_needed)
1013 F (emit_move_insn (frame_pointer_rtx, stack_pointer_rtx));
1015 /* Allocate stack for this frame. */
1016 if (size)
1017 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
1018 stack_pointer_rtx,
1019 GEN_INT (-size))));
1021 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1022 emit_insn (gen_load_pic ());
1025 void
1026 mn10300_expand_epilogue (void)
1028 HOST_WIDE_INT size = mn10300_frame_size ();
1029 unsigned int reg_save_bytes;
1031 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1033 if (TARGET_AM33_2 && fp_regs_to_save ())
1035 int num_regs_to_save = fp_regs_to_save (), i;
1036 rtx reg = 0;
1038 /* We have several options to restore FP registers. We could
1039 load them from SP offsets, but, if there are enough FP
1040 registers to restore, we win if we use a post-increment
1041 addressing mode. */
1043 /* If we have a frame pointer, it's the best option, because we
1044 already know it has the value we want. */
1045 if (frame_pointer_needed)
1046 reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
1047 /* Otherwise, we may use `a1', since it's call-clobbered and
1048 it's never used for return values. But only do so if it's
1049 smaller than using SP offsets. */
1050 else
1052 enum { restore_sp_post_adjust,
1053 restore_sp_pre_adjust,
1054 restore_sp_partial_adjust,
1055 restore_a1 } strategy;
1056 unsigned int this_strategy_size, strategy_size = (unsigned)-1;
1058 /* Consider using sp offsets before adjusting sp. */
1059 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1060 this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
1061 /* If size is too large, we'll have to adjust SP with an
1062 add. */
1063 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1065 /* Insn: add size + 4 * num_regs_to_save, sp. */
1066 this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
1068 /* If we don't have to restore any non-FP registers,
1069 we'll be able to save one byte by using rets. */
1070 if (! reg_save_bytes)
1071 this_strategy_size--;
1073 if (this_strategy_size < strategy_size)
1075 strategy = restore_sp_post_adjust;
1076 strategy_size = this_strategy_size;
1079 /* Consider using sp offsets after adjusting sp. */
1080 /* Insn: add size, sp. */
1081 this_strategy_size = SIZE_ADD_SP (size);
1082 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1083 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1084 /* We're going to use ret to release the FP registers
1085 save area, so, no savings. */
1087 if (this_strategy_size < strategy_size)
1089 strategy = restore_sp_pre_adjust;
1090 strategy_size = this_strategy_size;
1093 /* Consider using sp offsets after partially adjusting sp.
1094 When size is close to 32Kb, we may be able to adjust SP
1095 with an imm16 add instruction while still using fmov
1096 (d8,sp). */
1097 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1099 /* Insn: add size + 4 * num_regs_to_save
1100 + reg_save_bytes - 252,sp. */
1101 this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
1102 + (int) reg_save_bytes - 252);
1103 /* Insn: fmov (##,sp),fs#, fo each fs# to be restored. */
1104 this_strategy_size += SIZE_FMOV_SP (252 - reg_save_bytes
1105 - 4 * num_regs_to_save,
1106 num_regs_to_save);
1107 /* We're going to use ret to release the FP registers
1108 save area, so, no savings. */
1110 if (this_strategy_size < strategy_size)
1112 strategy = restore_sp_partial_adjust;
1113 strategy_size = this_strategy_size;
1117 /* Consider using a1 in post-increment mode, as long as the
1118 user hasn't changed the calling conventions of a1. */
1119 if (call_really_used_regs [FIRST_ADDRESS_REGNUM + 1]
1120 && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1122 /* Insn: mov sp,a1. */
1123 this_strategy_size = 1;
1124 if (size)
1126 /* Insn: add size,a1. */
1127 this_strategy_size += SIZE_ADD_AX (size);
1129 /* Insn: fmov (a1+),fs#, for each fs# to be restored. */
1130 this_strategy_size += 3 * num_regs_to_save;
1131 /* If size is large enough, we may be able to save a
1132 couple of bytes. */
1133 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1135 /* Insn: mov a1,sp. */
1136 this_strategy_size += 2;
1138 /* If we don't have to restore any non-FP registers,
1139 we'll be able to save one byte by using rets. */
1140 if (! reg_save_bytes)
1141 this_strategy_size--;
1143 if (this_strategy_size < strategy_size)
1145 strategy = restore_a1;
1146 strategy_size = this_strategy_size;
1150 switch (strategy)
1152 case restore_sp_post_adjust:
1153 break;
1155 case restore_sp_pre_adjust:
1156 emit_insn (gen_addsi3 (stack_pointer_rtx,
1157 stack_pointer_rtx,
1158 GEN_INT (size)));
1159 size = 0;
1160 break;
1162 case restore_sp_partial_adjust:
1163 emit_insn (gen_addsi3 (stack_pointer_rtx,
1164 stack_pointer_rtx,
1165 GEN_INT (size + 4 * num_regs_to_save
1166 + reg_save_bytes - 252)));
1167 size = 252 - reg_save_bytes - 4 * num_regs_to_save;
1168 break;
1170 case restore_a1:
1171 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1172 emit_insn (gen_movsi (reg, stack_pointer_rtx));
1173 if (size)
1174 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1175 break;
1177 default:
1178 gcc_unreachable ();
1182 /* Adjust the selected register, if any, for post-increment. */
1183 if (reg)
1184 reg = gen_rtx_POST_INC (SImode, reg);
1186 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1187 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
1189 rtx addr;
1191 if (reg)
1192 addr = reg;
1193 else if (size)
1195 /* If we aren't using a post-increment register, use an
1196 SP offset. */
1197 addr = gen_rtx_PLUS (SImode,
1198 stack_pointer_rtx,
1199 GEN_INT (size));
1201 else
1202 addr = stack_pointer_rtx;
1204 size += 4;
1206 emit_insn (gen_movsf (gen_rtx_REG (SFmode, i),
1207 gen_rtx_MEM (SFmode, addr)));
1210 /* If we were using the restore_a1 strategy and the number of
1211 bytes to be released won't fit in the `ret' byte, copy `a1'
1212 to `sp', to avoid having to use `add' to adjust it. */
1213 if (! frame_pointer_needed && reg && size + reg_save_bytes > 255)
1215 emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1216 size = 0;
1220 /* Maybe cut back the stack, except for the register save area.
1222 If the frame pointer exists, then use the frame pointer to
1223 cut back the stack.
1225 If the stack size + register save area is more than 255 bytes,
1226 then the stack must be cut back here since the size + register
1227 save size is too big for a ret/retf instruction.
1229 Else leave it alone, it will be cut back as part of the
1230 ret/retf instruction, or there wasn't any stack to begin with.
1232 Under no circumstances should the register save area be
1233 deallocated here, that would leave a window where an interrupt
1234 could occur and trash the register save area. */
1235 if (frame_pointer_needed)
1237 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1238 size = 0;
1240 else if (size + reg_save_bytes > 255)
1242 emit_insn (gen_addsi3 (stack_pointer_rtx,
1243 stack_pointer_rtx,
1244 GEN_INT (size)));
1245 size = 0;
1248 /* Adjust the stack and restore callee-saved registers, if any. */
1249 if (mn10300_can_use_rets_insn ())
1250 emit_jump_insn (ret_rtx);
1251 else
1252 emit_jump_insn (gen_return_ret (GEN_INT (size + reg_save_bytes)));
1255 /* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
1256 This function is for MATCH_PARALLEL and so assumes OP is known to be
1257 parallel. If OP is a multiple store, return a mask indicating which
1258 registers it saves. Return 0 otherwise. */
1260 unsigned int
1261 mn10300_store_multiple_regs (rtx op)
1263 int count;
1264 int mask;
1265 int i;
1266 unsigned int last;
1267 rtx elt;
1269 count = XVECLEN (op, 0);
1270 if (count < 2)
1271 return 0;
1273 /* Check that first instruction has the form (set (sp) (plus A B)) */
1274 elt = XVECEXP (op, 0, 0);
1275 if (GET_CODE (elt) != SET
1276 || (! REG_P (SET_DEST (elt)))
1277 || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1278 || GET_CODE (SET_SRC (elt)) != PLUS)
1279 return 0;
1281 /* Check that A is the stack pointer and B is the expected stack size.
1282 For OP to match, each subsequent instruction should push a word onto
1283 the stack. We therefore expect the first instruction to create
1284 COUNT-1 stack slots. */
1285 elt = SET_SRC (elt);
1286 if ((! REG_P (XEXP (elt, 0)))
1287 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1288 || (! CONST_INT_P (XEXP (elt, 1)))
1289 || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1290 return 0;
1292 mask = 0;
1293 for (i = 1; i < count; i++)
1295 /* Check that element i is a (set (mem M) R). */
1296 /* ??? Validate the register order a-la mn10300_gen_multiple_store.
1297 Remember: the ordering is *not* monotonic. */
1298 elt = XVECEXP (op, 0, i);
1299 if (GET_CODE (elt) != SET
1300 || (! MEM_P (SET_DEST (elt)))
1301 || (! REG_P (SET_SRC (elt))))
1302 return 0;
1304 /* Remember which registers are to be saved. */
1305 last = REGNO (SET_SRC (elt));
1306 mask |= (1 << last);
1308 /* Check that M has the form (plus (sp) (const_int -I*4)) */
1309 elt = XEXP (SET_DEST (elt), 0);
1310 if (GET_CODE (elt) != PLUS
1311 || (! REG_P (XEXP (elt, 0)))
1312 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1313 || (! CONST_INT_P (XEXP (elt, 1)))
1314 || INTVAL (XEXP (elt, 1)) != -i * 4)
1315 return 0;
1318 /* All or none of the callee-saved extended registers must be in the set. */
1319 if ((mask & 0x3c000) != 0
1320 && (mask & 0x3c000) != 0x3c000)
1321 return 0;
1323 return mask;
1326 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
1328 static reg_class_t
1329 mn10300_preferred_reload_class (rtx x, reg_class_t rclass)
1331 if (x == stack_pointer_rtx && rclass != SP_REGS)
1332 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1333 else if (MEM_P (x)
1334 || (REG_P (x)
1335 && !HARD_REGISTER_P (x))
1336 || (GET_CODE (x) == SUBREG
1337 && REG_P (SUBREG_REG (x))
1338 && !HARD_REGISTER_P (SUBREG_REG (x))))
1339 return LIMIT_RELOAD_CLASS (GET_MODE (x), rclass);
1340 else
1341 return rclass;
1344 /* Implement TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
1346 static reg_class_t
1347 mn10300_preferred_output_reload_class (rtx x, reg_class_t rclass)
1349 if (x == stack_pointer_rtx && rclass != SP_REGS)
1350 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1351 return rclass;
1354 /* Implement TARGET_SECONDARY_RELOAD. */
1356 static reg_class_t
1357 mn10300_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
1358 machine_mode mode, secondary_reload_info *sri)
1360 enum reg_class rclass = (enum reg_class) rclass_i;
1361 enum reg_class xclass = NO_REGS;
1362 unsigned int xregno = INVALID_REGNUM;
1364 if (REG_P (x))
1366 xregno = REGNO (x);
1367 if (xregno >= FIRST_PSEUDO_REGISTER)
1368 xregno = true_regnum (x);
1369 if (xregno != INVALID_REGNUM)
1370 xclass = REGNO_REG_CLASS (xregno);
1373 if (!TARGET_AM33)
1375 /* Memory load/stores less than a full word wide can't have an
1376 address or stack pointer destination. They must use a data
1377 register as an intermediate register. */
1378 if (rclass != DATA_REGS
1379 && (mode == QImode || mode == HImode)
1380 && xclass == NO_REGS)
1381 return DATA_REGS;
1383 /* We can only move SP to/from an address register. */
1384 if (in_p
1385 && rclass == SP_REGS
1386 && xclass != ADDRESS_REGS)
1387 return ADDRESS_REGS;
1388 if (!in_p
1389 && xclass == SP_REGS
1390 && rclass != ADDRESS_REGS
1391 && rclass != SP_OR_ADDRESS_REGS)
1392 return ADDRESS_REGS;
1395 /* We can't directly load sp + const_int into a register;
1396 we must use an address register as an scratch. */
1397 if (in_p
1398 && rclass != SP_REGS
1399 && rclass != SP_OR_ADDRESS_REGS
1400 && rclass != SP_OR_GENERAL_REGS
1401 && GET_CODE (x) == PLUS
1402 && (XEXP (x, 0) == stack_pointer_rtx
1403 || XEXP (x, 1) == stack_pointer_rtx))
1405 sri->icode = CODE_FOR_reload_plus_sp_const;
1406 return NO_REGS;
1409 /* We can only move MDR to/from a data register. */
1410 if (rclass == MDR_REGS && xclass != DATA_REGS)
1411 return DATA_REGS;
1412 if (xclass == MDR_REGS && rclass != DATA_REGS)
1413 return DATA_REGS;
1415 /* We can't load/store an FP register from a constant address. */
1416 if (TARGET_AM33_2
1417 && (rclass == FP_REGS || xclass == FP_REGS)
1418 && (xclass == NO_REGS || rclass == NO_REGS))
1420 rtx addr = NULL;
1422 if (xregno >= FIRST_PSEUDO_REGISTER && xregno != INVALID_REGNUM)
1424 addr = reg_equiv_mem (xregno);
1425 if (addr)
1426 addr = XEXP (addr, 0);
1428 else if (MEM_P (x))
1429 addr = XEXP (x, 0);
1431 if (addr && CONSTANT_ADDRESS_P (addr))
1432 return GENERAL_REGS;
1434 /* Otherwise assume no secondary reloads are needed. */
1435 return NO_REGS;
1439 mn10300_frame_size (void)
1441 /* size includes the fixed stack space needed for function calls. */
1442 int size = get_frame_size () + crtl->outgoing_args_size;
1444 /* And space for the return pointer. */
1445 size += crtl->outgoing_args_size ? 4 : 0;
1447 return size;
1451 mn10300_initial_offset (int from, int to)
1453 int diff = 0;
1455 gcc_assert (from == ARG_POINTER_REGNUM || from == FRAME_POINTER_REGNUM);
1456 gcc_assert (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
1458 if (to == STACK_POINTER_REGNUM)
1459 diff = mn10300_frame_size ();
1461 /* The difference between the argument pointer and the frame pointer
1462 is the size of the callee register save area. */
1463 if (from == ARG_POINTER_REGNUM)
1465 unsigned int reg_save_bytes;
1467 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1468 diff += reg_save_bytes;
1469 diff += 4 * fp_regs_to_save ();
1472 return diff;
1475 /* Worker function for TARGET_RETURN_IN_MEMORY. */
1477 static bool
1478 mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1480 /* Return values > 8 bytes in length in memory. */
1481 return (int_size_in_bytes (type) > 8
1482 || int_size_in_bytes (type) == 0
1483 || TYPE_MODE (type) == BLKmode);
1486 /* Flush the argument registers to the stack for a stdarg function;
1487 return the new argument pointer. */
1488 static rtx
1489 mn10300_builtin_saveregs (void)
1491 rtx offset, mem;
1492 tree fntype = TREE_TYPE (current_function_decl);
1493 int argadj = ((!stdarg_p (fntype))
1494 ? UNITS_PER_WORD : 0);
1495 alias_set_type set = get_varargs_alias_set ();
1497 if (argadj)
1498 offset = plus_constant (Pmode, crtl->args.arg_offset_rtx, argadj);
1499 else
1500 offset = crtl->args.arg_offset_rtx;
1502 mem = gen_rtx_MEM (SImode, crtl->args.internal_arg_pointer);
1503 set_mem_alias_set (mem, set);
1504 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1506 mem = gen_rtx_MEM (SImode,
1507 plus_constant (Pmode,
1508 crtl->args.internal_arg_pointer, 4));
1509 set_mem_alias_set (mem, set);
1510 emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1512 return copy_to_reg (expand_binop (Pmode, add_optab,
1513 crtl->args.internal_arg_pointer,
1514 offset, 0, 0, OPTAB_LIB_WIDEN));
1517 static void
1518 mn10300_va_start (tree valist, rtx nextarg)
1520 nextarg = expand_builtin_saveregs ();
1521 std_expand_builtin_va_start (valist, nextarg);
1524 /* Return true when a parameter should be passed by reference. */
1526 static bool
1527 mn10300_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
1528 machine_mode mode, const_tree type,
1529 bool named ATTRIBUTE_UNUSED)
1531 unsigned HOST_WIDE_INT size;
1533 if (type)
1534 size = int_size_in_bytes (type);
1535 else
1536 size = GET_MODE_SIZE (mode);
1538 return (size > 8 || size == 0);
1541 /* Return an RTX to represent where a value with mode MODE will be returned
1542 from a function. If the result is NULL_RTX, the argument is pushed. */
1544 static rtx
1545 mn10300_function_arg (cumulative_args_t cum_v, machine_mode mode,
1546 const_tree type, bool named ATTRIBUTE_UNUSED)
1548 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1549 rtx result = NULL_RTX;
1550 int size;
1552 /* We only support using 2 data registers as argument registers. */
1553 int nregs = 2;
1555 /* Figure out the size of the object to be passed. */
1556 if (mode == BLKmode)
1557 size = int_size_in_bytes (type);
1558 else
1559 size = GET_MODE_SIZE (mode);
1561 cum->nbytes = (cum->nbytes + 3) & ~3;
1563 /* Don't pass this arg via a register if all the argument registers
1564 are used up. */
1565 if (cum->nbytes > nregs * UNITS_PER_WORD)
1566 return result;
1568 /* Don't pass this arg via a register if it would be split between
1569 registers and memory. */
1570 if (type == NULL_TREE
1571 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1572 return result;
1574 switch (cum->nbytes / UNITS_PER_WORD)
1576 case 0:
1577 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM);
1578 break;
1579 case 1:
1580 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM + 1);
1581 break;
1582 default:
1583 break;
1586 return result;
1589 /* Update the data in CUM to advance over an argument
1590 of mode MODE and data type TYPE.
1591 (TYPE is null for libcalls where that information may not be available.) */
1593 static void
1594 mn10300_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
1595 const_tree type, bool named ATTRIBUTE_UNUSED)
1597 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1599 cum->nbytes += (mode != BLKmode
1600 ? (GET_MODE_SIZE (mode) + 3) & ~3
1601 : (int_size_in_bytes (type) + 3) & ~3);
1604 /* Return the number of bytes of registers to use for an argument passed
1605 partially in registers and partially in memory. */
1607 static int
1608 mn10300_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
1609 tree type, bool named ATTRIBUTE_UNUSED)
1611 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1612 int size;
1614 /* We only support using 2 data registers as argument registers. */
1615 int nregs = 2;
1617 /* Figure out the size of the object to be passed. */
1618 if (mode == BLKmode)
1619 size = int_size_in_bytes (type);
1620 else
1621 size = GET_MODE_SIZE (mode);
1623 cum->nbytes = (cum->nbytes + 3) & ~3;
1625 /* Don't pass this arg via a register if all the argument registers
1626 are used up. */
1627 if (cum->nbytes > nregs * UNITS_PER_WORD)
1628 return 0;
1630 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1631 return 0;
1633 /* Don't pass this arg via a register if it would be split between
1634 registers and memory. */
1635 if (type == NULL_TREE
1636 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1637 return 0;
1639 return nregs * UNITS_PER_WORD - cum->nbytes;
1642 /* Return the location of the function's value. This will be either
1643 $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1644 $d0 and $a0 if the -mreturn-pointer-on-do flag is set. Note that
1645 we only return the PARALLEL for outgoing values; we do not want
1646 callers relying on this extra copy. */
1648 static rtx
1649 mn10300_function_value (const_tree valtype,
1650 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1651 bool outgoing)
1653 rtx rv;
1654 machine_mode mode = TYPE_MODE (valtype);
1656 if (! POINTER_TYPE_P (valtype))
1657 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1658 else if (! TARGET_PTR_A0D0 || ! outgoing
1659 || cfun->returns_struct)
1660 return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1662 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1663 XVECEXP (rv, 0, 0)
1664 = gen_rtx_EXPR_LIST (VOIDmode,
1665 gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1666 GEN_INT (0));
1668 XVECEXP (rv, 0, 1)
1669 = gen_rtx_EXPR_LIST (VOIDmode,
1670 gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1671 GEN_INT (0));
1672 return rv;
1675 /* Implements TARGET_LIBCALL_VALUE. */
1677 static rtx
1678 mn10300_libcall_value (machine_mode mode,
1679 const_rtx fun ATTRIBUTE_UNUSED)
1681 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1684 /* Implements FUNCTION_VALUE_REGNO_P. */
1686 bool
1687 mn10300_function_value_regno_p (const unsigned int regno)
1689 return (regno == FIRST_DATA_REGNUM || regno == FIRST_ADDRESS_REGNUM);
1692 /* Output an addition operation. */
1694 const char *
1695 mn10300_output_add (rtx operands[3], bool need_flags)
1697 rtx dest, src1, src2;
1698 unsigned int dest_regnum, src1_regnum, src2_regnum;
1699 enum reg_class src1_class, src2_class, dest_class;
1701 dest = operands[0];
1702 src1 = operands[1];
1703 src2 = operands[2];
1705 dest_regnum = true_regnum (dest);
1706 src1_regnum = true_regnum (src1);
1708 dest_class = REGNO_REG_CLASS (dest_regnum);
1709 src1_class = REGNO_REG_CLASS (src1_regnum);
1711 if (CONST_INT_P (src2))
1713 gcc_assert (dest_regnum == src1_regnum);
1715 if (src2 == const1_rtx && !need_flags)
1716 return "inc %0";
1717 if (INTVAL (src2) == 4 && !need_flags && dest_class != DATA_REGS)
1718 return "inc4 %0";
1720 gcc_assert (!need_flags || dest_class != SP_REGS);
1721 return "add %2,%0";
1723 else if (CONSTANT_P (src2))
1724 return "add %2,%0";
1726 src2_regnum = true_regnum (src2);
1727 src2_class = REGNO_REG_CLASS (src2_regnum);
1729 if (dest_regnum == src1_regnum)
1730 return "add %2,%0";
1731 if (dest_regnum == src2_regnum)
1732 return "add %1,%0";
1734 /* The rest of the cases are reg = reg+reg. For AM33, we can implement
1735 this directly, as below, but when optimizing for space we can sometimes
1736 do better by using a mov+add. For MN103, we claimed that we could
1737 implement a three-operand add because the various move and add insns
1738 change sizes across register classes, and we can often do better than
1739 reload in choosing which operand to move. */
1740 if (TARGET_AM33 && optimize_insn_for_speed_p ())
1741 return "add %2,%1,%0";
1743 /* Catch cases where no extended register was used. */
1744 if (src1_class != EXTENDED_REGS
1745 && src2_class != EXTENDED_REGS
1746 && dest_class != EXTENDED_REGS)
1748 /* We have to copy one of the sources into the destination, then
1749 add the other source to the destination.
1751 Carefully select which source to copy to the destination; a
1752 naive implementation will waste a byte when the source classes
1753 are different and the destination is an address register.
1754 Selecting the lowest cost register copy will optimize this
1755 sequence. */
1756 if (src1_class == dest_class)
1757 return "mov %1,%0\n\tadd %2,%0";
1758 else
1759 return "mov %2,%0\n\tadd %1,%0";
1762 /* At least one register is an extended register. */
1764 /* The three operand add instruction on the am33 is a win iff the
1765 output register is an extended register, or if both source
1766 registers are extended registers. */
1767 if (dest_class == EXTENDED_REGS || src1_class == src2_class)
1768 return "add %2,%1,%0";
1770 /* It is better to copy one of the sources to the destination, then
1771 perform a 2 address add. The destination in this case must be
1772 an address or data register and one of the sources must be an
1773 extended register and the remaining source must not be an extended
1774 register.
1776 The best code for this case is to copy the extended reg to the
1777 destination, then emit a two address add. */
1778 if (src1_class == EXTENDED_REGS)
1779 return "mov %1,%0\n\tadd %2,%0";
1780 else
1781 return "mov %2,%0\n\tadd %1,%0";
1784 /* Return 1 if X contains a symbolic expression. We know these
1785 expressions will have one of a few well defined forms, so
1786 we need only check those forms. */
1789 mn10300_symbolic_operand (rtx op,
1790 machine_mode mode ATTRIBUTE_UNUSED)
1792 switch (GET_CODE (op))
1794 case SYMBOL_REF:
1795 case LABEL_REF:
1796 return 1;
1797 case CONST:
1798 op = XEXP (op, 0);
1799 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1800 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1801 && CONST_INT_P (XEXP (op, 1)));
1802 default:
1803 return 0;
1807 /* Try machine dependent ways of modifying an illegitimate address
1808 to be legitimate. If we find one, return the new valid address.
1809 This macro is used in only one place: `memory_address' in explow.c.
1811 OLDX is the address as it was before break_out_memory_refs was called.
1812 In some cases it is useful to look at this to decide what needs to be done.
1814 Normally it is always safe for this macro to do nothing. It exists to
1815 recognize opportunities to optimize the output.
1817 But on a few ports with segmented architectures and indexed addressing
1818 (mn10300, hppa) it is used to rewrite certain problematical addresses. */
1820 static rtx
1821 mn10300_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1822 machine_mode mode ATTRIBUTE_UNUSED)
1824 if (flag_pic && ! mn10300_legitimate_pic_operand_p (x))
1825 x = mn10300_legitimize_pic_address (oldx, NULL_RTX);
1827 /* Uh-oh. We might have an address for x[n-100000]. This needs
1828 special handling to avoid creating an indexed memory address
1829 with x-100000 as the base. */
1830 if (GET_CODE (x) == PLUS
1831 && mn10300_symbolic_operand (XEXP (x, 1), VOIDmode))
1833 /* Ugly. We modify things here so that the address offset specified
1834 by the index expression is computed first, then added to x to form
1835 the entire address. */
1837 rtx regx1, regy1, regy2, y;
1839 /* Strip off any CONST. */
1840 y = XEXP (x, 1);
1841 if (GET_CODE (y) == CONST)
1842 y = XEXP (y, 0);
1844 if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1846 regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1847 regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1848 regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1849 regx1 = force_reg (Pmode,
1850 gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1,
1851 regy2));
1852 return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1855 return x;
1858 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
1859 @GOTOFF in `reg'. */
1862 mn10300_legitimize_pic_address (rtx orig, rtx reg)
1864 rtx x;
1865 rtx_insn *insn;
1867 if (GET_CODE (orig) == LABEL_REF
1868 || (GET_CODE (orig) == SYMBOL_REF
1869 && (CONSTANT_POOL_ADDRESS_P (orig)
1870 || ! MN10300_GLOBAL_P (orig))))
1872 if (reg == NULL)
1873 reg = gen_reg_rtx (Pmode);
1875 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOTOFF);
1876 x = gen_rtx_CONST (SImode, x);
1877 emit_move_insn (reg, x);
1879 insn = emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
1881 else if (GET_CODE (orig) == SYMBOL_REF)
1883 if (reg == NULL)
1884 reg = gen_reg_rtx (Pmode);
1886 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOT);
1887 x = gen_rtx_CONST (SImode, x);
1888 x = gen_rtx_PLUS (SImode, pic_offset_table_rtx, x);
1889 x = gen_const_mem (SImode, x);
1891 insn = emit_move_insn (reg, x);
1893 else
1894 return orig;
1896 set_unique_reg_note (insn, REG_EQUAL, orig);
1897 return reg;
1900 /* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
1901 isn't protected by a PIC unspec; nonzero otherwise. */
1904 mn10300_legitimate_pic_operand_p (rtx x)
1906 const char *fmt;
1907 int i;
1909 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1910 return 0;
1912 if (GET_CODE (x) == UNSPEC
1913 && (XINT (x, 1) == UNSPEC_PIC
1914 || XINT (x, 1) == UNSPEC_GOT
1915 || XINT (x, 1) == UNSPEC_GOTOFF
1916 || XINT (x, 1) == UNSPEC_PLT
1917 || XINT (x, 1) == UNSPEC_GOTSYM_OFF))
1918 return 1;
1920 fmt = GET_RTX_FORMAT (GET_CODE (x));
1921 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1923 if (fmt[i] == 'E')
1925 int j;
1927 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1928 if (! mn10300_legitimate_pic_operand_p (XVECEXP (x, i, j)))
1929 return 0;
1931 else if (fmt[i] == 'e'
1932 && ! mn10300_legitimate_pic_operand_p (XEXP (x, i)))
1933 return 0;
1936 return 1;
1939 /* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1940 legitimate, and FALSE otherwise.
1942 On the mn10300, the value in the address register must be
1943 in the same memory space/segment as the effective address.
1945 This is problematical for reload since it does not understand
1946 that base+index != index+base in a memory reference.
1948 Note it is still possible to use reg+reg addressing modes,
1949 it's just much more difficult. For a discussion of a possible
1950 workaround and solution, see the comments in pa.c before the
1951 function record_unscaled_index_insn_codes. */
1953 static bool
1954 mn10300_legitimate_address_p (machine_mode mode, rtx x, bool strict)
1956 rtx base, index;
1958 if (CONSTANT_ADDRESS_P (x))
1959 return !flag_pic || mn10300_legitimate_pic_operand_p (x);
1961 if (RTX_OK_FOR_BASE_P (x, strict))
1962 return true;
1964 if (TARGET_AM33 && (mode == SImode || mode == SFmode || mode == HImode))
1966 if (GET_CODE (x) == POST_INC)
1967 return RTX_OK_FOR_BASE_P (XEXP (x, 0), strict);
1968 if (GET_CODE (x) == POST_MODIFY)
1969 return (RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1970 && CONSTANT_ADDRESS_P (XEXP (x, 1)));
1973 if (GET_CODE (x) != PLUS)
1974 return false;
1976 base = XEXP (x, 0);
1977 index = XEXP (x, 1);
1979 if (!REG_P (base))
1980 return false;
1981 if (REG_P (index))
1983 /* ??? Without AM33 generalized (Ri,Rn) addressing, reg+reg
1984 addressing is hard to satisfy. */
1985 if (!TARGET_AM33)
1986 return false;
1988 return (REGNO_GENERAL_P (REGNO (base), strict)
1989 && REGNO_GENERAL_P (REGNO (index), strict));
1992 if (!REGNO_STRICT_OK_FOR_BASE_P (REGNO (base), strict))
1993 return false;
1995 if (CONST_INT_P (index))
1996 return IN_RANGE (INTVAL (index), -1 - 0x7fffffff, 0x7fffffff);
1998 if (CONSTANT_ADDRESS_P (index))
1999 return !flag_pic || mn10300_legitimate_pic_operand_p (index);
2001 return false;
2004 bool
2005 mn10300_regno_in_class_p (unsigned regno, int rclass, bool strict)
2007 if (regno >= FIRST_PSEUDO_REGISTER)
2009 if (!strict)
2010 return true;
2011 if (!reg_renumber)
2012 return false;
2013 regno = reg_renumber[regno];
2014 if (regno == INVALID_REGNUM)
2015 return false;
2017 return TEST_HARD_REG_BIT (reg_class_contents[rclass], regno);
2021 mn10300_legitimize_reload_address (rtx x,
2022 machine_mode mode ATTRIBUTE_UNUSED,
2023 int opnum, int type,
2024 int ind_levels ATTRIBUTE_UNUSED)
2026 bool any_change = false;
2028 /* See above re disabling reg+reg addressing for MN103. */
2029 if (!TARGET_AM33)
2030 return NULL_RTX;
2032 if (GET_CODE (x) != PLUS)
2033 return NULL_RTX;
2035 if (XEXP (x, 0) == stack_pointer_rtx)
2037 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2038 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2039 opnum, (enum reload_type) type);
2040 any_change = true;
2042 if (XEXP (x, 1) == stack_pointer_rtx)
2044 push_reload (XEXP (x, 1), NULL_RTX, &XEXP (x, 1), NULL,
2045 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2046 opnum, (enum reload_type) type);
2047 any_change = true;
2050 return any_change ? x : NULL_RTX;
2053 /* Implement TARGET_LEGITIMATE_CONSTANT_P. Returns TRUE if X is a valid
2054 constant. Note that some "constants" aren't valid, such as TLS
2055 symbols and unconverted GOT-based references, so we eliminate
2056 those here. */
2058 static bool
2059 mn10300_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2061 switch (GET_CODE (x))
2063 case CONST:
2064 x = XEXP (x, 0);
2066 if (GET_CODE (x) == PLUS)
2068 if (! CONST_INT_P (XEXP (x, 1)))
2069 return false;
2070 x = XEXP (x, 0);
2073 /* Only some unspecs are valid as "constants". */
2074 if (GET_CODE (x) == UNSPEC)
2076 switch (XINT (x, 1))
2078 case UNSPEC_PIC:
2079 case UNSPEC_GOT:
2080 case UNSPEC_GOTOFF:
2081 case UNSPEC_PLT:
2082 return true;
2083 default:
2084 return false;
2088 /* We must have drilled down to a symbol. */
2089 if (! mn10300_symbolic_operand (x, Pmode))
2090 return false;
2091 break;
2093 default:
2094 break;
2097 return true;
2100 /* Undo pic address legitimization for the benefit of debug info. */
2102 static rtx
2103 mn10300_delegitimize_address (rtx orig_x)
2105 rtx x = orig_x, ret, addend = NULL;
2106 bool need_mem;
2108 if (MEM_P (x))
2109 x = XEXP (x, 0);
2110 if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
2111 return orig_x;
2113 if (XEXP (x, 0) == pic_offset_table_rtx)
2115 /* With the REG+REG addressing of AM33, var-tracking can re-assemble
2116 some odd-looking "addresses" that were never valid in the first place.
2117 We need to look harder to avoid warnings being emitted. */
2118 else if (GET_CODE (XEXP (x, 0)) == PLUS)
2120 rtx x0 = XEXP (x, 0);
2121 rtx x00 = XEXP (x0, 0);
2122 rtx x01 = XEXP (x0, 1);
2124 if (x00 == pic_offset_table_rtx)
2125 addend = x01;
2126 else if (x01 == pic_offset_table_rtx)
2127 addend = x00;
2128 else
2129 return orig_x;
2132 else
2133 return orig_x;
2134 x = XEXP (x, 1);
2136 if (GET_CODE (x) != CONST)
2137 return orig_x;
2138 x = XEXP (x, 0);
2139 if (GET_CODE (x) != UNSPEC)
2140 return orig_x;
2142 ret = XVECEXP (x, 0, 0);
2143 if (XINT (x, 1) == UNSPEC_GOTOFF)
2144 need_mem = false;
2145 else if (XINT (x, 1) == UNSPEC_GOT)
2146 need_mem = true;
2147 else
2148 return orig_x;
2150 gcc_assert (GET_CODE (ret) == SYMBOL_REF);
2151 if (need_mem != MEM_P (orig_x))
2152 return orig_x;
2153 if (need_mem && addend)
2154 return orig_x;
2155 if (addend)
2156 ret = gen_rtx_PLUS (Pmode, addend, ret);
2157 return ret;
2160 /* For addresses, costs are relative to "MOV (Rm),Rn". For AM33 this is
2161 the 3-byte fully general instruction; for MN103 this is the 2-byte form
2162 with an address register. */
2164 static int
2165 mn10300_address_cost (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
2166 addr_space_t as ATTRIBUTE_UNUSED, bool speed)
2168 HOST_WIDE_INT i;
2169 rtx base, index;
2171 switch (GET_CODE (x))
2173 case CONST:
2174 case SYMBOL_REF:
2175 case LABEL_REF:
2176 /* We assume all of these require a 32-bit constant, even though
2177 some symbol and label references can be relaxed. */
2178 return speed ? 1 : 4;
2180 case REG:
2181 case SUBREG:
2182 case POST_INC:
2183 return 0;
2185 case POST_MODIFY:
2186 /* Assume any symbolic offset is a 32-bit constant. */
2187 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2188 if (IN_RANGE (i, -128, 127))
2189 return speed ? 0 : 1;
2190 if (speed)
2191 return 1;
2192 if (IN_RANGE (i, -0x800000, 0x7fffff))
2193 return 3;
2194 return 4;
2196 case PLUS:
2197 base = XEXP (x, 0);
2198 index = XEXP (x, 1);
2199 if (register_operand (index, SImode))
2201 /* Attempt to minimize the number of registers in the address.
2202 This is similar to what other ports do. */
2203 if (register_operand (base, SImode))
2204 return 1;
2206 base = XEXP (x, 1);
2207 index = XEXP (x, 0);
2210 /* Assume any symbolic offset is a 32-bit constant. */
2211 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2212 if (IN_RANGE (i, -128, 127))
2213 return speed ? 0 : 1;
2214 if (IN_RANGE (i, -32768, 32767))
2215 return speed ? 0 : 2;
2216 return speed ? 2 : 6;
2218 default:
2219 return rtx_cost (x, Pmode, MEM, 0, speed);
2223 /* Implement the TARGET_REGISTER_MOVE_COST hook.
2225 Recall that the base value of 2 is required by assumptions elsewhere
2226 in the body of the compiler, and that cost 2 is special-cased as an
2227 early exit from reload meaning no work is required. */
2229 static int
2230 mn10300_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2231 reg_class_t ifrom, reg_class_t ito)
2233 enum reg_class from = (enum reg_class) ifrom;
2234 enum reg_class to = (enum reg_class) ito;
2235 enum reg_class scratch, test;
2237 /* Simplify the following code by unifying the fp register classes. */
2238 if (to == FP_ACC_REGS)
2239 to = FP_REGS;
2240 if (from == FP_ACC_REGS)
2241 from = FP_REGS;
2243 /* Diagnose invalid moves by costing them as two moves. */
2245 scratch = NO_REGS;
2246 test = from;
2247 if (to == SP_REGS)
2248 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2249 else if (to == MDR_REGS)
2250 scratch = DATA_REGS;
2251 else if (to == FP_REGS && to != from)
2252 scratch = GENERAL_REGS;
2253 else
2255 test = to;
2256 if (from == SP_REGS)
2257 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2258 else if (from == MDR_REGS)
2259 scratch = DATA_REGS;
2260 else if (from == FP_REGS && to != from)
2261 scratch = GENERAL_REGS;
2263 if (scratch != NO_REGS && !reg_class_subset_p (test, scratch))
2264 return (mn10300_register_move_cost (VOIDmode, from, scratch)
2265 + mn10300_register_move_cost (VOIDmode, scratch, to));
2267 /* From here on, all we need consider are legal combinations. */
2269 if (optimize_size)
2271 /* The scale here is bytes * 2. */
2273 if (from == to && (to == ADDRESS_REGS || to == DATA_REGS))
2274 return 2;
2276 if (from == SP_REGS)
2277 return (to == ADDRESS_REGS ? 2 : 6);
2279 /* For MN103, all remaining legal moves are two bytes. */
2280 if (TARGET_AM33)
2281 return 4;
2283 if (to == SP_REGS)
2284 return (from == ADDRESS_REGS ? 4 : 6);
2286 if ((from == ADDRESS_REGS || from == DATA_REGS)
2287 && (to == ADDRESS_REGS || to == DATA_REGS))
2288 return 4;
2290 if (to == EXTENDED_REGS)
2291 return (to == from ? 6 : 4);
2293 /* What's left are SP_REGS, FP_REGS, or combinations of the above. */
2294 return 6;
2296 else
2298 /* The scale here is cycles * 2. */
2300 if (to == FP_REGS)
2301 return 8;
2302 if (from == FP_REGS)
2303 return 4;
2305 /* All legal moves between integral registers are single cycle. */
2306 return 2;
2310 /* Implement the TARGET_MEMORY_MOVE_COST hook.
2312 Given lack of the form of the address, this must be speed-relative,
2313 though we should never be less expensive than a size-relative register
2314 move cost above. This is not a problem. */
2316 static int
2317 mn10300_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2318 reg_class_t iclass, bool in ATTRIBUTE_UNUSED)
2320 enum reg_class rclass = (enum reg_class) iclass;
2322 if (rclass == FP_REGS)
2323 return 8;
2324 return 6;
2327 /* Implement the TARGET_RTX_COSTS hook.
2329 Speed-relative costs are relative to COSTS_N_INSNS, which is intended
2330 to represent cycles. Size-relative costs are in bytes. */
2332 static bool
2333 mn10300_rtx_costs (rtx x, machine_mode mode, int outer_code,
2334 int opno ATTRIBUTE_UNUSED, int *ptotal, bool speed)
2336 /* This value is used for SYMBOL_REF etc where we want to pretend
2337 we have a full 32-bit constant. */
2338 HOST_WIDE_INT i = 0x12345678;
2339 int total;
2340 int code = GET_CODE (x);
2342 switch (code)
2344 case CONST_INT:
2345 i = INTVAL (x);
2346 do_int_costs:
2347 if (speed)
2349 if (outer_code == SET)
2351 /* 16-bit integer loads have latency 1, 32-bit loads 2. */
2352 if (IN_RANGE (i, -32768, 32767))
2353 total = COSTS_N_INSNS (1);
2354 else
2355 total = COSTS_N_INSNS (2);
2357 else
2359 /* 16-bit integer operands don't affect latency;
2360 24-bit and 32-bit operands add a cycle. */
2361 if (IN_RANGE (i, -32768, 32767))
2362 total = 0;
2363 else
2364 total = COSTS_N_INSNS (1);
2367 else
2369 if (outer_code == SET)
2371 if (i == 0)
2372 total = 1;
2373 else if (IN_RANGE (i, -128, 127))
2374 total = 2;
2375 else if (IN_RANGE (i, -32768, 32767))
2376 total = 3;
2377 else
2378 total = 6;
2380 else
2382 /* Reference here is ADD An,Dn, vs ADD imm,Dn. */
2383 if (IN_RANGE (i, -128, 127))
2384 total = 0;
2385 else if (IN_RANGE (i, -32768, 32767))
2386 total = 2;
2387 else if (TARGET_AM33 && IN_RANGE (i, -0x01000000, 0x00ffffff))
2388 total = 3;
2389 else
2390 total = 4;
2393 goto alldone;
2395 case CONST:
2396 case LABEL_REF:
2397 case SYMBOL_REF:
2398 case CONST_DOUBLE:
2399 /* We assume all of these require a 32-bit constant, even though
2400 some symbol and label references can be relaxed. */
2401 goto do_int_costs;
2403 case UNSPEC:
2404 switch (XINT (x, 1))
2406 case UNSPEC_PIC:
2407 case UNSPEC_GOT:
2408 case UNSPEC_GOTOFF:
2409 case UNSPEC_PLT:
2410 case UNSPEC_GOTSYM_OFF:
2411 /* The PIC unspecs also resolve to a 32-bit constant. */
2412 goto do_int_costs;
2414 default:
2415 /* Assume any non-listed unspec is some sort of arithmetic. */
2416 goto do_arith_costs;
2419 case PLUS:
2420 /* Notice the size difference of INC and INC4. */
2421 if (!speed && outer_code == SET && CONST_INT_P (XEXP (x, 1)))
2423 i = INTVAL (XEXP (x, 1));
2424 if (i == 1 || i == 4)
2426 total = 1 + rtx_cost (XEXP (x, 0), mode, PLUS, 0, speed);
2427 goto alldone;
2430 goto do_arith_costs;
2432 case MINUS:
2433 case AND:
2434 case IOR:
2435 case XOR:
2436 case NOT:
2437 case NEG:
2438 case ZERO_EXTEND:
2439 case SIGN_EXTEND:
2440 case COMPARE:
2441 case BSWAP:
2442 case CLZ:
2443 do_arith_costs:
2444 total = (speed ? COSTS_N_INSNS (1) : 2);
2445 break;
2447 case ASHIFT:
2448 /* Notice the size difference of ASL2 and variants. */
2449 if (!speed && CONST_INT_P (XEXP (x, 1)))
2450 switch (INTVAL (XEXP (x, 1)))
2452 case 1:
2453 case 2:
2454 total = 1;
2455 goto alldone;
2456 case 3:
2457 case 4:
2458 total = 2;
2459 goto alldone;
2461 /* FALLTHRU */
2463 case ASHIFTRT:
2464 case LSHIFTRT:
2465 total = (speed ? COSTS_N_INSNS (1) : 3);
2466 goto alldone;
2468 case MULT:
2469 total = (speed ? COSTS_N_INSNS (3) : 2);
2470 break;
2472 case DIV:
2473 case UDIV:
2474 case MOD:
2475 case UMOD:
2476 total = (speed ? COSTS_N_INSNS (39)
2477 /* Include space to load+retrieve MDR. */
2478 : code == MOD || code == UMOD ? 6 : 4);
2479 break;
2481 case MEM:
2482 total = mn10300_address_cost (XEXP (x, 0), mode,
2483 MEM_ADDR_SPACE (x), speed);
2484 if (speed)
2485 total = COSTS_N_INSNS (2 + total);
2486 goto alldone;
2488 default:
2489 /* Probably not implemented. Assume external call. */
2490 total = (speed ? COSTS_N_INSNS (10) : 7);
2491 break;
2494 *ptotal = total;
2495 return false;
2497 alldone:
2498 *ptotal = total;
2499 return true;
2502 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2503 may access it using GOTOFF instead of GOT. */
2505 static void
2506 mn10300_encode_section_info (tree decl, rtx rtl, int first)
2508 rtx symbol;
2510 default_encode_section_info (decl, rtl, first);
2512 if (! MEM_P (rtl))
2513 return;
2515 symbol = XEXP (rtl, 0);
2516 if (GET_CODE (symbol) != SYMBOL_REF)
2517 return;
2519 if (flag_pic)
2520 SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2523 /* Dispatch tables on the mn10300 are extremely expensive in terms of code
2524 and readonly data size. So we crank up the case threshold value to
2525 encourage a series of if/else comparisons to implement many small switch
2526 statements. In theory, this value could be increased much more if we
2527 were solely optimizing for space, but we keep it "reasonable" to avoid
2528 serious code efficiency lossage. */
2530 static unsigned int
2531 mn10300_case_values_threshold (void)
2533 return 6;
2536 /* Worker function for TARGET_TRAMPOLINE_INIT. */
2538 static void
2539 mn10300_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2541 rtx mem, disp, fnaddr = XEXP (DECL_RTL (fndecl), 0);
2543 /* This is a strict alignment target, which means that we play
2544 some games to make sure that the locations at which we need
2545 to store <chain> and <disp> wind up at aligned addresses.
2547 0x28 0x00 add 0,d0
2548 0xfc 0xdd mov chain,a1
2549 <chain>
2550 0xf8 0xed 0x00 btst 0,d1
2551 0xdc jmp fnaddr
2552 <disp>
2554 Note that the two extra insns are effectively nops; they
2555 clobber the flags but do not affect the contents of D0 or D1. */
2557 disp = expand_binop (SImode, sub_optab, fnaddr,
2558 plus_constant (Pmode, XEXP (m_tramp, 0), 11),
2559 NULL_RTX, 1, OPTAB_DIRECT);
2561 mem = adjust_address (m_tramp, SImode, 0);
2562 emit_move_insn (mem, gen_int_mode (0xddfc0028, SImode));
2563 mem = adjust_address (m_tramp, SImode, 4);
2564 emit_move_insn (mem, chain_value);
2565 mem = adjust_address (m_tramp, SImode, 8);
2566 emit_move_insn (mem, gen_int_mode (0xdc00edf8, SImode));
2567 mem = adjust_address (m_tramp, SImode, 12);
2568 emit_move_insn (mem, disp);
2571 /* Output the assembler code for a C++ thunk function.
2572 THUNK_DECL is the declaration for the thunk function itself, FUNCTION
2573 is the decl for the target function. DELTA is an immediate constant
2574 offset to be added to the THIS parameter. If VCALL_OFFSET is nonzero
2575 the word at the adjusted address *(*THIS' + VCALL_OFFSET) should be
2576 additionally added to THIS. Finally jump to the entry point of
2577 FUNCTION. */
2579 static void
2580 mn10300_asm_output_mi_thunk (FILE * file,
2581 tree thunk_fndecl ATTRIBUTE_UNUSED,
2582 HOST_WIDE_INT delta,
2583 HOST_WIDE_INT vcall_offset,
2584 tree function)
2586 const char * _this;
2588 /* Get the register holding the THIS parameter. Handle the case
2589 where there is a hidden first argument for a returned structure. */
2590 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2591 _this = reg_names [FIRST_ARGUMENT_REGNUM + 1];
2592 else
2593 _this = reg_names [FIRST_ARGUMENT_REGNUM];
2595 fprintf (file, "\t%s Thunk Entry Point:\n", ASM_COMMENT_START);
2597 if (delta)
2598 fprintf (file, "\tadd %d, %s\n", (int) delta, _this);
2600 if (vcall_offset)
2602 const char * scratch = reg_names [FIRST_ADDRESS_REGNUM + 1];
2604 fprintf (file, "\tmov %s, %s\n", _this, scratch);
2605 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2606 fprintf (file, "\tadd %d, %s\n", (int) vcall_offset, scratch);
2607 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2608 fprintf (file, "\tadd %s, %s\n", scratch, _this);
2611 fputs ("\tjmp ", file);
2612 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2613 putc ('\n', file);
2616 /* Return true if mn10300_output_mi_thunk would be able to output the
2617 assembler code for the thunk function specified by the arguments
2618 it is passed, and false otherwise. */
2620 static bool
2621 mn10300_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
2622 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
2623 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2624 const_tree function ATTRIBUTE_UNUSED)
2626 return true;
2629 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2631 static bool
2632 mn10300_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2634 if (REGNO_REG_CLASS (regno) == FP_REGS
2635 || REGNO_REG_CLASS (regno) == FP_ACC_REGS)
2636 /* Do not store integer values in FP registers. */
2637 return GET_MODE_CLASS (mode) == MODE_FLOAT && ((regno & 1) == 0);
2639 if (! TARGET_AM33 && REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2640 return false;
2642 if (((regno) & 1) == 0 || GET_MODE_SIZE (mode) == 4)
2643 return true;
2645 if (REGNO_REG_CLASS (regno) == DATA_REGS
2646 || (TARGET_AM33 && REGNO_REG_CLASS (regno) == ADDRESS_REGS)
2647 || REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2648 return GET_MODE_SIZE (mode) <= 4;
2650 return false;
2653 /* Implement TARGET_MODES_TIEABLE_P. */
2655 static bool
2656 mn10300_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2658 if (GET_MODE_CLASS (mode1) == MODE_FLOAT
2659 && GET_MODE_CLASS (mode2) != MODE_FLOAT)
2660 return false;
2662 if (GET_MODE_CLASS (mode2) == MODE_FLOAT
2663 && GET_MODE_CLASS (mode1) != MODE_FLOAT)
2664 return false;
2666 if (TARGET_AM33
2667 || mode1 == mode2
2668 || (GET_MODE_SIZE (mode1) <= 4 && GET_MODE_SIZE (mode2) <= 4))
2669 return true;
2671 return false;
2674 static int
2675 cc_flags_for_mode (machine_mode mode)
2677 switch (mode)
2679 case E_CCmode:
2680 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C | CC_FLAG_V;
2681 case E_CCZNCmode:
2682 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C;
2683 case E_CCZNmode:
2684 return CC_FLAG_Z | CC_FLAG_N;
2685 case E_CC_FLOATmode:
2686 return -1;
2687 default:
2688 gcc_unreachable ();
2692 static int
2693 cc_flags_for_code (enum rtx_code code)
2695 switch (code)
2697 case EQ: /* Z */
2698 case NE: /* ~Z */
2699 return CC_FLAG_Z;
2701 case LT: /* N */
2702 case GE: /* ~N */
2703 return CC_FLAG_N;
2705 case GT: /* ~(Z|(N^V)) */
2706 case LE: /* Z|(N^V) */
2707 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_V;
2709 case GEU: /* ~C */
2710 case LTU: /* C */
2711 return CC_FLAG_C;
2713 case GTU: /* ~(C | Z) */
2714 case LEU: /* C | Z */
2715 return CC_FLAG_Z | CC_FLAG_C;
2717 case ORDERED:
2718 case UNORDERED:
2719 case LTGT:
2720 case UNEQ:
2721 case UNGE:
2722 case UNGT:
2723 case UNLE:
2724 case UNLT:
2725 return -1;
2727 default:
2728 gcc_unreachable ();
2732 machine_mode
2733 mn10300_select_cc_mode (enum rtx_code code, rtx x, rtx y ATTRIBUTE_UNUSED)
2735 int req;
2737 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2738 return CC_FLOATmode;
2740 req = cc_flags_for_code (code);
2742 if (req & CC_FLAG_V)
2743 return CCmode;
2744 if (req & CC_FLAG_C)
2745 return CCZNCmode;
2746 return CCZNmode;
2749 static inline bool
2750 set_is_load_p (rtx set)
2752 return MEM_P (SET_SRC (set));
2755 static inline bool
2756 set_is_store_p (rtx set)
2758 return MEM_P (SET_DEST (set));
2761 /* Update scheduling costs for situations that cannot be
2762 described using the attributes and DFA machinery.
2763 DEP is the insn being scheduled.
2764 INSN is the previous insn.
2765 COST is the current cycle cost for DEP. */
2767 static int
2768 mn10300_adjust_sched_cost (rtx_insn *insn, int dep_type, rtx_insn *dep,
2769 int cost, unsigned int)
2771 rtx insn_set;
2772 rtx dep_set;
2773 int timings;
2775 if (!TARGET_AM33)
2776 return 1;
2778 /* We are only interested in pairs of SET. */
2779 insn_set = single_set (insn);
2780 if (!insn_set)
2781 return cost;
2783 dep_set = single_set (dep);
2784 if (!dep_set)
2785 return cost;
2787 /* For the AM34 a load instruction that follows a
2788 store instruction incurs an extra cycle of delay. */
2789 if (mn10300_tune_cpu == PROCESSOR_AM34
2790 && set_is_load_p (dep_set)
2791 && set_is_store_p (insn_set))
2792 cost += 1;
2794 /* For the AM34 a non-store, non-branch FPU insn that follows
2795 another FPU insn incurs a one cycle throughput increase. */
2796 else if (mn10300_tune_cpu == PROCESSOR_AM34
2797 && ! set_is_store_p (insn_set)
2798 && ! JUMP_P (insn)
2799 && GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) == MODE_FLOAT
2800 && GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) == MODE_FLOAT)
2801 cost += 1;
2803 /* Resolve the conflict described in section 1-7-4 of
2804 Chapter 3 of the MN103E Series Instruction Manual
2805 where it says:
2807 "When the preceding instruction is a CPU load or
2808 store instruction, a following FPU instruction
2809 cannot be executed until the CPU completes the
2810 latency period even though there are no register
2811 or flag dependencies between them." */
2813 /* Only the AM33-2 (and later) CPUs have FPU instructions. */
2814 if (! TARGET_AM33_2)
2815 return cost;
2817 /* If a data dependence already exists then the cost is correct. */
2818 if (dep_type == 0)
2819 return cost;
2821 /* Check that the instruction about to scheduled is an FPU instruction. */
2822 if (GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) != MODE_FLOAT)
2823 return cost;
2825 /* Now check to see if the previous instruction is a load or store. */
2826 if (! set_is_load_p (insn_set) && ! set_is_store_p (insn_set))
2827 return cost;
2829 /* XXX: Verify: The text of 1-7-4 implies that the restriction
2830 only applies when an INTEGER load/store precedes an FPU
2831 instruction, but is this true ? For now we assume that it is. */
2832 if (GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) != MODE_INT)
2833 return cost;
2835 /* Extract the latency value from the timings attribute. */
2836 timings = get_attr_timings (insn);
2837 return timings < 100 ? (timings % 10) : (timings % 100);
2840 static void
2841 mn10300_conditional_register_usage (void)
2843 unsigned int i;
2845 if (!TARGET_AM33)
2847 for (i = FIRST_EXTENDED_REGNUM;
2848 i <= LAST_EXTENDED_REGNUM; i++)
2849 fixed_regs[i] = call_used_regs[i] = 1;
2851 if (!TARGET_AM33_2)
2853 for (i = FIRST_FP_REGNUM;
2854 i <= LAST_FP_REGNUM; i++)
2855 fixed_regs[i] = call_used_regs[i] = 1;
2857 if (flag_pic)
2858 fixed_regs[PIC_OFFSET_TABLE_REGNUM] =
2859 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2862 /* Worker function for TARGET_MD_ASM_ADJUST.
2863 We do this in the mn10300 backend to maintain source compatibility
2864 with the old cc0-based compiler. */
2866 static rtx_insn *
2867 mn10300_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
2868 vec<const char *> &/*constraints*/,
2869 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
2871 clobbers.safe_push (gen_rtx_REG (CCmode, CC_REG));
2872 SET_HARD_REG_BIT (clobbered_regs, CC_REG);
2873 return NULL;
2876 /* A helper function for splitting cbranch patterns after reload. */
2878 void
2879 mn10300_split_cbranch (machine_mode cmp_mode, rtx cmp_op, rtx label_ref)
2881 rtx flags, x;
2883 flags = gen_rtx_REG (cmp_mode, CC_REG);
2884 x = gen_rtx_COMPARE (cmp_mode, XEXP (cmp_op, 0), XEXP (cmp_op, 1));
2885 x = gen_rtx_SET (flags, x);
2886 emit_insn (x);
2888 x = gen_rtx_fmt_ee (GET_CODE (cmp_op), VOIDmode, flags, const0_rtx);
2889 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x, label_ref, pc_rtx);
2890 x = gen_rtx_SET (pc_rtx, x);
2891 emit_jump_insn (x);
2894 /* A helper function for matching parallels that set the flags. */
2896 bool
2897 mn10300_match_ccmode (rtx insn, machine_mode cc_mode)
2899 rtx op1, flags;
2900 machine_mode flags_mode;
2902 gcc_checking_assert (XVECLEN (PATTERN (insn), 0) == 2);
2904 op1 = XVECEXP (PATTERN (insn), 0, 0);
2905 gcc_checking_assert (GET_CODE (SET_SRC (op1)) == COMPARE);
2907 flags = SET_DEST (op1);
2908 flags_mode = GET_MODE (flags);
2910 if (GET_MODE (SET_SRC (op1)) != flags_mode)
2911 return false;
2912 if (GET_MODE_CLASS (flags_mode) != MODE_CC)
2913 return false;
2915 /* Ensure that the mode of FLAGS is compatible with CC_MODE. */
2916 if (cc_flags_for_mode (flags_mode) & ~cc_flags_for_mode (cc_mode))
2917 return false;
2919 return true;
2922 /* This function is used to help split:
2924 (set (reg) (and (reg) (int)))
2926 into:
2928 (set (reg) (shift (reg) (int))
2929 (set (reg) (shift (reg) (int))
2931 where the shitfs will be shorter than the "and" insn.
2933 It returns the number of bits that should be shifted. A positive
2934 values means that the low bits are to be cleared (and hence the
2935 shifts should be right followed by left) whereas a negative value
2936 means that the high bits are to be cleared (left followed by right).
2937 Zero is returned when it would not be economical to split the AND. */
2940 mn10300_split_and_operand_count (rtx op)
2942 HOST_WIDE_INT val = INTVAL (op);
2943 int count;
2945 if (val < 0)
2947 /* High bit is set, look for bits clear at the bottom. */
2948 count = exact_log2 (-val);
2949 if (count < 0)
2950 return 0;
2951 /* This is only size win if we can use the asl2 insn. Otherwise we
2952 would be replacing 1 6-byte insn with 2 3-byte insns. */
2953 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2954 return 0;
2955 return count;
2957 else
2959 /* High bit is clear, look for bits set at the bottom. */
2960 count = exact_log2 (val + 1);
2961 count = 32 - count;
2962 /* Again, this is only a size win with asl2. */
2963 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2964 return 0;
2965 return -count;
2969 struct liw_data
2971 enum attr_liw slot;
2972 enum attr_liw_op op;
2973 rtx dest;
2974 rtx src;
2977 /* Decide if the given insn is a candidate for LIW bundling. If it is then
2978 extract the operands and LIW attributes from the insn and use them to fill
2979 in the liw_data structure. Return true upon success or false if the insn
2980 cannot be bundled. */
2982 static bool
2983 extract_bundle (rtx_insn *insn, struct liw_data * pdata)
2985 bool allow_consts = true;
2986 rtx p;
2988 gcc_assert (pdata != NULL);
2990 if (insn == NULL)
2991 return false;
2992 /* Make sure that we are dealing with a simple SET insn. */
2993 p = single_set (insn);
2994 if (p == NULL_RTX)
2995 return false;
2997 /* Make sure that it could go into one of the LIW pipelines. */
2998 pdata->slot = get_attr_liw (insn);
2999 if (pdata->slot == LIW_BOTH)
3000 return false;
3002 pdata->op = get_attr_liw_op (insn);
3004 switch (pdata->op)
3006 case LIW_OP_MOV:
3007 pdata->dest = SET_DEST (p);
3008 pdata->src = SET_SRC (p);
3009 break;
3010 case LIW_OP_CMP:
3011 pdata->dest = XEXP (SET_SRC (p), 0);
3012 pdata->src = XEXP (SET_SRC (p), 1);
3013 break;
3014 case LIW_OP_NONE:
3015 return false;
3016 case LIW_OP_AND:
3017 case LIW_OP_OR:
3018 case LIW_OP_XOR:
3019 /* The AND, OR and XOR long instruction words only accept register arguments. */
3020 allow_consts = false;
3021 /* Fall through. */
3022 default:
3023 pdata->dest = SET_DEST (p);
3024 pdata->src = XEXP (SET_SRC (p), 1);
3025 break;
3028 if (! REG_P (pdata->dest))
3029 return false;
3031 if (REG_P (pdata->src))
3032 return true;
3034 return allow_consts && satisfies_constraint_O (pdata->src);
3037 /* Make sure that it is OK to execute LIW1 and LIW2 in parallel. GCC generated
3038 the instructions with the assumption that LIW1 would be executed before LIW2
3039 so we must check for overlaps between their sources and destinations. */
3041 static bool
3042 check_liw_constraints (struct liw_data * pliw1, struct liw_data * pliw2)
3044 /* Check for slot conflicts. */
3045 if (pliw2->slot == pliw1->slot && pliw1->slot != LIW_EITHER)
3046 return false;
3048 /* If either operation is a compare, then "dest" is really an input; the real
3049 destination is CC_REG. So these instructions need different checks. */
3051 /* Changing "CMP ; OP" into "CMP | OP" is OK because the comparison will
3052 check its values prior to any changes made by OP. */
3053 if (pliw1->op == LIW_OP_CMP)
3055 /* Two sequential comparisons means dead code, which ought to
3056 have been eliminated given that bundling only happens with
3057 optimization. We cannot bundle them in any case. */
3058 gcc_assert (pliw1->op != pliw2->op);
3059 return true;
3062 /* Changing "OP ; CMP" into "OP | CMP" does not work if the value being compared
3063 is the destination of OP, as the CMP will look at the old value, not the new
3064 one. */
3065 if (pliw2->op == LIW_OP_CMP)
3067 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3068 return false;
3070 if (REG_P (pliw2->src))
3071 return REGNO (pliw2->src) != REGNO (pliw1->dest);
3073 return true;
3076 /* Changing "OP1 ; OP2" into "OP1 | OP2" does not work if they both write to the
3077 same destination register. */
3078 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3079 return false;
3081 /* Changing "OP1 ; OP2" into "OP1 | OP2" generally does not work if the destination
3082 of OP1 is the source of OP2. The exception is when OP1 is a MOVE instruction when
3083 we can replace the source in OP2 with the source of OP1. */
3084 if (REG_P (pliw2->src) && REGNO (pliw2->src) == REGNO (pliw1->dest))
3086 if (pliw1->op == LIW_OP_MOV && REG_P (pliw1->src))
3088 if (! REG_P (pliw1->src)
3089 && (pliw2->op == LIW_OP_AND
3090 || pliw2->op == LIW_OP_OR
3091 || pliw2->op == LIW_OP_XOR))
3092 return false;
3094 pliw2->src = pliw1->src;
3095 return true;
3097 return false;
3100 /* Everything else is OK. */
3101 return true;
3104 /* Combine pairs of insns into LIW bundles. */
3106 static void
3107 mn10300_bundle_liw (void)
3109 rtx_insn *r;
3111 for (r = get_insns (); r != NULL; r = next_nonnote_nondebug_insn (r))
3113 rtx_insn *insn1, *insn2;
3114 struct liw_data liw1, liw2;
3116 insn1 = r;
3117 if (! extract_bundle (insn1, & liw1))
3118 continue;
3120 insn2 = next_nonnote_nondebug_insn (insn1);
3121 if (! extract_bundle (insn2, & liw2))
3122 continue;
3124 /* Check for source/destination overlap. */
3125 if (! check_liw_constraints (& liw1, & liw2))
3126 continue;
3128 if (liw1.slot == LIW_OP2 || liw2.slot == LIW_OP1)
3130 struct liw_data temp;
3132 temp = liw1;
3133 liw1 = liw2;
3134 liw2 = temp;
3137 delete_insn (insn2);
3139 rtx insn2_pat;
3140 if (liw1.op == LIW_OP_CMP)
3141 insn2_pat = gen_cmp_liw (liw2.dest, liw2.src, liw1.dest, liw1.src,
3142 GEN_INT (liw2.op));
3143 else if (liw2.op == LIW_OP_CMP)
3144 insn2_pat = gen_liw_cmp (liw1.dest, liw1.src, liw2.dest, liw2.src,
3145 GEN_INT (liw1.op));
3146 else
3147 insn2_pat = gen_liw (liw1.dest, liw2.dest, liw1.src, liw2.src,
3148 GEN_INT (liw1.op), GEN_INT (liw2.op));
3150 insn2 = emit_insn_after (insn2_pat, insn1);
3151 delete_insn (insn1);
3152 r = insn2;
3156 #define DUMP(reason, insn) \
3157 do \
3159 if (dump_file) \
3161 fprintf (dump_file, reason "\n"); \
3162 if (insn != NULL_RTX) \
3163 print_rtl_single (dump_file, insn); \
3164 fprintf(dump_file, "\n"); \
3167 while (0)
3169 /* Replace the BRANCH insn with a Lcc insn that goes to LABEL.
3170 Insert a SETLB insn just before LABEL. */
3172 static void
3173 mn10300_insert_setlb_lcc (rtx_insn *label, rtx_insn *branch)
3175 rtx lcc, comparison, cmp_reg;
3177 if (LABEL_NUSES (label) > 1)
3179 rtx_insn *insn;
3181 /* This label is used both as an entry point to the loop
3182 and as a loop-back point for the loop. We need to separate
3183 these two functions so that the SETLB happens upon entry,
3184 but the loop-back does not go to the SETLB instruction. */
3185 DUMP ("Inserting SETLB insn after:", label);
3186 insn = emit_insn_after (gen_setlb (), label);
3187 label = gen_label_rtx ();
3188 emit_label_after (label, insn);
3189 DUMP ("Created new loop-back label:", label);
3191 else
3193 DUMP ("Inserting SETLB insn before:", label);
3194 emit_insn_before (gen_setlb (), label);
3197 comparison = XEXP (SET_SRC (PATTERN (branch)), 0);
3198 cmp_reg = XEXP (comparison, 0);
3199 gcc_assert (REG_P (cmp_reg));
3201 /* If the comparison has not already been split out of the branch
3202 then do so now. */
3203 gcc_assert (REGNO (cmp_reg) == CC_REG);
3205 if (GET_MODE (cmp_reg) == CC_FLOATmode)
3206 lcc = gen_FLcc (comparison, label);
3207 else
3208 lcc = gen_Lcc (comparison, label);
3210 rtx_insn *jump = emit_jump_insn_before (lcc, branch);
3211 mark_jump_label (XVECEXP (lcc, 0, 0), jump, 0);
3212 JUMP_LABEL (jump) = label;
3213 DUMP ("Replacing branch insn...", branch);
3214 DUMP ("... with Lcc insn:", jump);
3215 delete_insn (branch);
3218 static bool
3219 mn10300_block_contains_call (basic_block block)
3221 rtx_insn *insn;
3223 FOR_BB_INSNS (block, insn)
3224 if (CALL_P (insn))
3225 return true;
3227 return false;
3230 static bool
3231 mn10300_loop_contains_call_insn (loop_p loop)
3233 basic_block * bbs;
3234 bool result = false;
3235 unsigned int i;
3237 bbs = get_loop_body (loop);
3239 for (i = 0; i < loop->num_nodes; i++)
3240 if (mn10300_block_contains_call (bbs[i]))
3242 result = true;
3243 break;
3246 free (bbs);
3247 return result;
3250 static void
3251 mn10300_scan_for_setlb_lcc (void)
3253 loop_p loop;
3255 DUMP ("Looking for loops that can use the SETLB insn", NULL_RTX);
3257 df_analyze ();
3258 compute_bb_for_insn ();
3260 /* Find the loops. */
3261 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3263 /* FIXME: For now we only investigate innermost loops. In practice however
3264 if an inner loop is not suitable for use with the SETLB/Lcc insns, it may
3265 be the case that its parent loop is suitable. Thus we should check all
3266 loops, but work from the innermost outwards. */
3267 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
3269 const char * reason = NULL;
3271 /* Check to see if we can modify this loop. If we cannot
3272 then set 'reason' to describe why it could not be done. */
3273 if (loop->latch == NULL)
3274 reason = "it contains multiple latches";
3275 else if (loop->header != loop->latch)
3276 /* FIXME: We could handle loops that span multiple blocks,
3277 but this requires a lot more work tracking down the branches
3278 that need altering, so for now keep things simple. */
3279 reason = "the loop spans multiple blocks";
3280 else if (mn10300_loop_contains_call_insn (loop))
3281 reason = "it contains CALL insns";
3282 else
3284 rtx_insn *branch = BB_END (loop->latch);
3286 gcc_assert (JUMP_P (branch));
3287 if (single_set (branch) == NULL_RTX || ! any_condjump_p (branch))
3288 /* We cannot optimize tablejumps and the like. */
3289 /* FIXME: We could handle unconditional jumps. */
3290 reason = "it is not a simple loop";
3291 else
3293 rtx_insn *label;
3295 if (dump_file)
3296 flow_loop_dump (loop, dump_file, NULL, 0);
3298 label = BB_HEAD (loop->header);
3299 gcc_assert (LABEL_P (label));
3301 mn10300_insert_setlb_lcc (label, branch);
3305 if (dump_file && reason != NULL)
3306 fprintf (dump_file, "Loop starting with insn %d is not suitable because %s\n",
3307 INSN_UID (BB_HEAD (loop->header)),
3308 reason);
3311 loop_optimizer_finalize ();
3313 df_finish_pass (false);
3315 DUMP ("SETLB scan complete", NULL_RTX);
3318 static void
3319 mn10300_reorg (void)
3321 /* These are optimizations, so only run them if optimizing. */
3322 if (TARGET_AM33 && (optimize > 0 || optimize_size))
3324 if (TARGET_ALLOW_SETLB)
3325 mn10300_scan_for_setlb_lcc ();
3327 if (TARGET_ALLOW_LIW)
3328 mn10300_bundle_liw ();
3332 /* Initialize the GCC target structure. */
3334 #undef TARGET_MACHINE_DEPENDENT_REORG
3335 #define TARGET_MACHINE_DEPENDENT_REORG mn10300_reorg
3337 #undef TARGET_ASM_ALIGNED_HI_OP
3338 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3340 #undef TARGET_LEGITIMIZE_ADDRESS
3341 #define TARGET_LEGITIMIZE_ADDRESS mn10300_legitimize_address
3343 #undef TARGET_ADDRESS_COST
3344 #define TARGET_ADDRESS_COST mn10300_address_cost
3345 #undef TARGET_REGISTER_MOVE_COST
3346 #define TARGET_REGISTER_MOVE_COST mn10300_register_move_cost
3347 #undef TARGET_MEMORY_MOVE_COST
3348 #define TARGET_MEMORY_MOVE_COST mn10300_memory_move_cost
3349 #undef TARGET_RTX_COSTS
3350 #define TARGET_RTX_COSTS mn10300_rtx_costs
3352 #undef TARGET_ASM_FILE_START
3353 #define TARGET_ASM_FILE_START mn10300_file_start
3354 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
3355 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
3357 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
3358 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA mn10300_asm_output_addr_const_extra
3360 #undef TARGET_OPTION_OVERRIDE
3361 #define TARGET_OPTION_OVERRIDE mn10300_option_override
3363 #undef TARGET_ENCODE_SECTION_INFO
3364 #define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
3366 #undef TARGET_PROMOTE_PROTOTYPES
3367 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
3368 #undef TARGET_RETURN_IN_MEMORY
3369 #define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
3370 #undef TARGET_PASS_BY_REFERENCE
3371 #define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
3372 #undef TARGET_CALLEE_COPIES
3373 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
3374 #undef TARGET_ARG_PARTIAL_BYTES
3375 #define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
3376 #undef TARGET_FUNCTION_ARG
3377 #define TARGET_FUNCTION_ARG mn10300_function_arg
3378 #undef TARGET_FUNCTION_ARG_ADVANCE
3379 #define TARGET_FUNCTION_ARG_ADVANCE mn10300_function_arg_advance
3381 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
3382 #define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
3383 #undef TARGET_EXPAND_BUILTIN_VA_START
3384 #define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
3386 #undef TARGET_CASE_VALUES_THRESHOLD
3387 #define TARGET_CASE_VALUES_THRESHOLD mn10300_case_values_threshold
3389 #undef TARGET_LRA_P
3390 #define TARGET_LRA_P hook_bool_void_false
3392 #undef TARGET_LEGITIMATE_ADDRESS_P
3393 #define TARGET_LEGITIMATE_ADDRESS_P mn10300_legitimate_address_p
3394 #undef TARGET_DELEGITIMIZE_ADDRESS
3395 #define TARGET_DELEGITIMIZE_ADDRESS mn10300_delegitimize_address
3396 #undef TARGET_LEGITIMATE_CONSTANT_P
3397 #define TARGET_LEGITIMATE_CONSTANT_P mn10300_legitimate_constant_p
3399 #undef TARGET_PREFERRED_RELOAD_CLASS
3400 #define TARGET_PREFERRED_RELOAD_CLASS mn10300_preferred_reload_class
3401 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
3402 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS \
3403 mn10300_preferred_output_reload_class
3404 #undef TARGET_SECONDARY_RELOAD
3405 #define TARGET_SECONDARY_RELOAD mn10300_secondary_reload
3407 #undef TARGET_TRAMPOLINE_INIT
3408 #define TARGET_TRAMPOLINE_INIT mn10300_trampoline_init
3410 #undef TARGET_FUNCTION_VALUE
3411 #define TARGET_FUNCTION_VALUE mn10300_function_value
3412 #undef TARGET_LIBCALL_VALUE
3413 #define TARGET_LIBCALL_VALUE mn10300_libcall_value
3415 #undef TARGET_ASM_OUTPUT_MI_THUNK
3416 #define TARGET_ASM_OUTPUT_MI_THUNK mn10300_asm_output_mi_thunk
3417 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3418 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK mn10300_can_output_mi_thunk
3420 #undef TARGET_SCHED_ADJUST_COST
3421 #define TARGET_SCHED_ADJUST_COST mn10300_adjust_sched_cost
3423 #undef TARGET_CONDITIONAL_REGISTER_USAGE
3424 #define TARGET_CONDITIONAL_REGISTER_USAGE mn10300_conditional_register_usage
3426 #undef TARGET_MD_ASM_ADJUST
3427 #define TARGET_MD_ASM_ADJUST mn10300_md_asm_adjust
3429 #undef TARGET_FLAGS_REGNUM
3430 #define TARGET_FLAGS_REGNUM CC_REG
3432 #undef TARGET_HARD_REGNO_MODE_OK
3433 #define TARGET_HARD_REGNO_MODE_OK mn10300_hard_regno_mode_ok
3435 #undef TARGET_MODES_TIEABLE_P
3436 #define TARGET_MODES_TIEABLE_P mn10300_modes_tieable_p
3438 struct gcc_target targetm = TARGET_INITIALIZER;