Reverting merge from trunk
[official-gcc.git] / gcc / config / mn10300 / mn10300.c
blobdf563d03eac73a582233a48ea9aa61dbae6df163
1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2 Copyright (C) 1996-2013 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 "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "reload.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "function.h"
39 #include "obstack.h"
40 #include "diagnostic-core.h"
41 #include "tm_p.h"
42 #include "tm-constrs.h"
43 #include "target.h"
44 #include "target-def.h"
45 #include "df.h"
46 #include "opts.h"
47 #include "cfgloop.h"
48 #include "dumpfile.h"
50 /* This is used in the am33_2.0-linux-gnu port, in which global symbol
51 names are not prefixed by underscores, to tell whether to prefix a
52 label with a plus sign or not, so that the assembler can tell
53 symbol names from register names. */
54 int mn10300_protect_label;
56 /* Selected processor type for tuning. */
57 enum processor_type mn10300_tune_cpu = PROCESSOR_DEFAULT;
59 #define CC_FLAG_Z 1
60 #define CC_FLAG_N 2
61 #define CC_FLAG_C 4
62 #define CC_FLAG_V 8
64 static int cc_flags_for_mode(enum machine_mode);
65 static int cc_flags_for_code(enum rtx_code);
67 /* Implement TARGET_OPTION_OVERRIDE. */
69 static void
70 mn10300_option_override (void)
72 if (TARGET_AM33)
73 target_flags &= ~MASK_MULT_BUG;
74 else
76 /* Disable scheduling for the MN10300 as we do
77 not have timing information available for it. */
78 flag_schedule_insns = 0;
79 flag_schedule_insns_after_reload = 0;
81 /* Force enable splitting of wide types, as otherwise it is trivial
82 to run out of registers. Indeed, this works so well that register
83 allocation problems are now more common *without* optimization,
84 when this flag is not enabled by default. */
85 flag_split_wide_types = 1;
88 if (mn10300_tune_string)
90 if (strcasecmp (mn10300_tune_string, "mn10300") == 0)
91 mn10300_tune_cpu = PROCESSOR_MN10300;
92 else if (strcasecmp (mn10300_tune_string, "am33") == 0)
93 mn10300_tune_cpu = PROCESSOR_AM33;
94 else if (strcasecmp (mn10300_tune_string, "am33-2") == 0)
95 mn10300_tune_cpu = PROCESSOR_AM33_2;
96 else if (strcasecmp (mn10300_tune_string, "am34") == 0)
97 mn10300_tune_cpu = PROCESSOR_AM34;
98 else
99 error ("-mtune= expects mn10300, am33, am33-2, or am34");
103 static void
104 mn10300_file_start (void)
106 default_file_start ();
108 if (TARGET_AM33_2)
109 fprintf (asm_out_file, "\t.am33_2\n");
110 else if (TARGET_AM33)
111 fprintf (asm_out_file, "\t.am33\n");
114 /* Note: This list must match the liw_op attribute in mn10300.md. */
116 static const char *liw_op_names[] =
118 "add", "cmp", "sub", "mov",
119 "and", "or", "xor",
120 "asr", "lsr", "asl",
121 "none", "max"
124 /* Print operand X using operand code CODE to assembly language output file
125 FILE. */
127 void
128 mn10300_print_operand (FILE *file, rtx x, int code)
130 switch (code)
132 case 'W':
134 unsigned int liw_op = UINTVAL (x);
136 gcc_assert (TARGET_ALLOW_LIW);
137 gcc_assert (liw_op < LIW_OP_MAX);
138 fputs (liw_op_names[liw_op], file);
139 break;
142 case 'b':
143 case 'B':
145 enum rtx_code cmp = GET_CODE (x);
146 enum machine_mode mode = GET_MODE (XEXP (x, 0));
147 const char *str;
148 int have_flags;
150 if (code == 'B')
151 cmp = reverse_condition (cmp);
152 have_flags = cc_flags_for_mode (mode);
154 switch (cmp)
156 case NE:
157 str = "ne";
158 break;
159 case EQ:
160 str = "eq";
161 break;
162 case GE:
163 /* bge is smaller than bnc. */
164 str = (have_flags & CC_FLAG_V ? "ge" : "nc");
165 break;
166 case LT:
167 str = (have_flags & CC_FLAG_V ? "lt" : "ns");
168 break;
169 case GT:
170 str = "gt";
171 break;
172 case LE:
173 str = "le";
174 break;
175 case GEU:
176 str = "cc";
177 break;
178 case GTU:
179 str = "hi";
180 break;
181 case LEU:
182 str = "ls";
183 break;
184 case LTU:
185 str = "cs";
186 break;
187 case ORDERED:
188 str = "lge";
189 break;
190 case UNORDERED:
191 str = "uo";
192 break;
193 case LTGT:
194 str = "lg";
195 break;
196 case UNEQ:
197 str = "ue";
198 break;
199 case UNGE:
200 str = "uge";
201 break;
202 case UNGT:
203 str = "ug";
204 break;
205 case UNLE:
206 str = "ule";
207 break;
208 case UNLT:
209 str = "ul";
210 break;
211 default:
212 gcc_unreachable ();
215 gcc_checking_assert ((cc_flags_for_code (cmp) & ~have_flags) == 0);
216 fputs (str, file);
218 break;
220 case 'C':
221 /* This is used for the operand to a call instruction;
222 if it's a REG, enclose it in parens, else output
223 the operand normally. */
224 if (REG_P (x))
226 fputc ('(', file);
227 mn10300_print_operand (file, x, 0);
228 fputc (')', file);
230 else
231 mn10300_print_operand (file, x, 0);
232 break;
234 case 'D':
235 switch (GET_CODE (x))
237 case MEM:
238 fputc ('(', file);
239 output_address (XEXP (x, 0));
240 fputc (')', file);
241 break;
243 case REG:
244 fprintf (file, "fd%d", REGNO (x) - 18);
245 break;
247 default:
248 gcc_unreachable ();
250 break;
252 /* These are the least significant word in a 64bit value. */
253 case 'L':
254 switch (GET_CODE (x))
256 case MEM:
257 fputc ('(', file);
258 output_address (XEXP (x, 0));
259 fputc (')', file);
260 break;
262 case REG:
263 fprintf (file, "%s", reg_names[REGNO (x)]);
264 break;
266 case SUBREG:
267 fprintf (file, "%s", reg_names[subreg_regno (x)]);
268 break;
270 case CONST_DOUBLE:
272 long val[2];
273 REAL_VALUE_TYPE rv;
275 switch (GET_MODE (x))
277 case DFmode:
278 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
279 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
280 fprintf (file, "0x%lx", val[0]);
281 break;;
282 case SFmode:
283 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
284 REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
285 fprintf (file, "0x%lx", val[0]);
286 break;;
287 case VOIDmode:
288 case DImode:
289 mn10300_print_operand_address (file,
290 GEN_INT (CONST_DOUBLE_LOW (x)));
291 break;
292 default:
293 break;
295 break;
298 case CONST_INT:
300 rtx low, high;
301 split_double (x, &low, &high);
302 fprintf (file, "%ld", (long)INTVAL (low));
303 break;
306 default:
307 gcc_unreachable ();
309 break;
311 /* Similarly, but for the most significant word. */
312 case 'H':
313 switch (GET_CODE (x))
315 case MEM:
316 fputc ('(', file);
317 x = adjust_address (x, SImode, 4);
318 output_address (XEXP (x, 0));
319 fputc (')', file);
320 break;
322 case REG:
323 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
324 break;
326 case SUBREG:
327 fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
328 break;
330 case CONST_DOUBLE:
332 long val[2];
333 REAL_VALUE_TYPE rv;
335 switch (GET_MODE (x))
337 case DFmode:
338 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
339 REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
340 fprintf (file, "0x%lx", val[1]);
341 break;;
342 case SFmode:
343 gcc_unreachable ();
344 case VOIDmode:
345 case DImode:
346 mn10300_print_operand_address (file,
347 GEN_INT (CONST_DOUBLE_HIGH (x)));
348 break;
349 default:
350 break;
352 break;
355 case CONST_INT:
357 rtx low, high;
358 split_double (x, &low, &high);
359 fprintf (file, "%ld", (long)INTVAL (high));
360 break;
363 default:
364 gcc_unreachable ();
366 break;
368 case 'A':
369 fputc ('(', file);
370 if (REG_P (XEXP (x, 0)))
371 output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), const0_rtx));
372 else
373 output_address (XEXP (x, 0));
374 fputc (')', file);
375 break;
377 case 'N':
378 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
379 fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
380 break;
382 case 'U':
383 gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
384 fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
385 break;
387 /* For shift counts. The hardware ignores the upper bits of
388 any immediate, but the assembler will flag an out of range
389 shift count as an error. So we mask off the high bits
390 of the immediate here. */
391 case 'S':
392 if (CONST_INT_P (x))
394 fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
395 break;
397 /* FALL THROUGH */
399 default:
400 switch (GET_CODE (x))
402 case MEM:
403 fputc ('(', file);
404 output_address (XEXP (x, 0));
405 fputc (')', file);
406 break;
408 case PLUS:
409 output_address (x);
410 break;
412 case REG:
413 fprintf (file, "%s", reg_names[REGNO (x)]);
414 break;
416 case SUBREG:
417 fprintf (file, "%s", reg_names[subreg_regno (x)]);
418 break;
420 /* This will only be single precision.... */
421 case CONST_DOUBLE:
423 unsigned long val;
424 REAL_VALUE_TYPE rv;
426 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
427 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
428 fprintf (file, "0x%lx", val);
429 break;
432 case CONST_INT:
433 case SYMBOL_REF:
434 case CONST:
435 case LABEL_REF:
436 case CODE_LABEL:
437 case UNSPEC:
438 mn10300_print_operand_address (file, x);
439 break;
440 default:
441 gcc_unreachable ();
443 break;
447 /* Output assembly language output for the address ADDR to FILE. */
449 void
450 mn10300_print_operand_address (FILE *file, rtx addr)
452 switch (GET_CODE (addr))
454 case POST_INC:
455 mn10300_print_operand (file, XEXP (addr, 0), 0);
456 fputc ('+', file);
457 break;
459 case POST_MODIFY:
460 mn10300_print_operand (file, XEXP (addr, 0), 0);
461 fputc ('+', file);
462 fputc (',', file);
463 mn10300_print_operand (file, XEXP (addr, 1), 0);
464 break;
466 case REG:
467 mn10300_print_operand (file, addr, 0);
468 break;
469 case PLUS:
471 rtx base = XEXP (addr, 0);
472 rtx index = XEXP (addr, 1);
474 if (REG_P (index) && !REG_OK_FOR_INDEX_P (index))
476 rtx x = base;
477 base = index;
478 index = x;
480 gcc_assert (REG_P (index) && REG_OK_FOR_INDEX_P (index));
482 gcc_assert (REG_OK_FOR_BASE_P (base));
484 mn10300_print_operand (file, index, 0);
485 fputc (',', file);
486 mn10300_print_operand (file, base, 0);
487 break;
489 case SYMBOL_REF:
490 output_addr_const (file, addr);
491 break;
492 default:
493 output_addr_const (file, addr);
494 break;
498 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.
500 Used for PIC-specific UNSPECs. */
502 static bool
503 mn10300_asm_output_addr_const_extra (FILE *file, rtx x)
505 if (GET_CODE (x) == UNSPEC)
507 switch (XINT (x, 1))
509 case UNSPEC_PIC:
510 /* GLOBAL_OFFSET_TABLE or local symbols, no suffix. */
511 output_addr_const (file, XVECEXP (x, 0, 0));
512 break;
513 case UNSPEC_GOT:
514 output_addr_const (file, XVECEXP (x, 0, 0));
515 fputs ("@GOT", file);
516 break;
517 case UNSPEC_GOTOFF:
518 output_addr_const (file, XVECEXP (x, 0, 0));
519 fputs ("@GOTOFF", file);
520 break;
521 case UNSPEC_PLT:
522 output_addr_const (file, XVECEXP (x, 0, 0));
523 fputs ("@PLT", file);
524 break;
525 case UNSPEC_GOTSYM_OFF:
526 assemble_name (file, GOT_SYMBOL_NAME);
527 fputs ("-(", file);
528 output_addr_const (file, XVECEXP (x, 0, 0));
529 fputs ("-.)", file);
530 break;
531 default:
532 return false;
534 return true;
536 else
537 return false;
540 /* Count the number of FP registers that have to be saved. */
541 static int
542 fp_regs_to_save (void)
544 int i, n = 0;
546 if (! TARGET_AM33_2)
547 return 0;
549 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
550 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
551 ++n;
553 return n;
556 /* Print a set of registers in the format required by "movm" and "ret".
557 Register K is saved if bit K of MASK is set. The data and address
558 registers can be stored individually, but the extended registers cannot.
559 We assume that the mask already takes that into account. For instance,
560 bits 14 to 17 must have the same value. */
562 void
563 mn10300_print_reg_list (FILE *file, int mask)
565 int need_comma;
566 int i;
568 need_comma = 0;
569 fputc ('[', file);
571 for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
572 if ((mask & (1 << i)) != 0)
574 if (need_comma)
575 fputc (',', file);
576 fputs (reg_names [i], file);
577 need_comma = 1;
580 if ((mask & 0x3c000) != 0)
582 gcc_assert ((mask & 0x3c000) == 0x3c000);
583 if (need_comma)
584 fputc (',', file);
585 fputs ("exreg1", file);
586 need_comma = 1;
589 fputc (']', file);
592 /* If the MDR register is never clobbered, we can use the RETF instruction
593 which takes the address from the MDR register. This is 3 cycles faster
594 than having to load the address from the stack. */
596 bool
597 mn10300_can_use_retf_insn (void)
599 /* Don't bother if we're not optimizing. In this case we won't
600 have proper access to df_regs_ever_live_p. */
601 if (!optimize)
602 return false;
604 /* EH returns alter the saved return address; MDR is not current. */
605 if (crtl->calls_eh_return)
606 return false;
608 /* Obviously not if MDR is ever clobbered. */
609 if (df_regs_ever_live_p (MDR_REG))
610 return false;
612 /* ??? Careful not to use this during expand_epilogue etc. */
613 gcc_assert (!in_sequence_p ());
614 return leaf_function_p ();
617 bool
618 mn10300_can_use_rets_insn (void)
620 return !mn10300_initial_offset (ARG_POINTER_REGNUM, STACK_POINTER_REGNUM);
623 /* Returns the set of live, callee-saved registers as a bitmask. The
624 callee-saved extended registers cannot be stored individually, so
625 all of them will be included in the mask if any one of them is used.
626 Also returns the number of bytes in the registers in the mask if
627 BYTES_SAVED is not NULL. */
629 unsigned int
630 mn10300_get_live_callee_saved_regs (unsigned int * bytes_saved)
632 int mask;
633 int i;
634 unsigned int count;
636 count = mask = 0;
637 for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
638 if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
640 mask |= (1 << i);
641 ++ count;
644 if ((mask & 0x3c000) != 0)
646 for (i = 0x04000; i < 0x40000; i <<= 1)
647 if ((mask & i) == 0)
648 ++ count;
650 mask |= 0x3c000;
653 if (bytes_saved)
654 * bytes_saved = count * UNITS_PER_WORD;
656 return mask;
659 static rtx
660 F (rtx r)
662 RTX_FRAME_RELATED_P (r) = 1;
663 return r;
666 /* Generate an instruction that pushes several registers onto the stack.
667 Register K will be saved if bit K in MASK is set. The function does
668 nothing if MASK is zero.
670 To be compatible with the "movm" instruction, the lowest-numbered
671 register must be stored in the lowest slot. If MASK is the set
672 { R1,...,RN }, where R1...RN are ordered least first, the generated
673 instruction will have the form:
675 (parallel
676 (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
677 (set (mem:SI (plus:SI (reg:SI 9)
678 (const_int -1*4)))
679 (reg:SI RN))
681 (set (mem:SI (plus:SI (reg:SI 9)
682 (const_int -N*4)))
683 (reg:SI R1))) */
685 static void
686 mn10300_gen_multiple_store (unsigned int mask)
688 /* The order in which registers are stored, from SP-4 through SP-N*4. */
689 static const unsigned int store_order[8] = {
690 /* e2, e3: never saved */
691 FIRST_EXTENDED_REGNUM + 4,
692 FIRST_EXTENDED_REGNUM + 5,
693 FIRST_EXTENDED_REGNUM + 6,
694 FIRST_EXTENDED_REGNUM + 7,
695 /* e0, e1, mdrq, mcrh, mcrl, mcvf: never saved. */
696 FIRST_DATA_REGNUM + 2,
697 FIRST_DATA_REGNUM + 3,
698 FIRST_ADDRESS_REGNUM + 2,
699 FIRST_ADDRESS_REGNUM + 3,
700 /* d0, d1, a0, a1, mdr, lir, lar: never saved. */
703 rtx x, elts[9];
704 unsigned int i;
705 int count;
707 if (mask == 0)
708 return;
710 for (i = count = 0; i < ARRAY_SIZE(store_order); ++i)
712 unsigned regno = store_order[i];
714 if (((mask >> regno) & 1) == 0)
715 continue;
717 ++count;
718 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
719 x = gen_frame_mem (SImode, x);
720 x = gen_rtx_SET (VOIDmode, x, gen_rtx_REG (SImode, regno));
721 elts[count] = F(x);
723 /* Remove the register from the mask so that... */
724 mask &= ~(1u << regno);
727 /* ... we can make sure that we didn't try to use a register
728 not listed in the store order. */
729 gcc_assert (mask == 0);
731 /* Create the instruction that updates the stack pointer. */
732 x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
733 x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
734 elts[0] = F(x);
736 /* We need one PARALLEL element to update the stack pointer and
737 an additional element for each register that is stored. */
738 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (count + 1, elts));
739 F (emit_insn (x));
742 void
743 mn10300_expand_prologue (void)
745 HOST_WIDE_INT size = mn10300_frame_size ();
747 if (flag_stack_usage_info)
748 current_function_static_stack_size = size;
750 /* If we use any of the callee-saved registers, save them now. */
751 mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs (NULL));
753 if (TARGET_AM33_2 && fp_regs_to_save ())
755 int num_regs_to_save = fp_regs_to_save (), i;
756 HOST_WIDE_INT xsize;
757 enum
759 save_sp_merge,
760 save_sp_no_merge,
761 save_sp_partial_merge,
762 save_a0_merge,
763 save_a0_no_merge
764 } strategy;
765 unsigned int strategy_size = (unsigned)-1, this_strategy_size;
766 rtx reg;
768 /* We have several different strategies to save FP registers.
769 We can store them using SP offsets, which is beneficial if
770 there are just a few registers to save, or we can use `a0' in
771 post-increment mode (`a0' is the only call-clobbered address
772 register that is never used to pass information to a
773 function). Furthermore, if we don't need a frame pointer, we
774 can merge the two SP adds into a single one, but this isn't
775 always beneficial; sometimes we can just split the two adds
776 so that we don't exceed a 16-bit constant size. The code
777 below will select which strategy to use, so as to generate
778 smallest code. Ties are broken in favor or shorter sequences
779 (in terms of number of instructions). */
781 #define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
782 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
783 #define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
784 : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
786 /* We add 0 * (S) in two places to promote to the type of S,
787 so that all arms of the conditional have the same type. */
788 #define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
789 (((S) >= (L)) ? 0 * (S) + (SIZE1) * (N) \
790 : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
791 + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
792 : 0 * (S) + (ELSE))
793 #define SIZE_FMOV_SP_(S,N) \
794 (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
795 SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
796 (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
797 #define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
799 /* Consider alternative save_sp_merge only if we don't need the
800 frame pointer and size is nonzero. */
801 if (! frame_pointer_needed && size)
803 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
804 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
805 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
806 this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
808 if (this_strategy_size < strategy_size)
810 strategy = save_sp_merge;
811 strategy_size = this_strategy_size;
815 /* Consider alternative save_sp_no_merge unconditionally. */
816 /* Insn: add -4 * num_regs_to_save, sp. */
817 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
818 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
819 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
820 if (size)
822 /* Insn: add -size, sp. */
823 this_strategy_size += SIZE_ADD_SP (-size);
826 if (this_strategy_size < strategy_size)
828 strategy = save_sp_no_merge;
829 strategy_size = this_strategy_size;
832 /* Consider alternative save_sp_partial_merge only if we don't
833 need a frame pointer and size is reasonably large. */
834 if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
836 /* Insn: add -128, sp. */
837 this_strategy_size = SIZE_ADD_SP (-128);
838 /* Insn: fmov fs#, (##, sp), for each fs# to be saved. */
839 this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
840 num_regs_to_save);
841 if (size)
843 /* Insn: add 128-size, sp. */
844 this_strategy_size += SIZE_ADD_SP (128 - size);
847 if (this_strategy_size < strategy_size)
849 strategy = save_sp_partial_merge;
850 strategy_size = this_strategy_size;
854 /* Consider alternative save_a0_merge only if we don't need a
855 frame pointer, size is nonzero and the user hasn't
856 changed the calling conventions of a0. */
857 if (! frame_pointer_needed && size
858 && call_really_used_regs [FIRST_ADDRESS_REGNUM]
859 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
861 /* Insn: add -(size + 4 * num_regs_to_save), sp. */
862 this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
863 /* Insn: mov sp, a0. */
864 this_strategy_size++;
865 if (size)
867 /* Insn: add size, a0. */
868 this_strategy_size += SIZE_ADD_AX (size);
870 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
871 this_strategy_size += 3 * num_regs_to_save;
873 if (this_strategy_size < strategy_size)
875 strategy = save_a0_merge;
876 strategy_size = this_strategy_size;
880 /* Consider alternative save_a0_no_merge if the user hasn't
881 changed the calling conventions of a0. */
882 if (call_really_used_regs [FIRST_ADDRESS_REGNUM]
883 && ! fixed_regs[FIRST_ADDRESS_REGNUM])
885 /* Insn: add -4 * num_regs_to_save, sp. */
886 this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
887 /* Insn: mov sp, a0. */
888 this_strategy_size++;
889 /* Insn: fmov fs#, (a0+), for each fs# to be saved. */
890 this_strategy_size += 3 * num_regs_to_save;
891 if (size)
893 /* Insn: add -size, sp. */
894 this_strategy_size += SIZE_ADD_SP (-size);
897 if (this_strategy_size < strategy_size)
899 strategy = save_a0_no_merge;
900 strategy_size = this_strategy_size;
904 /* Emit the initial SP add, common to all strategies. */
905 switch (strategy)
907 case save_sp_no_merge:
908 case save_a0_no_merge:
909 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
910 stack_pointer_rtx,
911 GEN_INT (-4 * num_regs_to_save))));
912 xsize = 0;
913 break;
915 case save_sp_partial_merge:
916 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
917 stack_pointer_rtx,
918 GEN_INT (-128))));
919 xsize = 128 - 4 * num_regs_to_save;
920 size -= xsize;
921 break;
923 case save_sp_merge:
924 case save_a0_merge:
925 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
926 stack_pointer_rtx,
927 GEN_INT (-(size + 4 * num_regs_to_save)))));
928 /* We'll have to adjust FP register saves according to the
929 frame size. */
930 xsize = size;
931 /* Since we've already created the stack frame, don't do it
932 again at the end of the function. */
933 size = 0;
934 break;
936 default:
937 gcc_unreachable ();
940 /* Now prepare register a0, if we have decided to use it. */
941 switch (strategy)
943 case save_sp_merge:
944 case save_sp_no_merge:
945 case save_sp_partial_merge:
946 reg = 0;
947 break;
949 case save_a0_merge:
950 case save_a0_no_merge:
951 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
952 F (emit_insn (gen_movsi (reg, stack_pointer_rtx)));
953 if (xsize)
954 F (emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize))));
955 reg = gen_rtx_POST_INC (SImode, reg);
956 break;
958 default:
959 gcc_unreachable ();
962 /* Now actually save the FP registers. */
963 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
964 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
966 rtx addr;
968 if (reg)
969 addr = reg;
970 else
972 /* If we aren't using `a0', use an SP offset. */
973 if (xsize)
975 addr = gen_rtx_PLUS (SImode,
976 stack_pointer_rtx,
977 GEN_INT (xsize));
979 else
980 addr = stack_pointer_rtx;
982 xsize += 4;
985 F (emit_insn (gen_movsf (gen_rtx_MEM (SFmode, addr),
986 gen_rtx_REG (SFmode, i))));
990 /* Now put the frame pointer into the frame pointer register. */
991 if (frame_pointer_needed)
992 F (emit_move_insn (frame_pointer_rtx, stack_pointer_rtx));
994 /* Allocate stack for this frame. */
995 if (size)
996 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
997 stack_pointer_rtx,
998 GEN_INT (-size))));
1000 if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1001 emit_insn (gen_load_pic ());
1004 void
1005 mn10300_expand_epilogue (void)
1007 HOST_WIDE_INT size = mn10300_frame_size ();
1008 unsigned int reg_save_bytes;
1010 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1012 if (TARGET_AM33_2 && fp_regs_to_save ())
1014 int num_regs_to_save = fp_regs_to_save (), i;
1015 rtx reg = 0;
1017 /* We have several options to restore FP registers. We could
1018 load them from SP offsets, but, if there are enough FP
1019 registers to restore, we win if we use a post-increment
1020 addressing mode. */
1022 /* If we have a frame pointer, it's the best option, because we
1023 already know it has the value we want. */
1024 if (frame_pointer_needed)
1025 reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
1026 /* Otherwise, we may use `a1', since it's call-clobbered and
1027 it's never used for return values. But only do so if it's
1028 smaller than using SP offsets. */
1029 else
1031 enum { restore_sp_post_adjust,
1032 restore_sp_pre_adjust,
1033 restore_sp_partial_adjust,
1034 restore_a1 } strategy;
1035 unsigned int this_strategy_size, strategy_size = (unsigned)-1;
1037 /* Consider using sp offsets before adjusting sp. */
1038 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1039 this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
1040 /* If size is too large, we'll have to adjust SP with an
1041 add. */
1042 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1044 /* Insn: add size + 4 * num_regs_to_save, sp. */
1045 this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
1047 /* If we don't have to restore any non-FP registers,
1048 we'll be able to save one byte by using rets. */
1049 if (! reg_save_bytes)
1050 this_strategy_size--;
1052 if (this_strategy_size < strategy_size)
1054 strategy = restore_sp_post_adjust;
1055 strategy_size = this_strategy_size;
1058 /* Consider using sp offsets after adjusting sp. */
1059 /* Insn: add size, sp. */
1060 this_strategy_size = SIZE_ADD_SP (size);
1061 /* Insn: fmov (##,sp),fs#, for each fs# to be restored. */
1062 this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1063 /* We're going to use ret to release the FP registers
1064 save area, so, no savings. */
1066 if (this_strategy_size < strategy_size)
1068 strategy = restore_sp_pre_adjust;
1069 strategy_size = this_strategy_size;
1072 /* Consider using sp offsets after partially adjusting sp.
1073 When size is close to 32Kb, we may be able to adjust SP
1074 with an imm16 add instruction while still using fmov
1075 (d8,sp). */
1076 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1078 /* Insn: add size + 4 * num_regs_to_save
1079 + reg_save_bytes - 252,sp. */
1080 this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
1081 + (int) reg_save_bytes - 252);
1082 /* Insn: fmov (##,sp),fs#, fo each fs# to be restored. */
1083 this_strategy_size += SIZE_FMOV_SP (252 - reg_save_bytes
1084 - 4 * num_regs_to_save,
1085 num_regs_to_save);
1086 /* We're going to use ret to release the FP registers
1087 save area, so, no savings. */
1089 if (this_strategy_size < strategy_size)
1091 strategy = restore_sp_partial_adjust;
1092 strategy_size = this_strategy_size;
1096 /* Consider using a1 in post-increment mode, as long as the
1097 user hasn't changed the calling conventions of a1. */
1098 if (call_really_used_regs [FIRST_ADDRESS_REGNUM + 1]
1099 && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1101 /* Insn: mov sp,a1. */
1102 this_strategy_size = 1;
1103 if (size)
1105 /* Insn: add size,a1. */
1106 this_strategy_size += SIZE_ADD_AX (size);
1108 /* Insn: fmov (a1+),fs#, for each fs# to be restored. */
1109 this_strategy_size += 3 * num_regs_to_save;
1110 /* If size is large enough, we may be able to save a
1111 couple of bytes. */
1112 if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1114 /* Insn: mov a1,sp. */
1115 this_strategy_size += 2;
1117 /* If we don't have to restore any non-FP registers,
1118 we'll be able to save one byte by using rets. */
1119 if (! reg_save_bytes)
1120 this_strategy_size--;
1122 if (this_strategy_size < strategy_size)
1124 strategy = restore_a1;
1125 strategy_size = this_strategy_size;
1129 switch (strategy)
1131 case restore_sp_post_adjust:
1132 break;
1134 case restore_sp_pre_adjust:
1135 emit_insn (gen_addsi3 (stack_pointer_rtx,
1136 stack_pointer_rtx,
1137 GEN_INT (size)));
1138 size = 0;
1139 break;
1141 case restore_sp_partial_adjust:
1142 emit_insn (gen_addsi3 (stack_pointer_rtx,
1143 stack_pointer_rtx,
1144 GEN_INT (size + 4 * num_regs_to_save
1145 + reg_save_bytes - 252)));
1146 size = 252 - reg_save_bytes - 4 * num_regs_to_save;
1147 break;
1149 case restore_a1:
1150 reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1151 emit_insn (gen_movsi (reg, stack_pointer_rtx));
1152 if (size)
1153 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1154 break;
1156 default:
1157 gcc_unreachable ();
1161 /* Adjust the selected register, if any, for post-increment. */
1162 if (reg)
1163 reg = gen_rtx_POST_INC (SImode, reg);
1165 for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1166 if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
1168 rtx addr;
1170 if (reg)
1171 addr = reg;
1172 else if (size)
1174 /* If we aren't using a post-increment register, use an
1175 SP offset. */
1176 addr = gen_rtx_PLUS (SImode,
1177 stack_pointer_rtx,
1178 GEN_INT (size));
1180 else
1181 addr = stack_pointer_rtx;
1183 size += 4;
1185 emit_insn (gen_movsf (gen_rtx_REG (SFmode, i),
1186 gen_rtx_MEM (SFmode, addr)));
1189 /* If we were using the restore_a1 strategy and the number of
1190 bytes to be released won't fit in the `ret' byte, copy `a1'
1191 to `sp', to avoid having to use `add' to adjust it. */
1192 if (! frame_pointer_needed && reg && size + reg_save_bytes > 255)
1194 emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1195 size = 0;
1199 /* Maybe cut back the stack, except for the register save area.
1201 If the frame pointer exists, then use the frame pointer to
1202 cut back the stack.
1204 If the stack size + register save area is more than 255 bytes,
1205 then the stack must be cut back here since the size + register
1206 save size is too big for a ret/retf instruction.
1208 Else leave it alone, it will be cut back as part of the
1209 ret/retf instruction, or there wasn't any stack to begin with.
1211 Under no circumstances should the register save area be
1212 deallocated here, that would leave a window where an interrupt
1213 could occur and trash the register save area. */
1214 if (frame_pointer_needed)
1216 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1217 size = 0;
1219 else if (size + reg_save_bytes > 255)
1221 emit_insn (gen_addsi3 (stack_pointer_rtx,
1222 stack_pointer_rtx,
1223 GEN_INT (size)));
1224 size = 0;
1227 /* Adjust the stack and restore callee-saved registers, if any. */
1228 if (mn10300_can_use_rets_insn ())
1229 emit_jump_insn (ret_rtx);
1230 else
1231 emit_jump_insn (gen_return_ret (GEN_INT (size + reg_save_bytes)));
1234 /* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
1235 This function is for MATCH_PARALLEL and so assumes OP is known to be
1236 parallel. If OP is a multiple store, return a mask indicating which
1237 registers it saves. Return 0 otherwise. */
1240 mn10300_store_multiple_operation (rtx op,
1241 enum machine_mode mode ATTRIBUTE_UNUSED)
1243 int count;
1244 int mask;
1245 int i;
1246 unsigned int last;
1247 rtx elt;
1249 count = XVECLEN (op, 0);
1250 if (count < 2)
1251 return 0;
1253 /* Check that first instruction has the form (set (sp) (plus A B)) */
1254 elt = XVECEXP (op, 0, 0);
1255 if (GET_CODE (elt) != SET
1256 || (! REG_P (SET_DEST (elt)))
1257 || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1258 || GET_CODE (SET_SRC (elt)) != PLUS)
1259 return 0;
1261 /* Check that A is the stack pointer and B is the expected stack size.
1262 For OP to match, each subsequent instruction should push a word onto
1263 the stack. We therefore expect the first instruction to create
1264 COUNT-1 stack slots. */
1265 elt = SET_SRC (elt);
1266 if ((! REG_P (XEXP (elt, 0)))
1267 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1268 || (! CONST_INT_P (XEXP (elt, 1)))
1269 || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1270 return 0;
1272 mask = 0;
1273 for (i = 1; i < count; i++)
1275 /* Check that element i is a (set (mem M) R). */
1276 /* ??? Validate the register order a-la mn10300_gen_multiple_store.
1277 Remember: the ordering is *not* monotonic. */
1278 elt = XVECEXP (op, 0, i);
1279 if (GET_CODE (elt) != SET
1280 || (! MEM_P (SET_DEST (elt)))
1281 || (! REG_P (SET_SRC (elt))))
1282 return 0;
1284 /* Remember which registers are to be saved. */
1285 last = REGNO (SET_SRC (elt));
1286 mask |= (1 << last);
1288 /* Check that M has the form (plus (sp) (const_int -I*4)) */
1289 elt = XEXP (SET_DEST (elt), 0);
1290 if (GET_CODE (elt) != PLUS
1291 || (! REG_P (XEXP (elt, 0)))
1292 || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1293 || (! CONST_INT_P (XEXP (elt, 1)))
1294 || INTVAL (XEXP (elt, 1)) != -i * 4)
1295 return 0;
1298 /* All or none of the callee-saved extended registers must be in the set. */
1299 if ((mask & 0x3c000) != 0
1300 && (mask & 0x3c000) != 0x3c000)
1301 return 0;
1303 return mask;
1306 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
1308 static reg_class_t
1309 mn10300_preferred_reload_class (rtx x, reg_class_t rclass)
1311 if (x == stack_pointer_rtx && rclass != SP_REGS)
1312 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1313 else if (MEM_P (x)
1314 || (REG_P (x)
1315 && !HARD_REGISTER_P (x))
1316 || (GET_CODE (x) == SUBREG
1317 && REG_P (SUBREG_REG (x))
1318 && !HARD_REGISTER_P (SUBREG_REG (x))))
1319 return LIMIT_RELOAD_CLASS (GET_MODE (x), rclass);
1320 else
1321 return rclass;
1324 /* Implement TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
1326 static reg_class_t
1327 mn10300_preferred_output_reload_class (rtx x, reg_class_t rclass)
1329 if (x == stack_pointer_rtx && rclass != SP_REGS)
1330 return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1331 return rclass;
1334 /* Implement TARGET_SECONDARY_RELOAD. */
1336 static reg_class_t
1337 mn10300_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
1338 enum machine_mode mode, secondary_reload_info *sri)
1340 enum reg_class rclass = (enum reg_class) rclass_i;
1341 enum reg_class xclass = NO_REGS;
1342 unsigned int xregno = INVALID_REGNUM;
1344 if (REG_P (x))
1346 xregno = REGNO (x);
1347 if (xregno >= FIRST_PSEUDO_REGISTER)
1348 xregno = true_regnum (x);
1349 if (xregno != INVALID_REGNUM)
1350 xclass = REGNO_REG_CLASS (xregno);
1353 if (!TARGET_AM33)
1355 /* Memory load/stores less than a full word wide can't have an
1356 address or stack pointer destination. They must use a data
1357 register as an intermediate register. */
1358 if (rclass != DATA_REGS
1359 && (mode == QImode || mode == HImode)
1360 && xclass == NO_REGS)
1361 return DATA_REGS;
1363 /* We can only move SP to/from an address register. */
1364 if (in_p
1365 && rclass == SP_REGS
1366 && xclass != ADDRESS_REGS)
1367 return ADDRESS_REGS;
1368 if (!in_p
1369 && xclass == SP_REGS
1370 && rclass != ADDRESS_REGS
1371 && rclass != SP_OR_ADDRESS_REGS)
1372 return ADDRESS_REGS;
1375 /* We can't directly load sp + const_int into a register;
1376 we must use an address register as an scratch. */
1377 if (in_p
1378 && rclass != SP_REGS
1379 && rclass != SP_OR_ADDRESS_REGS
1380 && rclass != SP_OR_GENERAL_REGS
1381 && GET_CODE (x) == PLUS
1382 && (XEXP (x, 0) == stack_pointer_rtx
1383 || XEXP (x, 1) == stack_pointer_rtx))
1385 sri->icode = CODE_FOR_reload_plus_sp_const;
1386 return NO_REGS;
1389 /* We can only move MDR to/from a data register. */
1390 if (rclass == MDR_REGS && xclass != DATA_REGS)
1391 return DATA_REGS;
1392 if (xclass == MDR_REGS && rclass != DATA_REGS)
1393 return DATA_REGS;
1395 /* We can't load/store an FP register from a constant address. */
1396 if (TARGET_AM33_2
1397 && (rclass == FP_REGS || xclass == FP_REGS)
1398 && (xclass == NO_REGS || rclass == NO_REGS))
1400 rtx addr = NULL;
1402 if (xregno >= FIRST_PSEUDO_REGISTER && xregno != INVALID_REGNUM)
1404 addr = reg_equiv_mem (xregno);
1405 if (addr)
1406 addr = XEXP (addr, 0);
1408 else if (MEM_P (x))
1409 addr = XEXP (x, 0);
1411 if (addr && CONSTANT_ADDRESS_P (addr))
1412 return GENERAL_REGS;
1415 /* Otherwise assume no secondary reloads are needed. */
1416 return NO_REGS;
1420 mn10300_frame_size (void)
1422 /* size includes the fixed stack space needed for function calls. */
1423 int size = get_frame_size () + crtl->outgoing_args_size;
1425 /* And space for the return pointer. */
1426 size += crtl->outgoing_args_size ? 4 : 0;
1428 return size;
1432 mn10300_initial_offset (int from, int to)
1434 int diff = 0;
1436 gcc_assert (from == ARG_POINTER_REGNUM || from == FRAME_POINTER_REGNUM);
1437 gcc_assert (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
1439 if (to == STACK_POINTER_REGNUM)
1440 diff = mn10300_frame_size ();
1442 /* The difference between the argument pointer and the frame pointer
1443 is the size of the callee register save area. */
1444 if (from == ARG_POINTER_REGNUM)
1446 unsigned int reg_save_bytes;
1448 mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1449 diff += reg_save_bytes;
1450 diff += 4 * fp_regs_to_save ();
1453 return diff;
1456 /* Worker function for TARGET_RETURN_IN_MEMORY. */
1458 static bool
1459 mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1461 /* Return values > 8 bytes in length in memory. */
1462 return (int_size_in_bytes (type) > 8
1463 || int_size_in_bytes (type) == 0
1464 || TYPE_MODE (type) == BLKmode);
1467 /* Flush the argument registers to the stack for a stdarg function;
1468 return the new argument pointer. */
1469 static rtx
1470 mn10300_builtin_saveregs (void)
1472 rtx offset, mem;
1473 tree fntype = TREE_TYPE (current_function_decl);
1474 int argadj = ((!stdarg_p (fntype))
1475 ? UNITS_PER_WORD : 0);
1476 alias_set_type set = get_varargs_alias_set ();
1478 if (argadj)
1479 offset = plus_constant (Pmode, crtl->args.arg_offset_rtx, argadj);
1480 else
1481 offset = crtl->args.arg_offset_rtx;
1483 mem = gen_rtx_MEM (SImode, crtl->args.internal_arg_pointer);
1484 set_mem_alias_set (mem, set);
1485 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1487 mem = gen_rtx_MEM (SImode,
1488 plus_constant (Pmode,
1489 crtl->args.internal_arg_pointer, 4));
1490 set_mem_alias_set (mem, set);
1491 emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1493 return copy_to_reg (expand_binop (Pmode, add_optab,
1494 crtl->args.internal_arg_pointer,
1495 offset, 0, 0, OPTAB_LIB_WIDEN));
1498 static void
1499 mn10300_va_start (tree valist, rtx nextarg)
1501 nextarg = expand_builtin_saveregs ();
1502 std_expand_builtin_va_start (valist, nextarg);
1505 /* Return true when a parameter should be passed by reference. */
1507 static bool
1508 mn10300_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
1509 enum machine_mode mode, const_tree type,
1510 bool named ATTRIBUTE_UNUSED)
1512 unsigned HOST_WIDE_INT size;
1514 if (type)
1515 size = int_size_in_bytes (type);
1516 else
1517 size = GET_MODE_SIZE (mode);
1519 return (size > 8 || size == 0);
1522 /* Return an RTX to represent where a value with mode MODE will be returned
1523 from a function. If the result is NULL_RTX, the argument is pushed. */
1525 static rtx
1526 mn10300_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
1527 const_tree type, bool named ATTRIBUTE_UNUSED)
1529 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1530 rtx result = NULL_RTX;
1531 int size;
1533 /* We only support using 2 data registers as argument registers. */
1534 int nregs = 2;
1536 /* Figure out the size of the object to be passed. */
1537 if (mode == BLKmode)
1538 size = int_size_in_bytes (type);
1539 else
1540 size = GET_MODE_SIZE (mode);
1542 cum->nbytes = (cum->nbytes + 3) & ~3;
1544 /* Don't pass this arg via a register if all the argument registers
1545 are used up. */
1546 if (cum->nbytes > nregs * UNITS_PER_WORD)
1547 return result;
1549 /* Don't pass this arg via a register if it would be split between
1550 registers and memory. */
1551 if (type == NULL_TREE
1552 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1553 return result;
1555 switch (cum->nbytes / UNITS_PER_WORD)
1557 case 0:
1558 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM);
1559 break;
1560 case 1:
1561 result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM + 1);
1562 break;
1563 default:
1564 break;
1567 return result;
1570 /* Update the data in CUM to advance over an argument
1571 of mode MODE and data type TYPE.
1572 (TYPE is null for libcalls where that information may not be available.) */
1574 static void
1575 mn10300_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
1576 const_tree type, bool named ATTRIBUTE_UNUSED)
1578 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1580 cum->nbytes += (mode != BLKmode
1581 ? (GET_MODE_SIZE (mode) + 3) & ~3
1582 : (int_size_in_bytes (type) + 3) & ~3);
1585 /* Return the number of bytes of registers to use for an argument passed
1586 partially in registers and partially in memory. */
1588 static int
1589 mn10300_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
1590 tree type, bool named ATTRIBUTE_UNUSED)
1592 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1593 int size;
1595 /* We only support using 2 data registers as argument registers. */
1596 int nregs = 2;
1598 /* Figure out the size of the object to be passed. */
1599 if (mode == BLKmode)
1600 size = int_size_in_bytes (type);
1601 else
1602 size = GET_MODE_SIZE (mode);
1604 cum->nbytes = (cum->nbytes + 3) & ~3;
1606 /* Don't pass this arg via a register if all the argument registers
1607 are used up. */
1608 if (cum->nbytes > nregs * UNITS_PER_WORD)
1609 return 0;
1611 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1612 return 0;
1614 /* Don't pass this arg via a register if it would be split between
1615 registers and memory. */
1616 if (type == NULL_TREE
1617 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1618 return 0;
1620 return nregs * UNITS_PER_WORD - cum->nbytes;
1623 /* Return the location of the function's value. This will be either
1624 $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1625 $d0 and $a0 if the -mreturn-pointer-on-do flag is set. Note that
1626 we only return the PARALLEL for outgoing values; we do not want
1627 callers relying on this extra copy. */
1629 static rtx
1630 mn10300_function_value (const_tree valtype,
1631 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1632 bool outgoing)
1634 rtx rv;
1635 enum machine_mode mode = TYPE_MODE (valtype);
1637 if (! POINTER_TYPE_P (valtype))
1638 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1639 else if (! TARGET_PTR_A0D0 || ! outgoing
1640 || cfun->returns_struct)
1641 return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1643 rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1644 XVECEXP (rv, 0, 0)
1645 = gen_rtx_EXPR_LIST (VOIDmode,
1646 gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1647 GEN_INT (0));
1649 XVECEXP (rv, 0, 1)
1650 = gen_rtx_EXPR_LIST (VOIDmode,
1651 gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1652 GEN_INT (0));
1653 return rv;
1656 /* Implements TARGET_LIBCALL_VALUE. */
1658 static rtx
1659 mn10300_libcall_value (enum machine_mode mode,
1660 const_rtx fun ATTRIBUTE_UNUSED)
1662 return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1665 /* Implements FUNCTION_VALUE_REGNO_P. */
1667 bool
1668 mn10300_function_value_regno_p (const unsigned int regno)
1670 return (regno == FIRST_DATA_REGNUM || regno == FIRST_ADDRESS_REGNUM);
1673 /* Output an addition operation. */
1675 const char *
1676 mn10300_output_add (rtx operands[3], bool need_flags)
1678 rtx dest, src1, src2;
1679 unsigned int dest_regnum, src1_regnum, src2_regnum;
1680 enum reg_class src1_class, src2_class, dest_class;
1682 dest = operands[0];
1683 src1 = operands[1];
1684 src2 = operands[2];
1686 dest_regnum = true_regnum (dest);
1687 src1_regnum = true_regnum (src1);
1689 dest_class = REGNO_REG_CLASS (dest_regnum);
1690 src1_class = REGNO_REG_CLASS (src1_regnum);
1692 if (CONST_INT_P (src2))
1694 gcc_assert (dest_regnum == src1_regnum);
1696 if (src2 == const1_rtx && !need_flags)
1697 return "inc %0";
1698 if (INTVAL (src2) == 4 && !need_flags && dest_class != DATA_REGS)
1699 return "inc4 %0";
1701 gcc_assert (!need_flags || dest_class != SP_REGS);
1702 return "add %2,%0";
1704 else if (CONSTANT_P (src2))
1705 return "add %2,%0";
1707 src2_regnum = true_regnum (src2);
1708 src2_class = REGNO_REG_CLASS (src2_regnum);
1710 if (dest_regnum == src1_regnum)
1711 return "add %2,%0";
1712 if (dest_regnum == src2_regnum)
1713 return "add %1,%0";
1715 /* The rest of the cases are reg = reg+reg. For AM33, we can implement
1716 this directly, as below, but when optimizing for space we can sometimes
1717 do better by using a mov+add. For MN103, we claimed that we could
1718 implement a three-operand add because the various move and add insns
1719 change sizes across register classes, and we can often do better than
1720 reload in choosing which operand to move. */
1721 if (TARGET_AM33 && optimize_insn_for_speed_p ())
1722 return "add %2,%1,%0";
1724 /* Catch cases where no extended register was used. */
1725 if (src1_class != EXTENDED_REGS
1726 && src2_class != EXTENDED_REGS
1727 && dest_class != EXTENDED_REGS)
1729 /* We have to copy one of the sources into the destination, then
1730 add the other source to the destination.
1732 Carefully select which source to copy to the destination; a
1733 naive implementation will waste a byte when the source classes
1734 are different and the destination is an address register.
1735 Selecting the lowest cost register copy will optimize this
1736 sequence. */
1737 if (src1_class == dest_class)
1738 return "mov %1,%0\n\tadd %2,%0";
1739 else
1740 return "mov %2,%0\n\tadd %1,%0";
1743 /* At least one register is an extended register. */
1745 /* The three operand add instruction on the am33 is a win iff the
1746 output register is an extended register, or if both source
1747 registers are extended registers. */
1748 if (dest_class == EXTENDED_REGS || src1_class == src2_class)
1749 return "add %2,%1,%0";
1751 /* It is better to copy one of the sources to the destination, then
1752 perform a 2 address add. The destination in this case must be
1753 an address or data register and one of the sources must be an
1754 extended register and the remaining source must not be an extended
1755 register.
1757 The best code for this case is to copy the extended reg to the
1758 destination, then emit a two address add. */
1759 if (src1_class == EXTENDED_REGS)
1760 return "mov %1,%0\n\tadd %2,%0";
1761 else
1762 return "mov %2,%0\n\tadd %1,%0";
1765 /* Return 1 if X contains a symbolic expression. We know these
1766 expressions will have one of a few well defined forms, so
1767 we need only check those forms. */
1770 mn10300_symbolic_operand (rtx op,
1771 enum machine_mode mode ATTRIBUTE_UNUSED)
1773 switch (GET_CODE (op))
1775 case SYMBOL_REF:
1776 case LABEL_REF:
1777 return 1;
1778 case CONST:
1779 op = XEXP (op, 0);
1780 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1781 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1782 && CONST_INT_P (XEXP (op, 1)));
1783 default:
1784 return 0;
1788 /* Try machine dependent ways of modifying an illegitimate address
1789 to be legitimate. If we find one, return the new valid address.
1790 This macro is used in only one place: `memory_address' in explow.c.
1792 OLDX is the address as it was before break_out_memory_refs was called.
1793 In some cases it is useful to look at this to decide what needs to be done.
1795 Normally it is always safe for this macro to do nothing. It exists to
1796 recognize opportunities to optimize the output.
1798 But on a few ports with segmented architectures and indexed addressing
1799 (mn10300, hppa) it is used to rewrite certain problematical addresses. */
1801 static rtx
1802 mn10300_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1803 enum machine_mode mode ATTRIBUTE_UNUSED)
1805 if (flag_pic && ! mn10300_legitimate_pic_operand_p (x))
1806 x = mn10300_legitimize_pic_address (oldx, NULL_RTX);
1808 /* Uh-oh. We might have an address for x[n-100000]. This needs
1809 special handling to avoid creating an indexed memory address
1810 with x-100000 as the base. */
1811 if (GET_CODE (x) == PLUS
1812 && mn10300_symbolic_operand (XEXP (x, 1), VOIDmode))
1814 /* Ugly. We modify things here so that the address offset specified
1815 by the index expression is computed first, then added to x to form
1816 the entire address. */
1818 rtx regx1, regy1, regy2, y;
1820 /* Strip off any CONST. */
1821 y = XEXP (x, 1);
1822 if (GET_CODE (y) == CONST)
1823 y = XEXP (y, 0);
1825 if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1827 regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1828 regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1829 regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1830 regx1 = force_reg (Pmode,
1831 gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1,
1832 regy2));
1833 return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1836 return x;
1839 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
1840 @GOTOFF in `reg'. */
1843 mn10300_legitimize_pic_address (rtx orig, rtx reg)
1845 rtx x;
1847 if (GET_CODE (orig) == LABEL_REF
1848 || (GET_CODE (orig) == SYMBOL_REF
1849 && (CONSTANT_POOL_ADDRESS_P (orig)
1850 || ! MN10300_GLOBAL_P (orig))))
1852 if (reg == NULL)
1853 reg = gen_reg_rtx (Pmode);
1855 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOTOFF);
1856 x = gen_rtx_CONST (SImode, x);
1857 emit_move_insn (reg, x);
1859 x = emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
1861 else if (GET_CODE (orig) == SYMBOL_REF)
1863 if (reg == NULL)
1864 reg = gen_reg_rtx (Pmode);
1866 x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOT);
1867 x = gen_rtx_CONST (SImode, x);
1868 x = gen_rtx_PLUS (SImode, pic_offset_table_rtx, x);
1869 x = gen_const_mem (SImode, x);
1871 x = emit_move_insn (reg, x);
1873 else
1874 return orig;
1876 set_unique_reg_note (x, REG_EQUAL, orig);
1877 return reg;
1880 /* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
1881 isn't protected by a PIC unspec; nonzero otherwise. */
1884 mn10300_legitimate_pic_operand_p (rtx x)
1886 const char *fmt;
1887 int i;
1889 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1890 return 0;
1892 if (GET_CODE (x) == UNSPEC
1893 && (XINT (x, 1) == UNSPEC_PIC
1894 || XINT (x, 1) == UNSPEC_GOT
1895 || XINT (x, 1) == UNSPEC_GOTOFF
1896 || XINT (x, 1) == UNSPEC_PLT
1897 || XINT (x, 1) == UNSPEC_GOTSYM_OFF))
1898 return 1;
1900 fmt = GET_RTX_FORMAT (GET_CODE (x));
1901 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1903 if (fmt[i] == 'E')
1905 int j;
1907 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1908 if (! mn10300_legitimate_pic_operand_p (XVECEXP (x, i, j)))
1909 return 0;
1911 else if (fmt[i] == 'e'
1912 && ! mn10300_legitimate_pic_operand_p (XEXP (x, i)))
1913 return 0;
1916 return 1;
1919 /* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1920 legitimate, and FALSE otherwise.
1922 On the mn10300, the value in the address register must be
1923 in the same memory space/segment as the effective address.
1925 This is problematical for reload since it does not understand
1926 that base+index != index+base in a memory reference.
1928 Note it is still possible to use reg+reg addressing modes,
1929 it's just much more difficult. For a discussion of a possible
1930 workaround and solution, see the comments in pa.c before the
1931 function record_unscaled_index_insn_codes. */
1933 static bool
1934 mn10300_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1936 rtx base, index;
1938 if (CONSTANT_ADDRESS_P (x))
1939 return !flag_pic || mn10300_legitimate_pic_operand_p (x);
1941 if (RTX_OK_FOR_BASE_P (x, strict))
1942 return true;
1944 if (TARGET_AM33 && (mode == SImode || mode == SFmode || mode == HImode))
1946 if (GET_CODE (x) == POST_INC)
1947 return RTX_OK_FOR_BASE_P (XEXP (x, 0), strict);
1948 if (GET_CODE (x) == POST_MODIFY)
1949 return (RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1950 && CONSTANT_ADDRESS_P (XEXP (x, 1)));
1953 if (GET_CODE (x) != PLUS)
1954 return false;
1956 base = XEXP (x, 0);
1957 index = XEXP (x, 1);
1959 if (!REG_P (base))
1960 return false;
1961 if (REG_P (index))
1963 /* ??? Without AM33 generalized (Ri,Rn) addressing, reg+reg
1964 addressing is hard to satisfy. */
1965 if (!TARGET_AM33)
1966 return false;
1968 return (REGNO_GENERAL_P (REGNO (base), strict)
1969 && REGNO_GENERAL_P (REGNO (index), strict));
1972 if (!REGNO_STRICT_OK_FOR_BASE_P (REGNO (base), strict))
1973 return false;
1975 if (CONST_INT_P (index))
1976 return IN_RANGE (INTVAL (index), -1 - 0x7fffffff, 0x7fffffff);
1978 if (CONSTANT_ADDRESS_P (index))
1979 return !flag_pic || mn10300_legitimate_pic_operand_p (index);
1981 return false;
1984 bool
1985 mn10300_regno_in_class_p (unsigned regno, int rclass, bool strict)
1987 if (regno >= FIRST_PSEUDO_REGISTER)
1989 if (!strict)
1990 return true;
1991 if (!reg_renumber)
1992 return false;
1993 regno = reg_renumber[regno];
1994 if (regno == INVALID_REGNUM)
1995 return false;
1997 return TEST_HARD_REG_BIT (reg_class_contents[rclass], regno);
2001 mn10300_legitimize_reload_address (rtx x,
2002 enum machine_mode mode ATTRIBUTE_UNUSED,
2003 int opnum, int type,
2004 int ind_levels ATTRIBUTE_UNUSED)
2006 bool any_change = false;
2008 /* See above re disabling reg+reg addressing for MN103. */
2009 if (!TARGET_AM33)
2010 return NULL_RTX;
2012 if (GET_CODE (x) != PLUS)
2013 return NULL_RTX;
2015 if (XEXP (x, 0) == stack_pointer_rtx)
2017 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2018 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2019 opnum, (enum reload_type) type);
2020 any_change = true;
2022 if (XEXP (x, 1) == stack_pointer_rtx)
2024 push_reload (XEXP (x, 1), NULL_RTX, &XEXP (x, 1), NULL,
2025 GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2026 opnum, (enum reload_type) type);
2027 any_change = true;
2030 return any_change ? x : NULL_RTX;
2033 /* Implement TARGET_LEGITIMATE_CONSTANT_P. Returns TRUE if X is a valid
2034 constant. Note that some "constants" aren't valid, such as TLS
2035 symbols and unconverted GOT-based references, so we eliminate
2036 those here. */
2038 static bool
2039 mn10300_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2041 switch (GET_CODE (x))
2043 case CONST:
2044 x = XEXP (x, 0);
2046 if (GET_CODE (x) == PLUS)
2048 if (! CONST_INT_P (XEXP (x, 1)))
2049 return false;
2050 x = XEXP (x, 0);
2053 /* Only some unspecs are valid as "constants". */
2054 if (GET_CODE (x) == UNSPEC)
2056 switch (XINT (x, 1))
2058 case UNSPEC_PIC:
2059 case UNSPEC_GOT:
2060 case UNSPEC_GOTOFF:
2061 case UNSPEC_PLT:
2062 return true;
2063 default:
2064 return false;
2068 /* We must have drilled down to a symbol. */
2069 if (! mn10300_symbolic_operand (x, Pmode))
2070 return false;
2071 break;
2073 default:
2074 break;
2077 return true;
2080 /* Undo pic address legitimization for the benefit of debug info. */
2082 static rtx
2083 mn10300_delegitimize_address (rtx orig_x)
2085 rtx x = orig_x, ret, addend = NULL;
2086 bool need_mem;
2088 if (MEM_P (x))
2089 x = XEXP (x, 0);
2090 if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
2091 return orig_x;
2093 if (XEXP (x, 0) == pic_offset_table_rtx)
2095 /* With the REG+REG addressing of AM33, var-tracking can re-assemble
2096 some odd-looking "addresses" that were never valid in the first place.
2097 We need to look harder to avoid warnings being emitted. */
2098 else if (GET_CODE (XEXP (x, 0)) == PLUS)
2100 rtx x0 = XEXP (x, 0);
2101 rtx x00 = XEXP (x0, 0);
2102 rtx x01 = XEXP (x0, 1);
2104 if (x00 == pic_offset_table_rtx)
2105 addend = x01;
2106 else if (x01 == pic_offset_table_rtx)
2107 addend = x00;
2108 else
2109 return orig_x;
2112 else
2113 return orig_x;
2114 x = XEXP (x, 1);
2116 if (GET_CODE (x) != CONST)
2117 return orig_x;
2118 x = XEXP (x, 0);
2119 if (GET_CODE (x) != UNSPEC)
2120 return orig_x;
2122 ret = XVECEXP (x, 0, 0);
2123 if (XINT (x, 1) == UNSPEC_GOTOFF)
2124 need_mem = false;
2125 else if (XINT (x, 1) == UNSPEC_GOT)
2126 need_mem = true;
2127 else
2128 return orig_x;
2130 gcc_assert (GET_CODE (ret) == SYMBOL_REF);
2131 if (need_mem != MEM_P (orig_x))
2132 return orig_x;
2133 if (need_mem && addend)
2134 return orig_x;
2135 if (addend)
2136 ret = gen_rtx_PLUS (Pmode, addend, ret);
2137 return ret;
2140 /* For addresses, costs are relative to "MOV (Rm),Rn". For AM33 this is
2141 the 3-byte fully general instruction; for MN103 this is the 2-byte form
2142 with an address register. */
2144 static int
2145 mn10300_address_cost (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
2146 addr_space_t as ATTRIBUTE_UNUSED, bool speed)
2148 HOST_WIDE_INT i;
2149 rtx base, index;
2151 switch (GET_CODE (x))
2153 case CONST:
2154 case SYMBOL_REF:
2155 case LABEL_REF:
2156 /* We assume all of these require a 32-bit constant, even though
2157 some symbol and label references can be relaxed. */
2158 return speed ? 1 : 4;
2160 case REG:
2161 case SUBREG:
2162 case POST_INC:
2163 return 0;
2165 case POST_MODIFY:
2166 /* Assume any symbolic offset is a 32-bit constant. */
2167 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2168 if (IN_RANGE (i, -128, 127))
2169 return speed ? 0 : 1;
2170 if (speed)
2171 return 1;
2172 if (IN_RANGE (i, -0x800000, 0x7fffff))
2173 return 3;
2174 return 4;
2176 case PLUS:
2177 base = XEXP (x, 0);
2178 index = XEXP (x, 1);
2179 if (register_operand (index, SImode))
2181 /* Attempt to minimize the number of registers in the address.
2182 This is similar to what other ports do. */
2183 if (register_operand (base, SImode))
2184 return 1;
2186 base = XEXP (x, 1);
2187 index = XEXP (x, 0);
2190 /* Assume any symbolic offset is a 32-bit constant. */
2191 i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2192 if (IN_RANGE (i, -128, 127))
2193 return speed ? 0 : 1;
2194 if (IN_RANGE (i, -32768, 32767))
2195 return speed ? 0 : 2;
2196 return speed ? 2 : 6;
2198 default:
2199 return rtx_cost (x, MEM, 0, speed);
2203 /* Implement the TARGET_REGISTER_MOVE_COST hook.
2205 Recall that the base value of 2 is required by assumptions elsewhere
2206 in the body of the compiler, and that cost 2 is special-cased as an
2207 early exit from reload meaning no work is required. */
2209 static int
2210 mn10300_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
2211 reg_class_t ifrom, reg_class_t ito)
2213 enum reg_class from = (enum reg_class) ifrom;
2214 enum reg_class to = (enum reg_class) ito;
2215 enum reg_class scratch, test;
2217 /* Simplify the following code by unifying the fp register classes. */
2218 if (to == FP_ACC_REGS)
2219 to = FP_REGS;
2220 if (from == FP_ACC_REGS)
2221 from = FP_REGS;
2223 /* Diagnose invalid moves by costing them as two moves. */
2225 scratch = NO_REGS;
2226 test = from;
2227 if (to == SP_REGS)
2228 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2229 else if (to == MDR_REGS)
2230 scratch = DATA_REGS;
2231 else if (to == FP_REGS && to != from)
2232 scratch = GENERAL_REGS;
2233 else
2235 test = to;
2236 if (from == SP_REGS)
2237 scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2238 else if (from == MDR_REGS)
2239 scratch = DATA_REGS;
2240 else if (from == FP_REGS && to != from)
2241 scratch = GENERAL_REGS;
2243 if (scratch != NO_REGS && !reg_class_subset_p (test, scratch))
2244 return (mn10300_register_move_cost (VOIDmode, from, scratch)
2245 + mn10300_register_move_cost (VOIDmode, scratch, to));
2247 /* From here on, all we need consider are legal combinations. */
2249 if (optimize_size)
2251 /* The scale here is bytes * 2. */
2253 if (from == to && (to == ADDRESS_REGS || to == DATA_REGS))
2254 return 2;
2256 if (from == SP_REGS)
2257 return (to == ADDRESS_REGS ? 2 : 6);
2259 /* For MN103, all remaining legal moves are two bytes. */
2260 if (TARGET_AM33)
2261 return 4;
2263 if (to == SP_REGS)
2264 return (from == ADDRESS_REGS ? 4 : 6);
2266 if ((from == ADDRESS_REGS || from == DATA_REGS)
2267 && (to == ADDRESS_REGS || to == DATA_REGS))
2268 return 4;
2270 if (to == EXTENDED_REGS)
2271 return (to == from ? 6 : 4);
2273 /* What's left are SP_REGS, FP_REGS, or combinations of the above. */
2274 return 6;
2276 else
2278 /* The scale here is cycles * 2. */
2280 if (to == FP_REGS)
2281 return 8;
2282 if (from == FP_REGS)
2283 return 4;
2285 /* All legal moves between integral registers are single cycle. */
2286 return 2;
2290 /* Implement the TARGET_MEMORY_MOVE_COST hook.
2292 Given lack of the form of the address, this must be speed-relative,
2293 though we should never be less expensive than a size-relative register
2294 move cost above. This is not a problem. */
2296 static int
2297 mn10300_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
2298 reg_class_t iclass, bool in ATTRIBUTE_UNUSED)
2300 enum reg_class rclass = (enum reg_class) iclass;
2302 if (rclass == FP_REGS)
2303 return 8;
2304 return 6;
2307 /* Implement the TARGET_RTX_COSTS hook.
2309 Speed-relative costs are relative to COSTS_N_INSNS, which is intended
2310 to represent cycles. Size-relative costs are in bytes. */
2312 static bool
2313 mn10300_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
2314 int *ptotal, bool speed)
2316 /* This value is used for SYMBOL_REF etc where we want to pretend
2317 we have a full 32-bit constant. */
2318 HOST_WIDE_INT i = 0x12345678;
2319 int total;
2321 switch (code)
2323 case CONST_INT:
2324 i = INTVAL (x);
2325 do_int_costs:
2326 if (speed)
2328 if (outer_code == SET)
2330 /* 16-bit integer loads have latency 1, 32-bit loads 2. */
2331 if (IN_RANGE (i, -32768, 32767))
2332 total = COSTS_N_INSNS (1);
2333 else
2334 total = COSTS_N_INSNS (2);
2336 else
2338 /* 16-bit integer operands don't affect latency;
2339 24-bit and 32-bit operands add a cycle. */
2340 if (IN_RANGE (i, -32768, 32767))
2341 total = 0;
2342 else
2343 total = COSTS_N_INSNS (1);
2346 else
2348 if (outer_code == SET)
2350 if (i == 0)
2351 total = 1;
2352 else if (IN_RANGE (i, -128, 127))
2353 total = 2;
2354 else if (IN_RANGE (i, -32768, 32767))
2355 total = 3;
2356 else
2357 total = 6;
2359 else
2361 /* Reference here is ADD An,Dn, vs ADD imm,Dn. */
2362 if (IN_RANGE (i, -128, 127))
2363 total = 0;
2364 else if (IN_RANGE (i, -32768, 32767))
2365 total = 2;
2366 else if (TARGET_AM33 && IN_RANGE (i, -0x01000000, 0x00ffffff))
2367 total = 3;
2368 else
2369 total = 4;
2372 goto alldone;
2374 case CONST:
2375 case LABEL_REF:
2376 case SYMBOL_REF:
2377 case CONST_DOUBLE:
2378 /* We assume all of these require a 32-bit constant, even though
2379 some symbol and label references can be relaxed. */
2380 goto do_int_costs;
2382 case UNSPEC:
2383 switch (XINT (x, 1))
2385 case UNSPEC_PIC:
2386 case UNSPEC_GOT:
2387 case UNSPEC_GOTOFF:
2388 case UNSPEC_PLT:
2389 case UNSPEC_GOTSYM_OFF:
2390 /* The PIC unspecs also resolve to a 32-bit constant. */
2391 goto do_int_costs;
2393 default:
2394 /* Assume any non-listed unspec is some sort of arithmetic. */
2395 goto do_arith_costs;
2398 case PLUS:
2399 /* Notice the size difference of INC and INC4. */
2400 if (!speed && outer_code == SET && CONST_INT_P (XEXP (x, 1)))
2402 i = INTVAL (XEXP (x, 1));
2403 if (i == 1 || i == 4)
2405 total = 1 + rtx_cost (XEXP (x, 0), PLUS, 0, speed);
2406 goto alldone;
2409 goto do_arith_costs;
2411 case MINUS:
2412 case AND:
2413 case IOR:
2414 case XOR:
2415 case NOT:
2416 case NEG:
2417 case ZERO_EXTEND:
2418 case SIGN_EXTEND:
2419 case COMPARE:
2420 case BSWAP:
2421 case CLZ:
2422 do_arith_costs:
2423 total = (speed ? COSTS_N_INSNS (1) : 2);
2424 break;
2426 case ASHIFT:
2427 /* Notice the size difference of ASL2 and variants. */
2428 if (!speed && CONST_INT_P (XEXP (x, 1)))
2429 switch (INTVAL (XEXP (x, 1)))
2431 case 1:
2432 case 2:
2433 total = 1;
2434 goto alldone;
2435 case 3:
2436 case 4:
2437 total = 2;
2438 goto alldone;
2440 /* FALLTHRU */
2442 case ASHIFTRT:
2443 case LSHIFTRT:
2444 total = (speed ? COSTS_N_INSNS (1) : 3);
2445 goto alldone;
2447 case MULT:
2448 total = (speed ? COSTS_N_INSNS (3) : 2);
2449 break;
2451 case DIV:
2452 case UDIV:
2453 case MOD:
2454 case UMOD:
2455 total = (speed ? COSTS_N_INSNS (39)
2456 /* Include space to load+retrieve MDR. */
2457 : code == MOD || code == UMOD ? 6 : 4);
2458 break;
2460 case MEM:
2461 total = mn10300_address_cost (XEXP (x, 0), GET_MODE (x),
2462 MEM_ADDR_SPACE (x), speed);
2463 if (speed)
2464 total = COSTS_N_INSNS (2 + total);
2465 goto alldone;
2467 default:
2468 /* Probably not implemented. Assume external call. */
2469 total = (speed ? COSTS_N_INSNS (10) : 7);
2470 break;
2473 *ptotal = total;
2474 return false;
2476 alldone:
2477 *ptotal = total;
2478 return true;
2481 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2482 may access it using GOTOFF instead of GOT. */
2484 static void
2485 mn10300_encode_section_info (tree decl, rtx rtl, int first)
2487 rtx symbol;
2489 default_encode_section_info (decl, rtl, first);
2491 if (! MEM_P (rtl))
2492 return;
2494 symbol = XEXP (rtl, 0);
2495 if (GET_CODE (symbol) != SYMBOL_REF)
2496 return;
2498 if (flag_pic)
2499 SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2502 /* Dispatch tables on the mn10300 are extremely expensive in terms of code
2503 and readonly data size. So we crank up the case threshold value to
2504 encourage a series of if/else comparisons to implement many small switch
2505 statements. In theory, this value could be increased much more if we
2506 were solely optimizing for space, but we keep it "reasonable" to avoid
2507 serious code efficiency lossage. */
2509 static unsigned int
2510 mn10300_case_values_threshold (void)
2512 return 6;
2515 /* Worker function for TARGET_TRAMPOLINE_INIT. */
2517 static void
2518 mn10300_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2520 rtx mem, disp, fnaddr = XEXP (DECL_RTL (fndecl), 0);
2522 /* This is a strict alignment target, which means that we play
2523 some games to make sure that the locations at which we need
2524 to store <chain> and <disp> wind up at aligned addresses.
2526 0x28 0x00 add 0,d0
2527 0xfc 0xdd mov chain,a1
2528 <chain>
2529 0xf8 0xed 0x00 btst 0,d1
2530 0xdc jmp fnaddr
2531 <disp>
2533 Note that the two extra insns are effectively nops; they
2534 clobber the flags but do not affect the contents of D0 or D1. */
2536 disp = expand_binop (SImode, sub_optab, fnaddr,
2537 plus_constant (Pmode, XEXP (m_tramp, 0), 11),
2538 NULL_RTX, 1, OPTAB_DIRECT);
2540 mem = adjust_address (m_tramp, SImode, 0);
2541 emit_move_insn (mem, gen_int_mode (0xddfc0028, SImode));
2542 mem = adjust_address (m_tramp, SImode, 4);
2543 emit_move_insn (mem, chain_value);
2544 mem = adjust_address (m_tramp, SImode, 8);
2545 emit_move_insn (mem, gen_int_mode (0xdc00edf8, SImode));
2546 mem = adjust_address (m_tramp, SImode, 12);
2547 emit_move_insn (mem, disp);
2550 /* Output the assembler code for a C++ thunk function.
2551 THUNK_DECL is the declaration for the thunk function itself, FUNCTION
2552 is the decl for the target function. DELTA is an immediate constant
2553 offset to be added to the THIS parameter. If VCALL_OFFSET is nonzero
2554 the word at the adjusted address *(*THIS' + VCALL_OFFSET) should be
2555 additionally added to THIS. Finally jump to the entry point of
2556 FUNCTION. */
2558 static void
2559 mn10300_asm_output_mi_thunk (FILE * file,
2560 tree thunk_fndecl ATTRIBUTE_UNUSED,
2561 HOST_WIDE_INT delta,
2562 HOST_WIDE_INT vcall_offset,
2563 tree function)
2565 const char * _this;
2567 /* Get the register holding the THIS parameter. Handle the case
2568 where there is a hidden first argument for a returned structure. */
2569 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2570 _this = reg_names [FIRST_ARGUMENT_REGNUM + 1];
2571 else
2572 _this = reg_names [FIRST_ARGUMENT_REGNUM];
2574 fprintf (file, "\t%s Thunk Entry Point:\n", ASM_COMMENT_START);
2576 if (delta)
2577 fprintf (file, "\tadd %d, %s\n", (int) delta, _this);
2579 if (vcall_offset)
2581 const char * scratch = reg_names [FIRST_ADDRESS_REGNUM + 1];
2583 fprintf (file, "\tmov %s, %s\n", _this, scratch);
2584 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2585 fprintf (file, "\tadd %d, %s\n", (int) vcall_offset, scratch);
2586 fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2587 fprintf (file, "\tadd %s, %s\n", scratch, _this);
2590 fputs ("\tjmp ", file);
2591 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2592 putc ('\n', file);
2595 /* Return true if mn10300_output_mi_thunk would be able to output the
2596 assembler code for the thunk function specified by the arguments
2597 it is passed, and false otherwise. */
2599 static bool
2600 mn10300_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
2601 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
2602 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2603 const_tree function ATTRIBUTE_UNUSED)
2605 return true;
2608 bool
2609 mn10300_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
2611 if (REGNO_REG_CLASS (regno) == FP_REGS
2612 || REGNO_REG_CLASS (regno) == FP_ACC_REGS)
2613 /* Do not store integer values in FP registers. */
2614 return GET_MODE_CLASS (mode) == MODE_FLOAT && ((regno & 1) == 0);
2616 if (((regno) & 1) == 0 || GET_MODE_SIZE (mode) == 4)
2617 return true;
2619 if (REGNO_REG_CLASS (regno) == DATA_REGS
2620 || (TARGET_AM33 && REGNO_REG_CLASS (regno) == ADDRESS_REGS)
2621 || REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2622 return GET_MODE_SIZE (mode) <= 4;
2624 return false;
2627 bool
2628 mn10300_modes_tieable (enum machine_mode mode1, enum machine_mode mode2)
2630 if (GET_MODE_CLASS (mode1) == MODE_FLOAT
2631 && GET_MODE_CLASS (mode2) != MODE_FLOAT)
2632 return false;
2634 if (GET_MODE_CLASS (mode2) == MODE_FLOAT
2635 && GET_MODE_CLASS (mode1) != MODE_FLOAT)
2636 return false;
2638 if (TARGET_AM33
2639 || mode1 == mode2
2640 || (GET_MODE_SIZE (mode1) <= 4 && GET_MODE_SIZE (mode2) <= 4))
2641 return true;
2643 return false;
2646 static int
2647 cc_flags_for_mode (enum machine_mode mode)
2649 switch (mode)
2651 case CCmode:
2652 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C | CC_FLAG_V;
2653 case CCZNCmode:
2654 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C;
2655 case CCZNmode:
2656 return CC_FLAG_Z | CC_FLAG_N;
2657 case CC_FLOATmode:
2658 return -1;
2659 default:
2660 gcc_unreachable ();
2664 static int
2665 cc_flags_for_code (enum rtx_code code)
2667 switch (code)
2669 case EQ: /* Z */
2670 case NE: /* ~Z */
2671 return CC_FLAG_Z;
2673 case LT: /* N */
2674 case GE: /* ~N */
2675 return CC_FLAG_N;
2676 break;
2678 case GT: /* ~(Z|(N^V)) */
2679 case LE: /* Z|(N^V) */
2680 return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_V;
2682 case GEU: /* ~C */
2683 case LTU: /* C */
2684 return CC_FLAG_C;
2686 case GTU: /* ~(C | Z) */
2687 case LEU: /* C | Z */
2688 return CC_FLAG_Z | CC_FLAG_C;
2690 case ORDERED:
2691 case UNORDERED:
2692 case LTGT:
2693 case UNEQ:
2694 case UNGE:
2695 case UNGT:
2696 case UNLE:
2697 case UNLT:
2698 return -1;
2700 default:
2701 gcc_unreachable ();
2705 enum machine_mode
2706 mn10300_select_cc_mode (enum rtx_code code, rtx x, rtx y ATTRIBUTE_UNUSED)
2708 int req;
2710 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2711 return CC_FLOATmode;
2713 req = cc_flags_for_code (code);
2715 if (req & CC_FLAG_V)
2716 return CCmode;
2717 if (req & CC_FLAG_C)
2718 return CCZNCmode;
2719 return CCZNmode;
2722 static inline bool
2723 is_load_insn (rtx insn)
2725 if (GET_CODE (PATTERN (insn)) != SET)
2726 return false;
2728 return MEM_P (SET_SRC (PATTERN (insn)));
2731 static inline bool
2732 is_store_insn (rtx insn)
2734 if (GET_CODE (PATTERN (insn)) != SET)
2735 return false;
2737 return MEM_P (SET_DEST (PATTERN (insn)));
2740 /* Update scheduling costs for situations that cannot be
2741 described using the attributes and DFA machinery.
2742 DEP is the insn being scheduled.
2743 INSN is the previous insn.
2744 COST is the current cycle cost for DEP. */
2746 static int
2747 mn10300_adjust_sched_cost (rtx insn, rtx link, rtx dep, int cost)
2749 int timings = get_attr_timings (insn);
2751 if (!TARGET_AM33)
2752 return 1;
2754 if (GET_CODE (insn) == PARALLEL)
2755 insn = XVECEXP (insn, 0, 0);
2757 if (GET_CODE (dep) == PARALLEL)
2758 dep = XVECEXP (dep, 0, 0);
2760 /* For the AM34 a load instruction that follows a
2761 store instruction incurs an extra cycle of delay. */
2762 if (mn10300_tune_cpu == PROCESSOR_AM34
2763 && is_load_insn (dep)
2764 && is_store_insn (insn))
2765 cost += 1;
2767 /* For the AM34 a non-store, non-branch FPU insn that follows
2768 another FPU insn incurs a one cycle throughput increase. */
2769 else if (mn10300_tune_cpu == PROCESSOR_AM34
2770 && ! is_store_insn (insn)
2771 && ! JUMP_P (insn)
2772 && GET_CODE (PATTERN (dep)) == SET
2773 && GET_CODE (PATTERN (insn)) == SET
2774 && GET_MODE_CLASS (GET_MODE (SET_SRC (PATTERN (dep)))) == MODE_FLOAT
2775 && GET_MODE_CLASS (GET_MODE (SET_SRC (PATTERN (insn)))) == MODE_FLOAT)
2776 cost += 1;
2778 /* Resolve the conflict described in section 1-7-4 of
2779 Chapter 3 of the MN103E Series Instruction Manual
2780 where it says:
2782 "When the preceding instruction is a CPU load or
2783 store instruction, a following FPU instruction
2784 cannot be executed until the CPU completes the
2785 latency period even though there are no register
2786 or flag dependencies between them." */
2788 /* Only the AM33-2 (and later) CPUs have FPU instructions. */
2789 if (! TARGET_AM33_2)
2790 return cost;
2792 /* If a data dependence already exists then the cost is correct. */
2793 if (REG_NOTE_KIND (link) == 0)
2794 return cost;
2796 /* Check that the instruction about to scheduled is an FPU instruction. */
2797 if (GET_CODE (PATTERN (dep)) != SET)
2798 return cost;
2800 if (GET_MODE_CLASS (GET_MODE (SET_SRC (PATTERN (dep)))) != MODE_FLOAT)
2801 return cost;
2803 /* Now check to see if the previous instruction is a load or store. */
2804 if (! is_load_insn (insn) && ! is_store_insn (insn))
2805 return cost;
2807 /* XXX: Verify: The text of 1-7-4 implies that the restriction
2808 only applies when an INTEGER load/store precedes an FPU
2809 instruction, but is this true ? For now we assume that it is. */
2810 if (GET_MODE_CLASS (GET_MODE (SET_SRC (PATTERN (insn)))) != MODE_INT)
2811 return cost;
2813 /* Extract the latency value from the timings attribute. */
2814 return timings < 100 ? (timings % 10) : (timings % 100);
2817 static void
2818 mn10300_conditional_register_usage (void)
2820 unsigned int i;
2822 if (!TARGET_AM33)
2824 for (i = FIRST_EXTENDED_REGNUM;
2825 i <= LAST_EXTENDED_REGNUM; i++)
2826 fixed_regs[i] = call_used_regs[i] = 1;
2828 if (!TARGET_AM33_2)
2830 for (i = FIRST_FP_REGNUM;
2831 i <= LAST_FP_REGNUM; i++)
2832 fixed_regs[i] = call_used_regs[i] = 1;
2834 if (flag_pic)
2835 fixed_regs[PIC_OFFSET_TABLE_REGNUM] =
2836 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2839 /* Worker function for TARGET_MD_ASM_CLOBBERS.
2840 We do this in the mn10300 backend to maintain source compatibility
2841 with the old cc0-based compiler. */
2843 static tree
2844 mn10300_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
2845 tree inputs ATTRIBUTE_UNUSED,
2846 tree clobbers)
2848 clobbers = tree_cons (NULL_TREE, build_string (5, "EPSW"),
2849 clobbers);
2850 return clobbers;
2853 /* A helper function for splitting cbranch patterns after reload. */
2855 void
2856 mn10300_split_cbranch (enum machine_mode cmp_mode, rtx cmp_op, rtx label_ref)
2858 rtx flags, x;
2860 flags = gen_rtx_REG (cmp_mode, CC_REG);
2861 x = gen_rtx_COMPARE (cmp_mode, XEXP (cmp_op, 0), XEXP (cmp_op, 1));
2862 x = gen_rtx_SET (VOIDmode, flags, x);
2863 emit_insn (x);
2865 x = gen_rtx_fmt_ee (GET_CODE (cmp_op), VOIDmode, flags, const0_rtx);
2866 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x, label_ref, pc_rtx);
2867 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
2868 emit_jump_insn (x);
2871 /* A helper function for matching parallels that set the flags. */
2873 bool
2874 mn10300_match_ccmode (rtx insn, enum machine_mode cc_mode)
2876 rtx op1, flags;
2877 enum machine_mode flags_mode;
2879 gcc_checking_assert (XVECLEN (PATTERN (insn), 0) == 2);
2881 op1 = XVECEXP (PATTERN (insn), 0, 1);
2882 gcc_checking_assert (GET_CODE (SET_SRC (op1)) == COMPARE);
2884 flags = SET_DEST (op1);
2885 flags_mode = GET_MODE (flags);
2887 if (GET_MODE (SET_SRC (op1)) != flags_mode)
2888 return false;
2889 if (GET_MODE_CLASS (flags_mode) != MODE_CC)
2890 return false;
2892 /* Ensure that the mode of FLAGS is compatible with CC_MODE. */
2893 if (cc_flags_for_mode (flags_mode) & ~cc_flags_for_mode (cc_mode))
2894 return false;
2896 return true;
2899 /* This function is used to help split:
2901 (set (reg) (and (reg) (int)))
2903 into:
2905 (set (reg) (shift (reg) (int))
2906 (set (reg) (shift (reg) (int))
2908 where the shitfs will be shorter than the "and" insn.
2910 It returns the number of bits that should be shifted. A positive
2911 values means that the low bits are to be cleared (and hence the
2912 shifts should be right followed by left) whereas a negative value
2913 means that the high bits are to be cleared (left followed by right).
2914 Zero is returned when it would not be economical to split the AND. */
2917 mn10300_split_and_operand_count (rtx op)
2919 HOST_WIDE_INT val = INTVAL (op);
2920 int count;
2922 if (val < 0)
2924 /* High bit is set, look for bits clear at the bottom. */
2925 count = exact_log2 (-val);
2926 if (count < 0)
2927 return 0;
2928 /* This is only size win if we can use the asl2 insn. Otherwise we
2929 would be replacing 1 6-byte insn with 2 3-byte insns. */
2930 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2931 return 0;
2932 return count;
2934 else
2936 /* High bit is clear, look for bits set at the bottom. */
2937 count = exact_log2 (val + 1);
2938 count = 32 - count;
2939 /* Again, this is only a size win with asl2. */
2940 if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2941 return 0;
2942 return -count;
2946 struct liw_data
2948 enum attr_liw slot;
2949 enum attr_liw_op op;
2950 rtx dest;
2951 rtx src;
2954 /* Decide if the given insn is a candidate for LIW bundling. If it is then
2955 extract the operands and LIW attributes from the insn and use them to fill
2956 in the liw_data structure. Return true upon success or false if the insn
2957 cannot be bundled. */
2959 static bool
2960 extract_bundle (rtx insn, struct liw_data * pdata)
2962 bool allow_consts = true;
2963 rtx p;
2965 gcc_assert (pdata != NULL);
2967 if (insn == NULL_RTX)
2968 return false;
2969 /* Make sure that we are dealing with a simple SET insn. */
2970 p = single_set (insn);
2971 if (p == NULL_RTX)
2972 return false;
2974 /* Make sure that it could go into one of the LIW pipelines. */
2975 pdata->slot = get_attr_liw (insn);
2976 if (pdata->slot == LIW_BOTH)
2977 return false;
2979 pdata->op = get_attr_liw_op (insn);
2981 switch (pdata->op)
2983 case LIW_OP_MOV:
2984 pdata->dest = SET_DEST (p);
2985 pdata->src = SET_SRC (p);
2986 break;
2987 case LIW_OP_CMP:
2988 pdata->dest = XEXP (SET_SRC (p), 0);
2989 pdata->src = XEXP (SET_SRC (p), 1);
2990 break;
2991 case LIW_OP_NONE:
2992 return false;
2993 case LIW_OP_AND:
2994 case LIW_OP_OR:
2995 case LIW_OP_XOR:
2996 /* The AND, OR and XOR long instruction words only accept register arguments. */
2997 allow_consts = false;
2998 /* Fall through. */
2999 default:
3000 pdata->dest = SET_DEST (p);
3001 pdata->src = XEXP (SET_SRC (p), 1);
3002 break;
3005 if (! REG_P (pdata->dest))
3006 return false;
3008 if (REG_P (pdata->src))
3009 return true;
3011 return allow_consts && satisfies_constraint_O (pdata->src);
3014 /* Make sure that it is OK to execute LIW1 and LIW2 in parallel. GCC generated
3015 the instructions with the assumption that LIW1 would be executed before LIW2
3016 so we must check for overlaps between their sources and destinations. */
3018 static bool
3019 check_liw_constraints (struct liw_data * pliw1, struct liw_data * pliw2)
3021 /* Check for slot conflicts. */
3022 if (pliw2->slot == pliw1->slot && pliw1->slot != LIW_EITHER)
3023 return false;
3025 /* If either operation is a compare, then "dest" is really an input; the real
3026 destination is CC_REG. So these instructions need different checks. */
3028 /* Changing "CMP ; OP" into "CMP | OP" is OK because the comparison will
3029 check its values prior to any changes made by OP. */
3030 if (pliw1->op == LIW_OP_CMP)
3032 /* Two sequential comparisons means dead code, which ought to
3033 have been eliminated given that bundling only happens with
3034 optimization. We cannot bundle them in any case. */
3035 gcc_assert (pliw1->op != pliw2->op);
3036 return true;
3039 /* Changing "OP ; CMP" into "OP | CMP" does not work if the value being compared
3040 is the destination of OP, as the CMP will look at the old value, not the new
3041 one. */
3042 if (pliw2->op == LIW_OP_CMP)
3044 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3045 return false;
3047 if (REG_P (pliw2->src))
3048 return REGNO (pliw2->src) != REGNO (pliw1->dest);
3050 return true;
3053 /* Changing "OP1 ; OP2" into "OP1 | OP2" does not work if they both write to the
3054 same destination register. */
3055 if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3056 return false;
3058 /* Changing "OP1 ; OP2" into "OP1 | OP2" generally does not work if the destination
3059 of OP1 is the source of OP2. The exception is when OP1 is a MOVE instruction when
3060 we can replace the source in OP2 with the source of OP1. */
3061 if (REG_P (pliw2->src) && REGNO (pliw2->src) == REGNO (pliw1->dest))
3063 if (pliw1->op == LIW_OP_MOV && REG_P (pliw1->src))
3065 if (! REG_P (pliw1->src)
3066 && (pliw2->op == LIW_OP_AND
3067 || pliw2->op == LIW_OP_OR
3068 || pliw2->op == LIW_OP_XOR))
3069 return false;
3071 pliw2->src = pliw1->src;
3072 return true;
3074 return false;
3077 /* Everything else is OK. */
3078 return true;
3081 /* Combine pairs of insns into LIW bundles. */
3083 static void
3084 mn10300_bundle_liw (void)
3086 rtx r;
3088 for (r = get_insns (); r != NULL_RTX; r = next_nonnote_nondebug_insn (r))
3090 rtx insn1, insn2;
3091 struct liw_data liw1, liw2;
3093 insn1 = r;
3094 if (! extract_bundle (insn1, & liw1))
3095 continue;
3097 insn2 = next_nonnote_nondebug_insn (insn1);
3098 if (! extract_bundle (insn2, & liw2))
3099 continue;
3101 /* Check for source/destination overlap. */
3102 if (! check_liw_constraints (& liw1, & liw2))
3103 continue;
3105 if (liw1.slot == LIW_OP2 || liw2.slot == LIW_OP1)
3107 struct liw_data temp;
3109 temp = liw1;
3110 liw1 = liw2;
3111 liw2 = temp;
3114 delete_insn (insn2);
3116 if (liw1.op == LIW_OP_CMP)
3117 insn2 = gen_cmp_liw (liw2.dest, liw2.src, liw1.dest, liw1.src,
3118 GEN_INT (liw2.op));
3119 else if (liw2.op == LIW_OP_CMP)
3120 insn2 = gen_liw_cmp (liw1.dest, liw1.src, liw2.dest, liw2.src,
3121 GEN_INT (liw1.op));
3122 else
3123 insn2 = gen_liw (liw1.dest, liw2.dest, liw1.src, liw2.src,
3124 GEN_INT (liw1.op), GEN_INT (liw2.op));
3126 insn2 = emit_insn_after (insn2, insn1);
3127 delete_insn (insn1);
3128 r = insn2;
3132 #define DUMP(reason, insn) \
3133 do \
3135 if (dump_file) \
3137 fprintf (dump_file, reason "\n"); \
3138 if (insn != NULL_RTX) \
3139 print_rtl_single (dump_file, insn); \
3140 fprintf(dump_file, "\n"); \
3143 while (0)
3145 /* Replace the BRANCH insn with a Lcc insn that goes to LABEL.
3146 Insert a SETLB insn just before LABEL. */
3148 static void
3149 mn10300_insert_setlb_lcc (rtx label, rtx branch)
3151 rtx lcc, comparison, cmp_reg;
3153 if (LABEL_NUSES (label) > 1)
3155 rtx insn;
3157 /* This label is used both as an entry point to the loop
3158 and as a loop-back point for the loop. We need to separate
3159 these two functions so that the SETLB happens upon entry,
3160 but the loop-back does not go to the SETLB instruction. */
3161 DUMP ("Inserting SETLB insn after:", label);
3162 insn = emit_insn_after (gen_setlb (), label);
3163 label = gen_label_rtx ();
3164 emit_label_after (label, insn);
3165 DUMP ("Created new loop-back label:", label);
3167 else
3169 DUMP ("Inserting SETLB insn before:", label);
3170 emit_insn_before (gen_setlb (), label);
3173 comparison = XEXP (SET_SRC (PATTERN (branch)), 0);
3174 cmp_reg = XEXP (comparison, 0);
3175 gcc_assert (REG_P (cmp_reg));
3177 /* If the comparison has not already been split out of the branch
3178 then do so now. */
3179 gcc_assert (REGNO (cmp_reg) == CC_REG);
3181 if (GET_MODE (cmp_reg) == CC_FLOATmode)
3182 lcc = gen_FLcc (comparison, label);
3183 else
3184 lcc = gen_Lcc (comparison, label);
3186 lcc = emit_jump_insn_before (lcc, branch);
3187 mark_jump_label (XVECEXP (PATTERN (lcc), 0, 0), lcc, 0);
3188 JUMP_LABEL (lcc) = label;
3189 DUMP ("Replacing branch insn...", branch);
3190 DUMP ("... with Lcc insn:", lcc);
3191 delete_insn (branch);
3194 static bool
3195 mn10300_block_contains_call (basic_block block)
3197 rtx insn;
3199 FOR_BB_INSNS (block, insn)
3200 if (CALL_P (insn))
3201 return true;
3203 return false;
3206 static bool
3207 mn10300_loop_contains_call_insn (loop_p loop)
3209 basic_block * bbs;
3210 bool result = false;
3211 unsigned int i;
3213 bbs = get_loop_body (loop);
3215 for (i = 0; i < loop->num_nodes; i++)
3216 if (mn10300_block_contains_call (bbs[i]))
3218 result = true;
3219 break;
3222 free (bbs);
3223 return result;
3226 static void
3227 mn10300_scan_for_setlb_lcc (void)
3229 loop_iterator liter;
3230 loop_p loop;
3232 DUMP ("Looking for loops that can use the SETLB insn", NULL_RTX);
3234 df_analyze ();
3235 compute_bb_for_insn ();
3237 /* Find the loops. */
3238 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3240 /* FIXME: For now we only investigate innermost loops. In practice however
3241 if an inner loop is not suitable for use with the SETLB/Lcc insns, it may
3242 be the case that its parent loop is suitable. Thus we should check all
3243 loops, but work from the innermost outwards. */
3244 FOR_EACH_LOOP (liter, loop, LI_ONLY_INNERMOST)
3246 const char * reason = NULL;
3248 /* Check to see if we can modify this loop. If we cannot
3249 then set 'reason' to describe why it could not be done. */
3250 if (loop->latch == NULL)
3251 reason = "it contains multiple latches";
3252 else if (loop->header != loop->latch)
3253 /* FIXME: We could handle loops that span multiple blocks,
3254 but this requires a lot more work tracking down the branches
3255 that need altering, so for now keep things simple. */
3256 reason = "the loop spans multiple blocks";
3257 else if (mn10300_loop_contains_call_insn (loop))
3258 reason = "it contains CALL insns";
3259 else
3261 rtx branch = BB_END (loop->latch);
3263 gcc_assert (JUMP_P (branch));
3264 if (single_set (branch) == NULL_RTX || ! any_condjump_p (branch))
3265 /* We cannot optimize tablejumps and the like. */
3266 /* FIXME: We could handle unconditional jumps. */
3267 reason = "it is not a simple loop";
3268 else
3270 rtx label;
3272 if (dump_file)
3273 flow_loop_dump (loop, dump_file, NULL, 0);
3275 label = BB_HEAD (loop->header);
3276 gcc_assert (LABEL_P (label));
3278 mn10300_insert_setlb_lcc (label, branch);
3282 if (dump_file && reason != NULL)
3283 fprintf (dump_file, "Loop starting with insn %d is not suitable because %s\n",
3284 INSN_UID (BB_HEAD (loop->header)),
3285 reason);
3288 loop_optimizer_finalize ();
3290 df_finish_pass (false);
3292 DUMP ("SETLB scan complete", NULL_RTX);
3295 static void
3296 mn10300_reorg (void)
3298 /* These are optimizations, so only run them if optimizing. */
3299 if (TARGET_AM33 && (optimize > 0 || optimize_size))
3301 if (TARGET_ALLOW_SETLB)
3302 mn10300_scan_for_setlb_lcc ();
3304 if (TARGET_ALLOW_LIW)
3305 mn10300_bundle_liw ();
3309 /* Initialize the GCC target structure. */
3311 #undef TARGET_MACHINE_DEPENDENT_REORG
3312 #define TARGET_MACHINE_DEPENDENT_REORG mn10300_reorg
3314 #undef TARGET_ASM_ALIGNED_HI_OP
3315 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3317 #undef TARGET_LEGITIMIZE_ADDRESS
3318 #define TARGET_LEGITIMIZE_ADDRESS mn10300_legitimize_address
3320 #undef TARGET_ADDRESS_COST
3321 #define TARGET_ADDRESS_COST mn10300_address_cost
3322 #undef TARGET_REGISTER_MOVE_COST
3323 #define TARGET_REGISTER_MOVE_COST mn10300_register_move_cost
3324 #undef TARGET_MEMORY_MOVE_COST
3325 #define TARGET_MEMORY_MOVE_COST mn10300_memory_move_cost
3326 #undef TARGET_RTX_COSTS
3327 #define TARGET_RTX_COSTS mn10300_rtx_costs
3329 #undef TARGET_ASM_FILE_START
3330 #define TARGET_ASM_FILE_START mn10300_file_start
3331 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
3332 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
3334 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
3335 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA mn10300_asm_output_addr_const_extra
3337 #undef TARGET_OPTION_OVERRIDE
3338 #define TARGET_OPTION_OVERRIDE mn10300_option_override
3340 #undef TARGET_ENCODE_SECTION_INFO
3341 #define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
3343 #undef TARGET_PROMOTE_PROTOTYPES
3344 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
3345 #undef TARGET_RETURN_IN_MEMORY
3346 #define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
3347 #undef TARGET_PASS_BY_REFERENCE
3348 #define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
3349 #undef TARGET_CALLEE_COPIES
3350 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
3351 #undef TARGET_ARG_PARTIAL_BYTES
3352 #define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
3353 #undef TARGET_FUNCTION_ARG
3354 #define TARGET_FUNCTION_ARG mn10300_function_arg
3355 #undef TARGET_FUNCTION_ARG_ADVANCE
3356 #define TARGET_FUNCTION_ARG_ADVANCE mn10300_function_arg_advance
3358 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
3359 #define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
3360 #undef TARGET_EXPAND_BUILTIN_VA_START
3361 #define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
3363 #undef TARGET_CASE_VALUES_THRESHOLD
3364 #define TARGET_CASE_VALUES_THRESHOLD mn10300_case_values_threshold
3366 #undef TARGET_LEGITIMATE_ADDRESS_P
3367 #define TARGET_LEGITIMATE_ADDRESS_P mn10300_legitimate_address_p
3368 #undef TARGET_DELEGITIMIZE_ADDRESS
3369 #define TARGET_DELEGITIMIZE_ADDRESS mn10300_delegitimize_address
3370 #undef TARGET_LEGITIMATE_CONSTANT_P
3371 #define TARGET_LEGITIMATE_CONSTANT_P mn10300_legitimate_constant_p
3373 #undef TARGET_PREFERRED_RELOAD_CLASS
3374 #define TARGET_PREFERRED_RELOAD_CLASS mn10300_preferred_reload_class
3375 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
3376 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS \
3377 mn10300_preferred_output_reload_class
3378 #undef TARGET_SECONDARY_RELOAD
3379 #define TARGET_SECONDARY_RELOAD mn10300_secondary_reload
3381 #undef TARGET_TRAMPOLINE_INIT
3382 #define TARGET_TRAMPOLINE_INIT mn10300_trampoline_init
3384 #undef TARGET_FUNCTION_VALUE
3385 #define TARGET_FUNCTION_VALUE mn10300_function_value
3386 #undef TARGET_LIBCALL_VALUE
3387 #define TARGET_LIBCALL_VALUE mn10300_libcall_value
3389 #undef TARGET_ASM_OUTPUT_MI_THUNK
3390 #define TARGET_ASM_OUTPUT_MI_THUNK mn10300_asm_output_mi_thunk
3391 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
3392 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK mn10300_can_output_mi_thunk
3394 #undef TARGET_SCHED_ADJUST_COST
3395 #define TARGET_SCHED_ADJUST_COST mn10300_adjust_sched_cost
3397 #undef TARGET_CONDITIONAL_REGISTER_USAGE
3398 #define TARGET_CONDITIONAL_REGISTER_USAGE mn10300_conditional_register_usage
3400 #undef TARGET_MD_ASM_CLOBBERS
3401 #define TARGET_MD_ASM_CLOBBERS mn10300_md_asm_clobbers
3403 #undef TARGET_FLAGS_REGNUM
3404 #define TARGET_FLAGS_REGNUM CC_REG
3406 struct gcc_target targetm = TARGET_INITIALIZER;