OSDN Git Service

0d2ae38beca351d25dc9a55c203e5540f13fbf98
[pf3gnuchains/gcc-fork.git] / gcc / config / mn10300 / mn10300.c
1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Contributed by Jeff Law (law@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "recog.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "target-def.h"
45
46 /* This is used by GOTaddr2picreg to uniquely identify
47    UNSPEC_INT_LABELs.  */
48 int mn10300_unspec_int_label_counter;
49
50 /* This is used in the am33_2.0-linux-gnu port, in which global symbol
51    names are not prefixed by underscores, to tell whether to prefix a
52    label with a plus sign or not, so that the assembler can tell
53    symbol names from register names.  */
54 int mn10300_protect_label;
55
56 /* The selected processor.  */
57 enum processor_type mn10300_processor = PROCESSOR_DEFAULT;
58
59 /* The size of the callee register save area.  Right now we save everything
60    on entry since it costs us nothing in code size.  It does cost us from a
61    speed standpoint, so we want to optimize this sooner or later.  */
62 #define REG_SAVE_BYTES (4 * df_regs_ever_live_p (2)     \
63                         + 4 * df_regs_ever_live_p (3)   \
64                         + 4 * df_regs_ever_live_p (6)   \
65                         + 4 * df_regs_ever_live_p (7)                   \
66                         + 16 * (df_regs_ever_live_p (14) || df_regs_ever_live_p (15) \
67                                 || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)))
68
69
70 static bool mn10300_handle_option (size_t, const char *, int);
71 static int mn10300_address_cost_1 (rtx, int *);
72 static int mn10300_address_cost (rtx);
73 static bool mn10300_rtx_costs (rtx, int, int, int *);
74 static void mn10300_file_start (void);
75 static bool mn10300_return_in_memory (const_tree, const_tree);
76 static rtx mn10300_builtin_saveregs (void);
77 static void mn10300_va_start (tree, rtx);
78 static bool mn10300_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
79                                        const_tree, bool);
80 static int mn10300_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
81                                       tree, bool);
82 \f
83 /* Initialize the GCC target structure.  */
84 #undef TARGET_ASM_ALIGNED_HI_OP
85 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
86
87 #undef TARGET_RTX_COSTS
88 #define TARGET_RTX_COSTS mn10300_rtx_costs
89 #undef TARGET_ADDRESS_COST
90 #define TARGET_ADDRESS_COST mn10300_address_cost
91
92 #undef TARGET_ASM_FILE_START
93 #define TARGET_ASM_FILE_START mn10300_file_start
94 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
95 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
96
97 #undef TARGET_DEFAULT_TARGET_FLAGS
98 #define TARGET_DEFAULT_TARGET_FLAGS MASK_MULT_BUG | MASK_PTR_A0D0
99 #undef TARGET_HANDLE_OPTION
100 #define TARGET_HANDLE_OPTION mn10300_handle_option
101
102 #undef  TARGET_ENCODE_SECTION_INFO
103 #define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
104
105 #undef TARGET_PROMOTE_PROTOTYPES
106 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
107 #undef TARGET_RETURN_IN_MEMORY
108 #define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
109 #undef TARGET_PASS_BY_REFERENCE
110 #define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
111 #undef TARGET_CALLEE_COPIES
112 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
113 #undef TARGET_ARG_PARTIAL_BYTES
114 #define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
115
116 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
117 #define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
118 #undef TARGET_EXPAND_BUILTIN_VA_START
119 #define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
120
121 static void mn10300_encode_section_info (tree, rtx, int);
122 struct gcc_target targetm = TARGET_INITIALIZER;
123 \f
124 /* Implement TARGET_HANDLE_OPTION.  */
125
126 static bool
127 mn10300_handle_option (size_t code,
128                        const char *arg ATTRIBUTE_UNUSED,
129                        int value)
130 {
131   switch (code)
132     {
133     case OPT_mam33:
134       mn10300_processor = value ? PROCESSOR_AM33 : PROCESSOR_MN10300;
135       return true;
136     case OPT_mam33_2:
137       mn10300_processor = (value
138                            ? PROCESSOR_AM33_2
139                            : MIN (PROCESSOR_AM33, PROCESSOR_DEFAULT));
140       return true;
141     default:
142       return true;
143     }
144 }
145
146 /* Implement OVERRIDE_OPTIONS.  */
147
148 void
149 mn10300_override_options (void)
150 {
151   if (TARGET_AM33)
152     target_flags &= ~MASK_MULT_BUG;
153 }
154
155 static void
156 mn10300_file_start (void)
157 {
158   default_file_start ();
159
160   if (TARGET_AM33_2)
161     fprintf (asm_out_file, "\t.am33_2\n");
162   else if (TARGET_AM33)
163     fprintf (asm_out_file, "\t.am33\n");
164 }
165 \f
166
167 /* Print operand X using operand code CODE to assembly language output file
168    FILE.  */
169
170 void
171 print_operand (FILE *file, rtx x, int code)
172 {
173   switch (code)
174     {
175       case 'b':
176       case 'B':
177         if (cc_status.mdep.fpCC)
178           {
179             switch (code == 'b' ? GET_CODE (x)
180                     : reverse_condition_maybe_unordered (GET_CODE (x)))
181               {
182               case NE:
183                 fprintf (file, "ne");
184                 break;
185               case EQ:
186                 fprintf (file, "eq");
187                 break;
188               case GE:
189                 fprintf (file, "ge");
190                 break;
191               case GT:
192                 fprintf (file, "gt");
193                 break;
194               case LE:
195                 fprintf (file, "le");
196                 break;
197               case LT:
198                 fprintf (file, "lt");
199                 break;
200               case ORDERED:
201                 fprintf (file, "lge");
202                 break;
203               case UNORDERED:
204                 fprintf (file, "uo");
205                 break;
206               case LTGT:
207                 fprintf (file, "lg");
208                 break;
209               case UNEQ:
210                 fprintf (file, "ue");
211                 break;
212               case UNGE:
213                 fprintf (file, "uge");
214                 break;
215               case UNGT:
216                 fprintf (file, "ug");
217                 break;
218               case UNLE:
219                 fprintf (file, "ule");
220                 break;
221               case UNLT:
222                 fprintf (file, "ul");
223                 break;
224               default:
225                 gcc_unreachable ();
226               }
227             break;
228           }
229         /* These are normal and reversed branches.  */
230         switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
231           {
232           case NE:
233             fprintf (file, "ne");
234             break;
235           case EQ:
236             fprintf (file, "eq");
237             break;
238           case GE:
239             fprintf (file, "ge");
240             break;
241           case GT:
242             fprintf (file, "gt");
243             break;
244           case LE:
245             fprintf (file, "le");
246             break;
247           case LT:
248             fprintf (file, "lt");
249             break;
250           case GEU:
251             fprintf (file, "cc");
252             break;
253           case GTU:
254             fprintf (file, "hi");
255             break;
256           case LEU:
257             fprintf (file, "ls");
258             break;
259           case LTU:
260             fprintf (file, "cs");
261             break;
262           default:
263             gcc_unreachable ();
264           }
265         break;
266       case 'C':
267         /* This is used for the operand to a call instruction;
268            if it's a REG, enclose it in parens, else output
269            the operand normally.  */
270         if (GET_CODE (x) == REG)
271           {
272             fputc ('(', file);
273             print_operand (file, x, 0);
274             fputc (')', file);
275           }
276         else
277           print_operand (file, x, 0);
278         break;
279
280       case 'D':
281         switch (GET_CODE (x))
282           {
283           case MEM:
284             fputc ('(', file);
285             output_address (XEXP (x, 0));
286             fputc (')', file);
287             break;
288
289           case REG:
290             fprintf (file, "fd%d", REGNO (x) - 18);
291             break;
292
293           default:
294             gcc_unreachable ();
295           }
296         break;
297
298       /* These are the least significant word in a 64bit value.  */
299       case 'L':
300         switch (GET_CODE (x))
301           {
302           case MEM:
303             fputc ('(', file);
304             output_address (XEXP (x, 0));
305             fputc (')', file);
306             break;
307
308           case REG:
309             fprintf (file, "%s", reg_names[REGNO (x)]);
310             break;
311
312           case SUBREG:
313             fprintf (file, "%s", reg_names[subreg_regno (x)]);
314             break;
315
316           case CONST_DOUBLE:
317               {
318                 long val[2];
319                 REAL_VALUE_TYPE rv;
320
321                 switch (GET_MODE (x))
322                   {
323                     case DFmode:
324                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
325                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
326                       fprintf (file, "0x%lx", val[0]);
327                       break;;
328                     case SFmode:
329                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
330                       REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
331                       fprintf (file, "0x%lx", val[0]);
332                       break;;
333                     case VOIDmode:
334                     case DImode:
335                       print_operand_address (file,
336                                              GEN_INT (CONST_DOUBLE_LOW (x)));
337                       break;
338                     default:
339                       break;
340                   }
341                 break;
342               }
343
344           case CONST_INT:
345             {
346               rtx low, high;
347               split_double (x, &low, &high);
348               fprintf (file, "%ld", (long)INTVAL (low));
349               break;
350             }
351
352           default:
353             gcc_unreachable ();
354           }
355         break;
356
357       /* Similarly, but for the most significant word.  */
358       case 'H':
359         switch (GET_CODE (x))
360           {
361           case MEM:
362             fputc ('(', file);
363             x = adjust_address (x, SImode, 4);
364             output_address (XEXP (x, 0));
365             fputc (')', file);
366             break;
367
368           case REG:
369             fprintf (file, "%s", reg_names[REGNO (x) + 1]);
370             break;
371
372           case SUBREG:
373             fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
374             break;
375
376           case CONST_DOUBLE:
377               {
378                 long val[2];
379                 REAL_VALUE_TYPE rv;
380
381                 switch (GET_MODE (x))
382                   {
383                     case DFmode:
384                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
385                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
386                       fprintf (file, "0x%lx", val[1]);
387                       break;;
388                     case SFmode:
389                       gcc_unreachable ();
390                     case VOIDmode:
391                     case DImode:
392                       print_operand_address (file,
393                                              GEN_INT (CONST_DOUBLE_HIGH (x)));
394                       break;
395                     default:
396                       break;
397                   }
398                 break;
399               }
400
401           case CONST_INT:
402             {
403               rtx low, high;
404               split_double (x, &low, &high);
405               fprintf (file, "%ld", (long)INTVAL (high));
406               break;
407             }
408
409           default:
410             gcc_unreachable ();
411           }
412         break;
413
414       case 'A':
415         fputc ('(', file);
416         if (GET_CODE (XEXP (x, 0)) == REG)
417           output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), const0_rtx));
418         else
419           output_address (XEXP (x, 0));
420         fputc (')', file);
421         break;
422
423       case 'N':
424         gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
425         fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
426         break;
427
428       case 'U':
429         gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
430         fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
431         break;
432
433       /* For shift counts.  The hardware ignores the upper bits of
434          any immediate, but the assembler will flag an out of range
435          shift count as an error.  So we mask off the high bits
436          of the immediate here.  */
437       case 'S':
438         if (GET_CODE (x) == CONST_INT)
439           {
440             fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
441             break;
442           }
443         /* FALL THROUGH */
444
445       default:
446         switch (GET_CODE (x))
447           {
448           case MEM:
449             fputc ('(', file);
450             output_address (XEXP (x, 0));
451             fputc (')', file);
452             break;
453
454           case PLUS:
455             output_address (x);
456             break;
457
458           case REG:
459             fprintf (file, "%s", reg_names[REGNO (x)]);
460             break;
461
462           case SUBREG:
463             fprintf (file, "%s", reg_names[subreg_regno (x)]);
464             break;
465
466           /* This will only be single precision....  */
467           case CONST_DOUBLE:
468             {
469               unsigned long val;
470               REAL_VALUE_TYPE rv;
471
472               REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
473               REAL_VALUE_TO_TARGET_SINGLE (rv, val);
474               fprintf (file, "0x%lx", val);
475               break;
476             }
477
478           case CONST_INT:
479           case SYMBOL_REF:
480           case CONST:
481           case LABEL_REF:
482           case CODE_LABEL:
483           case UNSPEC:
484             print_operand_address (file, x);
485             break;
486           default:
487             gcc_unreachable ();
488           }
489         break;
490    }
491 }
492
493 /* Output assembly language output for the address ADDR to FILE.  */
494
495 void
496 print_operand_address (FILE *file, rtx addr)
497 {
498   switch (GET_CODE (addr))
499     {
500     case POST_INC:
501       print_operand_address (file, XEXP (addr, 0));
502       fputc ('+', file);
503       break;
504     case REG:
505       print_operand (file, addr, 0);
506       break;
507     case PLUS:
508       {
509         rtx base, index;
510         if (REG_P (XEXP (addr, 0))
511             && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
512           base = XEXP (addr, 0), index = XEXP (addr, 1);
513         else if (REG_P (XEXP (addr, 1))
514             && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
515           base = XEXP (addr, 1), index = XEXP (addr, 0);
516         else
517           gcc_unreachable ();
518         print_operand (file, index, 0);
519         fputc (',', file);
520         print_operand (file, base, 0);;
521         break;
522       }
523     case SYMBOL_REF:
524       output_addr_const (file, addr);
525       break;
526     default:
527       output_addr_const (file, addr);
528       break;
529     }
530 }
531
532 /* Count the number of FP registers that have to be saved.  */
533 static int
534 fp_regs_to_save (void)
535 {
536   int i, n = 0;
537
538   if (! TARGET_AM33_2)
539     return 0;
540
541   for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
542     if (df_regs_ever_live_p (i) && ! call_used_regs[i])
543       ++n;
544
545   return n;
546 }
547
548 /* Print a set of registers in the format required by "movm" and "ret".
549    Register K is saved if bit K of MASK is set.  The data and address
550    registers can be stored individually, but the extended registers cannot.
551    We assume that the mask already takes that into account.  For instance,
552    bits 14 to 17 must have the same value.  */
553
554 void
555 mn10300_print_reg_list (FILE *file, int mask)
556 {
557   int need_comma;
558   int i;
559
560   need_comma = 0;
561   fputc ('[', file);
562
563   for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
564     if ((mask & (1 << i)) != 0)
565       {
566         if (need_comma)
567           fputc (',', file);
568         fputs (reg_names [i], file);
569         need_comma = 1;
570       }
571
572   if ((mask & 0x3c000) != 0)
573     {
574       gcc_assert ((mask & 0x3c000) == 0x3c000);
575       if (need_comma)
576         fputc (',', file);
577       fputs ("exreg1", file);
578       need_comma = 1;
579     }
580
581   fputc (']', file);
582 }
583
584 int
585 can_use_return_insn (void)
586 {
587   /* size includes the fixed stack space needed for function calls.  */
588   int size = get_frame_size () + current_function_outgoing_args_size;
589
590   /* And space for the return pointer.  */
591   size += current_function_outgoing_args_size ? 4 : 0;
592
593   return (reload_completed
594           && size == 0
595           && !df_regs_ever_live_p (2)
596           && !df_regs_ever_live_p (3)
597           && !df_regs_ever_live_p (6)
598           && !df_regs_ever_live_p (7)
599           && !df_regs_ever_live_p (14)
600           && !df_regs_ever_live_p (15)
601           && !df_regs_ever_live_p (16)
602           && !df_regs_ever_live_p (17)
603           && fp_regs_to_save () == 0
604           && !frame_pointer_needed);
605 }
606
607 /* Returns the set of live, callee-saved registers as a bitmask.  The
608    callee-saved extended registers cannot be stored individually, so
609    all of them will be included in the mask if any one of them is used.  */
610
611 int
612 mn10300_get_live_callee_saved_regs (void)
613 {
614   int mask;
615   int i;
616
617   mask = 0;
618   for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
619     if (df_regs_ever_live_p (i) && ! call_used_regs[i])
620       mask |= (1 << i);
621   if ((mask & 0x3c000) != 0)
622     mask |= 0x3c000;
623
624   return mask;
625 }
626
627 /* Generate an instruction that pushes several registers onto the stack.
628    Register K will be saved if bit K in MASK is set.  The function does
629    nothing if MASK is zero.
630
631    To be compatible with the "movm" instruction, the lowest-numbered
632    register must be stored in the lowest slot.  If MASK is the set
633    { R1,...,RN }, where R1...RN are ordered least first, the generated
634    instruction will have the form:
635
636        (parallel
637          (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
638          (set (mem:SI (plus:SI (reg:SI 9)
639                                (const_int -1*4)))
640               (reg:SI RN))
641          ...
642          (set (mem:SI (plus:SI (reg:SI 9)
643                                (const_int -N*4)))
644               (reg:SI R1))) */
645
646 void
647 mn10300_gen_multiple_store (int mask)
648 {
649   if (mask != 0)
650     {
651       int i;
652       int count;
653       rtx par;
654       int pari;
655
656       /* Count how many registers need to be saved.  */
657       count = 0;
658       for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
659         if ((mask & (1 << i)) != 0)
660           count += 1;
661
662       /* We need one PARALLEL element to update the stack pointer and
663          an additional element for each register that is stored.  */
664       par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count + 1));
665
666       /* Create the instruction that updates the stack pointer.  */
667       XVECEXP (par, 0, 0)
668         = gen_rtx_SET (SImode,
669                        stack_pointer_rtx,
670                        gen_rtx_PLUS (SImode,
671                                      stack_pointer_rtx,
672                                      GEN_INT (-count * 4)));
673
674       /* Create each store.  */
675       pari = 1;
676       for (i = LAST_EXTENDED_REGNUM; i >= 0; i--)
677         if ((mask & (1 << i)) != 0)
678           {
679             rtx address = gen_rtx_PLUS (SImode,
680                                         stack_pointer_rtx,
681                                         GEN_INT (-pari * 4));
682             XVECEXP(par, 0, pari)
683               = gen_rtx_SET (VOIDmode,
684                              gen_rtx_MEM (SImode, address),
685                              gen_rtx_REG (SImode, i));
686             pari += 1;
687           }
688
689       par = emit_insn (par);
690       RTX_FRAME_RELATED_P (par) = 1;
691     }
692 }
693
694 void
695 expand_prologue (void)
696 {
697   HOST_WIDE_INT size;
698
699   /* SIZE includes the fixed stack space needed for function calls.  */
700   size = get_frame_size () + current_function_outgoing_args_size;
701   size += (current_function_outgoing_args_size ? 4 : 0);
702
703   /* If we use any of the callee-saved registers, save them now.  */
704   mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs ());
705
706   if (TARGET_AM33_2 && fp_regs_to_save ())
707     {
708       int num_regs_to_save = fp_regs_to_save (), i;
709       HOST_WIDE_INT xsize;
710       enum { save_sp_merge,
711              save_sp_no_merge,
712              save_sp_partial_merge,
713              save_a0_merge,
714              save_a0_no_merge } strategy;
715       unsigned int strategy_size = (unsigned)-1, this_strategy_size;
716       rtx reg;
717       rtx insn;
718
719       /* We have several different strategies to save FP registers.
720          We can store them using SP offsets, which is beneficial if
721          there are just a few registers to save, or we can use `a0' in
722          post-increment mode (`a0' is the only call-clobbered address
723          register that is never used to pass information to a
724          function).  Furthermore, if we don't need a frame pointer, we
725          can merge the two SP adds into a single one, but this isn't
726          always beneficial; sometimes we can just split the two adds
727          so that we don't exceed a 16-bit constant size.  The code
728          below will select which strategy to use, so as to generate
729          smallest code.  Ties are broken in favor or shorter sequences
730          (in terms of number of instructions).  */
731
732 #define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
733                         : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
734 #define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
735                         : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
736 #define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
737   (((S) >= (L)) ? (SIZE1) * (N) \
738    : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
739                                + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
740    : (ELSE))
741 #define SIZE_FMOV_SP_(S,N) \
742   (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
743                    SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
744                                     (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
745 #define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
746
747       /* Consider alternative save_sp_merge only if we don't need the
748          frame pointer and size is nonzero.  */
749       if (! frame_pointer_needed && size)
750         {
751           /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
752           this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
753           /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
754           this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
755
756           if (this_strategy_size < strategy_size)
757             {
758               strategy = save_sp_merge;
759               strategy_size = this_strategy_size;
760             }
761         }
762
763       /* Consider alternative save_sp_no_merge unconditionally.  */
764       /* Insn: add -4 * num_regs_to_save, sp.  */
765       this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
766       /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
767       this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
768       if (size)
769         {
770           /* Insn: add -size, sp.  */
771           this_strategy_size += SIZE_ADD_SP (-size);
772         }
773
774       if (this_strategy_size < strategy_size)
775         {
776           strategy = save_sp_no_merge;
777           strategy_size = this_strategy_size;
778         }
779
780       /* Consider alternative save_sp_partial_merge only if we don't
781          need a frame pointer and size is reasonably large.  */
782       if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
783         {
784           /* Insn: add -128, sp.  */
785           this_strategy_size = SIZE_ADD_SP (-128);
786           /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
787           this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
788                                               num_regs_to_save);
789           if (size)
790             {
791               /* Insn: add 128-size, sp.  */
792               this_strategy_size += SIZE_ADD_SP (128 - size);
793             }
794
795           if (this_strategy_size < strategy_size)
796             {
797               strategy = save_sp_partial_merge;
798               strategy_size = this_strategy_size;
799             }
800         }
801
802       /* Consider alternative save_a0_merge only if we don't need a
803          frame pointer, size is nonzero and the user hasn't
804          changed the calling conventions of a0.  */
805       if (! frame_pointer_needed && size
806           && call_used_regs[FIRST_ADDRESS_REGNUM]
807           && ! fixed_regs[FIRST_ADDRESS_REGNUM])
808         {
809           /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
810           this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
811           /* Insn: mov sp, a0.  */
812           this_strategy_size++;
813           if (size)
814             {
815               /* Insn: add size, a0.  */
816               this_strategy_size += SIZE_ADD_AX (size);
817             }
818           /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
819           this_strategy_size += 3 * num_regs_to_save;
820
821           if (this_strategy_size < strategy_size)
822             {
823               strategy = save_a0_merge;
824               strategy_size = this_strategy_size;
825             }
826         }
827
828       /* Consider alternative save_a0_no_merge if the user hasn't
829          changed the calling conventions of a0.  */
830       if (call_used_regs[FIRST_ADDRESS_REGNUM]
831           && ! fixed_regs[FIRST_ADDRESS_REGNUM])
832         {
833           /* Insn: add -4 * num_regs_to_save, sp.  */
834           this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
835           /* Insn: mov sp, a0.  */
836           this_strategy_size++;
837           /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
838           this_strategy_size += 3 * num_regs_to_save;
839           if (size)
840             {
841               /* Insn: add -size, sp.  */
842               this_strategy_size += SIZE_ADD_SP (-size);
843             }
844
845           if (this_strategy_size < strategy_size)
846             {
847               strategy = save_a0_no_merge;
848               strategy_size = this_strategy_size;
849             }
850         }
851
852       /* Emit the initial SP add, common to all strategies.  */
853       switch (strategy)
854         {
855         case save_sp_no_merge:
856         case save_a0_no_merge:
857           emit_insn (gen_addsi3 (stack_pointer_rtx,
858                                  stack_pointer_rtx,
859                                  GEN_INT (-4 * num_regs_to_save)));
860           xsize = 0;
861           break;
862
863         case save_sp_partial_merge:
864           emit_insn (gen_addsi3 (stack_pointer_rtx,
865                                  stack_pointer_rtx,
866                                  GEN_INT (-128)));
867           xsize = 128 - 4 * num_regs_to_save;
868           size -= xsize;
869           break;
870
871         case save_sp_merge:
872         case save_a0_merge:
873           emit_insn (gen_addsi3 (stack_pointer_rtx,
874                                  stack_pointer_rtx,
875                                  GEN_INT (-(size + 4 * num_regs_to_save))));
876           /* We'll have to adjust FP register saves according to the
877              frame size.  */
878           xsize = size;
879           /* Since we've already created the stack frame, don't do it
880              again at the end of the function.  */
881           size = 0;
882           break;
883
884         default:
885           gcc_unreachable ();
886         }
887
888       /* Now prepare register a0, if we have decided to use it.  */
889       switch (strategy)
890         {
891         case save_sp_merge:
892         case save_sp_no_merge:
893         case save_sp_partial_merge:
894           reg = 0;
895           break;
896
897         case save_a0_merge:
898         case save_a0_no_merge:
899           reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
900           emit_insn (gen_movsi (reg, stack_pointer_rtx));
901           if (xsize)
902             emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize)));
903           reg = gen_rtx_POST_INC (SImode, reg);
904           break;
905
906         default:
907           gcc_unreachable ();
908         }
909
910       /* Now actually save the FP registers.  */
911       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
912         if (df_regs_ever_live_p (i) && ! call_used_regs[i])
913           {
914             rtx addr;
915
916             if (reg)
917               addr = reg;
918             else
919               {
920                 /* If we aren't using `a0', use an SP offset.  */
921                 if (xsize)
922                   {
923                     addr = gen_rtx_PLUS (SImode,
924                                          stack_pointer_rtx,
925                                          GEN_INT (xsize));
926                   }
927                 else
928                   addr = stack_pointer_rtx;
929
930                 xsize += 4;
931               }
932
933             insn = emit_insn (gen_movsi (gen_rtx_MEM (SImode, addr),
934                                          gen_rtx_REG (SImode, i)));
935
936             RTX_FRAME_RELATED_P (insn) = 1;
937           }
938     }
939
940   /* Now put the frame pointer into the frame pointer register.  */
941   if (frame_pointer_needed)
942     emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
943
944   /* Allocate stack for this frame.  */
945   if (size)
946     emit_insn (gen_addsi3 (stack_pointer_rtx,
947                            stack_pointer_rtx,
948                            GEN_INT (-size)));
949   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
950     emit_insn (gen_GOTaddr2picreg ());
951 }
952
953 void
954 expand_epilogue (void)
955 {
956   HOST_WIDE_INT size;
957
958   /* SIZE includes the fixed stack space needed for function calls.  */
959   size = get_frame_size () + current_function_outgoing_args_size;
960   size += (current_function_outgoing_args_size ? 4 : 0);
961
962   if (TARGET_AM33_2 && fp_regs_to_save ())
963     {
964       int num_regs_to_save = fp_regs_to_save (), i;
965       rtx reg = 0;
966
967       /* We have several options to restore FP registers.  We could
968          load them from SP offsets, but, if there are enough FP
969          registers to restore, we win if we use a post-increment
970          addressing mode.  */
971
972       /* If we have a frame pointer, it's the best option, because we
973          already know it has the value we want.  */
974       if (frame_pointer_needed)
975         reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
976       /* Otherwise, we may use `a1', since it's call-clobbered and
977          it's never used for return values.  But only do so if it's
978          smaller than using SP offsets.  */
979       else
980         {
981           enum { restore_sp_post_adjust,
982                  restore_sp_pre_adjust,
983                  restore_sp_partial_adjust,
984                  restore_a1 } strategy;
985           unsigned int this_strategy_size, strategy_size = (unsigned)-1;
986
987           /* Consider using sp offsets before adjusting sp.  */
988           /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
989           this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
990           /* If size is too large, we'll have to adjust SP with an
991                  add.  */
992           if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
993             {
994               /* Insn: add size + 4 * num_regs_to_save, sp.  */
995               this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
996             }
997           /* If we don't have to restore any non-FP registers,
998                  we'll be able to save one byte by using rets.  */
999           if (! REG_SAVE_BYTES)
1000             this_strategy_size--;
1001
1002           if (this_strategy_size < strategy_size)
1003             {
1004               strategy = restore_sp_post_adjust;
1005               strategy_size = this_strategy_size;
1006             }
1007
1008           /* Consider using sp offsets after adjusting sp.  */
1009           /* Insn: add size, sp.  */
1010           this_strategy_size = SIZE_ADD_SP (size);
1011           /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
1012           this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
1013           /* We're going to use ret to release the FP registers
1014                  save area, so, no savings.  */
1015
1016           if (this_strategy_size < strategy_size)
1017             {
1018               strategy = restore_sp_pre_adjust;
1019               strategy_size = this_strategy_size;
1020             }
1021
1022           /* Consider using sp offsets after partially adjusting sp.
1023              When size is close to 32Kb, we may be able to adjust SP
1024              with an imm16 add instruction while still using fmov
1025              (d8,sp).  */
1026           if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1027             {
1028               /* Insn: add size + 4 * num_regs_to_save
1029                                 + REG_SAVE_BYTES - 252,sp.  */
1030               this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
1031                                                 + REG_SAVE_BYTES - 252);
1032               /* Insn: fmov (##,sp),fs#, fo each fs# to be restored.  */
1033               this_strategy_size += SIZE_FMOV_SP (252 - REG_SAVE_BYTES
1034                                                   - 4 * num_regs_to_save,
1035                                                   num_regs_to_save);
1036               /* We're going to use ret to release the FP registers
1037                  save area, so, no savings.  */
1038
1039               if (this_strategy_size < strategy_size)
1040                 {
1041                   strategy = restore_sp_partial_adjust;
1042                   strategy_size = this_strategy_size;
1043                 }
1044             }
1045
1046           /* Consider using a1 in post-increment mode, as long as the
1047              user hasn't changed the calling conventions of a1.  */
1048           if (call_used_regs[FIRST_ADDRESS_REGNUM+1]
1049               && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1050             {
1051               /* Insn: mov sp,a1.  */
1052               this_strategy_size = 1;
1053               if (size)
1054                 {
1055                   /* Insn: add size,a1.  */
1056                   this_strategy_size += SIZE_ADD_AX (size);
1057                 }
1058               /* Insn: fmov (a1+),fs#, for each fs# to be restored.  */
1059               this_strategy_size += 3 * num_regs_to_save;
1060               /* If size is large enough, we may be able to save a
1061                  couple of bytes.  */
1062               if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1063                 {
1064                   /* Insn: mov a1,sp.  */
1065                   this_strategy_size += 2;
1066                 }
1067               /* If we don't have to restore any non-FP registers,
1068                  we'll be able to save one byte by using rets.  */
1069               if (! REG_SAVE_BYTES)
1070                 this_strategy_size--;
1071
1072               if (this_strategy_size < strategy_size)
1073                 {
1074                   strategy = restore_a1;
1075                   strategy_size = this_strategy_size;
1076                 }
1077             }
1078
1079           switch (strategy)
1080             {
1081             case restore_sp_post_adjust:
1082               break;
1083
1084             case restore_sp_pre_adjust:
1085               emit_insn (gen_addsi3 (stack_pointer_rtx,
1086                                      stack_pointer_rtx,
1087                                      GEN_INT (size)));
1088               size = 0;
1089               break;
1090
1091             case restore_sp_partial_adjust:
1092               emit_insn (gen_addsi3 (stack_pointer_rtx,
1093                                      stack_pointer_rtx,
1094                                      GEN_INT (size + 4 * num_regs_to_save
1095                                               + REG_SAVE_BYTES - 252)));
1096               size = 252 - REG_SAVE_BYTES - 4 * num_regs_to_save;
1097               break;
1098
1099             case restore_a1:
1100               reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1101               emit_insn (gen_movsi (reg, stack_pointer_rtx));
1102               if (size)
1103                 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1104               break;
1105
1106             default:
1107               gcc_unreachable ();
1108             }
1109         }
1110
1111       /* Adjust the selected register, if any, for post-increment.  */
1112       if (reg)
1113         reg = gen_rtx_POST_INC (SImode, reg);
1114
1115       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1116         if (df_regs_ever_live_p (i) && ! call_used_regs[i])
1117           {
1118             rtx addr;
1119
1120             if (reg)
1121               addr = reg;
1122             else if (size)
1123               {
1124                 /* If we aren't using a post-increment register, use an
1125                    SP offset.  */
1126                 addr = gen_rtx_PLUS (SImode,
1127                                      stack_pointer_rtx,
1128                                      GEN_INT (size));
1129               }
1130             else
1131               addr = stack_pointer_rtx;
1132
1133             size += 4;
1134
1135             emit_insn (gen_movsi (gen_rtx_REG (SImode, i),
1136                                   gen_rtx_MEM (SImode, addr)));
1137           }
1138
1139       /* If we were using the restore_a1 strategy and the number of
1140          bytes to be released won't fit in the `ret' byte, copy `a1'
1141          to `sp', to avoid having to use `add' to adjust it.  */
1142       if (! frame_pointer_needed && reg && size + REG_SAVE_BYTES > 255)
1143         {
1144           emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1145           size = 0;
1146         }
1147     }
1148
1149   /* Maybe cut back the stack, except for the register save area.
1150
1151      If the frame pointer exists, then use the frame pointer to
1152      cut back the stack.
1153
1154      If the stack size + register save area is more than 255 bytes,
1155      then the stack must be cut back here since the size + register
1156      save size is too big for a ret/retf instruction.
1157
1158      Else leave it alone, it will be cut back as part of the
1159      ret/retf instruction, or there wasn't any stack to begin with.
1160
1161      Under no circumstances should the register save area be
1162      deallocated here, that would leave a window where an interrupt
1163      could occur and trash the register save area.  */
1164   if (frame_pointer_needed)
1165     {
1166       emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1167       size = 0;
1168     }
1169   else if (size + REG_SAVE_BYTES > 255)
1170     {
1171       emit_insn (gen_addsi3 (stack_pointer_rtx,
1172                              stack_pointer_rtx,
1173                              GEN_INT (size)));
1174       size = 0;
1175     }
1176
1177   /* Adjust the stack and restore callee-saved registers, if any.  */
1178   if (size || df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1179       || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1180       || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1181       || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1182       || frame_pointer_needed)
1183     emit_jump_insn (gen_return_internal_regs
1184                     (GEN_INT (size + REG_SAVE_BYTES)));
1185   else
1186     emit_jump_insn (gen_return_internal ());
1187 }
1188
1189 /* Update the condition code from the insn.  */
1190
1191 void
1192 notice_update_cc (rtx body, rtx insn)
1193 {
1194   switch (get_attr_cc (insn))
1195     {
1196     case CC_NONE:
1197       /* Insn does not affect CC at all.  */
1198       break;
1199
1200     case CC_NONE_0HIT:
1201       /* Insn does not change CC, but the 0'th operand has been changed.  */
1202       if (cc_status.value1 != 0
1203           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1204         cc_status.value1 = 0;
1205       break;
1206
1207     case CC_SET_ZN:
1208       /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1209          V,C are unusable.  */
1210       CC_STATUS_INIT;
1211       cc_status.flags |= CC_NO_CARRY | CC_OVERFLOW_UNUSABLE;
1212       cc_status.value1 = recog_data.operand[0];
1213       break;
1214
1215     case CC_SET_ZNV:
1216       /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1217          C is unusable.  */
1218       CC_STATUS_INIT;
1219       cc_status.flags |= CC_NO_CARRY;
1220       cc_status.value1 = recog_data.operand[0];
1221       break;
1222
1223     case CC_COMPARE:
1224       /* The insn is a compare instruction.  */
1225       CC_STATUS_INIT;
1226       cc_status.value1 = SET_SRC (body);
1227       if (GET_CODE (cc_status.value1) == COMPARE
1228           && GET_MODE (XEXP (cc_status.value1, 0)) == SFmode)
1229         cc_status.mdep.fpCC = 1;
1230       break;
1231
1232     case CC_CLOBBER:
1233       /* Insn doesn't leave CC in a usable state.  */
1234       CC_STATUS_INIT;
1235       break;
1236
1237     default:
1238       gcc_unreachable ();
1239     }
1240 }
1241
1242 /* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
1243    This function is for MATCH_PARALLEL and so assumes OP is known to be
1244    parallel.  If OP is a multiple store, return a mask indicating which
1245    registers it saves.  Return 0 otherwise.  */
1246
1247 int
1248 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1249 {
1250   int count;
1251   int mask;
1252   int i;
1253   unsigned int last;
1254   rtx elt;
1255
1256   count = XVECLEN (op, 0);
1257   if (count < 2)
1258     return 0;
1259
1260   /* Check that first instruction has the form (set (sp) (plus A B)) */
1261   elt = XVECEXP (op, 0, 0);
1262   if (GET_CODE (elt) != SET
1263       || GET_CODE (SET_DEST (elt)) != REG
1264       || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1265       || GET_CODE (SET_SRC (elt)) != PLUS)
1266     return 0;
1267
1268   /* Check that A is the stack pointer and B is the expected stack size.
1269      For OP to match, each subsequent instruction should push a word onto
1270      the stack.  We therefore expect the first instruction to create
1271      COUNT-1 stack slots.  */
1272   elt = SET_SRC (elt);
1273   if (GET_CODE (XEXP (elt, 0)) != REG
1274       || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1275       || GET_CODE (XEXP (elt, 1)) != CONST_INT
1276       || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1277     return 0;
1278
1279   /* Now go through the rest of the vector elements.  They must be
1280      ordered so that the first instruction stores the highest-numbered
1281      register to the highest stack slot and that subsequent instructions
1282      store a lower-numbered register to the slot below.
1283
1284      LAST keeps track of the smallest-numbered register stored so far.
1285      MASK is the set of stored registers.  */
1286   last = LAST_EXTENDED_REGNUM + 1;
1287   mask = 0;
1288   for (i = 1; i < count; i++)
1289     {
1290       /* Check that element i is a (set (mem M) R) and that R is valid.  */
1291       elt = XVECEXP (op, 0, i);
1292       if (GET_CODE (elt) != SET
1293           || GET_CODE (SET_DEST (elt)) != MEM
1294           || GET_CODE (SET_SRC (elt)) != REG
1295           || REGNO (SET_SRC (elt)) >= last)
1296         return 0;
1297
1298       /* R was OK, so provisionally add it to MASK.  We return 0 in any
1299          case if the rest of the instruction has a flaw.  */
1300       last = REGNO (SET_SRC (elt));
1301       mask |= (1 << last);
1302
1303       /* Check that M has the form (plus (sp) (const_int -I*4)) */
1304       elt = XEXP (SET_DEST (elt), 0);
1305       if (GET_CODE (elt) != PLUS
1306           || GET_CODE (XEXP (elt, 0)) != REG
1307           || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1308           || GET_CODE (XEXP (elt, 1)) != CONST_INT
1309           || INTVAL (XEXP (elt, 1)) != -i * 4)
1310         return 0;
1311     }
1312
1313   /* All or none of the callee-saved extended registers must be in the set.  */
1314   if ((mask & 0x3c000) != 0
1315       && (mask & 0x3c000) != 0x3c000)
1316     return 0;
1317
1318   return mask;
1319 }
1320
1321 /* What (if any) secondary registers are needed to move IN with mode
1322    MODE into a register in register class CLASS.
1323
1324    We might be able to simplify this.  */
1325 enum reg_class
1326 mn10300_secondary_reload_class (enum reg_class class, enum machine_mode mode,
1327                                 rtx in)
1328 {
1329   /* Memory loads less than a full word wide can't have an
1330      address or stack pointer destination.  They must use
1331      a data register as an intermediate register.  */
1332   if ((GET_CODE (in) == MEM
1333        || (GET_CODE (in) == REG
1334            && REGNO (in) >= FIRST_PSEUDO_REGISTER)
1335        || (GET_CODE (in) == SUBREG
1336            && GET_CODE (SUBREG_REG (in)) == REG
1337            && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER))
1338       && (mode == QImode || mode == HImode)
1339       && (class == ADDRESS_REGS || class == SP_REGS
1340           || class == SP_OR_ADDRESS_REGS))
1341     {
1342       if (TARGET_AM33)
1343         return DATA_OR_EXTENDED_REGS;
1344       return DATA_REGS;
1345     }
1346
1347   /* We can't directly load sp + const_int into a data register;
1348      we must use an address register as an intermediate.  */
1349   if (class != SP_REGS
1350       && class != ADDRESS_REGS
1351       && class != SP_OR_ADDRESS_REGS
1352       && class != SP_OR_EXTENDED_REGS
1353       && class != ADDRESS_OR_EXTENDED_REGS
1354       && class != SP_OR_ADDRESS_OR_EXTENDED_REGS
1355       && (in == stack_pointer_rtx
1356           || (GET_CODE (in) == PLUS
1357               && (XEXP (in, 0) == stack_pointer_rtx
1358                   || XEXP (in, 1) == stack_pointer_rtx))))
1359     return ADDRESS_REGS;
1360
1361   if (GET_CODE (in) == PLUS
1362       && (XEXP (in, 0) == stack_pointer_rtx
1363           || XEXP (in, 1) == stack_pointer_rtx))
1364     return GENERAL_REGS;
1365
1366   if (TARGET_AM33_2 && class == FP_REGS
1367       && GET_CODE (in) == MEM
1368       && ! (GET_CODE (in) == MEM && !CONSTANT_ADDRESS_P (XEXP (in, 0))))
1369     {
1370       if (TARGET_AM33)
1371         return DATA_OR_EXTENDED_REGS;
1372       return DATA_REGS;
1373     }
1374
1375   /* Otherwise assume no secondary reloads are needed.  */
1376   return NO_REGS;
1377 }
1378
1379 int
1380 initial_offset (int from, int to)
1381 {
1382   /* The difference between the argument pointer and the frame pointer
1383      is the size of the callee register save area.  */
1384   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1385     {
1386       if (df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1387           || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1388           || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1389           || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1390           || fp_regs_to_save ()
1391           || frame_pointer_needed)
1392         return REG_SAVE_BYTES
1393           + 4 * fp_regs_to_save ();
1394       else
1395         return 0;
1396     }
1397
1398   /* The difference between the argument pointer and the stack pointer is
1399      the sum of the size of this function's frame, the callee register save
1400      area, and the fixed stack space needed for function calls (if any).  */
1401   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1402     {
1403       if (df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1404           || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1405           || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1406           || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1407           || fp_regs_to_save ()
1408           || frame_pointer_needed)
1409         return (get_frame_size () + REG_SAVE_BYTES
1410                 + 4 * fp_regs_to_save ()
1411                 + (current_function_outgoing_args_size
1412                    ? current_function_outgoing_args_size + 4 : 0));
1413       else
1414         return (get_frame_size ()
1415                 + (current_function_outgoing_args_size
1416                    ? current_function_outgoing_args_size + 4 : 0));
1417     }
1418
1419   /* The difference between the frame pointer and stack pointer is the sum
1420      of the size of this function's frame and the fixed stack space needed
1421      for function calls (if any).  */
1422   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1423     return (get_frame_size ()
1424             + (current_function_outgoing_args_size
1425                ? current_function_outgoing_args_size + 4 : 0));
1426
1427   gcc_unreachable ();
1428 }
1429
1430 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
1431
1432 static bool
1433 mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1434 {
1435   /* Return values > 8 bytes in length in memory.  */
1436   return (int_size_in_bytes (type) > 8
1437           || int_size_in_bytes (type) == 0
1438           || TYPE_MODE (type) == BLKmode);
1439 }
1440
1441 /* Flush the argument registers to the stack for a stdarg function;
1442    return the new argument pointer.  */
1443 static rtx
1444 mn10300_builtin_saveregs (void)
1445 {
1446   rtx offset, mem;
1447   tree fntype = TREE_TYPE (current_function_decl);
1448   int argadj = ((!(TYPE_ARG_TYPES (fntype) != 0
1449                    && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1450                        != void_type_node)))
1451                 ? UNITS_PER_WORD : 0);
1452   alias_set_type set = get_varargs_alias_set ();
1453
1454   if (argadj)
1455     offset = plus_constant (current_function_arg_offset_rtx, argadj);
1456   else
1457     offset = current_function_arg_offset_rtx;
1458
1459   mem = gen_rtx_MEM (SImode, current_function_internal_arg_pointer);
1460   set_mem_alias_set (mem, set);
1461   emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1462
1463   mem = gen_rtx_MEM (SImode,
1464                      plus_constant (current_function_internal_arg_pointer, 4));
1465   set_mem_alias_set (mem, set);
1466   emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1467
1468   return copy_to_reg (expand_binop (Pmode, add_optab,
1469                                     current_function_internal_arg_pointer,
1470                                     offset, 0, 0, OPTAB_LIB_WIDEN));
1471 }
1472
1473 static void
1474 mn10300_va_start (tree valist, rtx nextarg)
1475 {
1476   nextarg = expand_builtin_saveregs ();
1477   std_expand_builtin_va_start (valist, nextarg);
1478 }
1479
1480 /* Return true when a parameter should be passed by reference.  */
1481
1482 static bool
1483 mn10300_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
1484                            enum machine_mode mode, const_tree type,
1485                            bool named ATTRIBUTE_UNUSED)
1486 {
1487   unsigned HOST_WIDE_INT size;
1488
1489   if (type)
1490     size = int_size_in_bytes (type);
1491   else
1492     size = GET_MODE_SIZE (mode);
1493
1494   return (size > 8 || size == 0);
1495 }
1496
1497 /* Return an RTX to represent where a value with mode MODE will be returned
1498    from a function.  If the result is 0, the argument is pushed.  */
1499
1500 rtx
1501 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1502               tree type, int named ATTRIBUTE_UNUSED)
1503 {
1504   rtx result = 0;
1505   int size, align;
1506
1507   /* We only support using 2 data registers as argument registers.  */
1508   int nregs = 2;
1509
1510   /* Figure out the size of the object to be passed.  */
1511   if (mode == BLKmode)
1512     size = int_size_in_bytes (type);
1513   else
1514     size = GET_MODE_SIZE (mode);
1515
1516   /* Figure out the alignment of the object to be passed.  */
1517   align = size;
1518
1519   cum->nbytes = (cum->nbytes + 3) & ~3;
1520
1521   /* Don't pass this arg via a register if all the argument registers
1522      are used up.  */
1523   if (cum->nbytes > nregs * UNITS_PER_WORD)
1524     return 0;
1525
1526   /* Don't pass this arg via a register if it would be split between
1527      registers and memory.  */
1528   if (type == NULL_TREE
1529       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1530     return 0;
1531
1532   switch (cum->nbytes / UNITS_PER_WORD)
1533     {
1534     case 0:
1535       result = gen_rtx_REG (mode, 0);
1536       break;
1537     case 1:
1538       result = gen_rtx_REG (mode, 1);
1539       break;
1540     default:
1541       result = 0;
1542     }
1543
1544   return result;
1545 }
1546
1547 /* Return the number of bytes of registers to use for an argument passed
1548    partially in registers and partially in memory.  */
1549
1550 static int
1551 mn10300_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1552                            tree type, bool named ATTRIBUTE_UNUSED)
1553 {
1554   int size, align;
1555
1556   /* We only support using 2 data registers as argument registers.  */
1557   int nregs = 2;
1558
1559   /* Figure out the size of the object to be passed.  */
1560   if (mode == BLKmode)
1561     size = int_size_in_bytes (type);
1562   else
1563     size = GET_MODE_SIZE (mode);
1564
1565   /* Figure out the alignment of the object to be passed.  */
1566   align = size;
1567
1568   cum->nbytes = (cum->nbytes + 3) & ~3;
1569
1570   /* Don't pass this arg via a register if all the argument registers
1571      are used up.  */
1572   if (cum->nbytes > nregs * UNITS_PER_WORD)
1573     return 0;
1574
1575   if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1576     return 0;
1577
1578   /* Don't pass this arg via a register if it would be split between
1579      registers and memory.  */
1580   if (type == NULL_TREE
1581       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1582     return 0;
1583
1584   return nregs * UNITS_PER_WORD - cum->nbytes;
1585 }
1586
1587 /* Return the location of the function's value.  This will be either
1588    $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1589    $d0 and $a0 if the -mreturn-pointer-on-do flag is set.  Note that
1590    we only return the PARALLEL for outgoing values; we do not want
1591    callers relying on this extra copy.  */
1592
1593 rtx
1594 mn10300_function_value (const_tree valtype, const_tree func, int outgoing)
1595 {
1596   rtx rv;
1597   enum machine_mode mode = TYPE_MODE (valtype);
1598
1599   if (! POINTER_TYPE_P (valtype))
1600     return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1601   else if (! TARGET_PTR_A0D0 || ! outgoing
1602            || current_function_returns_struct)
1603     return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1604
1605   rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1606   XVECEXP (rv, 0, 0)
1607     = gen_rtx_EXPR_LIST (VOIDmode,
1608                          gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1609                          GEN_INT (0));
1610
1611   XVECEXP (rv, 0, 1)
1612     = gen_rtx_EXPR_LIST (VOIDmode,
1613                          gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1614                          GEN_INT (0));
1615   return rv;
1616 }
1617
1618 /* Output a tst insn.  */
1619 const char *
1620 output_tst (rtx operand, rtx insn)
1621 {
1622   rtx temp;
1623   int past_call = 0;
1624
1625   /* We can save a byte if we can find a register which has the value
1626      zero in it.  */
1627   temp = PREV_INSN (insn);
1628   while (optimize && temp)
1629     {
1630       rtx set;
1631
1632       /* We allow the search to go through call insns.  We record
1633          the fact that we've past a CALL_INSN and reject matches which
1634          use call clobbered registers.  */
1635       if (GET_CODE (temp) == CODE_LABEL
1636           || GET_CODE (temp) == JUMP_INSN
1637           || GET_CODE (temp) == BARRIER)
1638         break;
1639
1640       if (GET_CODE (temp) == CALL_INSN)
1641         past_call = 1;
1642
1643       if (GET_CODE (temp) == NOTE)
1644         {
1645           temp = PREV_INSN (temp);
1646           continue;
1647         }
1648
1649       /* It must be an insn, see if it is a simple set.  */
1650       set = single_set (temp);
1651       if (!set)
1652         {
1653           temp = PREV_INSN (temp);
1654           continue;
1655         }
1656
1657       /* Are we setting a data register to zero (this does not win for
1658          address registers)?
1659
1660          If it's a call clobbered register, have we past a call?
1661
1662          Make sure the register we find isn't the same as ourself;
1663          the mn10300 can't encode that.
1664
1665          ??? reg_set_between_p return nonzero anytime we pass a CALL_INSN
1666          so the code to detect calls here isn't doing anything useful.  */
1667       if (REG_P (SET_DEST (set))
1668           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1669           && !reg_set_between_p (SET_DEST (set), temp, insn)
1670           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1671               == REGNO_REG_CLASS (REGNO (operand)))
1672           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) != EXTENDED_REGS
1673           && REGNO (SET_DEST (set)) != REGNO (operand)
1674           && (!past_call
1675               || !call_used_regs[REGNO (SET_DEST (set))]))
1676         {
1677           rtx xoperands[2];
1678           xoperands[0] = operand;
1679           xoperands[1] = SET_DEST (set);
1680
1681           output_asm_insn ("cmp %1,%0", xoperands);
1682           return "";
1683         }
1684
1685       if (REGNO_REG_CLASS (REGNO (operand)) == EXTENDED_REGS
1686           && REG_P (SET_DEST (set))
1687           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1688           && !reg_set_between_p (SET_DEST (set), temp, insn)
1689           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1690               != REGNO_REG_CLASS (REGNO (operand)))
1691           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) == EXTENDED_REGS
1692           && REGNO (SET_DEST (set)) != REGNO (operand)
1693           && (!past_call
1694               || !call_used_regs[REGNO (SET_DEST (set))]))
1695         {
1696           rtx xoperands[2];
1697           xoperands[0] = operand;
1698           xoperands[1] = SET_DEST (set);
1699
1700           output_asm_insn ("cmp %1,%0", xoperands);
1701           return "";
1702         }
1703       temp = PREV_INSN (temp);
1704     }
1705   return "cmp 0,%0";
1706 }
1707
1708 int
1709 impossible_plus_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1710 {
1711   if (GET_CODE (op) != PLUS)
1712     return 0;
1713
1714   if (XEXP (op, 0) == stack_pointer_rtx
1715       || XEXP (op, 1) == stack_pointer_rtx)
1716     return 1;
1717
1718   return 0;
1719 }
1720
1721 /* Similarly, but when using a zero_extract pattern for a btst where
1722    the source operand might end up in memory.  */
1723 int
1724 mask_ok_for_mem_btst (int len, int bit)
1725 {
1726   unsigned int mask = 0;
1727
1728   while (len > 0)
1729     {
1730       mask |= (1 << bit);
1731       bit++;
1732       len--;
1733     }
1734
1735   /* MASK must bit into an 8bit value.  */
1736   return (((mask & 0xff) == mask)
1737           || ((mask & 0xff00) == mask)
1738           || ((mask & 0xff0000) == mask)
1739           || ((mask & 0xff000000) == mask));
1740 }
1741
1742 /* Return 1 if X contains a symbolic expression.  We know these
1743    expressions will have one of a few well defined forms, so
1744    we need only check those forms.  */
1745 int
1746 symbolic_operand (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1747 {
1748   switch (GET_CODE (op))
1749     {
1750     case SYMBOL_REF:
1751     case LABEL_REF:
1752       return 1;
1753     case CONST:
1754       op = XEXP (op, 0);
1755       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1756                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1757               && GET_CODE (XEXP (op, 1)) == CONST_INT);
1758     default:
1759       return 0;
1760     }
1761 }
1762
1763 /* Try machine dependent ways of modifying an illegitimate address
1764    to be legitimate.  If we find one, return the new valid address.
1765    This macro is used in only one place: `memory_address' in explow.c.
1766
1767    OLDX is the address as it was before break_out_memory_refs was called.
1768    In some cases it is useful to look at this to decide what needs to be done.
1769
1770    MODE and WIN are passed so that this macro can use
1771    GO_IF_LEGITIMATE_ADDRESS.
1772
1773    Normally it is always safe for this macro to do nothing.  It exists to
1774    recognize opportunities to optimize the output.
1775
1776    But on a few ports with segmented architectures and indexed addressing
1777    (mn10300, hppa) it is used to rewrite certain problematical addresses.  */
1778 rtx
1779 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1780                     enum machine_mode mode ATTRIBUTE_UNUSED)
1781 {
1782   if (flag_pic && ! legitimate_pic_operand_p (x))
1783     x = legitimize_pic_address (oldx, NULL_RTX);
1784
1785   /* Uh-oh.  We might have an address for x[n-100000].  This needs
1786      special handling to avoid creating an indexed memory address
1787      with x-100000 as the base.  */
1788   if (GET_CODE (x) == PLUS
1789       && symbolic_operand (XEXP (x, 1), VOIDmode))
1790     {
1791       /* Ugly.  We modify things here so that the address offset specified
1792          by the index expression is computed first, then added to x to form
1793          the entire address.  */
1794
1795       rtx regx1, regy1, regy2, y;
1796
1797       /* Strip off any CONST.  */
1798       y = XEXP (x, 1);
1799       if (GET_CODE (y) == CONST)
1800         y = XEXP (y, 0);
1801
1802       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1803         {
1804           regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1805           regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1806           regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1807           regx1 = force_reg (Pmode,
1808                              gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1, regy2));
1809           return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1810         }
1811     }
1812   return x;
1813 }
1814
1815 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
1816    @GOTOFF in `reg'.  */
1817 rtx
1818 legitimize_pic_address (rtx orig, rtx reg)
1819 {
1820   if (GET_CODE (orig) == LABEL_REF
1821       || (GET_CODE (orig) == SYMBOL_REF
1822           && (CONSTANT_POOL_ADDRESS_P (orig)
1823               || ! MN10300_GLOBAL_P (orig))))
1824     {
1825       if (reg == 0)
1826         reg = gen_reg_rtx (Pmode);
1827
1828       emit_insn (gen_symGOTOFF2reg (reg, orig));
1829       return reg;
1830     }
1831   else if (GET_CODE (orig) == SYMBOL_REF)
1832     {
1833       if (reg == 0)
1834         reg = gen_reg_rtx (Pmode);
1835
1836       emit_insn (gen_symGOT2reg (reg, orig));
1837       return reg;
1838     }
1839   return orig;
1840 }
1841
1842 /* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
1843    isn't protected by a PIC unspec; nonzero otherwise.  */
1844 int
1845 legitimate_pic_operand_p (rtx x)
1846 {
1847   register const char *fmt;
1848   register int i;
1849
1850   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1851     return 0;
1852
1853   if (GET_CODE (x) == UNSPEC
1854       && (XINT (x, 1) == UNSPEC_PIC
1855           || XINT (x, 1) == UNSPEC_GOT
1856           || XINT (x, 1) == UNSPEC_GOTOFF
1857           || XINT (x, 1) == UNSPEC_PLT))
1858       return 1;
1859
1860   fmt = GET_RTX_FORMAT (GET_CODE (x));
1861   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1862     {
1863       if (fmt[i] == 'E')
1864         {
1865           register int j;
1866
1867           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1868             if (! legitimate_pic_operand_p (XVECEXP (x, i, j)))
1869               return 0;
1870         }
1871       else if (fmt[i] == 'e' && ! legitimate_pic_operand_p (XEXP (x, i)))
1872         return 0;
1873     }
1874
1875   return 1;
1876 }
1877
1878 /* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1879    legitimate, and FALSE otherwise.  */
1880 bool
1881 legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1882 {
1883   if (CONSTANT_ADDRESS_P (x)
1884       && (! flag_pic || legitimate_pic_operand_p (x)))
1885     return TRUE;
1886
1887   if (RTX_OK_FOR_BASE_P (x, strict))
1888     return TRUE;
1889
1890   if (TARGET_AM33
1891       && GET_CODE (x) == POST_INC
1892       && RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1893       && (mode == SImode || mode == SFmode || mode == HImode))
1894     return TRUE;
1895
1896   if (GET_CODE (x) == PLUS)
1897     {
1898       rtx base = 0, index = 0;
1899
1900       if (REG_P (XEXP (x, 0))
1901           && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 0)), strict))
1902         {
1903           base = XEXP (x, 0);
1904           index = XEXP (x, 1);
1905         }
1906
1907       if (REG_P (XEXP (x, 1))
1908           && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 1)), strict))
1909         {
1910           base = XEXP (x, 1);
1911           index = XEXP (x, 0);
1912         }
1913
1914       if (base != 0 && index != 0)
1915         {
1916           if (GET_CODE (index) == CONST_INT)
1917             return TRUE;
1918           if (GET_CODE (index) == CONST
1919               && GET_CODE (XEXP (index, 0)) != PLUS
1920               && (! flag_pic
1921                   || legitimate_pic_operand_p (index)))
1922             return TRUE;
1923         }
1924     }
1925
1926   return FALSE;
1927 }
1928
1929 static int
1930 mn10300_address_cost_1 (rtx x, int *unsig)
1931 {
1932   switch (GET_CODE (x))
1933     {
1934     case REG:
1935       switch (REGNO_REG_CLASS (REGNO (x)))
1936         {
1937         case SP_REGS:
1938           *unsig = 1;
1939           return 0;
1940
1941         case ADDRESS_REGS:
1942           return 1;
1943
1944         case DATA_REGS:
1945         case EXTENDED_REGS:
1946         case FP_REGS:
1947           return 3;
1948
1949         case NO_REGS:
1950           return 5;
1951
1952         default:
1953           gcc_unreachable ();
1954         }
1955
1956     case PLUS:
1957     case MINUS:
1958     case ASHIFT:
1959     case AND:
1960     case IOR:
1961       return (mn10300_address_cost_1 (XEXP (x, 0), unsig)
1962               + mn10300_address_cost_1 (XEXP (x, 1), unsig));
1963
1964     case EXPR_LIST:
1965     case SUBREG:
1966     case MEM:
1967       return mn10300_address_cost (XEXP (x, 0));
1968
1969     case ZERO_EXTEND:
1970       *unsig = 1;
1971       return mn10300_address_cost_1 (XEXP (x, 0), unsig);
1972
1973     case CONST_INT:
1974       if (INTVAL (x) == 0)
1975         return 0;
1976       if (INTVAL (x) + (*unsig ? 0 : 0x80) < 0x100)
1977         return 1;
1978       if (INTVAL (x) + (*unsig ? 0 : 0x8000) < 0x10000)
1979         return 3;
1980       if (INTVAL (x) + (*unsig ? 0 : 0x800000) < 0x1000000)
1981         return 5;
1982       return 7;
1983
1984     case CONST:
1985     case SYMBOL_REF:
1986     case LABEL_REF:
1987       return 8;
1988
1989     default:
1990       gcc_unreachable ();
1991
1992     }
1993 }
1994
1995 static int
1996 mn10300_address_cost (rtx x)
1997 {
1998   int s = 0;
1999   return mn10300_address_cost_1 (x, &s);
2000 }
2001
2002 static bool
2003 mn10300_rtx_costs (rtx x, int code, int outer_code, int *total)
2004 {
2005   switch (code)
2006     {
2007     case CONST_INT:
2008       /* Zeros are extremely cheap.  */
2009       if (INTVAL (x) == 0 && outer_code == SET)
2010         *total = 0;
2011       /* If it fits in 8 bits, then it's still relatively cheap.  */
2012       else if (INT_8_BITS (INTVAL (x)))
2013         *total = 1;
2014       /* This is the "base" cost, includes constants where either the
2015          upper or lower 16bits are all zeros.  */
2016       else if (INT_16_BITS (INTVAL (x))
2017                || (INTVAL (x) & 0xffff) == 0
2018                || (INTVAL (x) & 0xffff0000) == 0)
2019         *total = 2;
2020       else
2021         *total = 4;
2022       return true;
2023
2024     case CONST:
2025     case LABEL_REF:
2026     case SYMBOL_REF:
2027       /* These are more costly than a CONST_INT, but we can relax them,
2028          so they're less costly than a CONST_DOUBLE.  */
2029       *total = 6;
2030       return true;
2031
2032     case CONST_DOUBLE:
2033       /* We don't optimize CONST_DOUBLEs well nor do we relax them well,
2034          so their cost is very high.  */
2035       *total = 8;
2036       return true;
2037
2038    /* ??? This probably needs more work.  */
2039     case MOD:
2040     case DIV:
2041     case MULT:
2042       *total = 8;
2043       return true;
2044
2045     default:
2046       return false;
2047     }
2048 }
2049
2050 /* Check whether a constant used to initialize a DImode or DFmode can
2051    use a clr instruction.  The code here must be kept in sync with
2052    movdf and movdi.  */
2053
2054 bool
2055 mn10300_wide_const_load_uses_clr (rtx operands[2])
2056 {
2057   long val[2];
2058
2059   if (GET_CODE (operands[0]) != REG
2060       || REGNO_REG_CLASS (REGNO (operands[0])) != DATA_REGS)
2061     return false;
2062
2063   switch (GET_CODE (operands[1]))
2064     {
2065     case CONST_INT:
2066       {
2067         rtx low, high;
2068         split_double (operands[1], &low, &high);
2069         val[0] = INTVAL (low);
2070         val[1] = INTVAL (high);
2071       }
2072       break;
2073
2074     case CONST_DOUBLE:
2075       if (GET_MODE (operands[1]) == DFmode)
2076         {
2077           REAL_VALUE_TYPE rv;
2078
2079           REAL_VALUE_FROM_CONST_DOUBLE (rv, operands[1]);
2080           REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
2081         }
2082       else if (GET_MODE (operands[1]) == VOIDmode
2083                || GET_MODE (operands[1]) == DImode)
2084         {
2085           val[0] = CONST_DOUBLE_LOW (operands[1]);
2086           val[1] = CONST_DOUBLE_HIGH (operands[1]);
2087         }
2088       break;
2089
2090     default:
2091       return false;
2092     }
2093
2094   return val[0] == 0 || val[1] == 0;
2095 }
2096 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2097    may access it using GOTOFF instead of GOT.  */
2098
2099 static void
2100 mn10300_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
2101 {
2102   rtx symbol;
2103
2104   if (GET_CODE (rtl) != MEM)
2105     return;
2106   symbol = XEXP (rtl, 0);
2107   if (GET_CODE (symbol) != SYMBOL_REF)
2108     return;
2109
2110   if (flag_pic)
2111     SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2112 }