1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2    Copyright (C) 1996-2020 Free Software Foundation, Inc.
3    Contributed by Jeff Law (law@cygnus.com).
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20 
21 #define IN_TARGET_CODE 1
22 
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "cfghooks.h"
33 #include "cfgloop.h"
34 #include "df.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "emit-rtl.h"
40 #include "recog.h"
41 #include "diagnostic-core.h"
42 #include "alias.h"
43 #include "stor-layout.h"
44 #include "varasm.h"
45 #include "calls.h"
46 #include "output.h"
47 #include "insn-attr.h"
48 #include "reload.h"
49 #include "explow.h"
50 #include "expr.h"
51 #include "tm-constrs.h"
52 #include "cfgrtl.h"
53 #include "dumpfile.h"
54 #include "builtins.h"
55 
56 /* This file should be included last.  */
57 #include "target-def.h"
58 
59 /* This is used in the am33_2.0-linux-gnu port, in which global symbol
60    names are not prefixed by underscores, to tell whether to prefix a
61    label with a plus sign or not, so that the assembler can tell
62    symbol names from register names.  */
63 int mn10300_protect_label;
64 
65 /* Selected processor type for tuning.  */
66 enum processor_type mn10300_tune_cpu = PROCESSOR_DEFAULT;
67 
68 #define CC_FLAG_Z	1
69 #define CC_FLAG_N	2
70 #define CC_FLAG_C	4
71 #define CC_FLAG_V	8
72 
73 static int cc_flags_for_mode(machine_mode);
74 static int cc_flags_for_code(enum rtx_code);
75 
76 /* Implement TARGET_OPTION_OVERRIDE.  */
77 static void
mn10300_option_override(void)78 mn10300_option_override (void)
79 {
80   if (TARGET_AM33)
81     target_flags &= ~MASK_MULT_BUG;
82   else
83     {
84       /* Disable scheduling for the MN10300 as we do
85 	 not have timing information available for it.  */
86       flag_schedule_insns = 0;
87       flag_schedule_insns_after_reload = 0;
88 
89       /* Force enable splitting of wide types, as otherwise it is trivial
90 	 to run out of registers.  Indeed, this works so well that register
91 	 allocation problems are now more common *without* optimization,
92 	 when this flag is not enabled by default.  */
93       flag_split_wide_types = 1;
94     }
95 
96   if (mn10300_tune_string)
97     {
98       if (strcasecmp (mn10300_tune_string, "mn10300") == 0)
99 	mn10300_tune_cpu = PROCESSOR_MN10300;
100       else if (strcasecmp (mn10300_tune_string, "am33") == 0)
101 	mn10300_tune_cpu = PROCESSOR_AM33;
102       else if (strcasecmp (mn10300_tune_string, "am33-2") == 0)
103 	mn10300_tune_cpu = PROCESSOR_AM33_2;
104       else if (strcasecmp (mn10300_tune_string, "am34") == 0)
105 	mn10300_tune_cpu = PROCESSOR_AM34;
106       else
107 	error ("%<-mtune=%> expects mn10300, am33, am33-2, or am34");
108     }
109 }
110 
111 static void
mn10300_file_start(void)112 mn10300_file_start (void)
113 {
114   default_file_start ();
115 
116   if (TARGET_AM33_2)
117     fprintf (asm_out_file, "\t.am33_2\n");
118   else if (TARGET_AM33)
119     fprintf (asm_out_file, "\t.am33\n");
120 }
121 
122 /* Note: This list must match the liw_op attribute in mn10300.md.  */
123 
124 static const char *liw_op_names[] =
125 {
126   "add", "cmp", "sub", "mov",
127   "and", "or", "xor",
128   "asr", "lsr", "asl",
129   "none", "max"
130 };
131 
132 /* Print operand X using operand code CODE to assembly language output file
133    FILE.  */
134 
135 void
mn10300_print_operand(FILE * file,rtx x,int code)136 mn10300_print_operand (FILE *file, rtx x, int code)
137 {
138   switch (code)
139     {
140     case 'W':
141       {
142 	unsigned int liw_op = UINTVAL (x);
143 
144 	gcc_assert (TARGET_ALLOW_LIW);
145 	gcc_assert (liw_op < LIW_OP_MAX);
146 	fputs (liw_op_names[liw_op], file);
147 	break;
148       }
149 
150     case 'b':
151     case 'B':
152       {
153 	enum rtx_code cmp = GET_CODE (x);
154 	machine_mode mode = GET_MODE (XEXP (x, 0));
155 	const char *str;
156 	int have_flags;
157 
158 	if (code == 'B')
159 	  cmp = reverse_condition (cmp);
160 	have_flags = cc_flags_for_mode (mode);
161 
162 	switch (cmp)
163 	  {
164 	  case NE:
165 	    str = "ne";
166 	    break;
167 	  case EQ:
168 	    str = "eq";
169 	    break;
170 	  case GE:
171 	    /* bge is smaller than bnc.  */
172 	    str = (have_flags & CC_FLAG_V ? "ge" : "nc");
173 	    break;
174 	  case LT:
175 	    str = (have_flags & CC_FLAG_V ? "lt" : "ns");
176 	    break;
177 	  case GT:
178 	    str = "gt";
179 	    break;
180 	  case LE:
181 	    str = "le";
182 	    break;
183 	  case GEU:
184 	    str = "cc";
185 	    break;
186 	  case GTU:
187 	    str = "hi";
188 	    break;
189 	  case LEU:
190 	    str = "ls";
191 	    break;
192 	  case LTU:
193 	    str = "cs";
194 	    break;
195 	  case ORDERED:
196 	    str = "lge";
197 	    break;
198 	  case UNORDERED:
199 	    str = "uo";
200 	    break;
201 	  case LTGT:
202 	    str = "lg";
203 	    break;
204 	  case UNEQ:
205 	    str = "ue";
206 	    break;
207 	  case UNGE:
208 	    str = "uge";
209 	    break;
210 	  case UNGT:
211 	    str = "ug";
212 	    break;
213 	  case UNLE:
214 	    str = "ule";
215 	    break;
216 	  case UNLT:
217 	    str = "ul";
218 	    break;
219 	  default:
220 	    gcc_unreachable ();
221 	  }
222 
223 	gcc_checking_assert ((cc_flags_for_code (cmp) & ~have_flags) == 0);
224 	fputs (str, file);
225       }
226       break;
227 
228     case 'C':
229       /* This is used for the operand to a call instruction;
230 	 if it's a REG, enclose it in parens, else output
231 	 the operand normally.  */
232       if (REG_P (x))
233 	{
234 	  fputc ('(', file);
235 	  mn10300_print_operand (file, x, 0);
236 	  fputc (')', file);
237 	}
238       else
239 	mn10300_print_operand (file, x, 0);
240       break;
241 
242     case 'D':
243       switch (GET_CODE (x))
244 	{
245 	case MEM:
246 	  fputc ('(', file);
247 	  output_address (GET_MODE (x), XEXP (x, 0));
248 	  fputc (')', file);
249 	  break;
250 
251 	case REG:
252 	  fprintf (file, "fd%d", REGNO (x) - 18);
253 	  break;
254 
255 	default:
256 	  gcc_unreachable ();
257 	}
258       break;
259 
260       /* These are the least significant word in a 64bit value.  */
261     case 'L':
262       switch (GET_CODE (x))
263 	{
264 	case MEM:
265 	  fputc ('(', file);
266 	  output_address (GET_MODE (x), XEXP (x, 0));
267 	  fputc (')', file);
268 	  break;
269 
270 	case REG:
271 	  fprintf (file, "%s", reg_names[REGNO (x)]);
272 	  break;
273 
274 	case SUBREG:
275 	  fprintf (file, "%s", reg_names[subreg_regno (x)]);
276 	  break;
277 
278 	case CONST_DOUBLE:
279 	  {
280 	    long val[2];
281 
282 	    switch (GET_MODE (x))
283 	      {
284 	      case E_DFmode:
285 		REAL_VALUE_TO_TARGET_DOUBLE
286 		  (*CONST_DOUBLE_REAL_VALUE (x), val);
287 		fprintf (file, "0x%lx", val[0]);
288 		break;;
289 	      case E_SFmode:
290 		REAL_VALUE_TO_TARGET_SINGLE
291 		  (*CONST_DOUBLE_REAL_VALUE (x), val[0]);
292 		fprintf (file, "0x%lx", val[0]);
293 		break;;
294 	      case E_VOIDmode:
295 	      case E_DImode:
296 		mn10300_print_operand_address (file,
297 					       GEN_INT (CONST_DOUBLE_LOW (x)));
298 		break;
299 	      default:
300 		break;
301 	      }
302 	    break;
303 	  }
304 
305 	case CONST_INT:
306 	  {
307 	    rtx low, high;
308 	    split_double (x, &low, &high);
309 	    fprintf (file, "%ld", (long)INTVAL (low));
310 	    break;
311 	    }
312 
313 	default:
314 	  gcc_unreachable ();
315 	}
316       break;
317 
318       /* Similarly, but for the most significant word.  */
319     case 'H':
320       switch (GET_CODE (x))
321 	{
322 	case MEM:
323 	  fputc ('(', file);
324 	  x = adjust_address (x, SImode, 4);
325 	  output_address (GET_MODE (x), XEXP (x, 0));
326 	  fputc (')', file);
327 	  break;
328 
329 	case REG:
330 	  fprintf (file, "%s", reg_names[REGNO (x) + 1]);
331 	  break;
332 
333 	case SUBREG:
334 	  fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
335 	  break;
336 
337 	case CONST_DOUBLE:
338 	  {
339 	    long val[2];
340 
341 	    switch (GET_MODE (x))
342 	      {
343 	      case E_DFmode:
344 		REAL_VALUE_TO_TARGET_DOUBLE
345 		  (*CONST_DOUBLE_REAL_VALUE (x), val);
346 		fprintf (file, "0x%lx", val[1]);
347 		break;;
348 	      case E_SFmode:
349 		gcc_unreachable ();
350 	      case E_VOIDmode:
351 	      case E_DImode:
352 		mn10300_print_operand_address (file,
353 					       GEN_INT (CONST_DOUBLE_HIGH (x)));
354 		break;
355 	      default:
356 		break;
357 	      }
358 	    break;
359 	  }
360 
361 	case CONST_INT:
362 	  {
363 	    rtx low, high;
364 	    split_double (x, &low, &high);
365 	    fprintf (file, "%ld", (long)INTVAL (high));
366 	    break;
367 	  }
368 
369 	default:
370 	  gcc_unreachable ();
371 	}
372       break;
373 
374     case 'A':
375       fputc ('(', file);
376       if (REG_P (XEXP (x, 0)))
377 	output_address (VOIDmode, gen_rtx_PLUS (SImode,
378 						XEXP (x, 0), const0_rtx));
379       else
380 	output_address (VOIDmode, XEXP (x, 0));
381       fputc (')', file);
382       break;
383 
384     case 'N':
385       gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
386       fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
387       break;
388 
389     case 'U':
390       gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
391       fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
392       break;
393 
394       /* For shift counts.  The hardware ignores the upper bits of
395 	 any immediate, but the assembler will flag an out of range
396 	 shift count as an error.  So we mask off the high bits
397 	 of the immediate here.  */
398     case 'S':
399       if (CONST_INT_P (x))
400 	{
401 	  fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
402 	  break;
403 	}
404       /* FALL THROUGH */
405 
406     default:
407       switch (GET_CODE (x))
408 	{
409 	case MEM:
410 	  fputc ('(', file);
411 	  output_address (GET_MODE (x), XEXP (x, 0));
412 	  fputc (')', file);
413 	  break;
414 
415 	case PLUS:
416 	  output_address (VOIDmode, x);
417 	  break;
418 
419 	case REG:
420 	  fprintf (file, "%s", reg_names[REGNO (x)]);
421 	  break;
422 
423 	case SUBREG:
424 	  fprintf (file, "%s", reg_names[subreg_regno (x)]);
425 	  break;
426 
427 	  /* This will only be single precision....  */
428 	case CONST_DOUBLE:
429 	  {
430 	    unsigned long val;
431 
432 	    REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
433 	    fprintf (file, "0x%lx", val);
434 	    break;
435 	  }
436 
437 	case CONST_INT:
438 	case SYMBOL_REF:
439 	case CONST:
440 	case LABEL_REF:
441 	case CODE_LABEL:
442 	case UNSPEC:
443 	  mn10300_print_operand_address (file, x);
444 	  break;
445 	default:
446 	  gcc_unreachable ();
447 	}
448       break;
449     }
450 }
451 
452 /* Output assembly language output for the address ADDR to FILE.  */
453 
454 void
mn10300_print_operand_address(FILE * file,rtx addr)455 mn10300_print_operand_address (FILE *file, rtx addr)
456 {
457   switch (GET_CODE (addr))
458     {
459     case POST_INC:
460       mn10300_print_operand (file, XEXP (addr, 0), 0);
461       fputc ('+', file);
462       break;
463 
464     case POST_MODIFY:
465       mn10300_print_operand (file, XEXP (addr, 0), 0);
466       fputc ('+', file);
467       fputc (',', file);
468       mn10300_print_operand (file, XEXP (addr, 1), 0);
469       break;
470 
471     case REG:
472       mn10300_print_operand (file, addr, 0);
473       break;
474     case PLUS:
475       {
476 	rtx base = XEXP (addr, 0);
477 	rtx index = XEXP (addr, 1);
478 
479 	if (REG_P (index) && !REG_OK_FOR_INDEX_P (index))
480 	  {
481 	    rtx x = base;
482 	    base = index;
483 	    index = x;
484 
485 	    gcc_assert (REG_P (index) && REG_OK_FOR_INDEX_P (index));
486 	  }
487 	gcc_assert (REG_OK_FOR_BASE_P (base));
488 
489 	mn10300_print_operand (file, index, 0);
490 	fputc (',', file);
491 	mn10300_print_operand (file, base, 0);
492 	break;
493       }
494     case SYMBOL_REF:
495       output_addr_const (file, addr);
496       break;
497     default:
498       output_addr_const (file, addr);
499       break;
500     }
501 }
502 
503 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.
504 
505    Used for PIC-specific UNSPECs.  */
506 
507 static bool
mn10300_asm_output_addr_const_extra(FILE * file,rtx x)508 mn10300_asm_output_addr_const_extra (FILE *file, rtx x)
509 {
510   if (GET_CODE (x) == UNSPEC)
511     {
512       switch (XINT (x, 1))
513 	{
514 	case UNSPEC_PIC:
515 	  /* GLOBAL_OFFSET_TABLE or local symbols, no suffix.  */
516 	  output_addr_const (file, XVECEXP (x, 0, 0));
517 	  break;
518 	case UNSPEC_GOT:
519 	  output_addr_const (file, XVECEXP (x, 0, 0));
520 	  fputs ("@GOT", file);
521 	  break;
522 	case UNSPEC_GOTOFF:
523 	  output_addr_const (file, XVECEXP (x, 0, 0));
524 	  fputs ("@GOTOFF", file);
525 	  break;
526 	case UNSPEC_PLT:
527 	  output_addr_const (file, XVECEXP (x, 0, 0));
528 	  fputs ("@PLT", file);
529 	  break;
530 	case UNSPEC_GOTSYM_OFF:
531 	  assemble_name (file, GOT_SYMBOL_NAME);
532 	  fputs ("-(", file);
533 	  output_addr_const (file, XVECEXP (x, 0, 0));
534 	  fputs ("-.)", file);
535 	  break;
536 	default:
537 	  return false;
538 	}
539       return true;
540     }
541   else
542     return false;
543 }
544 
545 /* Count the number of FP registers that have to be saved.  */
546 static int
fp_regs_to_save(void)547 fp_regs_to_save (void)
548 {
549   int i, n = 0;
550 
551   if (! TARGET_AM33_2)
552     return 0;
553 
554   for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
555     if (df_regs_ever_live_p (i) && ! call_used_regs[i])
556       ++n;
557 
558   return n;
559 }
560 
561 /* Print a set of registers in the format required by "movm" and "ret".
562    Register K is saved if bit K of MASK is set.  The data and address
563    registers can be stored individually, but the extended registers cannot.
564    We assume that the mask already takes that into account.  For instance,
565    bits 14 to 17 must have the same value.  */
566 
567 void
mn10300_print_reg_list(FILE * file,int mask)568 mn10300_print_reg_list (FILE *file, int mask)
569 {
570   int need_comma;
571   int i;
572 
573   need_comma = 0;
574   fputc ('[', file);
575 
576   for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
577     if ((mask & (1 << i)) != 0)
578       {
579 	if (need_comma)
580 	  fputc (',', file);
581 	fputs (reg_names [i], file);
582 	need_comma = 1;
583       }
584 
585   if ((mask & 0x3c000) != 0)
586     {
587       gcc_assert ((mask & 0x3c000) == 0x3c000);
588       if (need_comma)
589 	fputc (',', file);
590       fputs ("exreg1", file);
591       need_comma = 1;
592     }
593 
594   fputc (']', file);
595 }
596 
597 /* If the MDR register is never clobbered, we can use the RETF instruction
598    which takes the address from the MDR register.  This is 3 cycles faster
599    than having to load the address from the stack.  */
600 
601 bool
mn10300_can_use_retf_insn(void)602 mn10300_can_use_retf_insn (void)
603 {
604   /* Don't bother if we're not optimizing.  In this case we won't
605      have proper access to df_regs_ever_live_p.  */
606   if (!optimize)
607     return false;
608 
609   /* EH returns alter the saved return address; MDR is not current.  */
610   if (crtl->calls_eh_return)
611     return false;
612 
613   /* Obviously not if MDR is ever clobbered.  */
614   if (df_regs_ever_live_p (MDR_REG))
615     return false;
616 
617   /* ??? Careful not to use this during expand_epilogue etc.  */
618   gcc_assert (!in_sequence_p ());
619   return leaf_function_p ();
620 }
621 
622 bool
mn10300_can_use_rets_insn(void)623 mn10300_can_use_rets_insn (void)
624 {
625   return !mn10300_initial_offset (ARG_POINTER_REGNUM, STACK_POINTER_REGNUM);
626 }
627 
628 /* Returns the set of live, callee-saved registers as a bitmask.  The
629    callee-saved extended registers cannot be stored individually, so
630    all of them will be included in the mask if any one of them is used.
631    Also returns the number of bytes in the registers in the mask if
632    BYTES_SAVED is not NULL.  */
633 
634 unsigned int
mn10300_get_live_callee_saved_regs(unsigned int * bytes_saved)635 mn10300_get_live_callee_saved_regs (unsigned int * bytes_saved)
636 {
637   int mask;
638   int i;
639   unsigned int count;
640 
641   count = mask = 0;
642   for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
643     if (df_regs_ever_live_p (i) && ! call_used_regs[i])
644       {
645 	mask |= (1 << i);
646 	++ count;
647       }
648 
649   if ((mask & 0x3c000) != 0)
650     {
651       for (i = 0x04000; i < 0x40000; i <<= 1)
652 	if ((mask & i) == 0)
653 	  ++ count;
654 
655       mask |= 0x3c000;
656     }
657 
658   if (bytes_saved)
659     * bytes_saved = count * UNITS_PER_WORD;
660 
661   return mask;
662 }
663 
664 static rtx
F(rtx r)665 F (rtx r)
666 {
667   RTX_FRAME_RELATED_P (r) = 1;
668   return r;
669 }
670 
671 /* Generate an instruction that pushes several registers onto the stack.
672    Register K will be saved if bit K in MASK is set.  The function does
673    nothing if MASK is zero.
674 
675    To be compatible with the "movm" instruction, the lowest-numbered
676    register must be stored in the lowest slot.  If MASK is the set
677    { R1,...,RN }, where R1...RN are ordered least first, the generated
678    instruction will have the form:
679 
680        (parallel
681          (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
682 	 (set (mem:SI (plus:SI (reg:SI 9)
683 	                       (const_int -1*4)))
684 	      (reg:SI RN))
685 	 ...
686 	 (set (mem:SI (plus:SI (reg:SI 9)
687 	                       (const_int -N*4)))
688 	      (reg:SI R1))) */
689 
690 static void
mn10300_gen_multiple_store(unsigned int mask)691 mn10300_gen_multiple_store (unsigned int mask)
692 {
693   /* The order in which registers are stored, from SP-4 through SP-N*4.  */
694   static const unsigned int store_order[8] = {
695     /* e2, e3: never saved */
696     FIRST_EXTENDED_REGNUM + 4,
697     FIRST_EXTENDED_REGNUM + 5,
698     FIRST_EXTENDED_REGNUM + 6,
699     FIRST_EXTENDED_REGNUM + 7,
700     /* e0, e1, mdrq, mcrh, mcrl, mcvf: never saved. */
701     FIRST_DATA_REGNUM + 2,
702     FIRST_DATA_REGNUM + 3,
703     FIRST_ADDRESS_REGNUM + 2,
704     FIRST_ADDRESS_REGNUM + 3,
705     /* d0, d1, a0, a1, mdr, lir, lar: never saved.  */
706   };
707 
708   rtx x, elts[9];
709   unsigned int i;
710   int count;
711 
712   if (mask == 0)
713     return;
714 
715   for (i = count = 0; i < ARRAY_SIZE(store_order); ++i)
716     {
717       unsigned regno = store_order[i];
718 
719       if (((mask >> regno) & 1) == 0)
720 	continue;
721 
722       ++count;
723       x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
724       x = gen_frame_mem (SImode, x);
725       x = gen_rtx_SET (x, gen_rtx_REG (SImode, regno));
726       elts[count] = F(x);
727 
728       /* Remove the register from the mask so that... */
729       mask &= ~(1u << regno);
730     }
731 
732   /* ... we can make sure that we didn't try to use a register
733      not listed in the store order.  */
734   gcc_assert (mask == 0);
735 
736   /* Create the instruction that updates the stack pointer.  */
737   x = plus_constant (Pmode, stack_pointer_rtx, count * -4);
738   x = gen_rtx_SET (stack_pointer_rtx, x);
739   elts[0] = F(x);
740 
741   /* We need one PARALLEL element to update the stack pointer and
742      an additional element for each register that is stored.  */
743   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (count + 1, elts));
744   F (emit_insn (x));
745 }
746 
747 static inline unsigned int
popcount(unsigned int mask)748 popcount (unsigned int mask)
749 {
750   unsigned int count = 0;
751 
752   while (mask)
753     {
754       ++ count;
755       mask &= ~ (mask & - mask);
756     }
757   return count;
758 }
759 
760 void
mn10300_expand_prologue(void)761 mn10300_expand_prologue (void)
762 {
763   HOST_WIDE_INT size = mn10300_frame_size ();
764   unsigned int mask;
765 
766   mask = mn10300_get_live_callee_saved_regs (NULL);
767   /* If we use any of the callee-saved registers, save them now.  */
768   mn10300_gen_multiple_store (mask);
769 
770   if (flag_stack_usage_info)
771     current_function_static_stack_size = size + popcount (mask) * 4;
772 
773   if (TARGET_AM33_2 && fp_regs_to_save ())
774     {
775       int num_regs_to_save = fp_regs_to_save (), i;
776       HOST_WIDE_INT xsize;
777       enum
778       {
779 	save_sp_merge,
780 	save_sp_no_merge,
781 	save_sp_partial_merge,
782 	save_a0_merge,
783 	save_a0_no_merge
784       } strategy;
785       unsigned int strategy_size = (unsigned)-1, this_strategy_size;
786       rtx reg;
787 
788       if (flag_stack_usage_info)
789 	current_function_static_stack_size += num_regs_to_save * 4;
790 
791       /* We have several different strategies to save FP registers.
792 	 We can store them using SP offsets, which is beneficial if
793 	 there are just a few registers to save, or we can use `a0' in
794 	 post-increment mode (`a0' is the only call-clobbered address
795 	 register that is never used to pass information to a
796 	 function).  Furthermore, if we don't need a frame pointer, we
797 	 can merge the two SP adds into a single one, but this isn't
798 	 always beneficial; sometimes we can just split the two adds
799 	 so that we don't exceed a 16-bit constant size.  The code
800 	 below will select which strategy to use, so as to generate
801 	 smallest code.  Ties are broken in favor or shorter sequences
802 	 (in terms of number of instructions).  */
803 
804 #define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
805 			: (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
806 #define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
807 			: (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
808 
809 /* We add 0 * (S) in two places to promote to the type of S,
810    so that all arms of the conditional have the same type.  */
811 #define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
812   (((S) >= (L)) ? 0 * (S) + (SIZE1) * (N) \
813    : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
814 			       + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
815    : 0 * (S) + (ELSE))
816 #define SIZE_FMOV_SP_(S,N) \
817   (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
818                    SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
819 				    (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
820 #define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
821 
822       /* Consider alternative save_sp_merge only if we don't need the
823 	 frame pointer and size is nonzero.  */
824       if (! frame_pointer_needed && size)
825 	{
826 	  /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
827 	  this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
828 	  /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
829 	  this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
830 
831 	  if (this_strategy_size < strategy_size)
832 	    {
833 	      strategy = save_sp_merge;
834 	      strategy_size = this_strategy_size;
835 	    }
836 	}
837 
838       /* Consider alternative save_sp_no_merge unconditionally.  */
839       /* Insn: add -4 * num_regs_to_save, sp.  */
840       this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
841       /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
842       this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
843       if (size)
844 	{
845 	  /* Insn: add -size, sp.  */
846 	  this_strategy_size += SIZE_ADD_SP (-size);
847 	}
848 
849       if (this_strategy_size < strategy_size)
850 	{
851 	  strategy = save_sp_no_merge;
852 	  strategy_size = this_strategy_size;
853 	}
854 
855       /* Consider alternative save_sp_partial_merge only if we don't
856 	 need a frame pointer and size is reasonably large.  */
857       if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
858 	{
859 	  /* Insn: add -128, sp.  */
860 	  this_strategy_size = SIZE_ADD_SP (-128);
861 	  /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
862 	  this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
863 					      num_regs_to_save);
864 	  if (size)
865 	    {
866 	      /* Insn: add 128-size, sp.  */
867 	      this_strategy_size += SIZE_ADD_SP (128 - size);
868 	    }
869 
870 	  if (this_strategy_size < strategy_size)
871 	    {
872 	      strategy = save_sp_partial_merge;
873 	      strategy_size = this_strategy_size;
874 	    }
875 	}
876 
877       /* Consider alternative save_a0_merge only if we don't need a
878 	 frame pointer, size is nonzero and the user hasn't
879 	 changed the calling conventions of a0.  */
880       if (! frame_pointer_needed && size
881 	  && call_used_regs[FIRST_ADDRESS_REGNUM]
882 	  && ! fixed_regs[FIRST_ADDRESS_REGNUM])
883 	{
884 	  /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
885 	  this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
886 	  /* Insn: mov sp, a0.  */
887 	  this_strategy_size++;
888 	  if (size)
889 	    {
890 	      /* Insn: add size, a0.  */
891 	      this_strategy_size += SIZE_ADD_AX (size);
892 	    }
893 	  /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
894 	  this_strategy_size += 3 * num_regs_to_save;
895 
896 	  if (this_strategy_size < strategy_size)
897 	    {
898 	      strategy = save_a0_merge;
899 	      strategy_size = this_strategy_size;
900 	    }
901 	}
902 
903       /* Consider alternative save_a0_no_merge if the user hasn't
904 	 changed the calling conventions of a0.  */
905       if (call_used_regs[FIRST_ADDRESS_REGNUM]
906 	  && ! fixed_regs[FIRST_ADDRESS_REGNUM])
907 	{
908 	  /* Insn: add -4 * num_regs_to_save, sp.  */
909 	  this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
910 	  /* Insn: mov sp, a0.  */
911 	  this_strategy_size++;
912 	  /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
913 	  this_strategy_size += 3 * num_regs_to_save;
914 	  if (size)
915 	    {
916 	      /* Insn: add -size, sp.  */
917 	      this_strategy_size += SIZE_ADD_SP (-size);
918 	    }
919 
920 	  if (this_strategy_size < strategy_size)
921 	    {
922 	      strategy = save_a0_no_merge;
923 	      strategy_size = this_strategy_size;
924 	    }
925 	}
926 
927       /* Emit the initial SP add, common to all strategies.  */
928       switch (strategy)
929 	{
930 	case save_sp_no_merge:
931 	case save_a0_no_merge:
932 	  F (emit_insn (gen_addsi3 (stack_pointer_rtx,
933 				    stack_pointer_rtx,
934 				    GEN_INT (-4 * num_regs_to_save))));
935 	  xsize = 0;
936 	  break;
937 
938 	case save_sp_partial_merge:
939 	  F (emit_insn (gen_addsi3 (stack_pointer_rtx,
940 				    stack_pointer_rtx,
941 				    GEN_INT (-128))));
942 	  xsize = 128 - 4 * num_regs_to_save;
943 	  size -= xsize;
944 	  break;
945 
946 	case save_sp_merge:
947 	case save_a0_merge:
948 	  F (emit_insn (gen_addsi3 (stack_pointer_rtx,
949 				    stack_pointer_rtx,
950 				    GEN_INT (-(size + 4 * num_regs_to_save)))));
951 	  /* We'll have to adjust FP register saves according to the
952 	     frame size.  */
953 	  xsize = size;
954 	  /* Since we've already created the stack frame, don't do it
955 	     again at the end of the function.  */
956 	  size = 0;
957 	  break;
958 
959 	default:
960 	  gcc_unreachable ();
961 	}
962 
963       /* Now prepare register a0, if we have decided to use it.  */
964       switch (strategy)
965 	{
966 	case save_sp_merge:
967 	case save_sp_no_merge:
968 	case save_sp_partial_merge:
969 	  reg = 0;
970 	  break;
971 
972 	case save_a0_merge:
973 	case save_a0_no_merge:
974 	  reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
975 	  F (emit_insn (gen_movsi (reg, stack_pointer_rtx)));
976 	  if (xsize)
977 	    F (emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize))));
978 	  reg = gen_rtx_POST_INC (SImode, reg);
979 	  break;
980 
981 	default:
982 	  gcc_unreachable ();
983 	}
984 
985       /* Now actually save the FP registers.  */
986       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
987 	if (df_regs_ever_live_p (i) && ! call_used_regs[i])
988 	  {
989 	    rtx addr;
990 
991 	    if (reg)
992 	      addr = reg;
993 	    else
994 	      {
995 		/* If we aren't using `a0', use an SP offset.  */
996 		if (xsize)
997 		  {
998 		    addr = gen_rtx_PLUS (SImode,
999 					 stack_pointer_rtx,
1000 					 GEN_INT (xsize));
1001 		  }
1002 		else
1003 		  addr = stack_pointer_rtx;
1004 
1005 		xsize += 4;
1006 	      }
1007 
1008 	    F (emit_insn (gen_movsf (gen_rtx_MEM (SFmode, addr),
1009 				     gen_rtx_REG (SFmode, i))));
1010 	  }
1011     }
1012 
1013   /* Now put the frame pointer into the frame pointer register.  */
1014   if (frame_pointer_needed)
1015     F (emit_move_insn (frame_pointer_rtx, stack_pointer_rtx));
1016 
1017   /* Allocate stack for this frame.  */
1018   if (size)
1019     F (emit_insn (gen_addsi3 (stack_pointer_rtx,
1020 			      stack_pointer_rtx,
1021 			      GEN_INT (-size))));
1022 
1023   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1024     emit_insn (gen_load_pic ());
1025 }
1026 
1027 void
mn10300_expand_epilogue(void)1028 mn10300_expand_epilogue (void)
1029 {
1030   HOST_WIDE_INT size = mn10300_frame_size ();
1031   unsigned int reg_save_bytes;
1032 
1033   mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1034 
1035   if (TARGET_AM33_2 && fp_regs_to_save ())
1036     {
1037       int num_regs_to_save = fp_regs_to_save (), i;
1038       rtx reg = 0;
1039 
1040       /* We have several options to restore FP registers.  We could
1041 	 load them from SP offsets, but, if there are enough FP
1042 	 registers to restore, we win if we use a post-increment
1043 	 addressing mode.  */
1044 
1045       /* If we have a frame pointer, it's the best option, because we
1046 	 already know it has the value we want.  */
1047       if (frame_pointer_needed)
1048 	reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
1049       /* Otherwise, we may use `a1', since it's call-clobbered and
1050 	 it's never used for return values.  But only do so if it's
1051 	 smaller than using SP offsets.  */
1052       else
1053 	{
1054 	  enum { restore_sp_post_adjust,
1055 		 restore_sp_pre_adjust,
1056 		 restore_sp_partial_adjust,
1057 		 restore_a1 } strategy;
1058 	  unsigned int this_strategy_size, strategy_size = (unsigned)-1;
1059 
1060 	  /* Consider using sp offsets before adjusting sp.  */
1061 	  /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
1062 	  this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
1063 	  /* If size is too large, we'll have to adjust SP with an
1064 		 add.  */
1065 	  if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1066 	    {
1067 	      /* Insn: add size + 4 * num_regs_to_save, sp.  */
1068 	      this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
1069 	    }
1070 	  /* If we don't have to restore any non-FP registers,
1071 		 we'll be able to save one byte by using rets.  */
1072 	  if (! reg_save_bytes)
1073 	    this_strategy_size--;
1074 
1075 	  if (this_strategy_size < strategy_size)
1076 	    {
1077 	      strategy = restore_sp_post_adjust;
1078 	      strategy_size = this_strategy_size;
1079 	    }
1080 
1081 	  /* Consider using sp offsets after adjusting sp.  */
1082 	  /* Insn: add size, sp.  */
1083 	  this_strategy_size = SIZE_ADD_SP (size);
1084 	  /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
1085 	  this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1086 	  /* We're going to use ret to release the FP registers
1087 		 save area, so, no savings.  */
1088 
1089 	  if (this_strategy_size < strategy_size)
1090 	    {
1091 	      strategy = restore_sp_pre_adjust;
1092 	      strategy_size = this_strategy_size;
1093 	    }
1094 
1095 	  /* Consider using sp offsets after partially adjusting sp.
1096 	     When size is close to 32Kb, we may be able to adjust SP
1097 	     with an imm16 add instruction while still using fmov
1098 	     (d8,sp).  */
1099 	  if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1100 	    {
1101 	      /* Insn: add size + 4 * num_regs_to_save
1102 				+ reg_save_bytes - 252,sp.  */
1103 	      this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
1104 						+ (int) reg_save_bytes - 252);
1105 	      /* Insn: fmov (##,sp),fs#, fo each fs# to be restored.  */
1106 	      this_strategy_size += SIZE_FMOV_SP (252 - reg_save_bytes
1107 						  - 4 * num_regs_to_save,
1108 						  num_regs_to_save);
1109 	      /* We're going to use ret to release the FP registers
1110 		 save area, so, no savings.  */
1111 
1112 	      if (this_strategy_size < strategy_size)
1113 		{
1114 		  strategy = restore_sp_partial_adjust;
1115 		  strategy_size = this_strategy_size;
1116 		}
1117 	    }
1118 
1119 	  /* Consider using a1 in post-increment mode, as long as the
1120 	     user hasn't changed the calling conventions of a1.  */
1121 	  if (call_used_regs[FIRST_ADDRESS_REGNUM + 1]
1122 	      && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1123 	    {
1124 	      /* Insn: mov sp,a1.  */
1125 	      this_strategy_size = 1;
1126 	      if (size)
1127 		{
1128 		  /* Insn: add size,a1.  */
1129 		  this_strategy_size += SIZE_ADD_AX (size);
1130 		}
1131 	      /* Insn: fmov (a1+),fs#, for each fs# to be restored.  */
1132 	      this_strategy_size += 3 * num_regs_to_save;
1133 	      /* If size is large enough, we may be able to save a
1134 		 couple of bytes.  */
1135 	      if (size + 4 * num_regs_to_save + reg_save_bytes > 255)
1136 		{
1137 		  /* Insn: mov a1,sp.  */
1138 		  this_strategy_size += 2;
1139 		}
1140 	      /* If we don't have to restore any non-FP registers,
1141 		 we'll be able to save one byte by using rets.  */
1142 	      if (! reg_save_bytes)
1143 		this_strategy_size--;
1144 
1145 	      if (this_strategy_size < strategy_size)
1146 		{
1147 		  strategy = restore_a1;
1148 		  strategy_size = this_strategy_size;
1149 		}
1150 	    }
1151 
1152 	  switch (strategy)
1153 	    {
1154 	    case restore_sp_post_adjust:
1155 	      break;
1156 
1157 	    case restore_sp_pre_adjust:
1158 	      emit_insn (gen_addsi3 (stack_pointer_rtx,
1159 				     stack_pointer_rtx,
1160 				     GEN_INT (size)));
1161 	      size = 0;
1162 	      break;
1163 
1164 	    case restore_sp_partial_adjust:
1165 	      emit_insn (gen_addsi3 (stack_pointer_rtx,
1166 				     stack_pointer_rtx,
1167 				     GEN_INT (size + 4 * num_regs_to_save
1168 					      + reg_save_bytes - 252)));
1169 	      size = 252 - reg_save_bytes - 4 * num_regs_to_save;
1170 	      break;
1171 
1172 	    case restore_a1:
1173 	      reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1174 	      emit_insn (gen_movsi (reg, stack_pointer_rtx));
1175 	      if (size)
1176 		emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1177 	      break;
1178 
1179 	    default:
1180 	      gcc_unreachable ();
1181 	    }
1182 	}
1183 
1184       /* Adjust the selected register, if any, for post-increment.  */
1185       if (reg)
1186 	reg = gen_rtx_POST_INC (SImode, reg);
1187 
1188       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1189 	if (df_regs_ever_live_p (i) && ! call_used_regs[i])
1190 	  {
1191 	    rtx addr;
1192 
1193 	    if (reg)
1194 	      addr = reg;
1195 	    else if (size)
1196 	      {
1197 		/* If we aren't using a post-increment register, use an
1198 		   SP offset.  */
1199 		addr = gen_rtx_PLUS (SImode,
1200 				     stack_pointer_rtx,
1201 				     GEN_INT (size));
1202 	      }
1203 	    else
1204 	      addr = stack_pointer_rtx;
1205 
1206 	    size += 4;
1207 
1208 	    emit_insn (gen_movsf (gen_rtx_REG (SFmode, i),
1209 				  gen_rtx_MEM (SFmode, addr)));
1210 	  }
1211 
1212       /* If we were using the restore_a1 strategy and the number of
1213 	 bytes to be released won't fit in the `ret' byte, copy `a1'
1214 	 to `sp', to avoid having to use `add' to adjust it.  */
1215       if (! frame_pointer_needed && reg && size + reg_save_bytes > 255)
1216 	{
1217 	  emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1218 	  size = 0;
1219 	}
1220     }
1221 
1222   /* Maybe cut back the stack, except for the register save area.
1223 
1224      If the frame pointer exists, then use the frame pointer to
1225      cut back the stack.
1226 
1227      If the stack size + register save area is more than 255 bytes,
1228      then the stack must be cut back here since the size + register
1229      save size is too big for a ret/retf instruction.
1230 
1231      Else leave it alone, it will be cut back as part of the
1232      ret/retf instruction, or there wasn't any stack to begin with.
1233 
1234      Under no circumstances should the register save area be
1235      deallocated here, that would leave a window where an interrupt
1236      could occur and trash the register save area.  */
1237   if (frame_pointer_needed)
1238     {
1239       emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1240       size = 0;
1241     }
1242   else if (size + reg_save_bytes > 255)
1243     {
1244       emit_insn (gen_addsi3 (stack_pointer_rtx,
1245 			     stack_pointer_rtx,
1246 			     GEN_INT (size)));
1247       size = 0;
1248     }
1249 
1250   /* Adjust the stack and restore callee-saved registers, if any.  */
1251   if (mn10300_can_use_rets_insn ())
1252     emit_jump_insn (ret_rtx);
1253   else
1254     emit_jump_insn (gen_return_ret (GEN_INT (size + reg_save_bytes)));
1255 }
1256 
1257 /* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
1258    This function is for MATCH_PARALLEL and so assumes OP is known to be
1259    parallel.  If OP is a multiple store, return a mask indicating which
1260    registers it saves.  Return 0 otherwise.  */
1261 
1262 unsigned int
mn10300_store_multiple_regs(rtx op)1263 mn10300_store_multiple_regs (rtx op)
1264 {
1265   int count;
1266   int mask;
1267   int i;
1268   unsigned int last;
1269   rtx elt;
1270 
1271   count = XVECLEN (op, 0);
1272   if (count < 2)
1273     return 0;
1274 
1275   /* Check that first instruction has the form (set (sp) (plus A B)) */
1276   elt = XVECEXP (op, 0, 0);
1277   if (GET_CODE (elt) != SET
1278       || (! REG_P (SET_DEST (elt)))
1279       || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1280       || GET_CODE (SET_SRC (elt)) != PLUS)
1281     return 0;
1282 
1283   /* Check that A is the stack pointer and B is the expected stack size.
1284      For OP to match, each subsequent instruction should push a word onto
1285      the stack.  We therefore expect the first instruction to create
1286      COUNT-1 stack slots.  */
1287   elt = SET_SRC (elt);
1288   if ((! REG_P (XEXP (elt, 0)))
1289       || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1290       || (! CONST_INT_P (XEXP (elt, 1)))
1291       || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1292     return 0;
1293 
1294   mask = 0;
1295   for (i = 1; i < count; i++)
1296     {
1297       /* Check that element i is a (set (mem M) R).  */
1298       /* ??? Validate the register order a-la mn10300_gen_multiple_store.
1299 	 Remember: the ordering is *not* monotonic.  */
1300       elt = XVECEXP (op, 0, i);
1301       if (GET_CODE (elt) != SET
1302 	  || (! MEM_P (SET_DEST (elt)))
1303 	  || (! REG_P (SET_SRC (elt))))
1304 	return 0;
1305 
1306       /* Remember which registers are to be saved.  */
1307       last = REGNO (SET_SRC (elt));
1308       mask |= (1 << last);
1309 
1310       /* Check that M has the form (plus (sp) (const_int -I*4)) */
1311       elt = XEXP (SET_DEST (elt), 0);
1312       if (GET_CODE (elt) != PLUS
1313 	  || (! REG_P (XEXP (elt, 0)))
1314 	  || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1315 	  || (! CONST_INT_P (XEXP (elt, 1)))
1316 	  || INTVAL (XEXP (elt, 1)) != -i * 4)
1317 	return 0;
1318     }
1319 
1320   /* All or none of the callee-saved extended registers must be in the set.  */
1321   if ((mask & 0x3c000) != 0
1322       && (mask & 0x3c000) != 0x3c000)
1323     return 0;
1324 
1325   return mask;
1326 }
1327 
1328 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
1329 
1330 static reg_class_t
mn10300_preferred_reload_class(rtx x,reg_class_t rclass)1331 mn10300_preferred_reload_class (rtx x, reg_class_t rclass)
1332 {
1333   if (x == stack_pointer_rtx && rclass != SP_REGS)
1334     return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1335   else if (MEM_P (x)
1336 	   || (REG_P (x)
1337 	       && !HARD_REGISTER_P (x))
1338 	   || (GET_CODE (x) == SUBREG
1339 	       && REG_P (SUBREG_REG (x))
1340 	       && !HARD_REGISTER_P (SUBREG_REG (x))))
1341     return LIMIT_RELOAD_CLASS (GET_MODE (x), rclass);
1342   else
1343     return rclass;
1344 }
1345 
1346 /* Implement TARGET_PREFERRED_OUTPUT_RELOAD_CLASS.  */
1347 
1348 static reg_class_t
mn10300_preferred_output_reload_class(rtx x,reg_class_t rclass)1349 mn10300_preferred_output_reload_class (rtx x, reg_class_t rclass)
1350 {
1351   if (x == stack_pointer_rtx && rclass != SP_REGS)
1352     return (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
1353   return rclass;
1354 }
1355 
1356 /* Implement TARGET_SECONDARY_RELOAD.  */
1357 
1358 static reg_class_t
mn10300_secondary_reload(bool in_p,rtx x,reg_class_t rclass_i,machine_mode mode,secondary_reload_info * sri)1359 mn10300_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
1360 			  machine_mode mode, secondary_reload_info *sri)
1361 {
1362   enum reg_class rclass = (enum reg_class) rclass_i;
1363   enum reg_class xclass = NO_REGS;
1364   unsigned int xregno = INVALID_REGNUM;
1365 
1366   if (REG_P (x))
1367     {
1368       xregno = REGNO (x);
1369       if (xregno >= FIRST_PSEUDO_REGISTER)
1370 	xregno = true_regnum (x);
1371       if (xregno != INVALID_REGNUM)
1372 	xclass = REGNO_REG_CLASS (xregno);
1373     }
1374 
1375   if (!TARGET_AM33)
1376     {
1377       /* Memory load/stores less than a full word wide can't have an
1378          address or stack pointer destination.  They must use a data
1379          register as an intermediate register.  */
1380       if (rclass != DATA_REGS
1381 	  && (mode == QImode || mode == HImode)
1382 	  && xclass == NO_REGS)
1383 	return DATA_REGS;
1384 
1385       /* We can only move SP to/from an address register.  */
1386       if (in_p
1387 	  && rclass == SP_REGS
1388 	  && xclass != ADDRESS_REGS)
1389 	return ADDRESS_REGS;
1390       if (!in_p
1391 	  && xclass == SP_REGS
1392 	  && rclass != ADDRESS_REGS
1393 	  && rclass != SP_OR_ADDRESS_REGS)
1394 	return ADDRESS_REGS;
1395     }
1396 
1397   /* We can't directly load sp + const_int into a register;
1398      we must use an address register as an scratch.  */
1399   if (in_p
1400       && rclass != SP_REGS
1401       && rclass != SP_OR_ADDRESS_REGS
1402       && rclass != SP_OR_GENERAL_REGS
1403       && GET_CODE (x) == PLUS
1404       && (XEXP (x, 0) == stack_pointer_rtx
1405 	  || XEXP (x, 1) == stack_pointer_rtx))
1406     {
1407       sri->icode = CODE_FOR_reload_plus_sp_const;
1408       return NO_REGS;
1409     }
1410 
1411   /* We can only move MDR to/from a data register.  */
1412   if (rclass == MDR_REGS && xclass != DATA_REGS)
1413     return DATA_REGS;
1414   if (xclass == MDR_REGS && rclass != DATA_REGS)
1415     return DATA_REGS;
1416 
1417   /* We can't load/store an FP register from a constant address.  */
1418   if (TARGET_AM33_2
1419       && (rclass == FP_REGS || xclass == FP_REGS)
1420       && (xclass == NO_REGS || rclass == NO_REGS))
1421     {
1422       rtx addr = NULL;
1423 
1424       if (xregno >= FIRST_PSEUDO_REGISTER && xregno != INVALID_REGNUM)
1425 	{
1426 	  addr = reg_equiv_mem (xregno);
1427 	  if (addr)
1428 	    addr = XEXP (addr, 0);
1429 	}
1430       else if (MEM_P (x))
1431 	addr = XEXP (x, 0);
1432 
1433       if (addr && CONSTANT_ADDRESS_P (addr))
1434 	return GENERAL_REGS;
1435     }
1436   /* Otherwise assume no secondary reloads are needed.  */
1437   return NO_REGS;
1438 }
1439 
1440 int
mn10300_frame_size(void)1441 mn10300_frame_size (void)
1442 {
1443   /* size includes the fixed stack space needed for function calls.  */
1444   int size = get_frame_size () + crtl->outgoing_args_size;
1445 
1446   /* And space for the return pointer.  */
1447   size += crtl->outgoing_args_size ? 4 : 0;
1448 
1449   return size;
1450 }
1451 
1452 int
mn10300_initial_offset(int from,int to)1453 mn10300_initial_offset (int from, int to)
1454 {
1455   int diff = 0;
1456 
1457   gcc_assert (from == ARG_POINTER_REGNUM || from == FRAME_POINTER_REGNUM);
1458   gcc_assert (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
1459 
1460   if (to == STACK_POINTER_REGNUM)
1461     diff = mn10300_frame_size ();
1462 
1463   /* The difference between the argument pointer and the frame pointer
1464      is the size of the callee register save area.  */
1465   if (from == ARG_POINTER_REGNUM)
1466     {
1467       unsigned int reg_save_bytes;
1468 
1469       mn10300_get_live_callee_saved_regs (& reg_save_bytes);
1470       diff += reg_save_bytes;
1471       diff += 4 * fp_regs_to_save ();
1472     }
1473 
1474   return diff;
1475 }
1476 
1477 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
1478 
1479 static bool
mn10300_return_in_memory(const_tree type,const_tree fntype ATTRIBUTE_UNUSED)1480 mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1481 {
1482   /* Return values > 8 bytes in length in memory.  */
1483   return (int_size_in_bytes (type) > 8
1484 	  || int_size_in_bytes (type) == 0
1485 	  || TYPE_MODE (type) == BLKmode);
1486 }
1487 
1488 /* Flush the argument registers to the stack for a stdarg function;
1489    return the new argument pointer.  */
1490 static rtx
mn10300_builtin_saveregs(void)1491 mn10300_builtin_saveregs (void)
1492 {
1493   rtx offset, mem;
1494   tree fntype = TREE_TYPE (current_function_decl);
1495   int argadj = ((!stdarg_p (fntype))
1496                 ? UNITS_PER_WORD : 0);
1497   alias_set_type set = get_varargs_alias_set ();
1498 
1499   if (argadj)
1500     offset = plus_constant (Pmode, crtl->args.arg_offset_rtx, argadj);
1501   else
1502     offset = crtl->args.arg_offset_rtx;
1503 
1504   mem = gen_rtx_MEM (SImode, crtl->args.internal_arg_pointer);
1505   set_mem_alias_set (mem, set);
1506   emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1507 
1508   mem = gen_rtx_MEM (SImode,
1509 		     plus_constant (Pmode,
1510 				    crtl->args.internal_arg_pointer, 4));
1511   set_mem_alias_set (mem, set);
1512   emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1513 
1514   return copy_to_reg (expand_binop (Pmode, add_optab,
1515 				    crtl->args.internal_arg_pointer,
1516 				    offset, 0, 0, OPTAB_LIB_WIDEN));
1517 }
1518 
1519 static void
mn10300_va_start(tree valist,rtx nextarg)1520 mn10300_va_start (tree valist, rtx nextarg)
1521 {
1522   nextarg = expand_builtin_saveregs ();
1523   std_expand_builtin_va_start (valist, nextarg);
1524 }
1525 
1526 /* Return true when a parameter should be passed by reference.  */
1527 
1528 static bool
mn10300_pass_by_reference(cumulative_args_t,const function_arg_info & arg)1529 mn10300_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
1530 {
1531   unsigned HOST_WIDE_INT size = arg.type_size_in_bytes ();
1532   return (size > 8 || size == 0);
1533 }
1534 
1535 /* Return an RTX to represent where argument ARG will be passed to a function.
1536    If the result is NULL_RTX, the argument is pushed.  */
1537 
1538 static rtx
mn10300_function_arg(cumulative_args_t cum_v,const function_arg_info & arg)1539 mn10300_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
1540 {
1541   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1542   rtx result = NULL_RTX;
1543   int size;
1544 
1545   /* We only support using 2 data registers as argument registers.  */
1546   int nregs = 2;
1547 
1548   /* Figure out the size of the object to be passed.  */
1549   size = arg.promoted_size_in_bytes ();
1550   cum->nbytes = (cum->nbytes + 3) & ~3;
1551 
1552   /* Don't pass this arg via a register if all the argument registers
1553      are used up.  */
1554   if (cum->nbytes > nregs * UNITS_PER_WORD)
1555     return result;
1556 
1557   /* Don't pass this arg via a register if it would be split between
1558      registers and memory.  */
1559   if (arg.type == NULL_TREE
1560       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1561     return result;
1562 
1563   switch (cum->nbytes / UNITS_PER_WORD)
1564     {
1565     case 0:
1566       result = gen_rtx_REG (arg.mode, FIRST_ARGUMENT_REGNUM);
1567       break;
1568     case 1:
1569       result = gen_rtx_REG (arg.mode, FIRST_ARGUMENT_REGNUM + 1);
1570       break;
1571     default:
1572       break;
1573     }
1574 
1575   return result;
1576 }
1577 
1578 /* Update the data in CUM to advance over argument ARG.  */
1579 
1580 static void
mn10300_function_arg_advance(cumulative_args_t cum_v,const function_arg_info & arg)1581 mn10300_function_arg_advance (cumulative_args_t cum_v,
1582 			      const function_arg_info &arg)
1583 {
1584   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1585 
1586   cum->nbytes += (arg.promoted_size_in_bytes () + 3) & ~3;
1587 }
1588 
1589 /* Return the number of bytes of registers to use for an argument passed
1590    partially in registers and partially in memory.  */
1591 
1592 static int
mn10300_arg_partial_bytes(cumulative_args_t cum_v,const function_arg_info & arg)1593 mn10300_arg_partial_bytes (cumulative_args_t cum_v,
1594 			   const function_arg_info &arg)
1595 {
1596   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1597   int size;
1598 
1599   /* We only support using 2 data registers as argument registers.  */
1600   int nregs = 2;
1601 
1602   /* Figure out the size of the object to be passed.  */
1603   size = arg.promoted_size_in_bytes ();
1604   cum->nbytes = (cum->nbytes + 3) & ~3;
1605 
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;
1610 
1611   if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1612     return 0;
1613 
1614   /* Don't pass this arg via a register if it would be split between
1615      registers and memory.  */
1616   if (arg.type == NULL_TREE
1617       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1618     return 0;
1619 
1620   return nregs * UNITS_PER_WORD - cum->nbytes;
1621 }
1622 
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.  */
1628 
1629 static rtx
mn10300_function_value(const_tree valtype,const_tree fn_decl_or_type ATTRIBUTE_UNUSED,bool outgoing)1630 mn10300_function_value (const_tree valtype,
1631 			const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1632 			bool outgoing)
1633 {
1634   rtx rv;
1635   machine_mode mode = TYPE_MODE (valtype);
1636 
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);
1642 
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));
1648 
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;
1654 }
1655 
1656 /* Implements TARGET_LIBCALL_VALUE.  */
1657 
1658 static rtx
mn10300_libcall_value(machine_mode mode,const_rtx fun ATTRIBUTE_UNUSED)1659 mn10300_libcall_value (machine_mode mode,
1660 		       const_rtx fun ATTRIBUTE_UNUSED)
1661 {
1662   return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1663 }
1664 
1665 /* Implements FUNCTION_VALUE_REGNO_P.  */
1666 
1667 bool
mn10300_function_value_regno_p(const unsigned int regno)1668 mn10300_function_value_regno_p (const unsigned int regno)
1669 {
1670  return (regno == FIRST_DATA_REGNUM || regno == FIRST_ADDRESS_REGNUM);
1671 }
1672 
1673 /* Output an addition operation.  */
1674 
1675 const char *
mn10300_output_add(rtx operands[3],bool need_flags)1676 mn10300_output_add (rtx operands[3], bool need_flags)
1677 {
1678   rtx dest, src1, src2;
1679   unsigned int dest_regnum, src1_regnum, src2_regnum;
1680   enum reg_class src1_class, src2_class, dest_class;
1681 
1682   dest = operands[0];
1683   src1 = operands[1];
1684   src2 = operands[2];
1685 
1686   dest_regnum = true_regnum (dest);
1687   src1_regnum = true_regnum (src1);
1688 
1689   dest_class = REGNO_REG_CLASS (dest_regnum);
1690   src1_class = REGNO_REG_CLASS (src1_regnum);
1691 
1692   if (CONST_INT_P (src2))
1693     {
1694       gcc_assert (dest_regnum == src1_regnum);
1695 
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";
1700 
1701       gcc_assert (!need_flags || dest_class != SP_REGS);
1702       return "add %2,%0";
1703     }
1704   else if (CONSTANT_P (src2))
1705     return "add %2,%0";
1706 
1707   src2_regnum = true_regnum (src2);
1708   src2_class = REGNO_REG_CLASS (src2_regnum);
1709 
1710   if (dest_regnum == src1_regnum)
1711     return "add %2,%0";
1712   if (dest_regnum == src2_regnum)
1713     return "add %1,%0";
1714 
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";
1723 
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)
1728     {
1729       /* We have to copy one of the sources into the destination, then
1730          add the other source to the destination.
1731 
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";
1741     }
1742 
1743   /* At least one register is an extended register.  */
1744 
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";
1750 
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.
1756 
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";
1763 }
1764 
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.  */
1768 
1769 int
mn10300_symbolic_operand(rtx op,machine_mode mode ATTRIBUTE_UNUSED)1770 mn10300_symbolic_operand (rtx op,
1771 			  machine_mode mode ATTRIBUTE_UNUSED)
1772 {
1773   switch (GET_CODE (op))
1774     {
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;
1785     }
1786 }
1787 
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.
1791 
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.
1794 
1795    Normally it is always safe for this macro to do nothing.  It exists to
1796    recognize opportunities to optimize the output.
1797 
1798    But on a few ports with segmented architectures and indexed addressing
1799    (mn10300, hppa) it is used to rewrite certain problematical addresses.  */
1800 
1801 static rtx
mn10300_legitimize_address(rtx x,rtx oldx ATTRIBUTE_UNUSED,machine_mode mode ATTRIBUTE_UNUSED)1802 mn10300_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1803 			    machine_mode mode ATTRIBUTE_UNUSED)
1804 {
1805   if (flag_pic && ! mn10300_legitimate_pic_operand_p (x))
1806     x = mn10300_legitimize_pic_address (oldx, NULL_RTX);
1807 
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))
1813     {
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.  */
1817 
1818       rtx regx1, regy1, regy2, y;
1819 
1820       /* Strip off any CONST.  */
1821       y = XEXP (x, 1);
1822       if (GET_CODE (y) == CONST)
1823         y = XEXP (y, 0);
1824 
1825       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1826 	{
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));
1834 	}
1835     }
1836   return x;
1837 }
1838 
1839 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
1840    @GOTOFF in `reg'.  */
1841 
1842 rtx
mn10300_legitimize_pic_address(rtx orig,rtx reg)1843 mn10300_legitimize_pic_address (rtx orig, rtx reg)
1844 {
1845   rtx x;
1846   rtx_insn *insn;
1847 
1848   if (GET_CODE (orig) == LABEL_REF
1849       || (GET_CODE (orig) == SYMBOL_REF
1850 	  && (CONSTANT_POOL_ADDRESS_P (orig)
1851 	      || ! MN10300_GLOBAL_P (orig))))
1852     {
1853       if (reg == NULL)
1854 	reg = gen_reg_rtx (Pmode);
1855 
1856       x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOTOFF);
1857       x = gen_rtx_CONST (SImode, x);
1858       emit_move_insn (reg, x);
1859 
1860       insn = emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
1861     }
1862   else if (GET_CODE (orig) == SYMBOL_REF)
1863     {
1864       if (reg == NULL)
1865 	reg = gen_reg_rtx (Pmode);
1866 
1867       x = gen_rtx_UNSPEC (SImode, gen_rtvec (1, orig), UNSPEC_GOT);
1868       x = gen_rtx_CONST (SImode, x);
1869       x = gen_rtx_PLUS (SImode, pic_offset_table_rtx, x);
1870       x = gen_const_mem (SImode, x);
1871 
1872       insn = emit_move_insn (reg, x);
1873     }
1874   else
1875     return orig;
1876 
1877   set_unique_reg_note (insn, REG_EQUAL, orig);
1878   return reg;
1879 }
1880 
1881 /* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
1882    isn't protected by a PIC unspec; nonzero otherwise.  */
1883 
1884 int
mn10300_legitimate_pic_operand_p(rtx x)1885 mn10300_legitimate_pic_operand_p (rtx x)
1886 {
1887   const char *fmt;
1888   int i;
1889 
1890   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1891     return 0;
1892 
1893   if (GET_CODE (x) == UNSPEC
1894       && (XINT (x, 1) == UNSPEC_PIC
1895 	  || XINT (x, 1) == UNSPEC_GOT
1896 	  || XINT (x, 1) == UNSPEC_GOTOFF
1897 	  || XINT (x, 1) == UNSPEC_PLT
1898 	  || XINT (x, 1) == UNSPEC_GOTSYM_OFF))
1899       return 1;
1900 
1901   fmt = GET_RTX_FORMAT (GET_CODE (x));
1902   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1903     {
1904       if (fmt[i] == 'E')
1905 	{
1906 	  int j;
1907 
1908 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1909 	    if (! mn10300_legitimate_pic_operand_p (XVECEXP (x, i, j)))
1910 	      return 0;
1911 	}
1912       else if (fmt[i] == 'e'
1913 	       && ! mn10300_legitimate_pic_operand_p (XEXP (x, i)))
1914 	return 0;
1915     }
1916 
1917   return 1;
1918 }
1919 
1920 /* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1921    legitimate, and FALSE otherwise.
1922 
1923    On the mn10300, the value in the address register must be
1924    in the same memory space/segment as the effective address.
1925 
1926    This is problematical for reload since it does not understand
1927    that base+index != index+base in a memory reference.
1928 
1929    Note it is still possible to use reg+reg addressing modes,
1930    it's just much more difficult.  For a discussion of a possible
1931    workaround and solution, see the comments in pa.c before the
1932    function record_unscaled_index_insn_codes.  */
1933 
1934 static bool
mn10300_legitimate_address_p(machine_mode mode,rtx x,bool strict)1935 mn10300_legitimate_address_p (machine_mode mode, rtx x, bool strict)
1936 {
1937   rtx base, index;
1938 
1939   if (CONSTANT_ADDRESS_P (x))
1940     return !flag_pic || mn10300_legitimate_pic_operand_p (x);
1941 
1942   if (RTX_OK_FOR_BASE_P (x, strict))
1943     return true;
1944 
1945   if (TARGET_AM33 && (mode == SImode || mode == SFmode || mode == HImode))
1946     {
1947       if (GET_CODE (x) == POST_INC)
1948 	return RTX_OK_FOR_BASE_P (XEXP (x, 0), strict);
1949       if (GET_CODE (x) == POST_MODIFY)
1950 	return (RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1951 		&& CONSTANT_ADDRESS_P (XEXP (x, 1)));
1952     }
1953 
1954   if (GET_CODE (x) != PLUS)
1955     return false;
1956 
1957   base = XEXP (x, 0);
1958   index = XEXP (x, 1);
1959 
1960   if (!REG_P (base))
1961     return false;
1962   if (REG_P (index))
1963     {
1964       /* ??? Without AM33 generalized (Ri,Rn) addressing, reg+reg
1965 	 addressing is hard to satisfy.  */
1966       if (!TARGET_AM33)
1967 	return false;
1968 
1969       return (REGNO_GENERAL_P (REGNO (base), strict)
1970 	      && REGNO_GENERAL_P (REGNO (index), strict));
1971     }
1972 
1973   if (!REGNO_STRICT_OK_FOR_BASE_P (REGNO (base), strict))
1974     return false;
1975 
1976   if (CONST_INT_P (index))
1977     return IN_RANGE (INTVAL (index), -1 - 0x7fffffff, 0x7fffffff);
1978 
1979   if (CONSTANT_ADDRESS_P (index))
1980     return !flag_pic || mn10300_legitimate_pic_operand_p (index);
1981 
1982   return false;
1983 }
1984 
1985 bool
mn10300_regno_in_class_p(unsigned regno,int rclass,bool strict)1986 mn10300_regno_in_class_p (unsigned regno, int rclass, bool strict)
1987 {
1988   if (regno >= FIRST_PSEUDO_REGISTER)
1989     {
1990       if (!strict)
1991 	return true;
1992       if (!reg_renumber)
1993 	return false;
1994       regno = reg_renumber[regno];
1995       if (regno == INVALID_REGNUM)
1996 	return false;
1997     }
1998   return TEST_HARD_REG_BIT (reg_class_contents[rclass], regno);
1999 }
2000 
2001 rtx
mn10300_legitimize_reload_address(rtx x,machine_mode mode ATTRIBUTE_UNUSED,int opnum,int type,int ind_levels ATTRIBUTE_UNUSED)2002 mn10300_legitimize_reload_address (rtx x,
2003 				   machine_mode mode ATTRIBUTE_UNUSED,
2004 				   int opnum, int type,
2005 				   int ind_levels ATTRIBUTE_UNUSED)
2006 {
2007   bool any_change = false;
2008 
2009   /* See above re disabling reg+reg addressing for MN103.  */
2010   if (!TARGET_AM33)
2011     return NULL_RTX;
2012 
2013   if (GET_CODE (x) != PLUS)
2014     return NULL_RTX;
2015 
2016   if (XEXP (x, 0) == stack_pointer_rtx)
2017     {
2018       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2019 		   GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2020 		   opnum, (enum reload_type) type);
2021       any_change = true;
2022     }
2023   if (XEXP (x, 1) == stack_pointer_rtx)
2024     {
2025       push_reload (XEXP (x, 1), NULL_RTX, &XEXP (x, 1), NULL,
2026 		   GENERAL_REGS, GET_MODE (x), VOIDmode, 0, 0,
2027 		   opnum, (enum reload_type) type);
2028       any_change = true;
2029     }
2030 
2031   return any_change ? x : NULL_RTX;
2032 }
2033 
2034 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  Returns TRUE if X is a valid
2035    constant.  Note that some "constants" aren't valid, such as TLS
2036    symbols and unconverted GOT-based references, so we eliminate
2037    those here.  */
2038 
2039 static bool
mn10300_legitimate_constant_p(machine_mode mode ATTRIBUTE_UNUSED,rtx x)2040 mn10300_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2041 {
2042   switch (GET_CODE (x))
2043     {
2044     case CONST:
2045       x = XEXP (x, 0);
2046 
2047       if (GET_CODE (x) == PLUS)
2048 	{
2049 	  if (! CONST_INT_P (XEXP (x, 1)))
2050 	    return false;
2051 	  x = XEXP (x, 0);
2052 	}
2053 
2054       /* Only some unspecs are valid as "constants".  */
2055       if (GET_CODE (x) == UNSPEC)
2056 	{
2057 	  switch (XINT (x, 1))
2058 	    {
2059 	    case UNSPEC_PIC:
2060 	    case UNSPEC_GOT:
2061 	    case UNSPEC_GOTOFF:
2062 	    case UNSPEC_PLT:
2063 	      return true;
2064 	    default:
2065 	      return false;
2066 	    }
2067 	}
2068 
2069       /* We must have drilled down to a symbol.  */
2070       if (! mn10300_symbolic_operand (x, Pmode))
2071 	return false;
2072       break;
2073 
2074     default:
2075       break;
2076     }
2077 
2078   return true;
2079 }
2080 
2081 /* Undo pic address legitimization for the benefit of debug info.  */
2082 
2083 static rtx
mn10300_delegitimize_address(rtx orig_x)2084 mn10300_delegitimize_address (rtx orig_x)
2085 {
2086   rtx x = orig_x, ret, addend = NULL;
2087   bool need_mem;
2088 
2089   if (MEM_P (x))
2090     x = XEXP (x, 0);
2091   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
2092     return orig_x;
2093 
2094   if (XEXP (x, 0) == pic_offset_table_rtx)
2095     ;
2096   /* With the REG+REG addressing of AM33, var-tracking can re-assemble
2097      some odd-looking "addresses" that were never valid in the first place.
2098      We need to look harder to avoid warnings being emitted.  */
2099   else if (GET_CODE (XEXP (x, 0)) == PLUS)
2100     {
2101       rtx x0 = XEXP (x, 0);
2102       rtx x00 = XEXP (x0, 0);
2103       rtx x01 = XEXP (x0, 1);
2104 
2105       if (x00 == pic_offset_table_rtx)
2106 	addend = x01;
2107       else if (x01 == pic_offset_table_rtx)
2108 	addend = x00;
2109       else
2110 	return orig_x;
2111 
2112     }
2113   else
2114     return orig_x;
2115   x = XEXP (x, 1);
2116 
2117   if (GET_CODE (x) != CONST)
2118     return orig_x;
2119   x = XEXP (x, 0);
2120   if (GET_CODE (x) != UNSPEC)
2121     return orig_x;
2122 
2123   ret = XVECEXP (x, 0, 0);
2124   if (XINT (x, 1) == UNSPEC_GOTOFF)
2125     need_mem = false;
2126   else if (XINT (x, 1) == UNSPEC_GOT)
2127     need_mem = true;
2128   else
2129     return orig_x;
2130 
2131   gcc_assert (GET_CODE (ret) == SYMBOL_REF);
2132   if (need_mem != MEM_P (orig_x))
2133     return orig_x;
2134   if (need_mem && addend)
2135     return orig_x;
2136   if (addend)
2137     ret = gen_rtx_PLUS (Pmode, addend, ret);
2138   return ret;
2139 }
2140 
2141 /* For addresses, costs are relative to "MOV (Rm),Rn".  For AM33 this is
2142    the 3-byte fully general instruction; for MN103 this is the 2-byte form
2143    with an address register.  */
2144 
2145 static int
mn10300_address_cost(rtx x,machine_mode mode ATTRIBUTE_UNUSED,addr_space_t as ATTRIBUTE_UNUSED,bool speed)2146 mn10300_address_cost (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
2147 		      addr_space_t as ATTRIBUTE_UNUSED, bool speed)
2148 {
2149   HOST_WIDE_INT i;
2150   rtx base, index;
2151 
2152   switch (GET_CODE (x))
2153     {
2154     case CONST:
2155     case SYMBOL_REF:
2156     case LABEL_REF:
2157       /* We assume all of these require a 32-bit constant, even though
2158 	 some symbol and label references can be relaxed.  */
2159       return speed ? 1 : 4;
2160 
2161     case REG:
2162     case SUBREG:
2163     case POST_INC:
2164       return 0;
2165 
2166     case POST_MODIFY:
2167       /* Assume any symbolic offset is a 32-bit constant.  */
2168       i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2169       if (IN_RANGE (i, -128, 127))
2170 	return speed ? 0 : 1;
2171       if (speed)
2172 	return 1;
2173       if (IN_RANGE (i, -0x800000, 0x7fffff))
2174 	return 3;
2175       return 4;
2176 
2177     case PLUS:
2178       base = XEXP (x, 0);
2179       index = XEXP (x, 1);
2180       if (register_operand (index, SImode))
2181 	{
2182 	  /* Attempt to minimize the number of registers in the address.
2183 	     This is similar to what other ports do.  */
2184 	  if (register_operand (base, SImode))
2185 	    return 1;
2186 
2187 	  base = XEXP (x, 1);
2188 	  index = XEXP (x, 0);
2189 	}
2190 
2191       /* Assume any symbolic offset is a 32-bit constant.  */
2192       i = (CONST_INT_P (XEXP (x, 1)) ? INTVAL (XEXP (x, 1)) : 0x12345678);
2193       if (IN_RANGE (i, -128, 127))
2194 	return speed ? 0 : 1;
2195       if (IN_RANGE (i, -32768, 32767))
2196 	return speed ? 0 : 2;
2197       return speed ? 2 : 6;
2198 
2199     default:
2200       return rtx_cost (x, Pmode, MEM, 0, speed);
2201     }
2202 }
2203 
2204 /* Implement the TARGET_REGISTER_MOVE_COST hook.
2205 
2206    Recall that the base value of 2 is required by assumptions elsewhere
2207    in the body of the compiler, and that cost 2 is special-cased as an
2208    early exit from reload meaning no work is required.  */
2209 
2210 static int
mn10300_register_move_cost(machine_mode mode ATTRIBUTE_UNUSED,reg_class_t ifrom,reg_class_t ito)2211 mn10300_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2212 			    reg_class_t ifrom, reg_class_t ito)
2213 {
2214   enum reg_class from = (enum reg_class) ifrom;
2215   enum reg_class to = (enum reg_class) ito;
2216   enum reg_class scratch, test;
2217 
2218   /* Simplify the following code by unifying the fp register classes.  */
2219   if (to == FP_ACC_REGS)
2220     to = FP_REGS;
2221   if (from == FP_ACC_REGS)
2222     from = FP_REGS;
2223 
2224   /* Diagnose invalid moves by costing them as two moves.  */
2225 
2226   scratch = NO_REGS;
2227   test = from;
2228   if (to == SP_REGS)
2229     scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2230   else if (to == MDR_REGS)
2231     scratch = DATA_REGS;
2232   else if (to == FP_REGS && to != from)
2233     scratch = GENERAL_REGS;
2234   else
2235     {
2236       test = to;
2237       if (from == SP_REGS)
2238 	scratch = (TARGET_AM33 ? GENERAL_REGS : ADDRESS_REGS);
2239       else if (from == MDR_REGS)
2240 	scratch = DATA_REGS;
2241       else if (from == FP_REGS && to != from)
2242 	scratch = GENERAL_REGS;
2243     }
2244   if (scratch != NO_REGS && !reg_class_subset_p (test, scratch))
2245     return (mn10300_register_move_cost (VOIDmode, from, scratch)
2246 	    + mn10300_register_move_cost (VOIDmode, scratch, to));
2247 
2248   /* From here on, all we need consider are legal combinations.  */
2249 
2250   if (optimize_size)
2251     {
2252       /* The scale here is bytes * 2.  */
2253 
2254       if (from == to && (to == ADDRESS_REGS || to == DATA_REGS))
2255 	return 2;
2256 
2257       if (from == SP_REGS)
2258 	return (to == ADDRESS_REGS ? 2 : 6);
2259 
2260       /* For MN103, all remaining legal moves are two bytes.  */
2261       if (TARGET_AM33)
2262 	return 4;
2263 
2264       if (to == SP_REGS)
2265 	return (from == ADDRESS_REGS ? 4 : 6);
2266 
2267       if ((from == ADDRESS_REGS || from == DATA_REGS)
2268 	   && (to == ADDRESS_REGS || to == DATA_REGS))
2269 	return 4;
2270 
2271       if (to == EXTENDED_REGS)
2272 	return (to == from ? 6 : 4);
2273 
2274       /* What's left are SP_REGS, FP_REGS, or combinations of the above.  */
2275       return 6;
2276     }
2277   else
2278     {
2279       /* The scale here is cycles * 2.  */
2280 
2281       if (to == FP_REGS)
2282 	return 8;
2283       if (from == FP_REGS)
2284 	return 4;
2285 
2286       /* All legal moves between integral registers are single cycle.  */
2287       return 2;
2288     }
2289 }
2290 
2291 /* Implement the TARGET_MEMORY_MOVE_COST hook.
2292 
2293    Given lack of the form of the address, this must be speed-relative,
2294    though we should never be less expensive than a size-relative register
2295    move cost above.  This is not a problem.  */
2296 
2297 static int
mn10300_memory_move_cost(machine_mode mode ATTRIBUTE_UNUSED,reg_class_t iclass,bool in ATTRIBUTE_UNUSED)2298 mn10300_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
2299 			  reg_class_t iclass, bool in ATTRIBUTE_UNUSED)
2300 {
2301   enum reg_class rclass = (enum reg_class) iclass;
2302 
2303   if (rclass == FP_REGS)
2304     return 8;
2305   return 6;
2306 }
2307 
2308 /* Implement the TARGET_RTX_COSTS hook.
2309 
2310    Speed-relative costs are relative to COSTS_N_INSNS, which is intended
2311    to represent cycles.  Size-relative costs are in bytes.  */
2312 
2313 static bool
mn10300_rtx_costs(rtx x,machine_mode mode,int outer_code,int opno ATTRIBUTE_UNUSED,int * ptotal,bool speed)2314 mn10300_rtx_costs (rtx x, machine_mode mode, int outer_code,
2315 		   int opno ATTRIBUTE_UNUSED, int *ptotal, bool speed)
2316 {
2317   /* This value is used for SYMBOL_REF etc where we want to pretend
2318      we have a full 32-bit constant.  */
2319   HOST_WIDE_INT i = 0x12345678;
2320   int total;
2321   int code = GET_CODE (x);
2322 
2323   switch (code)
2324     {
2325     case CONST_INT:
2326       i = INTVAL (x);
2327     do_int_costs:
2328       if (speed)
2329 	{
2330 	  if (outer_code == SET)
2331 	    {
2332 	      /* 16-bit integer loads have latency 1, 32-bit loads 2.  */
2333 	      if (IN_RANGE (i, -32768, 32767))
2334 		total = COSTS_N_INSNS (1);
2335 	      else
2336 		total = COSTS_N_INSNS (2);
2337 	    }
2338 	  else
2339 	    {
2340 	      /* 16-bit integer operands don't affect latency;
2341 		 24-bit and 32-bit operands add a cycle.  */
2342 	      if (IN_RANGE (i, -32768, 32767))
2343 		total = 0;
2344 	      else
2345 		total = COSTS_N_INSNS (1);
2346 	    }
2347 	}
2348       else
2349 	{
2350 	  if (outer_code == SET)
2351 	    {
2352 	      if (i == 0)
2353 		total = 1;
2354 	      else if (IN_RANGE (i, -128, 127))
2355 		total = 2;
2356 	      else if (IN_RANGE (i, -32768, 32767))
2357 		total = 3;
2358 	      else
2359 		total = 6;
2360 	    }
2361 	  else
2362 	    {
2363 	      /* Reference here is ADD An,Dn, vs ADD imm,Dn.  */
2364 	      if (IN_RANGE (i, -128, 127))
2365 		total = 0;
2366 	      else if (IN_RANGE (i, -32768, 32767))
2367 		total = 2;
2368 	      else if (TARGET_AM33 && IN_RANGE (i, -0x01000000, 0x00ffffff))
2369 		total = 3;
2370 	      else
2371 		total = 4;
2372 	    }
2373 	}
2374       goto alldone;
2375 
2376     case CONST:
2377     case LABEL_REF:
2378     case SYMBOL_REF:
2379     case CONST_DOUBLE:
2380       /* We assume all of these require a 32-bit constant, even though
2381 	 some symbol and label references can be relaxed.  */
2382       goto do_int_costs;
2383 
2384     case UNSPEC:
2385       switch (XINT (x, 1))
2386 	{
2387 	case UNSPEC_PIC:
2388 	case UNSPEC_GOT:
2389 	case UNSPEC_GOTOFF:
2390 	case UNSPEC_PLT:
2391 	case UNSPEC_GOTSYM_OFF:
2392 	  /* The PIC unspecs also resolve to a 32-bit constant.  */
2393 	  goto do_int_costs;
2394 
2395 	default:
2396 	  /* Assume any non-listed unspec is some sort of arithmetic.  */
2397 	  goto do_arith_costs;
2398 	}
2399 
2400     case PLUS:
2401       /* Notice the size difference of INC and INC4.  */
2402       if (!speed && outer_code == SET && CONST_INT_P (XEXP (x, 1)))
2403 	{
2404 	  i = INTVAL (XEXP (x, 1));
2405 	  if (i == 1 || i == 4)
2406 	    {
2407 	      total = 1 + rtx_cost (XEXP (x, 0), mode, PLUS, 0, speed);
2408 	      goto alldone;
2409 	    }
2410 	}
2411       goto do_arith_costs;
2412 
2413     case MINUS:
2414     case AND:
2415     case IOR:
2416     case XOR:
2417     case NOT:
2418     case NEG:
2419     case ZERO_EXTEND:
2420     case SIGN_EXTEND:
2421     case COMPARE:
2422     case BSWAP:
2423     case CLZ:
2424     do_arith_costs:
2425       total = (speed ? COSTS_N_INSNS (1) : 2);
2426       break;
2427 
2428     case ASHIFT:
2429       /* Notice the size difference of ASL2 and variants.  */
2430       if (!speed && CONST_INT_P (XEXP (x, 1)))
2431 	switch (INTVAL (XEXP (x, 1)))
2432 	  {
2433 	  case 1:
2434 	  case 2:
2435 	    total = 1;
2436 	    goto alldone;
2437 	  case 3:
2438 	  case 4:
2439 	    total = 2;
2440 	    goto alldone;
2441 	  }
2442       /* FALLTHRU */
2443 
2444     case ASHIFTRT:
2445     case LSHIFTRT:
2446       total = (speed ? COSTS_N_INSNS (1) : 3);
2447       goto alldone;
2448 
2449     case MULT:
2450       total = (speed ? COSTS_N_INSNS (3) : 2);
2451       break;
2452 
2453     case DIV:
2454     case UDIV:
2455     case MOD:
2456     case UMOD:
2457       total = (speed ? COSTS_N_INSNS (39)
2458 		/* Include space to load+retrieve MDR.  */
2459 		: code == MOD || code == UMOD ? 6 : 4);
2460       break;
2461 
2462     case MEM:
2463       total = mn10300_address_cost (XEXP (x, 0), mode,
2464 				    MEM_ADDR_SPACE (x), speed);
2465       if (speed)
2466 	total = COSTS_N_INSNS (2 + total);
2467       goto alldone;
2468 
2469     default:
2470       /* Probably not implemented.  Assume external call.  */
2471       total = (speed ? COSTS_N_INSNS (10) : 7);
2472       break;
2473     }
2474 
2475   *ptotal = total;
2476   return false;
2477 
2478  alldone:
2479   *ptotal = total;
2480   return true;
2481 }
2482 
2483 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2484    may access it using GOTOFF instead of GOT.  */
2485 
2486 static void
mn10300_encode_section_info(tree decl,rtx rtl,int first)2487 mn10300_encode_section_info (tree decl, rtx rtl, int first)
2488 {
2489   rtx symbol;
2490 
2491   default_encode_section_info (decl, rtl, first);
2492 
2493   if (! MEM_P (rtl))
2494     return;
2495 
2496   symbol = XEXP (rtl, 0);
2497   if (GET_CODE (symbol) != SYMBOL_REF)
2498     return;
2499 
2500   if (flag_pic)
2501     SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2502 }
2503 
2504 /* Dispatch tables on the mn10300 are extremely expensive in terms of code
2505    and readonly data size.  So we crank up the case threshold value to
2506    encourage a series of if/else comparisons to implement many small switch
2507    statements.  In theory, this value could be increased much more if we
2508    were solely optimizing for space, but we keep it "reasonable" to avoid
2509    serious code efficiency lossage.  */
2510 
2511 static unsigned int
mn10300_case_values_threshold(void)2512 mn10300_case_values_threshold (void)
2513 {
2514   return 6;
2515 }
2516 
2517 /* Worker function for TARGET_TRAMPOLINE_INIT.  */
2518 
2519 static void
mn10300_trampoline_init(rtx m_tramp,tree fndecl,rtx chain_value)2520 mn10300_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2521 {
2522   rtx mem, disp, fnaddr = XEXP (DECL_RTL (fndecl), 0);
2523 
2524   /* This is a strict alignment target, which means that we play
2525      some games to make sure that the locations at which we need
2526      to store <chain> and <disp> wind up at aligned addresses.
2527 
2528 	0x28 0x00			add 0,d0
2529 	          0xfc 0xdd		mov chain,a1
2530         <chain>
2531 	0xf8 0xed 0x00			btst 0,d1
2532 	               0xdc		jmp fnaddr
2533 	<disp>
2534 
2535      Note that the two extra insns are effectively nops; they
2536      clobber the flags but do not affect the contents of D0 or D1.  */
2537 
2538   disp = expand_binop (SImode, sub_optab, fnaddr,
2539 		       plus_constant (Pmode, XEXP (m_tramp, 0), 11),
2540 		       NULL_RTX, 1, OPTAB_DIRECT);
2541 
2542   mem = adjust_address (m_tramp, SImode, 0);
2543   emit_move_insn (mem, gen_int_mode (0xddfc0028, SImode));
2544   mem = adjust_address (m_tramp, SImode, 4);
2545   emit_move_insn (mem, chain_value);
2546   mem = adjust_address (m_tramp, SImode, 8);
2547   emit_move_insn (mem, gen_int_mode (0xdc00edf8, SImode));
2548   mem = adjust_address (m_tramp, SImode, 12);
2549   emit_move_insn (mem, disp);
2550 }
2551 
2552 /* Output the assembler code for a C++ thunk function.
2553    THUNK_DECL is the declaration for the thunk function itself, FUNCTION
2554    is the decl for the target function.  DELTA is an immediate constant
2555    offset to be added to the THIS parameter.  If VCALL_OFFSET is nonzero
2556    the word at the adjusted address *(*THIS' + VCALL_OFFSET) should be
2557    additionally added to THIS.  Finally jump to the entry point of
2558    FUNCTION.  */
2559 
2560 static void
mn10300_asm_output_mi_thunk(FILE * file,tree thunk_fndecl ATTRIBUTE_UNUSED,HOST_WIDE_INT delta,HOST_WIDE_INT vcall_offset,tree function)2561 mn10300_asm_output_mi_thunk (FILE *        file,
2562 			     tree          thunk_fndecl ATTRIBUTE_UNUSED,
2563 			     HOST_WIDE_INT delta,
2564 			     HOST_WIDE_INT vcall_offset,
2565 			     tree          function)
2566 {
2567   const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
2568   const char * _this;
2569 
2570   assemble_start_function (thunk_fndecl, fnname);
2571   /* Get the register holding the THIS parameter.  Handle the case
2572      where there is a hidden first argument for a returned structure.  */
2573   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2574     _this = reg_names [FIRST_ARGUMENT_REGNUM + 1];
2575   else
2576     _this = reg_names [FIRST_ARGUMENT_REGNUM];
2577 
2578   fprintf (file, "\t%s Thunk Entry Point:\n", ASM_COMMENT_START);
2579 
2580   if (delta)
2581     fprintf (file, "\tadd %d, %s\n", (int) delta, _this);
2582 
2583   if (vcall_offset)
2584     {
2585       const char * scratch = reg_names [FIRST_ADDRESS_REGNUM + 1];
2586 
2587       fprintf (file, "\tmov %s, %s\n", _this, scratch);
2588       fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2589       fprintf (file, "\tadd %d, %s\n", (int) vcall_offset, scratch);
2590       fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2591       fprintf (file, "\tadd %s, %s\n", scratch, _this);
2592     }
2593 
2594   fputs ("\tjmp ", file);
2595   assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2596   putc ('\n', file);
2597   assemble_end_function (thunk_fndecl, fnname);
2598 }
2599 
2600 /* Return true if mn10300_output_mi_thunk would be able to output the
2601    assembler code for the thunk function specified by the arguments
2602    it is passed, and false otherwise.  */
2603 
2604 static bool
mn10300_can_output_mi_thunk(const_tree thunk_fndecl ATTRIBUTE_UNUSED,HOST_WIDE_INT delta ATTRIBUTE_UNUSED,HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,const_tree function ATTRIBUTE_UNUSED)2605 mn10300_can_output_mi_thunk (const_tree    thunk_fndecl ATTRIBUTE_UNUSED,
2606 			     HOST_WIDE_INT delta        ATTRIBUTE_UNUSED,
2607 			     HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2608 			     const_tree    function     ATTRIBUTE_UNUSED)
2609 {
2610   return true;
2611 }
2612 
2613 /* Implement TARGET_HARD_REGNO_MODE_OK.  */
2614 
2615 static bool
mn10300_hard_regno_mode_ok(unsigned int regno,machine_mode mode)2616 mn10300_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2617 {
2618   if (REGNO_REG_CLASS (regno) == FP_REGS
2619       || REGNO_REG_CLASS (regno) == FP_ACC_REGS)
2620     /* Do not store integer values in FP registers.  */
2621     return GET_MODE_CLASS (mode) == MODE_FLOAT && ((regno & 1) == 0);
2622 
2623   if (! TARGET_AM33 && REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2624     return false;
2625 
2626   if (((regno) & 1) == 0 || GET_MODE_SIZE (mode) == 4)
2627     return true;
2628 
2629   if (REGNO_REG_CLASS (regno) == DATA_REGS
2630       || (TARGET_AM33 && REGNO_REG_CLASS (regno) == ADDRESS_REGS)
2631       || REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2632     return GET_MODE_SIZE (mode) <= 4;
2633 
2634   return false;
2635 }
2636 
2637 /* Implement TARGET_MODES_TIEABLE_P.  */
2638 
2639 static bool
mn10300_modes_tieable_p(machine_mode mode1,machine_mode mode2)2640 mn10300_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2641 {
2642   if (GET_MODE_CLASS (mode1) == MODE_FLOAT
2643       && GET_MODE_CLASS (mode2) != MODE_FLOAT)
2644     return false;
2645 
2646   if (GET_MODE_CLASS (mode2) == MODE_FLOAT
2647       && GET_MODE_CLASS (mode1) != MODE_FLOAT)
2648     return false;
2649 
2650   if (TARGET_AM33
2651       || mode1 == mode2
2652       || (GET_MODE_SIZE (mode1) <= 4 && GET_MODE_SIZE (mode2) <= 4))
2653     return true;
2654 
2655   return false;
2656 }
2657 
2658 static int
cc_flags_for_mode(machine_mode mode)2659 cc_flags_for_mode (machine_mode mode)
2660 {
2661   switch (mode)
2662     {
2663     case E_CCmode:
2664       return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C | CC_FLAG_V;
2665     case E_CCZNCmode:
2666       return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_C;
2667     case E_CCZNmode:
2668       return CC_FLAG_Z | CC_FLAG_N;
2669     case E_CC_FLOATmode:
2670       return -1;
2671     default:
2672       gcc_unreachable ();
2673     }
2674 }
2675 
2676 static int
cc_flags_for_code(enum rtx_code code)2677 cc_flags_for_code (enum rtx_code code)
2678 {
2679   switch (code)
2680     {
2681     case EQ:	/* Z */
2682     case NE:	/* ~Z */
2683       return CC_FLAG_Z;
2684 
2685     case LT:	/* N */
2686     case GE:	/* ~N */
2687       return CC_FLAG_N;
2688 
2689     case GT:    /* ~(Z|(N^V)) */
2690     case LE:    /* Z|(N^V) */
2691       return CC_FLAG_Z | CC_FLAG_N | CC_FLAG_V;
2692 
2693     case GEU:	/* ~C */
2694     case LTU:	/* C */
2695       return CC_FLAG_C;
2696 
2697     case GTU:	/* ~(C | Z) */
2698     case LEU:	/* C | Z */
2699       return CC_FLAG_Z | CC_FLAG_C;
2700 
2701     case ORDERED:
2702     case UNORDERED:
2703     case LTGT:
2704     case UNEQ:
2705     case UNGE:
2706     case UNGT:
2707     case UNLE:
2708     case UNLT:
2709       return -1;
2710 
2711     default:
2712       gcc_unreachable ();
2713     }
2714 }
2715 
2716 machine_mode
mn10300_select_cc_mode(enum rtx_code code,rtx x,rtx y ATTRIBUTE_UNUSED)2717 mn10300_select_cc_mode (enum rtx_code code, rtx x, rtx y ATTRIBUTE_UNUSED)
2718 {
2719   int req;
2720 
2721   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2722     return CC_FLOATmode;
2723 
2724   req = cc_flags_for_code (code);
2725 
2726   if (req & CC_FLAG_V)
2727     return CCmode;
2728   if (req & CC_FLAG_C)
2729     return CCZNCmode;
2730   return CCZNmode;
2731 }
2732 
2733 static inline bool
set_is_load_p(rtx set)2734 set_is_load_p (rtx set)
2735 {
2736   return MEM_P (SET_SRC (set));
2737 }
2738 
2739 static inline bool
set_is_store_p(rtx set)2740 set_is_store_p (rtx set)
2741 {
2742   return MEM_P (SET_DEST (set));
2743 }
2744 
2745 /* Update scheduling costs for situations that cannot be
2746    described using the attributes and DFA machinery.
2747    DEP is the insn being scheduled.
2748    INSN is the previous insn.
2749    COST is the current cycle cost for DEP.  */
2750 
2751 static int
mn10300_adjust_sched_cost(rtx_insn * insn,int dep_type,rtx_insn * dep,int cost,unsigned int)2752 mn10300_adjust_sched_cost (rtx_insn *insn, int dep_type, rtx_insn *dep,
2753 			   int cost, unsigned int)
2754 {
2755   rtx insn_set;
2756   rtx dep_set;
2757   int timings;
2758 
2759   if (!TARGET_AM33)
2760     return 1;
2761 
2762   /* We are only interested in pairs of SET. */
2763   insn_set = single_set (insn);
2764   if (!insn_set)
2765     return cost;
2766 
2767   dep_set = single_set (dep);
2768   if (!dep_set)
2769     return cost;
2770 
2771   /* For the AM34 a load instruction that follows a
2772      store instruction incurs an extra cycle of delay.  */
2773   if (mn10300_tune_cpu == PROCESSOR_AM34
2774       && set_is_load_p (dep_set)
2775       && set_is_store_p (insn_set))
2776     cost += 1;
2777 
2778   /* For the AM34 a non-store, non-branch FPU insn that follows
2779      another FPU insn incurs a one cycle throughput increase.  */
2780   else if (mn10300_tune_cpu == PROCESSOR_AM34
2781       && ! set_is_store_p (insn_set)
2782       && ! JUMP_P (insn)
2783       && GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) == MODE_FLOAT
2784       && GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) == MODE_FLOAT)
2785     cost += 1;
2786 
2787   /*  Resolve the conflict described in section 1-7-4 of
2788       Chapter 3 of the MN103E Series Instruction Manual
2789       where it says:
2790 
2791         "When the preceding instruction is a CPU load or
2792 	 store instruction, a following FPU instruction
2793 	 cannot be executed until the CPU completes the
2794 	 latency period even though there are no register
2795 	 or flag dependencies between them."  */
2796 
2797   /* Only the AM33-2 (and later) CPUs have FPU instructions.  */
2798   if (! TARGET_AM33_2)
2799     return cost;
2800 
2801   /* If a data dependence already exists then the cost is correct.  */
2802   if (dep_type == 0)
2803     return cost;
2804 
2805   /* Check that the instruction about to scheduled is an FPU instruction.  */
2806   if (GET_MODE_CLASS (GET_MODE (SET_SRC (dep_set))) != MODE_FLOAT)
2807     return cost;
2808 
2809   /* Now check to see if the previous instruction is a load or store.  */
2810   if (! set_is_load_p (insn_set) && ! set_is_store_p (insn_set))
2811     return cost;
2812 
2813   /* XXX: Verify: The text of 1-7-4 implies that the restriction
2814      only applies when an INTEGER load/store precedes an FPU
2815      instruction, but is this true ?  For now we assume that it is.  */
2816   if (GET_MODE_CLASS (GET_MODE (SET_SRC (insn_set))) != MODE_INT)
2817     return cost;
2818 
2819   /* Extract the latency value from the timings attribute.  */
2820   timings = get_attr_timings (insn);
2821   return timings < 100 ? (timings % 10) : (timings % 100);
2822 }
2823 
2824 static void
mn10300_conditional_register_usage(void)2825 mn10300_conditional_register_usage (void)
2826 {
2827   unsigned int i;
2828 
2829   if (!TARGET_AM33)
2830     {
2831       for (i = FIRST_EXTENDED_REGNUM;
2832 	   i <= LAST_EXTENDED_REGNUM; i++)
2833 	fixed_regs[i] = 1;
2834     }
2835   if (!TARGET_AM33_2)
2836     {
2837       for (i = FIRST_FP_REGNUM;
2838 	   i <= LAST_FP_REGNUM; i++)
2839 	fixed_regs[i] = 1;
2840     }
2841   if (flag_pic)
2842     fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
2843 }
2844 
2845 /* Worker function for TARGET_MD_ASM_ADJUST.
2846    We do this in the mn10300 backend to maintain source compatibility
2847    with the old cc0-based compiler.  */
2848 
2849 static rtx_insn *
mn10300_md_asm_adjust(vec<rtx> &,vec<rtx> &,vec<const char * > &,vec<rtx> & clobbers,HARD_REG_SET & clobbered_regs)2850 mn10300_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
2851 		       vec<const char *> &/*constraints*/,
2852 		       vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
2853 {
2854   clobbers.safe_push (gen_rtx_REG (CCmode, CC_REG));
2855   SET_HARD_REG_BIT (clobbered_regs, CC_REG);
2856   return NULL;
2857 }
2858 
2859 /* A helper function for splitting cbranch patterns after reload.  */
2860 
2861 void
mn10300_split_cbranch(machine_mode cmp_mode,rtx cmp_op,rtx label_ref)2862 mn10300_split_cbranch (machine_mode cmp_mode, rtx cmp_op, rtx label_ref)
2863 {
2864   rtx flags, x;
2865 
2866   flags = gen_rtx_REG (cmp_mode, CC_REG);
2867   x = gen_rtx_COMPARE (cmp_mode, XEXP (cmp_op, 0), XEXP (cmp_op, 1));
2868   x = gen_rtx_SET (flags, x);
2869   emit_insn (x);
2870 
2871   x = gen_rtx_fmt_ee (GET_CODE (cmp_op), VOIDmode, flags, const0_rtx);
2872   x = gen_rtx_IF_THEN_ELSE (VOIDmode, x, label_ref, pc_rtx);
2873   x = gen_rtx_SET (pc_rtx, x);
2874   emit_jump_insn (x);
2875 }
2876 
2877 /* A helper function for matching parallels that set the flags.  */
2878 
2879 bool
mn10300_match_ccmode(rtx insn,machine_mode cc_mode)2880 mn10300_match_ccmode (rtx insn, machine_mode cc_mode)
2881 {
2882   rtx op1, flags;
2883   machine_mode flags_mode;
2884 
2885   gcc_checking_assert (XVECLEN (PATTERN (insn), 0) == 2);
2886 
2887   op1 = XVECEXP (PATTERN (insn), 0, 0);
2888   gcc_checking_assert (GET_CODE (SET_SRC (op1)) == COMPARE);
2889 
2890   flags = SET_DEST (op1);
2891   flags_mode = GET_MODE (flags);
2892 
2893   if (GET_MODE (SET_SRC (op1)) != flags_mode)
2894     return false;
2895   if (GET_MODE_CLASS (flags_mode) != MODE_CC)
2896     return false;
2897 
2898   /* Ensure that the mode of FLAGS is compatible with CC_MODE.  */
2899   if (cc_flags_for_mode (flags_mode) & ~cc_flags_for_mode (cc_mode))
2900     return false;
2901 
2902   return true;
2903 }
2904 
2905 /* This function is used to help split:
2906 
2907      (set (reg) (and (reg) (int)))
2908 
2909    into:
2910 
2911      (set (reg) (shift (reg) (int))
2912      (set (reg) (shift (reg) (int))
2913 
2914    where the shitfs will be shorter than the "and" insn.
2915 
2916    It returns the number of bits that should be shifted.  A positive
2917    values means that the low bits are to be cleared (and hence the
2918    shifts should be right followed by left) whereas a negative value
2919    means that the high bits are to be cleared (left followed by right).
2920    Zero is returned when it would not be economical to split the AND.  */
2921 
2922 int
mn10300_split_and_operand_count(rtx op)2923 mn10300_split_and_operand_count (rtx op)
2924 {
2925   HOST_WIDE_INT val = INTVAL (op);
2926   int count;
2927 
2928   if (val < 0)
2929     {
2930       /* High bit is set, look for bits clear at the bottom.  */
2931       count = exact_log2 (-val);
2932       if (count < 0)
2933 	return 0;
2934       /* This is only size win if we can use the asl2 insn.  Otherwise we
2935 	 would be replacing 1 6-byte insn with 2 3-byte insns.  */
2936       if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2937 	return 0;
2938       return count;
2939     }
2940   else
2941     {
2942       /* High bit is clear, look for bits set at the bottom.  */
2943       count = exact_log2 (val + 1);
2944       count = 32 - count;
2945       /* Again, this is only a size win with asl2.  */
2946       if (count > (optimize_insn_for_speed_p () ? 2 : 4))
2947 	return 0;
2948       return -count;
2949     }
2950 }
2951 
2952 struct liw_data
2953 {
2954   enum attr_liw slot;
2955   enum attr_liw_op op;
2956   rtx dest;
2957   rtx src;
2958 };
2959 
2960 /* Decide if the given insn is a candidate for LIW bundling.  If it is then
2961    extract the operands and LIW attributes from the insn and use them to fill
2962    in the liw_data structure.  Return true upon success or false if the insn
2963    cannot be bundled.  */
2964 
2965 static bool
extract_bundle(rtx_insn * insn,struct liw_data * pdata)2966 extract_bundle (rtx_insn *insn, struct liw_data * pdata)
2967 {
2968   bool allow_consts = true;
2969   rtx p;
2970 
2971   gcc_assert (pdata != NULL);
2972 
2973   if (insn == NULL)
2974     return false;
2975   /* Make sure that we are dealing with a simple SET insn.  */
2976   p = single_set (insn);
2977   if (p == NULL_RTX)
2978     return false;
2979 
2980   /* Make sure that it could go into one of the LIW pipelines.  */
2981   pdata->slot = get_attr_liw (insn);
2982   if (pdata->slot == LIW_BOTH)
2983     return false;
2984 
2985   pdata->op = get_attr_liw_op (insn);
2986 
2987   switch (pdata->op)
2988     {
2989     case LIW_OP_MOV:
2990       pdata->dest = SET_DEST (p);
2991       pdata->src = SET_SRC (p);
2992       break;
2993     case LIW_OP_CMP:
2994       pdata->dest = XEXP (SET_SRC (p), 0);
2995       pdata->src = XEXP (SET_SRC (p), 1);
2996       break;
2997     case LIW_OP_NONE:
2998       return false;
2999     case LIW_OP_AND:
3000     case LIW_OP_OR:
3001     case LIW_OP_XOR:
3002       /* The AND, OR and XOR long instruction words only accept register arguments.  */
3003       allow_consts = false;
3004       /* Fall through.  */
3005     default:
3006       pdata->dest = SET_DEST (p);
3007       pdata->src = XEXP (SET_SRC (p), 1);
3008       break;
3009     }
3010 
3011   if (! REG_P (pdata->dest))
3012     return false;
3013 
3014   if (REG_P (pdata->src))
3015     return true;
3016 
3017   return allow_consts && satisfies_constraint_O (pdata->src);
3018 }
3019 
3020 /* Make sure that it is OK to execute LIW1 and LIW2 in parallel.  GCC generated
3021    the instructions with the assumption that LIW1 would be executed before LIW2
3022    so we must check for overlaps between their sources and destinations.  */
3023 
3024 static bool
check_liw_constraints(struct liw_data * pliw1,struct liw_data * pliw2)3025 check_liw_constraints (struct liw_data * pliw1, struct liw_data * pliw2)
3026 {
3027   /* Check for slot conflicts.  */
3028   if (pliw2->slot == pliw1->slot && pliw1->slot != LIW_EITHER)
3029     return false;
3030 
3031   /* If either operation is a compare, then "dest" is really an input; the real
3032      destination is CC_REG.  So these instructions need different checks.  */
3033 
3034   /* Changing "CMP ; OP" into "CMP | OP" is OK because the comparison will
3035      check its values prior to any changes made by OP.  */
3036   if (pliw1->op == LIW_OP_CMP)
3037     {
3038       /* Two sequential comparisons means dead code, which ought to
3039          have been eliminated given that bundling only happens with
3040          optimization.  We cannot bundle them in any case.  */
3041       gcc_assert (pliw1->op != pliw2->op);
3042       return true;
3043     }
3044 
3045   /* Changing "OP ; CMP" into "OP | CMP" does not work if the value being compared
3046      is the destination of OP, as the CMP will look at the old value, not the new
3047      one.  */
3048   if (pliw2->op == LIW_OP_CMP)
3049     {
3050       if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3051 	return false;
3052 
3053       if (REG_P (pliw2->src))
3054 	return REGNO (pliw2->src) != REGNO (pliw1->dest);
3055 
3056       return true;
3057     }
3058 
3059   /* Changing "OP1 ; OP2" into "OP1 | OP2" does not work if they both write to the
3060      same destination register.  */
3061   if (REGNO (pliw2->dest) == REGNO (pliw1->dest))
3062     return false;
3063 
3064   /* Changing "OP1 ; OP2" into "OP1 | OP2" generally does not work if the destination
3065      of OP1 is the source of OP2.  The exception is when OP1 is a MOVE instruction when
3066      we can replace the source in OP2 with the source of OP1.  */
3067   if (REG_P (pliw2->src) && REGNO (pliw2->src) == REGNO (pliw1->dest))
3068     {
3069       if (pliw1->op == LIW_OP_MOV && REG_P (pliw1->src))
3070 	{
3071 	  if (! REG_P (pliw1->src)
3072 	      && (pliw2->op == LIW_OP_AND
3073 		  || pliw2->op == LIW_OP_OR
3074 		  || pliw2->op == LIW_OP_XOR))
3075 	    return false;
3076 
3077 	  pliw2->src = pliw1->src;
3078 	  return true;
3079 	}
3080       return false;
3081     }
3082 
3083   /* Everything else is OK.  */
3084   return true;
3085 }
3086 
3087 /* Combine pairs of insns into LIW bundles.  */
3088 
3089 static void
mn10300_bundle_liw(void)3090 mn10300_bundle_liw (void)
3091 {
3092   rtx_insn *r;
3093 
3094   for (r = get_insns (); r != NULL; r = next_nonnote_nondebug_insn (r))
3095     {
3096       rtx_insn *insn1, *insn2;
3097       struct liw_data liw1, liw2;
3098 
3099       insn1 = r;
3100       if (! extract_bundle (insn1, & liw1))
3101 	continue;
3102 
3103       insn2 = next_nonnote_nondebug_insn (insn1);
3104       if (! extract_bundle (insn2, & liw2))
3105 	continue;
3106 
3107       /* Check for source/destination overlap.  */
3108       if (! check_liw_constraints (& liw1, & liw2))
3109 	continue;
3110 
3111       if (liw1.slot == LIW_OP2 || liw2.slot == LIW_OP1)
3112 	{
3113 	  struct liw_data temp;
3114 
3115 	  temp = liw1;
3116 	  liw1 = liw2;
3117 	  liw2 = temp;
3118 	}
3119 
3120       delete_insn (insn2);
3121 
3122       rtx insn2_pat;
3123       if (liw1.op == LIW_OP_CMP)
3124 	insn2_pat = gen_cmp_liw (liw2.dest, liw2.src, liw1.dest, liw1.src,
3125 				 GEN_INT (liw2.op));
3126       else if (liw2.op == LIW_OP_CMP)
3127 	insn2_pat = gen_liw_cmp (liw1.dest, liw1.src, liw2.dest, liw2.src,
3128 				 GEN_INT (liw1.op));
3129       else
3130 	insn2_pat = gen_liw (liw1.dest, liw2.dest, liw1.src, liw2.src,
3131 			     GEN_INT (liw1.op), GEN_INT (liw2.op));
3132 
3133       insn2 = emit_insn_after (insn2_pat, insn1);
3134       delete_insn (insn1);
3135       r = insn2;
3136     }
3137 }
3138 
3139 #define DUMP(reason, insn)			\
3140   do						\
3141     {						\
3142       if (dump_file)				\
3143 	{					\
3144 	  fprintf (dump_file, reason "\n");	\
3145 	  if (insn != NULL_RTX)			\
3146 	    print_rtl_single (dump_file, insn);	\
3147 	  fprintf(dump_file, "\n");		\
3148 	}					\
3149     }						\
3150   while (0)
3151 
3152 /* Replace the BRANCH insn with a Lcc insn that goes to LABEL.
3153    Insert a SETLB insn just before LABEL.  */
3154 
3155 static void
mn10300_insert_setlb_lcc(rtx_insn * label,rtx_insn * branch)3156 mn10300_insert_setlb_lcc (rtx_insn *label, rtx_insn *branch)
3157 {
3158   rtx lcc, comparison, cmp_reg;
3159 
3160   if (LABEL_NUSES (label) > 1)
3161     {
3162       rtx_insn *insn;
3163 
3164       /* This label is used both as an entry point to the loop
3165 	 and as a loop-back point for the loop.  We need to separate
3166 	 these two functions so that the SETLB happens upon entry,
3167 	 but the loop-back does not go to the SETLB instruction.  */
3168       DUMP ("Inserting SETLB insn after:", label);
3169       insn = emit_insn_after (gen_setlb (), label);
3170       label = gen_label_rtx ();
3171       emit_label_after (label, insn);
3172       DUMP ("Created new loop-back label:", label);
3173     }
3174   else
3175     {
3176       DUMP ("Inserting SETLB insn before:", label);
3177       emit_insn_before (gen_setlb (), label);
3178     }
3179 
3180   comparison = XEXP (SET_SRC (PATTERN (branch)), 0);
3181   cmp_reg = XEXP (comparison, 0);
3182   gcc_assert (REG_P (cmp_reg));
3183 
3184   /* If the comparison has not already been split out of the branch
3185      then do so now.  */
3186   gcc_assert (REGNO (cmp_reg) == CC_REG);
3187 
3188   if (GET_MODE (cmp_reg) == CC_FLOATmode)
3189     lcc = gen_FLcc (comparison, label);
3190   else
3191     lcc = gen_Lcc (comparison, label);
3192 
3193   rtx_insn *jump = emit_jump_insn_before (lcc, branch);
3194   mark_jump_label (XVECEXP (lcc, 0, 0), jump, 0);
3195   JUMP_LABEL (jump) = label;
3196   DUMP ("Replacing branch insn...", branch);
3197   DUMP ("... with Lcc insn:", jump);
3198   delete_insn (branch);
3199 }
3200 
3201 static bool
mn10300_block_contains_call(basic_block block)3202 mn10300_block_contains_call (basic_block block)
3203 {
3204   rtx_insn *insn;
3205 
3206   FOR_BB_INSNS (block, insn)
3207     if (CALL_P (insn))
3208       return true;
3209 
3210   return false;
3211 }
3212 
3213 static bool
mn10300_loop_contains_call_insn(loop_p loop)3214 mn10300_loop_contains_call_insn (loop_p loop)
3215 {
3216   basic_block * bbs;
3217   bool result = false;
3218   unsigned int i;
3219 
3220   bbs = get_loop_body (loop);
3221 
3222   for (i = 0; i < loop->num_nodes; i++)
3223     if (mn10300_block_contains_call (bbs[i]))
3224       {
3225 	result = true;
3226 	break;
3227       }
3228 
3229   free (bbs);
3230   return result;
3231 }
3232 
3233 static void
mn10300_scan_for_setlb_lcc(void)3234 mn10300_scan_for_setlb_lcc (void)
3235 {
3236   loop_p loop;
3237 
3238   DUMP ("Looking for loops that can use the SETLB insn", NULL_RTX);
3239 
3240   df_analyze ();
3241   compute_bb_for_insn ();
3242 
3243   /* Find the loops.  */
3244   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
3245 
3246   /* FIXME: For now we only investigate innermost loops.  In practice however
3247      if an inner loop is not suitable for use with the SETLB/Lcc insns, it may
3248      be the case that its parent loop is suitable.  Thus we should check all
3249      loops, but work from the innermost outwards.  */
3250   FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
3251     {
3252       const char * reason = NULL;
3253 
3254       /* Check to see if we can modify this loop.  If we cannot
3255 	 then set 'reason' to describe why it could not be done.  */
3256       if (loop->latch == NULL)
3257 	reason = "it contains multiple latches";
3258       else if (loop->header != loop->latch)
3259 	/* FIXME: We could handle loops that span multiple blocks,
3260 	   but this requires a lot more work tracking down the branches
3261 	   that need altering, so for now keep things simple.  */
3262 	reason = "the loop spans multiple blocks";
3263       else if (mn10300_loop_contains_call_insn (loop))
3264 	reason = "it contains CALL insns";
3265       else
3266 	{
3267 	  rtx_insn *branch = BB_END (loop->latch);
3268 
3269 	  gcc_assert (JUMP_P (branch));
3270 	  if (single_set (branch) == NULL_RTX || ! any_condjump_p (branch))
3271 	    /* We cannot optimize tablejumps and the like.  */
3272 	    /* FIXME: We could handle unconditional jumps.  */
3273 	    reason = "it is not a simple loop";
3274 	  else
3275 	    {
3276 	      rtx_insn *label;
3277 
3278 	      if (dump_file)
3279 		flow_loop_dump (loop, dump_file, NULL, 0);
3280 
3281 	      label = BB_HEAD (loop->header);
3282 	      gcc_assert (LABEL_P (label));
3283 
3284 	      mn10300_insert_setlb_lcc (label, branch);
3285 	    }
3286 	}
3287 
3288       if (dump_file && reason != NULL)
3289 	fprintf (dump_file, "Loop starting with insn %d is not suitable because %s\n",
3290 		 INSN_UID (BB_HEAD (loop->header)),
3291 		 reason);
3292     }
3293 
3294   loop_optimizer_finalize ();
3295 
3296   df_finish_pass (false);
3297 
3298   DUMP ("SETLB scan complete", NULL_RTX);
3299 }
3300 
3301 static void
mn10300_reorg(void)3302 mn10300_reorg (void)
3303 {
3304   /* These are optimizations, so only run them if optimizing.  */
3305   if (TARGET_AM33 && (optimize > 0 || optimize_size))
3306     {
3307       if (TARGET_ALLOW_SETLB)
3308 	mn10300_scan_for_setlb_lcc ();
3309 
3310       if (TARGET_ALLOW_LIW)
3311 	mn10300_bundle_liw ();
3312     }
3313 }
3314 
3315 /* Initialize the GCC target structure.  */
3316 
3317 #undef  TARGET_MACHINE_DEPENDENT_REORG
3318 #define TARGET_MACHINE_DEPENDENT_REORG mn10300_reorg
3319 
3320 #undef  TARGET_ASM_ALIGNED_HI_OP
3321 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
3322 
3323 #undef  TARGET_LEGITIMIZE_ADDRESS
3324 #define TARGET_LEGITIMIZE_ADDRESS mn10300_legitimize_address
3325 
3326 #undef  TARGET_ADDRESS_COST
3327 #define TARGET_ADDRESS_COST  mn10300_address_cost
3328 #undef  TARGET_REGISTER_MOVE_COST
3329 #define TARGET_REGISTER_MOVE_COST  mn10300_register_move_cost
3330 #undef  TARGET_MEMORY_MOVE_COST
3331 #define TARGET_MEMORY_MOVE_COST  mn10300_memory_move_cost
3332 #undef  TARGET_RTX_COSTS
3333 #define TARGET_RTX_COSTS mn10300_rtx_costs
3334 
3335 #undef  TARGET_ASM_FILE_START
3336 #define TARGET_ASM_FILE_START mn10300_file_start
3337 #undef  TARGET_ASM_FILE_START_FILE_DIRECTIVE
3338 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
3339 
3340 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
3341 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA mn10300_asm_output_addr_const_extra
3342 
3343 #undef  TARGET_OPTION_OVERRIDE
3344 #define TARGET_OPTION_OVERRIDE mn10300_option_override
3345 
3346 #undef  TARGET_ENCODE_SECTION_INFO
3347 #define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
3348 
3349 #undef  TARGET_PROMOTE_PROTOTYPES
3350 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
3351 #undef  TARGET_RETURN_IN_MEMORY
3352 #define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
3353 #undef  TARGET_PASS_BY_REFERENCE
3354 #define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
3355 #undef  TARGET_CALLEE_COPIES
3356 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_arg_info_true
3357 #undef  TARGET_ARG_PARTIAL_BYTES
3358 #define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
3359 #undef  TARGET_FUNCTION_ARG
3360 #define TARGET_FUNCTION_ARG mn10300_function_arg
3361 #undef  TARGET_FUNCTION_ARG_ADVANCE
3362 #define TARGET_FUNCTION_ARG_ADVANCE mn10300_function_arg_advance
3363 
3364 #undef  TARGET_EXPAND_BUILTIN_SAVEREGS
3365 #define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
3366 #undef  TARGET_EXPAND_BUILTIN_VA_START
3367 #define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
3368 
3369 #undef  TARGET_CASE_VALUES_THRESHOLD
3370 #define TARGET_CASE_VALUES_THRESHOLD mn10300_case_values_threshold
3371 
3372 #undef TARGET_LRA_P
3373 #define TARGET_LRA_P hook_bool_void_false
3374 
3375 #undef  TARGET_LEGITIMATE_ADDRESS_P
3376 #define TARGET_LEGITIMATE_ADDRESS_P	mn10300_legitimate_address_p
3377 #undef  TARGET_DELEGITIMIZE_ADDRESS
3378 #define TARGET_DELEGITIMIZE_ADDRESS	mn10300_delegitimize_address
3379 #undef  TARGET_LEGITIMATE_CONSTANT_P
3380 #define TARGET_LEGITIMATE_CONSTANT_P	mn10300_legitimate_constant_p
3381 
3382 #undef  TARGET_PREFERRED_RELOAD_CLASS
3383 #define TARGET_PREFERRED_RELOAD_CLASS mn10300_preferred_reload_class
3384 #undef  TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
3385 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS \
3386   mn10300_preferred_output_reload_class
3387 #undef  TARGET_SECONDARY_RELOAD
3388 #define TARGET_SECONDARY_RELOAD  mn10300_secondary_reload
3389 
3390 #undef  TARGET_TRAMPOLINE_INIT
3391 #define TARGET_TRAMPOLINE_INIT mn10300_trampoline_init
3392 
3393 #undef  TARGET_FUNCTION_VALUE
3394 #define TARGET_FUNCTION_VALUE mn10300_function_value
3395 #undef  TARGET_LIBCALL_VALUE
3396 #define TARGET_LIBCALL_VALUE mn10300_libcall_value
3397 
3398 #undef  TARGET_ASM_OUTPUT_MI_THUNK
3399 #define TARGET_ASM_OUTPUT_MI_THUNK      mn10300_asm_output_mi_thunk
3400 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
3401 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK  mn10300_can_output_mi_thunk
3402 
3403 #undef  TARGET_SCHED_ADJUST_COST
3404 #define TARGET_SCHED_ADJUST_COST mn10300_adjust_sched_cost
3405 
3406 #undef  TARGET_CONDITIONAL_REGISTER_USAGE
3407 #define TARGET_CONDITIONAL_REGISTER_USAGE mn10300_conditional_register_usage
3408 
3409 #undef TARGET_MD_ASM_ADJUST
3410 #define TARGET_MD_ASM_ADJUST mn10300_md_asm_adjust
3411 
3412 #undef  TARGET_FLAGS_REGNUM
3413 #define TARGET_FLAGS_REGNUM  CC_REG
3414 
3415 #undef  TARGET_HARD_REGNO_MODE_OK
3416 #define TARGET_HARD_REGNO_MODE_OK mn10300_hard_regno_mode_ok
3417 
3418 #undef  TARGET_MODES_TIEABLE_P
3419 #define TARGET_MODES_TIEABLE_P mn10300_modes_tieable_p
3420 
3421 #undef  TARGET_HAVE_SPECULATION_SAFE_VALUE
3422 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
3423 
3424 struct gcc_target targetm = TARGET_INITIALIZER;
3425