OSDN Git Service

* config/s390/2064.md: Fix comment typos.
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Renesas / SuperH SH.
2    Copyright (C) 1993, 1994, 1995, 1997, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003 Free Software Foundation, Inc.
4    Contributed by Steve Chamberlain (sac@cygnus.com).
5    Improved by Jim Wilson (wilson@cygnus.com). 
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "insn-config.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "expr.h"
33 #include "optabs.h"
34 #include "function.h"
35 #include "regs.h"
36 #include "hard-reg-set.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "toplev.h"
40 #include "recog.h"
41 #include "c-pragma.h"
42 #include "integrate.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "target-def.h"
46 #include "real.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "ra.h"
50 #include "cfglayout.h"
51
52 int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
53
54 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
55 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
56
57 /* These are some macros to abstract register modes.  */
58 #define CONST_OK_FOR_ADD(size) \
59   (TARGET_SHMEDIA ? CONST_OK_FOR_I10 (size) : CONST_OK_FOR_I08 (size))
60 #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
61 #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
62 #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
63
64 /* Set to 1 by expand_prologue() when the function is an interrupt handler.  */
65 int current_function_interrupt;
66
67 /* ??? The pragma interrupt support will not work for SH3.  */
68 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
69    output code for the next function appropriate for an interrupt handler.  */
70 int pragma_interrupt;
71
72 /* This is set by the trap_exit attribute for functions.   It specifies
73    a trap number to be used in a trapa instruction at function exit
74    (instead of an rte instruction).  */
75 int trap_exit;
76
77 /* This is used by the sp_switch attribute for functions.  It specifies
78    a variable holding the address of the stack the interrupt function
79    should switch to/from at entry/exit.  */
80 rtx sp_switch;
81
82 /* This is set by #pragma trapa, and is similar to the above, except that
83    the compiler doesn't emit code to preserve all registers.  */
84 static int pragma_trapa;
85
86 /* This is set by #pragma nosave_low_regs.  This is useful on the SH3,
87    which has a separate set of low regs for User and Supervisor modes.
88    This should only be used for the lowest level of interrupts.  Higher levels
89    of interrupts must save the registers in case they themselves are
90    interrupted.  */
91 int pragma_nosave_low_regs;
92
93 /* This is used for communication between SETUP_INCOMING_VARARGS and
94    sh_expand_prologue.  */
95 int current_function_anonymous_args;
96
97 /* Global variables for machine-dependent things.  */
98
99 /* Which cpu are we scheduling for.  */
100 enum processor_type sh_cpu;
101
102 /* Saved operands from the last compare to use when we generate an scc
103    or bcc insn.  */
104
105 rtx sh_compare_op0;
106 rtx sh_compare_op1;
107
108 /* Provides the class number of the smallest class containing
109    reg number.  */
110
111 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
112 {
113   R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
114   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
115   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
116   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
117   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
118   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
119   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
120   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
121   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
122   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
123   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
124   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
125   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
126   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
127   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
128   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
129   FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
130   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
131   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
132   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
133   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
134   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
135   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
136   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
137   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
138   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
139   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
140   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
141   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
142   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
143   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
144   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
145   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
146   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
147   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
148   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
149   NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
150   MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
151   GENERAL_REGS,
152 };
153
154 char sh_register_names[FIRST_PSEUDO_REGISTER] \
155   [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
156
157 char sh_additional_register_names[ADDREGNAMES_SIZE] \
158   [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
159   = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
160
161 /* Provide reg_class from a letter such as appears in the machine
162    description.  *: target independently reserved letter.
163    reg_class_from_letter['e' - 'a'] is set to NO_REGS for TARGET_FMOVD.  */
164
165 enum reg_class reg_class_from_letter[] =
166 {
167   /* a */ ALL_REGS,  /* b */ TARGET_REGS, /* c */ FPSCR_REGS, /* d */ DF_REGS,
168   /* e */ FP_REGS,   /* f */ FP_REGS,  /* g **/ NO_REGS,     /* h */ NO_REGS,
169   /* i **/ NO_REGS,  /* j */ NO_REGS,  /* k */ SIBCALL_REGS, /* l */ PR_REGS,
170   /* m **/ NO_REGS,  /* n **/ NO_REGS, /* o **/ NO_REGS,     /* p **/ NO_REGS,
171   /* q */ NO_REGS,   /* r **/ NO_REGS, /* s **/ NO_REGS,     /* t */ T_REGS,
172   /* u */ NO_REGS,   /* v */ NO_REGS,  /* w */ FP0_REGS,     /* x */ MAC_REGS,
173   /* y */ FPUL_REGS, /* z */ R0_REGS
174 };
175
176 int assembler_dialect;
177
178 static bool shmedia_space_reserved_for_target_registers;
179
180 static void split_branches PARAMS ((rtx));
181 static int branch_dest PARAMS ((rtx));
182 static void force_into PARAMS ((rtx, rtx));
183 static void print_slot PARAMS ((rtx));
184 static rtx add_constant PARAMS ((rtx, enum machine_mode, rtx));
185 static void dump_table PARAMS ((rtx));
186 static int hi_const PARAMS ((rtx));
187 static int broken_move PARAMS ((rtx));
188 static int mova_p PARAMS ((rtx));
189 static rtx find_barrier PARAMS ((int, rtx, rtx));
190 static int noncall_uses_reg PARAMS ((rtx, rtx, rtx *));
191 static rtx gen_block_redirect PARAMS ((rtx, int, int));
192 static void sh_reorg PARAMS ((void));
193 static void output_stack_adjust PARAMS ((int, rtx, int, rtx (*) (rtx)));
194 static rtx frame_insn PARAMS ((rtx));
195 static rtx push PARAMS ((int));
196 static void pop PARAMS ((int));
197 static void push_regs PARAMS ((HARD_REG_SET *, int));
198 static int calc_live_regs PARAMS ((HARD_REG_SET *));
199 static void mark_use PARAMS ((rtx, rtx *));
200 static HOST_WIDE_INT rounded_frame_size PARAMS ((int));
201 static rtx mark_constant_pool_use PARAMS ((rtx));
202 const struct attribute_spec sh_attribute_table[];
203 static tree sh_handle_interrupt_handler_attribute PARAMS ((tree *, tree, tree, int, bool *));
204 static tree sh_handle_sp_switch_attribute PARAMS ((tree *, tree, tree, int, bool *));
205 static tree sh_handle_trap_exit_attribute PARAMS ((tree *, tree, tree, int, bool *));
206 static void sh_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
207 static void sh_insert_attributes PARAMS ((tree, tree *));
208 static int sh_adjust_cost PARAMS ((rtx, rtx, rtx, int));
209 static int sh_use_dfa_interface PARAMS ((void));
210 static int sh_issue_rate PARAMS ((void));
211 static bool sh_function_ok_for_sibcall PARAMS ((tree, tree));
212
213 static bool sh_cannot_modify_jumps_p PARAMS ((void));
214 static int sh_target_reg_class (void);
215 static bool sh_optimize_target_register_callee_saved (bool);
216 static bool sh_ms_bitfield_layout_p PARAMS ((tree));
217
218 static void sh_init_builtins PARAMS ((void));
219 static void sh_media_init_builtins PARAMS ((void));
220 static rtx sh_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
221 static void sh_output_mi_thunk PARAMS ((FILE *, tree, HOST_WIDE_INT,
222                                         HOST_WIDE_INT, tree));
223 static void sh_file_start PARAMS ((void));
224 static int flow_dependent_p PARAMS ((rtx, rtx));
225 static void flow_dependent_p_1 PARAMS ((rtx, rtx, void *));
226 static int shiftcosts PARAMS ((rtx));
227 static int andcosts PARAMS ((rtx));
228 static int addsubcosts PARAMS ((rtx));
229 static int multcosts PARAMS ((rtx));
230 static bool unspec_caller_rtx_p PARAMS ((rtx));
231 static bool sh_cannot_copy_insn_p PARAMS ((rtx));
232 static bool sh_rtx_costs PARAMS ((rtx, int, int, int *));
233 static int sh_address_cost PARAMS ((rtx));
234 static int shmedia_target_regs_stack_space (HARD_REG_SET *);
235 static int shmedia_reserve_space_for_target_registers_p (int, HARD_REG_SET *);
236 static int shmedia_target_regs_stack_adjust (HARD_REG_SET *);
237 \f
238 /* Initialize the GCC target structure.  */
239 #undef TARGET_ATTRIBUTE_TABLE
240 #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
241
242 /* The next two are used for debug info when compiling with -gdwarf.  */
243 #undef TARGET_ASM_UNALIGNED_HI_OP
244 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
245 #undef TARGET_ASM_UNALIGNED_SI_OP
246 #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
247
248 /* These are NULLed out on non-SH5 in OVERRIDE_OPTIONS.  */
249 #undef TARGET_ASM_UNALIGNED_DI_OP
250 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
251 #undef TARGET_ASM_ALIGNED_DI_OP
252 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
253
254 #undef TARGET_ASM_FUNCTION_EPILOGUE
255 #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
256
257 #undef TARGET_ASM_OUTPUT_MI_THUNK
258 #define TARGET_ASM_OUTPUT_MI_THUNK sh_output_mi_thunk
259
260 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
261 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
262
263 #undef TARGET_ASM_FILE_START
264 #define TARGET_ASM_FILE_START sh_file_start
265 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
266 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
267
268 #undef TARGET_INSERT_ATTRIBUTES
269 #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
270
271 #undef TARGET_SCHED_ADJUST_COST
272 #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
273
274 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
275 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE \
276                                 sh_use_dfa_interface
277 #undef TARGET_SCHED_ISSUE_RATE
278 #define TARGET_SCHED_ISSUE_RATE sh_issue_rate
279
280 #undef TARGET_CANNOT_MODIFY_JUMPS_P
281 #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
282 #undef TARGET_BRANCH_TARGET_REGISTER_CLASS
283 #define TARGET_BRANCH_TARGET_REGISTER_CLASS sh_target_reg_class
284 #undef TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED
285 #define TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED \
286  sh_optimize_target_register_callee_saved
287
288 #undef TARGET_MS_BITFIELD_LAYOUT_P
289 #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
290
291 #undef TARGET_INIT_BUILTINS
292 #define TARGET_INIT_BUILTINS sh_init_builtins
293 #undef TARGET_EXPAND_BUILTIN
294 #define TARGET_EXPAND_BUILTIN sh_expand_builtin
295
296 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
297 #define TARGET_FUNCTION_OK_FOR_SIBCALL sh_function_ok_for_sibcall
298
299 #undef TARGET_CANNOT_COPY_INSN_P
300 #define TARGET_CANNOT_COPY_INSN_P sh_cannot_copy_insn_p
301 #undef TARGET_RTX_COSTS
302 #define TARGET_RTX_COSTS sh_rtx_costs
303 #undef TARGET_ADDRESS_COST
304 #define TARGET_ADDRESS_COST sh_address_cost
305
306 #undef TARGET_MACHINE_DEPENDENT_REORG
307 #define TARGET_MACHINE_DEPENDENT_REORG sh_reorg
308
309 #ifdef HAVE_AS_TLS
310 #undef TARGET_HAVE_TLS
311 #define TARGET_HAVE_TLS true
312 #endif
313
314 struct gcc_target targetm = TARGET_INITIALIZER;
315 \f
316 /* Print the operand address in x to the stream.  */
317
318 void
319 print_operand_address (stream, x)
320      FILE *stream;
321      rtx x;
322 {
323   switch (GET_CODE (x))
324     {
325     case REG:
326     case SUBREG:
327       fprintf (stream, "@%s", reg_names[true_regnum (x)]);
328       break;
329
330     case PLUS:
331       {
332         rtx base = XEXP (x, 0);
333         rtx index = XEXP (x, 1);
334
335         switch (GET_CODE (index))
336           {
337           case CONST_INT:
338             fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
339                      reg_names[true_regnum (base)]);
340             break;
341
342           case REG:
343           case SUBREG:
344             {
345               int base_num = true_regnum (base);
346               int index_num = true_regnum (index);
347
348               fprintf (stream, "@(r0,%s)",
349                        reg_names[MAX (base_num, index_num)]);
350               break;
351             }
352
353           default:
354             debug_rtx (x);
355             abort ();
356           }
357       }
358       break;
359
360     case PRE_DEC:
361       fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
362       break;
363
364     case POST_INC:
365       fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
366       break;
367
368     default:
369       x = mark_constant_pool_use (x);
370       output_addr_const (stream, x);
371       break;
372     }
373 }
374
375 /* Print operand x (an rtx) in assembler syntax to file stream
376    according to modifier code.
377
378    '.'  print a .s if insn needs delay slot
379    ','  print LOCAL_LABEL_PREFIX
380    '@'  print trap, rte or rts depending upon pragma interruptness
381    '#'  output a nop if there is nothing to put in the delay slot
382    '''  print likelihood suffix (/u for unlikely).
383    'O'  print a constant without the #
384    'R'  print the LSW of a dp value - changes if in little endian
385    'S'  print the MSW of a dp value - changes if in little endian
386    'T'  print the next word of a dp value - same as 'R' in big endian mode.
387    'M'  print an `x' if `m' will print `base,index'.
388    'N'  print 'r63' if the operand is (const_int 0).
389    'm'  print a pair `base,offset' or `base,index', for LD and ST.
390    'u'  prints the lowest 16 bits of CONST_INT, as an unsigned value.
391    'o'  output an operator.  */
392
393 void
394 print_operand (stream, x, code)
395      FILE *stream;
396      rtx x;
397      int code;
398 {
399   switch (code)
400     {
401     case '.':
402       if (final_sequence
403           && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
404           && get_attr_length (XVECEXP (final_sequence, 0, 1)))
405         fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
406       break;
407     case ',':
408       fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
409       break;
410     case '@':
411       if (trap_exit)
412         fprintf (stream, "trapa #%d", trap_exit);
413       else if (sh_cfun_interrupt_handler_p ())
414         fprintf (stream, "rte");
415       else
416         fprintf (stream, "rts");
417       break;
418     case '#':
419       /* Output a nop if there's nothing in the delay slot.  */
420       if (dbr_sequence_length () == 0)
421         fprintf (stream, "\n\tnop");
422       break;
423     case '\'':
424       {
425         rtx note = find_reg_note (current_output_insn, REG_BR_PROB, 0);
426
427         if (note && INTVAL (XEXP (note, 0)) * 2 < REG_BR_PROB_BASE)
428           fputs ("/u", stream);
429         break;
430       }
431     case 'O':
432       x = mark_constant_pool_use (x);
433       output_addr_const (stream, x);
434       break;
435     case 'R':
436       fputs (reg_names[REGNO (x) + LSW], (stream));
437       break;
438     case 'S':
439       fputs (reg_names[REGNO (x) + MSW], (stream));
440       break;
441     case 'T':
442       /* Next word of a double.  */
443       switch (GET_CODE (x))
444         {
445         case REG:
446           fputs (reg_names[REGNO (x) + 1], (stream));
447           break;
448         case MEM:
449           if (GET_CODE (XEXP (x, 0)) != PRE_DEC
450               && GET_CODE (XEXP (x, 0)) != POST_INC)
451             x = adjust_address (x, SImode, 4);
452           print_operand_address (stream, XEXP (x, 0));
453           break;
454         default:
455           break;
456         }
457       break;
458     case 'o':
459       switch (GET_CODE (x))
460         {
461         case PLUS:  fputs ("add", stream); break;
462         case MINUS: fputs ("sub", stream); break;
463         case MULT:  fputs ("mul", stream); break;
464         case DIV:   fputs ("div", stream); break;
465         case EQ:    fputs ("eq",  stream); break;
466         case NE:    fputs ("ne",  stream); break;
467         case GT:  case LT:  fputs ("gt",  stream); break;
468         case GE:  case LE:  fputs ("ge",  stream); break;
469         case GTU: case LTU: fputs ("gtu", stream); break;
470         case GEU: case LEU: fputs ("geu", stream); break;
471         default:
472           break;
473         }
474       break;
475     case 'M':
476       if (GET_CODE (x) == MEM
477           && GET_CODE (XEXP (x, 0)) == PLUS
478           && (GET_CODE (XEXP (XEXP (x, 0), 1)) == REG
479               || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
480         fputc ('x', stream);
481       break;
482
483     case 'm':
484       if (GET_CODE (x) != MEM)
485         abort ();
486       x = XEXP (x, 0);
487       switch (GET_CODE (x))
488         {
489         case REG:
490         case SUBREG:
491           print_operand (stream, x, 0);
492           fputs (", 0", stream);
493           break;
494
495         case PLUS:
496           print_operand (stream, XEXP (x, 0), 0);
497           fputs (", ", stream);
498           print_operand (stream, XEXP (x, 1), 0);
499           break;
500
501         default:
502           abort ();
503         }
504       break;
505
506     case 'N':
507       if (x == CONST0_RTX (GET_MODE (x)))
508         {
509           fprintf ((stream), "r63");
510           break;
511         }
512       goto default_output;
513     case 'u':
514       if (GET_CODE (x) == CONST_INT)
515         {
516           fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
517           break;
518         }
519       /* Fall through.  */
520
521     default_output:
522     default:
523       switch (GET_CODE (x))
524         {
525           /* FIXME: We need this on SHmedia32 because reload generates
526              some sign-extended HI or QI loads into DImode registers
527              but, because Pmode is SImode, the address ends up with a
528              subreg:SI of the DImode register.  Maybe reload should be
529              fixed so as to apply alter_subreg to such loads?  */
530         case SUBREG:
531           if (SUBREG_BYTE (x) != 0
532               || GET_CODE (SUBREG_REG (x)) != REG)
533             abort ();
534
535           x = SUBREG_REG (x);
536           /* Fall through.  */
537
538         case REG:
539           if (FP_REGISTER_P (REGNO (x))
540               && GET_MODE (x) == V16SFmode)
541             fprintf ((stream), "mtrx%s", reg_names[REGNO (x)] + 2);
542           else if (FP_REGISTER_P (REGNO (x))
543                    && GET_MODE (x) == V4SFmode)
544             fprintf ((stream), "fv%s", reg_names[REGNO (x)] + 2);
545           else if (GET_CODE (x) == REG
546                    && GET_MODE (x) == V2SFmode)
547             fprintf ((stream), "fp%s", reg_names[REGNO (x)] + 2);
548           else if (FP_REGISTER_P (REGNO (x))
549                    && GET_MODE_SIZE (GET_MODE (x)) > 4)
550             fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
551           else
552             fputs (reg_names[REGNO (x)], (stream));
553           break;
554
555         case MEM:
556           output_address (XEXP (x, 0));
557           break;
558           
559         case CONST:
560           if (TARGET_SHMEDIA
561               && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
562               && GET_MODE (XEXP (x, 0)) == DImode
563               && GET_CODE (XEXP (XEXP (x, 0), 0)) == TRUNCATE
564               && GET_MODE (XEXP (XEXP (x, 0), 0)) == HImode)
565             {
566               rtx val = XEXP (XEXP (XEXP (x, 0), 0), 0);
567
568               fputc ('(', stream);
569               if (GET_CODE (val) == ASHIFTRT)
570                 {
571                   fputc ('(', stream);
572                   if (GET_CODE (XEXP (val, 0)) == CONST)
573                     fputc ('(', stream);
574                   output_addr_const (stream, XEXP (val, 0));
575                   if (GET_CODE (XEXP (val, 0)) == CONST)
576                     fputc (')', stream);
577                   fputs (" >> ", stream);
578                   output_addr_const (stream, XEXP (val, 1));
579                   fputc (')', stream);
580                 }
581               else
582                 {
583                   if (GET_CODE (val) == CONST)
584                     fputc ('(', stream);
585                   output_addr_const (stream, val);
586                   if (GET_CODE (val) == CONST)
587                     fputc (')', stream);
588                 }
589               fputs (" & 65535)", stream);
590               break;
591             }
592
593           /* Fall through.  */
594         default:
595           if (TARGET_SH1)
596             fputc ('#', stream);
597           output_addr_const (stream, x);
598           break;
599         }
600       break;
601     }
602 }
603 \f
604 /* Like force_operand, but guarantees that VALUE ends up in TARGET.  */
605 static void
606 force_into (value, target)
607      rtx value, target;
608 {
609   value = force_operand (value, target);
610   if (! rtx_equal_p (value, target))
611     emit_insn (gen_move_insn (target, value));
612 }
613
614 /* Emit code to perform a block move.  Choose the best method.
615
616    OPERANDS[0] is the destination.
617    OPERANDS[1] is the source.
618    OPERANDS[2] is the size.
619    OPERANDS[3] is the alignment safe to use.  */
620
621 int
622 expand_block_move (operands)
623      rtx *operands;
624 {
625   int align = INTVAL (operands[3]);
626   int constp = (GET_CODE (operands[2]) == CONST_INT);
627   int bytes = (constp ? INTVAL (operands[2]) : 0);
628
629   /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
630      alignment, or if it isn't a multiple of 4 bytes, then fail.  */
631   if (! constp || align < 4 || (bytes % 4 != 0))
632     return 0;
633
634   if (TARGET_HARD_SH4)
635     {
636       if (bytes < 12)
637         return 0;
638       else if (bytes == 12)
639         {
640           tree entry_name;
641           rtx sym;
642           rtx func_addr_rtx;
643           rtx r4 = gen_rtx (REG, SImode, 4);
644           rtx r5 = gen_rtx (REG, SImode, 5);
645
646           entry_name = get_identifier ("__movstrSI12_i4");
647
648           sym = function_symbol (IDENTIFIER_POINTER (entry_name));
649           func_addr_rtx = copy_to_mode_reg (Pmode, sym);
650           force_into (XEXP (operands[0], 0), r4);
651           force_into (XEXP (operands[1], 0), r5);
652           emit_insn (gen_block_move_real_i4 (func_addr_rtx));
653           return 1;
654         }
655       else if (! TARGET_SMALLCODE)
656         {
657           tree entry_name;
658           rtx sym;
659           rtx func_addr_rtx;
660           int dwords;
661           rtx r4 = gen_rtx (REG, SImode, 4);
662           rtx r5 = gen_rtx (REG, SImode, 5);
663           rtx r6 = gen_rtx (REG, SImode, 6);
664
665           entry_name = get_identifier (bytes & 4
666                                        ? "__movstr_i4_odd"
667                                        : "__movstr_i4_even");
668           sym = function_symbol (IDENTIFIER_POINTER (entry_name));
669           func_addr_rtx = copy_to_mode_reg (Pmode, sym);
670           force_into (XEXP (operands[0], 0), r4);
671           force_into (XEXP (operands[1], 0), r5);
672
673           dwords = bytes >> 3;
674           emit_insn (gen_move_insn (r6, GEN_INT (dwords - 1)));
675           emit_insn (gen_block_lump_real_i4 (func_addr_rtx));
676           return 1;
677         }
678       else
679         return 0;
680     }
681   if (bytes < 64)
682     {
683       char entry[30];
684       tree entry_name;
685       rtx sym;
686       rtx func_addr_rtx;
687       rtx r4 = gen_rtx_REG (SImode, 4);
688       rtx r5 = gen_rtx_REG (SImode, 5);
689
690       sprintf (entry, "__movstrSI%d", bytes);
691       entry_name = get_identifier (entry);
692       sym = function_symbol (IDENTIFIER_POINTER (entry_name));
693       func_addr_rtx = copy_to_mode_reg (Pmode, sym);
694       force_into (XEXP (operands[0], 0), r4);
695       force_into (XEXP (operands[1], 0), r5);
696       emit_insn (gen_block_move_real (func_addr_rtx));
697       return 1;
698     }
699
700   /* This is the same number of bytes as a memcpy call, but to a different
701      less common function name, so this will occasionally use more space.  */
702   if (! TARGET_SMALLCODE)
703     {
704       tree entry_name;
705       rtx sym;
706       rtx func_addr_rtx;
707       int final_switch, while_loop;
708       rtx r4 = gen_rtx_REG (SImode, 4);
709       rtx r5 = gen_rtx_REG (SImode, 5);
710       rtx r6 = gen_rtx_REG (SImode, 6);
711
712       entry_name = get_identifier ("__movstr");
713       sym = function_symbol (IDENTIFIER_POINTER (entry_name));
714       func_addr_rtx = copy_to_mode_reg (Pmode, sym);
715       force_into (XEXP (operands[0], 0), r4);
716       force_into (XEXP (operands[1], 0), r5);
717
718       /* r6 controls the size of the move.  16 is decremented from it
719          for each 64 bytes moved.  Then the negative bit left over is used
720          as an index into a list of move instructions.  e.g., a 72 byte move
721          would be set up with size(r6) = 14, for one iteration through the
722          big while loop, and a switch of -2 for the last part.  */
723
724       final_switch = 16 - ((bytes / 4) % 16);
725       while_loop = ((bytes / 4) / 16 - 1) * 16;
726       emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
727       emit_insn (gen_block_lump_real (func_addr_rtx));
728       return 1;
729     }
730
731   return 0;
732 }
733
734 /* Prepare operands for a move define_expand; specifically, one of the
735    operands must be in a register.  */
736
737 int
738 prepare_move_operands (operands, mode)
739      rtx operands[];
740      enum machine_mode mode;
741 {
742   if ((mode == SImode || mode == DImode)
743       && flag_pic
744       && ! ((mode == Pmode || mode == ptr_mode)
745             && tls_symbolic_operand (operands[1], Pmode) != 0))
746     {
747       rtx temp;
748       if (SYMBOLIC_CONST_P (operands[1]))
749         {
750           if (GET_CODE (operands[0]) == MEM)
751             operands[1] = force_reg (Pmode, operands[1]);
752           else if (TARGET_SHMEDIA
753                    && GET_CODE (operands[1]) == LABEL_REF
754                    && target_reg_operand (operands[0], mode))
755             /* It's ok.  */;
756           else
757             {
758               temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
759               operands[1] = legitimize_pic_address (operands[1], mode, temp);
760             }
761         }
762       else if (GET_CODE (operands[1]) == CONST
763                && GET_CODE (XEXP (operands[1], 0)) == PLUS
764                && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
765         {
766           temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
767           temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
768                                          mode, temp);
769           operands[1] = expand_binop (mode, add_optab, temp,
770                                       XEXP (XEXP (operands[1], 0), 1),
771                                       no_new_pseudos ? temp
772                                       : gen_reg_rtx (Pmode),
773                                       0, OPTAB_LIB_WIDEN);
774         }
775     }
776
777   if (! reload_in_progress && ! reload_completed)
778     {
779       /* Copy the source to a register if both operands aren't registers.  */
780       if (! register_operand (operands[0], mode)
781           && ! sh_register_operand (operands[1], mode))
782         operands[1] = copy_to_mode_reg (mode, operands[1]);
783
784       if (GET_CODE (operands[0]) == MEM && ! memory_operand (operands[0], mode))
785         {
786           /* This is like change_address_1 (operands[0], mode, 0, 1) ,
787              except that we can't use that function because it is static.  */
788           rtx new = change_address (operands[0], mode, 0);
789           MEM_COPY_ATTRIBUTES (new, operands[0]);
790           operands[0] = new;
791         }
792
793       /* This case can happen while generating code to move the result
794          of a library call to the target.  Reject `st r0,@(rX,rY)' because
795          reload will fail to find a spill register for rX, since r0 is already
796          being used for the source.  */
797       else if (refers_to_regno_p (R0_REG, R0_REG + 1, operands[1], (rtx *)0)
798                && GET_CODE (operands[0]) == MEM
799                && GET_CODE (XEXP (operands[0], 0)) == PLUS
800                && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
801         operands[1] = copy_to_mode_reg (mode, operands[1]);
802     }
803
804   if (mode == Pmode || mode == ptr_mode)
805     {
806       rtx op0, op1;
807       enum tls_model tls_kind;
808
809       op0 = operands[0];
810       op1 = operands[1];
811       if ((tls_kind = tls_symbolic_operand (op1, Pmode)))
812         {
813           rtx tga_op1, tga_ret, tmp, tmp2;
814
815           
816           switch (tls_kind)
817             {
818             case TLS_MODEL_GLOBAL_DYNAMIC:
819               tga_ret = gen_rtx_REG (Pmode, R0_REG);
820               emit_insn (gen_tls_global_dynamic (tga_ret, op1));
821               op1 = tga_ret;
822               break;
823
824             case TLS_MODEL_LOCAL_DYNAMIC:
825               tga_ret = gen_rtx_REG (Pmode, R0_REG);
826               emit_insn (gen_tls_local_dynamic (tga_ret, op1));
827
828               tmp = gen_reg_rtx (Pmode);
829               emit_move_insn (tmp, tga_ret);
830
831               if (register_operand (op0, Pmode))
832                 tmp2 = op0;
833               else
834                 tmp2 = gen_reg_rtx (Pmode);
835
836               emit_insn (gen_symDTPOFF2reg (tmp2, op1, tmp));
837               op1 = tmp2;
838               break;
839
840             case TLS_MODEL_INITIAL_EXEC:
841               if (! flag_pic)
842                 emit_insn (gen_GOTaddr2picreg ());
843               tga_op1 = gen_reg_rtx (Pmode);
844               tmp = gen_sym2GOTTPOFF (op1);
845               emit_insn (gen_tls_initial_exec (tga_op1, tmp));
846               op1 = tga_op1;
847               break;
848
849             case TLS_MODEL_LOCAL_EXEC:
850               tmp2 = gen_reg_rtx (Pmode);
851               emit_insn (gen_load_gbr (tmp2));
852               tmp = gen_reg_rtx (Pmode);
853               emit_insn (gen_symTPOFF2reg (tmp, op1));
854               RTX_UNCHANGING_P (tmp) = 1;
855
856               if (register_operand (op0, Pmode))
857                 op1 = op0;
858               else
859                 op1 = gen_reg_rtx (Pmode);
860
861               emit_insn (gen_addsi3 (op1, tmp, tmp2));
862               break;
863
864             default:
865               abort ();
866             }
867           operands[1] = op1;
868         }
869     }
870
871   return 0;
872 }
873
874 /* Prepare the operands for an scc instruction; make sure that the
875    compare has been done.  */
876 rtx
877 prepare_scc_operands (code)
878      enum rtx_code code;
879 {
880   rtx t_reg = gen_rtx_REG (SImode, T_REG);
881   enum rtx_code oldcode = code;
882   enum machine_mode mode;
883
884   /* First need a compare insn.  */
885   switch (code)
886     {
887     case NE:
888       /* It isn't possible to handle this case.  */
889       abort ();
890     case LT:
891       code = GT;
892       break;
893     case LE:
894       code = GE;
895       break;
896     case LTU:
897       code = GTU;
898       break;
899     case LEU:
900       code = GEU;
901       break;
902     default:
903       break;
904     }
905   if (code != oldcode)
906     {
907       rtx tmp = sh_compare_op0;
908       sh_compare_op0 = sh_compare_op1;
909       sh_compare_op1 = tmp;
910     }
911
912   mode = GET_MODE (sh_compare_op0);
913   if (mode == VOIDmode)
914     mode = GET_MODE (sh_compare_op1);
915
916   sh_compare_op0 = force_reg (mode, sh_compare_op0);
917   if ((code != EQ && code != NE
918        && (sh_compare_op1 != const0_rtx
919            || code == GTU  || code == GEU || code == LTU || code == LEU))
920       || (mode == DImode && sh_compare_op1 != const0_rtx)
921       || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
922     sh_compare_op1 = force_reg (mode, sh_compare_op1);
923
924   if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
925     (mode == SFmode ? emit_sf_insn : emit_df_insn)
926      (gen_rtx (PARALLEL, VOIDmode, gen_rtvec (2,
927                 gen_rtx (SET, VOIDmode, t_reg,
928                          gen_rtx (code, SImode,
929                                   sh_compare_op0, sh_compare_op1)),
930                 gen_rtx (USE, VOIDmode, get_fpscr_rtx ()))));
931   else
932     emit_insn (gen_rtx (SET, VOIDmode, t_reg,
933                         gen_rtx (code, SImode, sh_compare_op0,
934                                  sh_compare_op1)));
935
936   return t_reg;
937 }
938
939 /* Called from the md file, set up the operands of a compare instruction.  */
940
941 void
942 from_compare (operands, code)
943      rtx *operands;
944      int code;
945 {
946   enum machine_mode mode = GET_MODE (sh_compare_op0);
947   rtx insn;
948   if (mode == VOIDmode)
949     mode = GET_MODE (sh_compare_op1);
950   if (code != EQ
951       || mode == DImode
952       || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
953     {
954       /* Force args into regs, since we can't use constants here.  */
955       sh_compare_op0 = force_reg (mode, sh_compare_op0);
956       if (sh_compare_op1 != const0_rtx
957           || code == GTU  || code == GEU
958           || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
959         sh_compare_op1 = force_reg (mode, sh_compare_op1);
960     }
961   if (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT && code == GE)
962     {
963       from_compare (operands, GT);
964       insn = gen_ieee_ccmpeqsf_t (sh_compare_op0, sh_compare_op1);
965     }
966   else
967     insn = gen_rtx_SET (VOIDmode,
968                         gen_rtx_REG (SImode, T_REG),
969                         gen_rtx (code, SImode, sh_compare_op0,
970                                  sh_compare_op1));
971   if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
972     {
973       insn = gen_rtx (PARALLEL, VOIDmode,
974                       gen_rtvec (2, insn,
975                                  gen_rtx (USE, VOIDmode, get_fpscr_rtx ())));
976       (mode == SFmode ? emit_sf_insn : emit_df_insn) (insn);
977     }
978   else
979     emit_insn (insn);
980 }
981 \f
982 /* Functions to output assembly code.  */
983
984 /* Return a sequence of instructions to perform DI or DF move.
985
986    Since the SH cannot move a DI or DF in one instruction, we have
987    to take care when we see overlapping source and dest registers.  */
988
989 const char *
990 output_movedouble (insn, operands, mode)
991      rtx insn ATTRIBUTE_UNUSED;
992      rtx operands[];
993      enum machine_mode mode;
994 {
995   rtx dst = operands[0];
996   rtx src = operands[1];
997
998   if (GET_CODE (dst) == MEM
999       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
1000     return "mov.l       %T1,%0\n\tmov.l %1,%0";
1001
1002   if (register_operand (dst, mode)
1003       && register_operand (src, mode))
1004     {
1005       if (REGNO (src) == MACH_REG)
1006         return "sts     mach,%S0\n\tsts macl,%R0";
1007
1008       /* When mov.d r1,r2 do r2->r3 then r1->r2;
1009          when mov.d r1,r0 do r1->r0 then r2->r1.  */
1010
1011       if (REGNO (src) + 1 == REGNO (dst))
1012         return "mov     %T1,%T0\n\tmov  %1,%0";
1013       else
1014         return "mov     %1,%0\n\tmov    %T1,%T0";
1015     }
1016   else if (GET_CODE (src) == CONST_INT)
1017     {
1018       if (INTVAL (src) < 0)
1019         output_asm_insn ("mov   #-1,%S0", operands);
1020       else
1021         output_asm_insn ("mov   #0,%S0", operands);
1022
1023       return "mov       %1,%R0";
1024     }
1025   else if (GET_CODE (src) == MEM)
1026     {
1027       int ptrreg = -1;
1028       int dreg = REGNO (dst);
1029       rtx inside = XEXP (src, 0);
1030
1031       if (GET_CODE (inside) == REG)
1032         ptrreg = REGNO (inside);
1033       else if (GET_CODE (inside) == SUBREG)
1034         ptrreg = subreg_regno (inside);
1035       else if (GET_CODE (inside) == PLUS)
1036         {
1037           ptrreg = REGNO (XEXP (inside, 0));
1038           /* ??? A r0+REG address shouldn't be possible here, because it isn't
1039              an offsettable address.  Unfortunately, offsettable addresses use
1040              QImode to check the offset, and a QImode offsettable address
1041              requires r0 for the other operand, which is not currently
1042              supported, so we can't use the 'o' constraint.
1043              Thus we must check for and handle r0+REG addresses here.
1044              We punt for now, since this is likely very rare.  */
1045           if (GET_CODE (XEXP (inside, 1)) == REG)
1046             abort ();
1047         }
1048       else if (GET_CODE (inside) == LABEL_REF)
1049         return "mov.l   %1,%0\n\tmov.l  %1+4,%T0";
1050       else if (GET_CODE (inside) == POST_INC)
1051         return "mov.l   %1,%0\n\tmov.l  %1,%T0";
1052       else
1053         abort ();
1054
1055       /* Work out the safe way to copy.  Copy into the second half first.  */
1056       if (dreg == ptrreg)
1057         return "mov.l   %T1,%T0\n\tmov.l        %1,%0";
1058     }
1059
1060   return "mov.l %1,%0\n\tmov.l  %T1,%T0";
1061 }
1062
1063 /* Print an instruction which would have gone into a delay slot after
1064    another instruction, but couldn't because the other instruction expanded
1065    into a sequence where putting the slot insn at the end wouldn't work.  */
1066
1067 static void
1068 print_slot (insn)
1069      rtx insn;
1070 {
1071   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
1072
1073   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
1074 }
1075
1076 const char *
1077 output_far_jump (insn, op)
1078      rtx insn;
1079      rtx op;
1080 {
1081   struct { rtx lab, reg, op; } this;
1082   rtx braf_base_lab = NULL_RTX;
1083   const char *jump;
1084   int far;
1085   int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
1086   rtx prev;
1087
1088   this.lab = gen_label_rtx ();
1089
1090   if (TARGET_SH2
1091       && offset >= -32764
1092       && offset - get_attr_length (insn) <= 32766)
1093     {
1094       far = 0;
1095       jump = "mov.w     %O0,%1; braf    %1";
1096     }
1097   else
1098     {
1099       far = 1;
1100       if (flag_pic)
1101         {
1102           if (TARGET_SH2)
1103             jump = "mov.l       %O0,%1; braf    %1";
1104           else
1105             jump = "mov.l       r0,@-r15; mova  %O0,r0; mov.l   @r0,%1; add     r0,%1; mov.l    @r15+,r0; jmp   @%1";
1106         }
1107       else
1108         jump = "mov.l   %O0,%1; jmp     @%1";
1109     }
1110   /* If we have a scratch register available, use it.  */
1111   if (GET_CODE ((prev = prev_nonnote_insn (insn))) == INSN
1112       && INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
1113     {
1114       this.reg = SET_DEST (XVECEXP (PATTERN (prev), 0, 0));
1115       if (REGNO (this.reg) == R0_REG && flag_pic && ! TARGET_SH2)
1116         jump = "mov.l   r1,@-r15; mova  %O0,r0; mov.l   @r0,r1; add     r1,r0; mov.l    @r15+,r1; jmp   @%1";
1117       output_asm_insn (jump, &this.lab);
1118       if (dbr_sequence_length ())
1119         print_slot (final_sequence);
1120       else
1121         output_asm_insn ("nop", 0);
1122     }
1123   else
1124     {
1125       /* Output the delay slot insn first if any.  */
1126       if (dbr_sequence_length ())
1127         print_slot (final_sequence);
1128
1129       this.reg = gen_rtx_REG (SImode, 13);
1130       /* We must keep the stack aligned to 8-byte boundaries on SH5.
1131          Fortunately, MACL is fixed and call-clobbered, and we never
1132          need its value across jumps, so save r13 in it instead of in
1133          the stack.  */
1134       if (TARGET_SH5)
1135         output_asm_insn ("lds   r13, macl", 0);
1136       else
1137         output_asm_insn ("mov.l r13,@-r15", 0);
1138       output_asm_insn (jump, &this.lab);
1139       if (TARGET_SH5)
1140         output_asm_insn ("sts   macl, r13", 0);
1141       else
1142         output_asm_insn ("mov.l @r15+,r13", 0);
1143     }
1144   if (far && flag_pic && TARGET_SH2)
1145     {
1146       braf_base_lab = gen_label_rtx ();
1147       (*targetm.asm_out.internal_label) (asm_out_file, "L",
1148                                  CODE_LABEL_NUMBER (braf_base_lab));
1149     }
1150   if (far)
1151     output_asm_insn (".align    2", 0);
1152   (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (this.lab));
1153   this.op = op;
1154   if (far && flag_pic)
1155     {
1156       if (TARGET_SH2)
1157         this.lab = braf_base_lab;
1158       output_asm_insn (".long   %O2-%O0", &this.lab);
1159     }
1160   else
1161     output_asm_insn (far ? ".long       %O2" : ".word %O2-%O0", &this.lab);
1162   return "";
1163 }
1164
1165 /* Local label counter, used for constants in the pool and inside
1166    pattern branches.  */
1167
1168 static int lf = 100;
1169
1170 /* Output code for ordinary branches.  */
1171
1172 const char *
1173 output_branch (logic, insn, operands)
1174      int logic;
1175      rtx insn;
1176      rtx *operands;
1177 {
1178   switch (get_attr_length (insn))
1179     {
1180     case 6:
1181       /* This can happen if filling the delay slot has caused a forward
1182          branch to exceed its range (we could reverse it, but only
1183          when we know we won't overextend other branches; this should
1184          best be handled by relaxation).
1185          It can also happen when other condbranches hoist delay slot insn
1186          from their destination, thus leading to code size increase.
1187          But the branch will still be in the range -4092..+4098 bytes.  */
1188
1189       if (! TARGET_RELAX)
1190         {
1191           int label = lf++;
1192           /* The call to print_slot will clobber the operands.  */
1193           rtx op0 = operands[0];
1194     
1195           /* If the instruction in the delay slot is annulled (true), then
1196              there is no delay slot where we can put it now.  The only safe
1197              place for it is after the label.  final will do that by default.  */
1198     
1199           if (final_sequence
1200               && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1201             {
1202               asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
1203                            ASSEMBLER_DIALECT ? "/" : ".", label);
1204               print_slot (final_sequence);
1205             }
1206           else
1207             asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
1208     
1209           output_asm_insn ("bra\t%l0", &op0);
1210           fprintf (asm_out_file, "\tnop\n");
1211           (*targetm.asm_out.internal_label)(asm_out_file, "LF", label);
1212     
1213           return "";
1214         }
1215       /* When relaxing, handle this like a short branch.  The linker
1216          will fix it up if it still doesn't fit after relaxation.  */
1217     case 2:
1218       return logic ? "bt%.\t%l0" : "bf%.\t%l0";
1219
1220       /* These are for SH2e, in which we have to account for the
1221          extra nop because of the hardware bug in annulled branches.  */
1222     case 8:
1223       if (! TARGET_RELAX)
1224         {
1225           int label = lf++;
1226
1227           if (final_sequence
1228               && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1229             abort ();
1230           asm_fprintf (asm_out_file, "b%s%ss\t%LLF%d\n",
1231                        logic ? "f" : "t",
1232                        ASSEMBLER_DIALECT ? "/" : ".", label);
1233           fprintf (asm_out_file, "\tnop\n");
1234           output_asm_insn ("bra\t%l0", operands);
1235           fprintf (asm_out_file, "\tnop\n");
1236           (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
1237
1238           return "";
1239         }
1240       /* When relaxing, fall through.  */
1241     case 4:
1242       {
1243         char buffer[10];
1244         
1245         sprintf (buffer, "b%s%ss\t%%l0",
1246                  logic ? "t" : "f",
1247                  ASSEMBLER_DIALECT ? "/" : ".");
1248         output_asm_insn (buffer, &operands[0]);
1249         return "nop";
1250       }
1251
1252     default:
1253       /* There should be no longer branches now - that would
1254          indicate that something has destroyed the branches set
1255          up in machine_dependent_reorg.  */
1256       abort ();
1257     }
1258 }
1259
1260 const char *
1261 output_branchy_insn (code, template, insn, operands)
1262      enum rtx_code code;
1263      const char *template;
1264      rtx insn;
1265      rtx *operands;
1266 {
1267   rtx next_insn = NEXT_INSN (insn);
1268
1269   if (next_insn && GET_CODE (next_insn) == JUMP_INSN && condjump_p (next_insn))
1270     {
1271       rtx src = SET_SRC (PATTERN (next_insn));
1272       if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
1273         {
1274           /* Following branch not taken */
1275           operands[9] = gen_label_rtx ();
1276           emit_label_after (operands[9], next_insn);
1277           INSN_ADDRESSES_NEW (operands[9],
1278                               INSN_ADDRESSES (INSN_UID (next_insn))
1279                               + get_attr_length (next_insn));
1280           return template;
1281         }
1282       else
1283         {
1284           int offset = (branch_dest (next_insn)
1285                         - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
1286           if (offset >= -252 && offset <= 258)
1287             {
1288               if (GET_CODE (src) == IF_THEN_ELSE)
1289                 /* branch_true */
1290                 src = XEXP (src, 1);
1291               operands[9] = src;
1292               return template;
1293             }
1294         }
1295     }
1296   operands[9] = gen_label_rtx ();
1297   emit_label_after (operands[9], insn);
1298   INSN_ADDRESSES_NEW (operands[9],
1299                       INSN_ADDRESSES (INSN_UID (insn))
1300                       + get_attr_length (insn));
1301   return template;
1302 }
1303
1304 const char *
1305 output_ieee_ccmpeq (insn, operands)
1306      rtx insn, *operands;
1307 {
1308   return output_branchy_insn (NE, "bt\t%l9\\;fcmp/eq\t%1,%0", insn, operands);
1309 }
1310 \f
1311 /* Output the start of the assembler file.  */
1312
1313 static void
1314 sh_file_start ()
1315 {
1316   default_file_start ();
1317
1318   if (TARGET_ELF)
1319     /* We need to show the text section with the proper
1320        attributes as in TEXT_SECTION_ASM_OP, before dwarf2out
1321        emits it without attributes in TEXT_SECTION, else GAS
1322        will complain.  We can teach GAS specifically about the
1323        default attributes for our choice of text section, but
1324        then we would have to change GAS again if/when we change
1325        the text section name.  */
1326     fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
1327   else
1328     /* Switch to the data section so that the coffsem symbol
1329        isn't in the text section.  */
1330     data_section ();
1331
1332   if (TARGET_LITTLE_ENDIAN)
1333     fputs ("\t.little\n", asm_out_file);
1334
1335   if (!TARGET_ELF)
1336     {
1337       if (TARGET_SHCOMPACT)
1338         fputs ("\t.mode\tSHcompact\n", asm_out_file);
1339       else if (TARGET_SHMEDIA)
1340         fprintf (asm_out_file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
1341                  TARGET_SHMEDIA64 ? 64 : 32);
1342     }
1343 }
1344 \f
1345 /* Check if PAT includes UNSPEC_CALLER unspec pattern.  */
1346
1347 static bool
1348 unspec_caller_rtx_p (pat)
1349      rtx pat;
1350 {
1351   switch (GET_CODE (pat))
1352     {
1353     case CONST:
1354       return unspec_caller_rtx_p (XEXP (pat, 0));
1355     case PLUS:
1356     case MINUS:
1357       if (unspec_caller_rtx_p (XEXP (pat, 0)))
1358         return true;
1359       return unspec_caller_rtx_p (XEXP (pat, 1));
1360     case UNSPEC:
1361       if (XINT (pat, 1) == UNSPEC_CALLER)
1362         return true;
1363     default:
1364       break;
1365     }
1366
1367   return false;
1368 }
1369
1370 /* Indicate that INSN cannot be duplicated.  This is true for insn
1371    that generates an unique label.  */
1372
1373 static bool
1374 sh_cannot_copy_insn_p (insn)
1375      rtx insn;
1376 {
1377   rtx pat;
1378
1379   if (!reload_completed || !flag_pic)
1380     return false;
1381
1382   if (GET_CODE (insn) != INSN)
1383     return false;
1384   if (asm_noperands (insn) >= 0)
1385     return false;
1386
1387   pat = PATTERN (insn);
1388   if (GET_CODE (pat) != SET)
1389     return false;
1390   pat = SET_SRC (pat);
1391
1392   if (unspec_caller_rtx_p (pat))
1393     return true;
1394
1395   return false;
1396 }
1397 \f
1398 /* Actual number of instructions used to make a shift by N.  */
1399 static const char ashiftrt_insns[] =
1400   { 0,1,2,3,4,5,8,8,8,8,8,8,8,8,8,8,2,3,4,5,8,8,8,8,8,8,8,8,8,8,8,2};
1401
1402 /* Left shift and logical right shift are the same.  */
1403 static const char shift_insns[]    =
1404   { 0,1,1,2,2,3,3,4,1,2,2,3,3,4,3,3,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
1405
1406 /* Individual shift amounts needed to get the above length sequences.
1407    One bit right shifts clobber the T bit, so when possible, put one bit
1408    shifts in the middle of the sequence, so the ends are eligible for
1409    branch delay slots.  */
1410 static const short shift_amounts[32][5] = {
1411   {0}, {1}, {2}, {2, 1},
1412   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
1413   {8}, {8, 1}, {8, 2}, {8, 1, 2},
1414   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
1415   {16}, {16, 1}, {16, 2}, {16, 1, 2},
1416   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1417   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1418   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1419
1420 /* Likewise, but for shift amounts < 16, up to three highmost bits
1421    might be clobbered.  This is typically used when combined with some
1422    kind of sign or zero extension.  */
1423    
1424 static const char ext_shift_insns[]    =
1425   { 0,1,1,2,2,3,2,2,1,2,2,3,3,3,2,2,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
1426
1427 static const short ext_shift_amounts[32][4] = {
1428   {0}, {1}, {2}, {2, 1},
1429   {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
1430   {8}, {8, 1}, {8, 2}, {8, 1, 2},
1431   {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
1432   {16}, {16, 1}, {16, 2}, {16, 1, 2},
1433   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1434   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1435   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1436
1437 /* Assuming we have a value that has been sign-extended by at least one bit,
1438    can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
1439    to shift it by N without data loss, and quicker than by other means?  */
1440 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
1441
1442 /* This is used in length attributes in sh.md to help compute the length
1443    of arbitrary constant shift instructions.  */
1444
1445 int
1446 shift_insns_rtx (insn)
1447      rtx insn;
1448 {
1449   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1450   int shift_count = INTVAL (XEXP (set_src, 1));
1451   enum rtx_code shift_code = GET_CODE (set_src);
1452
1453   switch (shift_code)
1454     {
1455     case ASHIFTRT:
1456       return ashiftrt_insns[shift_count];
1457     case LSHIFTRT:
1458     case ASHIFT:
1459       return shift_insns[shift_count];
1460     default:
1461       abort();
1462     }
1463 }
1464
1465 /* Return the cost of a shift.  */
1466
1467 static inline int
1468 shiftcosts (x)
1469      rtx x;
1470 {
1471   int value;
1472
1473   if (TARGET_SHMEDIA)
1474     return 1;
1475
1476   if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
1477     {
1478       if (GET_MODE (x) == DImode
1479           && GET_CODE (XEXP (x, 1)) == CONST_INT
1480           && INTVAL (XEXP (x, 1)) == 1)
1481         return 2;
1482
1483       /* Everything else is invalid, because there is no pattern for it.  */
1484       return 10000;
1485     }
1486   /* If shift by a non constant, then this will be expensive.  */
1487   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1488     return SH_DYNAMIC_SHIFT_COST;
1489
1490   value = INTVAL (XEXP (x, 1));
1491
1492   /* Otherwise, return the true cost in instructions.  */
1493   if (GET_CODE (x) == ASHIFTRT)
1494     {
1495       int cost = ashiftrt_insns[value];
1496       /* If SH3, then we put the constant in a reg and use shad.  */
1497       if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
1498         cost = 1 + SH_DYNAMIC_SHIFT_COST;
1499       return cost;
1500     }
1501   else
1502     return shift_insns[value];
1503 }
1504
1505 /* Return the cost of an AND operation.  */
1506
1507 static inline int
1508 andcosts (x)
1509      rtx x;
1510 {
1511   int i;
1512
1513   /* Anding with a register is a single cycle and instruction.  */
1514   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1515     return 1;
1516
1517   i = INTVAL (XEXP (x, 1));
1518
1519   if (TARGET_SHMEDIA)
1520     {
1521       if ((GET_CODE (XEXP (x, 1)) == CONST_INT
1522            && CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
1523           || EXTRA_CONSTRAINT_C16 (XEXP (x, 1)))
1524         return 1;
1525       else
1526         return 2;
1527     }
1528
1529   /* These constants are single cycle extu.[bw] instructions.  */
1530   if (i == 0xff || i == 0xffff)
1531     return 1;
1532   /* Constants that can be used in an and immediate instruction in a single
1533      cycle, but this requires r0, so make it a little more expensive.  */
1534   if (CONST_OK_FOR_K08 (i))
1535     return 2;
1536   /* Constants that can be loaded with a mov immediate and an and.
1537      This case is probably unnecessary.  */
1538   if (CONST_OK_FOR_I08 (i))
1539     return 2;
1540   /* Any other constants requires a 2 cycle pc-relative load plus an and.
1541      This case is probably unnecessary.  */
1542   return 3;
1543 }
1544
1545 /* Return the cost of an addition or a subtraction.  */
1546
1547 static inline int
1548 addsubcosts (x)
1549      rtx x;
1550 {
1551   /* Adding a register is a single cycle insn.  */
1552   if (GET_CODE (XEXP (x, 1)) == REG
1553       || GET_CODE (XEXP (x, 1)) == SUBREG)
1554     return 1;
1555
1556   /* Likewise for small constants.  */
1557   if (GET_CODE (XEXP (x, 1)) == CONST_INT
1558       && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
1559     return 1;
1560
1561   if (TARGET_SHMEDIA)
1562     switch (GET_CODE (XEXP (x, 1)))
1563       {
1564       case CONST:
1565       case LABEL_REF:
1566       case SYMBOL_REF:
1567         return TARGET_SHMEDIA64 ? 5 : 3;
1568
1569       case CONST_INT:
1570         if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
1571           return 2;
1572         else if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1)) >> 16))
1573           return 3;
1574         else if (CONST_OK_FOR_I16 ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
1575           return 4;
1576
1577         /* Fall through.  */
1578       default:
1579           return 5;
1580       }
1581
1582   /* Any other constant requires a 2 cycle pc-relative load plus an
1583      addition.  */
1584   return 3;
1585 }
1586
1587 /* Return the cost of a multiply.  */
1588 static inline int
1589 multcosts (x)
1590      rtx x ATTRIBUTE_UNUSED;
1591 {
1592   if (TARGET_SHMEDIA)
1593     return 3;
1594
1595   if (TARGET_SH2)
1596     {
1597       /* We have a mul insn, so we can never take more than the mul and the
1598          read of the mac reg, but count more because of the latency and extra
1599          reg usage.  */
1600       if (TARGET_SMALLCODE)
1601         return 2;
1602       return 3;
1603     }
1604
1605   /* If we're aiming at small code, then just count the number of
1606      insns in a multiply call sequence.  */
1607   if (TARGET_SMALLCODE)
1608     return 5;
1609
1610   /* Otherwise count all the insns in the routine we'd be calling too.  */
1611   return 20;
1612 }
1613
1614 /* Compute a (partial) cost for rtx X.  Return true if the complete
1615    cost has been computed, and false if subexpressions should be
1616    scanned.  In either case, *TOTAL contains the cost result.  */
1617
1618 static bool
1619 sh_rtx_costs (x, code, outer_code, total)
1620      rtx x;
1621      int code, outer_code, *total;
1622 {
1623   switch (code)
1624     {
1625     case CONST_INT:
1626       if (TARGET_SHMEDIA)
1627         {
1628           if (INTVAL (x) == 0)
1629             *total = 0;
1630           else if (outer_code == AND && and_operand ((x), DImode))
1631             *total = 0;
1632           else if ((outer_code == IOR || outer_code == XOR
1633                     || outer_code == PLUS)
1634                    && CONST_OK_FOR_I10 (INTVAL (x)))
1635             *total = 0;
1636           else if (CONST_OK_FOR_I16 (INTVAL (x)))
1637             *total = COSTS_N_INSNS (outer_code != SET);
1638           else if (CONST_OK_FOR_I16 (INTVAL (x) >> 16))
1639             *total = COSTS_N_INSNS (2);
1640           else if (CONST_OK_FOR_I16 ((INTVAL (x) >> 16) >> 16))
1641             *total = COSTS_N_INSNS (3);
1642           else
1643             *total = COSTS_N_INSNS (4);
1644           return true;
1645         }
1646       if (CONST_OK_FOR_I08 (INTVAL (x)))
1647         *total = 0;
1648       else if ((outer_code == AND || outer_code == IOR || outer_code == XOR)
1649                && CONST_OK_FOR_K08 (INTVAL (x)))
1650         *total = 1;
1651       else
1652         *total = 8;
1653       return true;
1654
1655     case CONST:
1656     case LABEL_REF:
1657     case SYMBOL_REF:
1658       if (TARGET_SHMEDIA64)
1659         *total = COSTS_N_INSNS (4);
1660       else if (TARGET_SHMEDIA32)
1661         *total = COSTS_N_INSNS (2);
1662       else
1663         *total = 5;
1664       return true;
1665
1666     case CONST_DOUBLE:
1667       if (TARGET_SHMEDIA)
1668         *total = COSTS_N_INSNS (4);
1669       else
1670         *total = 10;
1671       return true;
1672
1673     case PLUS:
1674       *total = COSTS_N_INSNS (addsubcosts (x));
1675       return true;
1676
1677     case AND:
1678       *total = COSTS_N_INSNS (andcosts (x));
1679       return true;
1680
1681     case MULT:
1682       *total = COSTS_N_INSNS (multcosts (x));
1683       return true;
1684
1685     case ASHIFT:
1686     case ASHIFTRT:
1687     case LSHIFTRT:
1688       *total = COSTS_N_INSNS (shiftcosts (x));
1689       return true;
1690
1691     case DIV:
1692     case UDIV:
1693     case MOD:
1694     case UMOD:
1695       *total = COSTS_N_INSNS (20);
1696       return true;
1697
1698     case FLOAT:
1699     case FIX:
1700       *total = 100;
1701       return true;
1702
1703     default:
1704       return false;
1705     }
1706 }
1707
1708 /* Compute the cost of an address.  For the SH, all valid addresses are
1709    the same cost.  Use a slightly higher cost for reg + reg addressing,
1710    since it increases pressure on r0.  */
1711
1712 static int
1713 sh_address_cost (X)
1714      rtx X;
1715 {
1716   return (GET_CODE (X) == PLUS
1717           && ! CONSTANT_P (XEXP (X, 1))
1718           && ! TARGET_SHMEDIA ? 1 : 0);
1719 }
1720
1721 /* Code to expand a shift.  */
1722
1723 void
1724 gen_ashift (type, n, reg)
1725      int type;
1726      int n;
1727      rtx reg;
1728 {
1729   /* Negative values here come from the shift_amounts array.  */
1730   if (n < 0)
1731     {
1732       if (type == ASHIFT)
1733         type = LSHIFTRT;
1734       else
1735         type = ASHIFT;
1736       n = -n;
1737     }
1738
1739   switch (type)
1740     {
1741     case ASHIFTRT:
1742       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
1743       break;
1744     case LSHIFTRT:
1745       if (n == 1)
1746         emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
1747       else
1748         emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
1749       break;
1750     case ASHIFT:
1751       emit_insn (gen_ashlsi3_std (reg, reg, GEN_INT (n)));
1752       break;
1753     }
1754 }
1755
1756 /* Same for HImode */
1757
1758 void
1759 gen_ashift_hi (type, n, reg)
1760      int type;
1761      int n;
1762      rtx reg;
1763 {
1764   /* Negative values here come from the shift_amounts array.  */
1765   if (n < 0)
1766     {
1767       if (type == ASHIFT)
1768         type = LSHIFTRT;
1769       else
1770         type = ASHIFT;
1771       n = -n;
1772     }
1773
1774   switch (type)
1775     {
1776     case ASHIFTRT:
1777     case LSHIFTRT:
1778       /* We don't have HImode right shift operations because using the
1779          ordinary 32 bit shift instructions for that doesn't generate proper
1780          zero/sign extension.
1781          gen_ashift_hi is only called in contexts where we know that the
1782          sign extension works out correctly.  */
1783       {
1784         int offset = 0;
1785         if (GET_CODE (reg) == SUBREG)
1786           {
1787             offset = SUBREG_BYTE (reg);
1788             reg = SUBREG_REG (reg);
1789           }
1790         gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
1791         break;
1792       }
1793     case ASHIFT:
1794       emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
1795       break;
1796     }
1797 }
1798
1799 /* Output RTL to split a constant shift into its component SH constant
1800    shift instructions.  */
1801    
1802 void
1803 gen_shifty_op (code, operands)
1804      int code;
1805      rtx *operands;
1806 {
1807   int value = INTVAL (operands[2]);
1808   int max, i;
1809
1810   /* Truncate the shift count in case it is out of bounds.  */
1811   value = value & 0x1f;
1812  
1813   if (value == 31)
1814     {
1815       if (code == LSHIFTRT)
1816         {
1817           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
1818           emit_insn (gen_movt (operands[0]));
1819           return;
1820         }
1821       else if (code == ASHIFT)
1822         {
1823           /* There is a two instruction sequence for 31 bit left shifts,
1824              but it requires r0.  */
1825           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
1826             {
1827               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
1828               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
1829               return;
1830             }
1831         }
1832     }
1833   else if (value == 0)
1834     {
1835       /* This can happen when not optimizing.  We must output something here
1836          to prevent the compiler from aborting in final.c after the try_split
1837          call.  */
1838       emit_insn (gen_nop ());
1839       return;
1840     }
1841
1842   max = shift_insns[value];
1843   for (i = 0; i < max; i++)
1844     gen_ashift (code, shift_amounts[value][i], operands[0]);
1845 }
1846    
1847 /* Same as above, but optimized for values where the topmost bits don't
1848    matter.  */
1849
1850 void
1851 gen_shifty_hi_op (code, operands)
1852      int code;
1853      rtx *operands;
1854 {
1855   int value = INTVAL (operands[2]);
1856   int max, i;
1857   void (*gen_fun) PARAMS ((int, int, rtx));
1858
1859   /* This operation is used by and_shl for SImode values with a few
1860      high bits known to be cleared.  */
1861   value &= 31;
1862   if (value == 0)
1863     {
1864       emit_insn (gen_nop ());
1865       return;
1866     }
1867
1868   gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
1869   if (code == ASHIFT)
1870     {
1871       max = ext_shift_insns[value];
1872       for (i = 0; i < max; i++)
1873         gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1874     }
1875   else
1876     /* When shifting right, emit the shifts in reverse order, so that
1877        solitary negative values come first.  */
1878     for (i = ext_shift_insns[value] - 1; i >= 0; i--)
1879       gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1880 }
1881
1882 /* Output RTL for an arithmetic right shift.  */
1883
1884 /* ??? Rewrite to use super-optimizer sequences.  */
1885
1886 int
1887 expand_ashiftrt (operands)
1888      rtx *operands;
1889 {
1890   rtx sym;
1891   rtx wrk;
1892   char func[18];
1893   tree func_name;
1894   int value;
1895
1896   if (TARGET_SH3)
1897     {
1898       if (GET_CODE (operands[2]) != CONST_INT)
1899         {
1900           rtx count = copy_to_mode_reg (SImode, operands[2]);
1901           emit_insn (gen_negsi2 (count, count));
1902           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1903           return 1;
1904         }
1905       else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
1906                > 1 + SH_DYNAMIC_SHIFT_COST)
1907         {
1908           rtx count
1909             = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
1910           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1911           return 1;
1912         }
1913     }
1914   if (GET_CODE (operands[2]) != CONST_INT)
1915     return 0;
1916
1917   value = INTVAL (operands[2]) & 31;
1918
1919   if (value == 31)
1920     {
1921       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1922       return 1;
1923     }
1924   else if (value >= 16 && value <= 19)
1925     {
1926       wrk = gen_reg_rtx (SImode);
1927       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1928       value -= 16;
1929       while (value--)
1930         gen_ashift (ASHIFTRT, 1, wrk);
1931       emit_move_insn (operands[0], wrk);
1932       return 1;
1933     }
1934   /* Expand a short sequence inline, longer call a magic routine.  */
1935   else if (value <= 5)
1936     {
1937       wrk = gen_reg_rtx (SImode);
1938       emit_move_insn (wrk, operands[1]);
1939       while (value--)
1940         gen_ashift (ASHIFTRT, 1, wrk);
1941       emit_move_insn (operands[0], wrk);
1942       return 1;
1943     }
1944
1945   wrk = gen_reg_rtx (Pmode);
1946
1947   /* Load the value into an arg reg and call a helper.  */
1948   emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1949   sprintf (func, "__ashiftrt_r4_%d", value);
1950   func_name = get_identifier (func);
1951   sym = function_symbol (IDENTIFIER_POINTER (func_name));
1952   emit_move_insn (wrk, sym);
1953   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1954   emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
1955   return 1;
1956 }
1957
1958 int
1959 sh_dynamicalize_shift_p (count)
1960      rtx count;
1961 {
1962   return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
1963 }
1964
1965 /* Try to find a good way to implement the combiner pattern
1966   [(set (match_operand:SI 0 "register_operand" "r")
1967         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1968                            (match_operand:SI 2 "const_int_operand" "n"))
1969                 (match_operand:SI 3 "const_int_operand" "n"))) .
1970   LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1971   return 0 for simple right / left or left/right shift combination.
1972   return 1 for a combination of shifts with zero_extend.
1973   return 2 for a combination of shifts with an AND that needs r0.
1974   return 3 for a combination of shifts with an AND that needs an extra
1975     scratch register, when the three highmost bits of the AND mask are clear.
1976   return 4 for a combination of shifts with an AND that needs an extra
1977     scratch register, when any of the three highmost bits of the AND mask
1978     is set.
1979   If ATTRP is set, store an initial right shift width in ATTRP[0],
1980   and the instruction length in ATTRP[1] .  These values are not valid
1981   when returning 0.
1982   When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1983   shift_amounts for the last shift value that is to be used before the
1984   sign extend.  */
1985 int
1986 shl_and_kind (left_rtx, mask_rtx, attrp)
1987      rtx left_rtx, mask_rtx;
1988      int *attrp;
1989 {
1990   unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1991   int left = INTVAL (left_rtx), right;
1992   int best = 0;
1993   int cost, best_cost = 10000;
1994   int best_right = 0, best_len = 0;
1995   int i;
1996   int can_ext;
1997
1998   if (left < 0 || left > 31)
1999     return 0;
2000   if (GET_CODE (mask_rtx) == CONST_INT)
2001     mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
2002   else
2003     mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
2004   /* Can this be expressed as a right shift / left shift pair ? */
2005   lsb = ((mask ^ (mask - 1)) >> 1) + 1;
2006   right = exact_log2 (lsb);
2007   mask2 = ~(mask + lsb - 1);
2008   lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
2009   /* mask has no zeroes but trailing zeroes <==> ! mask2 */
2010   if (! mask2)
2011     best_cost = shift_insns[right] + shift_insns[right + left];
2012   /* mask has no trailing zeroes <==> ! right */
2013   else if (! right && mask2 == ~(lsb2 - 1))
2014     {
2015       int late_right = exact_log2 (lsb2);
2016       best_cost = shift_insns[left + late_right] + shift_insns[late_right];
2017     }
2018   /* Try to use zero extend */
2019   if (mask2 == ~(lsb2 - 1))
2020     {
2021       int width, first;
2022
2023       for (width = 8; width <= 16; width += 8)
2024         {
2025           /* Can we zero-extend right away? */
2026           if (lsb2 == (unsigned HOST_WIDE_INT)1 << width)
2027             {
2028               cost
2029                 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
2030               if (cost < best_cost)
2031                 {
2032                   best = 1;
2033                   best_cost = cost;
2034                   best_right = right;
2035                   best_len = cost;
2036                   if (attrp)
2037                     attrp[2] = -1;
2038                 }
2039               continue;
2040             }
2041           /* ??? Could try to put zero extend into initial right shift,
2042              or even shift a bit left before the right shift.  */
2043           /* Determine value of first part of left shift, to get to the
2044              zero extend cut-off point.  */
2045           first = width - exact_log2 (lsb2) + right;
2046           if (first >= 0 && right + left - first >= 0)
2047             {
2048               cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
2049                 + ext_shift_insns[right + left - first];
2050               if (cost < best_cost)
2051                 {
2052                   best = 1;
2053                   best_cost = cost;
2054                   best_right = right;
2055                   best_len = cost;
2056                   if (attrp)
2057                     attrp[2] = first;
2058                   }
2059             }
2060         }
2061     }
2062   /* Try to use r0 AND pattern */
2063   for (i = 0; i <= 2; i++)
2064     {
2065       if (i > right)
2066         break;
2067       if (! CONST_OK_FOR_K08 (mask >> i))
2068         continue;
2069       cost = (i != 0) + 2 + ext_shift_insns[left + i];
2070       if (cost < best_cost)
2071         {
2072           best = 2;
2073           best_cost = cost;
2074           best_right = i;
2075           best_len = cost - 1;
2076         }
2077     }
2078   /* Try to use a scratch register to hold the AND operand.  */
2079   can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT)3 << 30)) == 0;
2080   for (i = 0; i <= 2; i++)
2081     {
2082       if (i > right)
2083         break;
2084       cost = (i != 0) + (CONST_OK_FOR_I08 (mask >> i) ? 2 : 3)
2085         + (can_ext ? ext_shift_insns : shift_insns)[left + i];
2086       if (cost < best_cost)
2087         {
2088           best = 4 - can_ext;
2089           best_cost = cost;
2090           best_right = i;
2091           best_len = cost - 1 - ! CONST_OK_FOR_I08 (mask >> i);
2092         }
2093     }
2094
2095   if (attrp)
2096     {
2097       attrp[0] = best_right;
2098       attrp[1] = best_len;
2099     }
2100   return best;
2101 }
2102
2103 /* This is used in length attributes of the unnamed instructions
2104    corresponding to shl_and_kind return values of 1 and 2.  */
2105 int
2106 shl_and_length (insn)
2107      rtx insn;
2108 {
2109   rtx set_src, left_rtx, mask_rtx;
2110   int attributes[3];
2111
2112   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2113   left_rtx = XEXP (XEXP (set_src, 0), 1);
2114   mask_rtx = XEXP (set_src, 1);
2115   shl_and_kind (left_rtx, mask_rtx, attributes);
2116   return attributes[1];
2117 }
2118
2119 /* This is used in length attribute of the and_shl_scratch instruction.  */
2120
2121 int
2122 shl_and_scr_length (insn)
2123      rtx insn;
2124 {
2125   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2126   int len = shift_insns[INTVAL (XEXP (set_src, 1))];
2127   rtx op = XEXP (set_src, 0);
2128   len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
2129   op = XEXP (XEXP (op, 0), 0);
2130   return len + shift_insns[INTVAL (XEXP (op, 1))];
2131 }
2132
2133 /* Generating rtl? */
2134 extern int rtx_equal_function_value_matters;
2135
2136 /* Generate rtl for instructions for which shl_and_kind advised a particular
2137    method of generating them, i.e. returned zero.  */
2138
2139 int
2140 gen_shl_and (dest, left_rtx, mask_rtx, source)
2141      rtx dest, left_rtx, mask_rtx, source;
2142 {
2143   int attributes[3];
2144   unsigned HOST_WIDE_INT mask;
2145   int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
2146   int right, total_shift;
2147   void (*shift_gen_fun) PARAMS ((int, rtx*)) = gen_shifty_hi_op;
2148
2149   right = attributes[0];
2150   total_shift = INTVAL (left_rtx) + right;
2151   mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
2152   switch (kind)
2153     {
2154     default:
2155       return -1;
2156     case 1:
2157       {
2158         int first = attributes[2];
2159         rtx operands[3];
2160
2161         if (first < 0)
2162           {
2163             emit_insn ((mask << right) <= 0xff
2164                        ? gen_zero_extendqisi2(dest,
2165                                               gen_lowpart (QImode, source))
2166                        : gen_zero_extendhisi2(dest,
2167                                               gen_lowpart (HImode, source)));
2168             source = dest;
2169           }
2170         if (source != dest)
2171           emit_insn (gen_movsi (dest, source));
2172         operands[0] = dest;
2173         if (right)
2174           {
2175             operands[2] = GEN_INT (right);
2176             gen_shifty_hi_op (LSHIFTRT, operands);
2177           }
2178         if (first > 0)
2179           {
2180             operands[2] = GEN_INT (first);
2181             gen_shifty_hi_op (ASHIFT, operands);
2182             total_shift -= first;
2183             mask <<= first;
2184           }
2185         if (first >= 0)
2186           emit_insn (mask <= 0xff
2187                      ? gen_zero_extendqisi2(dest, gen_lowpart (QImode, dest))
2188                      : gen_zero_extendhisi2(dest, gen_lowpart (HImode, dest)));
2189         if (total_shift > 0)
2190           {
2191             operands[2] = GEN_INT (total_shift);
2192             gen_shifty_hi_op (ASHIFT, operands);
2193           }
2194         break;
2195       }
2196     case 4:
2197       shift_gen_fun = gen_shifty_op;
2198     case 3:
2199       /* If the topmost bit that matters is set, set the topmost bits
2200          that don't matter.  This way, we might be able to get a shorter
2201          signed constant.  */
2202       if (mask & ((HOST_WIDE_INT)1 << (31 - total_shift)))
2203         mask |= (HOST_WIDE_INT)~0 << (31 - total_shift);
2204     case 2:
2205       /* Don't expand fine-grained when combining, because that will
2206          make the pattern fail.  */
2207       if (rtx_equal_function_value_matters
2208           || reload_in_progress || reload_completed)
2209         {
2210           rtx operands[3];
2211   
2212           /* Cases 3 and 4 should be handled by this split
2213              only while combining  */
2214           if (kind > 2)
2215             abort ();
2216           if (right)
2217             {
2218               emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
2219               source = dest;
2220             }
2221           emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
2222           if (total_shift)
2223             {
2224               operands[0] = dest;
2225               operands[1] = dest;
2226               operands[2] = GEN_INT (total_shift);
2227               shift_gen_fun (ASHIFT, operands);
2228             }
2229           break;
2230         }
2231       else
2232         {
2233           int neg = 0;
2234           if (kind != 4 && total_shift < 16)
2235             {
2236               neg = -ext_shift_amounts[total_shift][1];
2237               if (neg > 0)
2238                 neg -= ext_shift_amounts[total_shift][2];
2239               else
2240                 neg = 0;
2241             }
2242           emit_insn (gen_and_shl_scratch (dest, source,
2243                                           GEN_INT (right),
2244                                           GEN_INT (mask),
2245                                           GEN_INT (total_shift + neg),
2246                                           GEN_INT (neg)));
2247           emit_insn (gen_movsi (dest, dest));
2248           break;
2249         }
2250     }
2251   return 0;
2252 }
2253
2254 /* Try to find a good way to implement the combiner pattern
2255   [(set (match_operand:SI 0 "register_operand" "=r")
2256         (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
2257                                     (match_operand:SI 2 "const_int_operand" "n")
2258                          (match_operand:SI 3 "const_int_operand" "n")
2259                          (const_int 0)))
2260    (clobber (reg:SI T_REG))]
2261   LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
2262   return 0 for simple left / right shift combination.
2263   return 1 for left shift / 8 bit sign extend / left shift.
2264   return 2 for left shift / 16 bit sign extend / left shift.
2265   return 3 for left shift / 8 bit sign extend / shift / sign extend.
2266   return 4 for left shift / 16 bit sign extend / shift / sign extend.
2267   return 5 for left shift / 16 bit sign extend / right shift
2268   return 6 for < 8 bit sign extend / left shift.
2269   return 7 for < 8 bit sign extend / left shift / single right shift.
2270   If COSTP is nonzero, assign the calculated cost to *COSTP.  */
2271
2272 int
2273 shl_sext_kind (left_rtx, size_rtx, costp)
2274      rtx left_rtx, size_rtx;
2275      int *costp;
2276 {
2277   int left, size, insize, ext;
2278   int cost = 0, best_cost;
2279   int kind;
2280
2281   left = INTVAL (left_rtx);
2282   size = INTVAL (size_rtx);
2283   insize = size - left;
2284   if (insize <= 0)
2285     abort ();
2286   /* Default to left / right shift.  */
2287   kind = 0;
2288   best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
2289   if (size <= 16)
2290     {
2291       /* 16 bit shift / sign extend / 16 bit shift */
2292       cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
2293       /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
2294          below, by alternative 3 or something even better.  */
2295       if (cost < best_cost)
2296         {
2297           kind = 5;
2298           best_cost = cost;
2299         }
2300     }
2301   /* Try a plain sign extend between two shifts.  */
2302   for (ext = 16; ext >= insize; ext -= 8)
2303     {
2304       if (ext <= size)
2305         {
2306           cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
2307           if (cost < best_cost)
2308             {
2309               kind = ext / (unsigned) 8;
2310               best_cost = cost;
2311             }
2312         }
2313       /* Check if we can do a sloppy shift with a final signed shift
2314          restoring the sign.  */
2315       if (EXT_SHIFT_SIGNED (size - ext))
2316         cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
2317       /* If not, maybe it's still cheaper to do the second shift sloppy,
2318          and do a final sign extend?  */
2319       else if (size <= 16)
2320         cost = ext_shift_insns[ext - insize] + 1
2321           + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
2322       else
2323         continue;
2324       if (cost < best_cost)
2325         {
2326           kind = ext / (unsigned) 8 + 2;
2327           best_cost = cost;
2328         }
2329     }
2330   /* Check if we can sign extend in r0 */
2331   if (insize < 8)
2332     {
2333       cost = 3 + shift_insns[left];
2334       if (cost < best_cost)
2335         {
2336           kind = 6;
2337           best_cost = cost;
2338         }
2339       /* Try the same with a final signed shift.  */
2340       if (left < 31)
2341         {
2342           cost = 3 + ext_shift_insns[left + 1] + 1;
2343           if (cost < best_cost)
2344             {
2345               kind = 7;
2346               best_cost = cost;
2347             }
2348         }
2349     }
2350   if (TARGET_SH3)
2351     {
2352       /* Try to use a dynamic shift.  */
2353       cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
2354       if (cost < best_cost)
2355         {
2356           kind = 0;
2357           best_cost = cost;
2358         }
2359     }
2360   if (costp)
2361     *costp = cost;
2362   return kind;
2363 }
2364
2365 /* Function to be used in the length attribute of the instructions
2366    implementing this pattern.  */
2367
2368 int
2369 shl_sext_length (insn)
2370      rtx insn;
2371 {
2372   rtx set_src, left_rtx, size_rtx;
2373   int cost;
2374
2375   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2376   left_rtx = XEXP (XEXP (set_src, 0), 1);
2377   size_rtx = XEXP (set_src, 1);
2378   shl_sext_kind (left_rtx, size_rtx, &cost);
2379   return cost;
2380 }
2381
2382 /* Generate rtl for this pattern */
2383
2384 int
2385 gen_shl_sext (dest, left_rtx, size_rtx, source)
2386      rtx dest, left_rtx, size_rtx, source;
2387 {
2388   int kind;
2389   int left, size, insize, cost;
2390   rtx operands[3];
2391
2392   kind = shl_sext_kind (left_rtx, size_rtx, &cost);
2393   left = INTVAL (left_rtx);
2394   size = INTVAL (size_rtx);
2395   insize = size - left;
2396   switch (kind)
2397     {
2398     case 1:
2399     case 2:
2400     case 3:
2401     case 4:
2402       {
2403         int ext = kind & 1 ? 8 : 16;
2404         int shift2 = size - ext;
2405
2406         /* Don't expand fine-grained when combining, because that will
2407            make the pattern fail.  */
2408         if (! rtx_equal_function_value_matters
2409             && ! reload_in_progress && ! reload_completed)
2410           {
2411             emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2412             emit_insn (gen_movsi (dest, source));
2413             break;
2414           }
2415         if (dest != source)
2416           emit_insn (gen_movsi (dest, source));
2417         operands[0] = dest;
2418         if (ext - insize)
2419           {
2420             operands[2] = GEN_INT (ext - insize);
2421             gen_shifty_hi_op (ASHIFT, operands);
2422           }
2423         emit_insn (kind & 1
2424                    ? gen_extendqisi2(dest, gen_lowpart (QImode, dest))
2425                    : gen_extendhisi2(dest, gen_lowpart (HImode, dest)));
2426         if (kind <= 2)
2427           {
2428             if (shift2)
2429               {
2430                 operands[2] = GEN_INT (shift2);
2431                 gen_shifty_op (ASHIFT, operands);
2432               }
2433           }
2434         else
2435           {
2436             if (shift2 > 0)
2437               {
2438                 if (EXT_SHIFT_SIGNED (shift2))
2439                   {
2440                     operands[2] = GEN_INT (shift2 + 1);
2441                     gen_shifty_op (ASHIFT, operands);
2442                     operands[2] = GEN_INT (1);
2443                     gen_shifty_op (ASHIFTRT, operands);
2444                     break;
2445                   }
2446                 operands[2] = GEN_INT (shift2);
2447                 gen_shifty_hi_op (ASHIFT, operands);
2448               }
2449             else if (shift2)
2450               {
2451                 operands[2] = GEN_INT (-shift2);
2452                 gen_shifty_hi_op (LSHIFTRT, operands);
2453               }
2454             emit_insn (size <= 8
2455                        ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
2456                        : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2457           }
2458         break;
2459       }
2460     case 5:
2461       {
2462         int i = 16 - size;
2463         if (! rtx_equal_function_value_matters
2464             && ! reload_in_progress && ! reload_completed)
2465           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2466         else
2467           {
2468             operands[0] = dest;
2469             operands[2] = GEN_INT (16 - insize);
2470             gen_shifty_hi_op (ASHIFT, operands);
2471             emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2472           }
2473         /* Don't use gen_ashrsi3 because it generates new pseudos.  */
2474         while (--i >= 0)
2475           gen_ashift (ASHIFTRT, 1, dest);
2476         break;
2477       }
2478     case 6:
2479     case 7:
2480       /* Don't expand fine-grained when combining, because that will
2481          make the pattern fail.  */
2482       if (! rtx_equal_function_value_matters
2483           && ! reload_in_progress && ! reload_completed)
2484         {
2485           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2486           emit_insn (gen_movsi (dest, source));
2487           break;
2488         }
2489       emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
2490       emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
2491       emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
2492       operands[0] = dest;
2493       operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
2494       gen_shifty_op (ASHIFT, operands);
2495       if (kind == 7)
2496         emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
2497       break;
2498     default:
2499       return -1;
2500     }
2501   return 0;
2502 }
2503
2504 /* Prefix a symbol_ref name with "datalabel".  */
2505
2506 rtx
2507 gen_datalabel_ref (sym)
2508      rtx sym;
2509 {
2510   if (GET_CODE (sym) == LABEL_REF)
2511     return gen_rtx_CONST (GET_MODE (sym),
2512                           gen_rtx_UNSPEC (GET_MODE (sym),
2513                                           gen_rtvec (1, sym),
2514                                           UNSPEC_DATALABEL));
2515     
2516   if (GET_CODE (sym) != SYMBOL_REF)
2517     abort ();
2518
2519   return sym;
2520 }
2521
2522 \f
2523 /* The SH cannot load a large constant into a register, constants have to
2524    come from a pc relative load.  The reference of a pc relative load
2525    instruction must be less than 1k infront of the instruction.  This
2526    means that we often have to dump a constant inside a function, and
2527    generate code to branch around it.
2528
2529    It is important to minimize this, since the branches will slow things
2530    down and make things bigger.
2531
2532    Worst case code looks like:
2533
2534    mov.l L1,rn
2535    bra   L2
2536    nop
2537    align
2538    L1:   .long value
2539    L2:
2540    ..
2541
2542    mov.l L3,rn
2543    bra   L4
2544    nop
2545    align
2546    L3:   .long value
2547    L4:
2548    ..
2549
2550    We fix this by performing a scan before scheduling, which notices which
2551    instructions need to have their operands fetched from the constant table
2552    and builds the table.
2553
2554    The algorithm is:
2555
2556    scan, find an instruction which needs a pcrel move.  Look forward, find the
2557    last barrier which is within MAX_COUNT bytes of the requirement.
2558    If there isn't one, make one.  Process all the instructions between
2559    the find and the barrier.
2560
2561    In the above example, we can tell that L3 is within 1k of L1, so
2562    the first move can be shrunk from the 3 insn+constant sequence into
2563    just 1 insn, and the constant moved to L3 to make:
2564
2565    mov.l        L1,rn
2566    ..
2567    mov.l        L3,rn
2568    bra          L4
2569    nop
2570    align
2571    L3:.long value
2572    L4:.long value
2573
2574    Then the second move becomes the target for the shortening process.  */
2575
2576 typedef struct
2577 {
2578   rtx value;                    /* Value in table.  */
2579   rtx label;                    /* Label of value.  */
2580   rtx wend;                     /* End of window.  */
2581   enum machine_mode mode;       /* Mode of value.  */
2582
2583   /* True if this constant is accessed as part of a post-increment
2584      sequence.  Note that HImode constants are never accessed in this way.  */
2585   bool part_of_sequence_p;
2586 } pool_node;
2587
2588 /* The maximum number of constants that can fit into one pool, since
2589    the pc relative range is 0...1020 bytes and constants are at least 4
2590    bytes long.  */
2591
2592 #define MAX_POOL_SIZE (1020/4)
2593 static pool_node pool_vector[MAX_POOL_SIZE];
2594 static int pool_size;
2595 static rtx pool_window_label;
2596 static int pool_window_last;
2597
2598 /* ??? If we need a constant in HImode which is the truncated value of a
2599    constant we need in SImode, we could combine the two entries thus saving
2600    two bytes.  Is this common enough to be worth the effort of implementing
2601    it?  */
2602
2603 /* ??? This stuff should be done at the same time that we shorten branches.
2604    As it is now, we must assume that all branches are the maximum size, and
2605    this causes us to almost always output constant pools sooner than
2606    necessary.  */
2607
2608 /* Add a constant to the pool and return its label.  */
2609
2610 static rtx
2611 add_constant (x, mode, last_value)
2612      rtx x;
2613      enum machine_mode mode;
2614      rtx last_value;
2615 {
2616   int i;
2617   rtx lab, new, ref, newref;
2618
2619   /* First see if we've already got it.  */
2620   for (i = 0; i < pool_size; i++)
2621     {
2622       if (x->code == pool_vector[i].value->code
2623           && mode == pool_vector[i].mode)
2624         {
2625           if (x->code == CODE_LABEL)
2626             {
2627               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
2628                 continue;
2629             }
2630           if (rtx_equal_p (x, pool_vector[i].value))
2631             {
2632               lab = new = 0;
2633               if (! last_value
2634                   || ! i
2635                   || ! rtx_equal_p (last_value, pool_vector[i-1].value))
2636                 {
2637                   new = gen_label_rtx ();
2638                   LABEL_REFS (new) = pool_vector[i].label;
2639                   pool_vector[i].label = lab = new;
2640                 }
2641               if (lab && pool_window_label)
2642                 {
2643                   newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2644                   ref = pool_vector[pool_window_last].wend;
2645                   LABEL_NEXTREF (newref) = ref;
2646                   pool_vector[pool_window_last].wend = newref;
2647                 }
2648               if (new)
2649                 pool_window_label = new;
2650               pool_window_last = i;
2651               return lab;
2652             }
2653         }
2654     }
2655
2656   /* Need a new one.  */
2657   pool_vector[pool_size].value = x;
2658   if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
2659     {
2660       lab = 0;
2661       pool_vector[pool_size - 1].part_of_sequence_p = true;
2662     }
2663   else
2664     lab = gen_label_rtx ();
2665   pool_vector[pool_size].mode = mode;
2666   pool_vector[pool_size].label = lab;
2667   pool_vector[pool_size].wend = NULL_RTX;
2668   pool_vector[pool_size].part_of_sequence_p = (lab == 0);
2669   if (lab && pool_window_label)
2670     {
2671       newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2672       ref = pool_vector[pool_window_last].wend;
2673       LABEL_NEXTREF (newref) = ref;
2674       pool_vector[pool_window_last].wend = newref;
2675     }
2676   if (lab)
2677     pool_window_label = lab;
2678   pool_window_last = pool_size;
2679   pool_size++;
2680   return lab;
2681 }
2682
2683 /* Output the literal table.  */
2684
2685 static void
2686 dump_table (scan)
2687      rtx scan;
2688 {
2689   int i;
2690   int need_align = 1;
2691   rtx lab, ref;
2692   int have_df = 0;
2693
2694   /* Do two passes, first time dump out the HI sized constants.  */
2695
2696   for (i = 0; i < pool_size; i++)
2697     {
2698       pool_node *p = &pool_vector[i];
2699
2700       if (p->mode == HImode)
2701         {
2702           if (need_align)
2703             {
2704               scan = emit_insn_after (gen_align_2 (), scan);
2705               need_align = 0;
2706             }
2707           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2708             scan = emit_label_after (lab, scan);
2709           scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
2710                                   scan);
2711           for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2712             {
2713               lab = XEXP (ref, 0);
2714               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2715             }
2716         }
2717       else if (p->mode == DFmode)
2718         have_df = 1;
2719     }
2720
2721   need_align = 1;
2722
2723   if (TARGET_FMOVD && TARGET_ALIGN_DOUBLE && have_df)
2724     {
2725       rtx align_insn = NULL_RTX;
2726
2727       scan = emit_label_after (gen_label_rtx (), scan);
2728       scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2729       need_align = 0;
2730
2731       for (i = 0; i < pool_size; i++)
2732         {
2733           pool_node *p = &pool_vector[i];
2734
2735           switch (p->mode)
2736             {
2737             case HImode:
2738               break;
2739             case SImode:
2740             case SFmode:
2741               if (align_insn && !p->part_of_sequence_p)
2742                 {
2743                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
2744                     emit_label_before (lab, align_insn);
2745                   emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
2746                                     align_insn);
2747                   for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2748                     {
2749                       lab = XEXP (ref, 0);
2750                       emit_insn_before (gen_consttable_window_end (lab),
2751                                        align_insn);
2752                     }
2753                   delete_insn (align_insn);
2754                   align_insn = NULL_RTX;
2755                   continue;
2756                 }
2757               else
2758                 {
2759                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
2760                     scan = emit_label_after (lab, scan);
2761                   scan = emit_insn_after (gen_consttable_4 (p->value,
2762                                                             const0_rtx), scan);
2763                   need_align = ! need_align;
2764                 }
2765               break;
2766             case DFmode:
2767               if (need_align)
2768                 {
2769                   scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2770                   align_insn = scan;
2771                   need_align = 0;
2772                 }
2773             case DImode:
2774               for (lab = p->label; lab; lab = LABEL_REFS (lab))
2775                 scan = emit_label_after (lab, scan);
2776               scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2777                                       scan);
2778               break;
2779             default:
2780               abort ();
2781               break;
2782             }
2783
2784           if (p->mode != HImode)
2785             {
2786               for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2787                 {
2788                   lab = XEXP (ref, 0);
2789                   scan = emit_insn_after (gen_consttable_window_end (lab),
2790                                           scan);
2791                 }
2792             }
2793         }
2794
2795       pool_size = 0;
2796     }
2797   
2798   for (i = 0; i < pool_size; i++)
2799     {
2800       pool_node *p = &pool_vector[i];
2801
2802       switch (p->mode)
2803         {
2804         case HImode:
2805           break;
2806         case SImode:
2807         case SFmode:
2808           if (need_align)
2809             {
2810               need_align = 0;
2811               scan = emit_label_after (gen_label_rtx (), scan);
2812               scan = emit_insn_after (gen_align_4 (), scan);
2813             }
2814           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2815             scan = emit_label_after (lab, scan);
2816           scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
2817                                   scan);
2818           break;
2819         case DFmode:
2820         case DImode:
2821           if (need_align)
2822             {
2823               need_align = 0;
2824               scan = emit_label_after (gen_label_rtx (), scan);
2825               scan = emit_insn_after (gen_align_4 (), scan);
2826             }
2827           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2828             scan = emit_label_after (lab, scan);
2829           scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2830                                   scan);
2831           break;
2832         default:
2833           abort ();
2834           break;
2835         }
2836
2837       if (p->mode != HImode)
2838         {
2839           for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2840             {
2841               lab = XEXP (ref, 0);
2842               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2843             }
2844         }
2845     }
2846
2847   scan = emit_insn_after (gen_consttable_end (), scan);
2848   scan = emit_barrier_after (scan);
2849   pool_size = 0;
2850   pool_window_label = NULL_RTX;
2851   pool_window_last = 0;
2852 }
2853
2854 /* Return nonzero if constant would be an ok source for a
2855    mov.w instead of a mov.l.  */
2856
2857 static int
2858 hi_const (src)
2859      rtx src;
2860 {
2861   return (GET_CODE (src) == CONST_INT
2862           && INTVAL (src) >= -32768
2863           && INTVAL (src) <= 32767);
2864 }
2865
2866 /* Nonzero if the insn is a move instruction which needs to be fixed.  */
2867
2868 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
2869    CONST_DOUBLE input value is CONST_OK_FOR_I08.  For a SFmode move, we don't
2870    need to fix it if the input value is CONST_OK_FOR_I08.  */
2871
2872 static int
2873 broken_move (insn)
2874      rtx insn;
2875 {
2876   if (GET_CODE (insn) == INSN)
2877     {
2878       rtx pat = PATTERN (insn);
2879       if (GET_CODE (pat) == PARALLEL)
2880         pat = XVECEXP (pat, 0, 0);
2881       if (GET_CODE (pat) == SET
2882           /* We can load any 8 bit value if we don't care what the high
2883              order bits end up as.  */
2884           && GET_MODE (SET_DEST (pat)) != QImode
2885           && (CONSTANT_P (SET_SRC (pat))
2886               /* Match mova_const.  */
2887               || (GET_CODE (SET_SRC (pat)) == UNSPEC
2888                   && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
2889                   && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
2890           && ! (TARGET_SH2E
2891                 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
2892                 && (fp_zero_operand (SET_SRC (pat))
2893                     || fp_one_operand (SET_SRC (pat)))
2894                 /* ??? If this is a -m4 or -m4-single compilation, in general
2895                    we don't know the current setting of fpscr, so disable fldi.
2896                    There is an exception if this was a register-register move
2897                    before reload - and hence it was ascertained that we have
2898                    single precision setting - and in a post-reload optimization
2899                    we changed this to do a constant load.  In that case
2900                    we don't have an r0 clobber, hence we must use fldi.  */
2901                 && (! TARGET_SH4 || TARGET_FMOVD
2902                     || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
2903                         == SCRATCH))
2904                 && GET_CODE (SET_DEST (pat)) == REG
2905                 && FP_REGISTER_P (REGNO (SET_DEST (pat))))
2906           && (GET_CODE (SET_SRC (pat)) != CONST_INT
2907               || ! CONST_OK_FOR_I08 (INTVAL (SET_SRC (pat)))))
2908         return 1;
2909     }
2910
2911   return 0;
2912 }
2913
2914 static int
2915 mova_p (insn)
2916      rtx insn;
2917 {
2918   return (GET_CODE (insn) == INSN
2919           && GET_CODE (PATTERN (insn)) == SET
2920           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
2921           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
2922           /* Don't match mova_const.  */
2923           && GET_CODE (XVECEXP (SET_SRC (PATTERN (insn)), 0, 0)) == LABEL_REF);
2924 }
2925
2926 /* Find the last barrier from insn FROM which is close enough to hold the
2927    constant pool.  If we can't find one, then create one near the end of
2928    the range.  */
2929
2930 static rtx
2931 find_barrier (num_mova, mova, from)
2932      int num_mova;
2933      rtx mova, from;
2934 {
2935   int count_si = 0;
2936   int count_hi = 0;
2937   int found_hi = 0;
2938   int found_si = 0;
2939   int found_di = 0;
2940   int hi_align = 2;
2941   int si_align = 2;
2942   int leading_mova = num_mova;
2943   rtx barrier_before_mova = 0, found_barrier = 0, good_barrier = 0;
2944   int si_limit;
2945   int hi_limit;
2946
2947   /* For HImode: range is 510, add 4 because pc counts from address of
2948      second instruction after this one, subtract 2 for the jump instruction
2949      that we may need to emit before the table, subtract 2 for the instruction
2950      that fills the jump delay slot (in very rare cases, reorg will take an
2951      instruction from after the constant pool or will leave the delay slot
2952      empty).  This gives 510.
2953      For SImode: range is 1020, add 4 because pc counts from address of
2954      second instruction after this one, subtract 2 in case pc is 2 byte
2955      aligned, subtract 2 for the jump instruction that we may need to emit
2956      before the table, subtract 2 for the instruction that fills the jump
2957      delay slot.  This gives 1018.  */
2958
2959   /* The branch will always be shortened now that the reference address for
2960      forward branches is the successor address, thus we need no longer make
2961      adjustments to the [sh]i_limit for -O0.  */
2962
2963   si_limit = 1018;
2964   hi_limit = 510;
2965
2966   while (from && count_si < si_limit && count_hi < hi_limit)
2967     {
2968       int inc = get_attr_length (from);
2969       int new_align = 1;
2970
2971       if (GET_CODE (from) == CODE_LABEL)
2972         {
2973           if (optimize)
2974             new_align = 1 << label_to_alignment (from);
2975           else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
2976             new_align = 1 << barrier_align (from);
2977           else
2978             new_align = 1;
2979           inc = 0;
2980         }
2981
2982       if (GET_CODE (from) == BARRIER)
2983         {
2984
2985           found_barrier = from;
2986
2987           /* If we are at the end of the function, or in front of an alignment
2988              instruction, we need not insert an extra alignment.  We prefer
2989              this kind of barrier.  */
2990           if (barrier_align (from) > 2)
2991             good_barrier = from;
2992         }
2993
2994       if (broken_move (from))
2995         {
2996           rtx pat, src, dst;
2997           enum machine_mode mode;
2998
2999           pat = PATTERN (from);
3000           if (GET_CODE (pat) == PARALLEL)
3001             pat = XVECEXP (pat, 0, 0);
3002           src = SET_SRC (pat);
3003           dst = SET_DEST (pat);
3004           mode = GET_MODE (dst);
3005
3006           /* We must explicitly check the mode, because sometimes the
3007              front end will generate code to load unsigned constants into
3008              HImode targets without properly sign extending them.  */
3009           if (mode == HImode
3010               || (mode == SImode && hi_const (src) && REGNO (dst) != FPUL_REG))
3011             {
3012               found_hi += 2;
3013               /* We put the short constants before the long constants, so
3014                  we must count the length of short constants in the range
3015                  for the long constants.  */
3016               /* ??? This isn't optimal, but is easy to do.  */
3017               si_limit -= 2;
3018             }
3019           else
3020             {
3021               /* We dump DF/DI constants before SF/SI ones, because
3022                  the limit is the same, but the alignment requirements
3023                  are higher.  We may waste up to 4 additional bytes
3024                  for alignment, and the DF/DI constant may have
3025                  another SF/SI constant placed before it. */
3026               if (TARGET_SHCOMPACT
3027                   && ! found_di
3028                   && (mode == DFmode || mode == DImode))
3029                 {
3030                   found_di = 1;
3031                   si_limit -= 8;
3032                 }
3033               while (si_align > 2 && found_si + si_align - 2 > count_si)
3034                 si_align >>= 1;
3035               if (found_si > count_si)
3036                 count_si = found_si;
3037               found_si += GET_MODE_SIZE (mode);
3038               if (num_mova)
3039                 si_limit -= GET_MODE_SIZE (mode);
3040             }
3041
3042           /* See the code in machine_dependent_reorg, which has a similar if
3043              statement that generates a new mova insn in many cases.  */
3044           if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
3045             inc += 2;
3046         }
3047
3048       if (mova_p (from))
3049         {
3050           if (! num_mova++)
3051             {
3052               leading_mova = 0;
3053               mova = from;
3054               barrier_before_mova = good_barrier ? good_barrier : found_barrier;
3055             }
3056           if (found_si > count_si)
3057             count_si = found_si;
3058         }
3059       else if (GET_CODE (from) == JUMP_INSN
3060                && (GET_CODE (PATTERN (from)) == ADDR_VEC
3061                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
3062         {
3063           if (num_mova)
3064             num_mova--;
3065           if (barrier_align (next_real_insn (from)) == align_jumps_log)
3066             {
3067               /* We have just passed the barrier in front of the
3068                  ADDR_DIFF_VEC, which is stored in found_barrier.  Since
3069                  the ADDR_DIFF_VEC is accessed as data, just like our pool
3070                  constants, this is a good opportunity to accommodate what
3071                  we have gathered so far.
3072                  If we waited any longer, we could end up at a barrier in
3073                  front of code, which gives worse cache usage for separated
3074                  instruction / data caches.  */
3075               good_barrier = found_barrier;
3076               break;
3077             }
3078           else
3079             {
3080               rtx body = PATTERN (from);
3081               inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
3082             }
3083         }
3084       /* For the SH1, we generate alignments even after jumps-around-jumps.  */
3085       else if (GET_CODE (from) == JUMP_INSN
3086                && ! TARGET_SH2
3087                && ! TARGET_SMALLCODE)
3088         new_align = 4;
3089
3090       if (found_si)
3091         {
3092           count_si += inc;
3093           if (new_align > si_align)
3094             {
3095               si_limit -= (count_si - 1) & (new_align - si_align);
3096               si_align = new_align;
3097             }
3098           count_si = (count_si + new_align - 1) & -new_align;
3099         }
3100       if (found_hi)
3101         {
3102           count_hi += inc;
3103           if (new_align > hi_align)
3104             {
3105               hi_limit -= (count_hi - 1) & (new_align - hi_align);
3106               hi_align = new_align;
3107             }
3108           count_hi = (count_hi + new_align - 1) & -new_align;
3109         }
3110       from = NEXT_INSN (from);
3111     }
3112
3113   if (num_mova)
3114     {
3115       if (leading_mova)
3116         {
3117           /* Try as we might, the leading mova is out of range.  Change
3118              it into a load (which will become a pcload) and retry.  */
3119           SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
3120           INSN_CODE (mova) = -1;
3121           return find_barrier (0, 0, mova);
3122         }
3123       else
3124         {
3125           /* Insert the constant pool table before the mova instruction,
3126              to prevent the mova label reference from going out of range.  */
3127           from = mova;
3128           good_barrier = found_barrier = barrier_before_mova;
3129         }
3130     }
3131
3132   if (found_barrier)
3133     {
3134       if (good_barrier && next_real_insn (found_barrier))
3135         found_barrier = good_barrier;
3136     }
3137   else
3138     {
3139       /* We didn't find a barrier in time to dump our stuff,
3140          so we'll make one.  */
3141       rtx label = gen_label_rtx ();
3142
3143       /* If we exceeded the range, then we must back up over the last
3144          instruction we looked at.  Otherwise, we just need to undo the
3145          NEXT_INSN at the end of the loop.  */
3146       if (count_hi > hi_limit || count_si > si_limit)
3147         from = PREV_INSN (PREV_INSN (from));
3148       else
3149         from = PREV_INSN (from);
3150
3151       /* Walk back to be just before any jump or label.
3152          Putting it before a label reduces the number of times the branch
3153          around the constant pool table will be hit.  Putting it before
3154          a jump makes it more likely that the bra delay slot will be
3155          filled.  */
3156       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
3157              || GET_CODE (from) == CODE_LABEL)
3158         from = PREV_INSN (from);
3159
3160       from = emit_jump_insn_after (gen_jump (label), from);
3161       JUMP_LABEL (from) = label;
3162       LABEL_NUSES (label) = 1;
3163       found_barrier = emit_barrier_after (from);
3164       emit_label_after (label, found_barrier);
3165     }
3166
3167   return found_barrier;
3168 }
3169
3170 /* If the instruction INSN is implemented by a special function, and we can
3171    positively find the register that is used to call the sfunc, and this
3172    register is not used anywhere else in this instruction - except as the
3173    destination of a set, return this register; else, return 0.  */
3174 rtx
3175 sfunc_uses_reg (insn)
3176      rtx insn;
3177 {
3178   int i;
3179   rtx pattern, part, reg_part, reg;
3180
3181   if (GET_CODE (insn) != INSN)
3182     return 0;
3183   pattern = PATTERN (insn);
3184   if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
3185     return 0;
3186
3187   for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
3188     {
3189       part = XVECEXP (pattern, 0, i);
3190       if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
3191         reg_part = part;
3192     }
3193   if (! reg_part)
3194     return 0;
3195   reg = XEXP (reg_part, 0);
3196   for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
3197     {
3198       part = XVECEXP (pattern, 0, i);
3199       if (part == reg_part || GET_CODE (part) == CLOBBER)
3200         continue;
3201       if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
3202                                   && GET_CODE (SET_DEST (part)) == REG)
3203                                  ? SET_SRC (part) : part)))
3204         return 0;
3205     }
3206   return reg;
3207 }
3208
3209 /* See if the only way in which INSN uses REG is by calling it, or by
3210    setting it while calling it.  Set *SET to a SET rtx if the register
3211    is set by INSN.  */
3212
3213 static int
3214 noncall_uses_reg (reg, insn, set)
3215      rtx reg;
3216      rtx insn;
3217      rtx *set;
3218 {
3219   rtx pattern, reg2;
3220
3221   *set = NULL_RTX;
3222
3223   reg2 = sfunc_uses_reg (insn);
3224   if (reg2 && REGNO (reg2) == REGNO (reg))
3225     {
3226       pattern = single_set (insn);
3227       if (pattern
3228           && GET_CODE (SET_DEST (pattern)) == REG
3229           && REGNO (reg) == REGNO (SET_DEST (pattern)))
3230         *set = pattern;
3231       return 0;
3232     }
3233   if (GET_CODE (insn) != CALL_INSN)
3234     {
3235       /* We don't use rtx_equal_p because we don't care if the mode is
3236          different.  */
3237       pattern = single_set (insn);
3238       if (pattern
3239           && GET_CODE (SET_DEST (pattern)) == REG
3240           && REGNO (reg) == REGNO (SET_DEST (pattern)))
3241         {
3242           rtx par, part;
3243           int i;
3244
3245           *set = pattern;
3246           par = PATTERN (insn);
3247           if (GET_CODE (par) == PARALLEL)
3248             for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
3249               {
3250                 part = XVECEXP (par, 0, i);
3251                 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
3252                   return 1;
3253               }
3254           return reg_mentioned_p (reg, SET_SRC (pattern));
3255         }
3256
3257       return 1;
3258     }
3259
3260   pattern = PATTERN (insn);
3261
3262   if (GET_CODE (pattern) == PARALLEL)
3263     {
3264       int i;
3265
3266       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
3267         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
3268           return 1;
3269       pattern = XVECEXP (pattern, 0, 0);
3270     }
3271
3272   if (GET_CODE (pattern) == SET)
3273     {
3274       if (reg_mentioned_p (reg, SET_DEST (pattern)))
3275         {
3276           /* We don't use rtx_equal_p, because we don't care if the
3277              mode is different.  */
3278           if (GET_CODE (SET_DEST (pattern)) != REG
3279               || REGNO (reg) != REGNO (SET_DEST (pattern)))
3280             return 1;
3281
3282           *set = pattern;
3283         }
3284
3285       pattern = SET_SRC (pattern);
3286     }
3287
3288   if (GET_CODE (pattern) != CALL
3289       || GET_CODE (XEXP (pattern, 0)) != MEM
3290       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
3291     return 1;
3292
3293   return 0;
3294 }
3295
3296 /* Given a X, a pattern of an insn or a part of it, return a mask of used
3297    general registers.  Bits 0..15 mean that the respective registers
3298    are used as inputs in the instruction.  Bits 16..31 mean that the
3299    registers 0..15, respectively, are used as outputs, or are clobbered.
3300    IS_DEST should be set to 16 if X is the destination of a SET, else to 0.  */
3301 int
3302 regs_used (x, is_dest)
3303      rtx x; int is_dest;
3304 {
3305   enum rtx_code code;
3306   const char *fmt;
3307   int i, used = 0;
3308
3309   if (! x)
3310     return used;
3311   code = GET_CODE (x);
3312   switch (code)
3313     {
3314     case REG:
3315       if (REGNO (x) < 16)
3316         return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
3317                 << (REGNO (x) + is_dest));
3318       return 0;
3319     case SUBREG:
3320       {
3321         rtx y = SUBREG_REG (x);
3322      
3323         if (GET_CODE (y) != REG)
3324           break;
3325         if (REGNO (y) < 16)
3326           return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
3327                   << (REGNO (y) +
3328                       subreg_regno_offset (REGNO (y),
3329                                            GET_MODE (y),
3330                                            SUBREG_BYTE (x),
3331                                            GET_MODE (x)) + is_dest));
3332         return 0;
3333       }
3334     case SET:
3335       return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
3336     case RETURN:
3337       /* If there was a return value, it must have been indicated with USE.  */
3338       return 0x00ffff00;
3339     case CLOBBER:
3340       is_dest = 1;
3341       break;
3342     case MEM:
3343       is_dest = 0;
3344       break;
3345     case CALL:
3346       used |= 0x00ff00f0;
3347       break;
3348     default:
3349       break;
3350     }
3351
3352   fmt = GET_RTX_FORMAT (code);
3353
3354   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3355     {
3356       if (fmt[i] == 'E')
3357         {
3358           register int j;
3359           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3360             used |= regs_used (XVECEXP (x, i, j), is_dest);
3361         }
3362       else if (fmt[i] == 'e')
3363         used |= regs_used (XEXP (x, i), is_dest);
3364     }
3365   return used;
3366 }
3367
3368 /* Create an instruction that prevents redirection of a conditional branch
3369    to the destination of the JUMP with address ADDR.
3370    If the branch needs to be implemented as an indirect jump, try to find
3371    a scratch register for it.
3372    If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
3373    If any preceding insn that doesn't fit into a delay slot is good enough,
3374    pass 1.  Pass 2 if a definite blocking insn is needed.
3375    -1 is used internally to avoid deep recursion.
3376    If a blocking instruction is made or recognized, return it.  */
3377    
3378 static rtx
3379 gen_block_redirect (jump, addr, need_block)
3380      rtx jump;
3381      int addr, need_block;
3382 {
3383   int dead = 0;
3384   rtx prev = prev_nonnote_insn (jump);
3385   rtx dest;
3386
3387   /* First, check if we already have an instruction that satisfies our need.  */
3388   if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
3389     {
3390       if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
3391         return prev;
3392       if (GET_CODE (PATTERN (prev)) == USE
3393           || GET_CODE (PATTERN (prev)) == CLOBBER
3394           || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3395         prev = jump;
3396       else if ((need_block &= ~1) < 0)
3397         return prev;
3398       else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
3399         need_block = 0;
3400     }
3401   /* We can't use JUMP_LABEL here because it might be undefined
3402      when not optimizing.  */
3403   dest = XEXP (SET_SRC (PATTERN (jump)), 0);
3404   /* If the branch is out of range, try to find a scratch register for it.  */
3405   if (optimize
3406       && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3407           > 4092 + 4098))
3408     {
3409       rtx scan;
3410       /* Don't look for the stack pointer as a scratch register,
3411          it would cause trouble if an interrupt occurred.  */
3412       unsigned try = 0x7fff, used;
3413       int jump_left = flag_expensive_optimizations + 1;
3414     
3415       /* It is likely that the most recent eligible instruction is wanted for
3416          the delay slot.  Therefore, find out which registers it uses, and
3417          try to avoid using them.  */
3418          
3419       for (scan = jump; (scan = PREV_INSN (scan)); )
3420         {
3421           enum rtx_code code;
3422
3423           if (INSN_DELETED_P (scan))
3424             continue;
3425           code = GET_CODE (scan);
3426           if (code == CODE_LABEL || code == JUMP_INSN)
3427             break;
3428           if (code == INSN
3429               && GET_CODE (PATTERN (scan)) != USE
3430               && GET_CODE (PATTERN (scan)) != CLOBBER
3431               && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
3432             {
3433               try &= ~regs_used (PATTERN (scan), 0);
3434               break;
3435             }
3436         }
3437       for (used = dead = 0, scan = JUMP_LABEL (jump);
3438            (scan = NEXT_INSN (scan)); )
3439         {
3440           enum rtx_code code;
3441
3442           if (INSN_DELETED_P (scan))
3443             continue;
3444           code = GET_CODE (scan);
3445           if (GET_RTX_CLASS (code) == 'i')
3446             {
3447               used |= regs_used (PATTERN (scan), 0);
3448               if (code == CALL_INSN)
3449                 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
3450               dead |= (used >> 16) & ~used;
3451               if (dead & try)
3452                 {
3453                   dead &= try;
3454                   break;
3455                 }
3456               if (code == JUMP_INSN)
3457                 {
3458                   if (jump_left-- && simplejump_p (scan))
3459                     scan = JUMP_LABEL (scan);
3460                   else
3461                     break;
3462                 }
3463             }
3464         }
3465       /* Mask out the stack pointer again, in case it was
3466          the only 'free' register we have found.  */
3467       dead &= 0x7fff;
3468     }
3469   /* If the immediate destination is still in range, check for possible
3470      threading with a jump beyond the delay slot insn.
3471      Don't check if we are called recursively; the jump has been or will be
3472      checked in a different invocation then.  */
3473         
3474   else if (optimize && need_block >= 0)
3475     {
3476       rtx next = next_active_insn (next_active_insn (dest));
3477       if (next && GET_CODE (next) == JUMP_INSN
3478           && GET_CODE (PATTERN (next)) == SET
3479           && recog_memoized (next) == CODE_FOR_jump_compact)
3480         {
3481           dest = JUMP_LABEL (next);
3482           if (dest
3483               && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3484                   > 4092 + 4098))
3485             gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
3486         }
3487     }
3488
3489   if (dead)
3490     {
3491       rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
3492
3493       /* It would be nice if we could convert the jump into an indirect
3494          jump / far branch right now, and thus exposing all constituent
3495          instructions to further optimization.  However, reorg uses
3496          simplejump_p to determine if there is an unconditional jump where
3497          it should try to schedule instructions from the target of the
3498          branch; simplejump_p fails for indirect jumps even if they have
3499          a JUMP_LABEL.  */
3500       rtx insn = emit_insn_before (gen_indirect_jump_scratch
3501                                    (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
3502                                    , jump);
3503       /* ??? We would like this to have the scope of the jump, but that
3504          scope will change when a delay slot insn of an inner scope is added.
3505          Hence, after delay slot scheduling, we'll have to expect
3506          NOTE_INSN_BLOCK_END notes between the indirect_jump_scratch and
3507          the jump.  */
3508          
3509       INSN_LOCATOR (insn) = INSN_LOCATOR (jump);
3510       INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
3511       return insn;
3512     }
3513   else if (need_block)
3514     /* We can't use JUMP_LABEL here because it might be undefined
3515        when not optimizing.  */
3516     return emit_insn_before (gen_block_branch_redirect
3517                       (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
3518                       , jump);
3519   return prev;
3520 }
3521
3522 #define CONDJUMP_MIN -252
3523 #define CONDJUMP_MAX 262
3524 struct far_branch
3525 {
3526   /* A label (to be placed) in front of the jump
3527      that jumps to our ultimate destination.  */
3528   rtx near_label;
3529   /* Where we are going to insert it if we cannot move the jump any farther,
3530      or the jump itself if we have picked up an existing jump.  */
3531   rtx insert_place;
3532   /* The ultimate destination.  */
3533   rtx far_label;
3534   struct far_branch *prev;
3535   /* If the branch has already been created, its address;
3536      else the address of its first prospective user.  */
3537   int address;
3538 };
3539
3540 static void gen_far_branch PARAMS ((struct far_branch *));
3541 enum mdep_reorg_phase_e mdep_reorg_phase;
3542 static void
3543 gen_far_branch (bp)
3544      struct far_branch *bp;
3545 {
3546   rtx insn = bp->insert_place;
3547   rtx jump;
3548   rtx label = gen_label_rtx ();
3549
3550   emit_label_after (label, insn);
3551   if (bp->far_label)
3552     {
3553       jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
3554       LABEL_NUSES (bp->far_label)++;
3555     }
3556   else
3557     jump = emit_jump_insn_after (gen_return (), insn);
3558   /* Emit a barrier so that reorg knows that any following instructions
3559      are not reachable via a fall-through path.
3560      But don't do this when not optimizing, since we wouldn't suppress the
3561      alignment for the barrier then, and could end up with out-of-range
3562      pc-relative loads.  */
3563   if (optimize)
3564     emit_barrier_after (jump);
3565   emit_label_after (bp->near_label, insn);
3566   JUMP_LABEL (jump) = bp->far_label;
3567   if (! invert_jump (insn, label, 1))
3568     abort ();
3569   (emit_insn_after
3570    (gen_stuff_delay_slot
3571     (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))),
3572      GEN_INT (recog_memoized (insn) == CODE_FOR_branch_false)),
3573     insn));
3574   /* Prevent reorg from undoing our splits.  */
3575   gen_block_redirect (jump, bp->address += 2, 2);
3576 }
3577
3578 /* Fix up ADDR_DIFF_VECs.  */
3579 void
3580 fixup_addr_diff_vecs (first)
3581      rtx first;
3582 {
3583   rtx insn;
3584
3585   for (insn = first; insn; insn = NEXT_INSN (insn))
3586     {
3587       rtx vec_lab, pat, prev, prevpat, x, braf_label;
3588
3589       if (GET_CODE (insn) != JUMP_INSN
3590           || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
3591         continue;
3592       pat = PATTERN (insn);
3593       vec_lab = XEXP (XEXP (pat, 0), 0);
3594
3595       /* Search the matching casesi_jump_2.  */
3596       for (prev = vec_lab; ; prev = PREV_INSN (prev))
3597         {
3598           if (GET_CODE (prev) != JUMP_INSN)
3599             continue;
3600           prevpat = PATTERN (prev);
3601           if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
3602             continue;
3603           x = XVECEXP (prevpat, 0, 1);
3604           if (GET_CODE (x) != USE)
3605             continue;
3606           x = XEXP (x, 0);
3607           if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
3608             break;
3609         }
3610
3611       /* Emit the reference label of the braf where it belongs, right after
3612          the casesi_jump_2 (i.e. braf).  */
3613       braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
3614       emit_label_after (braf_label, prev);
3615
3616       /* Fix up the ADDR_DIF_VEC to be relative
3617          to the reference address of the braf.  */
3618       XEXP (XEXP (pat, 0), 0) = braf_label;
3619     }
3620 }
3621
3622 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
3623    a barrier.  Return the base 2 logarithm of the desired alignment.  */
3624 int
3625 barrier_align (barrier_or_label)
3626      rtx barrier_or_label;
3627 {
3628   rtx next = next_real_insn (barrier_or_label), pat, prev;
3629   int slot, credit, jump_to_next = 0;
3630  
3631   if (! next)
3632     return 0;
3633
3634   pat = PATTERN (next);
3635
3636   if (GET_CODE (pat) == ADDR_DIFF_VEC)
3637     return 2;
3638
3639   if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
3640     /* This is a barrier in front of a constant table.  */
3641     return 0;
3642
3643   prev = prev_real_insn (barrier_or_label);
3644   if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
3645     {
3646       pat = PATTERN (prev);
3647       /* If this is a very small table, we want to keep the alignment after
3648          the table to the minimum for proper code alignment.  */
3649       return ((TARGET_SMALLCODE
3650                || ((unsigned) XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
3651                    <= (unsigned)1 << (CACHE_LOG - 2)))
3652               ? 1 << TARGET_SHMEDIA : align_jumps_log);
3653     }
3654
3655   if (TARGET_SMALLCODE)
3656     return 0;
3657
3658   if (! TARGET_SH2 || ! optimize)
3659     return align_jumps_log;
3660
3661   /* When fixing up pcloads, a constant table might be inserted just before
3662      the basic block that ends with the barrier.  Thus, we can't trust the
3663      instruction lengths before that.  */
3664   if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
3665     {
3666       /* Check if there is an immediately preceding branch to the insn beyond
3667          the barrier.  We must weight the cost of discarding useful information
3668          from the current cache line when executing this branch and there is
3669          an alignment, against that of fetching unneeded insn in front of the
3670          branch target when there is no alignment.  */
3671
3672       /* There are two delay_slot cases to consider.  One is the simple case 
3673          where the preceding branch is to the insn beyond the barrier (simple 
3674          delay slot filling), and the other is where the preceding branch has 
3675          a delay slot that is a duplicate of the insn after the barrier 
3676          (fill_eager_delay_slots) and the branch is to the insn after the insn 
3677          after the barrier.  */
3678
3679       /* PREV is presumed to be the JUMP_INSN for the barrier under
3680          investigation.  Skip to the insn before it.  */
3681       prev = prev_real_insn (prev);
3682
3683       for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
3684            credit >= 0 && prev && GET_CODE (prev) == INSN;
3685            prev = prev_real_insn (prev))
3686         {
3687           jump_to_next = 0;
3688           if (GET_CODE (PATTERN (prev)) == USE
3689               || GET_CODE (PATTERN (prev)) == CLOBBER)
3690             continue;
3691           if (GET_CODE (PATTERN (prev)) == SEQUENCE)
3692             {
3693               prev = XVECEXP (PATTERN (prev), 0, 1);
3694               if (INSN_UID (prev) == INSN_UID (next)) 
3695                 {
3696                   /* Delay slot was filled with insn at jump target.  */
3697                   jump_to_next = 1;
3698                   continue;
3699                 }
3700             }
3701
3702           if (slot &&
3703               get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3704             slot = 0;
3705           credit -= get_attr_length (prev);
3706         }
3707       if (prev
3708           && GET_CODE (prev) == JUMP_INSN
3709           && JUMP_LABEL (prev))
3710         {
3711           rtx x;
3712           if (jump_to_next
3713               || next_real_insn (JUMP_LABEL (prev)) == next
3714               /* If relax_delay_slots() decides NEXT was redundant
3715                  with some previous instruction, it will have
3716                  redirected PREV's jump to the following insn.  */
3717               || JUMP_LABEL (prev) == next_nonnote_insn (next)
3718               /* There is no upper bound on redundant instructions
3719                  that might have been skipped, but we must not put an
3720                  alignment where none had been before.  */
3721               || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),           
3722                   (INSN_P (x) 
3723                    && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
3724                        || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch
3725                        || INSN_CODE (x) == CODE_FOR_stuff_delay_slot))))
3726             {
3727               rtx pat = PATTERN (prev);
3728               if (GET_CODE (pat) == PARALLEL)
3729                 pat = XVECEXP (pat, 0, 0);
3730               if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
3731                 return 0;
3732             }
3733         }
3734     }
3735   
3736   return align_jumps_log;
3737 }
3738
3739 /* If we are inside a phony loop, almost any kind of label can turn up as the
3740    first one in the loop.  Aligning a braf label causes incorrect switch
3741    destination addresses; we can detect braf labels because they are
3742    followed by a BARRIER.
3743    Applying loop alignment to small constant or switch tables is a waste
3744    of space, so we suppress this too.  */
3745 int
3746 sh_loop_align (label)
3747      rtx label;
3748 {
3749   rtx next = label;
3750
3751   do
3752     next = next_nonnote_insn (next);
3753   while (next && GET_CODE (next) == CODE_LABEL);
3754
3755   if (! next
3756       || ! INSN_P (next)
3757       || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC
3758       || recog_memoized (next) == CODE_FOR_consttable_2)
3759     return 0;
3760
3761   return align_loops_log;
3762 }
3763
3764 /* Do a final pass over the function, just before delayed branch
3765    scheduling.  */
3766
3767 static void
3768 sh_reorg ()
3769 {
3770   rtx first, insn, mova = NULL_RTX;
3771   int num_mova;
3772   rtx r0_rtx = gen_rtx_REG (Pmode, 0);
3773   rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
3774
3775   first = get_insns ();
3776
3777   /* We must split call insns before introducing `mova's.  If we're
3778      optimizing, they'll have already been split.  Otherwise, make
3779      sure we don't split them too late.  */
3780   if (! optimize)
3781     split_all_insns_noflow ();
3782
3783   if (TARGET_SHMEDIA)
3784     return;
3785
3786   /* If relaxing, generate pseudo-ops to associate function calls with
3787      the symbols they call.  It does no harm to not generate these
3788      pseudo-ops.  However, when we can generate them, it enables to
3789      linker to potentially relax the jsr to a bsr, and eliminate the
3790      register load and, possibly, the constant pool entry.  */
3791
3792   mdep_reorg_phase = SH_INSERT_USES_LABELS;
3793   if (TARGET_RELAX)
3794     {
3795       /* Remove all REG_LABEL notes.  We want to use them for our own
3796          purposes.  This works because none of the remaining passes
3797          need to look at them.
3798
3799          ??? But it may break in the future.  We should use a machine
3800          dependent REG_NOTE, or some other approach entirely.  */
3801       for (insn = first; insn; insn = NEXT_INSN (insn))
3802         {
3803           if (INSN_P (insn))
3804             {
3805               rtx note;
3806
3807               while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
3808                 remove_note (insn, note);
3809             }
3810         }
3811
3812       for (insn = first; insn; insn = NEXT_INSN (insn))
3813         {
3814           rtx pattern, reg, link, set, scan, dies, label;
3815           int rescan = 0, foundinsn = 0;
3816
3817           if (GET_CODE (insn) == CALL_INSN)
3818             {
3819               pattern = PATTERN (insn);
3820
3821               if (GET_CODE (pattern) == PARALLEL)
3822                 pattern = XVECEXP (pattern, 0, 0);
3823               if (GET_CODE (pattern) == SET)
3824                 pattern = SET_SRC (pattern);
3825
3826               if (GET_CODE (pattern) != CALL
3827                   || GET_CODE (XEXP (pattern, 0)) != MEM)
3828                 continue;
3829
3830               reg = XEXP (XEXP (pattern, 0), 0);
3831             }
3832           else
3833             {
3834               reg = sfunc_uses_reg (insn);
3835               if (! reg)
3836                 continue;
3837             }
3838
3839           if (GET_CODE (reg) != REG)
3840             continue;
3841
3842           /* This is a function call via REG.  If the only uses of REG
3843              between the time that it is set and the time that it dies
3844              are in function calls, then we can associate all the
3845              function calls with the setting of REG.  */
3846
3847           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3848             {
3849               if (REG_NOTE_KIND (link) != 0)
3850                 continue;
3851               set = single_set (XEXP (link, 0));
3852               if (set && rtx_equal_p (reg, SET_DEST (set)))
3853                 {
3854                   link = XEXP (link, 0);
3855                   break;
3856                 }
3857             }
3858
3859           if (! link)
3860             {
3861               /* ??? Sometimes global register allocation will have
3862                  deleted the insn pointed to by LOG_LINKS.  Try
3863                  scanning backward to find where the register is set.  */
3864               for (scan = PREV_INSN (insn);
3865                    scan && GET_CODE (scan) != CODE_LABEL;
3866                    scan = PREV_INSN (scan))
3867                 {
3868                   if (! INSN_P (scan))
3869                     continue;
3870
3871                   if (! reg_mentioned_p (reg, scan))
3872                     continue;
3873
3874                   if (noncall_uses_reg (reg, scan, &set))
3875                     break;
3876
3877                   if (set)
3878                     {
3879                       link = scan;
3880                       break;
3881                     }
3882                 }
3883             }
3884
3885           if (! link)
3886             continue;
3887
3888           /* The register is set at LINK.  */
3889
3890           /* We can only optimize the function call if the register is
3891              being set to a symbol.  In theory, we could sometimes
3892              optimize calls to a constant location, but the assembler
3893              and linker do not support that at present.  */
3894           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
3895               && GET_CODE (SET_SRC (set)) != LABEL_REF)
3896             continue;
3897
3898           /* Scan forward from LINK to the place where REG dies, and
3899              make sure that the only insns which use REG are
3900              themselves function calls.  */
3901
3902           /* ??? This doesn't work for call targets that were allocated
3903              by reload, since there may not be a REG_DEAD note for the
3904              register.  */
3905
3906           dies = NULL_RTX;
3907           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
3908             {
3909               rtx scanset;
3910
3911               /* Don't try to trace forward past a CODE_LABEL if we haven't
3912                  seen INSN yet.  Ordinarily, we will only find the setting insn
3913                  in LOG_LINKS if it is in the same basic block.  However,
3914                  cross-jumping can insert code labels in between the load and
3915                  the call, and can result in situations where a single call
3916                  insn may have two targets depending on where we came from.  */
3917
3918               if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
3919                 break;
3920
3921               if (! INSN_P (scan))
3922                 continue;
3923
3924               /* Don't try to trace forward past a JUMP.  To optimize
3925                  safely, we would have to check that all the
3926                  instructions at the jump destination did not use REG.  */
3927
3928               if (GET_CODE (scan) == JUMP_INSN)
3929                 break;
3930
3931               if (! reg_mentioned_p (reg, scan))
3932                 continue;
3933
3934               if (noncall_uses_reg (reg, scan, &scanset))
3935                 break;
3936
3937               if (scan == insn)
3938                 foundinsn = 1;
3939
3940               if (scan != insn
3941                   && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
3942                 {
3943                   /* There is a function call to this register other
3944                      than the one we are checking.  If we optimize
3945                      this call, we need to rescan again below.  */
3946                   rescan = 1;
3947                 }
3948
3949               /* ??? We shouldn't have to worry about SCANSET here.
3950                  We should just be able to check for a REG_DEAD note
3951                  on a function call.  However, the REG_DEAD notes are
3952                  apparently not dependable around libcalls; c-torture
3953                  execute/920501-2 is a test case.  If SCANSET is set,
3954                  then this insn sets the register, so it must have
3955                  died earlier.  Unfortunately, this will only handle
3956                  the cases in which the register is, in fact, set in a
3957                  later insn.  */
3958
3959               /* ??? We shouldn't have to use FOUNDINSN here.
3960                  However, the LOG_LINKS fields are apparently not
3961                  entirely reliable around libcalls;
3962                  newlib/libm/math/e_pow.c is a test case.  Sometimes
3963                  an insn will appear in LOG_LINKS even though it is
3964                  not the most recent insn which sets the register.  */
3965
3966               if (foundinsn
3967                   && (scanset
3968                       || find_reg_note (scan, REG_DEAD, reg)))
3969                 {
3970                   dies = scan;
3971                   break;
3972                 }
3973             }
3974
3975           if (! dies)
3976             {
3977               /* Either there was a branch, or some insn used REG
3978                  other than as a function call address.  */
3979               continue;
3980             }
3981
3982           /* Create a code label, and put it in a REG_LABEL note on
3983              the insn which sets the register, and on each call insn
3984              which uses the register.  In final_prescan_insn we look
3985              for the REG_LABEL notes, and output the appropriate label
3986              or pseudo-op.  */
3987
3988           label = gen_label_rtx ();
3989           REG_NOTES (link) = gen_rtx_INSN_LIST (REG_LABEL, label,
3990                                                 REG_NOTES (link));
3991           REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
3992                                                 REG_NOTES (insn));
3993           if (rescan)
3994             {
3995               scan = link;
3996               do
3997                 {
3998                   rtx reg2;
3999
4000                   scan = NEXT_INSN (scan);
4001                   if (scan != insn
4002                       && ((GET_CODE (scan) == CALL_INSN
4003                            && reg_mentioned_p (reg, scan))
4004                           || ((reg2 = sfunc_uses_reg (scan))
4005                               && REGNO (reg2) == REGNO (reg))))
4006                     REG_NOTES (scan)
4007                       = gen_rtx_INSN_LIST (REG_LABEL, label, REG_NOTES (scan));
4008                 }
4009               while (scan != dies);
4010             }
4011         }
4012     }
4013
4014   if (TARGET_SH2)
4015     fixup_addr_diff_vecs (first);
4016
4017   if (optimize)
4018     {
4019       mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
4020       shorten_branches (first);
4021     }
4022   /* Scan the function looking for move instructions which have to be
4023      changed to pc-relative loads and insert the literal tables.  */
4024
4025   mdep_reorg_phase = SH_FIXUP_PCLOAD;
4026   for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
4027     {
4028       if (mova_p (insn))
4029         {
4030           if (! num_mova++)
4031             mova = insn;
4032         }
4033       else if (GET_CODE (insn) == JUMP_INSN
4034                && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
4035                && num_mova)
4036         {
4037           rtx scan;
4038           int total;
4039
4040           num_mova--;
4041
4042           /* Some code might have been inserted between the mova and
4043              its ADDR_DIFF_VEC.  Check if the mova is still in range.  */
4044           for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
4045             total += get_attr_length (scan);
4046
4047           /* range of mova is 1020, add 4 because pc counts from address of
4048              second instruction after this one, subtract 2 in case pc is 2
4049              byte aligned.  Possible alignment needed for the ADDR_DIFF_VEC
4050              cancels out with alignment effects of the mova itself.  */
4051           if (total > 1022)
4052             {
4053               /* Change the mova into a load, and restart scanning
4054                  there.  broken_move will then return true for mova.  */
4055               SET_SRC (PATTERN (mova))
4056                 = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
4057               INSN_CODE (mova) = -1;
4058               insn = mova;
4059             }
4060         }
4061       if (broken_move (insn))
4062         {
4063           rtx scan;
4064           /* Scan ahead looking for a barrier to stick the constant table
4065              behind.  */
4066           rtx barrier = find_barrier (num_mova, mova, insn);
4067           rtx last_float_move = NULL_RTX, last_float = 0, *last_float_addr = NULL;
4068
4069           if (num_mova && ! mova_p (mova))
4070             {
4071               /* find_barrier had to change the first mova into a
4072                  pcload; thus, we have to start with this new pcload.  */
4073               insn = mova;
4074               num_mova = 0;
4075             }
4076           /* Now find all the moves between the points and modify them.  */
4077           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
4078             {
4079               if (GET_CODE (scan) == CODE_LABEL)
4080                 last_float = 0;
4081               if (broken_move (scan))
4082                 {
4083                   rtx *patp = &PATTERN (scan), pat = *patp;
4084                   rtx src, dst;
4085                   rtx lab;
4086                   rtx newsrc;
4087                   enum machine_mode mode;
4088
4089                   if (GET_CODE (pat) == PARALLEL)
4090                     patp = &XVECEXP (pat, 0, 0), pat = *patp;
4091                   src = SET_SRC (pat);
4092                   dst = SET_DEST (pat);
4093                   mode = GET_MODE (dst);
4094
4095                   if (mode == SImode && hi_const (src)
4096                       && REGNO (dst) != FPUL_REG)
4097                     {
4098                       int offset = 0;
4099
4100                       mode = HImode;
4101                       while (GET_CODE (dst) == SUBREG)
4102                         {
4103                           offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
4104                                                          GET_MODE (SUBREG_REG (dst)),
4105                                                          SUBREG_BYTE (dst),
4106                                                          GET_MODE (dst));
4107                           dst = SUBREG_REG (dst);
4108                         }
4109                       dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
4110                     }
4111
4112                   if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
4113                     {
4114                       /* This must be an insn that clobbers r0.  */
4115                       rtx clobber = XVECEXP (PATTERN (scan), 0,
4116                                              XVECLEN (PATTERN (scan), 0) - 1);
4117
4118                       if (GET_CODE (clobber) != CLOBBER
4119                           || ! rtx_equal_p (XEXP (clobber, 0), r0_rtx))
4120                         abort ();
4121
4122                       if (last_float
4123                           && reg_set_between_p (r0_rtx, last_float_move, scan))
4124                         last_float = 0;
4125                       if (last_float
4126                           && TARGET_SHCOMPACT
4127                           && GET_MODE_SIZE (mode) != 4
4128                           && GET_MODE_SIZE (GET_MODE (last_float)) == 4)
4129                         last_float = 0;
4130                       lab = add_constant (src, mode, last_float);
4131                       if (lab)
4132                         emit_insn_before (gen_mova (lab), scan);
4133                       else
4134                         {
4135                           /* There will be a REG_UNUSED note for r0 on
4136                              LAST_FLOAT_MOVE; we have to change it to REG_INC,
4137                              lest reorg:mark_target_live_regs will not
4138                              consider r0 to be used, and we end up with delay
4139                              slot insn in front of SCAN that clobbers r0.  */
4140                           rtx note
4141                             = find_regno_note (last_float_move, REG_UNUSED, 0);
4142
4143                           /* If we are not optimizing, then there may not be
4144                              a note.  */
4145                           if (note)
4146                             PUT_MODE (note, REG_INC);
4147
4148                           *last_float_addr = r0_inc_rtx;
4149                         }
4150                       last_float_move = scan;
4151                       last_float = src;
4152                       newsrc = gen_rtx (MEM, mode,
4153                                         (((TARGET_SH4 && ! TARGET_FMOVD)
4154                                           || REGNO (dst) == FPUL_REG)
4155                                          ? r0_inc_rtx
4156                                          : r0_rtx));
4157                       last_float_addr = &XEXP (newsrc, 0);
4158
4159                       /* Remove the clobber of r0.  */
4160                       XEXP (clobber, 0) = gen_rtx_SCRATCH (Pmode);
4161                       RTX_UNCHANGING_P (newsrc) = 1;
4162                     }
4163                   /* This is a mova needing a label.  Create it.  */
4164                   else if (GET_CODE (src) == UNSPEC
4165                            && XINT (src, 1) == UNSPEC_MOVA
4166                            && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
4167                     {
4168                       lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
4169                       newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
4170                       newsrc = gen_rtx_UNSPEC (SImode,
4171                                                gen_rtvec (1, newsrc),
4172                                                UNSPEC_MOVA);
4173                     }
4174                   else
4175                     {
4176                       lab = add_constant (src, mode, 0);
4177                       newsrc = gen_rtx_MEM (mode,
4178                                             gen_rtx_LABEL_REF (VOIDmode, lab));
4179                       RTX_UNCHANGING_P (newsrc) = 1;
4180                     }
4181                   *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
4182                   INSN_CODE (scan) = -1;
4183                 }
4184             }
4185           dump_table (barrier);
4186           insn = barrier;
4187         }
4188     }
4189
4190   mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
4191   INSN_ADDRESSES_FREE ();
4192   split_branches (first);
4193
4194   /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
4195      also has an effect on the register that holds the address of the sfunc.
4196      Insert an extra dummy insn in front of each sfunc that pretends to
4197      use this register.  */
4198   if (flag_delayed_branch)
4199     {
4200       for (insn = first; insn; insn = NEXT_INSN (insn))
4201         {
4202           rtx reg = sfunc_uses_reg (insn);
4203
4204           if (! reg)
4205             continue;
4206           emit_insn_before (gen_use_sfunc_addr (reg), insn);
4207         }
4208     }
4209 #if 0
4210   /* fpscr is not actually a user variable, but we pretend it is for the
4211      sake of the previous optimization passes, since we want it handled like
4212      one.  However, we don't have any debugging information for it, so turn
4213      it into a non-user variable now.  */
4214   if (TARGET_SH4)
4215     REG_USERVAR_P (get_fpscr_rtx ()) = 0;
4216 #endif
4217   mdep_reorg_phase = SH_AFTER_MDEP_REORG;
4218 }
4219
4220 int
4221 get_dest_uid (label, max_uid)
4222      rtx label;
4223      int max_uid;
4224 {
4225   rtx dest = next_real_insn (label);
4226   int dest_uid;
4227   if (! dest)
4228     /* This can happen for an undefined label.  */
4229     return 0;
4230   dest_uid = INSN_UID (dest);
4231   /* If this is a newly created branch redirection blocking instruction,
4232      we cannot index the branch_uid or insn_addresses arrays with its
4233      uid.  But then, we won't need to, because the actual destination is
4234      the following branch.  */
4235   while (dest_uid >= max_uid)
4236     {
4237       dest = NEXT_INSN (dest);
4238       dest_uid = INSN_UID (dest);
4239     }
4240   if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
4241     return 0;
4242   return dest_uid;
4243 }
4244
4245 /* Split condbranches that are out of range.  Also add clobbers for
4246    scratch registers that are needed in far jumps.
4247    We do this before delay slot scheduling, so that it can take our
4248    newly created instructions into account.  It also allows us to
4249    find branches with common targets more easily.  */
4250
4251 static void
4252 split_branches (first)
4253      rtx first;
4254 {
4255   rtx insn;
4256   struct far_branch **uid_branch, *far_branch_list = 0;
4257   int max_uid = get_max_uid ();
4258
4259   /* Find out which branches are out of range.  */
4260   shorten_branches (first);
4261
4262   uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
4263   memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
4264
4265   for (insn = first; insn; insn = NEXT_INSN (insn))
4266     if (! INSN_P (insn))
4267       continue;
4268     else if (INSN_DELETED_P (insn))
4269       {
4270         /* Shorten_branches would split this instruction again,
4271            so transform it into a note.  */
4272         PUT_CODE (insn, NOTE);
4273         NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4274         NOTE_SOURCE_FILE (insn) = 0;
4275       }
4276     else if (GET_CODE (insn) == JUMP_INSN
4277              /* Don't mess with ADDR_DIFF_VEC */
4278              && (GET_CODE (PATTERN (insn)) == SET
4279                  || GET_CODE (PATTERN (insn)) == RETURN))
4280       {
4281         enum attr_type type = get_attr_type (insn);
4282         if (type == TYPE_CBRANCH)
4283           {
4284             rtx next, beyond;
4285     
4286             if (get_attr_length (insn) > 4)
4287               {
4288                 rtx src = SET_SRC (PATTERN (insn));
4289                 rtx olabel = XEXP (XEXP (src, 1), 0);
4290                 int addr = INSN_ADDRESSES (INSN_UID (insn));
4291                 rtx label = 0;
4292                 int dest_uid = get_dest_uid (olabel, max_uid);
4293                 struct far_branch *bp = uid_branch[dest_uid];
4294     
4295                 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
4296                    the label if the LABEL_NUSES count drops to zero.  There is
4297                    always a jump_optimize pass that sets these values, but it
4298                    proceeds to delete unreferenced code, and then if not
4299                    optimizing, to un-delete the deleted instructions, thus
4300                    leaving labels with too low uses counts.  */
4301                 if (! optimize)
4302                   {
4303                     JUMP_LABEL (insn) = olabel;
4304                     LABEL_NUSES (olabel)++;
4305                   }
4306                 if (! bp)
4307                   {
4308                     bp = (struct far_branch *) alloca (sizeof *bp);
4309                     uid_branch[dest_uid] = bp;
4310                     bp->prev = far_branch_list;
4311                     far_branch_list = bp;
4312                     bp->far_label
4313                       = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
4314                     LABEL_NUSES (bp->far_label)++;
4315                   }
4316                 else
4317                   {
4318                     label = bp->near_label;
4319                     if (! label && bp->address - addr >= CONDJUMP_MIN)
4320                       {
4321                         rtx block = bp->insert_place;
4322
4323                         if (GET_CODE (PATTERN (block)) == RETURN)
4324                           block = PREV_INSN (block);
4325                         else
4326                           block = gen_block_redirect (block,
4327                                                       bp->address, 2);
4328                         label = emit_label_after (gen_label_rtx (),
4329                                                   PREV_INSN (block));
4330                         bp->near_label = label;
4331                       }
4332                     else if (label && ! NEXT_INSN (label))
4333                       {
4334                         if (addr + 2 - bp->address <= CONDJUMP_MAX)
4335                           bp->insert_place = insn;
4336                         else
4337                           gen_far_branch (bp);
4338                       }
4339                   }
4340                 if (! label
4341                     || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
4342                   {
4343                     bp->near_label = label = gen_label_rtx ();
4344                     bp->insert_place = insn;
4345                     bp->address = addr;
4346                   }
4347                 if (! redirect_jump (insn, label, 1))
4348                   abort ();
4349               }
4350             else
4351               {
4352                 /* get_attr_length (insn) == 2 */
4353                 /* Check if we have a pattern where reorg wants to redirect
4354                    the branch to a label from an unconditional branch that
4355                    is too far away.  */
4356                 /* We can't use JUMP_LABEL here because it might be undefined
4357                    when not optimizing.  */
4358                 /* A syntax error might cause beyond to be NULL_RTX.  */
4359                 beyond
4360                   = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
4361                                             0));
4362         
4363                 if (beyond
4364                     && (GET_CODE (beyond) == JUMP_INSN
4365                         || ((beyond = next_active_insn (beyond))
4366                             && GET_CODE (beyond) == JUMP_INSN))
4367                     && GET_CODE (PATTERN (beyond)) == SET
4368                     && recog_memoized (beyond) == CODE_FOR_jump_compact
4369                     && ((INSN_ADDRESSES
4370                          (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
4371                          - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4372                         > 252 + 258 + 2))
4373                   gen_block_redirect (beyond,
4374                                       INSN_ADDRESSES (INSN_UID (beyond)), 1);
4375               }
4376     
4377             next = next_active_insn (insn);
4378
4379             if ((GET_CODE (next) == JUMP_INSN
4380                  || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
4381                 && GET_CODE (PATTERN (next)) == SET
4382                 && recog_memoized (next) == CODE_FOR_jump_compact
4383                 && ((INSN_ADDRESSES
4384                      (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
4385                      - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4386                     > 252 + 258 + 2))
4387               gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
4388           }
4389         else if (type == TYPE_JUMP || type == TYPE_RETURN)
4390           {
4391             int addr = INSN_ADDRESSES (INSN_UID (insn));
4392             rtx far_label = 0;
4393             int dest_uid = 0;
4394             struct far_branch *bp;
4395
4396             if (type == TYPE_JUMP)
4397               {
4398                 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
4399                 dest_uid = get_dest_uid (far_label, max_uid);
4400                 if (! dest_uid)
4401                   {
4402                     /* Parse errors can lead to labels outside
4403                       the insn stream.  */
4404                     if (! NEXT_INSN (far_label))
4405                       continue;
4406
4407                     if (! optimize)
4408                       {
4409                         JUMP_LABEL (insn) = far_label;
4410                         LABEL_NUSES (far_label)++;
4411                       }
4412                     redirect_jump (insn, NULL_RTX, 1);
4413                     far_label = 0;
4414                   }
4415               }
4416             bp = uid_branch[dest_uid];
4417             if (! bp)
4418               {
4419                 bp = (struct far_branch *) alloca (sizeof *bp);
4420                 uid_branch[dest_uid] = bp;
4421                 bp->prev = far_branch_list;
4422                 far_branch_list = bp;
4423                 bp->near_label = 0;
4424                 bp->far_label = far_label;
4425                 if (far_label)
4426                   LABEL_NUSES (far_label)++;
4427               }
4428             else if (bp->near_label && ! NEXT_INSN (bp->near_label))
4429               if (addr - bp->address <= CONDJUMP_MAX)
4430                 emit_label_after (bp->near_label, PREV_INSN (insn));
4431               else
4432                 {
4433                   gen_far_branch (bp);
4434                   bp->near_label = 0;
4435                 }
4436             else
4437               bp->near_label = 0;
4438             bp->address = addr;
4439             bp->insert_place = insn;
4440             if (! far_label)
4441               emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
4442             else
4443               gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
4444           }
4445       }
4446   /* Generate all pending far branches,
4447      and free our references to the far labels.  */
4448   while (far_branch_list)
4449     {
4450       if (far_branch_list->near_label
4451           && ! NEXT_INSN (far_branch_list->near_label))
4452         gen_far_branch (far_branch_list);
4453       if (optimize
4454           && far_branch_list->far_label
4455           && ! --LABEL_NUSES (far_branch_list->far_label))
4456         delete_insn (far_branch_list->far_label);
4457       far_branch_list = far_branch_list->prev;
4458     }
4459
4460   /* Instruction length information is no longer valid due to the new
4461      instructions that have been generated.  */
4462   init_insn_lengths ();
4463 }
4464
4465 /* Dump out instruction addresses, which is useful for debugging the
4466    constant pool table stuff.
4467
4468    If relaxing, output the label and pseudo-ops used to link together
4469    calls and the instruction which set the registers.  */
4470
4471 /* ??? The addresses printed by this routine for insns are nonsense for
4472    insns which are inside of a sequence where none of the inner insns have
4473    variable length.  This is because the second pass of shorten_branches
4474    does not bother to update them.  */
4475
4476 void
4477 final_prescan_insn (insn, opvec, noperands)
4478      rtx insn;
4479      rtx *opvec ATTRIBUTE_UNUSED;
4480      int noperands ATTRIBUTE_UNUSED;
4481 {
4482   if (TARGET_DUMPISIZE)
4483     fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
4484
4485   if (TARGET_RELAX)
4486     {
4487       rtx note;
4488
4489       note = find_reg_note (insn, REG_LABEL, NULL_RTX);
4490       if (note)
4491         {
4492           rtx pattern;
4493
4494           pattern = PATTERN (insn);
4495           if (GET_CODE (pattern) == PARALLEL)
4496             pattern = XVECEXP (pattern, 0, 0);
4497           if (GET_CODE (pattern) == CALL
4498               || (GET_CODE (pattern) == SET
4499                   && (GET_CODE (SET_SRC (pattern)) == CALL
4500                       || get_attr_type (insn) == TYPE_SFUNC)))
4501             asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
4502                          CODE_LABEL_NUMBER (XEXP (note, 0)));
4503           else if (GET_CODE (pattern) == SET)
4504             (*targetm.asm_out.internal_label) (asm_out_file, "L",
4505                                        CODE_LABEL_NUMBER (XEXP (note, 0)));
4506           else
4507             abort ();
4508         }
4509     }
4510 }
4511
4512 /* Dump out any constants accumulated in the final pass.  These will
4513    only be labels.  */
4514
4515 const char *
4516 output_jump_label_table ()
4517 {
4518   int i;
4519
4520   if (pool_size)
4521     {
4522       fprintf (asm_out_file, "\t.align 2\n");
4523       for (i = 0; i < pool_size; i++)
4524         {
4525           pool_node *p = &pool_vector[i];
4526
4527           (*targetm.asm_out.internal_label) (asm_out_file, "L",
4528                                      CODE_LABEL_NUMBER (p->label));
4529           output_asm_insn (".long       %O0", &p->value);
4530         }
4531       pool_size = 0;
4532     }
4533
4534   return "";
4535 }
4536 \f
4537 /* A full frame looks like:
4538
4539    arg-5
4540    arg-4
4541    [ if current_function_anonymous_args
4542    arg-3
4543    arg-2
4544    arg-1
4545    arg-0 ]
4546    saved-fp
4547    saved-r10
4548    saved-r11
4549    saved-r12
4550    saved-pr
4551    local-n
4552    ..
4553    local-1
4554    local-0        <- fp points here.  */
4555
4556 /* Number of bytes pushed for anonymous args, used to pass information
4557    between expand_prologue and expand_epilogue.  */
4558
4559 static int extra_push;
4560
4561 /* Adjust the stack by SIZE bytes.  REG holds the rtl of the register
4562   to be adjusted, and TEMP, if nonnegative, holds the register number
4563   of a general register that we may clobber.  */
4564
4565 static void
4566 output_stack_adjust (size, reg, temp, emit_fn)
4567      int size;
4568      rtx reg;
4569      int temp;
4570      rtx (*emit_fn) PARAMS ((rtx));
4571 {
4572   if (size)
4573     {
4574       HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4575
4576       if (size % align)
4577         abort ();
4578
4579       if (CONST_OK_FOR_ADD (size))
4580         emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size)));
4581       /* Try to do it with two partial adjustments; however, we must make
4582          sure that the stack is properly aligned at all times, in case
4583          an interrupt occurs between the two partial adjustments.  */
4584       else if (CONST_OK_FOR_ADD (size / 2 & -align)
4585                && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
4586         {
4587           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
4588           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
4589         }
4590       else
4591         {
4592           rtx const_reg;
4593           rtx insn;
4594
4595           /* If TEMP is invalid, we could temporarily save a general
4596              register to MACL.  However, there is currently no need
4597              to handle this case, so just abort when we see it.  */
4598           if (temp < 0)
4599             abort ();
4600           const_reg = gen_rtx_REG (GET_MODE (reg), temp);
4601
4602           /* If SIZE is negative, subtract the positive value.
4603              This sometimes allows a constant pool entry to be shared
4604              between prologue and epilogue code.  */
4605           if (size < 0)
4606             {
4607               emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
4608               insn = emit_fn (GEN_SUB3 (reg, reg, const_reg));
4609             }
4610           else
4611             {
4612               emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
4613               insn = emit_fn (GEN_ADD3 (reg, reg, const_reg));
4614             }
4615           if (emit_fn == frame_insn)
4616             REG_NOTES (insn)
4617               = (gen_rtx_EXPR_LIST
4618                  (REG_FRAME_RELATED_EXPR,
4619                   gen_rtx_SET (VOIDmode, reg,
4620                                gen_rtx_PLUS (SImode, reg, GEN_INT (size))),
4621                   REG_NOTES (insn)));
4622         }
4623     }
4624 }
4625
4626 static rtx
4627 frame_insn (x)
4628      rtx x;
4629 {
4630   x = emit_insn (x);
4631   RTX_FRAME_RELATED_P (x) = 1;
4632   return x;
4633 }
4634
4635 /* Output RTL to push register RN onto the stack.  */
4636
4637 static rtx
4638 push (rn)
4639      int rn;
4640 {
4641   rtx x;
4642   if (rn == FPUL_REG)
4643     x = gen_push_fpul ();
4644   else if (rn == FPSCR_REG)
4645     x = gen_push_fpscr ();
4646   else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4647            && FP_OR_XD_REGISTER_P (rn))
4648     {
4649       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4650         return NULL_RTX;
4651       x = gen_push_4 (gen_rtx_REG (DFmode, rn));
4652     }
4653   else if (TARGET_SH2E && FP_REGISTER_P (rn))
4654     x = gen_push_e (gen_rtx_REG (SFmode, rn));
4655   else
4656     x = gen_push (gen_rtx_REG (SImode, rn));
4657
4658   x = frame_insn (x);
4659   REG_NOTES (x)
4660     = gen_rtx_EXPR_LIST (REG_INC,
4661                          gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4662   return x;
4663 }
4664
4665 /* Output RTL to pop register RN from the stack.  */
4666
4667 static void
4668 pop (rn)
4669      int rn;
4670 {
4671   rtx x;
4672   if (rn == FPUL_REG)
4673     x = gen_pop_fpul ();
4674   else if (rn == FPSCR_REG)
4675     x = gen_pop_fpscr ();
4676   else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4677            && FP_OR_XD_REGISTER_P (rn))
4678     {
4679       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4680         return;
4681       x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
4682     }
4683   else if (TARGET_SH2E && FP_REGISTER_P (rn))
4684     x = gen_pop_e (gen_rtx_REG (SFmode, rn));
4685   else
4686     x = gen_pop (gen_rtx_REG (SImode, rn));
4687     
4688   x = emit_insn (x);
4689   REG_NOTES (x)
4690     = gen_rtx_EXPR_LIST (REG_INC,
4691                          gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4692 }
4693
4694 /* Generate code to push the regs specified in the mask.  */
4695
4696 static void
4697 push_regs (mask, interrupt_handler)
4698      HARD_REG_SET *mask;
4699      int interrupt_handler;
4700 {
4701   int i;
4702   int skip_fpscr = 0;
4703
4704   /* Push PR last; this gives better latencies after the prologue, and
4705      candidates for the return delay slot when there are no general
4706      registers pushed.  */
4707   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4708     {
4709       /* If this is an interrupt handler, and the SZ bit varies,
4710          and we have to push any floating point register, we need
4711          to switch to the correct precision first.  */
4712       if (i == FIRST_FP_REG && interrupt_handler && TARGET_FMOVD
4713           && hard_regs_intersect_p (mask, &reg_class_contents[DF_REGS]))
4714         {
4715           HARD_REG_SET unsaved;
4716
4717           push (FPSCR_REG);
4718           COMPL_HARD_REG_SET(unsaved, *mask);
4719           fpscr_set_from_mem (NORMAL_MODE (FP_MODE), unsaved);
4720           skip_fpscr = 1;
4721         }
4722       if (i != PR_REG
4723           && (i != FPSCR_REG || ! skip_fpscr)
4724           && TEST_HARD_REG_BIT (*mask, i))
4725         push (i);
4726     }
4727   if (TEST_HARD_REG_BIT (*mask, PR_REG))
4728     push (PR_REG);
4729 }
4730
4731 /* Calculate how much extra space is needed to save all callee-saved
4732    target registers.
4733    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
4734
4735 static int
4736 shmedia_target_regs_stack_space (HARD_REG_SET *live_regs_mask)
4737 {
4738   int reg;
4739   int stack_space = 0;
4740   int interrupt_handler = sh_cfun_interrupt_handler_p ();
4741
4742   for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
4743     if ((! call_used_regs[reg] || interrupt_handler)
4744         && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
4745       /* Leave space to save this target register on the stack,
4746          in case target register allocation wants to use it. */
4747       stack_space += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
4748   return stack_space;
4749 }
4750    
4751 /* Decide whether we should reserve space for callee-save target registers,
4752    in case target register allocation wants to use them.  REGS_SAVED is
4753    the space, in bytes, that is already required for register saves.
4754    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
4755
4756 static int
4757 shmedia_reserve_space_for_target_registers_p (int regs_saved,
4758                                               HARD_REG_SET *live_regs_mask)
4759 {
4760   if (optimize_size)
4761     return 0;
4762   return shmedia_target_regs_stack_space (live_regs_mask) <= regs_saved;
4763 }
4764
4765 /* Decide how much space to reserve for callee-save target registers
4766    in case target register allocation wants to use them.
4767    LIVE_REGS_MASK is the register mask calculated by calc_live_regs.  */
4768
4769 static int
4770 shmedia_target_regs_stack_adjust (HARD_REG_SET *live_regs_mask)
4771 {
4772   if (shmedia_space_reserved_for_target_registers)
4773     return shmedia_target_regs_stack_space (live_regs_mask);
4774   else
4775     return 0;
4776 }
4777
4778 /* Work out the registers which need to be saved, both as a mask and a
4779    count of saved words.  Return the count.
4780
4781    If doing a pragma interrupt function, then push all regs used by the
4782    function, and if we call another function (we can tell by looking at PR),
4783    make sure that all the regs it clobbers are safe too.  */
4784
4785 static int
4786 calc_live_regs (live_regs_mask)
4787      HARD_REG_SET *live_regs_mask;
4788 {
4789   int reg;
4790   int count;
4791   int interrupt_handler;
4792   int pr_live;
4793
4794   interrupt_handler = sh_cfun_interrupt_handler_p ();
4795
4796   for (count = 0; 32 * count < FIRST_PSEUDO_REGISTER; count++)
4797     CLEAR_HARD_REG_SET (*live_regs_mask);
4798   if (TARGET_SH4 && TARGET_FMOVD && interrupt_handler
4799       && regs_ever_live[FPSCR_REG])
4800     target_flags &= ~FPU_SINGLE_BIT;
4801   /* If we can save a lot of saves by switching to double mode, do that.  */
4802   else if (TARGET_SH4 && TARGET_FMOVD && TARGET_FPU_SINGLE)
4803     for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
4804       if (regs_ever_live[reg] && regs_ever_live[reg+1]
4805           && (! call_used_regs[reg] || (interrupt_handler && ! pragma_trapa))
4806           && ++count > 2)
4807         {
4808           target_flags &= ~FPU_SINGLE_BIT;
4809           break;
4810         }
4811   /* PR_MEDIA_REG is a general purpose register, thus global_alloc already
4812      knows how to use it.  That means the pseudo originally allocated for
4813      the initial value can become the PR_MEDIA_REG hard register, as seen for
4814      execute/20010122-1.c:test9.  */
4815   if (TARGET_SHMEDIA)
4816     pr_live = regs_ever_live[PR_MEDIA_REG];
4817   else
4818     {
4819       rtx pr_initial = has_hard_reg_initial_val (Pmode, PR_REG);
4820       pr_live = (pr_initial
4821                  ? (GET_CODE (pr_initial) != REG
4822                     || REGNO (pr_initial) != (PR_REG))
4823                  : regs_ever_live[PR_REG]);
4824     }
4825   /* Force PR to be live if the prologue has to call the SHmedia
4826      argument decoder or register saver.  */
4827   if (TARGET_SHCOMPACT
4828       && ((current_function_args_info.call_cookie
4829            & ~ CALL_COOKIE_RET_TRAMP (1))
4830           || current_function_has_nonlocal_label))
4831     pr_live = 1;
4832   for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
4833     {
4834       if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
4835           ? pr_live
4836           : (interrupt_handler && ! pragma_trapa)
4837           ? (/* Need to save all the regs ever live.  */
4838              (regs_ever_live[reg]
4839               || (call_used_regs[reg]
4840                   && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
4841                   && pr_live))
4842              && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
4843              && reg != RETURN_ADDRESS_POINTER_REGNUM
4844              && reg != T_REG && reg != GBR_REG
4845              /* Push fpscr only on targets which have FPU */
4846              && (reg != FPSCR_REG || TARGET_FPU_ANY))
4847           : (/* Only push those regs which are used and need to be saved.  */
4848              (TARGET_SHCOMPACT
4849               && flag_pic
4850               && current_function_args_info.call_cookie
4851               && reg == PIC_OFFSET_TABLE_REGNUM)
4852              || (regs_ever_live[reg] && ! call_used_regs[reg])
4853              || (current_function_calls_eh_return
4854                  && (reg == EH_RETURN_DATA_REGNO (0)
4855                      || reg == EH_RETURN_DATA_REGNO (1)
4856                      || reg == EH_RETURN_DATA_REGNO (2)
4857                      || reg == EH_RETURN_DATA_REGNO (3)))))
4858         {
4859           SET_HARD_REG_BIT (*live_regs_mask, reg);
4860           count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
4861
4862           if ((TARGET_SH4 || TARGET_SH5) && TARGET_FMOVD
4863               && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
4864             {
4865               if (FP_REGISTER_P (reg))
4866                 {
4867                   if (! TARGET_FPU_SINGLE && ! regs_ever_live[reg ^ 1])
4868                     {
4869                       SET_HARD_REG_BIT (*live_regs_mask, (reg ^ 1));
4870                       count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
4871                     }
4872                 }
4873               else if (XD_REGISTER_P (reg))
4874                 {
4875                   /* Must switch to double mode to access these registers.  */
4876                   target_flags &= ~FPU_SINGLE_BIT;
4877                 }
4878             }
4879         }
4880     }
4881   /* If we have a target register optimization pass after prologue / epilogue
4882      threading, we need to assume all target registers will be live even if
4883      they aren't now.  */
4884   if (flag_branch_target_load_optimize2
4885       && TARGET_SAVE_ALL_TARGET_REGS
4886       && shmedia_space_reserved_for_target_registers)
4887     for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
4888       if ((! call_used_regs[reg] || interrupt_handler)
4889           && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
4890         {
4891           SET_HARD_REG_BIT (*live_regs_mask, reg);
4892           count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
4893         }
4894
4895   return count;
4896 }
4897
4898 /* Code to generate prologue and epilogue sequences */
4899
4900 /* PUSHED is the number of bytes that are being pushed on the
4901    stack for register saves.  Return the frame size, padded
4902    appropriately so that the stack stays properly aligned.  */
4903 static HOST_WIDE_INT
4904 rounded_frame_size (pushed)
4905      int pushed;
4906 {
4907   HOST_WIDE_INT size = get_frame_size ();
4908   HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4909
4910   return ((size + pushed + align - 1) & -align) - pushed;
4911 }
4912
4913 /* Choose a call-clobbered target-branch register that remains
4914    unchanged along the whole function.  We set it up as the return
4915    value in the prologue.  */
4916 int
4917 sh_media_register_for_return ()
4918 {
4919   int regno;
4920   int tr0_used;
4921
4922   if (! current_function_is_leaf)
4923     return -1;
4924
4925   tr0_used = flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
4926
4927   for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
4928     if (call_used_regs[regno] && ! regs_ever_live[regno])
4929       return regno;
4930
4931   return -1;
4932 }
4933
4934 void
4935 sh_expand_prologue ()
4936 {
4937   HARD_REG_SET live_regs_mask;
4938   int d, i;
4939   int d_rounding = 0;
4940   int save_flags = target_flags;
4941
4942   current_function_interrupt = sh_cfun_interrupt_handler_p ();
4943
4944   /* We have pretend args if we had an object sent partially in registers
4945      and partially on the stack, e.g. a large structure.  */
4946   output_stack_adjust (-current_function_pretend_args_size
4947                        - current_function_args_info.stack_regs * 8,
4948                        stack_pointer_rtx, TARGET_SH5 ? 0 : 1, frame_insn);
4949
4950   extra_push = 0;
4951
4952   if (TARGET_SHCOMPACT && flag_pic && current_function_args_info.call_cookie)
4953     /* We're going to use the PIC register to load the address of the
4954        incoming-argument decoder and/or of the return trampoline from
4955        the GOT, so make sure the PIC register is preserved and
4956        initialized.  */
4957     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
4958
4959   if (TARGET_SHCOMPACT
4960       && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
4961     {
4962       int reg;
4963
4964       /* First, make all registers with incoming arguments that will
4965          be pushed onto the stack live, so that register renaming
4966          doesn't overwrite them.  */
4967       for (reg = 0; reg < NPARM_REGS (SImode); reg++)
4968         if (CALL_COOKIE_STACKSEQ_GET (current_function_args_info.call_cookie)
4969             >= NPARM_REGS (SImode) - reg)
4970           for (; reg < NPARM_REGS (SImode); reg++)
4971             emit_insn (gen_shcompact_preserve_incoming_args
4972                        (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4973         else if (CALL_COOKIE_INT_REG_GET
4974                  (current_function_args_info.call_cookie, reg) == 1)
4975           emit_insn (gen_shcompact_preserve_incoming_args
4976                      (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4977
4978       emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
4979                       stack_pointer_rtx);
4980       emit_move_insn (gen_rtx_REG (SImode, R0_REG),
4981                       GEN_INT (current_function_args_info.call_cookie));
4982       emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
4983                       gen_rtx_REG (SImode, R0_REG));
4984     }
4985   else if (TARGET_SHMEDIA)
4986     {
4987       int tr = sh_media_register_for_return ();
4988
4989       if (tr >= 0)
4990         {
4991           rtx insn = emit_move_insn (gen_rtx_REG (DImode, tr),
4992                                      gen_rtx_REG (DImode, PR_MEDIA_REG));
4993
4994           /* If this function only exits with sibcalls, this copy
4995              will be flagged as dead.  */
4996           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4997                                                 const0_rtx,
4998                                                 REG_NOTES (insn));
4999         }
5000     }
5001
5002   /* Emit the code for SETUP_VARARGS.  */
5003   if (current_function_stdarg)
5004     {
5005       /* This is not used by the SH2E calling convention  */
5006       if (TARGET_SH1 && ! TARGET_SH2E && ! TARGET_SH5 && ! TARGET_HITACHI)
5007         {
5008           /* Push arg regs as if they'd been provided by caller in stack.  */
5009           for (i = 0; i < NPARM_REGS(SImode); i++)
5010             {
5011               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
5012               rtx insn;
5013
5014               if (i >= (NPARM_REGS(SImode) 
5015                         - current_function_args_info.arg_count[(int) SH_ARG_INT]
5016                         ))
5017                 break;
5018               insn = push (rn);
5019               RTX_FRAME_RELATED_P (insn) = 0;
5020               extra_push += 4;
5021             }
5022         }
5023     }
5024
5025   /* If we're supposed to switch stacks at function entry, do so now.  */
5026   if (sp_switch)
5027     emit_insn (gen_sp_switch_1 ());
5028
5029   d = calc_live_regs (&live_regs_mask);
5030   /* ??? Maybe we could save some switching if we can move a mode switch
5031      that already happens to be at the function start into the prologue.  */
5032   if (target_flags != save_flags && ! current_function_interrupt)
5033     emit_insn (gen_toggle_sz ());
5034     
5035   if (TARGET_SH5)
5036     {
5037       int i;
5038       int offset;
5039       int align;
5040       rtx r0 = gen_rtx_REG (Pmode, R0_REG);
5041       int offset_in_r0 = -1;
5042       int sp_in_r0 = 0;
5043       int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
5044       int total_size, save_size;
5045
5046       /* D is the actual number of bytes that we need for saving registers,
5047          however, in initial_elimination_offset we have committed to using
5048          an additional TREGS_SPACE amount of bytes - in order to keep both
5049          addresses to arguments supplied by the caller and local variables
5050          valid, we must keep this gap.  Place it between the incoming
5051          arguments and the actually saved registers in a bid to optimize
5052          locality of reference.  */
5053       total_size = d + tregs_space;
5054       total_size += rounded_frame_size (total_size);
5055       save_size = total_size - rounded_frame_size (d);
5056       if (save_size % (STACK_BOUNDARY / BITS_PER_UNIT))
5057         d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5058                         - save_size % (STACK_BOUNDARY / BITS_PER_UNIT));
5059
5060       /* If adjusting the stack in a single step costs nothing extra, do so.
5061          I.e. either if a single addi is enough, or we need a movi anyway,
5062          and we don't exceed the maximum offset range (the test for the
5063          latter is conservative for simplicity).  */
5064       if (TARGET_SHMEDIA
5065           && (CONST_OK_FOR_I10 (-total_size)
5066               || (! CONST_OK_FOR_I10 (-(save_size + d_rounding))
5067                   && total_size <= 2044)))
5068         d_rounding = total_size - save_size;
5069
5070       offset = d + d_rounding;
5071
5072       output_stack_adjust (-(save_size + d_rounding), stack_pointer_rtx,
5073                            1, frame_insn);
5074
5075       /* We loop twice: first, we save 8-byte aligned registers in the
5076          higher addresses, that are known to be aligned.  Then, we
5077          proceed to saving 32-bit registers that don't need 8-byte
5078          alignment.  */
5079       /* Note that if you change this code in a way that affects where
5080          the return register is saved, you have to update not only
5081          sh_expand_epilogue, but also sh_set_return_address.  */
5082       for (align = 1; align >= 0; align--)
5083         for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
5084           if (TEST_HARD_REG_BIT (live_regs_mask, i))
5085             {
5086               enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5087               int reg = i;
5088               rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
5089
5090               if (mode == SFmode && (i % 2) == 1
5091                   && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5092                   && (TEST_HARD_REG_BIT (live_regs_mask, (i ^ 1))))
5093                 {
5094                   mode = DFmode;
5095                   i--;
5096                   reg--;
5097                 }
5098                 
5099               /* If we're doing the aligned pass and this is not aligned,
5100                  or we're doing the unaligned pass and this is aligned,
5101                  skip it.  */
5102               if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
5103                    == 0) != align)
5104                 continue;
5105
5106               offset -= GET_MODE_SIZE (mode);
5107
5108               reg_rtx = gen_rtx_REG (mode, reg);
5109
5110               mem_rtx = gen_rtx_MEM (mode,
5111                                      gen_rtx_PLUS (Pmode,
5112                                                    stack_pointer_rtx,
5113                                                    GEN_INT (offset)));
5114
5115               GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_pre_dec);
5116
5117               mem_rtx = NULL_RTX;
5118
5119             try_pre_dec:
5120               do
5121                 if (HAVE_PRE_DECREMENT
5122                     && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
5123                         || mem_rtx == NULL_RTX
5124                         || i == PR_REG || SPECIAL_REGISTER_P (i)))
5125                   {
5126                     pre_dec = gen_rtx_MEM (mode,
5127                                            gen_rtx_PRE_DEC (Pmode, r0));
5128
5129                     GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (pre_dec, 0),
5130                                               pre_dec_ok);
5131
5132                     pre_dec = NULL_RTX;
5133
5134                     break;
5135
5136                   pre_dec_ok:
5137                     mem_rtx = NULL_RTX;
5138                     offset += GET_MODE_SIZE (mode);
5139                   }
5140               while (0);
5141
5142               if (mem_rtx != NULL_RTX)
5143                 goto addr_ok;
5144
5145               if (offset_in_r0 == -1)
5146                 {
5147                   emit_move_insn (r0, GEN_INT (offset));
5148                   offset_in_r0 = offset;
5149                 }
5150               else if (offset != offset_in_r0)
5151                 {
5152                   emit_move_insn (r0,
5153                                   gen_rtx_PLUS
5154                                   (Pmode, r0,
5155                                    GEN_INT (offset - offset_in_r0)));
5156                   offset_in_r0 += offset - offset_in_r0;
5157                 }
5158                                                   
5159               if (pre_dec != NULL_RTX)
5160                 {
5161                   if (! sp_in_r0)
5162                     {
5163                       emit_move_insn (r0,
5164                                       gen_rtx_PLUS
5165                                       (Pmode, r0, stack_pointer_rtx));
5166                       sp_in_r0 = 1;
5167                     }
5168
5169                   offset -= GET_MODE_SIZE (mode);
5170                   offset_in_r0 -= GET_MODE_SIZE (mode);
5171
5172                   mem_rtx = pre_dec;
5173                 }
5174               else if (sp_in_r0)
5175                 mem_rtx = gen_rtx_MEM (mode, r0);
5176               else
5177                 mem_rtx = gen_rtx_MEM (mode,
5178                                        gen_rtx_PLUS (Pmode,
5179                                                      stack_pointer_rtx,
5180                                                      r0));
5181
5182               /* We must not use an r0-based address for target-branch
5183                  registers or for special registers without pre-dec
5184                  memory addresses, since we store their values in r0
5185                  first.  */
5186               if (TARGET_REGISTER_P (i)
5187                   || ((i == PR_REG || SPECIAL_REGISTER_P (i))
5188                       && mem_rtx != pre_dec))
5189                 abort ();
5190
5191             addr_ok:
5192               if (TARGET_REGISTER_P (i)
5193                   || ((i == PR_REG || SPECIAL_REGISTER_P (i))
5194                       && mem_rtx != pre_dec))
5195                 {
5196                   rtx r0mode = gen_rtx_REG (GET_MODE (reg_rtx), R0_REG);
5197
5198                   emit_move_insn (r0mode, reg_rtx);
5199
5200                   offset_in_r0 = -1;
5201                   sp_in_r0 = 0;
5202
5203                   reg_rtx = r0mode;
5204                 }
5205
5206               emit_move_insn (mem_rtx, reg_rtx);
5207             }
5208
5209       if (offset != d_rounding)
5210         abort ();
5211     }
5212   else
5213     push_regs (&live_regs_mask, current_function_interrupt);
5214
5215   if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5216     {
5217       rtx insn = get_last_insn ();
5218       rtx last = emit_insn (gen_GOTaddr2picreg ());
5219
5220       /* Mark these insns as possibly dead.  Sometimes, flow2 may
5221          delete all uses of the PIC register.  In this case, let it
5222          delete the initialization too.  */
5223       do
5224         {
5225           insn = NEXT_INSN (insn);
5226
5227           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5228                                                 const0_rtx,
5229                                                 REG_NOTES (insn));
5230         }
5231       while (insn != last);
5232     }
5233
5234   if (SHMEDIA_REGS_STACK_ADJUST ())
5235     {
5236       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5237                       function_symbol (TARGET_FPU_ANY
5238                                        ? "__GCC_push_shmedia_regs"
5239                                        : "__GCC_push_shmedia_regs_nofpu"));
5240       /* This must NOT go through the PLT, otherwise mach and macl
5241          may be clobbered.  */
5242       emit_insn (gen_shmedia_save_restore_regs_compact
5243                  (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
5244     }
5245
5246   if (target_flags != save_flags && ! current_function_interrupt)
5247     {
5248       rtx insn = emit_insn (gen_toggle_sz ());
5249
5250       /* If we're lucky, a mode switch in the function body will
5251          overwrite fpscr, turning this insn dead.  Tell flow this
5252          insn is ok to delete.  */
5253       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
5254                                             const0_rtx,
5255                                             REG_NOTES (insn));
5256     }
5257
5258   target_flags = save_flags;
5259
5260   output_stack_adjust (-rounded_frame_size (d) + d_rounding,
5261                        stack_pointer_rtx, TARGET_SH5 ? 0 : 1, frame_insn);
5262
5263   if (frame_pointer_needed)
5264     frame_insn (GEN_MOV (frame_pointer_rtx, stack_pointer_rtx));
5265
5266   if (TARGET_SHCOMPACT
5267       && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
5268     {
5269       /* This must NOT go through the PLT, otherwise mach and macl
5270          may be clobbered.  */
5271       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5272                       function_symbol ("__GCC_shcompact_incoming_args"));
5273       emit_insn (gen_shcompact_incoming_args ());
5274     }
5275 }
5276
5277 void
5278 sh_expand_epilogue ()
5279 {
5280   HARD_REG_SET live_regs_mask;
5281   int d, i;
5282   int d_rounding = 0;
5283
5284   int save_flags = target_flags;
5285   int frame_size, save_size;
5286   int fpscr_deferred = 0;
5287
5288   d = calc_live_regs (&live_regs_mask);
5289
5290   save_size = d;
5291   frame_size = rounded_frame_size (d);
5292
5293   if (TARGET_SH5)
5294     {
5295       int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
5296       int total_size;
5297       if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
5298       d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5299                     - d % (STACK_BOUNDARY / BITS_PER_UNIT));
5300
5301       total_size = d + tregs_space;
5302       total_size += rounded_frame_size (total_size);
5303       save_size = total_size - frame_size;
5304
5305       /* If adjusting the stack in a single step costs nothing extra, do so.
5306          I.e. either if a single addi is enough, or we need a movi anyway,
5307          and we don't exceed the maximum offset range (the test for the
5308          latter is conservative for simplicity).  */
5309       if (TARGET_SHMEDIA
5310           && ! frame_pointer_needed
5311           && (CONST_OK_FOR_I10 (total_size)
5312               || (! CONST_OK_FOR_I10 (save_size + d_rounding)
5313                   && total_size <= 2044)))
5314         d_rounding = frame_size;
5315
5316       frame_size -= d_rounding;
5317     }
5318
5319   if (frame_pointer_needed)
5320     {
5321       output_stack_adjust (frame_size, frame_pointer_rtx, 7, emit_insn);
5322
5323       /* We must avoid moving the stack pointer adjustment past code
5324          which reads from the local frame, else an interrupt could
5325          occur after the SP adjustment and clobber data in the local
5326          frame.  */
5327       emit_insn (gen_blockage ());
5328       emit_insn (GEN_MOV (stack_pointer_rtx, frame_pointer_rtx));
5329     }
5330   else if (frame_size)
5331     {
5332       /* We must avoid moving the stack pointer adjustment past code
5333          which reads from the local frame, else an interrupt could
5334          occur after the SP adjustment and clobber data in the local
5335          frame.  */
5336       emit_insn (gen_blockage ());
5337       output_stack_adjust (frame_size, stack_pointer_rtx, 7, emit_insn);
5338     }
5339
5340   if (SHMEDIA_REGS_STACK_ADJUST ())
5341     {
5342       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
5343                       function_symbol (TARGET_FPU_ANY
5344                                        ? "__GCC_pop_shmedia_regs"
5345                                        : "__GCC_pop_shmedia_regs_nofpu"));
5346       /* This must NOT go through the PLT, otherwise mach and macl
5347          may be clobbered.  */
5348       emit_insn (gen_shmedia_save_restore_regs_compact
5349                  (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
5350     }
5351
5352   /* Pop all the registers.  */
5353
5354   if (target_flags != save_flags && ! current_function_interrupt)
5355     emit_insn (gen_toggle_sz ());
5356   if (TARGET_SH5)
5357     {
5358       int offset = d_rounding;
5359       int offset_in_r0 = -1;
5360       int sp_in_r0 = 0;
5361       int align;
5362       rtx r0 = gen_rtx_REG (Pmode, R0_REG);
5363       int tmp_regno = R20_REG;
5364       
5365       /* We loop twice: first, we save 8-byte aligned registers in the
5366          higher addresses, that are known to be aligned.  Then, we
5367          proceed to saving 32-bit registers that don't need 8-byte
5368          alignment.  */
5369       for (align = 0; align <= 1; align++)
5370         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5371           if (TEST_HARD_REG_BIT (live_regs_mask, i))
5372             {
5373               enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5374               int reg = i;
5375               rtx reg_rtx, mem_rtx, post_inc = NULL_RTX, insn;
5376
5377               if (mode == SFmode && (i % 2) == 0
5378                   && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5379                   && (TEST_HARD_REG_BIT (live_regs_mask, (i ^ 1))))
5380                 {
5381                   mode = DFmode;
5382                   i++;
5383                 }
5384
5385               /* If we're doing the aligned pass and this is not aligned,
5386                  or we're doing the unaligned pass and this is aligned,
5387                  skip it.  */
5388               if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
5389                    == 0) != align)
5390                 continue;
5391
5392               reg_rtx = gen_rtx_REG (mode, reg);
5393
5394               mem_rtx = gen_rtx_MEM (mode,
5395                                      gen_rtx_PLUS (Pmode,
5396                                                    stack_pointer_rtx,
5397                                                    GEN_INT (offset)));
5398
5399               GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_post_inc);
5400
5401               mem_rtx = NULL_RTX;
5402
5403             try_post_inc:
5404               do
5405                 if (HAVE_POST_INCREMENT
5406                     && (offset == offset_in_r0
5407                         || (offset + GET_MODE_SIZE (mode) != d + d_rounding
5408                             && mem_rtx == NULL_RTX)
5409                         || i == PR_REG || SPECIAL_REGISTER_P (i)))
5410                   {
5411                     post_inc = gen_rtx_MEM (mode,
5412                                             gen_rtx_POST_INC (Pmode, r0));
5413
5414                     GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (post_inc, 0),
5415                                               post_inc_ok);
5416
5417                     post_inc = NULL_RTX;
5418
5419                     break;
5420                     
5421                   post_inc_ok:
5422                     mem_rtx = NULL_RTX;
5423                   }
5424               while (0);
5425               
5426               if (mem_rtx != NULL_RTX)
5427                 goto addr_ok;
5428
5429               if (offset_in_r0 == -1)
5430                 {
5431                   emit_move_insn (r0, GEN_INT (offset));
5432                   offset_in_r0 = offset;
5433                 }
5434               else if (offset != offset_in_r0)
5435                 {
5436                   emit_move_insn (r0,
5437                                   gen_rtx_PLUS
5438                                   (Pmode, r0,
5439                                    GEN_INT (offset - offset_in_r0)));
5440                   offset_in_r0 += offset - offset_in_r0;
5441                 }
5442                   
5443               if (post_inc != NULL_RTX)
5444                 {
5445                   if (! sp_in_r0)
5446                     {
5447                       emit_move_insn (r0,
5448                                       gen_rtx_PLUS
5449                                       (Pmode, r0, stack_pointer_rtx));
5450                       sp_in_r0 = 1;
5451                     }
5452                   
5453                   mem_rtx = post_inc;
5454
5455                   offset_in_r0 += GET_MODE_SIZE (mode);
5456                 }
5457               else if (sp_in_r0)
5458                 mem_rtx = gen_rtx_MEM (mode, r0);
5459               else
5460                 mem_rtx = gen_rtx_MEM (mode,
5461                                        gen_rtx_PLUS (Pmode,
5462                                                      stack_pointer_rtx,
5463                                                      r0));
5464
5465               if ((i == PR_REG || SPECIAL_REGISTER_P (i))
5466                   && mem_rtx != post_inc)
5467                 abort ();
5468
5469             addr_ok:
5470               if ((i == PR_REG || SPECIAL_REGISTER_P (i))
5471                   && mem_rtx != post_inc)
5472                 {
5473                   insn = emit_move_insn (r0, mem_rtx);
5474                   mem_rtx = r0;
5475                 }
5476               else if (TARGET_REGISTER_P (i))
5477                 {
5478                   rtx tmp_reg = gen_rtx_REG (mode, tmp_regno);
5479
5480                   /* Give the scheduler a bit of freedom by using R20..R23
5481                      in a round-robin fashion.  Don't use R1 here because
5482                      we want to use it for EH_RETURN_STACKADJ_RTX.  */
5483                   insn = emit_move_insn (tmp_reg, mem_rtx);
5484                   mem_rtx = tmp_reg;
5485                   if (++tmp_regno > R23_REG)
5486                     tmp_regno = R20_REG;
5487                 }
5488
5489               insn = emit_move_insn (reg_rtx, mem_rtx);
5490
5491               offset += GET_MODE_SIZE (mode);
5492             }
5493
5494       if (offset != d + d_rounding)
5495         abort ();
5496     }
5497   else /* ! TARGET_SH5 */
5498     {
5499       save_size = 0;
5500       if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
5501         pop (PR_REG);
5502       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5503         {
5504           int j = (FIRST_PSEUDO_REGISTER - 1) - i;
5505   
5506           if (j == FPSCR_REG && current_function_interrupt && TARGET_FMOVD
5507               && hard_regs_intersect_p (&live_regs_mask,
5508                                         &reg_class_contents[DF_REGS]))
5509             fpscr_deferred = 1;
5510           else if (j != PR_REG && TEST_HARD_REG_BIT (live_regs_mask, j))
5511             pop (j);
5512           if (j == FIRST_FP_REG && fpscr_deferred)
5513             pop (FPSCR_REG);
5514
5515         }
5516     }
5517   if (target_flags != save_flags && ! current_function_interrupt)
5518     emit_insn (gen_toggle_sz ());
5519   target_flags = save_flags;
5520
5521   output_stack_adjust (extra_push + current_function_pretend_args_size
5522                        + save_size + d_rounding
5523                        + current_function_args_info.stack_regs * 8,
5524                        stack_pointer_rtx, 7, emit_insn);
5525
5526   if (current_function_calls_eh_return)
5527     emit_insn (GEN_ADD3 (stack_pointer_rtx, stack_pointer_rtx,
5528                          EH_RETURN_STACKADJ_RTX));
5529
5530   /* Switch back to the normal stack if necessary.  */
5531   if (sp_switch)
5532     emit_insn (gen_sp_switch_2 ());
5533
5534   /* Tell flow the insn that pops PR isn't dead.  */
5535   /* PR_REG will never be live in SHmedia mode, and we don't need to
5536      USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
5537      by the return pattern.  */
5538   if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
5539     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, PR_REG)));
5540 }
5541
5542 static int sh_need_epilogue_known = 0;
5543
5544 int
5545 sh_need_epilogue ()
5546 {
5547   if (! sh_need_epilogue_known)
5548     {
5549       rtx epilogue;
5550
5551       start_sequence ();
5552       sh_expand_epilogue ();
5553       epilogue = get_insns ();
5554       end_sequence ();
5555       sh_need_epilogue_known = (epilogue == NULL ? -1 : 1);
5556     }
5557   return sh_need_epilogue_known > 0;
5558 }
5559
5560 /* Emit code to change the current function's return address to RA.
5561    TEMP is available as a scratch register, if needed.  */
5562
5563 void
5564 sh_set_return_address (ra, tmp)
5565      rtx ra, tmp;
5566 {
5567   HARD_REG_SET live_regs_mask;
5568   int d;
5569   int d_rounding = 0;
5570   int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
5571   int pr_offset;
5572
5573   d = calc_live_regs (&live_regs_mask);
5574
5575   /* If pr_reg isn't life, we can set it (or the register given in
5576      sh_media_register_for_return) directly.  */
5577   if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
5578     {
5579       rtx rr;
5580
5581       if (TARGET_SHMEDIA)
5582         {
5583           int rr_regno = sh_media_register_for_return ();
5584
5585           if (rr_regno < 0)
5586             rr_regno = pr_reg;
5587
5588           rr = gen_rtx_REG (DImode, rr_regno);
5589         }
5590       else
5591         rr = gen_rtx_REG (SImode, pr_reg);
5592
5593       emit_insn (GEN_MOV (rr, ra));
5594       /* Tell flow the register for return isn't dead.  */
5595       emit_insn (gen_rtx_USE (VOIDmode, rr));
5596       return;
5597     }
5598
5599   if (TARGET_SH5)
5600     {
5601       int i;
5602       int offset;
5603       int align;
5604       
5605       if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
5606         d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5607                       - d % (STACK_BOUNDARY / BITS_PER_UNIT));
5608
5609       offset = 0;
5610
5611       /* We loop twice: first, we save 8-byte aligned registers in the
5612          higher addresses, that are known to be aligned.  Then, we
5613          proceed to saving 32-bit registers that don't need 8-byte
5614          alignment.  */
5615       for (align = 0; align <= 1; align++)
5616         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5617           if (TEST_HARD_REG_BIT (live_regs_mask, i))
5618             {
5619               enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5620
5621               if (mode == SFmode && (i % 2) == 0
5622                   && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5623                   && (TEST_HARD_REG_BIT (live_regs_mask, (i ^ 1))))
5624                 {
5625                   mode = DFmode;
5626                   i++;
5627                 }
5628
5629               /* If we're doing the aligned pass and this is not aligned,
5630                  or we're doing the unaligned pass and this is aligned,
5631                  skip it.  */
5632               if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
5633                    == 0) != align)
5634                 continue;
5635
5636               if (i == pr_reg)
5637                 goto found;
5638
5639               offset += GET_MODE_SIZE (mode);
5640             }
5641
5642       /* We can't find pr register.  */
5643       abort ();
5644
5645     found:
5646       pr_offset = (rounded_frame_size (d) - d_rounding + offset
5647                    + SHMEDIA_REGS_STACK_ADJUST ());
5648     }
5649   else
5650     pr_offset = rounded_frame_size (d) - d_rounding;
5651
5652   emit_insn (GEN_MOV (tmp, GEN_INT (pr_offset)));
5653   emit_insn (GEN_ADD3 (tmp, tmp, frame_pointer_rtx));
5654
5655   tmp = gen_rtx_MEM (Pmode, tmp);
5656   emit_insn (GEN_MOV (tmp, ra));
5657 }
5658
5659 /* Clear variables at function end.  */
5660
5661 static void
5662 sh_output_function_epilogue (file, size)
5663      FILE *file ATTRIBUTE_UNUSED;
5664      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
5665 {
5666   trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
5667   sh_need_epilogue_known = 0;
5668   sp_switch = NULL_RTX;
5669 }
5670
5671 rtx
5672 sh_builtin_saveregs ()
5673 {
5674   /* First unnamed integer register.  */
5675   int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
5676   /* Number of integer registers we need to save.  */
5677   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
5678   /* First unnamed SFmode float reg */
5679   int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
5680   /* Number of SFmode float regs to save.  */
5681   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
5682   rtx regbuf, fpregs;
5683   int bufsize, regno;
5684   HOST_WIDE_INT alias_set;
5685
5686   if (TARGET_SH5)
5687     {
5688       if (n_intregs)
5689         {
5690           int pushregs = n_intregs;
5691
5692           while (pushregs < NPARM_REGS (SImode) - 1
5693                  && (CALL_COOKIE_INT_REG_GET
5694                         (current_function_args_info.call_cookie,
5695                          NPARM_REGS (SImode) - pushregs)
5696                      == 1))
5697             {
5698               current_function_args_info.call_cookie
5699                 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
5700                                           - pushregs, 1);
5701               pushregs++;
5702             }
5703
5704           if (pushregs == NPARM_REGS (SImode))
5705             current_function_args_info.call_cookie
5706               |= (CALL_COOKIE_INT_REG (0, 1)
5707                   | CALL_COOKIE_STACKSEQ (pushregs - 1));
5708           else
5709             current_function_args_info.call_cookie
5710               |= CALL_COOKIE_STACKSEQ (pushregs);
5711
5712           current_function_pretend_args_size += 8 * n_intregs;
5713         }
5714       if (TARGET_SHCOMPACT)
5715         return const0_rtx;
5716     }
5717   
5718   if (! TARGET_SH2E && ! TARGET_SH4 && ! TARGET_SH5)
5719     {
5720       error ("__builtin_saveregs not supported by this subtarget");
5721       return const0_rtx;
5722     }
5723
5724   if (TARGET_SHMEDIA)
5725     n_floatregs = 0;
5726
5727   /* Allocate block of memory for the regs.  */
5728   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
5729      Or can assign_stack_local accept a 0 SIZE argument?  */
5730   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
5731
5732   if (TARGET_SHMEDIA)
5733     regbuf = gen_rtx_MEM (BLKmode,
5734                           gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
5735   else if (n_floatregs & 1)
5736     {
5737       rtx addr;
5738
5739       regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
5740       addr = copy_to_mode_reg (Pmode, XEXP (regbuf, 0));
5741       emit_insn (gen_iorsi3 (addr, addr, GEN_INT (UNITS_PER_WORD)));
5742       regbuf = change_address (regbuf, BLKmode, addr);
5743     }
5744   else
5745     regbuf = assign_stack_local (BLKmode, bufsize, 0);
5746   alias_set = get_varargs_alias_set ();
5747   set_mem_alias_set (regbuf, alias_set);
5748
5749   /* Save int args.
5750      This is optimized to only save the regs that are necessary.  Explicitly
5751      named args need not be saved.  */
5752   if (n_intregs > 0)
5753     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
5754                          adjust_address (regbuf, BLKmode,
5755                                          n_floatregs * UNITS_PER_WORD),
5756                          n_intregs);
5757
5758   if (TARGET_SHMEDIA)
5759     /* Return the address of the regbuf.  */
5760     return XEXP (regbuf, 0);
5761
5762   /* Save float args.
5763      This is optimized to only save the regs that are necessary.  Explicitly
5764      named args need not be saved.
5765      We explicitly build a pointer to the buffer because it halves the insn
5766      count when not optimizing (otherwise the pointer is built for each reg
5767      saved).
5768      We emit the moves in reverse order so that we can use predecrement.  */
5769
5770   fpregs = gen_reg_rtx (Pmode);
5771   emit_move_insn (fpregs, XEXP (regbuf, 0));
5772   emit_insn (gen_addsi3 (fpregs, fpregs,
5773                          GEN_INT (n_floatregs * UNITS_PER_WORD)));
5774   if (TARGET_SH4)
5775     {
5776       rtx mem;
5777       for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
5778         {
5779           emit_insn (gen_addsi3 (fpregs, fpregs,
5780                                  GEN_INT (-2 * UNITS_PER_WORD)));
5781           mem = gen_rtx_MEM (DFmode, fpregs);
5782           set_mem_alias_set (mem, alias_set);
5783           emit_move_insn (mem, 
5784                           gen_rtx (REG, DFmode, BASE_ARG_REG (DFmode) + regno));
5785         }
5786       regno = first_floatreg;
5787       if (regno & 1)
5788         {
5789           emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5790           mem = gen_rtx_MEM (SFmode, fpregs);
5791           set_mem_alias_set (mem, alias_set);
5792           emit_move_insn (mem,
5793                           gen_rtx (REG, SFmode, BASE_ARG_REG (SFmode) + regno
5794                                                 - (TARGET_LITTLE_ENDIAN != 0)));
5795         }
5796     }
5797   else
5798     for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
5799       {
5800         rtx mem;
5801
5802         emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5803         mem = gen_rtx_MEM (SFmode, fpregs);
5804         set_mem_alias_set (mem, alias_set);
5805         emit_move_insn (mem,
5806                         gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
5807       }
5808
5809   /* Return the address of the regbuf.  */
5810   return XEXP (regbuf, 0);
5811 }
5812
5813 /* Define the `__builtin_va_list' type for the ABI.  */
5814
5815 tree
5816 sh_build_va_list ()
5817 {
5818   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5819   tree record;
5820
5821   if (TARGET_SH5 || (! TARGET_SH2E && ! TARGET_SH4) || TARGET_HITACHI)
5822     return ptr_type_node;
5823
5824   record = make_node (RECORD_TYPE);
5825
5826   f_next_o = build_decl (FIELD_DECL, get_identifier ("__va_next_o"),
5827                          ptr_type_node);
5828   f_next_o_limit = build_decl (FIELD_DECL,
5829                                get_identifier ("__va_next_o_limit"),
5830                                ptr_type_node);
5831   f_next_fp = build_decl (FIELD_DECL, get_identifier ("__va_next_fp"),
5832                           ptr_type_node);
5833   f_next_fp_limit = build_decl (FIELD_DECL,
5834                                 get_identifier ("__va_next_fp_limit"),
5835                                 ptr_type_node);
5836   f_next_stack = build_decl (FIELD_DECL, get_identifier ("__va_next_stack"),
5837                              ptr_type_node);
5838
5839   DECL_FIELD_CONTEXT (f_next_o) = record;
5840   DECL_FIELD_CONTEXT (f_next_o_limit) = record;
5841   DECL_FIELD_CONTEXT (f_next_fp) = record;
5842   DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
5843   DECL_FIELD_CONTEXT (f_next_stack) = record;
5844
5845   TYPE_FIELDS (record) = f_next_o;
5846   TREE_CHAIN (f_next_o) = f_next_o_limit;
5847   TREE_CHAIN (f_next_o_limit) = f_next_fp;
5848   TREE_CHAIN (f_next_fp) = f_next_fp_limit;
5849   TREE_CHAIN (f_next_fp_limit) = f_next_stack;
5850
5851   layout_type (record);
5852
5853   return record;
5854 }
5855
5856 /* Implement `va_start' for varargs and stdarg.  */
5857
5858 void
5859 sh_va_start (valist, nextarg)
5860      tree valist;
5861      rtx nextarg;
5862 {
5863   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5864   tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5865   tree t, u;
5866   int nfp, nint;
5867
5868   if (TARGET_SH5)
5869     {
5870       expand_builtin_saveregs ();
5871       std_expand_builtin_va_start (valist, nextarg);
5872       return;
5873     }
5874
5875   if ((! TARGET_SH2E && ! TARGET_SH4) || TARGET_HITACHI)
5876     {
5877       std_expand_builtin_va_start (valist, nextarg);
5878       return;
5879     }
5880
5881   f_next_o = TYPE_FIELDS (va_list_type_node);
5882   f_next_o_limit = TREE_CHAIN (f_next_o);
5883   f_next_fp = TREE_CHAIN (f_next_o_limit);
5884   f_next_fp_limit = TREE_CHAIN (f_next_fp);
5885   f_next_stack = TREE_CHAIN (f_next_fp_limit);
5886
5887   next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5888   next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5889                         valist, f_next_o_limit);
5890   next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp);
5891   next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5892                          valist, f_next_fp_limit);
5893   next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5894                       valist, f_next_stack);
5895
5896   /* Call __builtin_saveregs.  */
5897   u = make_tree (ptr_type_node, expand_builtin_saveregs ());
5898   t = build (MODIFY_EXPR, ptr_type_node, next_fp, u);
5899   TREE_SIDE_EFFECTS (t) = 1;
5900   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5901
5902   nfp = current_function_args_info.arg_count[SH_ARG_FLOAT];
5903   if (nfp < 8)
5904     nfp = 8 - nfp;
5905   else
5906     nfp = 0;
5907   u = fold (build (PLUS_EXPR, ptr_type_node, u,
5908                    build_int_2 (UNITS_PER_WORD * nfp, 0)));
5909   t = build (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
5910   TREE_SIDE_EFFECTS (t) = 1;
5911   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5912
5913   t = build (MODIFY_EXPR, ptr_type_node, next_o, u);
5914   TREE_SIDE_EFFECTS (t) = 1;
5915   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5916
5917   nint = current_function_args_info.arg_count[SH_ARG_INT];
5918   if (nint < 4)
5919     nint = 4 - nint;
5920   else
5921     nint = 0;
5922   u = fold (build (PLUS_EXPR, ptr_type_node, u,
5923                    build_int_2 (UNITS_PER_WORD * nint, 0)));
5924   t = build (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
5925   TREE_SIDE_EFFECTS (t) = 1;
5926   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5927
5928   u = make_tree (ptr_type_node, nextarg);
5929   t = build (MODIFY_EXPR, ptr_type_node, next_stack, u);
5930   TREE_SIDE_EFFECTS (t) = 1;
5931   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5932 }
5933
5934 /* Implement `va_arg'.  */
5935
5936 rtx
5937 sh_va_arg (valist, type)
5938      tree valist, type;
5939 {
5940   HOST_WIDE_INT size, rsize;
5941   tree tmp, pptr_type_node;
5942   rtx addr_rtx, r;
5943   rtx result_ptr, result = NULL_RTX;
5944   int pass_by_ref = MUST_PASS_IN_STACK (TYPE_MODE (type), type);
5945   rtx lab_over;
5946
5947   size = int_size_in_bytes (type);
5948   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5949   pptr_type_node = build_pointer_type (ptr_type_node);
5950
5951   if (pass_by_ref)
5952     type = build_pointer_type (type);
5953
5954   if (! TARGET_SH5 && (TARGET_SH2E || TARGET_SH4) && ! TARGET_HITACHI)
5955     {
5956       tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5957       tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5958       int pass_as_float;
5959       rtx lab_false;
5960
5961       f_next_o = TYPE_FIELDS (va_list_type_node);
5962       f_next_o_limit = TREE_CHAIN (f_next_o);
5963       f_next_fp = TREE_CHAIN (f_next_o_limit);
5964       f_next_fp_limit = TREE_CHAIN (f_next_fp);
5965       f_next_stack = TREE_CHAIN (f_next_fp_limit);
5966
5967       next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5968       next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5969                             valist, f_next_o_limit);
5970       next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp),
5971                        valist, f_next_fp);
5972       next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5973                              valist, f_next_fp_limit);
5974       next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5975                           valist, f_next_stack);
5976
5977       /* Structures with a single member with a distinct mode are passed
5978          like their member.  This is relevant if the latter has a REAL_TYPE
5979          or COMPLEX_TYPE type.  */
5980       if (TREE_CODE (type) == RECORD_TYPE
5981           && TYPE_FIELDS (type)
5982           && TREE_CODE (TYPE_FIELDS (type)) == FIELD_DECL
5983           && (TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == REAL_TYPE
5984               || TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == COMPLEX_TYPE)
5985           && TREE_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
5986         type = TREE_TYPE (TYPE_FIELDS (type));
5987       if (TARGET_SH4)
5988         {
5989           pass_as_float = ((TREE_CODE (type) == REAL_TYPE && size <= 8)
5990                            || (TREE_CODE (type) == COMPLEX_TYPE
5991                                && TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
5992                                && size <= 16));
5993         }
5994       else
5995         {
5996           pass_as_float = (TREE_CODE (type) == REAL_TYPE && size == 4);
5997         }
5998
5999       addr_rtx = gen_reg_rtx (Pmode);
6000       lab_false = gen_label_rtx ();
6001       lab_over = gen_label_rtx ();
6002
6003       tmp = make_tree (pptr_type_node, addr_rtx);
6004       valist = build1 (INDIRECT_REF, ptr_type_node, tmp);
6005
6006       if (pass_as_float)
6007         {
6008           int first_floatreg
6009             = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
6010           int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
6011
6012           emit_cmp_and_jump_insns (expand_expr (next_fp, NULL_RTX, Pmode,
6013                                                 EXPAND_NORMAL),
6014                                    expand_expr (next_fp_limit, NULL_RTX,
6015                                                 Pmode, EXPAND_NORMAL),
6016                                    GE, const1_rtx, Pmode, 1, lab_false);
6017
6018           if (TYPE_ALIGN (type) > BITS_PER_WORD
6019               || (((TREE_CODE (type) == REAL_TYPE && size == 8) || size == 16)
6020                   && (n_floatregs & 1)))
6021             {
6022               tmp = build (BIT_AND_EXPR, ptr_type_node, next_fp,
6023                            build_int_2 (UNITS_PER_WORD, 0));
6024               tmp = build (PLUS_EXPR, ptr_type_node, next_fp, tmp);
6025               tmp = build (MODIFY_EXPR, ptr_type_node, next_fp, tmp);
6026               TREE_SIDE_EFFECTS (tmp) = 1;
6027               expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6028             }
6029
6030           tmp = build1 (ADDR_EXPR, pptr_type_node, next_fp);
6031           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
6032           if (r != addr_rtx)
6033             emit_move_insn (addr_rtx, r);
6034
6035 #ifdef FUNCTION_ARG_SCmode_WART
6036           if (TYPE_MODE (type) == SCmode && TARGET_SH4 && TARGET_LITTLE_ENDIAN)
6037             {
6038               rtx addr, real, imag, result_value, slot;
6039               tree subtype = TREE_TYPE (type);
6040
6041               addr = std_expand_builtin_va_arg (valist, subtype);
6042 #ifdef POINTERS_EXTEND_UNSIGNED
6043               if (GET_MODE (addr) != Pmode)
6044                 addr = convert_memory_address (Pmode, addr);
6045 #endif
6046               imag = gen_rtx_MEM (TYPE_MODE (type), addr);
6047               set_mem_alias_set (imag, get_varargs_alias_set ());
6048
6049               addr = std_expand_builtin_va_arg (valist, subtype);
6050 #ifdef POINTERS_EXTEND_UNSIGNED
6051               if (GET_MODE (addr) != Pmode)
6052                 addr = convert_memory_address (Pmode, addr);
6053 #endif
6054               real = gen_rtx_MEM (TYPE_MODE (type), addr);
6055               set_mem_alias_set (real, get_varargs_alias_set ());
6056
6057               result_value = gen_rtx_CONCAT (SCmode, real, imag);
6058               /* ??? this interface is stupid - why require a pointer?  */
6059               result = gen_reg_rtx (Pmode);
6060               slot = assign_stack_temp (SCmode, 8, 0);
6061               emit_move_insn (slot, result_value);
6062               emit_move_insn (result, XEXP (slot, 0));
6063             }
6064 #endif /* FUNCTION_ARG_SCmode_WART */
6065
6066           emit_jump_insn (gen_jump (lab_over));
6067           emit_barrier ();
6068           emit_label (lab_false);
6069
6070           tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
6071           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
6072           if (r != addr_rtx)
6073             emit_move_insn (addr_rtx, r);
6074         }
6075       else
6076         {
6077           tmp = build (PLUS_EXPR, ptr_type_node, next_o,
6078                        build_int_2 (rsize, 0));
6079           
6080           emit_cmp_and_jump_insns (expand_expr (tmp, NULL_RTX, Pmode,
6081                                                 EXPAND_NORMAL),
6082                                    expand_expr (next_o_limit, NULL_RTX,
6083                                                 Pmode, EXPAND_NORMAL),
6084                                    GT, const1_rtx, Pmode, 1, lab_false);
6085
6086           tmp = build1 (ADDR_EXPR, pptr_type_node, next_o);
6087           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
6088           if (r != addr_rtx)
6089             emit_move_insn (addr_rtx, r);
6090
6091           emit_jump_insn (gen_jump (lab_over));
6092           emit_barrier ();
6093           emit_label (lab_false);
6094
6095           if (size > 4 && ! TARGET_SH4)
6096             {
6097               tmp = build (MODIFY_EXPR, ptr_type_node, next_o, next_o_limit);
6098               TREE_SIDE_EFFECTS (tmp) = 1;
6099               expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6100             }
6101
6102           tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
6103           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
6104           if (r != addr_rtx)
6105             emit_move_insn (addr_rtx, r);
6106         }
6107
6108       if (! result)
6109         emit_label (lab_over);
6110     }
6111
6112   /* ??? In va-sh.h, there had been code to make values larger than
6113      size 8 indirect.  This does not match the FUNCTION_ARG macros.  */
6114
6115   result_ptr = std_expand_builtin_va_arg (valist, type);
6116   if (result)
6117     {
6118       emit_move_insn (result, result_ptr);
6119       emit_label (lab_over);
6120     }
6121   else
6122     result = result_ptr;
6123
6124   if (pass_by_ref)
6125     {
6126 #ifdef POINTERS_EXTEND_UNSIGNED
6127       if (GET_MODE (addr) != Pmode)
6128         addr = convert_memory_address (Pmode, result);
6129 #endif
6130       result = gen_rtx_MEM (ptr_mode, force_reg (Pmode, result));
6131       set_mem_alias_set (result, get_varargs_alias_set ());
6132     }
6133   /* ??? expand_builtin_va_arg will also set the alias set of the dereferenced
6134      argument to the varargs alias set.  */
6135   return result;
6136 }
6137
6138 /* Define the offset between two registers, one to be eliminated, and
6139    the other its replacement, at the start of a routine.  */
6140
6141 int
6142 initial_elimination_offset (from, to)
6143      int from;
6144      int to;
6145 {
6146   int regs_saved;
6147   int regs_saved_rounding = 0;
6148   int total_saved_regs_space;
6149   int total_auto_space;
6150   int save_flags = target_flags;
6151   int copy_flags;
6152   HARD_REG_SET live_regs_mask;
6153
6154   shmedia_space_reserved_for_target_registers = false;
6155   regs_saved = calc_live_regs (&live_regs_mask);
6156   regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
6157
6158   if (shmedia_reserve_space_for_target_registers_p (regs_saved, &live_regs_mask))
6159     {
6160       shmedia_space_reserved_for_target_registers = true;
6161       regs_saved += shmedia_target_regs_stack_adjust (&live_regs_mask);
6162     }
6163
6164   if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
6165     regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
6166                            - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
6167
6168   total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
6169   copy_flags = target_flags;
6170   target_flags = save_flags;
6171
6172   total_saved_regs_space = regs_saved + regs_saved_rounding;
6173
6174   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
6175     return total_saved_regs_space + total_auto_space
6176       + current_function_args_info.byref_regs * 8;
6177
6178   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
6179     return total_saved_regs_space + total_auto_space
6180       + current_function_args_info.byref_regs * 8;
6181
6182   /* Initial gap between fp and sp is 0.  */
6183   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
6184     return 0;
6185
6186   if (from == RETURN_ADDRESS_POINTER_REGNUM
6187       && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
6188     {
6189       if (TARGET_SH5)
6190         {
6191           int i, n = total_saved_regs_space;
6192           int align;
6193           int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
6194           
6195           n += total_auto_space;
6196
6197           /* If it wasn't saved, there's not much we can do.  */
6198           if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
6199             return n;
6200
6201           target_flags = copy_flags;
6202
6203           /* We loop twice: first, check 8-byte aligned registers,
6204              that are stored in the higher addresses, that are known
6205              to be aligned.  Then, check 32-bit registers that don't
6206              need 8-byte alignment.  */
6207           for (align = 1; align >= 0; align--)
6208             for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
6209               if (TEST_HARD_REG_BIT (live_regs_mask, i))
6210                 {
6211                   enum machine_mode mode = REGISTER_NATURAL_MODE (i);
6212
6213                   if (mode == SFmode && (i % 2) == 1
6214                       && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
6215                       && TEST_HARD_REG_BIT (live_regs_mask, (i ^ 1)))
6216                     {
6217                       mode = DFmode;
6218                       i--;
6219                     }
6220                 
6221                   /* If we're doing the aligned pass and this is not aligned,
6222                      or we're doing the unaligned pass and this is aligned,
6223                      skip it.  */
6224                   if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
6225                        == 0) != align)
6226                     continue;
6227
6228                   n -= GET_MODE_SIZE (mode);
6229
6230                   if (i == pr_reg)
6231                     {
6232                       target_flags = save_flags;
6233                       return n;
6234                     }
6235                 }
6236
6237           abort ();
6238         }
6239       else
6240         return total_auto_space;
6241     }
6242
6243   abort ();
6244 }
6245 \f
6246 /* Handle machine specific pragmas to be semi-compatible with Renesas
6247    compiler.  */
6248
6249 void
6250 sh_pr_interrupt (pfile)
6251      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
6252 {
6253   pragma_interrupt = 1;
6254 }
6255
6256 void
6257 sh_pr_trapa (pfile)
6258      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
6259 {
6260   pragma_interrupt = pragma_trapa = 1;
6261 }
6262
6263 void
6264 sh_pr_nosave_low_regs (pfile)
6265      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
6266 {
6267   pragma_nosave_low_regs = 1;
6268 }
6269
6270 /* Generate 'handle_interrupt' attribute for decls */
6271
6272 static void
6273 sh_insert_attributes (node, attributes)
6274      tree node;
6275      tree * attributes;
6276 {
6277   if (! pragma_interrupt
6278       || TREE_CODE (node) != FUNCTION_DECL)
6279     return;
6280
6281   /* We are only interested in fields.  */
6282   if (TREE_CODE_CLASS (TREE_CODE (node)) != 'd')
6283     return;
6284
6285   /* Add a 'handle_interrupt' attribute.  */
6286   * attributes = tree_cons (get_identifier ("interrupt_handler"), NULL, * attributes);
6287
6288   return;
6289 }
6290
6291 /* Supported attributes:
6292
6293    interrupt_handler -- specifies this function is an interrupt handler.
6294
6295    sp_switch -- specifies an alternate stack for an interrupt handler
6296    to run on.
6297
6298    trap_exit -- use a trapa to exit an interrupt function instead of
6299    an rte instruction.  */
6300
6301 const struct attribute_spec sh_attribute_table[] =
6302 {
6303   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
6304   { "interrupt_handler", 0, 0, true,  false, false, sh_handle_interrupt_handler_attribute },
6305   { "sp_switch",         1, 1, true,  false, false, sh_handle_sp_switch_attribute },
6306   { "trap_exit",         1, 1, true,  false, false, sh_handle_trap_exit_attribute },
6307   { NULL,                0, 0, false, false, false, NULL }
6308 };
6309
6310 /* Handle an "interrupt_handler" attribute; arguments as in
6311    struct attribute_spec.handler.  */
6312 static tree
6313 sh_handle_interrupt_handler_attribute (node, name, args, flags, no_add_attrs)
6314      tree *node;
6315      tree name;
6316      tree args ATTRIBUTE_UNUSED;
6317      int flags ATTRIBUTE_UNUSED;
6318      bool *no_add_attrs;
6319 {
6320   if (TREE_CODE (*node) != FUNCTION_DECL)
6321     {
6322       warning ("`%s' attribute only applies to functions",
6323                IDENTIFIER_POINTER (name));
6324       *no_add_attrs = true;
6325     }
6326   else if (TARGET_SHCOMPACT)
6327     {
6328       error ("attribute interrupt_handler is not compatible with -m5-compact");
6329       *no_add_attrs = true;
6330     }
6331
6332   return NULL_TREE;
6333 }
6334
6335 /* Handle an "sp_switch" attribute; arguments as in
6336    struct attribute_spec.handler.  */
6337 static tree
6338 sh_handle_sp_switch_attribute (node, name, args, flags, no_add_attrs)
6339      tree *node;
6340      tree name;
6341      tree args;
6342      int flags ATTRIBUTE_UNUSED;
6343      bool *no_add_attrs;
6344 {
6345   if (TREE_CODE (*node) != FUNCTION_DECL)
6346     {
6347       warning ("`%s' attribute only applies to functions",
6348                IDENTIFIER_POINTER (name));
6349       *no_add_attrs = true;
6350     }
6351   else if (!pragma_interrupt)
6352     {
6353       /* The sp_switch attribute only has meaning for interrupt functions.  */
6354       warning ("`%s' attribute only applies to interrupt functions",
6355                IDENTIFIER_POINTER (name));
6356       *no_add_attrs = true;
6357     }
6358   else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6359     {
6360       /* The argument must be a constant string.  */
6361       warning ("`%s' attribute argument not a string constant",
6362                IDENTIFIER_POINTER (name));
6363       *no_add_attrs = true;
6364     }
6365   else
6366     {
6367       sp_switch = gen_rtx_SYMBOL_REF (VOIDmode,
6368                                       TREE_STRING_POINTER (TREE_VALUE (args)));
6369     }
6370
6371   return NULL_TREE;
6372 }
6373
6374 /* Handle an "trap_exit" attribute; arguments as in
6375    struct attribute_spec.handler.  */
6376 static tree
6377 sh_handle_trap_exit_attribute (node, name, args, flags, no_add_attrs)
6378      tree *node;
6379      tree name;
6380      tree args;
6381      int flags ATTRIBUTE_UNUSED;
6382      bool *no_add_attrs;
6383 {
6384   if (TREE_CODE (*node) != FUNCTION_DECL)
6385     {
6386       warning ("`%s' attribute only applies to functions",
6387                IDENTIFIER_POINTER (name));
6388       *no_add_attrs = true;
6389     }
6390   else if (!pragma_interrupt)
6391     {
6392       /* The trap_exit attribute only has meaning for interrupt functions.  */
6393       warning ("`%s' attribute only applies to interrupt functions",
6394                IDENTIFIER_POINTER (name));
6395       *no_add_attrs = true;
6396     }
6397   else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
6398     {
6399       /* The argument must be a constant integer.  */
6400       warning ("`%s' attribute argument not an integer constant",
6401                IDENTIFIER_POINTER (name));
6402       *no_add_attrs = true;
6403     }
6404   else
6405     {
6406       trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
6407     }
6408
6409   return NULL_TREE;
6410 }
6411
6412 int
6413 sh_cfun_interrupt_handler_p ()
6414 {
6415   return (lookup_attribute ("interrupt_handler",
6416                             DECL_ATTRIBUTES (current_function_decl))
6417           != NULL_TREE);
6418 }
6419 \f
6420 /* Predicates used by the templates.  */
6421
6422 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
6423    Used only in general_movsrc_operand.  */
6424
6425 int
6426 system_reg_operand (op, mode)
6427      rtx op;
6428      enum machine_mode mode ATTRIBUTE_UNUSED;
6429 {
6430   switch (REGNO (op))
6431     {
6432     case PR_REG:
6433     case MACL_REG:
6434     case MACH_REG:
6435       return 1;
6436     }
6437   return 0;
6438 }
6439
6440 /* Returns 1 if OP can be source of a simple move operation.
6441    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
6442    invalid as are subregs of system registers.  */
6443
6444 int
6445 general_movsrc_operand (op, mode)
6446      rtx op;
6447      enum machine_mode mode;
6448 {
6449   if (GET_CODE (op) == MEM)
6450     {
6451       rtx inside = XEXP (op, 0);
6452       if (GET_CODE (inside) == CONST)
6453         inside = XEXP (inside, 0);
6454
6455       if (GET_CODE (inside) == LABEL_REF)
6456         return 1;
6457
6458       if (GET_CODE (inside) == PLUS
6459           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
6460           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
6461         return 1;
6462
6463       /* Only post inc allowed.  */
6464       if (GET_CODE (inside) == PRE_DEC)
6465         return 0;
6466     }
6467
6468   if ((mode == QImode || mode == HImode)
6469       && (GET_CODE (op) == SUBREG
6470           && GET_CODE (XEXP (op, 0)) == REG
6471           && system_reg_operand (XEXP (op, 0), mode)))
6472     return 0;
6473
6474   return general_operand (op, mode);
6475 }
6476
6477 /* Returns 1 if OP can be a destination of a move.
6478    Same as general_operand, but no preinc allowed.  */
6479
6480 int
6481 general_movdst_operand (op, mode)
6482      rtx op;
6483      enum machine_mode mode;
6484 {
6485   /* Only pre dec allowed.  */
6486   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
6487     return 0;
6488
6489   return general_operand (op, mode);
6490 }
6491
6492 /* Returns 1 if OP is a normal arithmetic register.  */
6493
6494 int
6495 arith_reg_operand (op, mode)
6496      rtx op;
6497      enum machine_mode mode;
6498 {
6499   if (register_operand (op, mode))
6500     {
6501       int regno;
6502
6503       if (GET_CODE (op) == REG)
6504         regno = REGNO (op);
6505       else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
6506         regno = REGNO (SUBREG_REG (op));
6507       else
6508         return 1;
6509
6510       return (regno != T_REG && regno != PR_REG
6511               && ! TARGET_REGISTER_P (regno)
6512               && (regno != FPUL_REG || TARGET_SH4)
6513               && regno != MACH_REG && regno != MACL_REG);
6514     }
6515   return 0;
6516 }
6517
6518 /* Like above, but for DImode destinations: forbid paradoxical DImode subregs,
6519    because this would lead to missing sign extensions when truncating from
6520    DImode to SImode.  */
6521 int
6522 arith_reg_dest (op, mode)
6523      rtx op;
6524      enum machine_mode mode;
6525 {
6526   if (mode == DImode && GET_CODE (op) == SUBREG
6527       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))) < 8)
6528     return 0;
6529   return arith_reg_operand (op, mode);
6530 }
6531
6532 int
6533 int_gpr_dest (op, mode)
6534      rtx op;
6535      enum machine_mode mode ATTRIBUTE_UNUSED;
6536 {
6537   enum machine_mode op_mode = GET_MODE (op);
6538
6539   if (GET_MODE_CLASS (op_mode) != MODE_INT
6540       || GET_MODE_SIZE (op_mode) >= UNITS_PER_WORD)
6541     return 0;
6542   if (! reload_completed)
6543     return 0;
6544   return true_regnum (op) <= LAST_GENERAL_REG;
6545 }
6546
6547 int
6548 fp_arith_reg_operand (op, mode)
6549      rtx op;
6550      enum machine_mode mode;
6551 {
6552   if (register_operand (op, mode))
6553     {
6554       int regno;
6555
6556       if (GET_CODE (op) == REG)
6557         regno = REGNO (op);
6558       else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
6559         regno = REGNO (SUBREG_REG (op));
6560       else
6561         return 1;
6562
6563       return (regno >= FIRST_PSEUDO_REGISTER
6564               || FP_REGISTER_P (regno));
6565     }
6566   return 0;
6567 }
6568
6569 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
6570
6571 int
6572 arith_operand (op, mode)
6573      rtx op;
6574      enum machine_mode mode;
6575 {
6576   if (arith_reg_operand (op, mode))
6577     return 1;
6578
6579   if (TARGET_SHMEDIA)
6580     {
6581       /* FIXME: We should be checking whether the CONST_INT fits in a
6582          CONST_OK_FOR_I16 here, but this causes reload_cse to crash when
6583          attempting to transform a sequence of two 64-bit sets of the
6584          same register from literal constants into a set and an add,
6585          when the difference is too wide for an add.  */
6586       if (GET_CODE (op) == CONST_INT
6587           || EXTRA_CONSTRAINT_C16 (op))
6588         return 1;
6589       else
6590         return 0;
6591     }
6592   else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I08 (INTVAL (op)))
6593     return 1;
6594
6595   return 0;
6596 }
6597
6598 /* Returns 1 if OP is a valid source operand for a compare insn.  */
6599
6600 int
6601 arith_reg_or_0_operand (op, mode)
6602      rtx op;
6603      enum machine_mode mode;
6604 {
6605   if (arith_reg_operand (op, mode))
6606     return 1;
6607
6608   if (EXTRA_CONSTRAINT_Z (op))
6609     return 1;
6610
6611   return 0;
6612 }
6613
6614 /* Return 1 if OP is a valid source operand for an SHmedia operation
6615    that takes either a register or a 6-bit immediate.  */
6616
6617 int
6618 shmedia_6bit_operand (op, mode)
6619      rtx op;
6620      enum machine_mode mode;
6621 {
6622   return (arith_reg_operand (op, mode)
6623           || (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I06 (INTVAL (op))));
6624 }
6625
6626 /* Returns 1 if OP is a valid source operand for a logical operation.  */
6627
6628 int
6629 logical_operand (op, mode)
6630      rtx op;
6631      enum machine_mode mode;
6632 {
6633   if (arith_reg_operand (op, mode))
6634     return 1;
6635
6636   if (TARGET_SHMEDIA)
6637     {
6638       if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I10 (INTVAL (op)))
6639         return 1;
6640       else
6641         return 0;
6642     }
6643   else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K08 (INTVAL (op)))
6644     return 1;
6645
6646   return 0;
6647 }
6648
6649 int
6650 and_operand (op, mode)
6651      rtx op;
6652      enum machine_mode mode;
6653 {
6654   if (logical_operand (op, mode))
6655     return 1;
6656
6657   /* Check mshflo.l / mshflhi.l opportunities.  */
6658   if (TARGET_SHMEDIA
6659       && mode == DImode
6660       && GET_CODE (op) == CONST_INT
6661       && CONST_OK_FOR_J16 (INTVAL (op)))
6662         return 1;
6663
6664   return 0;
6665 }
6666
6667 /* Nonzero if OP is a floating point value with value 0.0.  */
6668
6669 int
6670 fp_zero_operand (op)
6671      rtx op;
6672 {
6673   REAL_VALUE_TYPE r;
6674
6675   if (GET_MODE (op) != SFmode)
6676     return 0;
6677
6678   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6679   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
6680 }
6681
6682 /* Nonzero if OP is a floating point value with value 1.0.  */
6683
6684 int
6685 fp_one_operand (op)
6686      rtx op;
6687 {
6688   REAL_VALUE_TYPE r;
6689
6690   if (GET_MODE (op) != SFmode)
6691     return 0;
6692
6693   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6694   return REAL_VALUES_EQUAL (r, dconst1);
6695 }
6696
6697 /* For -m4 and -m4-single-only, mode switching is used.  If we are
6698    compiling without -mfmovd, movsf_ie isn't taken into account for
6699    mode switching.  We could check in machine_dependent_reorg for
6700    cases where we know we are in single precision mode, but there is
6701    interface to find that out during reload, so we must avoid
6702    choosing an fldi alternative during reload and thus failing to
6703    allocate a scratch register for the constant loading.  */
6704 int
6705 fldi_ok ()
6706 {
6707   return ! TARGET_SH4 || TARGET_FMOVD || reload_completed;
6708 }
6709
6710 int
6711 tertiary_reload_operand (op, mode)
6712      rtx op;
6713      enum machine_mode mode ATTRIBUTE_UNUSED;
6714 {
6715   enum rtx_code code = GET_CODE (op);
6716   return code == MEM || (TARGET_SH4 && code == CONST_DOUBLE);
6717 }
6718
6719 int
6720 fpscr_operand (op, mode)
6721      rtx op;
6722      enum machine_mode mode ATTRIBUTE_UNUSED;
6723 {
6724   return (GET_CODE (op) == REG && REGNO (op) == FPSCR_REG
6725           && GET_MODE (op) == PSImode);
6726 }
6727
6728 int
6729 fpul_operand (op, mode)
6730      rtx op;
6731      enum machine_mode mode;
6732 {
6733   if (TARGET_SHMEDIA)
6734     return fp_arith_reg_operand (op, mode);
6735
6736   return (GET_CODE (op) == REG
6737           && (REGNO (op) == FPUL_REG || REGNO (op) >= FIRST_PSEUDO_REGISTER)
6738           && GET_MODE (op) == mode);
6739 }
6740
6741 int
6742 symbol_ref_operand (op, mode)
6743      rtx op;
6744      enum machine_mode mode ATTRIBUTE_UNUSED;
6745 {
6746   return (GET_CODE (op) == SYMBOL_REF);
6747 }
6748
6749 /* Return the TLS type for TLS symbols, 0 for otherwise.  */
6750 int
6751 tls_symbolic_operand (op, mode)
6752      rtx op;
6753      enum machine_mode mode ATTRIBUTE_UNUSED;
6754 {
6755   if (GET_CODE (op) != SYMBOL_REF)
6756     return 0;
6757   return SYMBOL_REF_TLS_MODEL (op);
6758 }
6759
6760 int
6761 commutative_float_operator (op, mode)
6762      rtx op;
6763      enum machine_mode mode;
6764 {
6765   if (GET_MODE (op) != mode)
6766     return 0;
6767   switch (GET_CODE (op))
6768     {
6769     case PLUS:
6770     case MULT:
6771       return 1;
6772     default:
6773       break;
6774     }
6775   return 0;
6776 }
6777
6778 int
6779 noncommutative_float_operator (op, mode)
6780      rtx op;
6781      enum machine_mode mode;
6782 {
6783   if (GET_MODE (op) != mode)
6784     return 0;
6785   switch (GET_CODE (op))
6786     {
6787     case MINUS:
6788     case DIV:
6789       return 1;
6790     default:
6791       break;
6792     }
6793   return 0;
6794 }
6795
6796 int
6797 unary_float_operator (op, mode)
6798      rtx op;
6799      enum machine_mode mode;
6800 {
6801   if (GET_MODE (op) != mode)
6802     return 0;
6803   switch (GET_CODE (op))
6804     {
6805     case ABS:
6806     case NEG:
6807     case SQRT:
6808       return 1;
6809     default:
6810       break;
6811     }
6812   return 0;
6813 }
6814
6815 int
6816 binary_float_operator (op, mode)
6817      rtx op;
6818      enum machine_mode mode;
6819 {
6820   if (GET_MODE (op) != mode)
6821     return 0;
6822   switch (GET_CODE (op))
6823     {
6824     case PLUS:
6825     case MINUS:
6826     case MULT:
6827     case DIV:
6828       return 1;
6829     default:
6830       break;
6831     }
6832   return 0;
6833 }
6834
6835 int
6836 binary_logical_operator (op, mode)
6837      rtx op;
6838      enum machine_mode mode;
6839 {
6840   if (GET_MODE (op) != mode)
6841     return 0;
6842   switch (GET_CODE (op))
6843     {
6844     case IOR:
6845     case AND:
6846     case XOR:
6847       return 1;
6848     default:
6849       break;
6850     }
6851   return 0;
6852 }
6853
6854 int
6855 equality_comparison_operator (op, mode)
6856      rtx op;
6857      enum machine_mode mode;
6858 {
6859   return ((mode == VOIDmode || GET_MODE (op) == mode)
6860           && (GET_CODE (op) == EQ || GET_CODE (op) == NE));
6861 }
6862
6863 int greater_comparison_operator (op, mode)
6864      rtx op;
6865      enum machine_mode mode;
6866 {
6867   if (mode != VOIDmode && GET_MODE (op) == mode)
6868     return 0;
6869   switch (GET_CODE (op))
6870     {
6871     case GT:
6872     case GE:
6873     case GTU:
6874     case GEU:
6875       return 1;
6876     default:
6877       return 0;
6878     }
6879 }
6880
6881 int less_comparison_operator (op, mode)
6882      rtx op;
6883      enum machine_mode mode;
6884 {
6885   if (mode != VOIDmode && GET_MODE (op) == mode)
6886     return 0;
6887   switch (GET_CODE (op))
6888     {
6889     case LT:
6890     case LE:
6891     case LTU:
6892     case LEU:
6893       return 1;
6894     default:
6895       return 0;
6896     }
6897 }
6898
6899 /* Accept pseudos and branch target registers.  */
6900 int
6901 target_reg_operand (op, mode)
6902      rtx op;
6903      enum machine_mode mode;
6904 {
6905   if (mode != DImode
6906       || GET_MODE (op) != DImode)
6907     return 0;
6908
6909   if (GET_CODE (op) == SUBREG)
6910     op = XEXP (op, 0);
6911
6912   if (GET_CODE (op) != REG)
6913     return 0;
6914
6915   /* We must protect ourselves from matching pseudos that are virtual
6916      register, because they will eventually be replaced with hardware
6917      registers that aren't branch-target registers.  */
6918   if (REGNO (op) > LAST_VIRTUAL_REGISTER
6919       || TARGET_REGISTER_P (REGNO (op)))
6920     return 1;
6921
6922   return 0;
6923 }
6924
6925 /* Same as target_reg_operand, except that label_refs and symbol_refs
6926    are accepted before reload.  */
6927 int
6928 target_operand (op, mode)
6929      rtx op;
6930      enum machine_mode mode;
6931 {
6932   if (mode != DImode)
6933     return 0;
6934
6935   if ((GET_MODE (op) == DImode || GET_MODE (op) == VOIDmode)
6936       && EXTRA_CONSTRAINT_Csy (op))
6937     return ! reload_completed;
6938
6939   return target_reg_operand (op, mode);
6940 }
6941
6942 int
6943 mextr_bit_offset (op, mode)
6944      rtx op;
6945      enum machine_mode mode ATTRIBUTE_UNUSED;
6946 {
6947   HOST_WIDE_INT i;
6948
6949   if (GET_CODE (op) != CONST_INT)
6950     return 0;
6951   i = INTVAL (op);
6952   return i >= 1*8 && i <= 7*8 && (i & 7) == 0;
6953 }
6954
6955 int
6956 extend_reg_operand (op, mode)
6957      rtx op;
6958      enum machine_mode mode;
6959 {
6960   return (GET_CODE (op) == TRUNCATE
6961           ? arith_operand
6962           : arith_reg_operand) (op, mode);
6963 }
6964
6965 int
6966 trunc_hi_operand (op, mode)
6967      rtx op;
6968      enum machine_mode mode;
6969 {
6970   enum machine_mode op_mode = GET_MODE (op);
6971
6972   if (op_mode != SImode && op_mode != DImode
6973       && op_mode != V4HImode && op_mode != V2SImode)
6974     return 0;
6975   return extend_reg_operand (op, mode);
6976 }
6977
6978 int
6979 extend_reg_or_0_operand (op, mode)
6980      rtx op;
6981      enum machine_mode mode;
6982 {
6983   return (GET_CODE (op) == TRUNCATE
6984           ? arith_operand
6985           : arith_reg_or_0_operand) (op, mode);
6986 }
6987
6988 int
6989 general_extend_operand (op, mode)
6990      rtx op;
6991      enum machine_mode mode;
6992 {
6993   return (GET_CODE (op) == TRUNCATE
6994           ? arith_operand
6995           : nonimmediate_operand) (op, mode);
6996 }
6997
6998 int
6999 inqhi_operand (op, mode)
7000      rtx op;
7001      enum machine_mode mode;
7002 {
7003   if (GET_CODE (op) != TRUNCATE || mode != GET_MODE (op))
7004     return 0;
7005   op = XEXP (op, 0);
7006   /* Can't use true_regnum here because copy_cost wants to know about
7007      SECONDARY_INPUT_RELOAD_CLASS.  */
7008   return GET_CODE (op) == REG && FP_REGISTER_P (REGNO (op));
7009 }
7010
7011 int
7012 sh_rep_vec (v, mode)
7013      rtx v;
7014      enum machine_mode mode;
7015 {
7016   int i;
7017   rtx x, y;
7018
7019   if ((GET_CODE (v) != CONST_VECTOR && GET_CODE (v) != PARALLEL)
7020       || (GET_MODE (v) != mode && mode != VOIDmode))
7021     return 0;
7022   i = XVECLEN (v, 0) - 2;
7023   x = XVECEXP (v, 0, i + 1);
7024   if (GET_MODE_UNIT_SIZE (mode) == 1)
7025     {
7026       y = XVECEXP (v, 0, i);
7027       for (i -= 2 ; i >= 0; i -= 2)
7028         if (! rtx_equal_p (XVECEXP (v, 0, i + 1), x)
7029             || ! rtx_equal_p (XVECEXP (v, 0, i), y))
7030           return 0;
7031     }
7032   else
7033     for (; i >= 0; i--)
7034       if (XVECEXP (v, 0, i) != x)
7035         return 0;
7036   return 1;
7037 }
7038
7039 /* Determine if V is a constant vector matching MODE with only one element
7040    that is not a sign extension.  Two byte-sized elements count as one.  */
7041 int
7042 sh_1el_vec (v, mode)
7043      rtx v;
7044      enum machine_mode mode;
7045 {
7046   int unit_size;
7047   int i, last, least, sign_ix;
7048   rtx sign;
7049
7050   if (GET_CODE (v) != CONST_VECTOR
7051       || (GET_MODE (v) != mode && mode != VOIDmode))
7052     return 0;
7053   /* Determine numbers of last and of least significant elements.  */
7054   last = XVECLEN (v, 0) - 1;
7055   least = TARGET_LITTLE_ENDIAN ? 0 : last;
7056   if (GET_CODE (XVECEXP (v, 0, least)) != CONST_INT)
7057     return 0;
7058   sign_ix = least;
7059   if (GET_MODE_UNIT_SIZE (mode) == 1)
7060     sign_ix = TARGET_LITTLE_ENDIAN ? 1 : last - 1;
7061   if (GET_CODE (XVECEXP (v, 0, sign_ix)) != CONST_INT)
7062     return 0;
7063   unit_size = GET_MODE_UNIT_SIZE (GET_MODE (v));
7064   sign = (INTVAL (XVECEXP (v, 0, sign_ix)) >> (unit_size * BITS_PER_UNIT - 1)
7065           ? constm1_rtx : const0_rtx);
7066   i = XVECLEN (v, 0) - 1;
7067   do
7068     if (i != least && i != sign_ix && XVECEXP (v, 0, i) != sign)
7069       return 0;
7070   while (--i);
7071   return 1;
7072 }
7073
7074 int
7075 sh_const_vec (v, mode)
7076      rtx v;
7077      enum machine_mode mode;
7078 {
7079   int i;
7080
7081   if (GET_CODE (v) != CONST_VECTOR
7082       || (GET_MODE (v) != mode && mode != VOIDmode))
7083     return 0;
7084   i = XVECLEN (v, 0) - 1;
7085   for (; i >= 0; i--)
7086     if (GET_CODE (XVECEXP (v, 0, i)) != CONST_INT)
7087       return 0;
7088   return 1;
7089 }
7090 \f
7091 /* Return the destination address of a branch.  */
7092    
7093 static int
7094 branch_dest (branch)
7095      rtx branch;
7096 {
7097   rtx dest = SET_SRC (PATTERN (branch));
7098   int dest_uid;
7099
7100   if (GET_CODE (dest) == IF_THEN_ELSE)
7101     dest = XEXP (dest, 1);
7102   dest = XEXP (dest, 0);
7103   dest_uid = INSN_UID (dest);
7104   return INSN_ADDRESSES (dest_uid);
7105 }
7106 \f
7107 /* Return nonzero if REG is not used after INSN.
7108    We assume REG is a reload reg, and therefore does
7109    not live past labels.  It may live past calls or jumps though.  */
7110 int
7111 reg_unused_after (reg, insn)
7112      rtx reg;
7113      rtx insn;
7114 {
7115   enum rtx_code code;
7116   rtx set;
7117
7118   /* If the reg is set by this instruction, then it is safe for our
7119      case.  Disregard the case where this is a store to memory, since
7120      we are checking a register used in the store address.  */
7121   set = single_set (insn);
7122   if (set && GET_CODE (SET_DEST (set)) != MEM
7123       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7124     return 1;
7125
7126   while ((insn = NEXT_INSN (insn)))
7127     {
7128       code = GET_CODE (insn);
7129
7130 #if 0
7131       /* If this is a label that existed before reload, then the register
7132          if dead here.  However, if this is a label added by reorg, then
7133          the register may still be live here.  We can't tell the difference,
7134          so we just ignore labels completely.  */
7135       if (code == CODE_LABEL)
7136         return 1;
7137       /* else */
7138 #endif
7139
7140       if (code == JUMP_INSN)
7141         return 0;
7142
7143       /* If this is a sequence, we must handle them all at once.
7144          We could have for instance a call that sets the target register,
7145          and an insn in a delay slot that uses the register.  In this case,
7146          we must return 0.  */
7147       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
7148         {
7149           int i;
7150           int retval = 0;
7151
7152           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
7153             {
7154               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
7155               rtx set = single_set (this_insn);
7156
7157               if (GET_CODE (this_insn) == CALL_INSN)
7158                 code = CALL_INSN;
7159               else if (GET_CODE (this_insn) == JUMP_INSN)
7160                 {
7161                   if (INSN_ANNULLED_BRANCH_P (this_insn))
7162                     return 0;
7163                   code = JUMP_INSN;
7164                 }
7165
7166               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
7167                 return 0;
7168               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7169                 {
7170                   if (GET_CODE (SET_DEST (set)) != MEM)
7171                     retval = 1;
7172                   else
7173                     return 0;
7174                 }
7175               if (set == 0
7176                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
7177                 return 0;
7178             }
7179           if (retval == 1)
7180             return 1;
7181           else if (code == JUMP_INSN)
7182             return 0;
7183         }
7184       else if (GET_RTX_CLASS (code) == 'i')
7185         {
7186           rtx set = single_set (insn);
7187
7188           if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
7189             return 0;
7190           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
7191             return GET_CODE (SET_DEST (set)) != MEM;
7192           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
7193             return 0;
7194         }
7195
7196       if (code == CALL_INSN && call_used_regs[REGNO (reg)])
7197         return 1;
7198     }
7199   return 1;
7200 }
7201 \f
7202 #include "ggc.h"
7203
7204 static GTY(()) rtx fpscr_rtx;
7205 rtx
7206 get_fpscr_rtx ()
7207 {
7208   if (! fpscr_rtx)
7209     {
7210       fpscr_rtx = gen_rtx (REG, PSImode, FPSCR_REG);
7211       REG_USERVAR_P (fpscr_rtx) = 1;
7212       mark_user_reg (fpscr_rtx);
7213     }
7214   if (! reload_completed || mdep_reorg_phase != SH_AFTER_MDEP_REORG)
7215     mark_user_reg (fpscr_rtx);
7216   return fpscr_rtx;
7217 }
7218
7219 void
7220 emit_sf_insn (pat)
7221      rtx pat;
7222 {
7223   emit_insn (pat);
7224 }
7225
7226 void
7227 emit_df_insn (pat)
7228      rtx pat;
7229 {
7230   emit_insn (pat);
7231 }
7232
7233 void
7234 expand_sf_unop (fun, operands)
7235      rtx (*fun) PARAMS ((rtx, rtx, rtx));
7236      rtx *operands;
7237 {
7238   emit_sf_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
7239 }
7240
7241 void
7242 expand_sf_binop (fun, operands)
7243      rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
7244      rtx *operands;
7245 {
7246   emit_sf_insn ((*fun) (operands[0], operands[1], operands[2],
7247                          get_fpscr_rtx ()));
7248 }
7249
7250 void
7251 expand_df_unop (fun, operands)
7252      rtx (*fun) PARAMS ((rtx, rtx, rtx));
7253      rtx *operands;
7254 {
7255   emit_df_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
7256 }
7257
7258 void
7259 expand_df_binop (fun, operands)
7260      rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
7261      rtx *operands;
7262 {
7263   emit_df_insn ((*fun) (operands[0], operands[1], operands[2],
7264                          get_fpscr_rtx ()));
7265 }
7266 \f
7267 /* ??? gcc does flow analysis strictly after common subexpression
7268    elimination.  As a result, common subexpression elimination fails
7269    when there are some intervening statements setting the same register.
7270    If we did nothing about this, this would hurt the precision switching
7271    for SH4 badly.  There is some cse after reload, but it is unable to
7272    undo the extra register pressure from the unused instructions, and
7273    it cannot remove auto-increment loads.
7274
7275    A C code example that shows this flow/cse weakness for (at least) SH
7276    and sparc (as of gcc ss-970706) is this:
7277
7278 double
7279 f(double a)
7280 {
7281   double d;
7282   d = 0.1;
7283   a += d;
7284   d = 1.1;
7285   d = 0.1;
7286   a *= d;
7287   return a;
7288 }
7289
7290    So we add another pass before common subexpression elimination, to
7291    remove assignments that are dead due to a following assignment in the
7292    same basic block.  */
7293
7294 static void 
7295 mark_use (x, reg_set_block)
7296      rtx x, *reg_set_block;
7297 {
7298   enum rtx_code code;
7299
7300   if (! x)
7301     return;
7302   code = GET_CODE (x);
7303   switch (code)
7304     {
7305     case REG:
7306       {
7307         int regno = REGNO (x);
7308         int nregs = (regno < FIRST_PSEUDO_REGISTER
7309                      ? HARD_REGNO_NREGS (regno, GET_MODE (x))
7310                      : 1);
7311         do
7312           {
7313             reg_set_block[regno + nregs - 1] = 0;
7314           }
7315         while (--nregs);
7316         break;
7317       }
7318     case SET:
7319       {
7320         rtx dest = SET_DEST (x);
7321
7322         if (GET_CODE (dest) == SUBREG)
7323           dest = SUBREG_REG (dest);
7324         if (GET_CODE (dest) != REG)
7325           mark_use (dest, reg_set_block);
7326         mark_use (SET_SRC (x), reg_set_block);
7327         break;
7328       }
7329     case CLOBBER:
7330       break;
7331     default:
7332       {
7333         const char *fmt = GET_RTX_FORMAT (code);
7334         int i, j;
7335         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7336           {
7337             if (fmt[i] == 'e')
7338               mark_use (XEXP (x, i), reg_set_block);
7339             else if (fmt[i] == 'E')
7340               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7341                 mark_use (XVECEXP (x, i, j), reg_set_block);
7342           }
7343         break;
7344       }
7345     }
7346 }
7347 \f
7348 static rtx get_free_reg PARAMS ((HARD_REG_SET));
7349
7350 /* This function returns a register to use to load the address to load
7351    the fpscr from.  Currently it always returns r1 or r7, but when we are
7352    able to use pseudo registers after combine, or have a better mechanism
7353    for choosing a register, it should be done here.  */
7354 /* REGS_LIVE is the liveness information for the point for which we
7355    need this allocation.  In some bare-bones exit blocks, r1 is live at the
7356    start.  We can even have all of r0..r3 being live:
7357 __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
7358    INSN before which new insns are placed with will clobber the register
7359    we return.  If a basic block consists only of setting the return value
7360    register to a pseudo and using that register, the return value is not
7361    live before or after this block, yet we we'll insert our insns right in
7362    the middle.  */
7363
7364 static rtx
7365 get_free_reg (regs_live)
7366      HARD_REG_SET regs_live;
7367 {
7368   if (! TEST_HARD_REG_BIT (regs_live, 1))
7369     return gen_rtx_REG (Pmode, 1);
7370
7371   /* Hard reg 1 is live; since this is a SMALL_REGISTER_CLASSES target,
7372      there shouldn't be anything but a jump before the function end.  */
7373   if (! TEST_HARD_REG_BIT (regs_live, 7))
7374     return gen_rtx_REG (Pmode, 7);
7375
7376   abort ();
7377 }
7378
7379 /* This function will set the fpscr from memory. 
7380    MODE is the mode we are setting it to.  */
7381 void
7382 fpscr_set_from_mem (mode, regs_live)
7383      int mode;
7384      HARD_REG_SET regs_live;
7385 {
7386   enum attr_fp_mode fp_mode = mode;
7387   rtx addr_reg = get_free_reg (regs_live);
7388
7389   if (fp_mode == (enum attr_fp_mode) ACTUAL_NORMAL_MODE (FP_MODE))
7390     emit_insn (gen_fpu_switch1 (addr_reg));
7391   else
7392     emit_insn (gen_fpu_switch0 (addr_reg));
7393 }
7394
7395 /* Is the given character a logical line separator for the assembler?  */
7396 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
7397 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C) ((C) == ';')
7398 #endif
7399
7400 int
7401 sh_insn_length_adjustment (insn)
7402      rtx insn;
7403 {
7404   /* Instructions with unfilled delay slots take up an extra two bytes for
7405      the nop in the delay slot.  */
7406   if (((GET_CODE (insn) == INSN
7407         && GET_CODE (PATTERN (insn)) != USE
7408         && GET_CODE (PATTERN (insn)) != CLOBBER)
7409        || GET_CODE (insn) == CALL_INSN
7410        || (GET_CODE (insn) == JUMP_INSN
7411            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
7412            && GET_CODE (PATTERN (insn)) != ADDR_VEC))
7413       && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE
7414       && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
7415     return 2;
7416
7417   /* SH2e has a bug that prevents the use of annulled branches, so if
7418      the delay slot is not filled, we'll have to put a NOP in it.  */
7419   if (sh_cpu == CPU_SH2E
7420       && GET_CODE (insn) == JUMP_INSN
7421       && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
7422       && GET_CODE (PATTERN (insn)) != ADDR_VEC
7423       && get_attr_type (insn) == TYPE_CBRANCH
7424       && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE)
7425     return 2;
7426
7427   /* sh-dsp parallel processing insn take four bytes instead of two.  */
7428      
7429   if (GET_CODE (insn) == INSN)
7430     {
7431       int sum = 0;
7432       rtx body = PATTERN (insn);
7433       const char *template;
7434       char c;
7435       int maybe_label = 1;
7436
7437       if (GET_CODE (body) == ASM_INPUT)
7438         template = XSTR (body, 0);
7439       else if (asm_noperands (body) >= 0)
7440         template
7441           = decode_asm_operands (body, NULL, NULL, NULL, NULL);
7442       else
7443         return 0;
7444       do
7445         {
7446           int ppi_adjust = 0;
7447
7448           do
7449             c = *template++;
7450           while (c == ' ' || c == '\t');
7451           /* all sh-dsp parallel-processing insns start with p.
7452              The only non-ppi sh insn starting with p is pref.
7453              The only ppi starting with pr is prnd.  */
7454           if ((c == 'p' || c == 'P') && strncasecmp ("re", template, 2))
7455             ppi_adjust = 2;
7456           /* The repeat pseudo-insn expands two three insns, a total of
7457              six bytes in size.  */
7458           else if ((c == 'r' || c == 'R')
7459                    && ! strncasecmp ("epeat", template, 5))
7460             ppi_adjust = 4;
7461           while (c && c != '\n' && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c))
7462             {
7463               /* If this is a label, it is obviously not a ppi insn.  */
7464               if (c == ':' && maybe_label)
7465                 {
7466                   ppi_adjust = 0;
7467                   break;
7468                 }
7469               else if (c == '\'' || c == '"')
7470                 maybe_label = 0;
7471               c = *template++;
7472             }
7473           sum += ppi_adjust;
7474           maybe_label = c != ':';
7475         }
7476       while (c);
7477       return sum;
7478     }
7479   return 0;
7480 }
7481 \f
7482 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
7483    isn't protected by a PIC unspec.  */
7484 int
7485 nonpic_symbol_mentioned_p (x)
7486      rtx x;
7487 {
7488   register const char *fmt;
7489   register int i;
7490
7491   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
7492       || GET_CODE (x) == PC)
7493     return 1;
7494
7495   /* We don't want to look into the possible MEM location of a
7496      CONST_DOUBLE, since we're not going to use it, in general.  */
7497   if (GET_CODE (x) == CONST_DOUBLE)
7498     return 0;
7499
7500   if (GET_CODE (x) == UNSPEC
7501       && (XINT (x, 1) == UNSPEC_PIC
7502           || XINT (x, 1) == UNSPEC_GOT
7503           || XINT (x, 1) == UNSPEC_GOTOFF
7504           || XINT (x, 1) == UNSPEC_GOTPLT
7505           || XINT (x, 1) == UNSPEC_GOTTPOFF
7506           || XINT (x, 1) == UNSPEC_DTPOFF
7507           || XINT (x, 1) == UNSPEC_PLT))
7508       return 0;
7509
7510   fmt = GET_RTX_FORMAT (GET_CODE (x));
7511   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7512     {
7513       if (fmt[i] == 'E')
7514         {
7515           register int j;
7516
7517           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7518             if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
7519               return 1;
7520         }
7521       else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
7522         return 1;
7523     }
7524
7525   return 0;
7526 }
7527
7528 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
7529    @GOTOFF in `reg'.  */
7530 rtx
7531 legitimize_pic_address (orig, mode, reg)
7532      rtx orig;
7533      enum machine_mode mode ATTRIBUTE_UNUSED;
7534      rtx reg;
7535 {
7536   if (tls_symbolic_operand (orig, Pmode))
7537     return orig;
7538
7539   if (GET_CODE (orig) == LABEL_REF
7540       || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
7541     {
7542       if (reg == 0)
7543         reg = gen_reg_rtx (Pmode);
7544
7545       emit_insn (gen_symGOTOFF2reg (reg, orig));
7546       return reg;
7547     }
7548   else if (GET_CODE (orig) == SYMBOL_REF)
7549     {
7550       if (reg == 0)
7551         reg = gen_reg_rtx (Pmode);
7552
7553       emit_insn (gen_symGOT2reg (reg, orig));
7554       return reg;
7555     }
7556   return orig;
7557 }
7558
7559 /* Mark the use of a constant in the literal table. If the constant
7560    has multiple labels, make it unique.  */
7561 static rtx
7562 mark_constant_pool_use (x)
7563      rtx x;
7564 {
7565   rtx insn, lab, pattern;
7566
7567   if (x == NULL)
7568     return x;
7569
7570   switch (GET_CODE (x))
7571     {
7572     case LABEL_REF:
7573       x = XEXP (x, 0);
7574     case CODE_LABEL:
7575       break;
7576     default:
7577       return x;
7578     }
7579
7580   /* Get the first label in the list of labels for the same constant
7581      and delete another labels in the list.  */
7582   lab = x;
7583   for (insn = PREV_INSN (x); insn; insn = PREV_INSN (insn))
7584     {
7585       if (GET_CODE (insn) != CODE_LABEL
7586           || LABEL_REFS (insn) != NEXT_INSN (insn))
7587         break;
7588       lab = insn;
7589     }
7590
7591   for (insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
7592     INSN_DELETED_P (insn) = 1;
7593
7594   /* Mark constants in a window.  */
7595   for (insn = NEXT_INSN (x); insn; insn = NEXT_INSN (insn))
7596     {
7597       if (GET_CODE (insn) != INSN)
7598         continue;
7599
7600       pattern = PATTERN (insn);
7601       if (GET_CODE (pattern) != UNSPEC_VOLATILE)
7602         continue;
7603
7604       switch (XINT (pattern, 1))
7605         {
7606         case UNSPECV_CONST2:
7607         case UNSPECV_CONST4:
7608         case UNSPECV_CONST8:
7609           XVECEXP (pattern, 0, 1) = const1_rtx;
7610           break;
7611         case UNSPECV_WINDOW_END:
7612           if (XVECEXP (pattern, 0, 0) == x)
7613             return lab;
7614           break;
7615         case UNSPECV_CONST_END:
7616           return lab;
7617         default:
7618           break;
7619         }
7620     }
7621
7622   return lab;
7623 }
7624 \f
7625 /* Return true if it's possible to redirect BRANCH1 to the destination
7626    of an unconditional jump BRANCH2.  We only want to do this if the
7627    resulting branch will have a short displacement.  */
7628 int 
7629 sh_can_redirect_branch (branch1, branch2)
7630      rtx branch1;
7631      rtx branch2;
7632 {
7633   if (flag_expensive_optimizations && simplejump_p (branch2))
7634     {
7635       rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
7636       rtx insn;
7637       int distance;
7638       
7639       for (distance = 0, insn = NEXT_INSN (branch1); 
7640            insn && distance < 256; 
7641            insn = PREV_INSN (insn))
7642         {
7643           if (insn == dest)    
7644             return 1;
7645           else
7646             distance += get_attr_length (insn);
7647         }
7648       for (distance = 0, insn = NEXT_INSN (branch1); 
7649            insn && distance < 256; 
7650            insn = NEXT_INSN (insn))
7651         {
7652           if (insn == dest)    
7653             return 1;
7654           else
7655             distance += get_attr_length (insn);
7656         }
7657     }
7658   return 0;
7659 }
7660
7661 /* Return nonzero if register old_reg can be renamed to register new_reg.  */
7662 int
7663 sh_hard_regno_rename_ok (old_reg, new_reg)
7664      unsigned int old_reg ATTRIBUTE_UNUSED;
7665      unsigned int new_reg;
7666 {
7667
7668 /* Interrupt functions can only use registers that have already been
7669    saved by the prologue, even if they would normally be
7670    call-clobbered.  */
7671
7672   if (sh_cfun_interrupt_handler_p () && !regs_ever_live[new_reg])
7673      return 0;
7674
7675    return 1;
7676 }
7677
7678 /* Function to update the integer COST
7679    based on the relationship between INSN that is dependent on
7680    DEP_INSN through the dependence LINK.  The default is to make no
7681    adjustment to COST.  This can be used for example to specify to
7682    the scheduler that an output- or anti-dependence does not incur
7683    the same cost as a data-dependence.  The return value should be
7684    the new value for COST.  */
7685 static int
7686 sh_adjust_cost (insn, link, dep_insn, cost)
7687      rtx insn;
7688      rtx link ATTRIBUTE_UNUSED;
7689      rtx dep_insn;
7690      int cost;
7691 {
7692   rtx reg, use_pat;
7693
7694   if (TARGET_SHMEDIA)
7695     {
7696       /* On SHmedia, if the dependence is an anti-dependence or
7697          output-dependence, there is no cost. */              
7698       if (REG_NOTE_KIND (link) != 0)
7699         cost = 0;
7700
7701       if (get_attr_is_mac_media (insn)
7702           && get_attr_is_mac_media (dep_insn))
7703         cost = 1;
7704     }
7705   else if (REG_NOTE_KIND (link) == 0)
7706     {
7707       enum attr_type dep_type, type;
7708
7709       if (recog_memoized (insn) < 0
7710           || recog_memoized (dep_insn) < 0)
7711         return cost;
7712
7713       dep_type = get_attr_type (dep_insn);
7714       if (dep_type == TYPE_FLOAD || dep_type == TYPE_PCFLOAD)
7715         cost--;
7716       if ((dep_type == TYPE_LOAD_SI || dep_type == TYPE_PCLOAD_SI)
7717           && (type = get_attr_type (insn)) != TYPE_CALL
7718           && type != TYPE_SFUNC)
7719         cost--;
7720
7721       /* The only input for a call that is timing-critical is the
7722          function's address.  */
7723       if (GET_CODE(insn) == CALL_INSN)
7724         {
7725           rtx call = PATTERN (insn);
7726
7727           if (GET_CODE (call) == PARALLEL)
7728             call = XVECEXP (call, 0 ,0);
7729           if (GET_CODE (call) == SET)
7730             call = SET_SRC (call);
7731           if (GET_CODE (call) == CALL && GET_CODE (XEXP (call, 0)) == MEM
7732               && ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn))
7733             cost = 0;
7734         }
7735       /* Likewise, the most timing critical input for an sfuncs call
7736          is the function address.  However, sfuncs typically start
7737          using their arguments pretty quickly.
7738          Assume a four cycle delay before they are needed.  */
7739       /* All sfunc calls are parallels with at least four components.
7740          Exploit this to avoid unnecessary calls to sfunc_uses_reg.  */
7741       else if (GET_CODE (PATTERN (insn)) == PARALLEL
7742                && XVECLEN (PATTERN (insn), 0) >= 4
7743                && (reg = sfunc_uses_reg (insn)))
7744         {
7745           if (! reg_set_p (reg, dep_insn))
7746             cost -= 4;
7747         }
7748       /* When the preceding instruction loads the shift amount of
7749          the following SHAD/SHLD, the latency of the load is increased
7750          by 1 cycle.  */
7751       else if (TARGET_SH4
7752                && get_attr_type (insn) == TYPE_DYN_SHIFT
7753                && get_attr_any_int_load (dep_insn) == ANY_INT_LOAD_YES
7754                && reg_overlap_mentioned_p (SET_DEST (PATTERN (dep_insn)),
7755                                            XEXP (SET_SRC (single_set(insn)),
7756                                                  1)))
7757         cost++;
7758       /* When an LS group instruction with a latency of less than
7759          3 cycles is followed by a double-precision floating-point
7760          instruction, FIPR, or FTRV, the latency of the first
7761          instruction is increased to 3 cycles.  */
7762       else if (cost < 3
7763                && get_attr_insn_class (dep_insn) == INSN_CLASS_LS_GROUP
7764                && get_attr_dfp_comp (insn) == DFP_COMP_YES)
7765         cost = 3;
7766       /* The lsw register of a double-precision computation is ready one
7767          cycle earlier.  */
7768       else if (reload_completed
7769                && get_attr_dfp_comp (dep_insn) == DFP_COMP_YES
7770                && (use_pat = single_set (insn))
7771                && ! regno_use_in (REGNO (SET_DEST (single_set (dep_insn))),
7772                                   SET_SRC (use_pat)))
7773         cost -= 1;
7774
7775       if (get_attr_any_fp_comp (dep_insn) == ANY_FP_COMP_YES
7776           && get_attr_late_fp_use (insn) == LATE_FP_USE_YES)
7777         cost -= 1;
7778     }
7779   /* An anti-dependence penalty of two applies if the first insn is a double
7780      precision fadd / fsub / fmul.  */
7781   else if (REG_NOTE_KIND (link) == REG_DEP_ANTI
7782            && recog_memoized (dep_insn) >= 0
7783            && get_attr_type (dep_insn) == TYPE_DFP_ARITH
7784            /* A lot of alleged anti-flow dependences are fake,
7785               so check this one is real.  */
7786            && flow_dependent_p (dep_insn, insn))
7787     cost = 2;
7788
7789
7790   return cost;
7791 }
7792
7793 /* Check if INSN is flow-dependent on DEP_INSN.  Can also be used to check
7794    if DEP_INSN is anti-flow dependent on INSN.  */
7795 static int
7796 flow_dependent_p (insn, dep_insn)
7797      rtx insn, dep_insn;
7798 {
7799   rtx tmp = PATTERN (insn);
7800
7801   note_stores (PATTERN (dep_insn), flow_dependent_p_1, &tmp);
7802   return tmp == NULL_RTX;
7803 }
7804
7805 /* A helper function for flow_dependent_p called through note_stores.  */
7806 static void
7807 flow_dependent_p_1 (x, pat, data)
7808      rtx x;
7809      rtx pat ATTRIBUTE_UNUSED;
7810      void *data;
7811 {
7812   rtx * pinsn = (rtx *) data;
7813
7814   if (*pinsn && reg_referenced_p (x, *pinsn))
7815     *pinsn = NULL_RTX;
7816 }
7817
7818 /* For use by ALLOCATE_INITIAL_VALUE.  Note that sh.md contains some
7819    'special function' patterns (type sfunc) that clobber pr, but that
7820    do not look like function calls to leaf_function_p.  Hence we must
7821    do this extra check.  */
7822 int
7823 sh_pr_n_sets ()
7824 {
7825   return REG_N_SETS (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
7826 }
7827
7828 /* This Function returns nonzero if the DFA based scheduler interface
7829    is to be used.  At present this is supported for the SH4 only.  */
7830 static int
7831 sh_use_dfa_interface()
7832 {
7833   if (TARGET_HARD_SH4)
7834     return 1;
7835   else
7836     return 0;
7837 }
7838
7839 /* This function returns "2" to indicate dual issue for the SH4
7840    processor.  To be used by the DFA pipeline description.  */
7841 static int
7842 sh_issue_rate()
7843 {
7844   if (TARGET_SUPERSCALAR)
7845     return 2;
7846   else
7847     return 1;
7848 }
7849
7850 /* SHmedia requires registers for branches, so we can't generate new
7851    branches past reload.  */
7852 static bool
7853 sh_cannot_modify_jumps_p ()
7854 {
7855   return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
7856 }
7857
7858 static int
7859 sh_target_reg_class (void)
7860 {
7861   return TARGET_SHMEDIA ? TARGET_REGS : NO_REGS;
7862 }
7863
7864 static bool
7865 sh_optimize_target_register_callee_saved (bool after_prologue_epilogue_gen)
7866 {
7867   return (shmedia_space_reserved_for_target_registers
7868           && (! after_prologue_epilogue_gen || TARGET_SAVE_ALL_TARGET_REGS));
7869 }
7870
7871 static bool
7872 sh_ms_bitfield_layout_p (record_type)
7873      tree record_type ATTRIBUTE_UNUSED;
7874 {
7875   return TARGET_SH5;
7876 }
7877 \f
7878 /* 
7879    On the SH1..SH4, the trampoline looks like
7880    2 0002 D202                  mov.l   l2,r2
7881    1 0000 D301                  mov.l   l1,r3
7882    3 0004 422B                  jmp     @r2
7883    4 0006 0009                  nop
7884    5 0008 00000000      l1:     .long   area
7885    6 000c 00000000      l2:     .long   function
7886
7887    SH5 (compact) uses r1 instead of r3 for the static chain.  */
7888
7889
7890 /* Emit RTL insns to initialize the variable parts of a trampoline.
7891    FNADDR is an RTX for the address of the function's pure code.
7892    CXT is an RTX for the static chain value for the function.  */
7893
7894 void
7895 sh_initialize_trampoline (tramp, fnaddr, cxt)
7896      rtx tramp, fnaddr, cxt;
7897 {
7898   if (TARGET_SHMEDIA64)
7899     {
7900       rtx tramp_templ;
7901       int fixed_len;
7902
7903       rtx movi1 = GEN_INT (0xcc000010);
7904       rtx shori1 = GEN_INT (0xc8000010);
7905       rtx src, dst;
7906
7907       /* The following trampoline works within a +- 128 KB range for cxt:
7908          ptb/u cxt,tr1; movi fnaddr >> 48,r0; shori fnaddr >> 32,r0;
7909          shori fnaddr >> 16,r0; shori fnaddr,r0; ptabs/l r0,tr0
7910          gettr tr1,r1; blink tr0,r63  */
7911       /* Address rounding makes it hard to compute the exact bounds of the
7912          offset for this trampoline, but we have a rather generous offset
7913          range, so frame_offset should do fine as an upper bound.  */
7914       if (cxt == virtual_stack_vars_rtx && frame_offset < 0x20000)
7915         {
7916           /* ??? could optimize this trampoline initialization
7917              by writing DImode words with two insns each.  */
7918           rtx mask = force_reg (DImode, GEN_INT (0x3fffc00));
7919           rtx insn = gen_rtx_MINUS (DImode, cxt, tramp);
7920           insn = gen_rtx_ASHIFT (DImode, insn, GEN_INT (10-2));
7921           insn = gen_rtx_AND (DImode, insn, mask);
7922           /* Or in ptb/u .,tr1 pattern */
7923           insn = gen_rtx_IOR (DImode, insn, gen_int_mode (0xec000010, SImode));
7924           insn = force_operand (insn, NULL_RTX);
7925           insn = gen_lowpart (SImode, insn);
7926           emit_move_insn (gen_rtx_MEM (SImode, tramp), insn);
7927           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (38));
7928           insn = gen_rtx_AND (DImode, insn, mask);
7929           insn = force_operand (gen_rtx_IOR (DImode, movi1, insn), NULL_RTX);
7930           insn = gen_lowpart (SImode, insn);
7931           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)), insn);
7932           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (22));
7933           insn = gen_rtx_AND (DImode, insn, mask);
7934           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7935           insn = gen_lowpart (SImode, insn);
7936           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)), insn);
7937           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (6));
7938           insn = gen_rtx_AND (DImode, insn, mask);
7939           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7940           insn = gen_lowpart (SImode, insn);
7941           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
7942                           insn);
7943           insn = gen_rtx_ASHIFT (DImode, fnaddr, GEN_INT (10));
7944           insn = gen_rtx_AND (DImode, insn, mask);
7945           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7946           insn = gen_lowpart (SImode, insn);
7947           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 16)),
7948                           insn);
7949           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 20)),
7950                           GEN_INT (0x6bf10600));
7951           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 24)),
7952                           GEN_INT (0x4415fc10));
7953           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 28)),
7954                           GEN_INT (0x4401fff0));
7955           emit_insn (gen_ic_invalidate_line (tramp));
7956           return;
7957         }
7958       tramp_templ = gen_rtx_SYMBOL_REF (Pmode,"__GCC_nested_trampoline");
7959       fixed_len = TRAMPOLINE_SIZE - 2 * GET_MODE_SIZE (Pmode);
7960
7961       tramp_templ = gen_datalabel_ref (tramp_templ);
7962       dst = gen_rtx_MEM (BLKmode, tramp);
7963       src = gen_rtx_MEM (BLKmode, tramp_templ);
7964       set_mem_align (dst, 256);
7965       set_mem_align (src, 64);
7966       emit_block_move (dst, src, GEN_INT (fixed_len), BLOCK_OP_NORMAL);
7967
7968       emit_move_insn (gen_rtx_MEM (Pmode, plus_constant (tramp, fixed_len)),
7969                       fnaddr);
7970       emit_move_insn (gen_rtx_MEM (Pmode,
7971                                    plus_constant (tramp,
7972                                                   fixed_len
7973                                                   + GET_MODE_SIZE (Pmode))), 
7974                       cxt);
7975       emit_insn (gen_ic_invalidate_line (tramp));
7976       return;
7977     }
7978   else if (TARGET_SHMEDIA)
7979     {
7980       /* movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
7981          movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63  */
7982       rtx quad0 = gen_reg_rtx (DImode), cxtload = gen_reg_rtx (DImode);
7983       rtx quad1 = gen_reg_rtx (DImode), quad2 = gen_reg_rtx (DImode);
7984       /* movi 0,r1: 0xcc000010 shori 0,r1: c8000010  concatenated,
7985          rotated 10 right, and higher 16 bit of every 32 selected.  */
7986       rtx movishori
7987         = force_reg (V2HImode, (simplify_gen_subreg
7988                                 (V2HImode, GEN_INT (0x4330432), SImode, 0)));
7989       rtx ptabs = force_reg (DImode, GEN_INT (0x6bf10600));
7990       rtx blink = force_reg (DImode, GEN_INT (0x4401fff0));
7991
7992       tramp = force_reg (Pmode, tramp);
7993       fnaddr = force_reg (SImode, fnaddr);
7994       cxt = force_reg (SImode, cxt);
7995       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, quad0, 0),
7996                                  gen_rtx_SUBREG (V2HImode, fnaddr, 0),
7997                                  movishori));
7998       emit_insn (gen_rotrdi3_mextr (quad0, quad0,
7999                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
8000       emit_insn (gen_ashldi3_media (quad0, quad0, GEN_INT (2)));
8001       emit_move_insn (gen_rtx_MEM (DImode, tramp), quad0);
8002       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, cxtload, 0),
8003                                  gen_rtx_SUBREG (V2HImode, cxt, 0),
8004                                  movishori));
8005       emit_insn (gen_rotrdi3_mextr (cxtload, cxtload,
8006                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
8007       emit_insn (gen_ashldi3_media (cxtload, cxtload, GEN_INT (2)));
8008       if (TARGET_LITTLE_ENDIAN)
8009         {
8010           emit_insn (gen_mshflo_l_di (quad1, ptabs, cxtload));
8011           emit_insn (gen_mextr4 (quad2, cxtload, blink));
8012         }
8013       else
8014         {
8015           emit_insn (gen_mextr4 (quad1, cxtload, ptabs));
8016           emit_insn (gen_mshflo_l_di (quad2, blink, cxtload));
8017         }
8018       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 8)), quad1);
8019       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), quad2);
8020       emit_insn (gen_ic_invalidate_line (tramp));
8021       return;
8022     }
8023   else if (TARGET_SHCOMPACT)
8024     {
8025       emit_insn (gen_initialize_trampoline (tramp, cxt, fnaddr));
8026       return;
8027     }
8028   emit_move_insn (gen_rtx_MEM (SImode, tramp),
8029                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0xd301d202 : 0xd202d301,
8030                                 SImode));
8031   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
8032                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0x0009422b : 0x422b0009,
8033                                 SImode));
8034   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
8035                   cxt);
8036   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
8037                   fnaddr);
8038   if (TARGET_HARVARD)
8039     {
8040       if (TARGET_USERMODE)
8041         emit_library_call (function_symbol ("__ic_invalidate"),
8042                            0, VOIDmode, 1, tramp, SImode);
8043       else
8044         emit_insn (gen_ic_invalidate_line (tramp));
8045     }
8046 }
8047
8048 /* FIXME: This is overly conservative.  A SHcompact function that
8049    receives arguments ``by reference'' will have them stored in its
8050    own stack frame, so it must not pass pointers or references to
8051    these arguments to other functions by means of sibling calls.  */
8052 static bool
8053 sh_function_ok_for_sibcall (decl, exp)
8054      tree decl;
8055      tree exp ATTRIBUTE_UNUSED;
8056 {
8057   return (decl 
8058           && (! TARGET_SHCOMPACT
8059               || current_function_args_info.stack_regs == 0)
8060           && ! sh_cfun_interrupt_handler_p ());
8061 }
8062 \f
8063 /* Machine specific built-in functions.  */
8064
8065 struct builtin_description
8066 {
8067   const enum insn_code icode;
8068   const char *const name;
8069   int signature;
8070 };
8071
8072 /* describe number and signedness of arguments; arg[0] == result
8073    (1: unsigned, 2: signed, 4: don't care, 8: pointer 0: no argument */
8074 static const char signature_args[][4] =
8075 {
8076 #define SH_BLTIN_V2SI2 0
8077   { 4, 4 },
8078 #define SH_BLTIN_V4HI2 1
8079   { 4, 4 },
8080 #define SH_BLTIN_V2SI3 2
8081   { 4, 4, 4 },
8082 #define SH_BLTIN_V4HI3 3
8083   { 4, 4, 4 },
8084 #define SH_BLTIN_V8QI3 4
8085   { 4, 4, 4 },
8086 #define SH_BLTIN_MAC_HISI 5
8087   { 1, 4, 4, 1 },
8088 #define SH_BLTIN_SH_HI 6
8089   { 4, 4, 1 },
8090 #define SH_BLTIN_SH_SI 7
8091   { 4, 4, 1 },
8092 #define SH_BLTIN_V4HI2V2SI 8
8093   { 4, 4, 4 },
8094 #define SH_BLTIN_V4HI2V8QI 9
8095   { 4, 4, 4 },
8096 #define SH_BLTIN_SISF 10
8097   { 4, 2 },
8098 #define SH_BLTIN_LDUA_L 11
8099   { 2, 8 },
8100 #define SH_BLTIN_LDUA_Q 12
8101   { 1, 8 },
8102 #define SH_BLTIN_STUA_L 13
8103   { 0, 8, 2 },
8104 #define SH_BLTIN_STUA_Q 14
8105   { 0, 8, 1 },
8106 #define SH_BLTIN_UDI 15
8107   { 0, 8, 1 },
8108 #define SH_BLTIN_NUM_SHARED_SIGNATURES 16
8109 #define SH_BLTIN_2 16
8110 #define SH_BLTIN_SU 16
8111   { 1, 2 },
8112 #define SH_BLTIN_3 17
8113 #define SH_BLTIN_SUS 17
8114   { 2, 2, 1 },
8115 #define SH_BLTIN_PSSV 18
8116   { 0, 8, 2, 2 },
8117 #define SH_BLTIN_XXUU 19
8118 #define SH_BLTIN_UUUU 19
8119   { 1, 1, 1, 1 },
8120 #define SH_BLTIN_PV 20
8121   { 0, 8 },
8122 };
8123 /* mcmv: operands considered unsigned. */
8124 /* mmulsum_wq, msad_ubq: result considered unsigned long long.  */
8125 /* mperm: control value considered unsigned int. */
8126 /* mshalds, mshard, mshards, mshlld, mshlrd: shift count is unsigned int. */
8127 /* mshards_q: returns signed short.  */
8128 /* nsb: takes long long arg, returns unsigned char.  */
8129 static const struct builtin_description bdesc[] =
8130 {
8131   { CODE_FOR_absv2si2,  "__builtin_absv2si2", SH_BLTIN_V2SI2 },
8132   { CODE_FOR_absv4hi2,  "__builtin_absv4hi2", SH_BLTIN_V4HI2 },
8133   { CODE_FOR_addv2si3,  "__builtin_addv2si3", SH_BLTIN_V2SI3 },
8134   { CODE_FOR_addv4hi3,  "__builtin_addv4hi3", SH_BLTIN_V4HI3 },
8135   { CODE_FOR_ssaddv2si3,"__builtin_ssaddv2si3", SH_BLTIN_V2SI3 },
8136   { CODE_FOR_usaddv8qi3,"__builtin_usaddv8qi3", SH_BLTIN_V8QI3 },
8137   { CODE_FOR_ssaddv4hi3,"__builtin_ssaddv4hi3", SH_BLTIN_V4HI3 },
8138 #if 0
8139   { CODE_FOR_alloco32,  "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
8140   { CODE_FOR_alloco64,  "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
8141 #endif
8142   { CODE_FOR_negcmpeqv8qi,"__builtin_sh_media_MCMPEQ_B", SH_BLTIN_V8QI3 },
8143   { CODE_FOR_negcmpeqv2si,"__builtin_sh_media_MCMPEQ_L", SH_BLTIN_V2SI3 },
8144   { CODE_FOR_negcmpeqv4hi,"__builtin_sh_media_MCMPEQ_W", SH_BLTIN_V4HI3 },
8145   { CODE_FOR_negcmpgtuv8qi,"__builtin_sh_media_MCMPGT_UB", SH_BLTIN_V8QI3 },
8146   { CODE_FOR_negcmpgtv2si,"__builtin_sh_media_MCMPGT_L", SH_BLTIN_V2SI3 },
8147   { CODE_FOR_negcmpgtv4hi,"__builtin_sh_media_MCMPGT_W", SH_BLTIN_V4HI3 },
8148   { CODE_FOR_mcmv,      "__builtin_sh_media_MCMV", SH_BLTIN_UUUU },
8149   { CODE_FOR_mcnvs_lw,  "__builtin_sh_media_MCNVS_LW", SH_BLTIN_3 },
8150   { CODE_FOR_mcnvs_wb,  "__builtin_sh_media_MCNVS_WB", SH_BLTIN_V4HI2V8QI },
8151   { CODE_FOR_mcnvs_wub, "__builtin_sh_media_MCNVS_WUB", SH_BLTIN_V4HI2V8QI },
8152   { CODE_FOR_mextr1,    "__builtin_sh_media_MEXTR1", SH_BLTIN_UDI },
8153   { CODE_FOR_mextr2,    "__builtin_sh_media_MEXTR2", SH_BLTIN_UDI },
8154   { CODE_FOR_mextr3,    "__builtin_sh_media_MEXTR3", SH_BLTIN_UDI },
8155   { CODE_FOR_mextr4,    "__builtin_sh_media_MEXTR4", SH_BLTIN_UDI },
8156   { CODE_FOR_mextr5,    "__builtin_sh_media_MEXTR5", SH_BLTIN_UDI },
8157   { CODE_FOR_mextr6,    "__builtin_sh_media_MEXTR6", SH_BLTIN_UDI },
8158   { CODE_FOR_mextr7,    "__builtin_sh_media_MEXTR7", SH_BLTIN_UDI },
8159   { CODE_FOR_mmacfx_wl, "__builtin_sh_media_MMACFX_WL", SH_BLTIN_MAC_HISI },
8160   { CODE_FOR_mmacnfx_wl,"__builtin_sh_media_MMACNFX_WL", SH_BLTIN_MAC_HISI },
8161   { CODE_FOR_mulv2si3,  "__builtin_mulv2si3", SH_BLTIN_V2SI3, },
8162   { CODE_FOR_mulv4hi3,  "__builtin_mulv4hi3", SH_BLTIN_V4HI3 },
8163   { CODE_FOR_mmulfx_l,  "__builtin_sh_media_MMULFX_L", SH_BLTIN_V2SI3 },
8164   { CODE_FOR_mmulfx_w,  "__builtin_sh_media_MMULFX_W", SH_BLTIN_V4HI3 },
8165   { CODE_FOR_mmulfxrp_w,"__builtin_sh_media_MMULFXRP_W", SH_BLTIN_V4HI3 },
8166   { CODE_FOR_mmulhi_wl, "__builtin_sh_media_MMULHI_WL", SH_BLTIN_V4HI2V2SI },
8167   { CODE_FOR_mmullo_wl, "__builtin_sh_media_MMULLO_WL", SH_BLTIN_V4HI2V2SI },
8168   { CODE_FOR_mmulsum_wq,"__builtin_sh_media_MMULSUM_WQ", SH_BLTIN_XXUU },
8169   { CODE_FOR_mperm_w,   "__builtin_sh_media_MPERM_W", SH_BLTIN_SH_HI },
8170   { CODE_FOR_msad_ubq,  "__builtin_sh_media_MSAD_UBQ", SH_BLTIN_XXUU },
8171   { CODE_FOR_mshalds_l, "__builtin_sh_media_MSHALDS_L", SH_BLTIN_SH_SI },
8172   { CODE_FOR_mshalds_w, "__builtin_sh_media_MSHALDS_W", SH_BLTIN_SH_HI },
8173   { CODE_FOR_ashrv2si3, "__builtin_ashrv2si3", SH_BLTIN_SH_SI },
8174   { CODE_FOR_ashrv4hi3, "__builtin_ashrv4hi3", SH_BLTIN_SH_HI },
8175   { CODE_FOR_mshards_q, "__builtin_sh_media_MSHARDS_Q", SH_BLTIN_SUS },
8176   { CODE_FOR_mshfhi_b,  "__builtin_sh_media_MSHFHI_B", SH_BLTIN_V8QI3 },
8177   { CODE_FOR_mshfhi_l,  "__builtin_sh_media_MSHFHI_L", SH_BLTIN_V2SI3 },
8178   { CODE_FOR_mshfhi_w,  "__builtin_sh_media_MSHFHI_W", SH_BLTIN_V4HI3 },
8179   { CODE_FOR_mshflo_b,  "__builtin_sh_media_MSHFLO_B", SH_BLTIN_V8QI3 },
8180   { CODE_FOR_mshflo_l,  "__builtin_sh_media_MSHFLO_L", SH_BLTIN_V2SI3 },
8181   { CODE_FOR_mshflo_w,  "__builtin_sh_media_MSHFLO_W", SH_BLTIN_V4HI3 },
8182   { CODE_FOR_ashlv2si3, "__builtin_ashlv2si3", SH_BLTIN_SH_SI },
8183   { CODE_FOR_ashlv4hi3, "__builtin_ashlv4hi3", SH_BLTIN_SH_HI },
8184   { CODE_FOR_lshrv2si3, "__builtin_lshrv2si3", SH_BLTIN_SH_SI },
8185   { CODE_FOR_lshrv4hi3, "__builtin_lshrv4hi3", SH_BLTIN_SH_HI },
8186   { CODE_FOR_subv2si3,  "__builtin_subv2si3", SH_BLTIN_V2SI3 },
8187   { CODE_FOR_subv4hi3,  "__builtin_subv4hi3", SH_BLTIN_V4HI3 },
8188   { CODE_FOR_sssubv2si3,"__builtin_sssubv2si3", SH_BLTIN_V2SI3 },
8189   { CODE_FOR_ussubv8qi3,"__builtin_ussubv8qi3", SH_BLTIN_V8QI3 },
8190   { CODE_FOR_sssubv4hi3,"__builtin_sssubv4hi3", SH_BLTIN_V4HI3 },
8191   { CODE_FOR_fcosa_s,   "__builtin_sh_media_FCOSA_S", SH_BLTIN_SISF },
8192   { CODE_FOR_fsina_s,   "__builtin_sh_media_FSINA_S", SH_BLTIN_SISF },
8193   { CODE_FOR_fipr,      "__builtin_sh_media_FIPR_S", SH_BLTIN_3 },
8194   { CODE_FOR_ftrv,      "__builtin_sh_media_FTRV_S", SH_BLTIN_3 },
8195   { CODE_FOR_fsrra_s,   "__builtin_sh_media_FSRRA_S", SH_BLTIN_2 },
8196 #if 0
8197   { CODE_FOR_ldhi_l,    "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
8198   { CODE_FOR_ldhi_q,    "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
8199   { CODE_FOR_ldlo_l,    "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
8200   { CODE_FOR_ldlo_q,    "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
8201   { CODE_FOR_sthi_l,    "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
8202   { CODE_FOR_sthi_q,    "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
8203   { CODE_FOR_stlo_l,    "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
8204   { CODE_FOR_stlo_q,    "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
8205   { CODE_FOR_ldhi_l64,  "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
8206   { CODE_FOR_ldhi_q64,  "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
8207   { CODE_FOR_ldlo_l64,  "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
8208   { CODE_FOR_ldlo_q64,  "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
8209   { CODE_FOR_sthi_l64,  "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
8210   { CODE_FOR_sthi_q64,  "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
8211   { CODE_FOR_stlo_l64,  "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
8212   { CODE_FOR_stlo_q64,  "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
8213 #endif
8214   { CODE_FOR_nsb,       "__builtin_sh_media_NSB", SH_BLTIN_SU },
8215   { CODE_FOR_byterev,   "__builtin_sh_media_BYTEREV", SH_BLTIN_2 },
8216 #if 0
8217   { CODE_FOR_prefetch32,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV },
8218   { CODE_FOR_prefetch64,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV }
8219 #endif
8220 };
8221
8222 static void
8223 sh_media_init_builtins ()
8224 {
8225   tree shared[SH_BLTIN_NUM_SHARED_SIGNATURES];
8226   const struct builtin_description *d;
8227
8228   memset (shared, 0, sizeof shared);
8229   for (d = bdesc; d - bdesc < (int) (sizeof bdesc / sizeof bdesc[0]); d++)
8230     {
8231       tree type, arg_type;
8232       int signature = d->signature;
8233       int i;
8234
8235       if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES && shared[signature])
8236         type = shared[signature];
8237       else
8238         {
8239           int has_result = signature_args[signature][0] != 0;
8240
8241           if (signature_args[signature][1] == 8
8242               && (insn_data[d->icode].operand[has_result].mode != Pmode))
8243             continue;
8244           if (! TARGET_FPU_ANY
8245               && FLOAT_MODE_P (insn_data[d->icode].operand[0].mode))
8246             continue;
8247           type = void_list_node;
8248           for (i = 3; ; i--)
8249             {
8250               int arg = signature_args[signature][i];
8251               int opno = i - 1 + has_result;
8252
8253               if (arg == 8)
8254                 arg_type = ptr_type_node;
8255               else if (arg)
8256                 arg_type = ((*lang_hooks.types.type_for_mode)
8257                             (insn_data[d->icode].operand[opno].mode,
8258                              (arg & 1)));
8259               else if (i)
8260                 continue;
8261               else
8262                 arg_type = void_type_node;
8263               if (i == 0)
8264                 break;
8265               type = tree_cons (NULL_TREE, arg_type, type);
8266             }
8267           type = build_function_type (arg_type, type);
8268           if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES)
8269             shared[signature] = type;
8270         }
8271       builtin_function (d->name, type, d - bdesc, BUILT_IN_MD,
8272                         NULL, NULL_TREE);
8273     }
8274 }
8275
8276 static void
8277 sh_init_builtins ()
8278 {
8279   if (TARGET_SHMEDIA)
8280     sh_media_init_builtins ();
8281 }
8282
8283 /* Expand an expression EXP that calls a built-in function,
8284    with result going to TARGET if that's convenient
8285    (and in mode MODE if that's convenient).
8286    SUBTARGET may be used as the target for computing one of EXP's operands.
8287    IGNORE is nonzero if the value is to be ignored.  */
8288
8289 static rtx
8290 sh_expand_builtin (exp, target, subtarget, mode, ignore)
8291      tree exp;
8292      rtx target;
8293      rtx subtarget ATTRIBUTE_UNUSED;
8294      enum machine_mode mode ATTRIBUTE_UNUSED;
8295      int ignore;
8296 {
8297   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8298   tree arglist = TREE_OPERAND (exp, 1);
8299   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
8300   const struct builtin_description *d = &bdesc[fcode];
8301   enum insn_code icode = d->icode;
8302   int signature = d->signature;
8303   enum machine_mode tmode = VOIDmode;
8304   int nop = 0, i;
8305   rtx op[4];
8306   rtx pat;
8307
8308   if (signature_args[signature][0])
8309     {
8310       if (ignore)
8311         return 0;
8312
8313       tmode = insn_data[icode].operand[0].mode;
8314       if (! target
8315           || GET_MODE (target) != tmode
8316           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
8317         target = gen_reg_rtx (tmode);
8318       op[nop++] = target;
8319     }
8320   else
8321     target = 0;
8322
8323   for (i = 1; i <= 3; i++, nop++)
8324     {
8325       tree arg;
8326       enum machine_mode opmode, argmode;
8327
8328       if (! signature_args[signature][i])
8329         break;
8330       arg = TREE_VALUE (arglist);
8331       if (arg == error_mark_node)
8332         return const0_rtx;
8333       arglist = TREE_CHAIN (arglist);
8334       opmode = insn_data[icode].operand[nop].mode;
8335       argmode = TYPE_MODE (TREE_TYPE (arg));
8336       if (argmode != opmode)
8337         arg = build1 (NOP_EXPR,
8338                       (*lang_hooks.types.type_for_mode) (opmode, 0), arg);
8339       op[nop] = expand_expr (arg, NULL_RTX, opmode, 0);
8340       if (! (*insn_data[icode].operand[nop].predicate) (op[nop], opmode))
8341         op[nop] = copy_to_mode_reg (opmode, op[nop]);
8342     }
8343
8344   switch (nop)
8345     {
8346     case 1:
8347       pat = (*insn_data[d->icode].genfun) (op[0]);
8348       break;
8349     case 2:
8350       pat = (*insn_data[d->icode].genfun) (op[0], op[1]);
8351       break;
8352     case 3:
8353       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2]);
8354       break;
8355     case 4:
8356       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2], op[3]);
8357       break;
8358     default:
8359       abort ();
8360     }
8361   if (! pat)
8362     return 0;
8363   emit_insn (pat);
8364   return target;
8365 }
8366
8367 void
8368 sh_expand_unop_v2sf (code, op0, op1)
8369      enum rtx_code code;
8370      rtx op0, op1;
8371 {
8372   rtx sel0 = const0_rtx;
8373   rtx sel1 = const1_rtx;
8374   rtx (*fn) PARAMS ((rtx, rtx, rtx, rtx, rtx)) = gen_unary_sf_op;
8375   rtx op = gen_rtx_fmt_e (code, SFmode, op1);
8376
8377   emit_insn ((*fn) (op0, op1, op, sel0, sel0));
8378   emit_insn ((*fn) (op0, op1, op, sel1, sel1));
8379 }
8380
8381 void
8382 sh_expand_binop_v2sf (code, op0, op1, op2)
8383      enum rtx_code code;
8384      rtx op0, op1, op2;
8385 {
8386   rtx sel0 = const0_rtx;
8387   rtx sel1 = const1_rtx;
8388   rtx (*fn) PARAMS ((rtx, rtx, rtx, rtx, rtx, rtx, rtx, rtx))
8389     = gen_binary_sf_op;
8390   rtx op = gen_rtx_fmt_ee (code, SFmode, op1, op2);
8391
8392   emit_insn ((*fn) (op0, op1, op2, op, sel0, sel0, sel0, sel1));
8393   emit_insn ((*fn) (op0, op1, op2, op, sel1, sel1, sel1, sel0));
8394 }
8395
8396 /* Return the class of registers for which a mode change from FROM to TO
8397    is invalid.  */
8398 bool
8399 sh_cannot_change_mode_class (from, to, class)
8400      enum machine_mode from, to;
8401      enum reg_class class;
8402 {
8403   if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
8404     {
8405        if (TARGET_LITTLE_ENDIAN)
8406          {
8407            if (GET_MODE_SIZE (to) < 8 || GET_MODE_SIZE (from) < 8)
8408              return reg_classes_intersect_p (DF_REGS, class);
8409          }
8410        else
8411          {
8412            if (GET_MODE_SIZE (from) < 8)
8413              return reg_classes_intersect_p (DF_HI_REGS, class);
8414          }
8415     }
8416   return 0;
8417 }
8418
8419
8420 /* If ADDRESS refers to a CODE_LABEL, add NUSES to the number of times
8421    that label is used.  */
8422
8423 void
8424 sh_mark_label (address, nuses)
8425      rtx address;
8426      int nuses;
8427 {
8428   if (GOTOFF_P (address))
8429     {
8430       /* Extract the label or symbol.  */
8431       address = XEXP (address, 0);
8432       if (GET_CODE (address) == PLUS)
8433         address = XEXP (address, 0);
8434       address = XVECEXP (address, 0, 0);
8435     }
8436   if (GET_CODE (address) == LABEL_REF
8437       && GET_CODE (XEXP (address, 0)) == CODE_LABEL)
8438     LABEL_NUSES (XEXP (address, 0)) += nuses;
8439 }
8440
8441 /* Compute extra cost of moving data between one register class
8442    and another.  */
8443
8444 /* If SECONDARY*_RELOAD_CLASS says something about the src/dst pair, regclass
8445    uses this information.  Hence, the general register <-> floating point
8446    register information here is not used for SFmode.  */
8447
8448 int
8449 sh_register_move_cost (mode, srcclass, dstclass)
8450      enum machine_mode mode;
8451      enum reg_class srcclass, dstclass;
8452 {
8453   if (dstclass == T_REGS || dstclass == PR_REGS)
8454     return 10;
8455
8456   if (dstclass == MAC_REGS && srcclass == MAC_REGS)
8457     return 4;
8458
8459   if (mode == SImode && ! TARGET_SHMEDIA && TARGET_FMOVD
8460       && REGCLASS_HAS_FP_REG (srcclass)
8461       && REGCLASS_HAS_FP_REG (dstclass))
8462     return 4;
8463
8464   if ((REGCLASS_HAS_FP_REG (dstclass) && srcclass == MAC_REGS)
8465       || (dstclass== MAC_REGS && REGCLASS_HAS_FP_REG (srcclass)))
8466     return 9;
8467
8468   if ((REGCLASS_HAS_FP_REG (dstclass)
8469        && REGCLASS_HAS_GENERAL_REG (srcclass))
8470       || (REGCLASS_HAS_GENERAL_REG (dstclass)
8471           && REGCLASS_HAS_FP_REG (srcclass)))
8472    return ((TARGET_SHMEDIA ? 4 : TARGET_FMOVD ? 8 : 12)
8473            * ((GET_MODE_SIZE (mode) + 7) / 8U));
8474
8475   if ((dstclass == FPUL_REGS
8476        && REGCLASS_HAS_GENERAL_REG (srcclass))
8477       || (srcclass == FPUL_REGS
8478           && REGCLASS_HAS_GENERAL_REG (dstclass)))
8479     return 5;
8480
8481   if ((dstclass == FPUL_REGS
8482        && (srcclass == PR_REGS || srcclass == MAC_REGS || srcclass == T_REGS))
8483       || (srcclass == FPUL_REGS         
8484           && (dstclass == PR_REGS || dstclass == MAC_REGS)))
8485     return 7;
8486
8487   if ((srcclass == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
8488       || ((dstclass) == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
8489     return 20;
8490
8491   if ((srcclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
8492       || (dstclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
8493   return 4;
8494
8495   if (TARGET_SHMEDIA
8496       || (TARGET_FMOVD
8497           && ! REGCLASS_HAS_GENERAL_REG (srcclass)
8498           && ! REGCLASS_HAS_GENERAL_REG (dstclass)))
8499     return 2 * ((GET_MODE_SIZE (mode) + 7) / 8U);
8500
8501   return 2 * ((GET_MODE_SIZE (mode) + 3) / 4U);
8502 }
8503
8504 /* Like register_operand, but take into account that SHMEDIA can use
8505    the constant zero like a general register.  */
8506 int
8507 sh_register_operand (op, mode)
8508      rtx op;
8509      enum machine_mode mode;
8510 {
8511   if (op == CONST0_RTX (mode) && TARGET_SHMEDIA)
8512     return 1;
8513   return register_operand (op, mode);
8514 }
8515
8516 static rtx emit_load_ptr PARAMS ((rtx, rtx));
8517
8518 static rtx
8519 emit_load_ptr (reg, addr)
8520      rtx reg, addr;
8521 {
8522   rtx mem = gen_rtx_MEM (ptr_mode, addr);
8523
8524   if (Pmode != ptr_mode)
8525     mem = gen_rtx_SIGN_EXTEND (Pmode, mem);
8526   return emit_move_insn (reg, mem);
8527 }
8528
8529 void
8530 sh_output_mi_thunk (file, thunk_fndecl, delta, vcall_offset, function)
8531      FILE *file;
8532      tree thunk_fndecl ATTRIBUTE_UNUSED;
8533      HOST_WIDE_INT delta;
8534      HOST_WIDE_INT vcall_offset;
8535      tree function;
8536 {
8537   CUMULATIVE_ARGS cum;
8538   int structure_value_byref = 0;
8539   rtx this, this_value, sibcall, insns, funexp;
8540   tree funtype = TREE_TYPE (function);
8541   int simple_add = CONST_OK_FOR_ADD (delta);
8542   int did_load = 0;
8543   rtx scratch0, scratch1, scratch2;
8544
8545   reload_completed = 1;
8546   epilogue_completed = 1;
8547   no_new_pseudos = 1;
8548   current_function_uses_only_leaf_regs = 1;
8549
8550   emit_note (NOTE_INSN_PROLOGUE_END);
8551
8552   /* Find the "this" pointer.  We have such a wide range of ABIs for the
8553      SH that it's best to do this completely machine independently.
8554      "this" is passed as first argument, unless a structure return pointer 
8555      comes first, in which case "this" comes second.  */
8556   INIT_CUMULATIVE_ARGS (cum, funtype, NULL_RTX, 0);
8557 #ifndef PCC_STATIC_STRUCT_RETURN
8558   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function))))
8559     structure_value_byref = 1;
8560 #endif /* not PCC_STATIC_STRUCT_RETURN */
8561   if (structure_value_byref && struct_value_rtx == 0)
8562     { 
8563       tree ptype = build_pointer_type (TREE_TYPE (funtype));
8564
8565       FUNCTION_ARG_ADVANCE (cum, Pmode, ptype, 1);
8566     }
8567   this = FUNCTION_ARG (cum, Pmode, ptr_type_node, 1);
8568
8569   /* For SHcompact, we only have r0 for a scratch register: r1 is the
8570      static chain pointer (even if you can't have nested virtual functions
8571      right now, someone might implement them sometime), and the rest of the
8572      registers are used for argument passing, are callee-saved, or reserved.  */
8573   scratch0 = scratch1 = scratch2 = gen_rtx_REG (Pmode, 0);
8574   if (! TARGET_SH5)
8575     {
8576       scratch1 = gen_rtx_REG (ptr_mode, 1);
8577       /* N.B., if not TARGET_HITACHI, register 2 is used to pass the pointer
8578          pointing where to return struct values.  */
8579       scratch2 = gen_rtx_REG (Pmode, 3);
8580     }
8581   else if (TARGET_SHMEDIA)
8582     {
8583       scratch1 = gen_rtx_REG (ptr_mode, 21);
8584       scratch2 = gen_rtx_REG (Pmode, TR0_REG);
8585     }
8586
8587   this_value = plus_constant (this, delta);
8588   if (vcall_offset
8589       && (simple_add || scratch0 != scratch1)
8590       && strict_memory_address_p (ptr_mode, this_value))
8591     {
8592       emit_load_ptr (scratch0, this_value);
8593       did_load = 1;
8594     }
8595
8596   if (!delta)
8597     ; /* Do nothing.  */
8598   else if (simple_add)
8599     emit_move_insn (this, this_value);
8600   else
8601     {
8602       emit_move_insn (scratch1, GEN_INT (delta));
8603       emit_insn (gen_add2_insn (this, scratch1));
8604     }
8605
8606   if (vcall_offset)
8607     {
8608       rtx offset_addr;
8609
8610       if (!did_load)
8611         emit_load_ptr (scratch0, this);
8612
8613       offset_addr = plus_constant (scratch0, vcall_offset);
8614       if (strict_memory_address_p (ptr_mode, offset_addr))
8615         ; /* Do nothing.  */
8616       else if (! TARGET_SH5)
8617         {
8618           /* scratch0 != scratch1, and we have indexed loads.  Get better
8619              schedule by loading the offset into r1 and using an indexed
8620              load - then the load of r1 can issue before the load from
8621              (this + delta) finishes.  */
8622           emit_move_insn (scratch1, GEN_INT (vcall_offset));
8623           offset_addr = gen_rtx_PLUS (Pmode, scratch0, scratch1);
8624         }
8625       else if (CONST_OK_FOR_ADD (vcall_offset))
8626         {
8627           emit_insn (gen_add2_insn (scratch0, GEN_INT (vcall_offset)));
8628           offset_addr = scratch0;
8629         }
8630       else if (scratch0 != scratch1)
8631         {
8632           emit_move_insn (scratch1, GEN_INT (vcall_offset));
8633           emit_insn (gen_add2_insn (scratch0, scratch1));
8634           offset_addr = scratch0;
8635         }
8636       else
8637         abort (); /* FIXME */
8638       emit_load_ptr (scratch0, offset_addr);
8639
8640      if (Pmode != ptr_mode)
8641         scratch0 = gen_rtx_TRUNCATE (ptr_mode, scratch0);
8642       emit_insn (gen_add2_insn (this, scratch0));
8643     }
8644
8645   /* Generate a tail call to the target function.  */
8646   if (! TREE_USED (function))
8647     {
8648       assemble_external (function);
8649       TREE_USED (function) = 1;
8650     }
8651   funexp = XEXP (DECL_RTL (function), 0);
8652   emit_move_insn (scratch2, funexp);
8653   funexp = gen_rtx_MEM (FUNCTION_MODE, scratch2);
8654   sibcall = emit_call_insn (gen_sibcall (funexp, const0_rtx, NULL_RTX));
8655   SIBLING_CALL_P (sibcall) = 1;
8656   use_reg (&CALL_INSN_FUNCTION_USAGE (sibcall), this);
8657   emit_barrier ();
8658
8659   /* Run just enough of rest_of_compilation to do scheduling and get
8660      the insns emitted.  Note that use_thunk calls
8661      assemble_start_function and assemble_end_function.  */
8662
8663   insn_locators_initialize ();
8664   insns = get_insns ();
8665
8666   if (optimize > 0 && flag_schedule_insns_after_reload)
8667     {
8668
8669       find_basic_blocks (insns, max_reg_num (), rtl_dump_file);
8670       life_analysis (insns, rtl_dump_file, PROP_FINAL);
8671
8672       split_all_insns (1);
8673
8674       schedule_insns (rtl_dump_file);
8675     }
8676
8677   sh_reorg ();
8678
8679   if (optimize > 0 && flag_delayed_branch)
8680       dbr_schedule (insns, rtl_dump_file);
8681   shorten_branches (insns);
8682   final_start_function (insns, file, 1);
8683   final (insns, file, 1, 0);
8684   final_end_function ();
8685
8686   if (optimize > 0 && flag_schedule_insns_after_reload)
8687     {
8688       /* Release all memory allocated by flow.  */
8689       free_basic_block_vars (0);
8690
8691       /* Release all memory held by regsets now.  */
8692       regset_release_memory ();
8693     }
8694
8695   reload_completed = 0;
8696   epilogue_completed = 0;
8697   no_new_pseudos = 0;
8698 }
8699
8700 rtx
8701 function_symbol (const char *name)
8702 {
8703   rtx sym = gen_rtx_SYMBOL_REF (Pmode, name);
8704   SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_FUNCTION;
8705   return sym;
8706 }
8707
8708 #include "gt-sh.h"