OSDN Git Service

* Makefile.in (BUILD_RTL): Replace $(BUILD_PREFIX)insn-modes.o
[pf3gnuchains/gcc-fork.git] / gcc / config / m32r / m32r.c
1 /* Subroutines used for code generation on the Mitsubishi M32R cpu.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
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 2, 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 COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.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 "expr.h"
37 #include "function.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "ggc.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44
45 /* Save the operands last given to a compare for use when we
46    generate a scc or bcc insn.  */
47 rtx m32r_compare_op0, m32r_compare_op1;
48
49 /* Array of valid operand punctuation characters.  */
50 char m32r_punct_chars[256];
51
52 /* Selected code model.  */
53 const char * m32r_model_string = M32R_MODEL_DEFAULT;
54 enum m32r_model m32r_model;
55
56 /* Selected SDA support.  */
57 const char * m32r_sdata_string = M32R_SDATA_DEFAULT;
58 enum m32r_sdata m32r_sdata;
59
60 /* Scheduler support */
61 static int m32r_sched_odd_word_p;
62
63 /* Machine-specific symbol_ref flags.  */
64 #define SYMBOL_FLAG_MODEL_SHIFT         SYMBOL_FLAG_MACH_DEP_SHIFT
65 #define SYMBOL_REF_MODEL(X) \
66   ((enum m32r_model) ((SYMBOL_REF_FLAGS (X) >> SYMBOL_FLAG_MODEL_SHIFT) & 3))
67
68 /* For string literals, etc.  */
69 #define LIT_NAME_P(NAME) ((NAME)[0] == '*' && (NAME)[1] == '.')
70
71 /* Forward declaration.  */
72 static void  init_reg_tables                    PARAMS ((void));
73 static void  block_move_call                    PARAMS ((rtx, rtx, rtx));
74 static int   m32r_is_insn                       PARAMS ((rtx));
75 const struct attribute_spec m32r_attribute_table[];
76 static tree  m32r_handle_model_attribute PARAMS ((tree *, tree, tree, int, bool *));
77 static void  m32r_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
78 static void  m32r_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
79
80 static void  m32r_file_start PARAMS ((void));
81
82 static int    m32r_adjust_cost     PARAMS ((rtx, rtx, rtx, int));
83 static int    m32r_adjust_priority PARAMS ((rtx, int));
84 static void   m32r_sched_init      PARAMS ((FILE *, int, int));
85 static int    m32r_sched_reorder   PARAMS ((FILE *, int, rtx *, int *, int));
86 static int    m32r_variable_issue  PARAMS ((FILE *, int, rtx, int));
87 static int    m32r_issue_rate      PARAMS ((void));
88
89 static void m32r_encode_section_info PARAMS ((tree, rtx, int));
90 static bool m32r_in_small_data_p PARAMS ((tree));
91 static void init_idents PARAMS ((void));
92 static bool m32r_rtx_costs PARAMS ((rtx, int, int, int *));
93 \f
94 /* Initialize the GCC target structure.  */
95 #undef TARGET_ATTRIBUTE_TABLE
96 #define TARGET_ATTRIBUTE_TABLE m32r_attribute_table
97
98 #undef TARGET_ASM_ALIGNED_HI_OP
99 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
100 #undef TARGET_ASM_ALIGNED_SI_OP
101 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
102
103 #undef TARGET_ASM_FUNCTION_PROLOGUE
104 #define TARGET_ASM_FUNCTION_PROLOGUE m32r_output_function_prologue
105 #undef TARGET_ASM_FUNCTION_EPILOGUE
106 #define TARGET_ASM_FUNCTION_EPILOGUE m32r_output_function_epilogue
107
108 #undef TARGET_ASM_FILE_START
109 #define TARGET_ASM_FILE_START m32r_file_start
110
111 #undef TARGET_SCHED_ADJUST_COST
112 #define TARGET_SCHED_ADJUST_COST m32r_adjust_cost
113 #undef TARGET_SCHED_ADJUST_PRIORITY
114 #define TARGET_SCHED_ADJUST_PRIORITY m32r_adjust_priority
115 #undef TARGET_SCHED_ISSUE_RATE
116 #define TARGET_SCHED_ISSUE_RATE m32r_issue_rate
117 #undef TARGET_SCHED_VARIABLE_ISSUE
118 #define TARGET_SCHED_VARIABLE_ISSUE m32r_variable_issue
119 #undef TARGET_SCHED_INIT
120 #define TARGET_SCHED_INIT m32r_sched_init
121 #undef TARGET_SCHED_REORDER
122 #define TARGET_SCHED_REORDER m32r_sched_reorder
123
124 #undef TARGET_ENCODE_SECTION_INFO
125 #define TARGET_ENCODE_SECTION_INFO m32r_encode_section_info
126 #undef TARGET_IN_SMALL_DATA_P
127 #define TARGET_IN_SMALL_DATA_P m32r_in_small_data_p
128
129 #undef TARGET_RTX_COSTS
130 #define TARGET_RTX_COSTS m32r_rtx_costs
131 #undef TARGET_ADDRESS_COST
132 #define TARGET_ADDRESS_COST hook_int_rtx_0
133
134 struct gcc_target targetm = TARGET_INITIALIZER;
135 \f
136 /* Called by OVERRIDE_OPTIONS to initialize various things.  */
137
138 void
139 m32r_init ()
140 {
141   init_reg_tables ();
142
143   /* Initialize array for PRINT_OPERAND_PUNCT_VALID_P.  */
144   memset (m32r_punct_chars, 0, sizeof (m32r_punct_chars));
145   m32r_punct_chars['#'] = 1;
146   m32r_punct_chars['@'] = 1; /* ??? no longer used */
147
148   /* Provide default value if not specified.  */
149   if (!g_switch_set)
150     g_switch_value = SDATA_DEFAULT_SIZE;
151
152   if (strcmp (m32r_model_string, "small") == 0)
153     m32r_model = M32R_MODEL_SMALL;
154   else if (strcmp (m32r_model_string, "medium") == 0)
155     m32r_model = M32R_MODEL_MEDIUM;
156   else if (strcmp (m32r_model_string, "large") == 0)
157     m32r_model = M32R_MODEL_LARGE;
158   else
159     error ("bad value (%s) for -mmodel switch", m32r_model_string);
160
161   if (strcmp (m32r_sdata_string, "none") == 0)
162     m32r_sdata = M32R_SDATA_NONE;
163   else if (strcmp (m32r_sdata_string, "sdata") == 0)
164     m32r_sdata = M32R_SDATA_SDATA;
165   else if (strcmp (m32r_sdata_string, "use") == 0)
166     m32r_sdata = M32R_SDATA_USE;
167   else
168     error ("bad value (%s) for -msdata switch", m32r_sdata_string);
169 }
170
171 /* Vectors to keep interesting information about registers where it can easily
172    be got.  We use to use the actual mode value as the bit number, but there
173    is (or may be) more than 32 modes now.  Instead we use two tables: one
174    indexed by hard register number, and one indexed by mode.  */
175
176 /* The purpose of m32r_mode_class is to shrink the range of modes so that
177    they all fit (as bit numbers) in a 32 bit word (again).  Each real mode is
178    mapped into one m32r_mode_class mode.  */
179
180 enum m32r_mode_class
181 {
182   C_MODE,
183   S_MODE, D_MODE, T_MODE, O_MODE,
184   SF_MODE, DF_MODE, TF_MODE, OF_MODE, A_MODE
185 };
186
187 /* Modes for condition codes.  */
188 #define C_MODES (1 << (int) C_MODE)
189
190 /* Modes for single-word and smaller quantities.  */
191 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
192
193 /* Modes for double-word and smaller quantities.  */
194 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
195
196 /* Modes for quad-word and smaller quantities.  */
197 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
198
199 /* Modes for accumulators.  */
200 #define A_MODES (1 << (int) A_MODE)
201
202 /* Value is 1 if register/mode pair is acceptable on arc.  */
203
204 const unsigned int m32r_hard_regno_mode_ok[FIRST_PSEUDO_REGISTER] =
205 {
206   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
207   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, S_MODES, S_MODES, S_MODES,
208   S_MODES, C_MODES, A_MODES, A_MODES
209 };
210
211 unsigned int m32r_mode_class [NUM_MACHINE_MODES];
212
213 enum reg_class m32r_regno_reg_class[FIRST_PSEUDO_REGISTER];
214
215 static void
216 init_reg_tables ()
217 {
218   int i;
219
220   for (i = 0; i < NUM_MACHINE_MODES; i++)
221     {
222       switch (GET_MODE_CLASS (i))
223         {
224         case MODE_INT:
225         case MODE_PARTIAL_INT:
226         case MODE_COMPLEX_INT:
227           if (GET_MODE_SIZE (i) <= 4)
228             m32r_mode_class[i] = 1 << (int) S_MODE;
229           else if (GET_MODE_SIZE (i) == 8)
230             m32r_mode_class[i] = 1 << (int) D_MODE;
231           else if (GET_MODE_SIZE (i) == 16)
232             m32r_mode_class[i] = 1 << (int) T_MODE;
233           else if (GET_MODE_SIZE (i) == 32)
234             m32r_mode_class[i] = 1 << (int) O_MODE;
235           else 
236             m32r_mode_class[i] = 0;
237           break;
238         case MODE_FLOAT:
239         case MODE_COMPLEX_FLOAT:
240           if (GET_MODE_SIZE (i) <= 4)
241             m32r_mode_class[i] = 1 << (int) SF_MODE;
242           else if (GET_MODE_SIZE (i) == 8)
243             m32r_mode_class[i] = 1 << (int) DF_MODE;
244           else if (GET_MODE_SIZE (i) == 16)
245             m32r_mode_class[i] = 1 << (int) TF_MODE;
246           else if (GET_MODE_SIZE (i) == 32)
247             m32r_mode_class[i] = 1 << (int) OF_MODE;
248           else 
249             m32r_mode_class[i] = 0;
250           break;
251         case MODE_CC:
252           m32r_mode_class[i] = 1 << (int) C_MODE;
253           break;
254         default:
255           m32r_mode_class[i] = 0;
256           break;
257         }
258     }
259
260   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
261     {
262       if (GPR_P (i))
263         m32r_regno_reg_class[i] = GENERAL_REGS;
264       else if (i == ARG_POINTER_REGNUM)
265         m32r_regno_reg_class[i] = GENERAL_REGS;
266       else
267         m32r_regno_reg_class[i] = NO_REGS;
268     }
269 }
270 \f
271 /* M32R specific attribute support.
272
273    interrupt - for interrupt functions
274
275    model - select code model used to access object
276
277         small: addresses use 24 bits, use bl to make calls
278         medium: addresses use 32 bits, use bl to make calls
279         large: addresses use 32 bits, use seth/add3/jl to make calls
280
281         Grep for MODEL in m32r.h for more info.
282 */
283
284 static tree small_ident1;
285 static tree small_ident2;
286 static tree medium_ident1;
287 static tree medium_ident2;
288 static tree large_ident1;
289 static tree large_ident2;
290
291 static void
292 init_idents ()
293 {
294   if (small_ident1 == 0)
295     {
296       small_ident1 = get_identifier ("small");
297       small_ident2 = get_identifier ("__small__");
298       medium_ident1 = get_identifier ("medium");
299       medium_ident2 = get_identifier ("__medium__");
300       large_ident1 = get_identifier ("large");
301       large_ident2 = get_identifier ("__large__");
302     }
303 }
304
305 const struct attribute_spec m32r_attribute_table[] =
306 {
307   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
308   { "interrupt", 0, 0, true,  false, false, NULL },
309   { "model",     1, 1, true,  false, false, m32r_handle_model_attribute },
310   { NULL,        0, 0, false, false, false, NULL }
311 };
312
313
314 /* Handle an "model" attribute; arguments as in
315    struct attribute_spec.handler.  */
316 static tree
317 m32r_handle_model_attribute (node, name, args, flags, no_add_attrs)
318      tree *node ATTRIBUTE_UNUSED;
319      tree name;
320      tree args;
321      int flags ATTRIBUTE_UNUSED;
322      bool *no_add_attrs;
323 {
324   tree arg;
325
326   init_idents ();
327   arg = TREE_VALUE (args);
328
329   if (arg != small_ident1
330       && arg != small_ident2
331       && arg != medium_ident1
332       && arg != medium_ident2
333       && arg != large_ident1
334       && arg != large_ident2)
335     {
336       warning ("invalid argument of `%s' attribute",
337                IDENTIFIER_POINTER (name));
338       *no_add_attrs = true;
339     }
340
341   return NULL_TREE;
342 }
343 \f
344 /* Encode section information of DECL, which is either a VAR_DECL,
345    FUNCTION_DECL, STRING_CST, CONSTRUCTOR, or ???.
346
347    For the M32R we want to record:
348
349    - whether the object lives in .sdata/.sbss.
350    - what code model should be used to access the object
351 */
352
353 static void
354 m32r_encode_section_info (decl, rtl, first)
355      tree decl;
356      rtx rtl;
357      int first;
358 {
359   int extra_flags = 0;
360   tree model_attr;
361   enum m32r_model model;
362
363   default_encode_section_info (decl, rtl, first);
364
365   if (!DECL_P (decl))
366     return;
367
368   model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
369   if (model_attr)
370     {
371       tree id;
372
373       init_idents ();
374
375       id = TREE_VALUE (TREE_VALUE (model_attr));
376
377       if (id == small_ident1 || id == small_ident2)
378         model = M32R_MODEL_SMALL;
379       else if (id == medium_ident1 || id == medium_ident2)
380         model = M32R_MODEL_MEDIUM;
381       else if (id == large_ident1 || id == large_ident2)
382         model = M32R_MODEL_LARGE;
383       else
384         abort (); /* shouldn't happen */
385     }
386   else
387     {
388       if (TARGET_MODEL_SMALL)
389         model = M32R_MODEL_SMALL;
390       else if (TARGET_MODEL_MEDIUM)
391         model = M32R_MODEL_MEDIUM;
392       else if (TARGET_MODEL_LARGE)
393         model = M32R_MODEL_LARGE;
394       else
395         abort (); /* shouldn't happen */
396     }
397   extra_flags |= model << SYMBOL_FLAG_MODEL_SHIFT;
398
399   if (extra_flags)
400     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
401 }
402
403 /* Only mark the object as being small data area addressable if
404    it hasn't been explicitly marked with a code model.
405
406    The user can explicitly put an object in the small data area with the
407    section attribute.  If the object is in sdata/sbss and marked with a
408    code model do both [put the object in .sdata and mark it as being
409    addressed with a specific code model - don't mark it as being addressed
410    with an SDA reloc though].  This is ok and might be useful at times.  If
411    the object doesn't fit the linker will give an error.  */
412
413 static bool
414 m32r_in_small_data_p (decl)
415      tree decl;
416 {
417   tree section;
418
419   if (TREE_CODE (decl) != VAR_DECL)
420     return false;
421
422   if (lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
423     return false;
424
425   section = DECL_SECTION_NAME (decl);
426   if (section)
427     {
428       char *name = (char *) TREE_STRING_POINTER (section);
429       if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
430         return true;
431     }
432   else
433     {
434       if (! TREE_READONLY (decl) && ! TARGET_SDATA_NONE)
435         {
436           int size = int_size_in_bytes (TREE_TYPE (decl));
437
438           if (size > 0 && (unsigned HOST_WIDE_INT) size <= g_switch_value)
439             return true;
440         }
441     }
442
443   return false;
444 }
445
446 /* Do anything needed before RTL is emitted for each function.  */
447
448 void
449 m32r_init_expanders ()
450 {
451   /* ??? At one point there was code here.  The function is left in
452      to make it easy to experiment.  */
453 }
454 \f
455 /* Acceptable arguments to the call insn.  */
456
457 int
458 call_address_operand (op, mode)
459      rtx op;
460      enum machine_mode mode;
461 {
462   return symbolic_operand (op, mode);
463
464 /* Constants and values in registers are not OK, because
465    the m32r BL instruction can only support PC relative branching.  */ 
466 }
467
468 int
469 call_operand (op, mode)
470      rtx op;
471      enum machine_mode mode;
472 {
473   if (GET_CODE (op) != MEM)
474     return 0;
475   op = XEXP (op, 0);
476   return call_address_operand (op, mode);
477 }
478
479 /* Returns 1 if OP is a symbol reference.  */
480
481 int
482 symbolic_operand (op, mode)
483      rtx op;
484      enum machine_mode mode ATTRIBUTE_UNUSED;
485 {
486   switch (GET_CODE (op))
487     {
488     case SYMBOL_REF:
489     case LABEL_REF:
490     case CONST :
491       return 1;
492
493     default:
494       return 0;
495     }
496 }
497
498 /* Return 1 if OP is a reference to an object in .sdata/.sbss.  */
499
500 int
501 small_data_operand (op, mode)
502      rtx op;
503      enum machine_mode mode ATTRIBUTE_UNUSED;
504 {
505   if (! TARGET_SDATA_USE)
506     return 0;
507
508   if (GET_CODE (op) == SYMBOL_REF)
509     return SYMBOL_REF_SMALL_P (op);
510
511   if (GET_CODE (op) == CONST
512       && GET_CODE (XEXP (op, 0)) == PLUS
513       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
514       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
515       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
516     return SYMBOL_REF_SMALL_P (XEXP (XEXP (op, 0), 0));
517
518   return 0;
519 }
520
521 /* Return 1 if OP is a symbol that can use 24 bit addressing.  */
522
523 int
524 addr24_operand (op, mode)
525      rtx op;
526      enum machine_mode mode ATTRIBUTE_UNUSED;
527 {
528   rtx sym;
529
530   if (GET_CODE (op) == LABEL_REF)
531     return TARGET_ADDR24;
532
533   if (GET_CODE (op) == SYMBOL_REF)
534     sym = op;
535   else if (GET_CODE (op) == CONST
536            && GET_CODE (XEXP (op, 0)) == PLUS
537            && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
538            && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
539            && UINT24_P (INTVAL (XEXP (XEXP (op, 0), 1))))
540     sym = XEXP (XEXP (op, 0), 0);
541   else
542     return 0;
543
544   if (SYMBOL_REF_MODEL (sym) == M32R_MODEL_SMALL)
545     return 1;
546
547   if (TARGET_ADDR24
548       && (CONSTANT_POOL_ADDRESS_P (sym)
549           || LIT_NAME_P (XSTR (sym, 0))))
550     return 1;
551
552   return 0;
553 }
554
555 /* Return 1 if OP is a symbol that needs 32 bit addressing.  */
556
557 int
558 addr32_operand (op, mode)
559      rtx op;
560      enum machine_mode mode;
561 {
562   rtx sym;
563
564   if (GET_CODE (op) == LABEL_REF)
565     return TARGET_ADDR32;
566
567   if (GET_CODE (op) == SYMBOL_REF)
568     sym = op;
569   else if (GET_CODE (op) == CONST
570            && GET_CODE (XEXP (op, 0)) == PLUS
571            && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
572            && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
573     sym = XEXP (XEXP (op, 0), 0);
574   else
575     return 0;
576
577   return (! addr24_operand (sym, mode)
578           && ! small_data_operand (sym, mode));
579 }
580
581 /* Return 1 if OP is a function that can be called with the `bl' insn.  */
582
583 int
584 call26_operand (op, mode)
585      rtx op;
586      enum machine_mode mode ATTRIBUTE_UNUSED;
587 {
588   if (GET_CODE (op) == SYMBOL_REF)
589     return SYMBOL_REF_MODEL (op) != M32R_MODEL_LARGE;
590
591   return TARGET_CALL26;
592 }
593
594 /* Returns 1 if OP is an acceptable operand for seth/add3.  */
595
596 int
597 seth_add3_operand (op, mode)
598      rtx op;
599      enum machine_mode mode ATTRIBUTE_UNUSED;
600 {
601   if (GET_CODE (op) == SYMBOL_REF
602       || GET_CODE (op) == LABEL_REF)
603     return 1;
604
605   if (GET_CODE (op) == CONST
606       && GET_CODE (XEXP (op, 0)) == PLUS
607       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
608       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
609       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
610     return 1;
611
612   return 0;
613 }
614
615 /* Return true if OP is a signed 8 bit immediate value.  */
616
617 int
618 int8_operand (op, mode)
619      rtx op;
620      enum machine_mode mode ATTRIBUTE_UNUSED;
621 {
622   if (GET_CODE (op) != CONST_INT)
623     return 0;
624   return INT8_P (INTVAL (op));
625 }
626
627 /* Return true if OP is a signed 16 bit immediate value
628    useful in comparisons.  */
629
630 int
631 cmp_int16_operand (op, mode)
632      rtx op;
633      enum machine_mode mode ATTRIBUTE_UNUSED;
634 {
635   if (GET_CODE (op) != CONST_INT)
636     return 0;
637   return CMP_INT16_P (INTVAL (op));
638 }
639
640 /* Return true if OP is an unsigned 16 bit immediate value.  */
641
642 int
643 uint16_operand (op, mode)
644      rtx op;
645      enum machine_mode mode ATTRIBUTE_UNUSED;
646 {
647   if (GET_CODE (op) != CONST_INT)
648     return 0;
649   return UINT16_P (INTVAL (op));
650 }
651
652 /* Return true if OP is a register or signed 16 bit value.  */
653
654 int
655 reg_or_int16_operand (op, mode)
656      rtx op;
657      enum machine_mode mode;
658 {
659   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
660     return register_operand (op, mode);
661   if (GET_CODE (op) != CONST_INT)
662     return 0;
663   return INT16_P (INTVAL (op));
664 }
665
666 /* Return true if OP is a register or an unsigned 16 bit value.  */
667
668 int
669 reg_or_uint16_operand (op, mode)
670      rtx op;
671      enum machine_mode mode;
672 {
673   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
674     return register_operand (op, mode);
675   if (GET_CODE (op) != CONST_INT)
676     return 0;
677   return UINT16_P (INTVAL (op));
678 }
679
680 /* Return true if OP is a register or an integer value that can be
681    used is SEQ/SNE.  We can use either XOR of the value or ADD of
682    the negative of the value for the constant.  Don't allow 0,
683    because that is special cased.  */
684
685 int
686 reg_or_eq_int16_operand (op, mode)
687      rtx op;
688      enum machine_mode mode;
689 {
690   HOST_WIDE_INT value;
691
692   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
693     return register_operand (op, mode);
694
695   if (GET_CODE (op) != CONST_INT)
696     return 0;
697
698   value = INTVAL (op);
699   return (value != 0) && (UINT16_P (value) || CMP_INT16_P (-value));
700 }
701
702 /* Return true if OP is a register or signed 16 bit value for compares.  */
703
704 int
705 reg_or_cmp_int16_operand (op, mode)
706      rtx op;
707      enum machine_mode mode;
708 {
709   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
710     return register_operand (op, mode);
711   if (GET_CODE (op) != CONST_INT)
712     return 0;
713   return CMP_INT16_P (INTVAL (op));
714 }
715
716 /* Return true if OP is a register or the constant 0.  */
717
718 int
719 reg_or_zero_operand (op, mode)
720      rtx op;
721      enum machine_mode mode;
722 {
723   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
724     return register_operand (op, mode);
725
726   if (GET_CODE (op) != CONST_INT)
727     return 0;
728
729   return INTVAL (op) == 0;
730 }
731
732 /* Return true if OP is a const_int requiring two instructions to load.  */
733
734 int
735 two_insn_const_operand (op, mode)
736      rtx op;
737      enum machine_mode mode ATTRIBUTE_UNUSED;
738 {
739   if (GET_CODE (op) != CONST_INT)
740     return 0;
741   if (INT16_P (INTVAL (op))
742       || UINT24_P (INTVAL (op))
743       || UPPER16_P (INTVAL (op)))
744     return 0;
745   return 1;
746 }
747
748 /* Return true if OP is an acceptable argument for a single word
749    move source.  */
750
751 int
752 move_src_operand (op, mode)
753      rtx op;
754      enum machine_mode mode;
755 {
756   switch (GET_CODE (op))
757     {
758     case SYMBOL_REF :
759     case CONST :
760       return addr24_operand (op, mode);
761     case CONST_INT :
762       /* ??? We allow more cse opportunities if we only allow constants
763          loadable with one insn, and split the rest into two.  The instances
764          where this would help should be rare and the current way is
765          simpler.  */
766       if (HOST_BITS_PER_WIDE_INT > 32)
767         {
768           HOST_WIDE_INT rest = INTVAL (op) >> 31;
769           return (rest == 0 || rest == -1);
770         }
771       else
772         return 1;
773     case LABEL_REF :
774       return TARGET_ADDR24;
775     case CONST_DOUBLE :
776       if (mode == SFmode)
777         return 1;
778       else if (mode == SImode)
779         {
780           /* Large unsigned constants are represented as const_double's.  */
781           unsigned HOST_WIDE_INT low, high;
782
783           low = CONST_DOUBLE_LOW (op);
784           high = CONST_DOUBLE_HIGH (op);
785           return high == 0 && low <= (unsigned) 0xffffffff;
786         }
787       else
788         return 0;
789     case REG :
790       return register_operand (op, mode);
791     case SUBREG :
792       /* (subreg (mem ...) ...) can occur here if the inner part was once a
793          pseudo-reg and is now a stack slot.  */
794       if (GET_CODE (SUBREG_REG (op)) == MEM)
795         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
796       else
797         return register_operand (op, mode);
798     case MEM :
799       if (GET_CODE (XEXP (op, 0)) == PRE_INC
800           || GET_CODE (XEXP (op, 0)) == PRE_DEC)
801         return 0;               /* loads can't do pre-{inc,dec} */
802       return address_operand (XEXP (op, 0), mode);
803     default :
804       return 0;
805     }
806 }
807
808 /* Return true if OP is an acceptable argument for a double word
809    move source.  */
810
811 int
812 move_double_src_operand (op, mode)
813      rtx op;
814      enum machine_mode mode;
815 {
816   switch (GET_CODE (op))
817     {
818     case CONST_INT :
819     case CONST_DOUBLE :
820       return 1;
821     case REG :
822       return register_operand (op, mode);
823     case SUBREG :
824       /* (subreg (mem ...) ...) can occur here if the inner part was once a
825          pseudo-reg and is now a stack slot.  */
826       if (GET_CODE (SUBREG_REG (op)) == MEM)
827         return move_double_src_operand (SUBREG_REG (op), mode);
828       else
829         return register_operand (op, mode);
830     case MEM :
831       /* Disallow auto inc/dec for now.  */
832       if (GET_CODE (XEXP (op, 0)) == PRE_DEC
833           || GET_CODE (XEXP (op, 0)) == PRE_INC)
834         return 0;
835       return address_operand (XEXP (op, 0), mode);
836     default :
837       return 0;
838     }
839 }
840
841 /* Return true if OP is an acceptable argument for a move destination.  */
842
843 int
844 move_dest_operand (op, mode)
845      rtx op;
846      enum machine_mode mode;
847 {
848   switch (GET_CODE (op))
849     {
850     case REG :
851       return register_operand (op, mode);
852     case SUBREG :
853       /* (subreg (mem ...) ...) can occur here if the inner part was once a
854          pseudo-reg and is now a stack slot.  */
855       if (GET_CODE (SUBREG_REG (op)) == MEM)
856         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
857       else
858         return register_operand (op, mode);
859     case MEM :
860       if (GET_CODE (XEXP (op, 0)) == POST_INC)
861         return 0;               /* stores can't do post inc */
862       return address_operand (XEXP (op, 0), mode);
863     default :
864       return 0;
865     }
866 }
867
868 /* Return 1 if OP is a DImode const we want to handle inline.
869    This must match the code in the movdi pattern.
870    It is used by the 'G' CONST_DOUBLE_OK_FOR_LETTER.  */
871
872 int
873 easy_di_const (op)
874      rtx op;
875 {
876   rtx high_rtx, low_rtx;
877   HOST_WIDE_INT high, low;
878
879   split_double (op, &high_rtx, &low_rtx);
880   high = INTVAL (high_rtx);
881   low = INTVAL (low_rtx);
882   /* Pick constants loadable with 2 16 bit `ldi' insns.  */
883   if (high >= -128 && high <= 127
884       && low >= -128 && low <= 127)
885     return 1;
886   return 0;
887 }
888
889 /* Return 1 if OP is a DFmode const we want to handle inline.
890    This must match the code in the movdf pattern.
891    It is used by the 'H' CONST_DOUBLE_OK_FOR_LETTER.  */
892
893 int
894 easy_df_const (op)
895      rtx op;
896 {
897   REAL_VALUE_TYPE r;
898   long l[2];
899
900   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
901   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
902   if (l[0] == 0 && l[1] == 0)
903     return 1;
904   if ((l[0] & 0xffff) == 0 && l[1] == 0)
905     return 1;
906   return 0;
907 }
908
909 /* Return 1 if OP is an EQ or NE comparison operator.  */
910
911 int
912 eqne_comparison_operator (op, mode)
913     rtx op;
914     enum machine_mode mode ATTRIBUTE_UNUSED;
915 {
916   enum rtx_code code = GET_CODE (op);
917
918   if (GET_RTX_CLASS (code) != '<')
919     return 0;
920   return (code == EQ || code == NE);
921 }
922
923 /* Return 1 if OP is a signed comparison operator.  */
924
925 int
926 signed_comparison_operator (op, mode)
927     rtx op;
928     enum machine_mode mode ATTRIBUTE_UNUSED;
929 {
930   enum rtx_code code = GET_CODE (op);
931
932   if (GET_RTX_CLASS (code) != '<')
933     return 0;
934   return (code == EQ || code == NE
935           || code == LT || code == LE || code == GT || code == GE);
936 }
937
938 /* Return 1 if OP is (mem (reg ...)).
939    This is used in insn length calcs.  */
940
941 int
942 memreg_operand (op, mode)
943      rtx op;
944      enum machine_mode mode ATTRIBUTE_UNUSED;
945 {
946   return GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == REG;
947 }
948
949 /* Return true if OP is an acceptable input argument for a zero/sign extend
950    operation.  */
951
952 int
953 extend_operand (op, mode)
954      rtx op;
955      enum machine_mode mode;
956 {
957   rtx addr;
958
959   switch (GET_CODE (op))
960     {
961     case REG :
962     case SUBREG :
963       return register_operand (op, mode);
964
965     case MEM :
966       addr = XEXP (op, 0);
967       if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
968         return 0;               /* loads can't do pre inc/pre dec */
969
970       return address_operand (addr, mode);
971
972     default :
973       return 0;
974     }
975 }
976
977 /* Return nonzero if the operand is an insn that is a small insn.
978    Allow const_int 0 as well, which is a placeholder for NOP slots.  */
979
980 int
981 small_insn_p (op, mode)
982      rtx op;
983      enum machine_mode mode ATTRIBUTE_UNUSED;
984 {
985   if (GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
986     return 1;
987
988   if (! INSN_P (op))
989     return 0;
990
991   return get_attr_length (op) == 2;
992 }
993
994 /* Return nonzero if the operand is an insn that is a large insn.  */
995
996 int
997 large_insn_p (op, mode)
998      rtx op;
999      enum machine_mode mode ATTRIBUTE_UNUSED;
1000 {
1001   if (! INSN_P (op))
1002     return 0;
1003
1004   return get_attr_length (op) != 2;
1005 }
1006
1007 /* Return nonzero if TYPE must be passed or returned in memory.
1008    The m32r treats both directions the same so we handle both directions
1009    in this function.  */
1010
1011 int
1012 m32r_pass_by_reference (type)
1013      tree type;
1014 {
1015   int size = int_size_in_bytes (type);
1016
1017   if (size < 0 || size > 8)
1018     return 1;
1019
1020   return 0;
1021 }
1022 \f
1023 /* Comparisons.  */
1024
1025 /* X and Y are two things to compare using CODE.  Emit the compare insn and
1026    return the rtx for compare [arg0 of the if_then_else].
1027    If need_compare is true then the comparison insn must be generated, rather
1028    than being subsumed into the following branch instruction.  */
1029
1030 rtx
1031 gen_compare (code, x, y, need_compare)
1032      enum rtx_code code;
1033      rtx x, y;
1034      int need_compare;
1035 {
1036   enum rtx_code compare_code, branch_code;
1037   rtx cc_reg = gen_rtx_REG (CCmode, CARRY_REGNUM);
1038   int must_swap = 0;
1039
1040   switch (code)
1041     {
1042     case EQ:  compare_code = EQ;  branch_code = NE; break;
1043     case NE:  compare_code = EQ;  branch_code = EQ; break;
1044     case LT:  compare_code = LT;  branch_code = NE; break;
1045     case LE:  compare_code = LT;  branch_code = EQ; must_swap = 1; break;
1046     case GT:  compare_code = LT;  branch_code = NE; must_swap = 1; break;
1047     case GE:  compare_code = LT;  branch_code = EQ; break;
1048     case LTU: compare_code = LTU; branch_code = NE; break;
1049     case LEU: compare_code = LTU; branch_code = EQ; must_swap = 1; break;
1050     case GTU: compare_code = LTU; branch_code = NE; must_swap = 1; break;
1051     case GEU: compare_code = LTU; branch_code = EQ; break;
1052
1053     default:
1054       abort ();
1055     }
1056
1057   if (need_compare)
1058     {
1059       switch (compare_code)
1060         {
1061         case EQ:
1062           if (GET_CODE (y) == CONST_INT
1063               && CMP_INT16_P (INTVAL (y))               /* reg equal to small const.  */
1064               && y != const0_rtx)
1065             {
1066               rtx tmp = gen_reg_rtx (SImode);           
1067               
1068               emit_insn (gen_cmp_ne_small_const_insn (tmp, x, y));
1069               x = tmp;
1070               y = const0_rtx;
1071             }
1072           else if (CONSTANT_P (y))                      /* reg equal to const.  */
1073             {
1074               rtx tmp = force_reg (GET_MODE (x), y);
1075               y = tmp;
1076             }
1077
1078           if (register_operand (y, SImode)              /* reg equal to reg.  */
1079               || y == const0_rtx)                       /* req equal to zero. */
1080             {
1081               emit_insn (gen_cmp_eqsi_insn (x, y));
1082                 
1083               return gen_rtx (code, CCmode, cc_reg, const0_rtx);
1084             }
1085           break;
1086       
1087         case LT:
1088           if (register_operand (y, SImode)
1089               || (GET_CODE (y) == CONST_INT && CMP_INT16_P (INTVAL (y))))
1090             {
1091               rtx tmp = gen_reg_rtx (SImode);         /* reg compared to reg. */
1092               
1093               switch (code)
1094                 {
1095                 case LT:
1096                   emit_insn (gen_cmp_ltsi_insn (x, y));
1097                   code = EQ;
1098                   break;
1099                 case LE:
1100                   if (y == const0_rtx)
1101                     tmp = const1_rtx;
1102                   else
1103                     emit_insn (gen_cmp_ne_small_const_insn (tmp, y, const1_rtx));
1104                   emit_insn (gen_cmp_ltsi_insn (x, tmp));
1105                   code = EQ;
1106                   break;
1107                 case GT:
1108                   if (GET_CODE (y) == CONST_INT)
1109                     tmp = gen_rtx (PLUS, SImode, y, const1_rtx);
1110                   else
1111                     emit_insn (gen_cmp_ne_small_const_insn (tmp, y, const1_rtx));
1112                   emit_insn (gen_cmp_ltsi_insn (x, tmp));
1113                   code = NE;
1114                   break;
1115                 case GE:
1116                   emit_insn (gen_cmp_ltsi_insn (x, y));
1117                   code = NE;
1118                   break;
1119                 default:
1120                   abort ();
1121                 }
1122               
1123               return gen_rtx (code, CCmode, cc_reg, const0_rtx);
1124             }
1125           break;
1126           
1127         case LTU:
1128           if (register_operand (y, SImode)
1129               || (GET_CODE (y) == CONST_INT && CMP_INT16_P (INTVAL (y))))
1130             {
1131               rtx tmp = gen_reg_rtx (SImode);         /* reg (unsigned) compared to reg. */
1132               
1133               switch (code)
1134                 {
1135                 case LTU:
1136                   emit_insn (gen_cmp_ltusi_insn (x, y));
1137                   code = EQ;
1138                   break;
1139                 case LEU:
1140                   if (y == const0_rtx)
1141                     tmp = const1_rtx;
1142                   else
1143                     emit_insn (gen_cmp_ne_small_const_insn (tmp, y, const1_rtx));
1144                   emit_insn (gen_cmp_ltusi_insn (x, tmp));
1145                   code = EQ;
1146                   break;
1147                 case GTU:
1148                   if (GET_CODE (y) == CONST_INT)
1149                     tmp = gen_rtx (PLUS, SImode, y, const1_rtx);
1150                   else
1151                     emit_insn (gen_cmp_ne_small_const_insn (tmp, y, const1_rtx));
1152                   emit_insn (gen_cmp_ltusi_insn (x, tmp));
1153                   code = NE;
1154                   break;
1155                 case GEU:
1156                   emit_insn (gen_cmp_ltusi_insn (x, y));
1157                   code = NE;
1158                   break;
1159                 default:
1160                   abort();
1161                 }
1162               
1163               return gen_rtx (code, CCmode, cc_reg, const0_rtx);
1164             }
1165           break;
1166
1167         default:
1168           abort();
1169         }
1170     }
1171   else
1172     {
1173       /* reg/reg equal comparison */
1174       if (compare_code == EQ
1175           && register_operand (y, SImode))
1176         return gen_rtx (code, CCmode, x, y);
1177       
1178       /* reg/zero signed comparison */
1179       if ((compare_code == EQ || compare_code == LT)
1180           && y == const0_rtx)
1181         return gen_rtx (code, CCmode, x, y);
1182       
1183       /* reg/smallconst equal comparison */
1184       if (compare_code == EQ
1185           && GET_CODE (y) == CONST_INT
1186           && CMP_INT16_P (INTVAL (y)))
1187         {
1188           rtx tmp = gen_reg_rtx (SImode);
1189           emit_insn (gen_cmp_ne_small_const_insn (tmp, x, y));
1190           return gen_rtx (code, CCmode, tmp, const0_rtx);
1191         }
1192       
1193       /* reg/const equal comparison */
1194       if (compare_code == EQ
1195           && CONSTANT_P (y))
1196         {
1197           rtx tmp = force_reg (GET_MODE (x), y);
1198           return gen_rtx (code, CCmode, x, tmp);
1199         }
1200     }
1201
1202   if (CONSTANT_P (y))
1203     {
1204       if (must_swap)
1205         y = force_reg (GET_MODE (x), y);
1206       else
1207         {
1208           int ok_const =
1209             (code == LTU || code == LEU || code == GTU || code == GEU)
1210             ? uint16_operand (y, GET_MODE (y))
1211             : reg_or_cmp_int16_operand (y, GET_MODE (y));
1212           
1213           if (! ok_const)
1214             y = force_reg (GET_MODE (x), y);
1215         }
1216     }
1217
1218   switch (compare_code)
1219     {
1220     case EQ :
1221       emit_insn (gen_cmp_eqsi_insn (must_swap ? y : x, must_swap ? x : y));
1222       break;
1223     case LT :
1224       emit_insn (gen_cmp_ltsi_insn (must_swap ? y : x, must_swap ? x : y));
1225       break;
1226     case LTU :
1227       emit_insn (gen_cmp_ltusi_insn (must_swap ? y : x, must_swap ? x : y));
1228       break;
1229
1230     default:
1231       abort ();
1232     }
1233
1234   return gen_rtx (branch_code, VOIDmode, cc_reg, CONST0_RTX (CCmode));
1235 }
1236 \f
1237 /* Split a 2 word move (DI or DF) into component parts.  */
1238
1239 rtx
1240 gen_split_move_double (operands)
1241      rtx operands[];
1242 {
1243   enum machine_mode mode = GET_MODE (operands[0]);
1244   rtx dest = operands[0];
1245   rtx src  = operands[1];
1246   rtx val;
1247
1248   /* We might have (SUBREG (MEM)) here, so just get rid of the
1249      subregs to make this code simpler.  It is safe to call
1250      alter_subreg any time after reload.  */
1251   if (GET_CODE (dest) == SUBREG)
1252     alter_subreg (&dest);
1253   if (GET_CODE (src) == SUBREG)
1254     alter_subreg (&src);
1255
1256   start_sequence ();
1257   if (GET_CODE (dest) == REG)
1258     {
1259       int dregno = REGNO (dest);
1260
1261       /* reg = reg */
1262       if (GET_CODE (src) == REG)
1263         {
1264           int sregno = REGNO (src);
1265
1266           int reverse = (dregno == sregno + 1);
1267
1268           /* We normally copy the low-numbered register first.  However, if
1269              the first register operand 0 is the same as the second register of
1270              operand 1, we must copy in the opposite order.  */
1271           emit_insn (gen_rtx_SET (VOIDmode,
1272                                   operand_subword (dest, reverse, TRUE, mode),
1273                                   operand_subword (src,  reverse, TRUE, mode)));
1274
1275           emit_insn (gen_rtx_SET (VOIDmode,
1276                                   operand_subword (dest, !reverse, TRUE, mode),
1277                                   operand_subword (src,  !reverse, TRUE, mode)));
1278         }
1279
1280       /* reg = constant */
1281       else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
1282         {
1283           rtx words[2];
1284           split_double (src, &words[0], &words[1]);
1285           emit_insn (gen_rtx_SET (VOIDmode,
1286                                   operand_subword (dest, 0, TRUE, mode),
1287                                   words[0]));
1288
1289           emit_insn (gen_rtx_SET (VOIDmode,
1290                                   operand_subword (dest, 1, TRUE, mode),
1291                                   words[1]));
1292         }
1293
1294       /* reg = mem */
1295       else if (GET_CODE (src) == MEM)
1296         {
1297           /* If the high-address word is used in the address, we must load it
1298              last.  Otherwise, load it first.  */
1299           int reverse
1300             = (refers_to_regno_p (dregno, dregno + 1, XEXP (src, 0), 0) != 0);
1301
1302           /* We used to optimize loads from single registers as
1303
1304                 ld r1,r3+; ld r2,r3
1305
1306              if r3 were not used subsequently.  However, the REG_NOTES aren't
1307              propagated correctly by the reload phase, and it can cause bad
1308              code to be generated.  We could still try:
1309
1310                 ld r1,r3+; ld r2,r3; addi r3,-4
1311
1312              which saves 2 bytes and doesn't force longword alignment.  */
1313           emit_insn (gen_rtx_SET (VOIDmode,
1314                                   operand_subword (dest, reverse, TRUE, mode),
1315                                   adjust_address (src, SImode,
1316                                                   reverse * UNITS_PER_WORD)));
1317
1318           emit_insn (gen_rtx_SET (VOIDmode,
1319                                   operand_subword (dest, !reverse, TRUE, mode),
1320                                   adjust_address (src, SImode,
1321                                                   !reverse * UNITS_PER_WORD)));
1322         }
1323
1324       else
1325         abort ();
1326     }
1327
1328   /* mem = reg */
1329   /* We used to optimize loads from single registers as
1330
1331         st r1,r3; st r2,+r3
1332
1333      if r3 were not used subsequently.  However, the REG_NOTES aren't
1334      propagated correctly by the reload phase, and it can cause bad
1335      code to be generated.  We could still try:
1336
1337         st r1,r3; st r2,+r3; addi r3,-4
1338
1339      which saves 2 bytes and doesn't force longword alignment.  */
1340   else if (GET_CODE (dest) == MEM && GET_CODE (src) == REG)
1341     {
1342       emit_insn (gen_rtx_SET (VOIDmode,
1343                               adjust_address (dest, SImode, 0),
1344                               operand_subword (src, 0, TRUE, mode)));
1345
1346       emit_insn (gen_rtx_SET (VOIDmode,
1347                               adjust_address (dest, SImode, UNITS_PER_WORD),
1348                               operand_subword (src, 1, TRUE, mode)));
1349     }
1350
1351   else
1352     abort ();
1353
1354   val = get_insns ();
1355   end_sequence ();
1356   return val;
1357 }
1358
1359 \f
1360 /* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.  */
1361
1362 int
1363 function_arg_partial_nregs (cum, mode, type, named)
1364      CUMULATIVE_ARGS *cum;
1365      enum machine_mode mode;
1366      tree type;
1367      int named ATTRIBUTE_UNUSED;
1368 {
1369   int ret;
1370   unsigned int size =
1371     (((mode == BLKmode && type)
1372       ? (unsigned int) int_size_in_bytes (type)
1373       : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
1374     / UNITS_PER_WORD;
1375
1376   if (*cum >= M32R_MAX_PARM_REGS)
1377     ret = 0;
1378   else if (*cum + size > M32R_MAX_PARM_REGS)
1379     ret = (*cum + size) - M32R_MAX_PARM_REGS;
1380   else
1381     ret = 0;
1382
1383   return ret;
1384 }
1385
1386 /* Do any needed setup for a variadic function.  For the M32R, we must
1387    create a register parameter block, and then copy any anonymous arguments
1388    in registers to memory.
1389
1390    CUM has not been updated for the last named argument which has type TYPE
1391    and mode MODE, and we rely on this fact.  */
1392
1393 void
1394 m32r_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
1395      CUMULATIVE_ARGS *cum;
1396      enum machine_mode mode;
1397      tree type;
1398      int *pretend_size;
1399      int no_rtl;
1400 {
1401   int first_anon_arg;
1402
1403   if (no_rtl)
1404     return;
1405
1406   /* All BLKmode values are passed by reference.  */
1407   if (mode == BLKmode)
1408     abort ();
1409
1410   first_anon_arg = (ROUND_ADVANCE_CUM (*cum, mode, type)
1411                     + ROUND_ADVANCE_ARG (mode, type));
1412
1413   if (first_anon_arg < M32R_MAX_PARM_REGS)
1414     {
1415       /* Note that first_reg_offset < M32R_MAX_PARM_REGS.  */
1416       int first_reg_offset = first_anon_arg;
1417       /* Size in words to "pretend" allocate.  */
1418       int size = M32R_MAX_PARM_REGS - first_reg_offset;
1419       rtx regblock;
1420
1421       regblock = gen_rtx_MEM (BLKmode,
1422                               plus_constant (arg_pointer_rtx,
1423                                              FIRST_PARM_OFFSET (0)));
1424       set_mem_alias_set (regblock, get_varargs_alias_set ());
1425       move_block_from_reg (first_reg_offset, regblock, size);
1426
1427       *pretend_size = (size * UNITS_PER_WORD);
1428     }
1429 }
1430
1431 \f
1432 /* Implement `va_arg'.  */
1433
1434 rtx
1435 m32r_va_arg (valist, type)
1436      tree valist, type;
1437 {
1438   HOST_WIDE_INT size, rsize;
1439   tree t;
1440   rtx addr_rtx;
1441
1442   size = int_size_in_bytes (type);
1443   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
1444
1445   if (m32r_pass_by_reference (type))
1446     {
1447       tree type_ptr, type_ptr_ptr;
1448
1449       /* Pass by reference.  */
1450
1451       type_ptr = build_pointer_type (type);
1452       type_ptr_ptr = build_pointer_type (type_ptr);
1453
1454       t = build (POSTINCREMENT_EXPR, va_list_type_node, valist, 
1455                  build_int_2 (UNITS_PER_WORD, 0));
1456       TREE_SIDE_EFFECTS (t) = 1;
1457       t = build1 (NOP_EXPR, type_ptr_ptr, t);
1458       TREE_SIDE_EFFECTS (t) = 1;
1459       t = build1 (INDIRECT_REF, type_ptr, t);
1460
1461       addr_rtx = expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
1462     }
1463   else
1464     {
1465       /* Pass by value.  */
1466
1467       if (size < UNITS_PER_WORD)
1468         {
1469           /* Care for bigendian correction on the aligned address.  */
1470           t = build (PLUS_EXPR, ptr_type_node, valist,
1471                      build_int_2 (rsize - size, 0));
1472           addr_rtx = expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
1473           addr_rtx = copy_to_reg (addr_rtx);
1474
1475           /* Increment AP.  */
1476           t = build (PLUS_EXPR, va_list_type_node, valist,
1477                      build_int_2 (rsize, 0));
1478           t = build (MODIFY_EXPR, va_list_type_node, valist, t);
1479           TREE_SIDE_EFFECTS (t) = 1;
1480           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1481         }
1482       else
1483         {
1484           t = build (POSTINCREMENT_EXPR, va_list_type_node, valist, 
1485                      build_int_2 (rsize, 0));
1486           TREE_SIDE_EFFECTS (t) = 1;
1487           addr_rtx = expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
1488         }
1489     }
1490
1491   return addr_rtx;
1492 }
1493 \f
1494 static int
1495 m32r_adjust_cost (insn, link, dep_insn, cost)
1496      rtx insn ATTRIBUTE_UNUSED;
1497      rtx link ATTRIBUTE_UNUSED;
1498      rtx dep_insn ATTRIBUTE_UNUSED;
1499      int cost;
1500 {
1501   return cost;
1502 }
1503
1504 \f
1505 /* Return true if INSN is real instruction bearing insn.  */
1506
1507 static int
1508 m32r_is_insn (insn)
1509      rtx insn;
1510 {
1511   return (INSN_P (insn)
1512           && GET_CODE (PATTERN (insn)) != USE
1513           && GET_CODE (PATTERN (insn)) != CLOBBER
1514           && GET_CODE (PATTERN (insn)) != ADDR_VEC);
1515 }
1516
1517 /* Increase the priority of long instructions so that the
1518    short instructions are scheduled ahead of the long ones.  */
1519
1520 static int
1521 m32r_adjust_priority (insn, priority)
1522      rtx insn;
1523      int priority;
1524 {
1525   if (m32r_is_insn (insn)
1526       && get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1527     priority <<= 3;
1528
1529   return priority;
1530 }
1531
1532 \f
1533 /* Initialize for scheduling a group of instructions.  */
1534
1535 static void
1536 m32r_sched_init (stream, verbose, max_ready)
1537      FILE * stream ATTRIBUTE_UNUSED;
1538      int verbose ATTRIBUTE_UNUSED;
1539      int max_ready ATTRIBUTE_UNUSED;
1540 {
1541   m32r_sched_odd_word_p = FALSE;
1542 }
1543
1544 \f
1545 /* Reorder the schedulers priority list if needed */
1546
1547 static int
1548 m32r_sched_reorder (stream, verbose, ready, n_readyp, clock)
1549      FILE * stream;
1550      int verbose;
1551      rtx * ready;
1552      int *n_readyp;
1553      int clock ATTRIBUTE_UNUSED;
1554 {
1555   int n_ready = *n_readyp;
1556
1557   if (TARGET_DEBUG)
1558     return m32r_issue_rate ();
1559
1560   if (verbose <= 7)
1561     stream = (FILE *)0;
1562
1563   if (stream)
1564     fprintf (stream,
1565              ";;\t\t::: Looking at %d insn(s) on ready list, boundary is %s word\n",
1566              n_ready,
1567              (m32r_sched_odd_word_p) ? "odd" : "even");
1568
1569   if (n_ready > 1)
1570     {
1571       rtx * long_head = (rtx *) alloca (sizeof (rtx) * n_ready);
1572       rtx * long_tail = long_head;
1573       rtx * short_head = (rtx *) alloca (sizeof (rtx) * n_ready);
1574       rtx * short_tail = short_head;
1575       rtx * new_head = (rtx *) alloca (sizeof (rtx) * n_ready);
1576       rtx * new_tail = new_head + (n_ready - 1);
1577       int   i;
1578
1579       /* Loop through the instructions, classifying them as short/long.  Try
1580          to keep 2 short together and/or 1 long.  Note, the ready list is
1581          actually ordered backwards, so keep it in that manner.  */
1582       for (i = n_ready-1; i >= 0; i--)
1583         {
1584           rtx insn = ready[i];
1585
1586           if (! m32r_is_insn (insn))
1587             {
1588               /* Dump all current short/long insns just in case.  */
1589               while (long_head != long_tail)
1590                 *new_tail-- = *long_head++;
1591
1592               while (short_head != short_tail)
1593                 *new_tail-- = *short_head++;
1594
1595               *new_tail-- = insn;
1596               if (stream)
1597                 fprintf (stream,
1598                          ";;\t\t::: Skipping non instruction %d\n",
1599                          INSN_UID (insn));
1600
1601             }
1602
1603           else
1604             {
1605               if (get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1606                 *long_tail++ = insn;
1607
1608               else
1609                 *short_tail++ = insn;
1610             }
1611         }
1612
1613       /* If we are on an odd word, emit a single short instruction if
1614          we can */
1615       if (m32r_sched_odd_word_p && short_head != short_tail)
1616         *new_tail-- = *short_head++;
1617
1618       /* Now dump out all of the long instructions */
1619       while (long_head != long_tail)
1620         *new_tail-- = *long_head++;
1621
1622       /* Now dump out all of the short instructions */
1623       while (short_head != short_tail)
1624         *new_tail-- = *short_head++;
1625
1626       if (new_tail+1 != new_head)
1627         abort ();
1628
1629       memcpy (ready, new_head, sizeof (rtx) * n_ready);
1630       if (stream)
1631         {
1632           int i;
1633           fprintf (stream, ";;\t\t::: New ready list:               ");
1634           for (i = 0; i < n_ready; i++)
1635             {
1636               rtx insn = ready[i];
1637
1638               fprintf (stream, " %d", INSN_UID (ready[i]));
1639
1640               if (! m32r_is_insn (insn))
1641                 fputs ("(?)", stream);
1642
1643               else if (get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1644                 fputs ("(l)", stream);
1645
1646               else
1647                 fputs ("(s)", stream);
1648             }
1649
1650           fprintf (stream, "\n");
1651         }
1652     }
1653   return m32r_issue_rate ();
1654 }
1655
1656 /* Indicate how many instructions can be issued at the same time.
1657    This is sort of a lie.  The m32r can issue only 1 long insn at
1658    once, but it can issue 2 short insns.  The default therefore is
1659    set at 2, but this can be overridden by the command line option
1660    -missue-rate=1 */
1661 static int
1662 m32r_issue_rate ()
1663 {
1664   return ((TARGET_LOW_ISSUE_RATE) ? 1 : 2);
1665 }
1666
1667 /* If we have a machine that can issue a variable # of instructions
1668    per cycle, indicate how many more instructions can be issued
1669    after the current one.  */
1670 static int
1671 m32r_variable_issue (stream, verbose, insn, how_many)
1672      FILE * stream;
1673      int verbose;
1674      rtx insn;
1675      int how_many;
1676 {
1677   int orig_odd_word_p = m32r_sched_odd_word_p;
1678   int short_p = FALSE;
1679
1680   how_many--;
1681   if (how_many > 0 && !TARGET_DEBUG)
1682     {
1683       if (! m32r_is_insn (insn))
1684         how_many++;
1685
1686       else if (get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1687         {
1688           how_many = 0;
1689           m32r_sched_odd_word_p = 0;
1690         }
1691       else
1692         {
1693           m32r_sched_odd_word_p = !m32r_sched_odd_word_p;
1694           short_p = TRUE;
1695         }
1696     }
1697
1698   if (verbose > 7 && stream)
1699     fprintf (stream,
1700              ";;\t\t::: %s insn %d starts on an %s word, can emit %d more instruction(s)\n",
1701              short_p ? "short" : "long",
1702              INSN_UID (insn),
1703              orig_odd_word_p ? "odd" : "even",
1704              how_many);
1705
1706   return how_many;
1707 }
1708 \f
1709 /* Cost functions.  */
1710
1711 static bool
1712 m32r_rtx_costs (x, code, outer_code, total)
1713      rtx x;
1714      int code, outer_code ATTRIBUTE_UNUSED;
1715      int *total;
1716 {
1717   switch (code)
1718     {
1719       /* Small integers are as cheap as registers.  4 byte values can be
1720          fetched as immediate constants - let's give that the cost of an
1721          extra insn.  */
1722     case CONST_INT:
1723       if (INT16_P (INTVAL (x)))
1724         {
1725           *total = 0;
1726           return true;
1727         }
1728       /* FALLTHRU */
1729
1730     case CONST:
1731     case LABEL_REF:
1732     case SYMBOL_REF:
1733       *total = COSTS_N_INSNS (1);
1734       return true;
1735
1736     case CONST_DOUBLE:
1737       {
1738         rtx high, low;
1739         split_double (x, &high, &low);
1740         *total = COSTS_N_INSNS (!INT16_P (INTVAL (high))
1741                                 + !INT16_P (INTVAL (low)));
1742         return true;
1743       }
1744
1745     case MULT:
1746       *total = COSTS_N_INSNS (3);
1747       return true;
1748
1749     case DIV:
1750     case UDIV:
1751     case MOD:
1752     case UMOD:
1753       *total = COSTS_N_INSNS (10);
1754       return true;
1755
1756     default:
1757       return false;
1758     }
1759 }
1760 \f
1761 /* Type of function DECL.
1762
1763    The result is cached.  To reset the cache at the end of a function,
1764    call with DECL = NULL_TREE.  */
1765
1766 enum m32r_function_type
1767 m32r_compute_function_type (decl)
1768      tree decl;
1769 {
1770   /* Cached value.  */
1771   static enum m32r_function_type fn_type = M32R_FUNCTION_UNKNOWN;
1772   /* Last function we were called for.  */
1773   static tree last_fn = NULL_TREE;
1774
1775   /* Resetting the cached value?  */
1776   if (decl == NULL_TREE)
1777     {
1778       fn_type = M32R_FUNCTION_UNKNOWN;
1779       last_fn = NULL_TREE;
1780       return fn_type;
1781     }
1782
1783   if (decl == last_fn && fn_type != M32R_FUNCTION_UNKNOWN)
1784     return fn_type;
1785
1786   /* Compute function type.  */
1787   fn_type = (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE
1788              ? M32R_FUNCTION_INTERRUPT
1789              : M32R_FUNCTION_NORMAL);
1790
1791   last_fn = decl;
1792   return fn_type;
1793 }
1794 \f/* Function prologue/epilogue handlers.  */
1795
1796 /* M32R stack frames look like:
1797
1798              Before call                       After call
1799         +-----------------------+       +-----------------------+
1800         |                       |       |                       |
1801    high |  local variables,     |       |  local variables,     |
1802    mem  |  reg save area, etc.  |       |  reg save area, etc.  |
1803         |                       |       |                       |
1804         +-----------------------+       +-----------------------+
1805         |                       |       |                       |
1806         |  arguments on stack.  |       |  arguments on stack.  |
1807         |                       |       |                       |
1808   SP+0->+-----------------------+       +-----------------------+
1809                                         |  reg parm save area,  |
1810                                         |  only created for     |    
1811                                         |  variable argument    |    
1812                                         |  functions            |    
1813                                         +-----------------------+
1814                                         |   previous frame ptr  |
1815                                         +-----------------------+    
1816                                         |                       |    
1817                                         |  register save area   |    
1818                                         |                       |    
1819                                         +-----------------------+
1820                                         |    return address     |    
1821                                         +-----------------------+    
1822                                         |                       |    
1823                                         |  local variables      |    
1824                                         |                       |    
1825                                         +-----------------------+    
1826                                         |                       |    
1827                                         |  alloca allocations   |    
1828                                         |                       |    
1829                                         +-----------------------+    
1830                                         |                       |    
1831    low                                  |  arguments on stack   |    
1832    memory                               |                       |    
1833                                   SP+0->+-----------------------+    
1834
1835 Notes:
1836 1) The "reg parm save area" does not exist for non variable argument fns.
1837 2) The "reg parm save area" can be eliminated completely if we saved regs
1838    containing anonymous args separately but that complicates things too
1839    much (so it's not done).
1840 3) The return address is saved after the register save area so as to have as
1841    many insns as possible between the restoration of `lr' and the `jmp lr'.
1842 */
1843
1844 /* Structure to be filled in by m32r_compute_frame_size with register
1845    save masks, and offsets for the current function.  */
1846 struct m32r_frame_info
1847 {
1848   unsigned int total_size;      /* # bytes that the entire frame takes up */
1849   unsigned int extra_size;      /* # bytes of extra stuff */
1850   unsigned int pretend_size;    /* # bytes we push and pretend caller did */
1851   unsigned int args_size;       /* # bytes that outgoing arguments take up */
1852   unsigned int reg_size;        /* # bytes needed to store regs */
1853   unsigned int var_size;        /* # bytes that variables take up */
1854   unsigned int gmask;           /* mask of saved gp registers */
1855   unsigned int save_fp;         /* nonzero if fp must be saved */
1856   unsigned int save_lr;         /* nonzero if lr (return addr) must be saved */
1857   int          initialized;     /* nonzero if frame size already calculated */
1858 };
1859
1860 /* Current frame information calculated by m32r_compute_frame_size.  */
1861 static struct m32r_frame_info current_frame_info;
1862
1863 /* Zero structure to initialize current_frame_info.  */
1864 static struct m32r_frame_info zero_frame_info;
1865
1866 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
1867 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
1868
1869 /* Tell prologue and epilogue if register REGNO should be saved / restored.
1870    The return address and frame pointer are treated separately.
1871    Don't consider them here.  */
1872 #define MUST_SAVE_REGISTER(regno, interrupt_p) \
1873 ((regno) != RETURN_ADDR_REGNUM && (regno) != FRAME_POINTER_REGNUM \
1874  && (regs_ever_live[regno] && (!call_used_regs[regno] || interrupt_p)))
1875
1876 #define MUST_SAVE_FRAME_POINTER (regs_ever_live[FRAME_POINTER_REGNUM])
1877 #define MUST_SAVE_RETURN_ADDR (regs_ever_live[RETURN_ADDR_REGNUM] || current_function_profile)
1878
1879 #define SHORT_INSN_SIZE 2       /* size of small instructions */
1880 #define LONG_INSN_SIZE 4        /* size of long instructions */
1881
1882 /* Return the bytes needed to compute the frame pointer from the current
1883    stack pointer.
1884
1885    SIZE is the size needed for local variables.  */
1886
1887 unsigned int
1888 m32r_compute_frame_size (size)
1889      int size;                  /* # of var. bytes allocated.  */
1890 {
1891   int regno;
1892   unsigned int total_size, var_size, args_size, pretend_size, extra_size;
1893   unsigned int reg_size, frame_size;
1894   unsigned int gmask;
1895   enum m32r_function_type fn_type;
1896   int interrupt_p;
1897
1898   var_size      = M32R_STACK_ALIGN (size);
1899   args_size     = M32R_STACK_ALIGN (current_function_outgoing_args_size);
1900   pretend_size  = current_function_pretend_args_size;
1901   extra_size    = FIRST_PARM_OFFSET (0);
1902   total_size    = extra_size + pretend_size + args_size + var_size;
1903   reg_size      = 0;
1904   gmask         = 0;
1905
1906   /* See if this is an interrupt handler.  Call used registers must be saved
1907      for them too.  */
1908   fn_type = m32r_compute_function_type (current_function_decl);
1909   interrupt_p = M32R_INTERRUPT_P (fn_type);
1910
1911   /* Calculate space needed for registers.  */
1912
1913   for (regno = 0; regno < M32R_MAX_INT_REGS; regno++)
1914     {
1915       if (MUST_SAVE_REGISTER (regno, interrupt_p))
1916         {
1917           reg_size += UNITS_PER_WORD;
1918           gmask |= 1 << regno;
1919         }
1920     }
1921
1922   current_frame_info.save_fp = MUST_SAVE_FRAME_POINTER;
1923   current_frame_info.save_lr = MUST_SAVE_RETURN_ADDR;
1924
1925   reg_size += ((current_frame_info.save_fp + current_frame_info.save_lr)
1926                * UNITS_PER_WORD);
1927   total_size += reg_size;
1928
1929   /* ??? Not sure this is necessary, and I don't think the epilogue
1930      handler will do the right thing if this changes total_size.  */
1931   total_size = M32R_STACK_ALIGN (total_size);
1932
1933   frame_size = total_size - (pretend_size + reg_size);
1934
1935   /* Save computed information.  */
1936   current_frame_info.total_size   = total_size;
1937   current_frame_info.extra_size   = extra_size;
1938   current_frame_info.pretend_size = pretend_size;
1939   current_frame_info.var_size     = var_size;
1940   current_frame_info.args_size    = args_size;
1941   current_frame_info.reg_size     = reg_size;
1942   current_frame_info.gmask        = gmask;
1943   current_frame_info.initialized  = reload_completed;
1944
1945   /* Ok, we're done.  */
1946   return total_size;
1947 }
1948 \f
1949 /* When the `length' insn attribute is used, this macro specifies the
1950    value to be assigned to the address of the first insn in a
1951    function.  If not specified, 0 is used.  */
1952
1953 int
1954 m32r_first_insn_address ()
1955 {
1956   if (! current_frame_info.initialized)
1957     m32r_compute_frame_size (get_frame_size ());
1958
1959   return 0;
1960 }
1961 \f
1962 /* Expand the m32r prologue as a series of insns.  */
1963
1964 void
1965 m32r_expand_prologue ()
1966 {
1967   int regno;
1968   int frame_size;
1969   unsigned int gmask;
1970
1971   if (! current_frame_info.initialized)
1972     m32r_compute_frame_size (get_frame_size ());
1973
1974   gmask = current_frame_info.gmask;
1975
1976   /* These cases shouldn't happen.  Catch them now.  */
1977   if (current_frame_info.total_size == 0 && gmask)
1978     abort ();
1979
1980   /* Allocate space for register arguments if this is a variadic function.  */
1981   if (current_frame_info.pretend_size != 0)
1982     {
1983       /* Use a HOST_WIDE_INT temporary, since negating an unsigned int gives
1984          the wrong result on a 64-bit host.  */
1985       HOST_WIDE_INT pretend_size = current_frame_info.pretend_size;
1986       emit_insn (gen_addsi3 (stack_pointer_rtx,
1987                              stack_pointer_rtx,
1988                              GEN_INT (-pretend_size)));
1989     }
1990
1991   /* Save any registers we need to and set up fp.  */
1992
1993   if (current_frame_info.save_fp)
1994     emit_insn (gen_movsi_push (stack_pointer_rtx, frame_pointer_rtx));
1995
1996   gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1997
1998   /* Save any needed call-saved regs (and call-used if this is an
1999      interrupt handler).  */
2000   for (regno = 0; regno <= M32R_MAX_INT_REGS; ++regno)
2001     {
2002       if ((gmask & (1 << regno)) != 0)
2003         emit_insn (gen_movsi_push (stack_pointer_rtx,
2004                                    gen_rtx_REG (Pmode, regno)));
2005     }
2006
2007   if (current_frame_info.save_lr)
2008     emit_insn (gen_movsi_push (stack_pointer_rtx,
2009                                gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
2010
2011   /* Allocate the stack frame.  */
2012   frame_size = (current_frame_info.total_size
2013                 - (current_frame_info.pretend_size
2014                    + current_frame_info.reg_size));
2015
2016   if (frame_size == 0)
2017     ; /* nothing to do */
2018   else if (frame_size <= 32768)
2019     emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2020                            GEN_INT (-frame_size)));
2021   else
2022     {
2023       rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
2024       emit_insn (gen_movsi (tmp, GEN_INT (frame_size)));
2025       emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
2026     }
2027
2028   if (frame_pointer_needed)
2029     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2030
2031   if (current_function_profile)
2032     emit_insn (gen_blockage ());
2033 }
2034
2035 \f
2036 /* Set up the stack and frame pointer (if desired) for the function.
2037    Note, if this is changed, you need to mirror the changes in
2038    m32r_compute_frame_size which calculates the prolog size.  */
2039
2040 static void
2041 m32r_output_function_prologue (file, size)
2042      FILE * file;
2043      HOST_WIDE_INT size;
2044 {
2045   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
2046
2047   /* If this is an interrupt handler, mark it as such.  */
2048   if (M32R_INTERRUPT_P (fn_type))
2049     {
2050       fprintf (file, "\t%s interrupt handler\n",
2051                ASM_COMMENT_START);
2052     }
2053
2054   if (! current_frame_info.initialized)
2055     m32r_compute_frame_size (size);
2056
2057   /* This is only for the human reader.  */
2058   fprintf (file,
2059            "\t%s PROLOGUE, vars= %d, regs= %d, args= %d, extra= %d\n",
2060            ASM_COMMENT_START,
2061            current_frame_info.var_size,
2062            current_frame_info.reg_size / 4,
2063            current_frame_info.args_size,
2064            current_frame_info.extra_size);
2065 }
2066 \f
2067 /* Do any necessary cleanup after a function to restore stack, frame,
2068    and regs. */
2069
2070 static void
2071 m32r_output_function_epilogue (file, size)
2072      FILE * file;
2073      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
2074 {
2075   int regno;
2076   int noepilogue = FALSE;
2077   int total_size;
2078   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
2079
2080   /* This is only for the human reader.  */
2081   fprintf (file, "\t%s EPILOGUE\n", ASM_COMMENT_START);
2082
2083   if (!current_frame_info.initialized)
2084     abort ();
2085   total_size = current_frame_info.total_size;
2086
2087   if (total_size == 0)
2088     {
2089       rtx insn = get_last_insn ();
2090
2091       /* If the last insn was a BARRIER, we don't have to write any code
2092          because a jump (aka return) was put there.  */
2093       if (GET_CODE (insn) == NOTE)
2094         insn = prev_nonnote_insn (insn);
2095       if (insn && GET_CODE (insn) == BARRIER)
2096         noepilogue = TRUE;
2097     }
2098
2099   if (!noepilogue)
2100     {
2101       unsigned int var_size = current_frame_info.var_size;
2102       unsigned int args_size = current_frame_info.args_size;
2103       unsigned int gmask = current_frame_info.gmask;
2104       int can_trust_sp_p = !current_function_calls_alloca;
2105       const char * sp_str = reg_names[STACK_POINTER_REGNUM];
2106       const char * fp_str = reg_names[FRAME_POINTER_REGNUM];
2107
2108       /* The first thing to do is point the sp at the bottom of the register
2109          save area.  */
2110       if (can_trust_sp_p)
2111         {
2112           unsigned int reg_offset = var_size + args_size;
2113           if (reg_offset == 0)
2114             ; /* nothing to do */
2115           else if (reg_offset < 128)
2116             fprintf (file, "\taddi %s,%s%d\n",
2117                      sp_str, IMMEDIATE_PREFIX, reg_offset);
2118           else if (reg_offset < 32768)
2119             fprintf (file, "\tadd3 %s,%s,%s%d\n",
2120                      sp_str, sp_str, IMMEDIATE_PREFIX, reg_offset);
2121           else
2122             fprintf (file, "\tld24 %s,%s%d\n\tadd %s,%s\n",
2123                      reg_names[PROLOGUE_TMP_REGNUM],
2124                      IMMEDIATE_PREFIX, reg_offset,
2125                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
2126         }
2127       else if (frame_pointer_needed)
2128         {
2129           unsigned int reg_offset = var_size + args_size;
2130           if (reg_offset == 0)
2131             fprintf (file, "\tmv %s,%s\n", sp_str, fp_str);
2132           else if (reg_offset < 32768)
2133             fprintf (file, "\tadd3 %s,%s,%s%d\n",
2134                      sp_str, fp_str, IMMEDIATE_PREFIX, reg_offset);
2135           else
2136             fprintf (file, "\tld24 %s,%s%d\n\tadd %s,%s\n",
2137                      reg_names[PROLOGUE_TMP_REGNUM],
2138                      IMMEDIATE_PREFIX, reg_offset,
2139                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
2140         }
2141       else
2142         abort ();
2143
2144       if (current_frame_info.save_lr)
2145         fprintf (file, "\tpop %s\n", reg_names[RETURN_ADDR_REGNUM]);
2146
2147       /* Restore any saved registers, in reverse order of course.  */
2148       gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
2149       for (regno = M32R_MAX_INT_REGS - 1; regno >= 0; --regno)
2150         {
2151           if ((gmask & (1L << regno)) != 0)
2152             fprintf (file, "\tpop %s\n", reg_names[regno]);
2153         }
2154
2155       if (current_frame_info.save_fp)
2156         fprintf (file, "\tpop %s\n", fp_str);
2157
2158       /* Remove varargs area if present.  */
2159       if (current_frame_info.pretend_size != 0)
2160         fprintf (file, "\taddi %s,%s%d\n",
2161                  sp_str, IMMEDIATE_PREFIX, current_frame_info.pretend_size);
2162         
2163       /* Emit the return instruction.  */
2164       if (M32R_INTERRUPT_P (fn_type))
2165         fprintf (file, "\trte\n");
2166       else
2167         fprintf (file, "\tjmp %s\n", reg_names[RETURN_ADDR_REGNUM]);
2168     }
2169
2170 #if 0 /* no longer needed */
2171   /* Ensure the function cleanly ends on a 32 bit boundary.  */
2172   fprintf (file, "\t.fillinsn\n");
2173 #endif
2174
2175   /* Reset state info for each function.  */
2176   current_frame_info = zero_frame_info;
2177   m32r_compute_function_type (NULL_TREE);
2178 }
2179 \f
2180 /* Return nonzero if this function is known to have a null or 1 instruction
2181    epilogue.  */
2182
2183 int
2184 direct_return ()
2185 {
2186   if (!reload_completed)
2187     return FALSE;
2188
2189   if (! current_frame_info.initialized)
2190     m32r_compute_frame_size (get_frame_size ());
2191
2192   return current_frame_info.total_size == 0;
2193 }
2194
2195 \f
2196 /* PIC */
2197
2198 /* Emit special PIC prologues and epilogues.  */
2199
2200 void
2201 m32r_finalize_pic ()
2202 {
2203   /* nothing to do */
2204 }
2205 \f
2206 /* Nested function support.  */
2207
2208 /* Emit RTL insns to initialize the variable parts of a trampoline.
2209    FNADDR is an RTX for the address of the function's pure code.
2210    CXT is an RTX for the static chain value for the function.  */
2211
2212 void
2213 m32r_initialize_trampoline (tramp, fnaddr, cxt)
2214      rtx tramp ATTRIBUTE_UNUSED;
2215      rtx fnaddr ATTRIBUTE_UNUSED;
2216      rtx cxt ATTRIBUTE_UNUSED;
2217 {
2218 }
2219 \f
2220 static void
2221 m32r_file_start ()
2222 {
2223   default_file_start ();
2224
2225   if (flag_verbose_asm)
2226     fprintf (asm_out_file,
2227              "%s M32R/D special options: -G " HOST_WIDE_INT_PRINT_UNSIGNED "\n",
2228              ASM_COMMENT_START, g_switch_value);
2229 }
2230 \f
2231 /* Print operand X (an rtx) in assembler syntax to file FILE.
2232    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
2233    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
2234
2235 void
2236 m32r_print_operand (file, x, code)
2237      FILE * file;
2238      rtx    x;
2239      int    code;
2240 {
2241   rtx addr;
2242
2243   switch (code)
2244     {
2245       /* The 's' and 'p' codes are used by output_block_move() to
2246          indicate post-increment 's'tores and 'p're-increment loads.  */
2247     case 's':
2248       if (GET_CODE (x) == REG)
2249         fprintf (file, "@+%s", reg_names [REGNO (x)]);
2250       else
2251         output_operand_lossage ("invalid operand to %%s code");
2252       return;
2253       
2254     case 'p':
2255       if (GET_CODE (x) == REG)
2256         fprintf (file, "@%s+", reg_names [REGNO (x)]);
2257       else
2258         output_operand_lossage ("invalid operand to %%p code");
2259       return;
2260
2261     case 'R' :
2262       /* Write second word of DImode or DFmode reference,
2263          register or memory.  */
2264       if (GET_CODE (x) == REG)
2265         fputs (reg_names[REGNO (x)+1], file);
2266       else if (GET_CODE (x) == MEM)
2267         {
2268           fprintf (file, "@(");
2269           /* Handle possible auto-increment.  Since it is pre-increment and
2270              we have already done it, we can just use an offset of four.  */
2271           /* ??? This is taken from rs6000.c I think.  I don't think it is
2272              currently necessary, but keep it around.  */
2273           if (GET_CODE (XEXP (x, 0)) == PRE_INC
2274               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2275             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 4));
2276           else
2277             output_address (plus_constant (XEXP (x, 0), 4));
2278           fputc (')', file);
2279         }
2280       else
2281         output_operand_lossage ("invalid operand to %%R code");
2282       return;
2283
2284     case 'H' : /* High word */
2285     case 'L' : /* Low word */
2286       if (GET_CODE (x) == REG)
2287         {
2288           /* L = least significant word, H = most significant word */
2289           if ((WORDS_BIG_ENDIAN != 0) ^ (code == 'L'))
2290             fputs (reg_names[REGNO (x)], file);
2291           else
2292             fputs (reg_names[REGNO (x)+1], file);
2293         }
2294       else if (GET_CODE (x) == CONST_INT
2295                || GET_CODE (x) == CONST_DOUBLE)
2296         {
2297           rtx first, second;
2298
2299           split_double (x, &first, &second);
2300           fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2301                    code == 'L' ? INTVAL (first) : INTVAL (second));
2302         }
2303       else
2304         output_operand_lossage ("invalid operand to %%H/%%L code");
2305       return;
2306
2307     case 'A' :
2308       {
2309         char str[30];
2310
2311         if (GET_CODE (x) != CONST_DOUBLE
2312             || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
2313           fatal_insn ("bad insn for 'A'", x);
2314
2315         real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (x), sizeof (str), 0, 1);
2316         fprintf (file, "%s", str);
2317         return;
2318       }
2319
2320     case 'B' : /* Bottom half */
2321     case 'T' : /* Top half */
2322       /* Output the argument to a `seth' insn (sets the Top half-word).
2323          For constants output arguments to a seth/or3 pair to set Top and
2324          Bottom halves.  For symbols output arguments to a seth/add3 pair to
2325          set Top and Bottom halves.  The difference exists because for
2326          constants seth/or3 is more readable but for symbols we need to use
2327          the same scheme as `ld' and `st' insns (16 bit addend is signed).  */
2328       switch (GET_CODE (x))
2329         {
2330         case CONST_INT :
2331         case CONST_DOUBLE :
2332           {
2333             rtx first, second;
2334
2335             split_double (x, &first, &second);
2336             x = WORDS_BIG_ENDIAN ? second : first;
2337             fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2338                      (code == 'B'
2339                       ? INTVAL (x) & 0xffff
2340                       : (INTVAL (x) >> 16) & 0xffff));
2341           }
2342           return;
2343         case CONST :
2344         case SYMBOL_REF :
2345           if (code == 'B'
2346               && small_data_operand (x, VOIDmode))
2347             {
2348               fputs ("sda(", file);
2349               output_addr_const (file, x);
2350               fputc (')', file);
2351               return;
2352             }
2353           /* fall through */
2354         case LABEL_REF :
2355           fputs (code == 'T' ? "shigh(" : "low(", file);
2356           output_addr_const (file, x);
2357           fputc (')', file);
2358           return;
2359         default :
2360           output_operand_lossage ("invalid operand to %%T/%%B code");
2361           return;
2362         }
2363       break;
2364
2365     case 'U' :
2366       /* ??? wip */
2367       /* Output a load/store with update indicator if appropriate.  */
2368       if (GET_CODE (x) == MEM)
2369         {
2370           if (GET_CODE (XEXP (x, 0)) == PRE_INC
2371               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2372             fputs (".a", file);
2373         }
2374       else
2375         output_operand_lossage ("invalid operand to %%U code");
2376       return;
2377
2378     case 'N' :
2379       /* Print a constant value negated.  */
2380       if (GET_CODE (x) == CONST_INT)
2381         output_addr_const (file, GEN_INT (- INTVAL (x)));
2382       else
2383         output_operand_lossage ("invalid operand to %%N code");
2384       return;
2385
2386     case 'X' :
2387       /* Print a const_int in hex.  Used in comments.  */
2388       if (GET_CODE (x) == CONST_INT)
2389         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
2390       return;
2391
2392     case '#' :
2393       fputs (IMMEDIATE_PREFIX, file);
2394       return;
2395
2396 #if 0 /* ??? no longer used */
2397     case '@' :
2398       fputs (reg_names[SDA_REGNUM], file);
2399       return;
2400 #endif
2401
2402     case 0 :
2403       /* Do nothing special.  */
2404       break;
2405
2406     default :
2407       /* Unknown flag.  */
2408       output_operand_lossage ("invalid operand output code");
2409     }
2410
2411   switch (GET_CODE (x))
2412     {
2413     case REG :
2414       fputs (reg_names[REGNO (x)], file);
2415       break;
2416
2417     case MEM :
2418       addr = XEXP (x, 0);
2419       if (GET_CODE (addr) == PRE_INC)
2420         {
2421           if (GET_CODE (XEXP (addr, 0)) != REG)
2422             fatal_insn ("pre-increment address is not a register", x);
2423
2424           fprintf (file, "@+%s", reg_names[REGNO (XEXP (addr, 0))]);
2425         }
2426       else if (GET_CODE (addr) == PRE_DEC)
2427         {
2428           if (GET_CODE (XEXP (addr, 0)) != REG)
2429             fatal_insn ("pre-decrement address is not a register", x);
2430
2431           fprintf (file, "@-%s", reg_names[REGNO (XEXP (addr, 0))]);
2432         }
2433       else if (GET_CODE (addr) == POST_INC)
2434         {
2435           if (GET_CODE (XEXP (addr, 0)) != REG)
2436             fatal_insn ("post-increment address is not a register", x);
2437
2438           fprintf (file, "@%s+", reg_names[REGNO (XEXP (addr, 0))]);
2439         }
2440       else
2441         {
2442           fputs ("@(", file);
2443           output_address (XEXP (x, 0));
2444           fputc (')', file);
2445         }
2446       break;
2447
2448     case CONST_DOUBLE :
2449       /* We handle SFmode constants here as output_addr_const doesn't.  */
2450       if (GET_MODE (x) == SFmode)
2451         {
2452           REAL_VALUE_TYPE d;
2453           long l;
2454
2455           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
2456           REAL_VALUE_TO_TARGET_SINGLE (d, l);
2457           fprintf (file, "0x%08lx", l);
2458           break;
2459         }
2460
2461       /* Fall through.  Let output_addr_const deal with it.  */
2462
2463     default :
2464       output_addr_const (file, x);
2465       break;
2466     }
2467 }
2468
2469 /* Print a memory address as an operand to reference that memory location.  */
2470
2471 void
2472 m32r_print_operand_address (file, addr)
2473      FILE * file;
2474      rtx    addr;
2475 {
2476   register rtx base;
2477   register rtx index = 0;
2478   int          offset = 0;
2479
2480   switch (GET_CODE (addr))
2481     {
2482     case REG :
2483       fputs (reg_names[REGNO (addr)], file);
2484       break;
2485
2486     case PLUS :
2487       if (GET_CODE (XEXP (addr, 0)) == CONST_INT)
2488         offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
2489       else if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
2490         offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
2491       else
2492         base = XEXP (addr, 0), index = XEXP (addr, 1);
2493       if (GET_CODE (base) == REG)
2494         {
2495           /* Print the offset first (if present) to conform to the manual.  */
2496           if (index == 0)
2497             {
2498               if (offset != 0)
2499                 fprintf (file, "%d,", offset);
2500               fputs (reg_names[REGNO (base)], file);
2501             }
2502           /* The chip doesn't support this, but left in for generality.  */
2503           else if (GET_CODE (index) == REG)
2504             fprintf (file, "%s,%s",
2505                      reg_names[REGNO (base)], reg_names[REGNO (index)]);
2506           /* Not sure this can happen, but leave in for now.  */
2507           else if (GET_CODE (index) == SYMBOL_REF)
2508             {
2509               output_addr_const (file, index);
2510               fputc (',', file);
2511               fputs (reg_names[REGNO (base)], file);
2512             }
2513           else
2514             fatal_insn ("bad address", addr);
2515         }
2516       else if (GET_CODE (base) == LO_SUM)
2517         {
2518           if (index != 0
2519               || GET_CODE (XEXP (base, 0)) != REG)
2520             abort ();
2521           if (small_data_operand (XEXP (base, 1), VOIDmode))
2522             fputs ("sda(", file);
2523           else
2524             fputs ("low(", file);
2525           output_addr_const (file, plus_constant (XEXP (base, 1), offset));
2526           fputs ("),", file);
2527           fputs (reg_names[REGNO (XEXP (base, 0))], file);
2528         }
2529       else
2530         fatal_insn ("bad address", addr);
2531       break;
2532
2533     case LO_SUM :
2534       if (GET_CODE (XEXP (addr, 0)) != REG)
2535         fatal_insn ("lo_sum not of register", addr);
2536       if (small_data_operand (XEXP (addr, 1), VOIDmode))
2537         fputs ("sda(", file);
2538       else
2539         fputs ("low(", file);
2540       output_addr_const (file, XEXP (addr, 1));
2541       fputs ("),", file);
2542       fputs (reg_names[REGNO (XEXP (addr, 0))], file);
2543       break;
2544
2545     case PRE_INC :      /* Assume SImode */
2546       fprintf (file, "+%s", reg_names[REGNO (XEXP (addr, 0))]);
2547       break;
2548
2549     case PRE_DEC :      /* Assume SImode */
2550       fprintf (file, "-%s", reg_names[REGNO (XEXP (addr, 0))]);
2551       break;
2552
2553     case POST_INC :     /* Assume SImode */
2554       fprintf (file, "%s+", reg_names[REGNO (XEXP (addr, 0))]);
2555       break;
2556
2557     default :
2558       output_addr_const (file, addr);
2559       break;
2560     }
2561 }
2562
2563 /* Return true if the operands are the constants 0 and 1.  */
2564 int
2565 zero_and_one (operand1, operand2)
2566      rtx operand1;
2567      rtx operand2;
2568 {
2569   return
2570        GET_CODE (operand1) == CONST_INT
2571     && GET_CODE (operand2) == CONST_INT
2572     && (  ((INTVAL (operand1) == 0) && (INTVAL (operand2) == 1))
2573         ||((INTVAL (operand1) == 1) && (INTVAL (operand2) == 0)));
2574 }
2575
2576 /* Return nonzero if the operand is suitable for use in a conditional move sequence.  */
2577 int
2578 conditional_move_operand (operand, mode)
2579      rtx operand;
2580      enum machine_mode mode;
2581 {
2582   /* Only defined for simple integers so far... */
2583   if (mode != SImode && mode != HImode && mode != QImode)
2584     return FALSE;
2585
2586   /* At the moment we can handle moving registers and loading constants.  */
2587   /* To be added: Addition/subtraction/bitops/multiplication of registers.  */
2588
2589   switch (GET_CODE (operand))
2590     {
2591     case REG:
2592       return 1;
2593
2594     case CONST_INT:
2595       return INT8_P (INTVAL (operand));
2596
2597     default:
2598 #if 0
2599       fprintf (stderr, "Test for cond move op of type: %s\n",
2600                GET_RTX_NAME (GET_CODE (operand)));
2601 #endif
2602       return 0;
2603     }
2604 }
2605
2606 /* Return true if the code is a test of the carry bit */
2607 int
2608 carry_compare_operand (op, mode)
2609      rtx op;
2610      enum machine_mode mode ATTRIBUTE_UNUSED;
2611 {
2612   rtx x;
2613
2614   if (GET_MODE (op) != CCmode && GET_MODE (op) != VOIDmode)
2615     return FALSE;
2616
2617   if (GET_CODE (op) != NE && GET_CODE (op) != EQ)
2618     return FALSE;
2619
2620   x = XEXP (op, 0);
2621   if (GET_CODE (x) != REG || REGNO (x) != CARRY_REGNUM)
2622     return FALSE;
2623
2624   x = XEXP (op, 1);
2625   if (GET_CODE (x) != CONST_INT || INTVAL (x) != 0)
2626     return FALSE;
2627
2628   return TRUE;
2629 }
2630
2631 /* Generate the correct assembler code to handle the conditional loading of a
2632    value into a register.  It is known that the operands satisfy the
2633    conditional_move_operand() function above.  The destination is operand[0].
2634    The condition is operand [1].  The 'true' value is operand [2] and the
2635    'false' value is operand [3].  */
2636 char *
2637 emit_cond_move (operands, insn)
2638      rtx * operands;
2639      rtx   insn ATTRIBUTE_UNUSED;
2640 {
2641   static char buffer [100];
2642   const char * dest = reg_names [REGNO (operands [0])];
2643   
2644   buffer [0] = 0;
2645   
2646   /* Destination must be a register.  */
2647   if (GET_CODE (operands [0]) != REG)
2648     abort();
2649   if (! conditional_move_operand (operands [2], SImode))
2650     abort();
2651   if (! conditional_move_operand (operands [3], SImode))
2652     abort();
2653       
2654   /* Check to see if the test is reversed.  */
2655   if (GET_CODE (operands [1]) == NE)
2656     {
2657       rtx tmp = operands [2];
2658       operands [2] = operands [3];
2659       operands [3] = tmp;
2660     }
2661
2662   sprintf (buffer, "mvfc %s, cbr", dest);
2663
2664   /* If the true value was '0' then we need to invert the results of the move.  */
2665   if (INTVAL (operands [2]) == 0)
2666     sprintf (buffer + strlen (buffer), "\n\txor3 %s, %s, #1",
2667              dest, dest);
2668
2669   return buffer;
2670 }
2671
2672 /* Returns true if the registers contained in the two
2673    rtl expressions are different. */
2674 int
2675 m32r_not_same_reg (a, b)
2676      rtx a;
2677      rtx b;
2678 {
2679   int reg_a = -1;
2680   int reg_b = -2;
2681   
2682   while (GET_CODE (a) == SUBREG)
2683     a = SUBREG_REG (a);
2684   
2685   if (GET_CODE (a) == REG)
2686     reg_a = REGNO (a);
2687   
2688   while (GET_CODE (b) == SUBREG)
2689     b = SUBREG_REG (b);
2690   
2691   if (GET_CODE (b) == REG)
2692     reg_b = REGNO (b);
2693   
2694   return reg_a != reg_b;
2695 }
2696
2697 \f
2698 /* Use a library function to move some bytes.  */
2699 static void
2700 block_move_call (dest_reg, src_reg, bytes_rtx)
2701      rtx dest_reg;
2702      rtx src_reg;
2703      rtx bytes_rtx;
2704 {
2705   /* We want to pass the size as Pmode, which will normally be SImode
2706      but will be DImode if we are using 64 bit longs and pointers.  */
2707   if (GET_MODE (bytes_rtx) != VOIDmode
2708       && GET_MODE (bytes_rtx) != Pmode)
2709     bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
2710
2711 #ifdef TARGET_MEM_FUNCTIONS
2712   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "memcpy"), 0,
2713                      VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
2714                      convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
2715                                       TREE_UNSIGNED (sizetype)),
2716                      TYPE_MODE (sizetype));
2717 #else
2718   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "bcopy"), 0,
2719                      VOIDmode, 3, src_reg, Pmode, dest_reg, Pmode,
2720                      convert_to_mode (TYPE_MODE (integer_type_node), bytes_rtx,
2721                                       TREE_UNSIGNED (integer_type_node)),
2722                      TYPE_MODE (integer_type_node));
2723 #endif
2724 }
2725
2726 /* The maximum number of bytes to copy using pairs of load/store instructions.
2727    If a block is larger than this then a loop will be generated to copy
2728    MAX_MOVE_BYTES chunks at a time.  The value of 32 is a semi-arbitrary choice.
2729    A customer uses Dhrystome as their benchmark, and Dhrystone has a 31 byte
2730    string copy in it.  */
2731 #define MAX_MOVE_BYTES 32
2732
2733 /* Expand string/block move operations.
2734
2735    operands[0] is the pointer to the destination.
2736    operands[1] is the pointer to the source.
2737    operands[2] is the number of bytes to move.
2738    operands[3] is the alignment.  */
2739
2740 void
2741 m32r_expand_block_move (operands)
2742      rtx operands[];
2743 {
2744   rtx           orig_dst  = operands[0];
2745   rtx           orig_src  = operands[1];
2746   rtx           bytes_rtx = operands[2];
2747   rtx           align_rtx = operands[3];
2748   int           constp    = GET_CODE (bytes_rtx) == CONST_INT;
2749   HOST_WIDE_INT bytes     = constp ? INTVAL (bytes_rtx) : 0;
2750   int           align     = INTVAL (align_rtx);
2751   int           leftover;
2752   rtx           src_reg;
2753   rtx           dst_reg;
2754
2755   if (constp && bytes <= 0)
2756     return;
2757
2758   /* Move the address into scratch registers.  */
2759   dst_reg = copy_addr_to_reg (XEXP (orig_dst, 0));
2760   src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
2761
2762   if (align > UNITS_PER_WORD)
2763     align = UNITS_PER_WORD;
2764
2765   /* If we prefer size over speed, always use a function call.
2766      If we do not know the size, use a function call.
2767      If the blocks are not word aligned, use a function call.  */
2768   if (optimize_size || ! constp || align != UNITS_PER_WORD)
2769     {
2770       block_move_call (dst_reg, src_reg, bytes_rtx);
2771       return;
2772     }
2773
2774   leftover = bytes % MAX_MOVE_BYTES;
2775   bytes   -= leftover;
2776   
2777   /* If necessary, generate a loop to handle the bulk of the copy.  */
2778   if (bytes)
2779     {
2780       rtx label = NULL_RTX;
2781       rtx final_src = NULL_RTX;
2782       rtx at_a_time = GEN_INT (MAX_MOVE_BYTES);
2783       rtx rounded_total = GEN_INT (bytes);
2784
2785       /* If we are going to have to perform this loop more than
2786          once, then generate a label and compute the address the
2787          source register will contain upon completion of the final
2788          iteration.  */
2789       if (bytes > MAX_MOVE_BYTES)
2790         {
2791           final_src = gen_reg_rtx (Pmode);
2792
2793           if (INT16_P(bytes))
2794             emit_insn (gen_addsi3 (final_src, src_reg, rounded_total));
2795           else
2796             {
2797               emit_insn (gen_movsi (final_src, rounded_total));
2798               emit_insn (gen_addsi3 (final_src, final_src, src_reg));
2799             }
2800
2801           label = gen_label_rtx ();
2802           emit_label (label);
2803         }
2804
2805       /* It is known that output_block_move() will update src_reg to point
2806          to the word after the end of the source block, and dst_reg to point
2807          to the last word of the destination block, provided that the block
2808          is MAX_MOVE_BYTES long.  */
2809       emit_insn (gen_movstrsi_internal (dst_reg, src_reg, at_a_time));
2810       emit_insn (gen_addsi3 (dst_reg, dst_reg, GEN_INT (4)));
2811       
2812       if (bytes > MAX_MOVE_BYTES)
2813         {
2814           emit_insn (gen_cmpsi (src_reg, final_src));
2815           emit_jump_insn (gen_bne (label));
2816         }
2817     }
2818
2819   if (leftover)
2820     emit_insn (gen_movstrsi_internal (dst_reg, src_reg, GEN_INT (leftover)));
2821 }
2822
2823 \f
2824 /* Emit load/stores for a small constant word aligned block_move. 
2825
2826    operands[0] is the memory address of the destination.
2827    operands[1] is the memory address of the source.
2828    operands[2] is the number of bytes to move.
2829    operands[3] is a temp register.
2830    operands[4] is a temp register.  */
2831
2832 void
2833 m32r_output_block_move (insn, operands)
2834      rtx insn ATTRIBUTE_UNUSED;
2835      rtx operands[];
2836 {
2837   HOST_WIDE_INT bytes = INTVAL (operands[2]);
2838   int           first_time;
2839   int           got_extra = 0;
2840   
2841   if (bytes < 1 || bytes > MAX_MOVE_BYTES)
2842     abort ();
2843   
2844   /* We do not have a post-increment store available, so the first set of
2845      stores are done without any increment, then the remaining ones can use
2846      the pre-increment addressing mode.
2847      
2848      Note: expand_block_move() also relies upon this behavior when building
2849      loops to copy large blocks.  */
2850   first_time = 1;
2851   
2852   while (bytes > 0)
2853     {
2854       if (bytes >= 8)
2855         {
2856           if (first_time)
2857             {
2858               output_asm_insn ("ld\t%3, %p1", operands);
2859               output_asm_insn ("ld\t%4, %p1", operands);
2860               output_asm_insn ("st\t%3, @%0", operands);
2861               output_asm_insn ("st\t%4, %s0", operands);
2862             }
2863           else
2864             {
2865               output_asm_insn ("ld\t%3, %p1", operands);
2866               output_asm_insn ("ld\t%4, %p1", operands);
2867               output_asm_insn ("st\t%3, %s0", operands);
2868               output_asm_insn ("st\t%4, %s0", operands);
2869             }
2870
2871           bytes -= 8;
2872         }
2873       else if (bytes >= 4)
2874         {
2875           if (bytes > 4)
2876             got_extra = 1;
2877           
2878           output_asm_insn ("ld\t%3, %p1", operands);
2879           
2880           if (got_extra)
2881             output_asm_insn ("ld\t%4, %p1", operands);
2882                 
2883           if (first_time)
2884             output_asm_insn ("st\t%3, @%0", operands);
2885           else
2886             output_asm_insn ("st\t%3, %s0", operands);
2887
2888           bytes -= 4;
2889         }
2890       else 
2891         {
2892           /* Get the entire next word, even though we do not want all of it.
2893              The saves us from doing several smaller loads, and we assume that
2894              we cannot cause a page fault when at least part of the word is in
2895              valid memory [since we don't get called if things aren't properly
2896              aligned].  */
2897           int dst_offset = first_time ? 0 : 4;
2898           int last_shift;
2899           rtx my_operands[3];
2900
2901           /* If got_extra is true then we have already loaded
2902              the next word as part of loading and storing the previous word.  */
2903           if (! got_extra)
2904             output_asm_insn ("ld\t%4, @%1", operands);
2905
2906           if (bytes >= 2)
2907             {
2908               bytes -= 2;
2909
2910               output_asm_insn ("sra3\t%3, %4, #16", operands);
2911               my_operands[0] = operands[3];
2912               my_operands[1] = GEN_INT (dst_offset);
2913               my_operands[2] = operands[0];
2914               output_asm_insn ("sth\t%0, @(%1,%2)", my_operands);
2915               
2916               /* If there is a byte left to store then increment the
2917                  destination address and shift the contents of the source
2918                  register down by 8 bits.  We could not do the address
2919                  increment in the store half word instruction, because it does
2920                  not have an auto increment mode.  */
2921               if (bytes > 0)  /* assert (bytes == 1) */
2922                 {
2923                   dst_offset += 2;
2924                   last_shift = 8;
2925                 }
2926             }
2927           else
2928             last_shift = 24;
2929
2930           if (bytes > 0)
2931             {
2932               my_operands[0] = operands[4];
2933               my_operands[1] = GEN_INT (last_shift);
2934               output_asm_insn ("srai\t%0, #%1", my_operands);
2935               my_operands[0] = operands[4];
2936               my_operands[1] = GEN_INT (dst_offset);
2937               my_operands[2] = operands[0];
2938               output_asm_insn ("stb\t%0, @(%1,%2)", my_operands);
2939             }
2940           
2941           bytes = 0;
2942         }
2943
2944       first_time = 0;
2945     }
2946 }
2947
2948 /* Return true if op is an integer constant, less than or equal to
2949    MAX_MOVE_BYTES.  */
2950 int
2951 m32r_block_immediate_operand (op, mode)
2952      rtx op;
2953      enum machine_mode mode ATTRIBUTE_UNUSED;
2954 {
2955   if (GET_CODE (op) != CONST_INT
2956       || INTVAL (op) > MAX_MOVE_BYTES
2957       || INTVAL (op) <= 0)
2958     return 0;
2959
2960   return 1;
2961 }
2962
2963 /* Return true if using NEW_REG in place of OLD_REG is ok.  */
2964
2965 int
2966 m32r_hard_regno_rename_ok (old_reg, new_reg)
2967      unsigned int old_reg ATTRIBUTE_UNUSED;
2968      unsigned int new_reg;
2969 {
2970   /* Interrupt routines can't clobber any register that isn't already used.  */
2971   if (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl))
2972       && !regs_ever_live[new_reg])
2973     return 0;
2974
2975   /* We currently emit epilogues as text, not rtl, so the liveness
2976      of the return address register isn't visible.  */
2977   if (current_function_is_leaf && new_reg == RETURN_ADDR_REGNUM)
2978     return 0;
2979
2980   return 1;
2981 }