]> gcc.gnu.org Git - gcc.git/blob - gcc/config/sh/sh.c
sh.c (calc_live_regs, [...]): Use of PR_MEDIA_REG / PR_REG depends on TARGET_SHMEDIA...
[gcc.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Hitachi / SuperH SH.
2 Copyright (C) 1993, 1994, 1995, 1997, 1997, 1998, 1999, 2000, 2001, 2002
3 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 "insn-config.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "expr.h"
31 #include "optabs.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "toplev.h"
38 #include "recog.h"
39 #include "c-pragma.h"
40 #include "integrate.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44
45 int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
46
47 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
48 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
49
50 /* These are some macros to abstract register modes. */
51 #define CONST_OK_FOR_ADD(size) \
52 (TARGET_SHMEDIA ? CONST_OK_FOR_P (size) : CONST_OK_FOR_I (size))
53 #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
54 #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
55 #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
56
57 /* Set to 1 by expand_prologue() when the function is an interrupt handler. */
58 int current_function_interrupt;
59
60 /* ??? The pragma interrupt support will not work for SH3. */
61 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
62 output code for the next function appropriate for an interrupt handler. */
63 int pragma_interrupt;
64
65 /* This is set by the trap_exit attribute for functions. It specifies
66 a trap number to be used in a trapa instruction at function exit
67 (instead of an rte instruction). */
68 int trap_exit;
69
70 /* This is used by the sp_switch attribute for functions. It specifies
71 a variable holding the address of the stack the interrupt function
72 should switch to/from at entry/exit. */
73 rtx sp_switch;
74
75 /* This is set by #pragma trapa, and is similar to the above, except that
76 the compiler doesn't emit code to preserve all registers. */
77 static int pragma_trapa;
78
79 /* This is set by #pragma nosave_low_regs. This is useful on the SH3,
80 which has a separate set of low regs for User and Supervisor modes.
81 This should only be used for the lowest level of interrupts. Higher levels
82 of interrupts must save the registers in case they themselves are
83 interrupted. */
84 int pragma_nosave_low_regs;
85
86 /* This is used for communication between SETUP_INCOMING_VARARGS and
87 sh_expand_prologue. */
88 int current_function_anonymous_args;
89
90 /* Global variables for machine-dependent things. */
91
92 /* Which cpu are we scheduling for. */
93 enum processor_type sh_cpu;
94
95 /* Saved operands from the last compare to use when we generate an scc
96 or bcc insn. */
97
98 rtx sh_compare_op0;
99 rtx sh_compare_op1;
100
101 /* Provides the class number of the smallest class containing
102 reg number. */
103
104 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
105 {
106 R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
107 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
108 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
109 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
110 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
111 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
112 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
113 GENERAL_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 FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
123 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
124 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
125 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
126 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
127 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
128 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
129 FP_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 TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
139 TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
140 DF_REGS, DF_REGS, DF_REGS, DF_REGS,
141 DF_REGS, DF_REGS, DF_REGS, DF_REGS,
142 NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
143 MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
144 GENERAL_REGS,
145 };
146
147 char sh_register_names[FIRST_PSEUDO_REGISTER] \
148 [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
149
150 char sh_additional_register_names[ADDREGNAMES_SIZE] \
151 [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
152 = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
153
154 /* Provide reg_class from a letter such as appears in the machine
155 description. */
156
157 const enum reg_class reg_class_from_letter[] =
158 {
159 /* a */ ALL_REGS, /* b */ TARGET_REGS, /* c */ FPSCR_REGS, /* d */ DF_REGS,
160 /* e */ NO_REGS, /* f */ FP_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
161 /* i */ NO_REGS, /* j */ NO_REGS, /* k */ SIBCALL_REGS, /* l */ PR_REGS,
162 /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
163 /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
164 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
165 /* y */ FPUL_REGS, /* z */ R0_REGS
166 };
167
168 int assembler_dialect;
169
170 static void split_branches PARAMS ((rtx));
171 static int branch_dest PARAMS ((rtx));
172 static void force_into PARAMS ((rtx, rtx));
173 static void print_slot PARAMS ((rtx));
174 static rtx add_constant PARAMS ((rtx, enum machine_mode, rtx));
175 static void dump_table PARAMS ((rtx));
176 static int hi_const PARAMS ((rtx));
177 static int broken_move PARAMS ((rtx));
178 static int mova_p PARAMS ((rtx));
179 static rtx find_barrier PARAMS ((int, rtx, rtx));
180 static int noncall_uses_reg PARAMS ((rtx, rtx, rtx *));
181 static rtx gen_block_redirect PARAMS ((rtx, int, int));
182 static void output_stack_adjust PARAMS ((int, rtx, int));
183 static void push PARAMS ((int));
184 static void pop PARAMS ((int));
185 static void push_regs PARAMS ((HOST_WIDE_INT *));
186 static void calc_live_regs PARAMS ((int *, HOST_WIDE_INT *));
187 static void mark_use PARAMS ((rtx, rtx *));
188 static HOST_WIDE_INT rounded_frame_size PARAMS ((int));
189 static rtx mark_constant_pool_use PARAMS ((rtx));
190 const struct attribute_spec sh_attribute_table[];
191 static tree sh_handle_interrupt_handler_attribute PARAMS ((tree *, tree, tree, int, bool *));
192 static tree sh_handle_sp_switch_attribute PARAMS ((tree *, tree, tree, int, bool *));
193 static tree sh_handle_trap_exit_attribute PARAMS ((tree *, tree, tree, int, bool *));
194 static void sh_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
195 static void sh_insert_attributes PARAMS ((tree, tree *));
196 #ifndef OBJECT_FORMAT_ELF
197 static void sh_asm_named_section PARAMS ((const char *, unsigned int));
198 #endif
199 static int sh_adjust_cost PARAMS ((rtx, rtx, rtx, int));
200 static bool sh_cannot_modify_jumps_p PARAMS ((void));
201
202 static bool sh_ms_bitfield_layout_p PARAMS ((tree));
203 \f
204 /* Initialize the GCC target structure. */
205 #undef TARGET_ATTRIBUTE_TABLE
206 #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
207
208 /* The next two are used for debug info when compiling with -gdwarf. */
209 #undef TARGET_ASM_UNALIGNED_HI_OP
210 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
211 #undef TARGET_ASM_UNALIGNED_SI_OP
212 #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
213
214 /* These are NULLed out on non-SH5 in OVERRIDE_OPTIONS. */
215 #undef TARGET_ASM_UNALIGNED_DI_OP
216 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
217 #undef TARGET_ASM_ALIGNED_DI_OP
218 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
219
220 #undef TARGET_ASM_FUNCTION_EPILOGUE
221 #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
222
223 #undef TARGET_INSERT_ATTRIBUTES
224 #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
225
226 #undef TARGET_SCHED_ADJUST_COST
227 #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
228
229 #undef TARGET_CANNOT_MODIFY_JUMPS_P
230 #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
231
232 #undef TARGET_MS_BITFIELD_LAYOUT_P
233 #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
234
235 struct gcc_target targetm = TARGET_INITIALIZER;
236 \f
237 /* Print the operand address in x to the stream. */
238
239 void
240 print_operand_address (stream, x)
241 FILE *stream;
242 rtx x;
243 {
244 switch (GET_CODE (x))
245 {
246 case REG:
247 case SUBREG:
248 fprintf (stream, "@%s", reg_names[true_regnum (x)]);
249 break;
250
251 case PLUS:
252 {
253 rtx base = XEXP (x, 0);
254 rtx index = XEXP (x, 1);
255
256 switch (GET_CODE (index))
257 {
258 case CONST_INT:
259 fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
260 reg_names[true_regnum (base)]);
261 break;
262
263 case REG:
264 case SUBREG:
265 {
266 int base_num = true_regnum (base);
267 int index_num = true_regnum (index);
268
269 fprintf (stream, "@(r0,%s)",
270 reg_names[MAX (base_num, index_num)]);
271 break;
272 }
273
274 default:
275 debug_rtx (x);
276 abort ();
277 }
278 }
279 break;
280
281 case PRE_DEC:
282 fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
283 break;
284
285 case POST_INC:
286 fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
287 break;
288
289 default:
290 x = mark_constant_pool_use (x);
291 output_addr_const (stream, x);
292 break;
293 }
294 }
295
296 /* Print operand x (an rtx) in assembler syntax to file stream
297 according to modifier code.
298
299 '.' print a .s if insn needs delay slot
300 ',' print LOCAL_LABEL_PREFIX
301 '@' print trap, rte or rts depending upon pragma interruptness
302 '#' output a nop if there is nothing to put in the delay slot
303 'O' print a constant without the #
304 'R' print the LSW of a dp value - changes if in little endian
305 'S' print the MSW of a dp value - changes if in little endian
306 'T' print the next word of a dp value - same as 'R' in big endian mode.
307 'M' print an `x' if `m' will print `base,index'.
308 'm' print a pair `base,offset' or `base,index', for LD and ST.
309 'u' prints the lowest 16 bits of CONST_INT, as an unsigned value.
310 'o' output an operator. */
311
312 void
313 print_operand (stream, x, code)
314 FILE *stream;
315 rtx x;
316 int code;
317 {
318 switch (code)
319 {
320 case '.':
321 if (final_sequence
322 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
323 fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
324 break;
325 case ',':
326 fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
327 break;
328 case '@':
329 {
330 int interrupt_handler;
331
332 if ((lookup_attribute
333 ("interrupt_handler",
334 DECL_ATTRIBUTES (current_function_decl)))
335 != NULL_TREE)
336 interrupt_handler = 1;
337 else
338 interrupt_handler = 0;
339
340 if (trap_exit)
341 fprintf (stream, "trapa #%d", trap_exit);
342 else if (interrupt_handler)
343 fprintf (stream, "rte");
344 else
345 fprintf (stream, "rts");
346 break;
347 }
348 case '#':
349 /* Output a nop if there's nothing in the delay slot. */
350 if (dbr_sequence_length () == 0)
351 fprintf (stream, "\n\tnop");
352 break;
353 case 'O':
354 x = mark_constant_pool_use (x);
355 output_addr_const (stream, x);
356 break;
357 case 'R':
358 fputs (reg_names[REGNO (x) + LSW], (stream));
359 break;
360 case 'S':
361 fputs (reg_names[REGNO (x) + MSW], (stream));
362 break;
363 case 'T':
364 /* Next word of a double. */
365 switch (GET_CODE (x))
366 {
367 case REG:
368 fputs (reg_names[REGNO (x) + 1], (stream));
369 break;
370 case MEM:
371 if (GET_CODE (XEXP (x, 0)) != PRE_DEC
372 && GET_CODE (XEXP (x, 0)) != POST_INC)
373 x = adjust_address (x, SImode, 4);
374 print_operand_address (stream, XEXP (x, 0));
375 break;
376 default:
377 break;
378 }
379 break;
380 case 'o':
381 switch (GET_CODE (x))
382 {
383 case PLUS: fputs ("add", stream); break;
384 case MINUS: fputs ("sub", stream); break;
385 case MULT: fputs ("mul", stream); break;
386 case DIV: fputs ("div", stream); break;
387 default:
388 break;
389 }
390 break;
391 case 'M':
392 if (GET_CODE (x) == MEM
393 && GET_CODE (XEXP (x, 0)) == PLUS
394 && (GET_CODE (XEXP (XEXP (x, 0), 1)) == REG
395 || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
396 fputc ('x', stream);
397 break;
398
399 case 'm':
400 if (GET_CODE (x) != MEM)
401 abort ();
402 x = XEXP (x, 0);
403 switch (GET_CODE (x))
404 {
405 case REG:
406 case SUBREG:
407 print_operand (stream, x, 0);
408 fputs (", 0", stream);
409 break;
410
411 case PLUS:
412 print_operand (stream, XEXP (x, 0), 0);
413 fputs (", ", stream);
414 print_operand (stream, XEXP (x, 1), 0);
415 break;
416
417 default:
418 abort ();
419 }
420 break;
421
422 case 'u':
423 if (GET_CODE (x) == CONST_INT)
424 {
425 fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
426 break;
427 }
428 /* Fall through. */
429
430 default:
431 switch (GET_CODE (x))
432 {
433 /* FIXME: We need this on SHmedia32 because reload generates
434 some sign-extended HI or QI loads into DImode registers
435 but, because Pmode is SImode, the address ends up with a
436 subreg:SI of the DImode register. Maybe reload should be
437 fixed so as to apply alter_subreg to such loads? */
438 case SUBREG:
439 if (SUBREG_BYTE (x) != 0
440 || GET_CODE (SUBREG_REG (x)) != REG)
441 abort ();
442
443 x = SUBREG_REG (x);
444 /* Fall through. */
445
446 case REG:
447 if (FP_REGISTER_P (REGNO (x))
448 && GET_MODE (x) == V16SFmode)
449 fprintf ((stream), "mtrx%s", reg_names[REGNO (x)] + 2);
450 else if (FP_REGISTER_P (REGNO (x))
451 && GET_MODE (x) == V4SFmode)
452 fprintf ((stream), "fv%s", reg_names[REGNO (x)] + 2);
453 else if (GET_CODE (x) == REG
454 && GET_MODE (x) == V2SFmode)
455 fprintf ((stream), "fp%s", reg_names[REGNO (x)] + 2);
456 else if (FP_REGISTER_P (REGNO (x))
457 && GET_MODE_SIZE (GET_MODE (x)) > 4)
458 fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
459 else
460 fputs (reg_names[REGNO (x)], (stream));
461 break;
462
463 case MEM:
464 output_address (XEXP (x, 0));
465 break;
466
467 case CONST:
468 if (TARGET_SHMEDIA
469 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
470 && GET_MODE (XEXP (x, 0)) == DImode
471 && GET_CODE (XEXP (XEXP (x, 0), 0)) == TRUNCATE
472 && GET_MODE (XEXP (XEXP (x, 0), 0)) == HImode)
473 {
474 rtx val = XEXP (XEXP (XEXP (x, 0), 0), 0);
475
476 fputc ('(', stream);
477 if (GET_CODE (val) == ASHIFTRT)
478 {
479 fputc ('(', stream);
480 if (GET_CODE (XEXP (val, 0)) == CONST)
481 fputc ('(', stream);
482 output_addr_const (stream, XEXP (val, 0));
483 if (GET_CODE (XEXP (val, 0)) == CONST)
484 fputc (')', stream);
485 fputs (" >> ", stream);
486 output_addr_const (stream, XEXP (val, 1));
487 fputc (')', stream);
488 }
489 else
490 {
491 if (GET_CODE (val) == CONST)
492 fputc ('(', stream);
493 output_addr_const (stream, val);
494 if (GET_CODE (val) == CONST)
495 fputc (')', stream);
496 }
497 fputs (" & 65535)", stream);
498 break;
499 }
500
501 /* Fall through. */
502 default:
503 if (TARGET_SH1)
504 fputc ('#', stream);
505 output_addr_const (stream, x);
506 break;
507 }
508 break;
509 }
510 }
511 \f
512 /* Like force_operand, but guarantees that VALUE ends up in TARGET. */
513 static void
514 force_into (value, target)
515 rtx value, target;
516 {
517 value = force_operand (value, target);
518 if (! rtx_equal_p (value, target))
519 emit_insn (gen_move_insn (target, value));
520 }
521
522 /* Emit code to perform a block move. Choose the best method.
523
524 OPERANDS[0] is the destination.
525 OPERANDS[1] is the source.
526 OPERANDS[2] is the size.
527 OPERANDS[3] is the alignment safe to use. */
528
529 int
530 expand_block_move (operands)
531 rtx *operands;
532 {
533 int align = INTVAL (operands[3]);
534 int constp = (GET_CODE (operands[2]) == CONST_INT);
535 int bytes = (constp ? INTVAL (operands[2]) : 0);
536
537 /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
538 alignment, or if it isn't a multiple of 4 bytes, then fail. */
539 if (! constp || align < 4 || (bytes % 4 != 0))
540 return 0;
541
542 if (TARGET_HARD_SH4)
543 {
544 if (bytes < 12)
545 return 0;
546 else if (bytes == 12)
547 {
548 tree entry_name;
549 rtx sym;
550 rtx func_addr_rtx;
551 rtx r4 = gen_rtx (REG, SImode, 4);
552 rtx r5 = gen_rtx (REG, SImode, 5);
553
554 entry_name = get_identifier ("__movstrSI12_i4");
555
556 sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
557 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
558 force_into (XEXP (operands[0], 0), r4);
559 force_into (XEXP (operands[1], 0), r5);
560 emit_insn (gen_block_move_real_i4 (func_addr_rtx));
561 return 1;
562 }
563 else if (! TARGET_SMALLCODE)
564 {
565 tree entry_name;
566 rtx sym;
567 rtx func_addr_rtx;
568 int dwords;
569 rtx r4 = gen_rtx (REG, SImode, 4);
570 rtx r5 = gen_rtx (REG, SImode, 5);
571 rtx r6 = gen_rtx (REG, SImode, 6);
572
573 entry_name = get_identifier (bytes & 4
574 ? "__movstr_i4_odd"
575 : "__movstr_i4_even");
576 sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
577 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
578 force_into (XEXP (operands[0], 0), r4);
579 force_into (XEXP (operands[1], 0), r5);
580
581 dwords = bytes >> 3;
582 emit_insn (gen_move_insn (r6, GEN_INT (dwords - 1)));
583 emit_insn (gen_block_lump_real_i4 (func_addr_rtx));
584 return 1;
585 }
586 else
587 return 0;
588 }
589 if (bytes < 64)
590 {
591 char entry[30];
592 tree entry_name;
593 rtx sym;
594 rtx func_addr_rtx;
595 rtx r4 = gen_rtx_REG (SImode, 4);
596 rtx r5 = gen_rtx_REG (SImode, 5);
597
598 sprintf (entry, "__movstrSI%d", bytes);
599 entry_name = get_identifier (entry);
600 sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
601 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
602 force_into (XEXP (operands[0], 0), r4);
603 force_into (XEXP (operands[1], 0), r5);
604 emit_insn (gen_block_move_real (func_addr_rtx));
605 return 1;
606 }
607
608 /* This is the same number of bytes as a memcpy call, but to a different
609 less common function name, so this will occasionally use more space. */
610 if (! TARGET_SMALLCODE)
611 {
612 tree entry_name;
613 rtx sym;
614 rtx func_addr_rtx;
615 int final_switch, while_loop;
616 rtx r4 = gen_rtx_REG (SImode, 4);
617 rtx r5 = gen_rtx_REG (SImode, 5);
618 rtx r6 = gen_rtx_REG (SImode, 6);
619
620 entry_name = get_identifier ("__movstr");
621 sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
622 func_addr_rtx = copy_to_mode_reg (Pmode, sym);
623 force_into (XEXP (operands[0], 0), r4);
624 force_into (XEXP (operands[1], 0), r5);
625
626 /* r6 controls the size of the move. 16 is decremented from it
627 for each 64 bytes moved. Then the negative bit left over is used
628 as an index into a list of move instructions. e.g., a 72 byte move
629 would be set up with size(r6) = 14, for one iteration through the
630 big while loop, and a switch of -2 for the last part. */
631
632 final_switch = 16 - ((bytes / 4) % 16);
633 while_loop = ((bytes / 4) / 16 - 1) * 16;
634 emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
635 emit_insn (gen_block_lump_real (func_addr_rtx));
636 return 1;
637 }
638
639 return 0;
640 }
641
642 /* Prepare operands for a move define_expand; specifically, one of the
643 operands must be in a register. */
644
645 int
646 prepare_move_operands (operands, mode)
647 rtx operands[];
648 enum machine_mode mode;
649 {
650 if ((mode == SImode || mode == DImode) && flag_pic)
651 {
652 rtx temp;
653 if (SYMBOLIC_CONST_P (operands[1]))
654 {
655 if (GET_CODE (operands[0]) == MEM)
656 operands[1] = force_reg (Pmode, operands[1]);
657 else if (GET_CODE (operands[1]) == LABEL_REF
658 && target_reg_operand (operands[0], mode))
659 /* It's ok. */;
660 else
661 {
662 temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
663 operands[1] = legitimize_pic_address (operands[1], mode, temp);
664 }
665 }
666 else if (GET_CODE (operands[1]) == CONST
667 && GET_CODE (XEXP (operands[1], 0)) == PLUS
668 && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
669 {
670 temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
671 temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
672 mode, temp);
673 operands[1] = expand_binop (mode, add_optab, temp,
674 XEXP (XEXP (operands[1], 0), 1),
675 no_new_pseudos ? temp
676 : gen_reg_rtx (Pmode),
677 0, OPTAB_LIB_WIDEN);
678 }
679 }
680
681 if (! reload_in_progress && ! reload_completed)
682 {
683 /* Copy the source to a register if both operands aren't registers. */
684 if (! register_operand (operands[0], mode)
685 && ! register_operand (operands[1], mode))
686 operands[1] = copy_to_mode_reg (mode, operands[1]);
687
688 /* This case can happen while generating code to move the result
689 of a library call to the target. Reject `st r0,@(rX,rY)' because
690 reload will fail to find a spill register for rX, since r0 is already
691 being used for the source. */
692 else if (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 0
693 && GET_CODE (operands[0]) == MEM
694 && GET_CODE (XEXP (operands[0], 0)) == PLUS
695 && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
696 operands[1] = copy_to_mode_reg (mode, operands[1]);
697 }
698
699 return 0;
700 }
701
702 /* Prepare the operands for an scc instruction; make sure that the
703 compare has been done. */
704 rtx
705 prepare_scc_operands (code)
706 enum rtx_code code;
707 {
708 rtx t_reg = gen_rtx_REG (SImode, T_REG);
709 enum rtx_code oldcode = code;
710 enum machine_mode mode;
711
712 /* First need a compare insn. */
713 switch (code)
714 {
715 case NE:
716 /* It isn't possible to handle this case. */
717 abort ();
718 case LT:
719 code = GT;
720 break;
721 case LE:
722 code = GE;
723 break;
724 case LTU:
725 code = GTU;
726 break;
727 case LEU:
728 code = GEU;
729 break;
730 default:
731 break;
732 }
733 if (code != oldcode)
734 {
735 rtx tmp = sh_compare_op0;
736 sh_compare_op0 = sh_compare_op1;
737 sh_compare_op1 = tmp;
738 }
739
740 mode = GET_MODE (sh_compare_op0);
741 if (mode == VOIDmode)
742 mode = GET_MODE (sh_compare_op1);
743
744 sh_compare_op0 = force_reg (mode, sh_compare_op0);
745 if ((code != EQ && code != NE
746 && (sh_compare_op1 != const0_rtx
747 || code == GTU || code == GEU || code == LTU || code == LEU))
748 || (mode == DImode && sh_compare_op1 != const0_rtx)
749 || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
750 sh_compare_op1 = force_reg (mode, sh_compare_op1);
751
752 if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
753 (mode == SFmode ? emit_sf_insn : emit_df_insn)
754 (gen_rtx (PARALLEL, VOIDmode, gen_rtvec (2,
755 gen_rtx (SET, VOIDmode, t_reg,
756 gen_rtx (code, SImode,
757 sh_compare_op0, sh_compare_op1)),
758 gen_rtx (USE, VOIDmode, get_fpscr_rtx ()))));
759 else
760 emit_insn (gen_rtx (SET, VOIDmode, t_reg,
761 gen_rtx (code, SImode, sh_compare_op0,
762 sh_compare_op1)));
763
764 return t_reg;
765 }
766
767 /* Called from the md file, set up the operands of a compare instruction. */
768
769 void
770 from_compare (operands, code)
771 rtx *operands;
772 int code;
773 {
774 enum machine_mode mode = GET_MODE (sh_compare_op0);
775 rtx insn;
776 if (mode == VOIDmode)
777 mode = GET_MODE (sh_compare_op1);
778 if (code != EQ
779 || mode == DImode
780 || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
781 {
782 /* Force args into regs, since we can't use constants here. */
783 sh_compare_op0 = force_reg (mode, sh_compare_op0);
784 if (sh_compare_op1 != const0_rtx
785 || code == GTU || code == GEU
786 || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
787 sh_compare_op1 = force_reg (mode, sh_compare_op1);
788 }
789 if (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT && code == GE)
790 {
791 from_compare (operands, GT);
792 insn = gen_ieee_ccmpeqsf_t (sh_compare_op0, sh_compare_op1);
793 }
794 else
795 insn = gen_rtx_SET (VOIDmode,
796 gen_rtx_REG (SImode, T_REG),
797 gen_rtx (code, SImode, sh_compare_op0,
798 sh_compare_op1));
799 if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
800 {
801 insn = gen_rtx (PARALLEL, VOIDmode,
802 gen_rtvec (2, insn,
803 gen_rtx (USE, VOIDmode, get_fpscr_rtx ())));
804 (mode == SFmode ? emit_sf_insn : emit_df_insn) (insn);
805 }
806 else
807 emit_insn (insn);
808 }
809 \f
810 /* Functions to output assembly code. */
811
812 /* Return a sequence of instructions to perform DI or DF move.
813
814 Since the SH cannot move a DI or DF in one instruction, we have
815 to take care when we see overlapping source and dest registers. */
816
817 const char *
818 output_movedouble (insn, operands, mode)
819 rtx insn ATTRIBUTE_UNUSED;
820 rtx operands[];
821 enum machine_mode mode;
822 {
823 rtx dst = operands[0];
824 rtx src = operands[1];
825
826 if (GET_CODE (dst) == MEM
827 && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
828 return "mov.l %T1,%0\n\tmov.l %1,%0";
829
830 if (register_operand (dst, mode)
831 && register_operand (src, mode))
832 {
833 if (REGNO (src) == MACH_REG)
834 return "sts mach,%S0\n\tsts macl,%R0";
835
836 /* When mov.d r1,r2 do r2->r3 then r1->r2;
837 when mov.d r1,r0 do r1->r0 then r2->r1. */
838
839 if (REGNO (src) + 1 == REGNO (dst))
840 return "mov %T1,%T0\n\tmov %1,%0";
841 else
842 return "mov %1,%0\n\tmov %T1,%T0";
843 }
844 else if (GET_CODE (src) == CONST_INT)
845 {
846 if (INTVAL (src) < 0)
847 output_asm_insn ("mov #-1,%S0", operands);
848 else
849 output_asm_insn ("mov #0,%S0", operands);
850
851 return "mov %1,%R0";
852 }
853 else if (GET_CODE (src) == MEM)
854 {
855 int ptrreg = -1;
856 int dreg = REGNO (dst);
857 rtx inside = XEXP (src, 0);
858
859 if (GET_CODE (inside) == REG)
860 ptrreg = REGNO (inside);
861 else if (GET_CODE (inside) == SUBREG)
862 ptrreg = subreg_regno (inside);
863 else if (GET_CODE (inside) == PLUS)
864 {
865 ptrreg = REGNO (XEXP (inside, 0));
866 /* ??? A r0+REG address shouldn't be possible here, because it isn't
867 an offsettable address. Unfortunately, offsettable addresses use
868 QImode to check the offset, and a QImode offsettable address
869 requires r0 for the other operand, which is not currently
870 supported, so we can't use the 'o' constraint.
871 Thus we must check for and handle r0+REG addresses here.
872 We punt for now, since this is likely very rare. */
873 if (GET_CODE (XEXP (inside, 1)) == REG)
874 abort ();
875 }
876 else if (GET_CODE (inside) == LABEL_REF)
877 return "mov.l %1,%0\n\tmov.l %1+4,%T0";
878 else if (GET_CODE (inside) == POST_INC)
879 return "mov.l %1,%0\n\tmov.l %1,%T0";
880 else
881 abort ();
882
883 /* Work out the safe way to copy. Copy into the second half first. */
884 if (dreg == ptrreg)
885 return "mov.l %T1,%T0\n\tmov.l %1,%0";
886 }
887
888 return "mov.l %1,%0\n\tmov.l %T1,%T0";
889 }
890
891 /* Print an instruction which would have gone into a delay slot after
892 another instruction, but couldn't because the other instruction expanded
893 into a sequence where putting the slot insn at the end wouldn't work. */
894
895 static void
896 print_slot (insn)
897 rtx insn;
898 {
899 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
900
901 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
902 }
903
904 const char *
905 output_far_jump (insn, op)
906 rtx insn;
907 rtx op;
908 {
909 struct { rtx lab, reg, op; } this;
910 rtx braf_base_lab = NULL_RTX;
911 const char *jump;
912 int far;
913 int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
914
915 this.lab = gen_label_rtx ();
916
917 if (TARGET_SH2
918 && offset >= -32764
919 && offset - get_attr_length (insn) <= 32766)
920 {
921 far = 0;
922 jump = "mov.w %O0,%1; braf %1";
923 }
924 else
925 {
926 far = 1;
927 if (flag_pic)
928 {
929 if (TARGET_SH2)
930 jump = "mov.l %O0,%1; braf %1";
931 else
932 jump = "mov.l r0,@-r15; mova %O0,r0; mov.l @r0,%1; add r0,%1; mov.l @r15+,r0; jmp @%1";
933 }
934 else
935 jump = "mov.l %O0,%1; jmp @%1";
936 }
937 /* If we have a scratch register available, use it. */
938 if (GET_CODE (PREV_INSN (insn)) == INSN
939 && INSN_CODE (PREV_INSN (insn)) == CODE_FOR_indirect_jump_scratch)
940 {
941 this.reg = SET_DEST (PATTERN (PREV_INSN (insn)));
942 if (REGNO (this.reg) == R0_REG && flag_pic && ! TARGET_SH2)
943 jump = "mov.l r1,@-r15; mova %O0,r0; mov.l @r0,r1; add r1,r0; mov.l @r15+,r1; jmp @%1";
944 output_asm_insn (jump, &this.lab);
945 if (dbr_sequence_length ())
946 print_slot (final_sequence);
947 else
948 output_asm_insn ("nop", 0);
949 }
950 else
951 {
952 /* Output the delay slot insn first if any. */
953 if (dbr_sequence_length ())
954 print_slot (final_sequence);
955
956 this.reg = gen_rtx_REG (SImode, 13);
957 /* We must keep the stack aligned to 8-byte boundaries on SH5.
958 Fortunately, MACL is fixed and call-clobbered, and we never
959 need its value across jumps, so save r13 in it instead of in
960 the stack. */
961 if (TARGET_SH5)
962 output_asm_insn ("lds r13, macl", 0);
963 else
964 output_asm_insn ("mov.l r13,@-r15", 0);
965 output_asm_insn (jump, &this.lab);
966 if (TARGET_SH5)
967 output_asm_insn ("sts macl, r13", 0);
968 else
969 output_asm_insn ("mov.l @r15+,r13", 0);
970 }
971 if (far && flag_pic && TARGET_SH2)
972 {
973 braf_base_lab = gen_label_rtx ();
974 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
975 CODE_LABEL_NUMBER (braf_base_lab));
976 }
977 if (far)
978 output_asm_insn (".align 2", 0);
979 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (this.lab));
980 this.op = op;
981 if (far && flag_pic)
982 {
983 if (TARGET_SH2)
984 this.lab = braf_base_lab;
985 output_asm_insn (".long %O2-%O0", &this.lab);
986 }
987 else
988 output_asm_insn (far ? ".long %O2" : ".word %O2-%O0", &this.lab);
989 return "";
990 }
991
992 /* Local label counter, used for constants in the pool and inside
993 pattern branches. */
994
995 static int lf = 100;
996
997 /* Output code for ordinary branches. */
998
999 const char *
1000 output_branch (logic, insn, operands)
1001 int logic;
1002 rtx insn;
1003 rtx *operands;
1004 {
1005 switch (get_attr_length (insn))
1006 {
1007 case 6:
1008 /* This can happen if filling the delay slot has caused a forward
1009 branch to exceed its range (we could reverse it, but only
1010 when we know we won't overextend other branches; this should
1011 best be handled by relaxation).
1012 It can also happen when other condbranches hoist delay slot insn
1013 from their destination, thus leading to code size increase.
1014 But the branch will still be in the range -4092..+4098 bytes. */
1015
1016 if (! TARGET_RELAX)
1017 {
1018 int label = lf++;
1019 /* The call to print_slot will clobber the operands. */
1020 rtx op0 = operands[0];
1021
1022 /* If the instruction in the delay slot is annulled (true), then
1023 there is no delay slot where we can put it now. The only safe
1024 place for it is after the label. final will do that by default. */
1025
1026 if (final_sequence
1027 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1028 {
1029 asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
1030 ASSEMBLER_DIALECT ? "/" : ".", label);
1031 print_slot (final_sequence);
1032 }
1033 else
1034 asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
1035
1036 output_asm_insn ("bra\t%l0", &op0);
1037 fprintf (asm_out_file, "\tnop\n");
1038 ASM_OUTPUT_INTERNAL_LABEL(asm_out_file, "LF", label);
1039
1040 return "";
1041 }
1042 /* When relaxing, handle this like a short branch. The linker
1043 will fix it up if it still doesn't fit after relaxation. */
1044 case 2:
1045 return logic ? "bt%.\t%l0" : "bf%.\t%l0";
1046 default:
1047 /* There should be no longer branches now - that would
1048 indicate that something has destroyed the branches set
1049 up in machine_dependent_reorg. */
1050 abort ();
1051 }
1052 }
1053
1054 const char *
1055 output_branchy_insn (code, template, insn, operands)
1056 enum rtx_code code;
1057 const char *template;
1058 rtx insn;
1059 rtx *operands;
1060 {
1061 rtx next_insn = NEXT_INSN (insn);
1062
1063 if (next_insn && GET_CODE (next_insn) == JUMP_INSN && condjump_p (next_insn))
1064 {
1065 rtx src = SET_SRC (PATTERN (next_insn));
1066 if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
1067 {
1068 /* Following branch not taken */
1069 operands[9] = gen_label_rtx ();
1070 emit_label_after (operands[9], next_insn);
1071 INSN_ADDRESSES_NEW (operands[9],
1072 INSN_ADDRESSES (INSN_UID (next_insn))
1073 + get_attr_length (next_insn));
1074 return template;
1075 }
1076 else
1077 {
1078 int offset = (branch_dest (next_insn)
1079 - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
1080 if (offset >= -252 && offset <= 258)
1081 {
1082 if (GET_CODE (src) == IF_THEN_ELSE)
1083 /* branch_true */
1084 src = XEXP (src, 1);
1085 operands[9] = src;
1086 return template;
1087 }
1088 }
1089 }
1090 operands[9] = gen_label_rtx ();
1091 emit_label_after (operands[9], insn);
1092 INSN_ADDRESSES_NEW (operands[9],
1093 INSN_ADDRESSES (INSN_UID (insn))
1094 + get_attr_length (insn));
1095 return template;
1096 }
1097
1098 const char *
1099 output_ieee_ccmpeq (insn, operands)
1100 rtx insn, *operands;
1101 {
1102 return output_branchy_insn (NE, "bt\t%l9\\;fcmp/eq\t%1,%0", insn, operands);
1103 }
1104 \f
1105 /* Output to FILE the start of the assembler file. */
1106
1107 void
1108 output_file_start (file)
1109 FILE *file;
1110 {
1111 output_file_directive (file, main_input_filename);
1112
1113 /* Switch to the data section so that the coffsem symbol
1114 isn't in the text section. */
1115 data_section ();
1116
1117 if (TARGET_LITTLE_ENDIAN)
1118 fprintf (file, "\t.little\n");
1119
1120 if (TARGET_SHCOMPACT)
1121 fprintf (file, "\t.mode\tSHcompact\n");
1122 else if (TARGET_SHMEDIA)
1123 fprintf (file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
1124 TARGET_SHMEDIA64 ? 64 : 32);
1125 }
1126 \f
1127 /* Actual number of instructions used to make a shift by N. */
1128 static const char ashiftrt_insns[] =
1129 { 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};
1130
1131 /* Left shift and logical right shift are the same. */
1132 static const char shift_insns[] =
1133 { 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};
1134
1135 /* Individual shift amounts needed to get the above length sequences.
1136 One bit right shifts clobber the T bit, so when possible, put one bit
1137 shifts in the middle of the sequence, so the ends are eligible for
1138 branch delay slots. */
1139 static const short shift_amounts[32][5] = {
1140 {0}, {1}, {2}, {2, 1},
1141 {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
1142 {8}, {8, 1}, {8, 2}, {8, 1, 2},
1143 {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
1144 {16}, {16, 1}, {16, 2}, {16, 1, 2},
1145 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1146 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1147 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1148
1149 /* Likewise, but for shift amounts < 16, up to three highmost bits
1150 might be clobbered. This is typically used when combined with some
1151 kind of sign or zero extension. */
1152
1153 static const char ext_shift_insns[] =
1154 { 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};
1155
1156 static const short ext_shift_amounts[32][4] = {
1157 {0}, {1}, {2}, {2, 1},
1158 {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
1159 {8}, {8, 1}, {8, 2}, {8, 1, 2},
1160 {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
1161 {16}, {16, 1}, {16, 2}, {16, 1, 2},
1162 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1163 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1164 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1165
1166 /* Assuming we have a value that has been sign-extended by at least one bit,
1167 can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
1168 to shift it by N without data loss, and quicker than by other means? */
1169 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
1170
1171 /* This is used in length attributes in sh.md to help compute the length
1172 of arbitrary constant shift instructions. */
1173
1174 int
1175 shift_insns_rtx (insn)
1176 rtx insn;
1177 {
1178 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1179 int shift_count = INTVAL (XEXP (set_src, 1));
1180 enum rtx_code shift_code = GET_CODE (set_src);
1181
1182 switch (shift_code)
1183 {
1184 case ASHIFTRT:
1185 return ashiftrt_insns[shift_count];
1186 case LSHIFTRT:
1187 case ASHIFT:
1188 return shift_insns[shift_count];
1189 default:
1190 abort();
1191 }
1192 }
1193
1194 /* Return the cost of a shift. */
1195
1196 int
1197 shiftcosts (x)
1198 rtx x;
1199 {
1200 int value;
1201
1202 if (TARGET_SHMEDIA)
1203 return 1;
1204
1205 if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
1206 {
1207 if (GET_MODE (x) == DImode
1208 && GET_CODE (XEXP (x, 1)) == CONST_INT
1209 && INTVAL (XEXP (x, 1)) == 1)
1210 return 2;
1211
1212 /* Everything else is invalid, because there is no pattern for it. */
1213 return 10000;
1214 }
1215 /* If shift by a non constant, then this will be expensive. */
1216 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1217 return SH_DYNAMIC_SHIFT_COST;
1218
1219 value = INTVAL (XEXP (x, 1));
1220
1221 /* Otherwise, return the true cost in instructions. */
1222 if (GET_CODE (x) == ASHIFTRT)
1223 {
1224 int cost = ashiftrt_insns[value];
1225 /* If SH3, then we put the constant in a reg and use shad. */
1226 if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
1227 cost = 1 + SH_DYNAMIC_SHIFT_COST;
1228 return cost;
1229 }
1230 else
1231 return shift_insns[value];
1232 }
1233
1234 /* Return the cost of an AND operation. */
1235
1236 int
1237 andcosts (x)
1238 rtx x;
1239 {
1240 int i;
1241
1242 /* Anding with a register is a single cycle and instruction. */
1243 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1244 return 1;
1245
1246 i = INTVAL (XEXP (x, 1));
1247
1248 if (TARGET_SHMEDIA)
1249 {
1250 if ((GET_CODE (XEXP (x, 1)) == CONST_INT
1251 && CONST_OK_FOR_J (INTVAL (XEXP (x, 1))))
1252 || EXTRA_CONSTRAINT_S (XEXP (x, 1)))
1253 return 1;
1254 else
1255 return 2;
1256 }
1257
1258 /* These constants are single cycle extu.[bw] instructions. */
1259 if (i == 0xff || i == 0xffff)
1260 return 1;
1261 /* Constants that can be used in an and immediate instruction is a single
1262 cycle, but this requires r0, so make it a little more expensive. */
1263 if (CONST_OK_FOR_L (i))
1264 return 2;
1265 /* Constants that can be loaded with a mov immediate and an and.
1266 This case is probably unnecessary. */
1267 if (CONST_OK_FOR_I (i))
1268 return 2;
1269 /* Any other constants requires a 2 cycle pc-relative load plus an and.
1270 This case is probably unnecessary. */
1271 return 3;
1272 }
1273
1274 /* Return the cost of an addition or a subtraction. */
1275
1276 int
1277 addsubcosts (x)
1278 rtx x;
1279 {
1280 /* Adding a register is a single cycle insn. */
1281 if (GET_CODE (XEXP (x, 1)) == REG
1282 || GET_CODE (XEXP (x, 1)) == SUBREG)
1283 return 1;
1284
1285 /* Likewise for small constants. */
1286 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1287 && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
1288 return 1;
1289
1290 if (TARGET_SHMEDIA)
1291 switch (GET_CODE (XEXP (x, 1)))
1292 {
1293 case CONST:
1294 case LABEL_REF:
1295 case SYMBOL_REF:
1296 return TARGET_SHMEDIA64 ? 5 : 3;
1297
1298 case CONST_INT:
1299 if (CONST_OK_FOR_J (INTVAL (XEXP (x, 1))))
1300 return 2;
1301 else if (CONST_OK_FOR_J (INTVAL (XEXP (x, 1)) >> 16))
1302 return 3;
1303 else if (CONST_OK_FOR_J ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
1304 return 4;
1305
1306 /* Fall through. */
1307 default:
1308 return 5;
1309 }
1310
1311 /* Any other constant requires a 2 cycle pc-relative load plus an
1312 addition. */
1313 return 3;
1314 }
1315
1316 /* Return the cost of a multiply. */
1317 int
1318 multcosts (x)
1319 rtx x ATTRIBUTE_UNUSED;
1320 {
1321 if (TARGET_SHMEDIA)
1322 return 3;
1323
1324 if (TARGET_SH2)
1325 {
1326 /* We have a mul insn, so we can never take more than the mul and the
1327 read of the mac reg, but count more because of the latency and extra
1328 reg usage. */
1329 if (TARGET_SMALLCODE)
1330 return 2;
1331 return 3;
1332 }
1333
1334 /* If we're aiming at small code, then just count the number of
1335 insns in a multiply call sequence. */
1336 if (TARGET_SMALLCODE)
1337 return 5;
1338
1339 /* Otherwise count all the insns in the routine we'd be calling too. */
1340 return 20;
1341 }
1342
1343 /* Code to expand a shift. */
1344
1345 void
1346 gen_ashift (type, n, reg)
1347 int type;
1348 int n;
1349 rtx reg;
1350 {
1351 /* Negative values here come from the shift_amounts array. */
1352 if (n < 0)
1353 {
1354 if (type == ASHIFT)
1355 type = LSHIFTRT;
1356 else
1357 type = ASHIFT;
1358 n = -n;
1359 }
1360
1361 switch (type)
1362 {
1363 case ASHIFTRT:
1364 emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
1365 break;
1366 case LSHIFTRT:
1367 if (n == 1)
1368 emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
1369 else
1370 emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
1371 break;
1372 case ASHIFT:
1373 emit_insn (gen_ashlsi3_std (reg, reg, GEN_INT (n)));
1374 break;
1375 }
1376 }
1377
1378 /* Same for HImode */
1379
1380 void
1381 gen_ashift_hi (type, n, reg)
1382 int type;
1383 int n;
1384 rtx reg;
1385 {
1386 /* Negative values here come from the shift_amounts array. */
1387 if (n < 0)
1388 {
1389 if (type == ASHIFT)
1390 type = LSHIFTRT;
1391 else
1392 type = ASHIFT;
1393 n = -n;
1394 }
1395
1396 switch (type)
1397 {
1398 case ASHIFTRT:
1399 case LSHIFTRT:
1400 /* We don't have HImode right shift operations because using the
1401 ordinary 32 bit shift instructions for that doesn't generate proper
1402 zero/sign extension.
1403 gen_ashift_hi is only called in contexts where we know that the
1404 sign extension works out correctly. */
1405 {
1406 int offset = 0;
1407 if (GET_CODE (reg) == SUBREG)
1408 {
1409 offset = SUBREG_BYTE (reg);
1410 reg = SUBREG_REG (reg);
1411 }
1412 gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
1413 break;
1414 }
1415 case ASHIFT:
1416 emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
1417 break;
1418 }
1419 }
1420
1421 /* Output RTL to split a constant shift into its component SH constant
1422 shift instructions. */
1423
1424 void
1425 gen_shifty_op (code, operands)
1426 int code;
1427 rtx *operands;
1428 {
1429 int value = INTVAL (operands[2]);
1430 int max, i;
1431
1432 /* Truncate the shift count in case it is out of bounds. */
1433 value = value & 0x1f;
1434
1435 if (value == 31)
1436 {
1437 if (code == LSHIFTRT)
1438 {
1439 emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
1440 emit_insn (gen_movt (operands[0]));
1441 return;
1442 }
1443 else if (code == ASHIFT)
1444 {
1445 /* There is a two instruction sequence for 31 bit left shifts,
1446 but it requires r0. */
1447 if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
1448 {
1449 emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
1450 emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
1451 return;
1452 }
1453 }
1454 }
1455 else if (value == 0)
1456 {
1457 /* This can happen when not optimizing. We must output something here
1458 to prevent the compiler from aborting in final.c after the try_split
1459 call. */
1460 emit_insn (gen_nop ());
1461 return;
1462 }
1463
1464 max = shift_insns[value];
1465 for (i = 0; i < max; i++)
1466 gen_ashift (code, shift_amounts[value][i], operands[0]);
1467 }
1468
1469 /* Same as above, but optimized for values where the topmost bits don't
1470 matter. */
1471
1472 void
1473 gen_shifty_hi_op (code, operands)
1474 int code;
1475 rtx *operands;
1476 {
1477 int value = INTVAL (operands[2]);
1478 int max, i;
1479 void (*gen_fun) PARAMS ((int, int, rtx));
1480
1481 /* This operation is used by and_shl for SImode values with a few
1482 high bits known to be cleared. */
1483 value &= 31;
1484 if (value == 0)
1485 {
1486 emit_insn (gen_nop ());
1487 return;
1488 }
1489
1490 gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
1491 if (code == ASHIFT)
1492 {
1493 max = ext_shift_insns[value];
1494 for (i = 0; i < max; i++)
1495 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1496 }
1497 else
1498 /* When shifting right, emit the shifts in reverse order, so that
1499 solitary negative values come first. */
1500 for (i = ext_shift_insns[value] - 1; i >= 0; i--)
1501 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1502 }
1503
1504 /* Output RTL for an arithmetic right shift. */
1505
1506 /* ??? Rewrite to use super-optimizer sequences. */
1507
1508 int
1509 expand_ashiftrt (operands)
1510 rtx *operands;
1511 {
1512 rtx sym;
1513 rtx wrk;
1514 char func[18];
1515 tree func_name;
1516 int value;
1517
1518 if (TARGET_SH3)
1519 {
1520 if (GET_CODE (operands[2]) != CONST_INT)
1521 {
1522 rtx count = copy_to_mode_reg (SImode, operands[2]);
1523 emit_insn (gen_negsi2 (count, count));
1524 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1525 return 1;
1526 }
1527 else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
1528 > 1 + SH_DYNAMIC_SHIFT_COST)
1529 {
1530 rtx count
1531 = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
1532 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1533 return 1;
1534 }
1535 }
1536 if (GET_CODE (operands[2]) != CONST_INT)
1537 return 0;
1538
1539 value = INTVAL (operands[2]) & 31;
1540
1541 if (value == 31)
1542 {
1543 emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1544 return 1;
1545 }
1546 else if (value >= 16 && value <= 19)
1547 {
1548 wrk = gen_reg_rtx (SImode);
1549 emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1550 value -= 16;
1551 while (value--)
1552 gen_ashift (ASHIFTRT, 1, wrk);
1553 emit_move_insn (operands[0], wrk);
1554 return 1;
1555 }
1556 /* Expand a short sequence inline, longer call a magic routine. */
1557 else if (value <= 5)
1558 {
1559 wrk = gen_reg_rtx (SImode);
1560 emit_move_insn (wrk, operands[1]);
1561 while (value--)
1562 gen_ashift (ASHIFTRT, 1, wrk);
1563 emit_move_insn (operands[0], wrk);
1564 return 1;
1565 }
1566
1567 wrk = gen_reg_rtx (Pmode);
1568
1569 /* Load the value into an arg reg and call a helper. */
1570 emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1571 sprintf (func, "__ashiftrt_r4_%d", value);
1572 func_name = get_identifier (func);
1573 sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (func_name));
1574 emit_move_insn (wrk, sym);
1575 emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1576 emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
1577 return 1;
1578 }
1579
1580 int
1581 sh_dynamicalize_shift_p (count)
1582 rtx count;
1583 {
1584 return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
1585 }
1586
1587 /* Try to find a good way to implement the combiner pattern
1588 [(set (match_operand:SI 0 "register_operand" "r")
1589 (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1590 (match_operand:SI 2 "const_int_operand" "n"))
1591 (match_operand:SI 3 "const_int_operand" "n"))) .
1592 LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1593 return 0 for simple right / left or left/right shift combination.
1594 return 1 for a combination of shifts with zero_extend.
1595 return 2 for a combination of shifts with an AND that needs r0.
1596 return 3 for a combination of shifts with an AND that needs an extra
1597 scratch register, when the three highmost bits of the AND mask are clear.
1598 return 4 for a combination of shifts with an AND that needs an extra
1599 scratch register, when any of the three highmost bits of the AND mask
1600 is set.
1601 If ATTRP is set, store an initial right shift width in ATTRP[0],
1602 and the instruction length in ATTRP[1] . These values are not valid
1603 when returning 0.
1604 When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1605 shift_amounts for the last shift value that is to be used before the
1606 sign extend. */
1607 int
1608 shl_and_kind (left_rtx, mask_rtx, attrp)
1609 rtx left_rtx, mask_rtx;
1610 int *attrp;
1611 {
1612 unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1613 int left = INTVAL (left_rtx), right;
1614 int best = 0;
1615 int cost, best_cost = 10000;
1616 int best_right = 0, best_len = 0;
1617 int i;
1618 int can_ext;
1619
1620 if (left < 0 || left > 31)
1621 return 0;
1622 if (GET_CODE (mask_rtx) == CONST_INT)
1623 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
1624 else
1625 mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
1626 /* Can this be expressed as a right shift / left shift pair ? */
1627 lsb = ((mask ^ (mask - 1)) >> 1) + 1;
1628 right = exact_log2 (lsb);
1629 mask2 = ~(mask + lsb - 1);
1630 lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
1631 /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1632 if (! mask2)
1633 best_cost = shift_insns[right] + shift_insns[right + left];
1634 /* mask has no trailing zeroes <==> ! right */
1635 else if (! right && mask2 == ~(lsb2 - 1))
1636 {
1637 int late_right = exact_log2 (lsb2);
1638 best_cost = shift_insns[left + late_right] + shift_insns[late_right];
1639 }
1640 /* Try to use zero extend */
1641 if (mask2 == ~(lsb2 - 1))
1642 {
1643 int width, first;
1644
1645 for (width = 8; width <= 16; width += 8)
1646 {
1647 /* Can we zero-extend right away? */
1648 if (lsb2 == (unsigned HOST_WIDE_INT)1 << width)
1649 {
1650 cost
1651 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
1652 if (cost < best_cost)
1653 {
1654 best = 1;
1655 best_cost = cost;
1656 best_right = right;
1657 best_len = cost;
1658 if (attrp)
1659 attrp[2] = -1;
1660 }
1661 continue;
1662 }
1663 /* ??? Could try to put zero extend into initial right shift,
1664 or even shift a bit left before the right shift. */
1665 /* Determine value of first part of left shift, to get to the
1666 zero extend cut-off point. */
1667 first = width - exact_log2 (lsb2) + right;
1668 if (first >= 0 && right + left - first >= 0)
1669 {
1670 cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
1671 + ext_shift_insns[right + left - first];
1672 if (cost < best_cost)
1673 {
1674 best = 1;
1675 best_cost = cost;
1676 best_right = right;
1677 best_len = cost;
1678 if (attrp)
1679 attrp[2] = first;
1680 }
1681 }
1682 }
1683 }
1684 /* Try to use r0 AND pattern */
1685 for (i = 0; i <= 2; i++)
1686 {
1687 if (i > right)
1688 break;
1689 if (! CONST_OK_FOR_L (mask >> i))
1690 continue;
1691 cost = (i != 0) + 2 + ext_shift_insns[left + i];
1692 if (cost < best_cost)
1693 {
1694 best = 2;
1695 best_cost = cost;
1696 best_right = i;
1697 best_len = cost - 1;
1698 }
1699 }
1700 /* Try to use a scratch register to hold the AND operand. */
1701 can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT)3 << 30)) == 0;
1702 for (i = 0; i <= 2; i++)
1703 {
1704 if (i > right)
1705 break;
1706 cost = (i != 0) + (CONST_OK_FOR_I (mask >> i) ? 2 : 3)
1707 + (can_ext ? ext_shift_insns : shift_insns)[left + i];
1708 if (cost < best_cost)
1709 {
1710 best = 4 - can_ext;
1711 best_cost = cost;
1712 best_right = i;
1713 best_len = cost - 1 - ! CONST_OK_FOR_I (mask >> i);
1714 }
1715 }
1716
1717 if (attrp)
1718 {
1719 attrp[0] = best_right;
1720 attrp[1] = best_len;
1721 }
1722 return best;
1723 }
1724
1725 /* This is used in length attributes of the unnamed instructions
1726 corresponding to shl_and_kind return values of 1 and 2. */
1727 int
1728 shl_and_length (insn)
1729 rtx insn;
1730 {
1731 rtx set_src, left_rtx, mask_rtx;
1732 int attributes[3];
1733
1734 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1735 left_rtx = XEXP (XEXP (set_src, 0), 1);
1736 mask_rtx = XEXP (set_src, 1);
1737 shl_and_kind (left_rtx, mask_rtx, attributes);
1738 return attributes[1];
1739 }
1740
1741 /* This is used in length attribute of the and_shl_scratch instruction. */
1742
1743 int
1744 shl_and_scr_length (insn)
1745 rtx insn;
1746 {
1747 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1748 int len = shift_insns[INTVAL (XEXP (set_src, 1))];
1749 rtx op = XEXP (set_src, 0);
1750 len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
1751 op = XEXP (XEXP (op, 0), 0);
1752 return len + shift_insns[INTVAL (XEXP (op, 1))];
1753 }
1754
1755 /* Generating rtl? */
1756 extern int rtx_equal_function_value_matters;
1757
1758 /* Generate rtl for instructions for which shl_and_kind advised a particular
1759 method of generating them, i.e. returned zero. */
1760
1761 int
1762 gen_shl_and (dest, left_rtx, mask_rtx, source)
1763 rtx dest, left_rtx, mask_rtx, source;
1764 {
1765 int attributes[3];
1766 unsigned HOST_WIDE_INT mask;
1767 int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
1768 int right, total_shift;
1769 void (*shift_gen_fun) PARAMS ((int, rtx*)) = gen_shifty_hi_op;
1770
1771 right = attributes[0];
1772 total_shift = INTVAL (left_rtx) + right;
1773 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
1774 switch (kind)
1775 {
1776 default:
1777 return -1;
1778 case 1:
1779 {
1780 int first = attributes[2];
1781 rtx operands[3];
1782
1783 if (first < 0)
1784 {
1785 emit_insn ((mask << right) <= 0xff
1786 ? gen_zero_extendqisi2(dest,
1787 gen_lowpart (QImode, source))
1788 : gen_zero_extendhisi2(dest,
1789 gen_lowpart (HImode, source)));
1790 source = dest;
1791 }
1792 if (source != dest)
1793 emit_insn (gen_movsi (dest, source));
1794 operands[0] = dest;
1795 if (right)
1796 {
1797 operands[2] = GEN_INT (right);
1798 gen_shifty_hi_op (LSHIFTRT, operands);
1799 }
1800 if (first > 0)
1801 {
1802 operands[2] = GEN_INT (first);
1803 gen_shifty_hi_op (ASHIFT, operands);
1804 total_shift -= first;
1805 mask <<= first;
1806 }
1807 if (first >= 0)
1808 emit_insn (mask <= 0xff
1809 ? gen_zero_extendqisi2(dest, gen_lowpart (QImode, dest))
1810 : gen_zero_extendhisi2(dest, gen_lowpart (HImode, dest)));
1811 if (total_shift > 0)
1812 {
1813 operands[2] = GEN_INT (total_shift);
1814 gen_shifty_hi_op (ASHIFT, operands);
1815 }
1816 break;
1817 }
1818 case 4:
1819 shift_gen_fun = gen_shifty_op;
1820 case 3:
1821 /* If the topmost bit that matters is set, set the topmost bits
1822 that don't matter. This way, we might be able to get a shorter
1823 signed constant. */
1824 if (mask & ((HOST_WIDE_INT)1 << (31 - total_shift)))
1825 mask |= (HOST_WIDE_INT)~0 << (31 - total_shift);
1826 case 2:
1827 /* Don't expand fine-grained when combining, because that will
1828 make the pattern fail. */
1829 if (rtx_equal_function_value_matters
1830 || reload_in_progress || reload_completed)
1831 {
1832 rtx operands[3];
1833
1834 /* Cases 3 and 4 should be handled by this split
1835 only while combining */
1836 if (kind > 2)
1837 abort ();
1838 if (right)
1839 {
1840 emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
1841 source = dest;
1842 }
1843 emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
1844 if (total_shift)
1845 {
1846 operands[0] = dest;
1847 operands[1] = dest;
1848 operands[2] = GEN_INT (total_shift);
1849 shift_gen_fun (ASHIFT, operands);
1850 }
1851 break;
1852 }
1853 else
1854 {
1855 int neg = 0;
1856 if (kind != 4 && total_shift < 16)
1857 {
1858 neg = -ext_shift_amounts[total_shift][1];
1859 if (neg > 0)
1860 neg -= ext_shift_amounts[total_shift][2];
1861 else
1862 neg = 0;
1863 }
1864 emit_insn (gen_and_shl_scratch (dest, source,
1865 GEN_INT (right),
1866 GEN_INT (mask),
1867 GEN_INT (total_shift + neg),
1868 GEN_INT (neg)));
1869 emit_insn (gen_movsi (dest, dest));
1870 break;
1871 }
1872 }
1873 return 0;
1874 }
1875
1876 /* Try to find a good way to implement the combiner pattern
1877 [(set (match_operand:SI 0 "register_operand" "=r")
1878 (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1879 (match_operand:SI 2 "const_int_operand" "n")
1880 (match_operand:SI 3 "const_int_operand" "n")
1881 (const_int 0)))
1882 (clobber (reg:SI T_REG))]
1883 LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1884 return 0 for simple left / right shift combination.
1885 return 1 for left shift / 8 bit sign extend / left shift.
1886 return 2 for left shift / 16 bit sign extend / left shift.
1887 return 3 for left shift / 8 bit sign extend / shift / sign extend.
1888 return 4 for left shift / 16 bit sign extend / shift / sign extend.
1889 return 5 for left shift / 16 bit sign extend / right shift
1890 return 6 for < 8 bit sign extend / left shift.
1891 return 7 for < 8 bit sign extend / left shift / single right shift.
1892 If COSTP is nonzero, assign the calculated cost to *COSTP. */
1893
1894 int
1895 shl_sext_kind (left_rtx, size_rtx, costp)
1896 rtx left_rtx, size_rtx;
1897 int *costp;
1898 {
1899 int left, size, insize, ext;
1900 int cost, best_cost;
1901 int kind;
1902
1903 left = INTVAL (left_rtx);
1904 size = INTVAL (size_rtx);
1905 insize = size - left;
1906 if (insize <= 0)
1907 abort ();
1908 /* Default to left / right shift. */
1909 kind = 0;
1910 best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
1911 if (size <= 16)
1912 {
1913 /* 16 bit shift / sign extend / 16 bit shift */
1914 cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
1915 /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
1916 below, by alternative 3 or something even better. */
1917 if (cost < best_cost)
1918 {
1919 kind = 5;
1920 best_cost = cost;
1921 }
1922 }
1923 /* Try a plain sign extend between two shifts. */
1924 for (ext = 16; ext >= insize; ext -= 8)
1925 {
1926 if (ext <= size)
1927 {
1928 cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
1929 if (cost < best_cost)
1930 {
1931 kind = ext / (unsigned) 8;
1932 best_cost = cost;
1933 }
1934 }
1935 /* Check if we can do a sloppy shift with a final signed shift
1936 restoring the sign. */
1937 if (EXT_SHIFT_SIGNED (size - ext))
1938 cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
1939 /* If not, maybe it's still cheaper to do the second shift sloppy,
1940 and do a final sign extend? */
1941 else if (size <= 16)
1942 cost = ext_shift_insns[ext - insize] + 1
1943 + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
1944 else
1945 continue;
1946 if (cost < best_cost)
1947 {
1948 kind = ext / (unsigned) 8 + 2;
1949 best_cost = cost;
1950 }
1951 }
1952 /* Check if we can sign extend in r0 */
1953 if (insize < 8)
1954 {
1955 cost = 3 + shift_insns[left];
1956 if (cost < best_cost)
1957 {
1958 kind = 6;
1959 best_cost = cost;
1960 }
1961 /* Try the same with a final signed shift. */
1962 if (left < 31)
1963 {
1964 cost = 3 + ext_shift_insns[left + 1] + 1;
1965 if (cost < best_cost)
1966 {
1967 kind = 7;
1968 best_cost = cost;
1969 }
1970 }
1971 }
1972 if (TARGET_SH3)
1973 {
1974 /* Try to use a dynamic shift. */
1975 cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
1976 if (cost < best_cost)
1977 {
1978 kind = 0;
1979 best_cost = cost;
1980 }
1981 }
1982 if (costp)
1983 *costp = cost;
1984 return kind;
1985 }
1986
1987 /* Function to be used in the length attribute of the instructions
1988 implementing this pattern. */
1989
1990 int
1991 shl_sext_length (insn)
1992 rtx insn;
1993 {
1994 rtx set_src, left_rtx, size_rtx;
1995 int cost;
1996
1997 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1998 left_rtx = XEXP (XEXP (set_src, 0), 1);
1999 size_rtx = XEXP (set_src, 1);
2000 shl_sext_kind (left_rtx, size_rtx, &cost);
2001 return cost;
2002 }
2003
2004 /* Generate rtl for this pattern */
2005
2006 int
2007 gen_shl_sext (dest, left_rtx, size_rtx, source)
2008 rtx dest, left_rtx, size_rtx, source;
2009 {
2010 int kind;
2011 int left, size, insize, cost;
2012 rtx operands[3];
2013
2014 kind = shl_sext_kind (left_rtx, size_rtx, &cost);
2015 left = INTVAL (left_rtx);
2016 size = INTVAL (size_rtx);
2017 insize = size - left;
2018 switch (kind)
2019 {
2020 case 1:
2021 case 2:
2022 case 3:
2023 case 4:
2024 {
2025 int ext = kind & 1 ? 8 : 16;
2026 int shift2 = size - ext;
2027
2028 /* Don't expand fine-grained when combining, because that will
2029 make the pattern fail. */
2030 if (! rtx_equal_function_value_matters
2031 && ! reload_in_progress && ! reload_completed)
2032 {
2033 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2034 emit_insn (gen_movsi (dest, source));
2035 break;
2036 }
2037 if (dest != source)
2038 emit_insn (gen_movsi (dest, source));
2039 operands[0] = dest;
2040 if (ext - insize)
2041 {
2042 operands[2] = GEN_INT (ext - insize);
2043 gen_shifty_hi_op (ASHIFT, operands);
2044 }
2045 emit_insn (kind & 1
2046 ? gen_extendqisi2(dest, gen_lowpart (QImode, dest))
2047 : gen_extendhisi2(dest, gen_lowpart (HImode, dest)));
2048 if (kind <= 2)
2049 {
2050 if (shift2)
2051 {
2052 operands[2] = GEN_INT (shift2);
2053 gen_shifty_op (ASHIFT, operands);
2054 }
2055 }
2056 else
2057 {
2058 if (shift2 > 0)
2059 {
2060 if (EXT_SHIFT_SIGNED (shift2))
2061 {
2062 operands[2] = GEN_INT (shift2 + 1);
2063 gen_shifty_op (ASHIFT, operands);
2064 operands[2] = GEN_INT (1);
2065 gen_shifty_op (ASHIFTRT, operands);
2066 break;
2067 }
2068 operands[2] = GEN_INT (shift2);
2069 gen_shifty_hi_op (ASHIFT, operands);
2070 }
2071 else if (shift2)
2072 {
2073 operands[2] = GEN_INT (-shift2);
2074 gen_shifty_hi_op (LSHIFTRT, operands);
2075 }
2076 emit_insn (size <= 8
2077 ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
2078 : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2079 }
2080 break;
2081 }
2082 case 5:
2083 {
2084 int i = 16 - size;
2085 if (! rtx_equal_function_value_matters
2086 && ! reload_in_progress && ! reload_completed)
2087 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2088 else
2089 {
2090 operands[0] = dest;
2091 operands[2] = GEN_INT (16 - insize);
2092 gen_shifty_hi_op (ASHIFT, operands);
2093 emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2094 }
2095 /* Don't use gen_ashrsi3 because it generates new pseudos. */
2096 while (--i >= 0)
2097 gen_ashift (ASHIFTRT, 1, dest);
2098 break;
2099 }
2100 case 6:
2101 case 7:
2102 /* Don't expand fine-grained when combining, because that will
2103 make the pattern fail. */
2104 if (! rtx_equal_function_value_matters
2105 && ! reload_in_progress && ! reload_completed)
2106 {
2107 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2108 emit_insn (gen_movsi (dest, source));
2109 break;
2110 }
2111 emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
2112 emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
2113 emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
2114 operands[0] = dest;
2115 operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
2116 gen_shifty_op (ASHIFT, operands);
2117 if (kind == 7)
2118 emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
2119 break;
2120 default:
2121 return -1;
2122 }
2123 return 0;
2124 }
2125
2126 /* Prefix a symbol_ref name with "datalabel". */
2127
2128 rtx
2129 gen_datalabel_ref (sym)
2130 rtx sym;
2131 {
2132 if (GET_CODE (sym) == LABEL_REF)
2133 return gen_rtx_CONST (GET_MODE (sym),
2134 gen_rtx_UNSPEC (GET_MODE (sym),
2135 gen_rtvec (1, sym),
2136 UNSPEC_DATALABEL));
2137
2138 if (GET_CODE (sym) != SYMBOL_REF)
2139 abort ();
2140
2141 XSTR (sym, 0) = concat (SH_DATALABEL_ENCODING, XSTR (sym, 0), NULL);
2142
2143 return sym;
2144 }
2145
2146 \f
2147 /* The SH cannot load a large constant into a register, constants have to
2148 come from a pc relative load. The reference of a pc relative load
2149 instruction must be less than 1k infront of the instruction. This
2150 means that we often have to dump a constant inside a function, and
2151 generate code to branch around it.
2152
2153 It is important to minimize this, since the branches will slow things
2154 down and make things bigger.
2155
2156 Worst case code looks like:
2157
2158 mov.l L1,rn
2159 bra L2
2160 nop
2161 align
2162 L1: .long value
2163 L2:
2164 ..
2165
2166 mov.l L3,rn
2167 bra L4
2168 nop
2169 align
2170 L3: .long value
2171 L4:
2172 ..
2173
2174 We fix this by performing a scan before scheduling, which notices which
2175 instructions need to have their operands fetched from the constant table
2176 and builds the table.
2177
2178 The algorithm is:
2179
2180 scan, find an instruction which needs a pcrel move. Look forward, find the
2181 last barrier which is within MAX_COUNT bytes of the requirement.
2182 If there isn't one, make one. Process all the instructions between
2183 the find and the barrier.
2184
2185 In the above example, we can tell that L3 is within 1k of L1, so
2186 the first move can be shrunk from the 3 insn+constant sequence into
2187 just 1 insn, and the constant moved to L3 to make:
2188
2189 mov.l L1,rn
2190 ..
2191 mov.l L3,rn
2192 bra L4
2193 nop
2194 align
2195 L3:.long value
2196 L4:.long value
2197
2198 Then the second move becomes the target for the shortening process. */
2199
2200 typedef struct
2201 {
2202 rtx value; /* Value in table. */
2203 rtx label; /* Label of value. */
2204 rtx wend; /* End of window. */
2205 enum machine_mode mode; /* Mode of value. */
2206 } pool_node;
2207
2208 /* The maximum number of constants that can fit into one pool, since
2209 the pc relative range is 0...1020 bytes and constants are at least 4
2210 bytes long. */
2211
2212 #define MAX_POOL_SIZE (1020/4)
2213 static pool_node pool_vector[MAX_POOL_SIZE];
2214 static int pool_size;
2215 static rtx pool_window_label;
2216 static int pool_window_last;
2217
2218 /* ??? If we need a constant in HImode which is the truncated value of a
2219 constant we need in SImode, we could combine the two entries thus saving
2220 two bytes. Is this common enough to be worth the effort of implementing
2221 it? */
2222
2223 /* ??? This stuff should be done at the same time that we shorten branches.
2224 As it is now, we must assume that all branches are the maximum size, and
2225 this causes us to almost always output constant pools sooner than
2226 necessary. */
2227
2228 /* Add a constant to the pool and return its label. */
2229
2230 static rtx
2231 add_constant (x, mode, last_value)
2232 rtx x;
2233 enum machine_mode mode;
2234 rtx last_value;
2235 {
2236 int i;
2237 rtx lab, new, ref, newref;
2238
2239 /* First see if we've already got it. */
2240 for (i = 0; i < pool_size; i++)
2241 {
2242 if (x->code == pool_vector[i].value->code
2243 && mode == pool_vector[i].mode)
2244 {
2245 if (x->code == CODE_LABEL)
2246 {
2247 if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
2248 continue;
2249 }
2250 if (rtx_equal_p (x, pool_vector[i].value))
2251 {
2252 lab = new = 0;
2253 if (! last_value
2254 || ! i
2255 || ! rtx_equal_p (last_value, pool_vector[i-1].value))
2256 {
2257 new = gen_label_rtx ();
2258 LABEL_REFS (new) = pool_vector[i].label;
2259 pool_vector[i].label = lab = new;
2260 }
2261 if (lab && pool_window_label)
2262 {
2263 newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2264 ref = pool_vector[pool_window_last].wend;
2265 LABEL_NEXTREF (newref) = ref;
2266 pool_vector[pool_window_last].wend = newref;
2267 }
2268 if (new)
2269 pool_window_label = new;
2270 pool_window_last = i;
2271 return lab;
2272 }
2273 }
2274 }
2275
2276 /* Need a new one. */
2277 pool_vector[pool_size].value = x;
2278 if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
2279 lab = 0;
2280 else
2281 lab = gen_label_rtx ();
2282 pool_vector[pool_size].mode = mode;
2283 pool_vector[pool_size].label = lab;
2284 pool_vector[pool_size].wend = NULL_RTX;
2285 if (lab && pool_window_label)
2286 {
2287 newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2288 ref = pool_vector[pool_window_last].wend;
2289 LABEL_NEXTREF (newref) = ref;
2290 pool_vector[pool_window_last].wend = newref;
2291 }
2292 if (lab)
2293 pool_window_label = lab;
2294 pool_window_last = pool_size;
2295 pool_size++;
2296 return lab;
2297 }
2298
2299 /* Output the literal table. */
2300
2301 static void
2302 dump_table (scan)
2303 rtx scan;
2304 {
2305 int i;
2306 int need_align = 1;
2307 rtx lab, ref;
2308 int have_di = 0;
2309
2310 /* Do two passes, first time dump out the HI sized constants. */
2311
2312 for (i = 0; i < pool_size; i++)
2313 {
2314 pool_node *p = &pool_vector[i];
2315
2316 if (p->mode == HImode)
2317 {
2318 if (need_align)
2319 {
2320 scan = emit_insn_after (gen_align_2 (), scan);
2321 need_align = 0;
2322 }
2323 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2324 scan = emit_label_after (lab, scan);
2325 scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
2326 scan);
2327 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2328 {
2329 lab = XEXP (ref, 0);
2330 scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2331 }
2332 }
2333 else if (p->mode == DImode || p->mode == DFmode)
2334 have_di = 1;
2335 }
2336
2337 need_align = 1;
2338
2339 if (TARGET_SHCOMPACT && have_di)
2340 {
2341 rtx align_insn = NULL_RTX;
2342
2343 scan = emit_label_after (gen_label_rtx (), scan);
2344 scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2345 need_align = 0;
2346
2347 for (i = 0; i < pool_size; i++)
2348 {
2349 pool_node *p = &pool_vector[i];
2350
2351 switch (p->mode)
2352 {
2353 case HImode:
2354 break;
2355 case SImode:
2356 case SFmode:
2357 if (align_insn)
2358 {
2359 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2360 emit_label_before (lab, align_insn);
2361 emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
2362 align_insn);
2363 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2364 {
2365 lab = XEXP (ref, 0);
2366 emit_insn_before (gen_consttable_window_end (lab),
2367 align_insn);
2368 }
2369 delete_insn (align_insn);
2370 align_insn = NULL_RTX;
2371 continue;
2372 }
2373 else
2374 {
2375 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2376 scan = emit_label_after (lab, scan);
2377 scan = emit_insn_after (gen_consttable_4 (p->value,
2378 const0_rtx), scan);
2379 need_align = ! need_align;
2380 }
2381 break;
2382 case DFmode:
2383 case DImode:
2384 if (need_align)
2385 {
2386 scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2387 align_insn = scan;
2388 need_align = 0;
2389 }
2390 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2391 scan = emit_label_after (lab, scan);
2392 scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2393 scan);
2394 break;
2395 default:
2396 abort ();
2397 break;
2398 }
2399
2400 if (p->mode != HImode)
2401 {
2402 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2403 {
2404 lab = XEXP (ref, 0);
2405 scan = emit_insn_after (gen_consttable_window_end (lab),
2406 scan);
2407 }
2408 }
2409 }
2410
2411 pool_size = 0;
2412 }
2413
2414 for (i = 0; i < pool_size; i++)
2415 {
2416 pool_node *p = &pool_vector[i];
2417
2418 switch (p->mode)
2419 {
2420 case HImode:
2421 break;
2422 case SImode:
2423 case SFmode:
2424 if (need_align)
2425 {
2426 need_align = 0;
2427 scan = emit_label_after (gen_label_rtx (), scan);
2428 scan = emit_insn_after (gen_align_4 (), scan);
2429 }
2430 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2431 scan = emit_label_after (lab, scan);
2432 scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
2433 scan);
2434 break;
2435 case DFmode:
2436 case DImode:
2437 if (need_align)
2438 {
2439 need_align = 0;
2440 scan = emit_label_after (gen_label_rtx (), scan);
2441 scan = emit_insn_after (gen_align_4 (), scan);
2442 }
2443 for (lab = p->label; lab; lab = LABEL_REFS (lab))
2444 scan = emit_label_after (lab, scan);
2445 scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2446 scan);
2447 break;
2448 default:
2449 abort ();
2450 break;
2451 }
2452
2453 if (p->mode != HImode)
2454 {
2455 for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2456 {
2457 lab = XEXP (ref, 0);
2458 scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2459 }
2460 }
2461 }
2462
2463 scan = emit_insn_after (gen_consttable_end (), scan);
2464 scan = emit_barrier_after (scan);
2465 pool_size = 0;
2466 pool_window_label = NULL_RTX;
2467 pool_window_last = 0;
2468 }
2469
2470 /* Return non-zero if constant would be an ok source for a
2471 mov.w instead of a mov.l. */
2472
2473 static int
2474 hi_const (src)
2475 rtx src;
2476 {
2477 return (GET_CODE (src) == CONST_INT
2478 && INTVAL (src) >= -32768
2479 && INTVAL (src) <= 32767);
2480 }
2481
2482 /* Non-zero if the insn is a move instruction which needs to be fixed. */
2483
2484 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
2485 CONST_DOUBLE input value is CONST_OK_FOR_I. For a SFmode move, we don't
2486 need to fix it if the input value is CONST_OK_FOR_I. */
2487
2488 static int
2489 broken_move (insn)
2490 rtx insn;
2491 {
2492 if (GET_CODE (insn) == INSN)
2493 {
2494 rtx pat = PATTERN (insn);
2495 if (GET_CODE (pat) == PARALLEL)
2496 pat = XVECEXP (pat, 0, 0);
2497 if (GET_CODE (pat) == SET
2498 /* We can load any 8 bit value if we don't care what the high
2499 order bits end up as. */
2500 && GET_MODE (SET_DEST (pat)) != QImode
2501 && (CONSTANT_P (SET_SRC (pat))
2502 /* Match mova_const. */
2503 || (GET_CODE (SET_SRC (pat)) == UNSPEC
2504 && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
2505 && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
2506 && ! (TARGET_SH3E
2507 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
2508 && (fp_zero_operand (SET_SRC (pat))
2509 || fp_one_operand (SET_SRC (pat)))
2510 /* ??? If this is a -m4 or -m4-single compilation, in general
2511 we don't know the current setting of fpscr, so disable fldi.
2512 There is an exception if this was a register-register move
2513 before reload - and hence it was ascertained that we have
2514 single precision setting - and in a post-reload optimization
2515 we changed this to do a constant load. In that case
2516 we don't have an r0 clobber, hence we must use fldi. */
2517 && (! TARGET_SH4 || TARGET_FMOVD
2518 || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
2519 == SCRATCH))
2520 && GET_CODE (SET_DEST (pat)) == REG
2521 && FP_REGISTER_P (REGNO (SET_DEST (pat))))
2522 && (GET_CODE (SET_SRC (pat)) != CONST_INT
2523 || ! CONST_OK_FOR_I (INTVAL (SET_SRC (pat)))))
2524 return 1;
2525 }
2526
2527 return 0;
2528 }
2529
2530 static int
2531 mova_p (insn)
2532 rtx insn;
2533 {
2534 return (GET_CODE (insn) == INSN
2535 && GET_CODE (PATTERN (insn)) == SET
2536 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
2537 && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
2538 /* Don't match mova_const. */
2539 && GET_CODE (XVECEXP (SET_SRC (PATTERN (insn)), 0, 0)) == LABEL_REF);
2540 }
2541
2542 /* Find the last barrier from insn FROM which is close enough to hold the
2543 constant pool. If we can't find one, then create one near the end of
2544 the range. */
2545
2546 static rtx
2547 find_barrier (num_mova, mova, from)
2548 int num_mova;
2549 rtx mova, from;
2550 {
2551 int count_si = 0;
2552 int count_hi = 0;
2553 int found_hi = 0;
2554 int found_si = 0;
2555 int found_di = 0;
2556 int hi_align = 2;
2557 int si_align = 2;
2558 int leading_mova = num_mova;
2559 rtx barrier_before_mova, found_barrier = 0, good_barrier = 0;
2560 int si_limit;
2561 int hi_limit;
2562
2563 /* For HImode: range is 510, add 4 because pc counts from address of
2564 second instruction after this one, subtract 2 for the jump instruction
2565 that we may need to emit before the table, subtract 2 for the instruction
2566 that fills the jump delay slot (in very rare cases, reorg will take an
2567 instruction from after the constant pool or will leave the delay slot
2568 empty). This gives 510.
2569 For SImode: range is 1020, add 4 because pc counts from address of
2570 second instruction after this one, subtract 2 in case pc is 2 byte
2571 aligned, subtract 2 for the jump instruction that we may need to emit
2572 before the table, subtract 2 for the instruction that fills the jump
2573 delay slot. This gives 1018. */
2574
2575 /* The branch will always be shortened now that the reference address for
2576 forward branches is the successor address, thus we need no longer make
2577 adjustments to the [sh]i_limit for -O0. */
2578
2579 si_limit = 1018;
2580 hi_limit = 510;
2581
2582 while (from && count_si < si_limit && count_hi < hi_limit)
2583 {
2584 int inc = get_attr_length (from);
2585 int new_align = 1;
2586
2587 if (GET_CODE (from) == CODE_LABEL)
2588 {
2589 if (optimize)
2590 new_align = 1 << label_to_alignment (from);
2591 else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
2592 new_align = 1 << barrier_align (from);
2593 else
2594 new_align = 1;
2595 inc = 0;
2596 }
2597
2598 if (GET_CODE (from) == BARRIER)
2599 {
2600
2601 found_barrier = from;
2602
2603 /* If we are at the end of the function, or in front of an alignment
2604 instruction, we need not insert an extra alignment. We prefer
2605 this kind of barrier. */
2606 if (barrier_align (from) > 2)
2607 good_barrier = from;
2608 }
2609
2610 if (broken_move (from))
2611 {
2612 rtx pat, src, dst;
2613 enum machine_mode mode;
2614
2615 pat = PATTERN (from);
2616 if (GET_CODE (pat) == PARALLEL)
2617 pat = XVECEXP (pat, 0, 0);
2618 src = SET_SRC (pat);
2619 dst = SET_DEST (pat);
2620 mode = GET_MODE (dst);
2621
2622 /* We must explicitly check the mode, because sometimes the
2623 front end will generate code to load unsigned constants into
2624 HImode targets without properly sign extending them. */
2625 if (mode == HImode
2626 || (mode == SImode && hi_const (src) && REGNO (dst) != FPUL_REG))
2627 {
2628 found_hi += 2;
2629 /* We put the short constants before the long constants, so
2630 we must count the length of short constants in the range
2631 for the long constants. */
2632 /* ??? This isn't optimal, but is easy to do. */
2633 si_limit -= 2;
2634 }
2635 else
2636 {
2637 /* We dump DF/DI constants before SF/SI ones, because
2638 the limit is the same, but the alignment requirements
2639 are higher. We may waste up to 4 additional bytes
2640 for alignment, and the DF/DI constant may have
2641 another SF/SI constant placed before it. */
2642 if (TARGET_SHCOMPACT
2643 && ! found_di
2644 && (mode == DFmode || mode == DImode))
2645 {
2646 found_di = 1;
2647 si_limit -= 8;
2648 }
2649 while (si_align > 2 && found_si + si_align - 2 > count_si)
2650 si_align >>= 1;
2651 if (found_si > count_si)
2652 count_si = found_si;
2653 found_si += GET_MODE_SIZE (mode);
2654 if (num_mova)
2655 si_limit -= GET_MODE_SIZE (mode);
2656 }
2657
2658 /* See the code in machine_dependent_reorg, which has a similar if
2659 statement that generates a new mova insn in many cases. */
2660 if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
2661 inc += 2;
2662 }
2663
2664 if (mova_p (from))
2665 {
2666 if (! num_mova++)
2667 {
2668 leading_mova = 0;
2669 mova = from;
2670 barrier_before_mova = good_barrier ? good_barrier : found_barrier;
2671 }
2672 if (found_si > count_si)
2673 count_si = found_si;
2674 }
2675 else if (GET_CODE (from) == JUMP_INSN
2676 && (GET_CODE (PATTERN (from)) == ADDR_VEC
2677 || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
2678 {
2679 if (num_mova)
2680 num_mova--;
2681 if (barrier_align (next_real_insn (from)) == CACHE_LOG)
2682 {
2683 /* We have just passed the barrier in front of the
2684 ADDR_DIFF_VEC, which is stored in found_barrier. Since
2685 the ADDR_DIFF_VEC is accessed as data, just like our pool
2686 constants, this is a good opportunity to accommodate what
2687 we have gathered so far.
2688 If we waited any longer, we could end up at a barrier in
2689 front of code, which gives worse cache usage for separated
2690 instruction / data caches. */
2691 good_barrier = found_barrier;
2692 break;
2693 }
2694 else
2695 {
2696 rtx body = PATTERN (from);
2697 inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
2698 }
2699 }
2700 /* For the SH1, we generate alignments even after jumps-around-jumps. */
2701 else if (GET_CODE (from) == JUMP_INSN
2702 && ! TARGET_SH2
2703 && ! TARGET_SMALLCODE)
2704 new_align = 4;
2705
2706 if (found_si)
2707 {
2708 count_si += inc;
2709 if (new_align > si_align)
2710 {
2711 si_limit -= (count_si - 1) & (new_align - si_align);
2712 si_align = new_align;
2713 }
2714 count_si = (count_si + new_align - 1) & -new_align;
2715 }
2716 if (found_hi)
2717 {
2718 count_hi += inc;
2719 if (new_align > hi_align)
2720 {
2721 hi_limit -= (count_hi - 1) & (new_align - hi_align);
2722 hi_align = new_align;
2723 }
2724 count_hi = (count_hi + new_align - 1) & -new_align;
2725 }
2726 from = NEXT_INSN (from);
2727 }
2728
2729 if (num_mova)
2730 {
2731 if (leading_mova)
2732 {
2733 /* Try as we might, the leading mova is out of range. Change
2734 it into a load (which will become a pcload) and retry. */
2735 SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2736 INSN_CODE (mova) = -1;
2737 return find_barrier (0, 0, mova);
2738 }
2739 else
2740 {
2741 /* Insert the constant pool table before the mova instruction,
2742 to prevent the mova label reference from going out of range. */
2743 from = mova;
2744 good_barrier = found_barrier = barrier_before_mova;
2745 }
2746 }
2747
2748 if (found_barrier)
2749 {
2750 if (good_barrier && next_real_insn (found_barrier))
2751 found_barrier = good_barrier;
2752 }
2753 else
2754 {
2755 /* We didn't find a barrier in time to dump our stuff,
2756 so we'll make one. */
2757 rtx label = gen_label_rtx ();
2758
2759 /* If we exceeded the range, then we must back up over the last
2760 instruction we looked at. Otherwise, we just need to undo the
2761 NEXT_INSN at the end of the loop. */
2762 if (count_hi > hi_limit || count_si > si_limit)
2763 from = PREV_INSN (PREV_INSN (from));
2764 else
2765 from = PREV_INSN (from);
2766
2767 /* Walk back to be just before any jump or label.
2768 Putting it before a label reduces the number of times the branch
2769 around the constant pool table will be hit. Putting it before
2770 a jump makes it more likely that the bra delay slot will be
2771 filled. */
2772 while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
2773 || GET_CODE (from) == CODE_LABEL)
2774 from = PREV_INSN (from);
2775
2776 from = emit_jump_insn_after (gen_jump (label), from);
2777 JUMP_LABEL (from) = label;
2778 LABEL_NUSES (label) = 1;
2779 found_barrier = emit_barrier_after (from);
2780 emit_label_after (label, found_barrier);
2781 }
2782
2783 return found_barrier;
2784 }
2785
2786 /* If the instruction INSN is implemented by a special function, and we can
2787 positively find the register that is used to call the sfunc, and this
2788 register is not used anywhere else in this instruction - except as the
2789 destination of a set, return this register; else, return 0. */
2790 rtx
2791 sfunc_uses_reg (insn)
2792 rtx insn;
2793 {
2794 int i;
2795 rtx pattern, part, reg_part, reg;
2796
2797 if (GET_CODE (insn) != INSN)
2798 return 0;
2799 pattern = PATTERN (insn);
2800 if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
2801 return 0;
2802
2803 for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2804 {
2805 part = XVECEXP (pattern, 0, i);
2806 if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
2807 reg_part = part;
2808 }
2809 if (! reg_part)
2810 return 0;
2811 reg = XEXP (reg_part, 0);
2812 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2813 {
2814 part = XVECEXP (pattern, 0, i);
2815 if (part == reg_part || GET_CODE (part) == CLOBBER)
2816 continue;
2817 if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
2818 && GET_CODE (SET_DEST (part)) == REG)
2819 ? SET_SRC (part) : part)))
2820 return 0;
2821 }
2822 return reg;
2823 }
2824
2825 /* See if the only way in which INSN uses REG is by calling it, or by
2826 setting it while calling it. Set *SET to a SET rtx if the register
2827 is set by INSN. */
2828
2829 static int
2830 noncall_uses_reg (reg, insn, set)
2831 rtx reg;
2832 rtx insn;
2833 rtx *set;
2834 {
2835 rtx pattern, reg2;
2836
2837 *set = NULL_RTX;
2838
2839 reg2 = sfunc_uses_reg (insn);
2840 if (reg2 && REGNO (reg2) == REGNO (reg))
2841 {
2842 pattern = single_set (insn);
2843 if (pattern
2844 && GET_CODE (SET_DEST (pattern)) == REG
2845 && REGNO (reg) == REGNO (SET_DEST (pattern)))
2846 *set = pattern;
2847 return 0;
2848 }
2849 if (GET_CODE (insn) != CALL_INSN)
2850 {
2851 /* We don't use rtx_equal_p because we don't care if the mode is
2852 different. */
2853 pattern = single_set (insn);
2854 if (pattern
2855 && GET_CODE (SET_DEST (pattern)) == REG
2856 && REGNO (reg) == REGNO (SET_DEST (pattern)))
2857 {
2858 rtx par, part;
2859 int i;
2860
2861 *set = pattern;
2862 par = PATTERN (insn);
2863 if (GET_CODE (par) == PARALLEL)
2864 for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
2865 {
2866 part = XVECEXP (par, 0, i);
2867 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
2868 return 1;
2869 }
2870 return reg_mentioned_p (reg, SET_SRC (pattern));
2871 }
2872
2873 return 1;
2874 }
2875
2876 pattern = PATTERN (insn);
2877
2878 if (GET_CODE (pattern) == PARALLEL)
2879 {
2880 int i;
2881
2882 for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2883 if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
2884 return 1;
2885 pattern = XVECEXP (pattern, 0, 0);
2886 }
2887
2888 if (GET_CODE (pattern) == SET)
2889 {
2890 if (reg_mentioned_p (reg, SET_DEST (pattern)))
2891 {
2892 /* We don't use rtx_equal_p, because we don't care if the
2893 mode is different. */
2894 if (GET_CODE (SET_DEST (pattern)) != REG
2895 || REGNO (reg) != REGNO (SET_DEST (pattern)))
2896 return 1;
2897
2898 *set = pattern;
2899 }
2900
2901 pattern = SET_SRC (pattern);
2902 }
2903
2904 if (GET_CODE (pattern) != CALL
2905 || GET_CODE (XEXP (pattern, 0)) != MEM
2906 || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
2907 return 1;
2908
2909 return 0;
2910 }
2911
2912 /* Given a X, a pattern of an insn or a part of it, return a mask of used
2913 general registers. Bits 0..15 mean that the respective registers
2914 are used as inputs in the instruction. Bits 16..31 mean that the
2915 registers 0..15, respectively, are used as outputs, or are clobbered.
2916 IS_DEST should be set to 16 if X is the destination of a SET, else to 0. */
2917 int
2918 regs_used (x, is_dest)
2919 rtx x; int is_dest;
2920 {
2921 enum rtx_code code;
2922 const char *fmt;
2923 int i, used = 0;
2924
2925 if (! x)
2926 return used;
2927 code = GET_CODE (x);
2928 switch (code)
2929 {
2930 case REG:
2931 if (REGNO (x) < 16)
2932 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2933 << (REGNO (x) + is_dest));
2934 return 0;
2935 case SUBREG:
2936 {
2937 rtx y = SUBREG_REG (x);
2938
2939 if (GET_CODE (y) != REG)
2940 break;
2941 if (REGNO (y) < 16)
2942 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2943 << (REGNO (y) +
2944 subreg_regno_offset (REGNO (y),
2945 GET_MODE (y),
2946 SUBREG_BYTE (x),
2947 GET_MODE (x)) + is_dest));
2948 return 0;
2949 }
2950 case SET:
2951 return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
2952 case RETURN:
2953 /* If there was a return value, it must have been indicated with USE. */
2954 return 0x00ffff00;
2955 case CLOBBER:
2956 is_dest = 1;
2957 break;
2958 case MEM:
2959 is_dest = 0;
2960 break;
2961 case CALL:
2962 used |= 0x00ff00f0;
2963 break;
2964 default:
2965 break;
2966 }
2967
2968 fmt = GET_RTX_FORMAT (code);
2969
2970 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2971 {
2972 if (fmt[i] == 'E')
2973 {
2974 register int j;
2975 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2976 used |= regs_used (XVECEXP (x, i, j), is_dest);
2977 }
2978 else if (fmt[i] == 'e')
2979 used |= regs_used (XEXP (x, i), is_dest);
2980 }
2981 return used;
2982 }
2983
2984 /* Create an instruction that prevents redirection of a conditional branch
2985 to the destination of the JUMP with address ADDR.
2986 If the branch needs to be implemented as an indirect jump, try to find
2987 a scratch register for it.
2988 If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
2989 If any preceding insn that doesn't fit into a delay slot is good enough,
2990 pass 1. Pass 2 if a definite blocking insn is needed.
2991 -1 is used internally to avoid deep recursion.
2992 If a blocking instruction is made or recognized, return it. */
2993
2994 static rtx
2995 gen_block_redirect (jump, addr, need_block)
2996 rtx jump;
2997 int addr, need_block;
2998 {
2999 int dead = 0;
3000 rtx prev = prev_nonnote_insn (jump);
3001 rtx dest;
3002
3003 /* First, check if we already have an instruction that satisfies our need. */
3004 if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
3005 {
3006 if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
3007 return prev;
3008 if (GET_CODE (PATTERN (prev)) == USE
3009 || GET_CODE (PATTERN (prev)) == CLOBBER
3010 || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3011 prev = jump;
3012 else if ((need_block &= ~1) < 0)
3013 return prev;
3014 else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
3015 need_block = 0;
3016 }
3017 /* We can't use JUMP_LABEL here because it might be undefined
3018 when not optimizing. */
3019 dest = XEXP (SET_SRC (PATTERN (jump)), 0);
3020 /* If the branch is out of range, try to find a scratch register for it. */
3021 if (optimize
3022 && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3023 > 4092 + 4098))
3024 {
3025 rtx scan;
3026 /* Don't look for the stack pointer as a scratch register,
3027 it would cause trouble if an interrupt occurred. */
3028 unsigned try = 0x7fff, used;
3029 int jump_left = flag_expensive_optimizations + 1;
3030
3031 /* It is likely that the most recent eligible instruction is wanted for
3032 the delay slot. Therefore, find out which registers it uses, and
3033 try to avoid using them. */
3034
3035 for (scan = jump; (scan = PREV_INSN (scan)); )
3036 {
3037 enum rtx_code code;
3038
3039 if (INSN_DELETED_P (scan))
3040 continue;
3041 code = GET_CODE (scan);
3042 if (code == CODE_LABEL || code == JUMP_INSN)
3043 break;
3044 if (code == INSN
3045 && GET_CODE (PATTERN (scan)) != USE
3046 && GET_CODE (PATTERN (scan)) != CLOBBER
3047 && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
3048 {
3049 try &= ~regs_used (PATTERN (scan), 0);
3050 break;
3051 }
3052 }
3053 for (used = dead = 0, scan = JUMP_LABEL (jump);
3054 (scan = NEXT_INSN (scan)); )
3055 {
3056 enum rtx_code code;
3057
3058 if (INSN_DELETED_P (scan))
3059 continue;
3060 code = GET_CODE (scan);
3061 if (GET_RTX_CLASS (code) == 'i')
3062 {
3063 used |= regs_used (PATTERN (scan), 0);
3064 if (code == CALL_INSN)
3065 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
3066 dead |= (used >> 16) & ~used;
3067 if (dead & try)
3068 {
3069 dead &= try;
3070 break;
3071 }
3072 if (code == JUMP_INSN)
3073 {
3074 if (jump_left-- && simplejump_p (scan))
3075 scan = JUMP_LABEL (scan);
3076 else
3077 break;
3078 }
3079 }
3080 }
3081 /* Mask out the stack pointer again, in case it was
3082 the only 'free' register we have found. */
3083 dead &= 0x7fff;
3084 }
3085 /* If the immediate destination is still in range, check for possible
3086 threading with a jump beyond the delay slot insn.
3087 Don't check if we are called recursively; the jump has been or will be
3088 checked in a different invocation then. */
3089
3090 else if (optimize && need_block >= 0)
3091 {
3092 rtx next = next_active_insn (next_active_insn (dest));
3093 if (next && GET_CODE (next) == JUMP_INSN
3094 && GET_CODE (PATTERN (next)) == SET
3095 && recog_memoized (next) == CODE_FOR_jump)
3096 {
3097 dest = JUMP_LABEL (next);
3098 if (dest
3099 && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3100 > 4092 + 4098))
3101 gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
3102 }
3103 }
3104
3105 if (dead)
3106 {
3107 rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
3108
3109 /* It would be nice if we could convert the jump into an indirect
3110 jump / far branch right now, and thus exposing all constituent
3111 instructions to further optimization. However, reorg uses
3112 simplejump_p to determine if there is an unconditional jump where
3113 it should try to schedule instructions from the target of the
3114 branch; simplejump_p fails for indirect jumps even if they have
3115 a JUMP_LABEL. */
3116 rtx insn = emit_insn_before (gen_indirect_jump_scratch
3117 (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
3118 , jump);
3119 INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
3120 return insn;
3121 }
3122 else if (need_block)
3123 /* We can't use JUMP_LABEL here because it might be undefined
3124 when not optimizing. */
3125 return emit_insn_before (gen_block_branch_redirect
3126 (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
3127 , jump);
3128 return prev;
3129 }
3130
3131 #define CONDJUMP_MIN -252
3132 #define CONDJUMP_MAX 262
3133 struct far_branch
3134 {
3135 /* A label (to be placed) in front of the jump
3136 that jumps to our ultimate destination. */
3137 rtx near_label;
3138 /* Where we are going to insert it if we cannot move the jump any farther,
3139 or the jump itself if we have picked up an existing jump. */
3140 rtx insert_place;
3141 /* The ultimate destination. */
3142 rtx far_label;
3143 struct far_branch *prev;
3144 /* If the branch has already been created, its address;
3145 else the address of its first prospective user. */
3146 int address;
3147 };
3148
3149 static void gen_far_branch PARAMS ((struct far_branch *));
3150 enum mdep_reorg_phase_e mdep_reorg_phase;
3151 static void
3152 gen_far_branch (bp)
3153 struct far_branch *bp;
3154 {
3155 rtx insn = bp->insert_place;
3156 rtx jump;
3157 rtx label = gen_label_rtx ();
3158
3159 emit_label_after (label, insn);
3160 if (bp->far_label)
3161 {
3162 jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
3163 LABEL_NUSES (bp->far_label)++;
3164 }
3165 else
3166 jump = emit_jump_insn_after (gen_return (), insn);
3167 /* Emit a barrier so that reorg knows that any following instructions
3168 are not reachable via a fall-through path.
3169 But don't do this when not optimizing, since we wouldn't supress the
3170 alignment for the barrier then, and could end up with out-of-range
3171 pc-relative loads. */
3172 if (optimize)
3173 emit_barrier_after (jump);
3174 emit_label_after (bp->near_label, insn);
3175 JUMP_LABEL (jump) = bp->far_label;
3176 if (! invert_jump (insn, label, 1))
3177 abort ();
3178 /* Prevent reorg from undoing our splits. */
3179 gen_block_redirect (jump, bp->address += 2, 2);
3180 }
3181
3182 /* Fix up ADDR_DIFF_VECs. */
3183 void
3184 fixup_addr_diff_vecs (first)
3185 rtx first;
3186 {
3187 rtx insn;
3188
3189 for (insn = first; insn; insn = NEXT_INSN (insn))
3190 {
3191 rtx vec_lab, pat, prev, prevpat, x, braf_label;
3192
3193 if (GET_CODE (insn) != JUMP_INSN
3194 || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
3195 continue;
3196 pat = PATTERN (insn);
3197 vec_lab = XEXP (XEXP (pat, 0), 0);
3198
3199 /* Search the matching casesi_jump_2. */
3200 for (prev = vec_lab; ; prev = PREV_INSN (prev))
3201 {
3202 if (GET_CODE (prev) != JUMP_INSN)
3203 continue;
3204 prevpat = PATTERN (prev);
3205 if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
3206 continue;
3207 x = XVECEXP (prevpat, 0, 1);
3208 if (GET_CODE (x) != USE)
3209 continue;
3210 x = XEXP (x, 0);
3211 if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
3212 break;
3213 }
3214
3215 /* Emit the reference label of the braf where it belongs, right after
3216 the casesi_jump_2 (i.e. braf). */
3217 braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
3218 emit_label_after (braf_label, prev);
3219
3220 /* Fix up the ADDR_DIF_VEC to be relative
3221 to the reference address of the braf. */
3222 XEXP (XEXP (pat, 0), 0) = braf_label;
3223 }
3224 }
3225
3226 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
3227 a barrier. Return the base 2 logarithm of the desired alignment. */
3228 int
3229 barrier_align (barrier_or_label)
3230 rtx barrier_or_label;
3231 {
3232 rtx next = next_real_insn (barrier_or_label), pat, prev;
3233 int slot, credit, jump_to_next;
3234
3235 if (! next)
3236 return 0;
3237
3238 pat = PATTERN (next);
3239
3240 if (GET_CODE (pat) == ADDR_DIFF_VEC)
3241 return 2;
3242
3243 if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
3244 /* This is a barrier in front of a constant table. */
3245 return 0;
3246
3247 prev = prev_real_insn (barrier_or_label);
3248 if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
3249 {
3250 pat = PATTERN (prev);
3251 /* If this is a very small table, we want to keep the alignment after
3252 the table to the minimum for proper code alignment. */
3253 return ((TARGET_SMALLCODE
3254 || (XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
3255 <= (unsigned)1 << (CACHE_LOG - 2)))
3256 ? 1 << TARGET_SHMEDIA : CACHE_LOG);
3257 }
3258
3259 if (TARGET_SMALLCODE)
3260 return 0;
3261
3262 if (! TARGET_SH2 || ! optimize)
3263 return CACHE_LOG;
3264
3265 /* When fixing up pcloads, a constant table might be inserted just before
3266 the basic block that ends with the barrier. Thus, we can't trust the
3267 instruction lengths before that. */
3268 if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
3269 {
3270 /* Check if there is an immediately preceding branch to the insn beyond
3271 the barrier. We must weight the cost of discarding useful information
3272 from the current cache line when executing this branch and there is
3273 an alignment, against that of fetching unneeded insn in front of the
3274 branch target when there is no alignment. */
3275
3276 /* There are two delay_slot cases to consider. One is the simple case
3277 where the preceding branch is to the insn beyond the barrier (simple
3278 delay slot filling), and the other is where the preceding branch has
3279 a delay slot that is a duplicate of the insn after the barrier
3280 (fill_eager_delay_slots) and the branch is to the insn after the insn
3281 after the barrier. */
3282
3283 /* PREV is presumed to be the JUMP_INSN for the barrier under
3284 investigation. Skip to the insn before it. */
3285 prev = prev_real_insn (prev);
3286
3287 for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
3288 credit >= 0 && prev && GET_CODE (prev) == INSN;
3289 prev = prev_real_insn (prev))
3290 {
3291 jump_to_next = 0;
3292 if (GET_CODE (PATTERN (prev)) == USE
3293 || GET_CODE (PATTERN (prev)) == CLOBBER)
3294 continue;
3295 if (GET_CODE (PATTERN (prev)) == SEQUENCE)
3296 {
3297 prev = XVECEXP (PATTERN (prev), 0, 1);
3298 if (INSN_UID (prev) == INSN_UID (next))
3299 {
3300 /* Delay slot was filled with insn at jump target. */
3301 jump_to_next = 1;
3302 continue;
3303 }
3304 }
3305
3306 if (slot &&
3307 get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3308 slot = 0;
3309 credit -= get_attr_length (prev);
3310 }
3311 if (prev
3312 && GET_CODE (prev) == JUMP_INSN
3313 && JUMP_LABEL (prev))
3314 {
3315 rtx x;
3316 if (jump_to_next
3317 || next_real_insn (JUMP_LABEL (prev)) == next
3318 /* If relax_delay_slots() decides NEXT was redundant
3319 with some previous instruction, it will have
3320 redirected PREV's jump to the following insn. */
3321 || JUMP_LABEL (prev) == next_nonnote_insn (next)
3322 /* There is no upper bound on redundant instructions
3323 that might have been skipped, but we must not put an
3324 alignment where none had been before. */
3325 || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),
3326 (INSN_P (x)
3327 && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
3328 || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch))))
3329 {
3330 rtx pat = PATTERN (prev);
3331 if (GET_CODE (pat) == PARALLEL)
3332 pat = XVECEXP (pat, 0, 0);
3333 if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
3334 return 0;
3335 }
3336 }
3337 }
3338
3339 return CACHE_LOG;
3340 }
3341
3342 /* If we are inside a phony loop, almost any kind of label can turn up as the
3343 first one in the loop. Aligning a braf label causes incorrect switch
3344 destination addresses; we can detect braf labels because they are
3345 followed by a BARRIER.
3346 Applying loop alignment to small constant or switch tables is a waste
3347 of space, so we suppress this too. */
3348 int
3349 sh_loop_align (label)
3350 rtx label;
3351 {
3352 rtx next = label;
3353
3354 do
3355 next = next_nonnote_insn (next);
3356 while (next && GET_CODE (next) == CODE_LABEL);
3357
3358 if (! next
3359 || ! INSN_P (next)
3360 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC
3361 || recog_memoized (next) == CODE_FOR_consttable_2)
3362 return 0;
3363
3364 if (TARGET_SH5)
3365 return 3;
3366
3367 return 2;
3368 }
3369
3370 /* Exported to toplev.c.
3371
3372 Do a final pass over the function, just before delayed branch
3373 scheduling. */
3374
3375 void
3376 machine_dependent_reorg (first)
3377 rtx first;
3378 {
3379 rtx insn, mova;
3380 int num_mova;
3381 rtx r0_rtx = gen_rtx_REG (Pmode, 0);
3382 rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
3383
3384 /* We must split call insns before introducing `mova's. If we're
3385 optimizing, they'll have already been split. Otherwise, make
3386 sure we don't split them too late. */
3387 if (! optimize)
3388 split_all_insns_noflow ();
3389
3390 if (TARGET_SHMEDIA)
3391 return;
3392
3393 /* If relaxing, generate pseudo-ops to associate function calls with
3394 the symbols they call. It does no harm to not generate these
3395 pseudo-ops. However, when we can generate them, it enables to
3396 linker to potentially relax the jsr to a bsr, and eliminate the
3397 register load and, possibly, the constant pool entry. */
3398
3399 mdep_reorg_phase = SH_INSERT_USES_LABELS;
3400 if (TARGET_RELAX)
3401 {
3402 /* Remove all REG_LABEL notes. We want to use them for our own
3403 purposes. This works because none of the remaining passes
3404 need to look at them.
3405
3406 ??? But it may break in the future. We should use a machine
3407 dependent REG_NOTE, or some other approach entirely. */
3408 for (insn = first; insn; insn = NEXT_INSN (insn))
3409 {
3410 if (INSN_P (insn))
3411 {
3412 rtx note;
3413
3414 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
3415 remove_note (insn, note);
3416 }
3417 }
3418
3419 for (insn = first; insn; insn = NEXT_INSN (insn))
3420 {
3421 rtx pattern, reg, link, set, scan, dies, label;
3422 int rescan = 0, foundinsn = 0;
3423
3424 if (GET_CODE (insn) == CALL_INSN)
3425 {
3426 pattern = PATTERN (insn);
3427
3428 if (GET_CODE (pattern) == PARALLEL)
3429 pattern = XVECEXP (pattern, 0, 0);
3430 if (GET_CODE (pattern) == SET)
3431 pattern = SET_SRC (pattern);
3432
3433 if (GET_CODE (pattern) != CALL
3434 || GET_CODE (XEXP (pattern, 0)) != MEM)
3435 continue;
3436
3437 reg = XEXP (XEXP (pattern, 0), 0);
3438 }
3439 else
3440 {
3441 reg = sfunc_uses_reg (insn);
3442 if (! reg)
3443 continue;
3444 }
3445
3446 if (GET_CODE (reg) != REG)
3447 continue;
3448
3449 /* This is a function call via REG. If the only uses of REG
3450 between the time that it is set and the time that it dies
3451 are in function calls, then we can associate all the
3452 function calls with the setting of REG. */
3453
3454 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3455 {
3456 if (REG_NOTE_KIND (link) != 0)
3457 continue;
3458 set = single_set (XEXP (link, 0));
3459 if (set && rtx_equal_p (reg, SET_DEST (set)))
3460 {
3461 link = XEXP (link, 0);
3462 break;
3463 }
3464 }
3465
3466 if (! link)
3467 {
3468 /* ??? Sometimes global register allocation will have
3469 deleted the insn pointed to by LOG_LINKS. Try
3470 scanning backward to find where the register is set. */
3471 for (scan = PREV_INSN (insn);
3472 scan && GET_CODE (scan) != CODE_LABEL;
3473 scan = PREV_INSN (scan))
3474 {
3475 if (! INSN_P (scan))
3476 continue;
3477
3478 if (! reg_mentioned_p (reg, scan))
3479 continue;
3480
3481 if (noncall_uses_reg (reg, scan, &set))
3482 break;
3483
3484 if (set)
3485 {
3486 link = scan;
3487 break;
3488 }
3489 }
3490 }
3491
3492 if (! link)
3493 continue;
3494
3495 /* The register is set at LINK. */
3496
3497 /* We can only optimize the function call if the register is
3498 being set to a symbol. In theory, we could sometimes
3499 optimize calls to a constant location, but the assembler
3500 and linker do not support that at present. */
3501 if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
3502 && GET_CODE (SET_SRC (set)) != LABEL_REF)
3503 continue;
3504
3505 /* Scan forward from LINK to the place where REG dies, and
3506 make sure that the only insns which use REG are
3507 themselves function calls. */
3508
3509 /* ??? This doesn't work for call targets that were allocated
3510 by reload, since there may not be a REG_DEAD note for the
3511 register. */
3512
3513 dies = NULL_RTX;
3514 for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
3515 {
3516 rtx scanset;
3517
3518 /* Don't try to trace forward past a CODE_LABEL if we haven't
3519 seen INSN yet. Ordinarily, we will only find the setting insn
3520 in LOG_LINKS if it is in the same basic block. However,
3521 cross-jumping can insert code labels in between the load and
3522 the call, and can result in situations where a single call
3523 insn may have two targets depending on where we came from. */
3524
3525 if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
3526 break;
3527
3528 if (! INSN_P (scan))
3529 continue;
3530
3531 /* Don't try to trace forward past a JUMP. To optimize
3532 safely, we would have to check that all the
3533 instructions at the jump destination did not use REG. */
3534
3535 if (GET_CODE (scan) == JUMP_INSN)
3536 break;
3537
3538 if (! reg_mentioned_p (reg, scan))
3539 continue;
3540
3541 if (noncall_uses_reg (reg, scan, &scanset))
3542 break;
3543
3544 if (scan == insn)
3545 foundinsn = 1;
3546
3547 if (scan != insn
3548 && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
3549 {
3550 /* There is a function call to this register other
3551 than the one we are checking. If we optimize
3552 this call, we need to rescan again below. */
3553 rescan = 1;
3554 }
3555
3556 /* ??? We shouldn't have to worry about SCANSET here.
3557 We should just be able to check for a REG_DEAD note
3558 on a function call. However, the REG_DEAD notes are
3559 apparently not dependable around libcalls; c-torture
3560 execute/920501-2 is a test case. If SCANSET is set,
3561 then this insn sets the register, so it must have
3562 died earlier. Unfortunately, this will only handle
3563 the cases in which the register is, in fact, set in a
3564 later insn. */
3565
3566 /* ??? We shouldn't have to use FOUNDINSN here.
3567 However, the LOG_LINKS fields are apparently not
3568 entirely reliable around libcalls;
3569 newlib/libm/math/e_pow.c is a test case. Sometimes
3570 an insn will appear in LOG_LINKS even though it is
3571 not the most recent insn which sets the register. */
3572
3573 if (foundinsn
3574 && (scanset
3575 || find_reg_note (scan, REG_DEAD, reg)))
3576 {
3577 dies = scan;
3578 break;
3579 }
3580 }
3581
3582 if (! dies)
3583 {
3584 /* Either there was a branch, or some insn used REG
3585 other than as a function call address. */
3586 continue;
3587 }
3588
3589 /* Create a code label, and put it in a REG_LABEL note on
3590 the insn which sets the register, and on each call insn
3591 which uses the register. In final_prescan_insn we look
3592 for the REG_LABEL notes, and output the appropriate label
3593 or pseudo-op. */
3594
3595 label = gen_label_rtx ();
3596 REG_NOTES (link) = gen_rtx_INSN_LIST (REG_LABEL, label,
3597 REG_NOTES (link));
3598 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
3599 REG_NOTES (insn));
3600 if (rescan)
3601 {
3602 scan = link;
3603 do
3604 {
3605 rtx reg2;
3606
3607 scan = NEXT_INSN (scan);
3608 if (scan != insn
3609 && ((GET_CODE (scan) == CALL_INSN
3610 && reg_mentioned_p (reg, scan))
3611 || ((reg2 = sfunc_uses_reg (scan))
3612 && REGNO (reg2) == REGNO (reg))))
3613 REG_NOTES (scan)
3614 = gen_rtx_INSN_LIST (REG_LABEL, label, REG_NOTES (scan));
3615 }
3616 while (scan != dies);
3617 }
3618 }
3619 }
3620
3621 if (TARGET_SH2)
3622 fixup_addr_diff_vecs (first);
3623
3624 if (optimize)
3625 {
3626 mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
3627 shorten_branches (first);
3628 }
3629 /* Scan the function looking for move instructions which have to be
3630 changed to pc-relative loads and insert the literal tables. */
3631
3632 mdep_reorg_phase = SH_FIXUP_PCLOAD;
3633 for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
3634 {
3635 if (mova_p (insn))
3636 {
3637 if (! num_mova++)
3638 mova = insn;
3639 }
3640 else if (GET_CODE (insn) == JUMP_INSN
3641 && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3642 && num_mova)
3643 {
3644 rtx scan;
3645 int total;
3646
3647 num_mova--;
3648
3649 /* Some code might have been inserted between the mova and
3650 its ADDR_DIFF_VEC. Check if the mova is still in range. */
3651 for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
3652 total += get_attr_length (scan);
3653
3654 /* range of mova is 1020, add 4 because pc counts from address of
3655 second instruction after this one, subtract 2 in case pc is 2
3656 byte aligned. Possible alignment needed for the ADDR_DIFF_VEC
3657 cancels out with alignment effects of the mova itself. */
3658 if (total > 1022)
3659 {
3660 /* Change the mova into a load, and restart scanning
3661 there. broken_move will then return true for mova. */
3662 SET_SRC (PATTERN (mova))
3663 = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
3664 INSN_CODE (mova) = -1;
3665 insn = mova;
3666 }
3667 }
3668 if (broken_move (insn))
3669 {
3670 rtx scan;
3671 /* Scan ahead looking for a barrier to stick the constant table
3672 behind. */
3673 rtx barrier = find_barrier (num_mova, mova, insn);
3674 rtx last_float_move, last_float = 0, *last_float_addr;
3675 int may_need_align = 1;
3676
3677 if (num_mova && ! mova_p (mova))
3678 {
3679 /* find_barrier had to change the first mova into a
3680 pcload; thus, we have to start with this new pcload. */
3681 insn = mova;
3682 num_mova = 0;
3683 }
3684 /* Now find all the moves between the points and modify them. */
3685 for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
3686 {
3687 if (GET_CODE (scan) == CODE_LABEL)
3688 last_float = 0;
3689 if (broken_move (scan))
3690 {
3691 rtx *patp = &PATTERN (scan), pat = *patp;
3692 rtx src, dst;
3693 rtx lab;
3694 rtx newsrc;
3695 enum machine_mode mode;
3696
3697 if (GET_CODE (pat) == PARALLEL)
3698 patp = &XVECEXP (pat, 0, 0), pat = *patp;
3699 src = SET_SRC (pat);
3700 dst = SET_DEST (pat);
3701 mode = GET_MODE (dst);
3702
3703 if (mode == SImode && hi_const (src)
3704 && REGNO (dst) != FPUL_REG)
3705 {
3706 int offset = 0;
3707
3708 mode = HImode;
3709 while (GET_CODE (dst) == SUBREG)
3710 {
3711 offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
3712 GET_MODE (SUBREG_REG (dst)),
3713 SUBREG_BYTE (dst),
3714 GET_MODE (dst));
3715 dst = SUBREG_REG (dst);
3716 }
3717 dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
3718 }
3719
3720 if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
3721 {
3722 /* This must be an insn that clobbers r0. */
3723 rtx clobber = XVECEXP (PATTERN (scan), 0,
3724 XVECLEN (PATTERN (scan), 0) - 1);
3725
3726 if (GET_CODE (clobber) != CLOBBER
3727 || ! rtx_equal_p (XEXP (clobber, 0), r0_rtx))
3728 abort ();
3729
3730 if (last_float
3731 && reg_set_between_p (r0_rtx, last_float_move, scan))
3732 last_float = 0;
3733 if (TARGET_SHCOMPACT)
3734 {
3735 /* The first SFmode constant after a DFmode
3736 constant may be pulled before a sequence
3737 of DFmode constants, so the second SFmode
3738 needs a label, just in case. */
3739 if (GET_MODE_SIZE (mode) == 4)
3740 {
3741 if (last_float && may_need_align)
3742 last_float = 0;
3743 may_need_align = 0;
3744 }
3745 if (last_float
3746 && (GET_MODE_SIZE (GET_MODE (last_float))
3747 != GET_MODE_SIZE (mode)))
3748 {
3749 last_float = 0;
3750 if (GET_MODE_SIZE (mode) == 4)
3751 may_need_align = 1;
3752 }
3753 }
3754 lab = add_constant (src, mode, last_float);
3755 if (lab)
3756 emit_insn_before (gen_mova (lab), scan);
3757 else
3758 {
3759 /* There will be a REG_UNUSED note for r0 on
3760 LAST_FLOAT_MOVE; we have to change it to REG_INC,
3761 lest reorg:mark_target_live_regs will not
3762 consider r0 to be used, and we end up with delay
3763 slot insn in front of SCAN that clobbers r0. */
3764 rtx note
3765 = find_regno_note (last_float_move, REG_UNUSED, 0);
3766
3767 /* If we are not optimizing, then there may not be
3768 a note. */
3769 if (note)
3770 PUT_MODE (note, REG_INC);
3771
3772 *last_float_addr = r0_inc_rtx;
3773 }
3774 last_float_move = scan;
3775 last_float = src;
3776 newsrc = gen_rtx (MEM, mode,
3777 (((TARGET_SH4 && ! TARGET_FMOVD)
3778 || REGNO (dst) == FPUL_REG)
3779 ? r0_inc_rtx
3780 : r0_rtx));
3781 last_float_addr = &XEXP (newsrc, 0);
3782
3783 /* Remove the clobber of r0. */
3784 XEXP (clobber, 0) = gen_rtx_SCRATCH (Pmode);
3785 }
3786 /* This is a mova needing a label. Create it. */
3787 else if (GET_CODE (src) == UNSPEC
3788 && XINT (src, 1) == UNSPEC_MOVA
3789 && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
3790 {
3791 lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
3792 newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
3793 newsrc = gen_rtx_UNSPEC (VOIDmode,
3794 gen_rtvec (1, newsrc),
3795 UNSPEC_MOVA);
3796 }
3797 else
3798 {
3799 lab = add_constant (src, mode, 0);
3800 newsrc = gen_rtx_MEM (mode,
3801 gen_rtx_LABEL_REF (VOIDmode, lab));
3802 }
3803 RTX_UNCHANGING_P (newsrc) = 1;
3804 *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
3805 INSN_CODE (scan) = -1;
3806 }
3807 }
3808 dump_table (barrier);
3809 insn = barrier;
3810 }
3811 }
3812
3813 mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
3814 INSN_ADDRESSES_FREE ();
3815 split_branches (first);
3816
3817 /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
3818 also has an effect on the register that holds the addres of the sfunc.
3819 Insert an extra dummy insn in front of each sfunc that pretends to
3820 use this register. */
3821 if (flag_delayed_branch)
3822 {
3823 for (insn = first; insn; insn = NEXT_INSN (insn))
3824 {
3825 rtx reg = sfunc_uses_reg (insn);
3826
3827 if (! reg)
3828 continue;
3829 emit_insn_before (gen_use_sfunc_addr (reg), insn);
3830 }
3831 }
3832 #if 0
3833 /* fpscr is not actually a user variable, but we pretend it is for the
3834 sake of the previous optimization passes, since we want it handled like
3835 one. However, we don't have any debugging information for it, so turn
3836 it into a non-user variable now. */
3837 if (TARGET_SH4)
3838 REG_USERVAR_P (get_fpscr_rtx ()) = 0;
3839 #endif
3840 mdep_reorg_phase = SH_AFTER_MDEP_REORG;
3841 }
3842
3843 int
3844 get_dest_uid (label, max_uid)
3845 rtx label;
3846 int max_uid;
3847 {
3848 rtx dest = next_real_insn (label);
3849 int dest_uid;
3850 if (! dest)
3851 /* This can happen for an undefined label. */
3852 return 0;
3853 dest_uid = INSN_UID (dest);
3854 /* If this is a newly created branch redirection blocking instruction,
3855 we cannot index the branch_uid or insn_addresses arrays with its
3856 uid. But then, we won't need to, because the actual destination is
3857 the following branch. */
3858 while (dest_uid >= max_uid)
3859 {
3860 dest = NEXT_INSN (dest);
3861 dest_uid = INSN_UID (dest);
3862 }
3863 if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
3864 return 0;
3865 return dest_uid;
3866 }
3867
3868 /* Split condbranches that are out of range. Also add clobbers for
3869 scratch registers that are needed in far jumps.
3870 We do this before delay slot scheduling, so that it can take our
3871 newly created instructions into account. It also allows us to
3872 find branches with common targets more easily. */
3873
3874 static void
3875 split_branches (first)
3876 rtx first;
3877 {
3878 rtx insn;
3879 struct far_branch **uid_branch, *far_branch_list = 0;
3880 int max_uid = get_max_uid ();
3881
3882 /* Find out which branches are out of range. */
3883 shorten_branches (first);
3884
3885 uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
3886 memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
3887
3888 for (insn = first; insn; insn = NEXT_INSN (insn))
3889 if (! INSN_P (insn))
3890 continue;
3891 else if (INSN_DELETED_P (insn))
3892 {
3893 /* Shorten_branches would split this instruction again,
3894 so transform it into a note. */
3895 PUT_CODE (insn, NOTE);
3896 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3897 NOTE_SOURCE_FILE (insn) = 0;
3898 }
3899 else if (GET_CODE (insn) == JUMP_INSN
3900 /* Don't mess with ADDR_DIFF_VEC */
3901 && (GET_CODE (PATTERN (insn)) == SET
3902 || GET_CODE (PATTERN (insn)) == RETURN))
3903 {
3904 enum attr_type type = get_attr_type (insn);
3905 if (type == TYPE_CBRANCH)
3906 {
3907 rtx next, beyond;
3908
3909 if (get_attr_length (insn) > 4)
3910 {
3911 rtx src = SET_SRC (PATTERN (insn));
3912 rtx olabel = XEXP (XEXP (src, 1), 0);
3913 int addr = INSN_ADDRESSES (INSN_UID (insn));
3914 rtx label = 0;
3915 int dest_uid = get_dest_uid (olabel, max_uid);
3916 struct far_branch *bp = uid_branch[dest_uid];
3917
3918 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
3919 the label if the LABEL_NUSES count drops to zero. There is
3920 always a jump_optimize pass that sets these values, but it
3921 proceeds to delete unreferenced code, and then if not
3922 optimizing, to un-delete the deleted instructions, thus
3923 leaving labels with too low uses counts. */
3924 if (! optimize)
3925 {
3926 JUMP_LABEL (insn) = olabel;
3927 LABEL_NUSES (olabel)++;
3928 }
3929 if (! bp)
3930 {
3931 bp = (struct far_branch *) alloca (sizeof *bp);
3932 uid_branch[dest_uid] = bp;
3933 bp->prev = far_branch_list;
3934 far_branch_list = bp;
3935 bp->far_label
3936 = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
3937 LABEL_NUSES (bp->far_label)++;
3938 }
3939 else
3940 {
3941 label = bp->near_label;
3942 if (! label && bp->address - addr >= CONDJUMP_MIN)
3943 {
3944 rtx block = bp->insert_place;
3945
3946 if (GET_CODE (PATTERN (block)) == RETURN)
3947 block = PREV_INSN (block);
3948 else
3949 block = gen_block_redirect (block,
3950 bp->address, 2);
3951 label = emit_label_after (gen_label_rtx (),
3952 PREV_INSN (block));
3953 bp->near_label = label;
3954 }
3955 else if (label && ! NEXT_INSN (label))
3956 {
3957 if (addr + 2 - bp->address <= CONDJUMP_MAX)
3958 bp->insert_place = insn;
3959 else
3960 gen_far_branch (bp);
3961 }
3962 }
3963 if (! label
3964 || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
3965 {
3966 bp->near_label = label = gen_label_rtx ();
3967 bp->insert_place = insn;
3968 bp->address = addr;
3969 }
3970 if (! redirect_jump (insn, label, 1))
3971 abort ();
3972 }
3973 else
3974 {
3975 /* get_attr_length (insn) == 2 */
3976 /* Check if we have a pattern where reorg wants to redirect
3977 the branch to a label from an unconditional branch that
3978 is too far away. */
3979 /* We can't use JUMP_LABEL here because it might be undefined
3980 when not optimizing. */
3981 /* A syntax error might cause beyond to be NULL_RTX. */
3982 beyond
3983 = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
3984 0));
3985
3986 if (beyond
3987 && (GET_CODE (beyond) == JUMP_INSN
3988 || ((beyond = next_active_insn (beyond))
3989 && GET_CODE (beyond) == JUMP_INSN))
3990 && GET_CODE (PATTERN (beyond)) == SET
3991 && recog_memoized (beyond) == CODE_FOR_jump
3992 && ((INSN_ADDRESSES
3993 (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
3994 - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
3995 > 252 + 258 + 2))
3996 gen_block_redirect (beyond,
3997 INSN_ADDRESSES (INSN_UID (beyond)), 1);
3998 }
3999
4000 next = next_active_insn (insn);
4001
4002 if ((GET_CODE (next) == JUMP_INSN
4003 || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
4004 && GET_CODE (PATTERN (next)) == SET
4005 && recog_memoized (next) == CODE_FOR_jump
4006 && ((INSN_ADDRESSES
4007 (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
4008 - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4009 > 252 + 258 + 2))
4010 gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
4011 }
4012 else if (type == TYPE_JUMP || type == TYPE_RETURN)
4013 {
4014 int addr = INSN_ADDRESSES (INSN_UID (insn));
4015 rtx far_label = 0;
4016 int dest_uid = 0;
4017 struct far_branch *bp;
4018
4019 if (type == TYPE_JUMP)
4020 {
4021 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
4022 dest_uid = get_dest_uid (far_label, max_uid);
4023 if (! dest_uid)
4024 {
4025 /* Parse errors can lead to labels outside
4026 the insn stream. */
4027 if (! NEXT_INSN (far_label))
4028 continue;
4029
4030 if (! optimize)
4031 {
4032 JUMP_LABEL (insn) = far_label;
4033 LABEL_NUSES (far_label)++;
4034 }
4035 redirect_jump (insn, NULL_RTX, 1);
4036 far_label = 0;
4037 }
4038 }
4039 bp = uid_branch[dest_uid];
4040 if (! bp)
4041 {
4042 bp = (struct far_branch *) alloca (sizeof *bp);
4043 uid_branch[dest_uid] = bp;
4044 bp->prev = far_branch_list;
4045 far_branch_list = bp;
4046 bp->near_label = 0;
4047 bp->far_label = far_label;
4048 if (far_label)
4049 LABEL_NUSES (far_label)++;
4050 }
4051 else if (bp->near_label && ! NEXT_INSN (bp->near_label))
4052 if (addr - bp->address <= CONDJUMP_MAX)
4053 emit_label_after (bp->near_label, PREV_INSN (insn));
4054 else
4055 {
4056 gen_far_branch (bp);
4057 bp->near_label = 0;
4058 }
4059 else
4060 bp->near_label = 0;
4061 bp->address = addr;
4062 bp->insert_place = insn;
4063 if (! far_label)
4064 emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
4065 else
4066 gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
4067 }
4068 }
4069 /* Generate all pending far branches,
4070 and free our references to the far labels. */
4071 while (far_branch_list)
4072 {
4073 if (far_branch_list->near_label
4074 && ! NEXT_INSN (far_branch_list->near_label))
4075 gen_far_branch (far_branch_list);
4076 if (optimize
4077 && far_branch_list->far_label
4078 && ! --LABEL_NUSES (far_branch_list->far_label))
4079 delete_insn (far_branch_list->far_label);
4080 far_branch_list = far_branch_list->prev;
4081 }
4082
4083 /* Instruction length information is no longer valid due to the new
4084 instructions that have been generated. */
4085 init_insn_lengths ();
4086 }
4087
4088 /* Dump out instruction addresses, which is useful for debugging the
4089 constant pool table stuff.
4090
4091 If relaxing, output the label and pseudo-ops used to link together
4092 calls and the instruction which set the registers. */
4093
4094 /* ??? This is unnecessary, and probably should be deleted. This makes
4095 the insn_addresses declaration above unnecessary. */
4096
4097 /* ??? The addresses printed by this routine for insns are nonsense for
4098 insns which are inside of a sequence where none of the inner insns have
4099 variable length. This is because the second pass of shorten_branches
4100 does not bother to update them. */
4101
4102 void
4103 final_prescan_insn (insn, opvec, noperands)
4104 rtx insn;
4105 rtx *opvec ATTRIBUTE_UNUSED;
4106 int noperands ATTRIBUTE_UNUSED;
4107 {
4108 if (TARGET_DUMPISIZE)
4109 fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
4110
4111 if (TARGET_RELAX)
4112 {
4113 rtx note;
4114
4115 note = find_reg_note (insn, REG_LABEL, NULL_RTX);
4116 if (note)
4117 {
4118 rtx pattern;
4119
4120 pattern = PATTERN (insn);
4121 if (GET_CODE (pattern) == PARALLEL)
4122 pattern = XVECEXP (pattern, 0, 0);
4123 if (GET_CODE (pattern) == CALL
4124 || (GET_CODE (pattern) == SET
4125 && (GET_CODE (SET_SRC (pattern)) == CALL
4126 || get_attr_type (insn) == TYPE_SFUNC)))
4127 asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
4128 CODE_LABEL_NUMBER (XEXP (note, 0)));
4129 else if (GET_CODE (pattern) == SET)
4130 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
4131 CODE_LABEL_NUMBER (XEXP (note, 0)));
4132 else
4133 abort ();
4134 }
4135 }
4136 }
4137
4138 /* Dump out any constants accumulated in the final pass. These will
4139 only be labels. */
4140
4141 const char *
4142 output_jump_label_table ()
4143 {
4144 int i;
4145
4146 if (pool_size)
4147 {
4148 fprintf (asm_out_file, "\t.align 2\n");
4149 for (i = 0; i < pool_size; i++)
4150 {
4151 pool_node *p = &pool_vector[i];
4152
4153 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
4154 CODE_LABEL_NUMBER (p->label));
4155 output_asm_insn (".long %O0", &p->value);
4156 }
4157 pool_size = 0;
4158 }
4159
4160 return "";
4161 }
4162 \f
4163 /* A full frame looks like:
4164
4165 arg-5
4166 arg-4
4167 [ if current_function_anonymous_args
4168 arg-3
4169 arg-2
4170 arg-1
4171 arg-0 ]
4172 saved-fp
4173 saved-r10
4174 saved-r11
4175 saved-r12
4176 saved-pr
4177 local-n
4178 ..
4179 local-1
4180 local-0 <- fp points here. */
4181
4182 /* Number of bytes pushed for anonymous args, used to pass information
4183 between expand_prologue and expand_epilogue. */
4184
4185 static int extra_push;
4186
4187 /* Adjust the stack by SIZE bytes. REG holds the rtl of the register
4188 to be adjusted, and TEMP, if nonnegative, holds the register number
4189 of a general register that we may clobber. */
4190
4191 static void
4192 output_stack_adjust (size, reg, temp)
4193 int size;
4194 rtx reg;
4195 int temp;
4196 {
4197 if (size)
4198 {
4199 HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4200
4201 if (size % align)
4202 abort ();
4203
4204 if (CONST_OK_FOR_ADD (size))
4205 emit_insn (GEN_ADD3 (reg, reg, GEN_INT (size)));
4206 /* Try to do it with two partial adjustments; however, we must make
4207 sure that the stack is properly aligned at all times, in case
4208 an interrupt occurs between the two partial adjustments. */
4209 else if (CONST_OK_FOR_ADD (size / 2 & -align)
4210 && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
4211 {
4212 emit_insn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
4213 emit_insn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
4214 }
4215 else
4216 {
4217 rtx const_reg;
4218
4219 /* If TEMP is invalid, we could temporarily save a general
4220 register to MACL. However, there is currently no need
4221 to handle this case, so just abort when we see it. */
4222 if (temp < 0)
4223 abort ();
4224 const_reg = gen_rtx_REG (GET_MODE (reg), temp);
4225
4226 /* If SIZE is negative, subtract the positive value.
4227 This sometimes allows a constant pool entry to be shared
4228 between prologue and epilogue code. */
4229 if (size < 0)
4230 {
4231 emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
4232 emit_insn (GEN_SUB3 (reg, reg, const_reg));
4233 }
4234 else
4235 {
4236 emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
4237 emit_insn (GEN_ADD3 (reg, reg, const_reg));
4238 }
4239 }
4240 }
4241 }
4242
4243 /* Output RTL to push register RN onto the stack. */
4244
4245 static void
4246 push (rn)
4247 int rn;
4248 {
4249 rtx x;
4250 if (rn == FPUL_REG)
4251 x = gen_push_fpul ();
4252 else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4253 && FP_OR_XD_REGISTER_P (rn))
4254 {
4255 if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4256 return;
4257 x = gen_push_4 (gen_rtx_REG (DFmode, rn));
4258 }
4259 else if (TARGET_SH3E && FP_REGISTER_P (rn))
4260 x = gen_push_e (gen_rtx_REG (SFmode, rn));
4261 else
4262 x = gen_push (gen_rtx_REG (SImode, rn));
4263
4264 x = emit_insn (x);
4265 REG_NOTES (x)
4266 = gen_rtx_EXPR_LIST (REG_INC,
4267 gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4268 }
4269
4270 /* Output RTL to pop register RN from the stack. */
4271
4272 static void
4273 pop (rn)
4274 int rn;
4275 {
4276 rtx x;
4277 if (rn == FPUL_REG)
4278 x = gen_pop_fpul ();
4279 else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4280 && FP_OR_XD_REGISTER_P (rn))
4281 {
4282 if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4283 return;
4284 x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
4285 }
4286 else if (TARGET_SH3E && FP_REGISTER_P (rn))
4287 x = gen_pop_e (gen_rtx_REG (SFmode, rn));
4288 else
4289 x = gen_pop (gen_rtx_REG (SImode, rn));
4290
4291 x = emit_insn (x);
4292 REG_NOTES (x)
4293 = gen_rtx_EXPR_LIST (REG_INC,
4294 gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4295 }
4296
4297 /* Generate code to push the regs specified in the mask. */
4298
4299 static void
4300 push_regs (mask)
4301 HOST_WIDE_INT *mask;
4302 {
4303 int i;
4304
4305 /* Push PR last; this gives better latencies after the prologue, and
4306 candidates for the return delay slot when there are no general
4307 registers pushed. */
4308 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4309 if (i != PR_REG && mask[i / 32] & (1 << (i % 32)))
4310 push (i);
4311 if (mask[PR_REG / 32] & (1 << (PR_REG % 32)))
4312 push (PR_REG);
4313 }
4314
4315 /* Work out the registers which need to be saved, both as a mask and a
4316 count of saved words.
4317
4318 If doing a pragma interrupt function, then push all regs used by the
4319 function, and if we call another function (we can tell by looking at PR),
4320 make sure that all the regs it clobbers are safe too. */
4321
4322 static void
4323 calc_live_regs (count_ptr, live_regs_mask)
4324 int *count_ptr;
4325 HOST_WIDE_INT *live_regs_mask;
4326 {
4327 int reg;
4328 int count;
4329 int interrupt_handler;
4330 rtx pr_initial;
4331 int pr_live;
4332
4333 if ((lookup_attribute
4334 ("interrupt_handler",
4335 DECL_ATTRIBUTES (current_function_decl)))
4336 != NULL_TREE)
4337 interrupt_handler = 1;
4338 else
4339 interrupt_handler = 0;
4340
4341 for (count = 0; 32 * count < FIRST_PSEUDO_REGISTER; count++)
4342 live_regs_mask[count] = 0;
4343 /* If we can save a lot of saves by switching to double mode, do that. */
4344 if (TARGET_SH4 && TARGET_FMOVD && TARGET_FPU_SINGLE)
4345 for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
4346 if (regs_ever_live[reg] && regs_ever_live[reg+1]
4347 && (! call_used_regs[reg] || (interrupt_handler && ! pragma_trapa))
4348 && ++count > 2)
4349 {
4350 target_flags &= ~FPU_SINGLE_BIT;
4351 break;
4352 }
4353 pr_initial = has_hard_reg_initial_val (Pmode,
4354 TARGET_SHMEDIA
4355 ? PR_MEDIA_REG : PR_REG);
4356 pr_live = (pr_initial
4357 ? REGNO (pr_initial) != (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
4358 : regs_ever_live[TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG]);
4359 /* Force PR to be live if the prologue has to call the SHmedia
4360 argument decoder or register saver. */
4361 if (TARGET_SHCOMPACT
4362 && ((current_function_args_info.call_cookie
4363 & ~ CALL_COOKIE_RET_TRAMP (1))
4364 || current_function_has_nonlocal_label))
4365 pr_live = 1;
4366 for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
4367 {
4368 if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
4369 ? pr_live
4370 : (interrupt_handler && ! pragma_trapa)
4371 ? (/* Need to save all the regs ever live. */
4372 (regs_ever_live[reg]
4373 || (call_used_regs[reg]
4374 && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
4375 && pr_live))
4376 && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
4377 && reg != RETURN_ADDRESS_POINTER_REGNUM
4378 && reg != T_REG && reg != GBR_REG && reg != FPSCR_REG)
4379 : (/* Only push those regs which are used and need to be saved. */
4380 regs_ever_live[reg] && ! call_used_regs[reg]))
4381 {
4382 live_regs_mask[reg / 32] |= 1 << (reg % 32);
4383 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
4384
4385 if ((TARGET_SH4 || TARGET_SH5) && TARGET_FMOVD
4386 && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
4387 {
4388 if (FP_REGISTER_P (reg))
4389 {
4390 if (! TARGET_FPU_SINGLE && ! regs_ever_live[reg ^ 1])
4391 {
4392 live_regs_mask[(reg ^ 1) / 32] |= 1 << ((reg ^ 1) % 32);
4393 count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
4394 }
4395 }
4396 else if (XD_REGISTER_P (reg))
4397 {
4398 /* Must switch to double mode to access these registers. */
4399 target_flags &= ~FPU_SINGLE_BIT;
4400 }
4401 }
4402 }
4403 }
4404
4405 *count_ptr = count;
4406 }
4407
4408 /* Code to generate prologue and epilogue sequences */
4409
4410 /* PUSHED is the number of bytes that are bing pushed on the
4411 stack for register saves. Return the frame size, padded
4412 appropriately so that the stack stays properly aligned. */
4413 static HOST_WIDE_INT
4414 rounded_frame_size (pushed)
4415 int pushed;
4416 {
4417 HOST_WIDE_INT size = get_frame_size ();
4418 HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4419
4420 return ((size + pushed + align - 1) & -align) - pushed;
4421 }
4422
4423 /* Choose a call-clobbered target-branch register that remains
4424 unchanged along the whole function. We set it up as the return
4425 value in the prologue. */
4426 int
4427 sh_media_register_for_return ()
4428 {
4429 int regno;
4430 int tr0_used;
4431
4432 if (! current_function_is_leaf)
4433 return -1;
4434
4435 tr0_used = flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
4436
4437 for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
4438 if (call_used_regs[regno] && ! regs_ever_live[regno])
4439 return regno;
4440
4441 return -1;
4442 }
4443
4444 void
4445 sh_expand_prologue ()
4446 {
4447 HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
4448 int d, i;
4449 int d_rounding = 0;
4450 int save_flags = target_flags;
4451
4452 current_function_interrupt
4453 = lookup_attribute ("interrupt_handler",
4454 DECL_ATTRIBUTES (current_function_decl))
4455 != NULL_TREE;
4456
4457 /* We have pretend args if we had an object sent partially in registers
4458 and partially on the stack, e.g. a large structure. */
4459 output_stack_adjust (-current_function_pretend_args_size
4460 - current_function_args_info.stack_regs * 8,
4461 stack_pointer_rtx, TARGET_SH5 ? 0 : 1);
4462
4463 extra_push = 0;
4464
4465 if (TARGET_SHCOMPACT && flag_pic && current_function_args_info.call_cookie)
4466 /* We're going to use the PIC register to load the address of the
4467 incoming-argument decoder and/or of the return trampoline from
4468 the GOT, so make sure the PIC register is preserved and
4469 initialized. */
4470 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
4471
4472 if (TARGET_SHCOMPACT
4473 && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
4474 {
4475 int reg;
4476
4477 /* First, make all registers with incoming arguments that will
4478 be pushed onto the stack live, so that register renaming
4479 doesn't overwrite them. */
4480 for (reg = 0; reg < NPARM_REGS (SImode); reg++)
4481 if (CALL_COOKIE_STACKSEQ_GET (current_function_args_info.call_cookie)
4482 >= NPARM_REGS (SImode) - reg)
4483 for (; reg < NPARM_REGS (SImode); reg++)
4484 emit_insn (gen_shcompact_preserve_incoming_args
4485 (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4486 else if (CALL_COOKIE_INT_REG_GET
4487 (current_function_args_info.call_cookie, reg) == 1)
4488 emit_insn (gen_shcompact_preserve_incoming_args
4489 (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4490
4491 emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
4492 stack_pointer_rtx);
4493 emit_move_insn (gen_rtx_REG (SImode, R0_REG),
4494 GEN_INT (current_function_args_info.call_cookie));
4495 emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
4496 gen_rtx_REG (SImode, R0_REG));
4497 }
4498 else if (TARGET_SHMEDIA)
4499 {
4500 int tr = sh_media_register_for_return ();
4501
4502 if (tr >= 0)
4503 {
4504 rtx insn = emit_move_insn (gen_rtx_REG (DImode, tr),
4505 gen_rtx_REG (DImode, PR_MEDIA_REG));
4506
4507 /* If this function only exits with sibcalls, this copy
4508 will be flagged as dead. */
4509 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4510 const0_rtx,
4511 REG_NOTES (insn));
4512 }
4513 }
4514
4515 /* Emit the code for SETUP_VARARGS. */
4516 if (current_function_varargs || current_function_stdarg)
4517 {
4518 /* This is not used by the SH3E calling convention */
4519 if (TARGET_SH1 && ! TARGET_SH3E && ! TARGET_SH5 && ! TARGET_HITACHI)
4520 {
4521 /* Push arg regs as if they'd been provided by caller in stack. */
4522 for (i = 0; i < NPARM_REGS(SImode); i++)
4523 {
4524 int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
4525 if (i >= (NPARM_REGS(SImode)
4526 - current_function_args_info.arg_count[(int) SH_ARG_INT]
4527 ))
4528 break;
4529 push (rn);
4530 extra_push += 4;
4531 }
4532 }
4533 }
4534
4535 /* If we're supposed to switch stacks at function entry, do so now. */
4536 if (sp_switch)
4537 emit_insn (gen_sp_switch_1 ());
4538
4539 calc_live_regs (&d, live_regs_mask);
4540 /* ??? Maybe we could save some switching if we can move a mode switch
4541 that already happens to be at the function start into the prologue. */
4542 if (target_flags != save_flags)
4543 emit_insn (gen_toggle_sz ());
4544
4545 if (TARGET_SH5)
4546 {
4547 int i;
4548 int offset;
4549 int align;
4550 rtx r0 = gen_rtx_REG (Pmode, R0_REG);
4551 int offset_in_r0 = -1;
4552 int sp_in_r0 = 0;
4553
4554 if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
4555 d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
4556 - d % (STACK_BOUNDARY / BITS_PER_UNIT));
4557
4558 offset = d + d_rounding;
4559 output_stack_adjust (-offset, stack_pointer_rtx, 1);
4560
4561 /* We loop twice: first, we save 8-byte aligned registers in the
4562 higher addresses, that are known to be aligned. Then, we
4563 proceed to saving 32-bit registers that don't need 8-byte
4564 alignment. */
4565 for (align = 1; align >= 0; align--)
4566 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
4567 if (live_regs_mask[i/32] & (1 << (i % 32)))
4568 {
4569 enum machine_mode mode = REGISTER_NATURAL_MODE (i);
4570 int reg = i;
4571 rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
4572
4573 if (mode == SFmode && (i % 2) == 1
4574 && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
4575 && (live_regs_mask[(i ^ 1) / 32] & (1 << ((i ^ 1) % 32))))
4576 {
4577 mode = DFmode;
4578 i--;
4579 reg--;
4580 }
4581
4582 /* If we're doing the aligned pass and this is not aligned,
4583 or we're doing the unaligned pass and this is aligned,
4584 skip it. */
4585 if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
4586 == 0) != align)
4587 continue;
4588
4589 offset -= GET_MODE_SIZE (mode);
4590
4591 reg_rtx = gen_rtx_REG (mode, reg);
4592
4593 mem_rtx = gen_rtx_MEM (mode,
4594 gen_rtx_PLUS (Pmode,
4595 stack_pointer_rtx,
4596 GEN_INT (offset)));
4597
4598 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_pre_dec);
4599
4600 mem_rtx = NULL_RTX;
4601
4602 try_pre_dec:
4603 do
4604 if (HAVE_PRE_DECREMENT
4605 && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
4606 || mem_rtx == NULL_RTX
4607 || i == PR_REG || SPECIAL_REGISTER_P (i)))
4608 {
4609 pre_dec = gen_rtx_MEM (mode,
4610 gen_rtx_PRE_DEC (Pmode, r0));
4611
4612 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (pre_dec, 0),
4613 pre_dec_ok);
4614
4615 pre_dec = NULL_RTX;
4616
4617 break;
4618
4619 pre_dec_ok:
4620 mem_rtx = NULL_RTX;
4621 offset += GET_MODE_SIZE (mode);
4622 }
4623 while (0);
4624
4625 if (mem_rtx != NULL_RTX)
4626 goto addr_ok;
4627
4628 if (offset_in_r0 == -1)
4629 {
4630 emit_move_insn (r0, GEN_INT (offset));
4631 offset_in_r0 = offset;
4632 }
4633 else if (offset != offset_in_r0)
4634 {
4635 emit_move_insn (r0,
4636 gen_rtx_PLUS
4637 (Pmode, r0,
4638 GEN_INT (offset - offset_in_r0)));
4639 offset_in_r0 += offset - offset_in_r0;
4640 }
4641
4642 if (pre_dec != NULL_RTX)
4643 {
4644 if (! sp_in_r0)
4645 {
4646 emit_move_insn (r0,
4647 gen_rtx_PLUS
4648 (Pmode, r0, stack_pointer_rtx));
4649 sp_in_r0 = 1;
4650 }
4651
4652 offset -= GET_MODE_SIZE (mode);
4653 offset_in_r0 -= GET_MODE_SIZE (mode);
4654
4655 mem_rtx = pre_dec;
4656 }
4657 else if (sp_in_r0)
4658 mem_rtx = gen_rtx_MEM (mode, r0);
4659 else
4660 mem_rtx = gen_rtx_MEM (mode,
4661 gen_rtx_PLUS (Pmode,
4662 stack_pointer_rtx,
4663 r0));
4664
4665 /* We must not use an r0-based address for target-branch
4666 registers or for special registers without pre-dec
4667 memory addresses, since we store their values in r0
4668 first. */
4669 if (TARGET_REGISTER_P (i)
4670 || ((i == PR_REG || SPECIAL_REGISTER_P (i))
4671 && mem_rtx != pre_dec))
4672 abort ();
4673
4674 addr_ok:
4675 if (TARGET_REGISTER_P (i)
4676 || ((i == PR_REG || SPECIAL_REGISTER_P (i))
4677 && mem_rtx != pre_dec))
4678 {
4679 rtx r0mode = gen_rtx_REG (GET_MODE (reg_rtx), R0_REG);
4680
4681 emit_move_insn (r0mode, reg_rtx);
4682
4683 offset_in_r0 = -1;
4684 sp_in_r0 = 0;
4685
4686 reg_rtx = r0mode;
4687 }
4688
4689 emit_move_insn (mem_rtx, reg_rtx);
4690 }
4691
4692 if (offset != d_rounding)
4693 abort ();
4694 }
4695 else
4696 push_regs (live_regs_mask);
4697
4698 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
4699 {
4700 rtx insn = get_last_insn ();
4701 rtx last = emit_insn (gen_GOTaddr2picreg ());
4702
4703 /* Mark these insns as possibly dead. Sometimes, flow2 may
4704 delete all uses of the PIC register. In this case, let it
4705 delete the initialization too. */
4706 do
4707 {
4708 insn = NEXT_INSN (insn);
4709
4710 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4711 const0_rtx,
4712 REG_NOTES (insn));
4713 }
4714 while (insn != last);
4715 }
4716
4717 if (SHMEDIA_REGS_STACK_ADJUST ())
4718 {
4719 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4720 gen_rtx_SYMBOL_REF (Pmode,
4721 TARGET_FPU_ANY
4722 ? "__GCC_push_shmedia_regs"
4723 : "__GCC_push_shmedia_regs_nofpu"));
4724 /* This must NOT go through the PLT, otherwise mach and macl
4725 may be clobbered. */
4726 emit_insn (gen_shmedia_save_restore_regs_compact
4727 (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
4728 }
4729
4730 if (target_flags != save_flags)
4731 {
4732 rtx insn = emit_insn (gen_toggle_sz ());
4733
4734 /* If we're lucky, a mode switch in the function body will
4735 overwrite fpscr, turning this insn dead. Tell flow this
4736 insn is ok to delete. */
4737 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4738 const0_rtx,
4739 REG_NOTES (insn));
4740 }
4741
4742 target_flags = save_flags;
4743
4744 output_stack_adjust (-rounded_frame_size (d) + d_rounding,
4745 stack_pointer_rtx, TARGET_SH5 ? 0 : 1);
4746
4747 if (frame_pointer_needed)
4748 emit_insn (GEN_MOV (frame_pointer_rtx, stack_pointer_rtx));
4749
4750 if (TARGET_SHCOMPACT
4751 && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
4752 {
4753 /* This must NOT go through the PLT, otherwise mach and macl
4754 may be clobbered. */
4755 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4756 gen_rtx_SYMBOL_REF (Pmode,
4757 "__GCC_shcompact_incoming_args"));
4758 emit_insn (gen_shcompact_incoming_args ());
4759 }
4760 }
4761
4762 void
4763 sh_expand_epilogue ()
4764 {
4765 HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
4766 int d, i;
4767 int d_rounding = 0;
4768
4769 int save_flags = target_flags;
4770 int frame_size;
4771
4772 calc_live_regs (&d, live_regs_mask);
4773
4774 if (TARGET_SH5 && d % (STACK_BOUNDARY / BITS_PER_UNIT))
4775 d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
4776 - d % (STACK_BOUNDARY / BITS_PER_UNIT));
4777
4778 frame_size = rounded_frame_size (d) - d_rounding;
4779
4780 if (frame_pointer_needed)
4781 {
4782 output_stack_adjust (frame_size, frame_pointer_rtx, 7);
4783
4784 /* We must avoid moving the stack pointer adjustment past code
4785 which reads from the local frame, else an interrupt could
4786 occur after the SP adjustment and clobber data in the local
4787 frame. */
4788 emit_insn (gen_blockage ());
4789 emit_insn (GEN_MOV (stack_pointer_rtx, frame_pointer_rtx));
4790 }
4791 else if (frame_size)
4792 {
4793 /* We must avoid moving the stack pointer adjustment past code
4794 which reads from the local frame, else an interrupt could
4795 occur after the SP adjustment and clobber data in the local
4796 frame. */
4797 emit_insn (gen_blockage ());
4798 output_stack_adjust (frame_size, stack_pointer_rtx, 7);
4799 }
4800
4801 if (SHMEDIA_REGS_STACK_ADJUST ())
4802 {
4803 emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4804 gen_rtx_SYMBOL_REF (Pmode,
4805 TARGET_FPU_ANY
4806 ? "__GCC_pop_shmedia_regs"
4807 : "__GCC_pop_shmedia_regs_nofpu"));
4808 /* This must NOT go through the PLT, otherwise mach and macl
4809 may be clobbered. */
4810 emit_insn (gen_shmedia_save_restore_regs_compact
4811 (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
4812 }
4813
4814 /* Pop all the registers. */
4815
4816 if (target_flags != save_flags)
4817 emit_insn (gen_toggle_sz ());
4818 if (TARGET_SH5)
4819 {
4820 int offset = d_rounding;
4821 int offset_in_r0 = -1;
4822 int sp_in_r0 = 0;
4823 int align;
4824 rtx r0 = gen_rtx_REG (Pmode, R0_REG);
4825
4826 /* We loop twice: first, we save 8-byte aligned registers in the
4827 higher addresses, that are known to be aligned. Then, we
4828 proceed to saving 32-bit registers that don't need 8-byte
4829 alignment. */
4830 for (align = 0; align <= 1; align++)
4831 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4832 if (live_regs_mask[i/32] & (1 << (i % 32)))
4833 {
4834 enum machine_mode mode = REGISTER_NATURAL_MODE (i);
4835 int reg = i;
4836 rtx reg_rtx, mem_rtx, post_inc = NULL_RTX, insn;
4837
4838 if (mode == SFmode && (i % 2) == 0
4839 && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
4840 && (live_regs_mask[(i ^ 1) / 32] & (1 << ((i ^ 1) % 32))))
4841 {
4842 mode = DFmode;
4843 i++;
4844 }
4845
4846 /* If we're doing the aligned pass and this is not aligned,
4847 or we're doing the unaligned pass and this is aligned,
4848 skip it. */
4849 if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
4850 == 0) != align)
4851 continue;
4852
4853 reg_rtx = gen_rtx_REG (mode, reg);
4854
4855 mem_rtx = gen_rtx_MEM (mode,
4856 gen_rtx_PLUS (Pmode,
4857 stack_pointer_rtx,
4858 GEN_INT (offset)));
4859
4860 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_post_inc);
4861
4862 mem_rtx = NULL_RTX;
4863
4864 try_post_inc:
4865 do
4866 if (HAVE_POST_INCREMENT
4867 && (offset == offset_in_r0
4868 || (offset + GET_MODE_SIZE (mode) != d + d_rounding
4869 && mem_rtx == NULL_RTX)
4870 || i == PR_REG || SPECIAL_REGISTER_P (i)))
4871 {
4872 post_inc = gen_rtx_MEM (mode,
4873 gen_rtx_POST_INC (Pmode, r0));
4874
4875 GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (post_inc, 0),
4876 post_inc_ok);
4877
4878 post_inc = NULL_RTX;
4879
4880 break;
4881
4882 post_inc_ok:
4883 mem_rtx = NULL_RTX;
4884 }
4885 while (0);
4886
4887 if (mem_rtx != NULL_RTX)
4888 goto addr_ok;
4889
4890 if (offset_in_r0 == -1)
4891 {
4892 emit_move_insn (r0, GEN_INT (offset));
4893 offset_in_r0 = offset;
4894 }
4895 else if (offset != offset_in_r0)
4896 {
4897 emit_move_insn (r0,
4898 gen_rtx_PLUS
4899 (Pmode, r0,
4900 GEN_INT (offset - offset_in_r0)));
4901 offset_in_r0 += offset - offset_in_r0;
4902 }
4903
4904 if (post_inc != NULL_RTX)
4905 {
4906 if (! sp_in_r0)
4907 {
4908 emit_move_insn (r0,
4909 gen_rtx_PLUS
4910 (Pmode, r0, stack_pointer_rtx));
4911 sp_in_r0 = 1;
4912 }
4913
4914 mem_rtx = post_inc;
4915
4916 offset_in_r0 += GET_MODE_SIZE (mode);
4917 }
4918 else if (sp_in_r0)
4919 mem_rtx = gen_rtx_MEM (mode, r0);
4920 else
4921 mem_rtx = gen_rtx_MEM (mode,
4922 gen_rtx_PLUS (Pmode,
4923 stack_pointer_rtx,
4924 r0));
4925
4926 if ((i == PR_REG || SPECIAL_REGISTER_P (i))
4927 && mem_rtx != post_inc)
4928 abort ();
4929
4930 addr_ok:
4931 if ((i == PR_REG || SPECIAL_REGISTER_P (i))
4932 && mem_rtx != post_inc)
4933 {
4934 insn = emit_move_insn (r0, mem_rtx);
4935 mem_rtx = r0;
4936 }
4937 else if (TARGET_REGISTER_P (i))
4938 {
4939 rtx r1 = gen_rtx_REG (mode, R1_REG);
4940
4941 insn = emit_move_insn (r1, mem_rtx);
4942 mem_rtx = r1;
4943 }
4944
4945 insn = emit_move_insn (reg_rtx, mem_rtx);
4946
4947 offset += GET_MODE_SIZE (mode);
4948 }
4949
4950 if (offset != d + d_rounding)
4951 abort ();
4952
4953 goto finish;
4954 }
4955 else
4956 d = 0;
4957 if (live_regs_mask[PR_REG / 32] & (1 << (PR_REG % 32)))
4958 pop (PR_REG);
4959 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4960 {
4961 int j = (FIRST_PSEUDO_REGISTER - 1) - i;
4962
4963 if (j != PR_REG && live_regs_mask[j / 32] & (1 << (j % 32)))
4964 pop (j);
4965 }
4966 finish:
4967 if (target_flags != save_flags)
4968 emit_insn (gen_toggle_sz ());
4969 target_flags = save_flags;
4970
4971 output_stack_adjust (extra_push + current_function_pretend_args_size
4972 + d + d_rounding
4973 + current_function_args_info.stack_regs * 8,
4974 stack_pointer_rtx, 7);
4975
4976 /* Switch back to the normal stack if necessary. */
4977 if (sp_switch)
4978 emit_insn (gen_sp_switch_2 ());
4979
4980 /* Tell flow the insn that pops PR isn't dead. */
4981 /* PR_REG will never be live in SHmedia mode, and we don't need to
4982 USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
4983 by the return pattern. */
4984 if (live_regs_mask[PR_REG / 32] & (1 << (PR_REG % 32)))
4985 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, PR_REG)));
4986 }
4987
4988 static int sh_need_epilogue_known = 0;
4989
4990 int
4991 sh_need_epilogue ()
4992 {
4993 if (! sh_need_epilogue_known)
4994 {
4995 rtx epilogue;
4996
4997 start_sequence ();
4998 sh_expand_epilogue ();
4999 epilogue = gen_sequence ();
5000 end_sequence ();
5001 sh_need_epilogue_known
5002 = (GET_CODE (epilogue) == SEQUENCE && XVECLEN (epilogue, 0) == 0
5003 ? -1 : 1);
5004 }
5005 return sh_need_epilogue_known > 0;
5006 }
5007
5008 /* Clear variables at function end. */
5009
5010 static void
5011 sh_output_function_epilogue (file, size)
5012 FILE *file ATTRIBUTE_UNUSED;
5013 HOST_WIDE_INT size ATTRIBUTE_UNUSED;
5014 {
5015 trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
5016 sh_need_epilogue_known = 0;
5017 sp_switch = NULL_RTX;
5018 }
5019
5020 rtx
5021 sh_builtin_saveregs ()
5022 {
5023 /* First unnamed integer register. */
5024 int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
5025 /* Number of integer registers we need to save. */
5026 int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
5027 /* First unnamed SFmode float reg */
5028 int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
5029 /* Number of SFmode float regs to save. */
5030 int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
5031 rtx regbuf, fpregs;
5032 int bufsize, regno;
5033 HOST_WIDE_INT alias_set;
5034
5035 if (TARGET_SH5)
5036 {
5037 if (n_intregs)
5038 {
5039 int pushregs = n_intregs;
5040
5041 while (pushregs < NPARM_REGS (SImode) - 1
5042 && (CALL_COOKIE_INT_REG_GET
5043 (current_function_args_info.call_cookie,
5044 NPARM_REGS (SImode) - pushregs)
5045 == 1))
5046 {
5047 current_function_args_info.call_cookie
5048 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
5049 - pushregs, 1);
5050 pushregs++;
5051 }
5052
5053 if (pushregs == NPARM_REGS (SImode))
5054 current_function_args_info.call_cookie
5055 |= (CALL_COOKIE_INT_REG (0, 1)
5056 | CALL_COOKIE_STACKSEQ (pushregs - 1));
5057 else
5058 current_function_args_info.call_cookie
5059 |= CALL_COOKIE_STACKSEQ (pushregs);
5060
5061 current_function_pretend_args_size += 8 * n_intregs;
5062 }
5063 if (TARGET_SHCOMPACT)
5064 return const0_rtx;
5065 }
5066
5067 if (! TARGET_SH3E && ! TARGET_SH4 && ! TARGET_SH5)
5068 {
5069 error ("__builtin_saveregs not supported by this subtarget");
5070 return const0_rtx;
5071 }
5072
5073 if (TARGET_SHMEDIA)
5074 n_floatregs = 0;
5075
5076 /* Allocate block of memory for the regs. */
5077 /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
5078 Or can assign_stack_local accept a 0 SIZE argument? */
5079 bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
5080
5081 if (TARGET_SHMEDIA)
5082 regbuf = gen_rtx_MEM (BLKmode,
5083 gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
5084 else
5085 regbuf = assign_stack_local (BLKmode, bufsize, 0);
5086 alias_set = get_varargs_alias_set ();
5087 set_mem_alias_set (regbuf, alias_set);
5088
5089 /* Save int args.
5090 This is optimized to only save the regs that are necessary. Explicitly
5091 named args need not be saved. */
5092 if (n_intregs > 0)
5093 move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
5094 adjust_address (regbuf, BLKmode,
5095 n_floatregs * UNITS_PER_WORD),
5096 n_intregs, n_intregs * UNITS_PER_WORD);
5097
5098 if (TARGET_SHMEDIA)
5099 /* Return the address of the regbuf. */
5100 return XEXP (regbuf, 0);
5101
5102 /* Save float args.
5103 This is optimized to only save the regs that are necessary. Explicitly
5104 named args need not be saved.
5105 We explicitly build a pointer to the buffer because it halves the insn
5106 count when not optimizing (otherwise the pointer is built for each reg
5107 saved).
5108 We emit the moves in reverse order so that we can use predecrement. */
5109
5110 fpregs = gen_reg_rtx (Pmode);
5111 emit_move_insn (fpregs, XEXP (regbuf, 0));
5112 emit_insn (gen_addsi3 (fpregs, fpregs,
5113 GEN_INT (n_floatregs * UNITS_PER_WORD)));
5114 if (TARGET_SH4)
5115 {
5116 rtx mem;
5117 for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
5118 {
5119 emit_insn (gen_addsi3 (fpregs, fpregs,
5120 GEN_INT (-2 * UNITS_PER_WORD)));
5121 mem = gen_rtx_MEM (DFmode, fpregs);
5122 set_mem_alias_set (mem, alias_set);
5123 emit_move_insn (mem,
5124 gen_rtx (REG, DFmode, BASE_ARG_REG (DFmode) + regno));
5125 }
5126 regno = first_floatreg;
5127 if (regno & 1)
5128 {
5129 emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5130 mem = gen_rtx_MEM (SFmode, fpregs);
5131 set_mem_alias_set (mem, alias_set);
5132 emit_move_insn (mem,
5133 gen_rtx (REG, SFmode, BASE_ARG_REG (SFmode) + regno
5134 - (TARGET_LITTLE_ENDIAN != 0)));
5135 }
5136 }
5137 else
5138 for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
5139 {
5140 rtx mem;
5141
5142 emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5143 mem = gen_rtx_MEM (SFmode, fpregs);
5144 set_mem_alias_set (mem, alias_set);
5145 emit_move_insn (mem,
5146 gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
5147 }
5148
5149 /* Return the address of the regbuf. */
5150 return XEXP (regbuf, 0);
5151 }
5152
5153 /* Define the `__builtin_va_list' type for the ABI. */
5154
5155 tree
5156 sh_build_va_list ()
5157 {
5158 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5159 tree record;
5160
5161 if (TARGET_SH5 || (! TARGET_SH3E && ! TARGET_SH4) || TARGET_HITACHI)
5162 return ptr_type_node;
5163
5164 record = make_node (RECORD_TYPE);
5165
5166 f_next_o = build_decl (FIELD_DECL, get_identifier ("__va_next_o"),
5167 ptr_type_node);
5168 f_next_o_limit = build_decl (FIELD_DECL,
5169 get_identifier ("__va_next_o_limit"),
5170 ptr_type_node);
5171 f_next_fp = build_decl (FIELD_DECL, get_identifier ("__va_next_fp"),
5172 ptr_type_node);
5173 f_next_fp_limit = build_decl (FIELD_DECL,
5174 get_identifier ("__va_next_fp_limit"),
5175 ptr_type_node);
5176 f_next_stack = build_decl (FIELD_DECL, get_identifier ("__va_next_stack"),
5177 ptr_type_node);
5178
5179 DECL_FIELD_CONTEXT (f_next_o) = record;
5180 DECL_FIELD_CONTEXT (f_next_o_limit) = record;
5181 DECL_FIELD_CONTEXT (f_next_fp) = record;
5182 DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
5183 DECL_FIELD_CONTEXT (f_next_stack) = record;
5184
5185 TYPE_FIELDS (record) = f_next_o;
5186 TREE_CHAIN (f_next_o) = f_next_o_limit;
5187 TREE_CHAIN (f_next_o_limit) = f_next_fp;
5188 TREE_CHAIN (f_next_fp) = f_next_fp_limit;
5189 TREE_CHAIN (f_next_fp_limit) = f_next_stack;
5190
5191 layout_type (record);
5192
5193 return record;
5194 }
5195
5196 /* Implement `va_start' for varargs and stdarg. */
5197
5198 void
5199 sh_va_start (stdarg_p, valist, nextarg)
5200 int stdarg_p;
5201 tree valist;
5202 rtx nextarg;
5203 {
5204 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5205 tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5206 tree t, u;
5207 int nfp, nint;
5208
5209 if (TARGET_SH5)
5210 {
5211 expand_builtin_saveregs ();
5212 /* When the varargs dummy argument is ``passed'' on a register,
5213 we don't want std_expand_builtin_va_start() to apply any
5214 correction for it, so set stdarg_p so as to pretend there's
5215 no such dummy argument. */
5216 if (current_function_args_info.arg_count[(int) SH_ARG_INT]
5217 < NPARM_REGS (SImode))
5218 stdarg_p = 1;
5219 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
5220 return;
5221 }
5222
5223 if ((! TARGET_SH3E && ! TARGET_SH4) || TARGET_HITACHI)
5224 {
5225 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
5226 return;
5227 }
5228
5229 f_next_o = TYPE_FIELDS (va_list_type_node);
5230 f_next_o_limit = TREE_CHAIN (f_next_o);
5231 f_next_fp = TREE_CHAIN (f_next_o_limit);
5232 f_next_fp_limit = TREE_CHAIN (f_next_fp);
5233 f_next_stack = TREE_CHAIN (f_next_fp_limit);
5234
5235 next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5236 next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5237 valist, f_next_o_limit);
5238 next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp);
5239 next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5240 valist, f_next_fp_limit);
5241 next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5242 valist, f_next_stack);
5243
5244 /* Call __builtin_saveregs. */
5245 u = make_tree (ptr_type_node, expand_builtin_saveregs ());
5246 t = build (MODIFY_EXPR, ptr_type_node, next_fp, u);
5247 TREE_SIDE_EFFECTS (t) = 1;
5248 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5249
5250 nfp = current_function_args_info.arg_count[SH_ARG_FLOAT];
5251 if (nfp < 8)
5252 nfp = 8 - nfp;
5253 else
5254 nfp = 0;
5255 u = fold (build (PLUS_EXPR, ptr_type_node, u,
5256 build_int_2 (UNITS_PER_WORD * nfp, 0)));
5257 t = build (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
5258 TREE_SIDE_EFFECTS (t) = 1;
5259 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5260
5261 t = build (MODIFY_EXPR, ptr_type_node, next_o, u);
5262 TREE_SIDE_EFFECTS (t) = 1;
5263 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5264
5265 nint = current_function_args_info.arg_count[SH_ARG_INT];
5266 if (nint < 4)
5267 nint = 4 - nint;
5268 else
5269 nint = 0;
5270 u = fold (build (PLUS_EXPR, ptr_type_node, u,
5271 build_int_2 (UNITS_PER_WORD * nint, 0)));
5272 t = build (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
5273 TREE_SIDE_EFFECTS (t) = 1;
5274 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5275
5276 u = make_tree (ptr_type_node, nextarg);
5277 if (! stdarg_p && (nint == 0 || nfp == 0))
5278 {
5279 u = fold (build (PLUS_EXPR, ptr_type_node, u,
5280 build_int_2 (-UNITS_PER_WORD, -1)));
5281 }
5282 t = build (MODIFY_EXPR, ptr_type_node, next_stack, u);
5283 TREE_SIDE_EFFECTS (t) = 1;
5284 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5285 }
5286
5287 /* Implement `va_arg'. */
5288
5289 rtx
5290 sh_va_arg (valist, type)
5291 tree valist, type;
5292 {
5293 HOST_WIDE_INT size, rsize;
5294 tree tmp, pptr_type_node;
5295 rtx addr_rtx, r;
5296
5297 size = int_size_in_bytes (type);
5298 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5299 pptr_type_node = build_pointer_type (ptr_type_node);
5300
5301 if (! TARGET_SH5 && (TARGET_SH3E || TARGET_SH4) && ! TARGET_HITACHI)
5302 {
5303 tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5304 tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5305 int pass_as_float;
5306 rtx lab_false, lab_over;
5307
5308 f_next_o = TYPE_FIELDS (va_list_type_node);
5309 f_next_o_limit = TREE_CHAIN (f_next_o);
5310 f_next_fp = TREE_CHAIN (f_next_o_limit);
5311 f_next_fp_limit = TREE_CHAIN (f_next_fp);
5312 f_next_stack = TREE_CHAIN (f_next_fp_limit);
5313
5314 next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5315 next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5316 valist, f_next_o_limit);
5317 next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp),
5318 valist, f_next_fp);
5319 next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5320 valist, f_next_fp_limit);
5321 next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5322 valist, f_next_stack);
5323
5324 if (TARGET_SH4)
5325 {
5326 pass_as_float = ((TREE_CODE (type) == REAL_TYPE && size <= 8)
5327 || (TREE_CODE (type) == COMPLEX_TYPE
5328 && TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
5329 && size <= 16));
5330 }
5331 else
5332 {
5333 pass_as_float = (TREE_CODE (type) == REAL_TYPE && size == 4);
5334 }
5335
5336 addr_rtx = gen_reg_rtx (Pmode);
5337 lab_false = gen_label_rtx ();
5338 lab_over = gen_label_rtx ();
5339
5340 if (pass_as_float)
5341 {
5342 emit_cmp_and_jump_insns (expand_expr (next_fp, NULL_RTX, Pmode,
5343 EXPAND_NORMAL),
5344 expand_expr (next_fp_limit, NULL_RTX,
5345 Pmode, EXPAND_NORMAL),
5346 GE, const1_rtx, Pmode, 1, lab_false);
5347
5348 if (TYPE_ALIGN (type) > BITS_PER_WORD)
5349 {
5350 tmp = build (BIT_AND_EXPR, ptr_type_node, next_fp,
5351 build_int_2 (UNITS_PER_WORD, 0));
5352 tmp = build (PLUS_EXPR, ptr_type_node, next_fp, tmp);
5353 tmp = build (MODIFY_EXPR, ptr_type_node, next_fp, tmp);
5354 TREE_SIDE_EFFECTS (tmp) = 1;
5355 expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5356 }
5357
5358 tmp = build1 (ADDR_EXPR, pptr_type_node, next_fp);
5359 r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5360 if (r != addr_rtx)
5361 emit_move_insn (addr_rtx, r);
5362
5363 emit_jump_insn (gen_jump (lab_over));
5364 emit_barrier ();
5365 emit_label (lab_false);
5366
5367 tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
5368 r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5369 if (r != addr_rtx)
5370 emit_move_insn (addr_rtx, r);
5371 }
5372 else
5373 {
5374 tmp = build (PLUS_EXPR, ptr_type_node, next_o,
5375 build_int_2 (rsize, 0));
5376
5377 emit_cmp_and_jump_insns (expand_expr (tmp, NULL_RTX, Pmode,
5378 EXPAND_NORMAL),
5379 expand_expr (next_o_limit, NULL_RTX,
5380 Pmode, EXPAND_NORMAL),
5381 GT, const1_rtx, Pmode, 1, lab_false);
5382
5383 tmp = build1 (ADDR_EXPR, pptr_type_node, next_o);
5384 r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5385 if (r != addr_rtx)
5386 emit_move_insn (addr_rtx, r);
5387
5388 emit_jump_insn (gen_jump (lab_over));
5389 emit_barrier ();
5390 emit_label (lab_false);
5391
5392 if (size > 4 && ! TARGET_SH4)
5393 {
5394 tmp = build (MODIFY_EXPR, ptr_type_node, next_o, next_o_limit);
5395 TREE_SIDE_EFFECTS (tmp) = 1;
5396 expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5397 }
5398
5399 tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
5400 r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5401 if (r != addr_rtx)
5402 emit_move_insn (addr_rtx, r);
5403 }
5404
5405 emit_label (lab_over);
5406
5407 tmp = make_tree (pptr_type_node, addr_rtx);
5408 valist = build1 (INDIRECT_REF, ptr_type_node, tmp);
5409 }
5410
5411 /* ??? In va-sh.h, there had been code to make values larger than
5412 size 8 indirect. This does not match the FUNCTION_ARG macros. */
5413
5414 return std_expand_builtin_va_arg (valist, type);
5415 }
5416
5417 /* Define the offset between two registers, one to be eliminated, and
5418 the other its replacement, at the start of a routine. */
5419
5420 int
5421 initial_elimination_offset (from, to)
5422 int from;
5423 int to;
5424 {
5425 int regs_saved;
5426 int regs_saved_rounding = 0;
5427 int total_saved_regs_space;
5428 int total_auto_space;
5429 int save_flags = target_flags;
5430 int copy_flags;
5431
5432 HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
5433 calc_live_regs (&regs_saved, live_regs_mask);
5434 regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
5435 if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
5436 regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5437 - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
5438
5439 total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
5440 copy_flags = target_flags;
5441 target_flags = save_flags;
5442
5443 total_saved_regs_space = regs_saved + regs_saved_rounding;
5444
5445 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
5446 return total_saved_regs_space + total_auto_space
5447 + current_function_args_info.byref_regs * 8;
5448
5449 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
5450 return total_saved_regs_space + total_auto_space
5451 + current_function_args_info.byref_regs * 8;
5452
5453 /* Initial gap between fp and sp is 0. */
5454 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
5455 return 0;
5456
5457 if (from == RETURN_ADDRESS_POINTER_REGNUM
5458 && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
5459 if (TARGET_SH5)
5460 {
5461 int i, n = total_saved_regs_space;
5462 int align;
5463 int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
5464
5465 n += total_auto_space;
5466
5467 /* If it wasn't saved, there's not much we can do. */
5468 if ((live_regs_mask[pr_reg / 32] & (1 << (pr_reg % 32))) == 0)
5469 return n;
5470
5471 target_flags = copy_flags;
5472
5473 /* We loop twice: first, check 8-byte aligned registers,
5474 that are stored in the higher addresses, that are known
5475 to be aligned. Then, check 32-bit registers that don't
5476 need 8-byte alignment. */
5477 for (align = 1; align >= 0; align--)
5478 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
5479 if (live_regs_mask[i/32] & (1 << (i % 32)))
5480 {
5481 enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5482
5483 if (mode == SFmode && (i % 2) == 1
5484 && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5485 && (live_regs_mask[(i ^ 1) / 32]
5486 & (1 << ((i ^ 1) % 32))))
5487 {
5488 mode = DFmode;
5489 i--;
5490 }
5491
5492 /* If we're doing the aligned pass and this is not aligned,
5493 or we're doing the unaligned pass and this is aligned,
5494 skip it. */
5495 if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
5496 == 0) != align)
5497 continue;
5498
5499 n -= GET_MODE_SIZE (mode);
5500
5501 if (i == pr_reg)
5502 {
5503 target_flags = save_flags;
5504 return n;
5505 }
5506 }
5507
5508 abort ();
5509 }
5510 else
5511 return total_auto_space;
5512
5513 abort ();
5514 }
5515 \f
5516 /* Handle machine specific pragmas to be semi-compatible with Hitachi
5517 compiler. */
5518
5519 void
5520 sh_pr_interrupt (pfile)
5521 cpp_reader *pfile ATTRIBUTE_UNUSED;
5522 {
5523 pragma_interrupt = 1;
5524 }
5525
5526 void
5527 sh_pr_trapa (pfile)
5528 cpp_reader *pfile ATTRIBUTE_UNUSED;
5529 {
5530 pragma_interrupt = pragma_trapa = 1;
5531 }
5532
5533 void
5534 sh_pr_nosave_low_regs (pfile)
5535 cpp_reader *pfile ATTRIBUTE_UNUSED;
5536 {
5537 pragma_nosave_low_regs = 1;
5538 }
5539
5540 /* Generate 'handle_interrupt' attribute for decls */
5541
5542 static void
5543 sh_insert_attributes (node, attributes)
5544 tree node;
5545 tree * attributes;
5546 {
5547 if (! pragma_interrupt
5548 || TREE_CODE (node) != FUNCTION_DECL)
5549 return;
5550
5551 /* We are only interested in fields. */
5552 if (TREE_CODE_CLASS (TREE_CODE (node)) != 'd')
5553 return;
5554
5555 /* Add a 'handle_interrupt' attribute. */
5556 * attributes = tree_cons (get_identifier ("interrupt_handler"), NULL, * attributes);
5557
5558 return;
5559 }
5560
5561 /* Supported attributes:
5562
5563 interrupt_handler -- specifies this function is an interrupt handler.
5564
5565 sp_switch -- specifies an alternate stack for an interrupt handler
5566 to run on.
5567
5568 trap_exit -- use a trapa to exit an interrupt function instead of
5569 an rte instruction. */
5570
5571 const struct attribute_spec sh_attribute_table[] =
5572 {
5573 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
5574 { "interrupt_handler", 0, 0, true, false, false, sh_handle_interrupt_handler_attribute },
5575 { "sp_switch", 1, 1, true, false, false, sh_handle_sp_switch_attribute },
5576 { "trap_exit", 1, 1, true, false, false, sh_handle_trap_exit_attribute },
5577 { NULL, 0, 0, false, false, false, NULL }
5578 };
5579
5580 /* Handle an "interrupt_handler" attribute; arguments as in
5581 struct attribute_spec.handler. */
5582 static tree
5583 sh_handle_interrupt_handler_attribute (node, name, args, flags, no_add_attrs)
5584 tree *node;
5585 tree name;
5586 tree args ATTRIBUTE_UNUSED;
5587 int flags ATTRIBUTE_UNUSED;
5588 bool *no_add_attrs;
5589 {
5590 if (TREE_CODE (*node) != FUNCTION_DECL)
5591 {
5592 warning ("`%s' attribute only applies to functions",
5593 IDENTIFIER_POINTER (name));
5594 *no_add_attrs = true;
5595 }
5596
5597 return NULL_TREE;
5598 }
5599
5600 /* Handle an "sp_switch" attribute; arguments as in
5601 struct attribute_spec.handler. */
5602 static tree
5603 sh_handle_sp_switch_attribute (node, name, args, flags, no_add_attrs)
5604 tree *node;
5605 tree name;
5606 tree args;
5607 int flags ATTRIBUTE_UNUSED;
5608 bool *no_add_attrs;
5609 {
5610 if (TREE_CODE (*node) != FUNCTION_DECL)
5611 {
5612 warning ("`%s' attribute only applies to functions",
5613 IDENTIFIER_POINTER (name));
5614 *no_add_attrs = true;
5615 }
5616 else if (!pragma_interrupt)
5617 {
5618 /* The sp_switch attribute only has meaning for interrupt functions. */
5619 warning ("`%s' attribute only applies to interrupt functions",
5620 IDENTIFIER_POINTER (name));
5621 *no_add_attrs = true;
5622 }
5623 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5624 {
5625 /* The argument must be a constant string. */
5626 warning ("`%s' attribute argument not a string constant",
5627 IDENTIFIER_POINTER (name));
5628 *no_add_attrs = true;
5629 }
5630 else
5631 {
5632 sp_switch = gen_rtx_SYMBOL_REF (VOIDmode,
5633 TREE_STRING_POINTER (TREE_VALUE (args)));
5634 }
5635
5636 return NULL_TREE;
5637 }
5638
5639 /* Handle an "trap_exit" attribute; arguments as in
5640 struct attribute_spec.handler. */
5641 static tree
5642 sh_handle_trap_exit_attribute (node, name, args, flags, no_add_attrs)
5643 tree *node;
5644 tree name;
5645 tree args;
5646 int flags ATTRIBUTE_UNUSED;
5647 bool *no_add_attrs;
5648 {
5649 if (TREE_CODE (*node) != FUNCTION_DECL)
5650 {
5651 warning ("`%s' attribute only applies to functions",
5652 IDENTIFIER_POINTER (name));
5653 *no_add_attrs = true;
5654 }
5655 else if (!pragma_interrupt)
5656 {
5657 /* The trap_exit attribute only has meaning for interrupt functions. */
5658 warning ("`%s' attribute only applies to interrupt functions",
5659 IDENTIFIER_POINTER (name));
5660 *no_add_attrs = true;
5661 }
5662 else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
5663 {
5664 /* The argument must be a constant integer. */
5665 warning ("`%s' attribute argument not an integer constant",
5666 IDENTIFIER_POINTER (name));
5667 *no_add_attrs = true;
5668 }
5669 else
5670 {
5671 trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
5672 }
5673
5674 return NULL_TREE;
5675 }
5676
5677 \f
5678 /* Predicates used by the templates. */
5679
5680 /* Returns 1 if OP is MACL, MACH or PR. The input must be a REG rtx.
5681 Used only in general_movsrc_operand. */
5682
5683 int
5684 system_reg_operand (op, mode)
5685 rtx op;
5686 enum machine_mode mode ATTRIBUTE_UNUSED;
5687 {
5688 switch (REGNO (op))
5689 {
5690 case PR_REG:
5691 case MACL_REG:
5692 case MACH_REG:
5693 return 1;
5694 }
5695 return 0;
5696 }
5697
5698 /* Returns 1 if OP can be source of a simple move operation.
5699 Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
5700 invalid as are subregs of system registers. */
5701
5702 int
5703 general_movsrc_operand (op, mode)
5704 rtx op;
5705 enum machine_mode mode;
5706 {
5707 if (GET_CODE (op) == MEM)
5708 {
5709 rtx inside = XEXP (op, 0);
5710 if (GET_CODE (inside) == CONST)
5711 inside = XEXP (inside, 0);
5712
5713 if (GET_CODE (inside) == LABEL_REF)
5714 return 1;
5715
5716 if (GET_CODE (inside) == PLUS
5717 && GET_CODE (XEXP (inside, 0)) == LABEL_REF
5718 && GET_CODE (XEXP (inside, 1)) == CONST_INT)
5719 return 1;
5720
5721 /* Only post inc allowed. */
5722 if (GET_CODE (inside) == PRE_DEC)
5723 return 0;
5724 }
5725
5726 if ((mode == QImode || mode == HImode)
5727 && (GET_CODE (op) == SUBREG
5728 && GET_CODE (XEXP (op, 0)) == REG
5729 && system_reg_operand (XEXP (op, 0), mode)))
5730 return 0;
5731
5732 return general_operand (op, mode);
5733 }
5734
5735 /* Returns 1 if OP can be a destination of a move.
5736 Same as general_operand, but no preinc allowed. */
5737
5738 int
5739 general_movdst_operand (op, mode)
5740 rtx op;
5741 enum machine_mode mode;
5742 {
5743 /* Only pre dec allowed. */
5744 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
5745 return 0;
5746
5747 return general_operand (op, mode);
5748 }
5749
5750 /* Accept a register, but not a subreg of any kind. This allows us to
5751 avoid pathological cases in reload wrt data movement common in
5752 int->fp conversion. */
5753
5754 int
5755 reg_no_subreg_operand (op, mode)
5756 register rtx op;
5757 enum machine_mode mode;
5758 {
5759 if (GET_CODE (op) == SUBREG)
5760 return 0;
5761 return register_operand (op, mode);
5762 }
5763
5764 /* Returns 1 if OP is a normal arithmetic register. */
5765
5766 int
5767 arith_reg_operand (op, mode)
5768 rtx op;
5769 enum machine_mode mode;
5770 {
5771 if (register_operand (op, mode))
5772 {
5773 int regno;
5774
5775 if (GET_CODE (op) == REG)
5776 regno = REGNO (op);
5777 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
5778 regno = REGNO (SUBREG_REG (op));
5779 else
5780 return 1;
5781
5782 return (regno != T_REG && regno != PR_REG
5783 && ! TARGET_REGISTER_P (regno)
5784 && (regno != FPUL_REG || TARGET_SH4)
5785 && regno != MACH_REG && regno != MACL_REG);
5786 }
5787 return 0;
5788 }
5789
5790 int
5791 fp_arith_reg_operand (op, mode)
5792 rtx op;
5793 enum machine_mode mode;
5794 {
5795 if (register_operand (op, mode))
5796 {
5797 int regno;
5798
5799 if (GET_CODE (op) == REG)
5800 regno = REGNO (op);
5801 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
5802 regno = REGNO (SUBREG_REG (op));
5803 else
5804 return 1;
5805
5806 return (regno >= FIRST_PSEUDO_REGISTER
5807 || FP_REGISTER_P (regno));
5808 }
5809 return 0;
5810 }
5811
5812 /* Returns 1 if OP is a valid source operand for an arithmetic insn. */
5813
5814 int
5815 arith_operand (op, mode)
5816 rtx op;
5817 enum machine_mode mode;
5818 {
5819 if (arith_reg_operand (op, mode))
5820 return 1;
5821
5822 if (TARGET_SHMEDIA)
5823 {
5824 /* FIXME: We should be checking whether the CONST_INT fits in a
5825 CONST_OK_FOR_J here, but this causes reload_cse to crash when
5826 attempting to transform a sequence of two 64-bit sets of the
5827 same register from literal constants into a set and an add,
5828 when the difference is too wide for an add. */
5829 if (GET_CODE (op) == CONST_INT
5830 || EXTRA_CONSTRAINT_S (op))
5831 return 1;
5832 else
5833 return 0;
5834 }
5835 else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
5836 return 1;
5837
5838 return 0;
5839 }
5840
5841 /* Returns 1 if OP is a valid source operand for a compare insn. */
5842
5843 int
5844 arith_reg_or_0_operand (op, mode)
5845 rtx op;
5846 enum machine_mode mode;
5847 {
5848 if (arith_reg_operand (op, mode))
5849 return 1;
5850
5851 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
5852 return 1;
5853
5854 return 0;
5855 }
5856
5857 /* Return 1 if OP is a valid source operand for an SHmedia operation
5858 that takes either a register or a 6-bit immediate. */
5859
5860 int
5861 shmedia_6bit_operand (op, mode)
5862 rtx op;
5863 enum machine_mode mode;
5864 {
5865 return (arith_reg_operand (op, mode)
5866 || (GET_CODE (op) == CONST_INT && CONST_OK_FOR_O (INTVAL (op))));
5867 }
5868
5869 /* Returns 1 if OP is a valid source operand for a logical operation. */
5870
5871 int
5872 logical_operand (op, mode)
5873 rtx op;
5874 enum machine_mode mode;
5875 {
5876 if (arith_reg_operand (op, mode))
5877 return 1;
5878
5879 if (TARGET_SHMEDIA)
5880 {
5881 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_P (INTVAL (op)))
5882 return 1;
5883 else
5884 return 0;
5885 }
5886 else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
5887 return 1;
5888
5889 return 0;
5890 }
5891
5892 /* Nonzero if OP is a floating point value with value 0.0. */
5893
5894 int
5895 fp_zero_operand (op)
5896 rtx op;
5897 {
5898 REAL_VALUE_TYPE r;
5899
5900 if (GET_MODE (op) != SFmode)
5901 return 0;
5902
5903 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
5904 return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
5905 }
5906
5907 /* Nonzero if OP is a floating point value with value 1.0. */
5908
5909 int
5910 fp_one_operand (op)
5911 rtx op;
5912 {
5913 REAL_VALUE_TYPE r;
5914
5915 if (GET_MODE (op) != SFmode)
5916 return 0;
5917
5918 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
5919 return REAL_VALUES_EQUAL (r, dconst1);
5920 }
5921
5922 /* For -m4 and -m4-single-only, mode switching is used. If we are
5923 compiling without -mfmovd, movsf_ie isn't taken into account for
5924 mode switching. We could check in machine_dependent_reorg for
5925 cases where we know we are in single precision mode, but there is
5926 interface to find that out during reload, so we must avoid
5927 choosing an fldi alternative during reload and thus failing to
5928 allocate a scratch register for the constant loading. */
5929 int
5930 fldi_ok ()
5931 {
5932 return ! TARGET_SH4 || TARGET_FMOVD || reload_completed;
5933 }
5934
5935 int
5936 tertiary_reload_operand (op, mode)
5937 rtx op;
5938 enum machine_mode mode ATTRIBUTE_UNUSED;
5939 {
5940 enum rtx_code code = GET_CODE (op);
5941 return code == MEM || (TARGET_SH4 && code == CONST_DOUBLE);
5942 }
5943
5944 int
5945 fpscr_operand (op, mode)
5946 rtx op;
5947 enum machine_mode mode ATTRIBUTE_UNUSED;
5948 {
5949 return (GET_CODE (op) == REG && REGNO (op) == FPSCR_REG
5950 && GET_MODE (op) == PSImode);
5951 }
5952
5953 int
5954 fpul_operand (op, mode)
5955 rtx op;
5956 enum machine_mode mode;
5957 {
5958 if (TARGET_SHMEDIA)
5959 return fp_arith_reg_operand (op, mode);
5960
5961 return (GET_CODE (op) == REG
5962 && (REGNO (op) == FPUL_REG || REGNO (op) >= FIRST_PSEUDO_REGISTER)
5963 && GET_MODE (op) == mode);
5964 }
5965
5966 int
5967 symbol_ref_operand (op, mode)
5968 rtx op;
5969 enum machine_mode mode ATTRIBUTE_UNUSED;
5970 {
5971 return (GET_CODE (op) == SYMBOL_REF);
5972 }
5973
5974 int
5975 commutative_float_operator (op, mode)
5976 rtx op;
5977 enum machine_mode mode;
5978 {
5979 if (GET_MODE (op) != mode)
5980 return 0;
5981 switch (GET_CODE (op))
5982 {
5983 case PLUS:
5984 case MULT:
5985 return 1;
5986 default:
5987 break;
5988 }
5989 return 0;
5990 }
5991
5992 int
5993 noncommutative_float_operator (op, mode)
5994 rtx op;
5995 enum machine_mode mode;
5996 {
5997 if (GET_MODE (op) != mode)
5998 return 0;
5999 switch (GET_CODE (op))
6000 {
6001 case MINUS:
6002 case DIV:
6003 return 1;
6004 default:
6005 break;
6006 }
6007 return 0;
6008 }
6009
6010 int
6011 binary_float_operator (op, mode)
6012 rtx op;
6013 enum machine_mode mode;
6014 {
6015 if (GET_MODE (op) != mode)
6016 return 0;
6017 switch (GET_CODE (op))
6018 {
6019 case PLUS:
6020 case MINUS:
6021 case MULT:
6022 case DIV:
6023 return 1;
6024 default:
6025 break;
6026 }
6027 return 0;
6028 }
6029
6030 /* Accept pseudos and branch target registers. */
6031 int
6032 target_reg_operand (op, mode)
6033 rtx op;
6034 enum machine_mode mode;
6035 {
6036 if (mode != DImode
6037 || GET_MODE (op) != DImode)
6038 return 0;
6039
6040 if (GET_CODE (op) == SUBREG)
6041 op = XEXP (op, 0);
6042
6043 if (GET_CODE (op) != REG)
6044 return 0;
6045
6046 /* We must protect ourselves from matching pseudos that are virtual
6047 register, because they will eventually be replaced with hardware
6048 registers that aren't branch-target registers. */
6049 if (REGNO (op) > LAST_VIRTUAL_REGISTER
6050 || TARGET_REGISTER_P (REGNO (op)))
6051 return 1;
6052
6053 return 0;
6054 }
6055
6056 /* Same as target_reg_operand, except that label_refs and symbol_refs
6057 are accepted before reload. */
6058 int
6059 target_operand (op, mode)
6060 rtx op;
6061 enum machine_mode mode;
6062 {
6063 if (mode != DImode)
6064 return 0;
6065
6066 if ((GET_MODE (op) == DImode || GET_MODE (op) == VOIDmode)
6067 && EXTRA_CONSTRAINT_T (op))
6068 return ! reload_completed;
6069
6070 return target_reg_operand (op, mode);
6071 }
6072
6073 \f
6074 /* Return the destination address of a branch. */
6075
6076 static int
6077 branch_dest (branch)
6078 rtx branch;
6079 {
6080 rtx dest = SET_SRC (PATTERN (branch));
6081 int dest_uid;
6082
6083 if (GET_CODE (dest) == IF_THEN_ELSE)
6084 dest = XEXP (dest, 1);
6085 dest = XEXP (dest, 0);
6086 dest_uid = INSN_UID (dest);
6087 return INSN_ADDRESSES (dest_uid);
6088 }
6089 \f
6090 /* Return non-zero if REG is not used after INSN.
6091 We assume REG is a reload reg, and therefore does
6092 not live past labels. It may live past calls or jumps though. */
6093 int
6094 reg_unused_after (reg, insn)
6095 rtx reg;
6096 rtx insn;
6097 {
6098 enum rtx_code code;
6099 rtx set;
6100
6101 /* If the reg is set by this instruction, then it is safe for our
6102 case. Disregard the case where this is a store to memory, since
6103 we are checking a register used in the store address. */
6104 set = single_set (insn);
6105 if (set && GET_CODE (SET_DEST (set)) != MEM
6106 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6107 return 1;
6108
6109 while ((insn = NEXT_INSN (insn)))
6110 {
6111 code = GET_CODE (insn);
6112
6113 #if 0
6114 /* If this is a label that existed before reload, then the register
6115 if dead here. However, if this is a label added by reorg, then
6116 the register may still be live here. We can't tell the difference,
6117 so we just ignore labels completely. */
6118 if (code == CODE_LABEL)
6119 return 1;
6120 /* else */
6121 #endif
6122
6123 if (code == JUMP_INSN)
6124 return 0;
6125
6126 /* If this is a sequence, we must handle them all at once.
6127 We could have for instance a call that sets the target register,
6128 and an insn in a delay slot that uses the register. In this case,
6129 we must return 0. */
6130 else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
6131 {
6132 int i;
6133 int retval = 0;
6134
6135 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
6136 {
6137 rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
6138 rtx set = single_set (this_insn);
6139
6140 if (GET_CODE (this_insn) == CALL_INSN)
6141 code = CALL_INSN;
6142 else if (GET_CODE (this_insn) == JUMP_INSN)
6143 {
6144 if (INSN_ANNULLED_BRANCH_P (this_insn))
6145 return 0;
6146 code = JUMP_INSN;
6147 }
6148
6149 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
6150 return 0;
6151 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6152 {
6153 if (GET_CODE (SET_DEST (set)) != MEM)
6154 retval = 1;
6155 else
6156 return 0;
6157 }
6158 if (set == 0
6159 && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
6160 return 0;
6161 }
6162 if (retval == 1)
6163 return 1;
6164 else if (code == JUMP_INSN)
6165 return 0;
6166 }
6167 else if (GET_RTX_CLASS (code) == 'i')
6168 {
6169 rtx set = single_set (insn);
6170
6171 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
6172 return 0;
6173 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6174 return GET_CODE (SET_DEST (set)) != MEM;
6175 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
6176 return 0;
6177 }
6178
6179 if (code == CALL_INSN && call_used_regs[REGNO (reg)])
6180 return 1;
6181 }
6182 return 1;
6183 }
6184 \f
6185 #include "ggc.h"
6186
6187 rtx
6188 get_fpscr_rtx ()
6189 {
6190 static rtx fpscr_rtx;
6191
6192 if (! fpscr_rtx)
6193 {
6194 fpscr_rtx = gen_rtx (REG, PSImode, FPSCR_REG);
6195 REG_USERVAR_P (fpscr_rtx) = 1;
6196 ggc_add_rtx_root (&fpscr_rtx, 1);
6197 mark_user_reg (fpscr_rtx);
6198 }
6199 if (! reload_completed || mdep_reorg_phase != SH_AFTER_MDEP_REORG)
6200 mark_user_reg (fpscr_rtx);
6201 return fpscr_rtx;
6202 }
6203
6204 void
6205 emit_sf_insn (pat)
6206 rtx pat;
6207 {
6208 emit_insn (pat);
6209 }
6210
6211 void
6212 emit_df_insn (pat)
6213 rtx pat;
6214 {
6215 emit_insn (pat);
6216 }
6217
6218 void
6219 expand_sf_unop (fun, operands)
6220 rtx (*fun) PARAMS ((rtx, rtx, rtx));
6221 rtx *operands;
6222 {
6223 emit_sf_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
6224 }
6225
6226 void
6227 expand_sf_binop (fun, operands)
6228 rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
6229 rtx *operands;
6230 {
6231 emit_sf_insn ((*fun) (operands[0], operands[1], operands[2],
6232 get_fpscr_rtx ()));
6233 }
6234
6235 void
6236 expand_df_unop (fun, operands)
6237 rtx (*fun) PARAMS ((rtx, rtx, rtx));
6238 rtx *operands;
6239 {
6240 emit_df_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
6241 }
6242
6243 void
6244 expand_df_binop (fun, operands)
6245 rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
6246 rtx *operands;
6247 {
6248 emit_df_insn ((*fun) (operands[0], operands[1], operands[2],
6249 get_fpscr_rtx ()));
6250 }
6251 \f
6252 /* ??? gcc does flow analysis strictly after common subexpression
6253 elimination. As a result, common subespression elimination fails
6254 when there are some intervening statements setting the same register.
6255 If we did nothing about this, this would hurt the precision switching
6256 for SH4 badly. There is some cse after reload, but it is unable to
6257 undo the extra register pressure from the unused instructions, and
6258 it cannot remove auto-increment loads.
6259
6260 A C code example that shows this flow/cse weakness for (at least) SH
6261 and sparc (as of gcc ss-970706) is this:
6262
6263 double
6264 f(double a)
6265 {
6266 double d;
6267 d = 0.1;
6268 a += d;
6269 d = 1.1;
6270 d = 0.1;
6271 a *= d;
6272 return a;
6273 }
6274
6275 So we add another pass before common subexpression elimination, to
6276 remove assignments that are dead due to a following assignment in the
6277 same basic block. */
6278
6279 static void
6280 mark_use (x, reg_set_block)
6281 rtx x, *reg_set_block;
6282 {
6283 enum rtx_code code;
6284
6285 if (! x)
6286 return;
6287 code = GET_CODE (x);
6288 switch (code)
6289 {
6290 case REG:
6291 {
6292 int regno = REGNO (x);
6293 int nregs = (regno < FIRST_PSEUDO_REGISTER
6294 ? HARD_REGNO_NREGS (regno, GET_MODE (x))
6295 : 1);
6296 do
6297 {
6298 reg_set_block[regno + nregs - 1] = 0;
6299 }
6300 while (--nregs);
6301 break;
6302 }
6303 case SET:
6304 {
6305 rtx dest = SET_DEST (x);
6306
6307 if (GET_CODE (dest) == SUBREG)
6308 dest = SUBREG_REG (dest);
6309 if (GET_CODE (dest) != REG)
6310 mark_use (dest, reg_set_block);
6311 mark_use (SET_SRC (x), reg_set_block);
6312 break;
6313 }
6314 case CLOBBER:
6315 break;
6316 default:
6317 {
6318 const char *fmt = GET_RTX_FORMAT (code);
6319 int i, j;
6320 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6321 {
6322 if (fmt[i] == 'e')
6323 mark_use (XEXP (x, i), reg_set_block);
6324 else if (fmt[i] == 'E')
6325 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6326 mark_use (XVECEXP (x, i, j), reg_set_block);
6327 }
6328 break;
6329 }
6330 }
6331 }
6332 \f
6333 static rtx get_free_reg PARAMS ((HARD_REG_SET));
6334
6335 /* This function returns a register to use to load the address to load
6336 the fpscr from. Currently it always returns r1 or r7, but when we are
6337 able to use pseudo registers after combine, or have a better mechanism
6338 for choosing a register, it should be done here. */
6339 /* REGS_LIVE is the liveness information for the point for which we
6340 need this allocation. In some bare-bones exit blocks, r1 is live at the
6341 start. We can even have all of r0..r3 being live:
6342 __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
6343 INSN before which new insns are placed with will clobber the register
6344 we return. If a basic block consists only of setting the return value
6345 register to a pseudo and using that register, the return value is not
6346 live before or after this block, yet we we'll insert our insns right in
6347 the middle. */
6348
6349 static rtx
6350 get_free_reg (regs_live)
6351 HARD_REG_SET regs_live;
6352 {
6353 if (! TEST_HARD_REG_BIT (regs_live, 1))
6354 return gen_rtx_REG (Pmode, 1);
6355
6356 /* Hard reg 1 is live; since this is a SMALL_REGISTER_CLASSES target,
6357 there shouldn't be anything but a jump before the function end. */
6358 if (! TEST_HARD_REG_BIT (regs_live, 7))
6359 return gen_rtx_REG (Pmode, 7);
6360
6361 abort ();
6362 }
6363
6364 /* This function will set the fpscr from memory.
6365 MODE is the mode we are setting it to. */
6366 void
6367 fpscr_set_from_mem (mode, regs_live)
6368 int mode;
6369 HARD_REG_SET regs_live;
6370 {
6371 enum attr_fp_mode fp_mode = mode;
6372 rtx addr_reg = get_free_reg (regs_live);
6373
6374 if (fp_mode == (enum attr_fp_mode) NORMAL_MODE (FP_MODE))
6375 emit_insn (gen_fpu_switch1 (addr_reg));
6376 else
6377 emit_insn (gen_fpu_switch0 (addr_reg));
6378 }
6379
6380 /* Is the given character a logical line separator for the assembler? */
6381 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
6382 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C) ((C) == ';')
6383 #endif
6384
6385 int
6386 sh_insn_length_adjustment (insn)
6387 rtx insn;
6388 {
6389 /* Instructions with unfilled delay slots take up an extra two bytes for
6390 the nop in the delay slot. */
6391 if (((GET_CODE (insn) == INSN
6392 && GET_CODE (PATTERN (insn)) != USE
6393 && GET_CODE (PATTERN (insn)) != CLOBBER)
6394 || GET_CODE (insn) == CALL_INSN
6395 || (GET_CODE (insn) == JUMP_INSN
6396 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
6397 && GET_CODE (PATTERN (insn)) != ADDR_VEC))
6398 && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE
6399 && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
6400 return 2;
6401
6402 /* sh-dsp parallel processing insn take four bytes instead of two. */
6403
6404 if (GET_CODE (insn) == INSN)
6405 {
6406 int sum = 0;
6407 rtx body = PATTERN (insn);
6408 const char *template;
6409 char c;
6410 int maybe_label = 1;
6411
6412 if (GET_CODE (body) == ASM_INPUT)
6413 template = XSTR (body, 0);
6414 else if (asm_noperands (body) >= 0)
6415 template
6416 = decode_asm_operands (body, NULL, NULL, NULL, NULL);
6417 else
6418 return 0;
6419 do
6420 {
6421 int ppi_adjust = 0;
6422
6423 do
6424 c = *template++;
6425 while (c == ' ' || c == '\t');
6426 /* all sh-dsp parallel-processing insns start with p.
6427 The only non-ppi sh insn starting with p is pref.
6428 The only ppi starting with pr is prnd. */
6429 if ((c == 'p' || c == 'P') && strncasecmp ("re", template, 2))
6430 ppi_adjust = 2;
6431 /* The repeat pseudo-insn expands two three insns, a total of
6432 six bytes in size. */
6433 else if ((c == 'r' || c == 'R')
6434 && ! strncasecmp ("epeat", template, 5))
6435 ppi_adjust = 4;
6436 while (c && c != '\n' && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c))
6437 {
6438 /* If this is a label, it is obviously not a ppi insn. */
6439 if (c == ':' && maybe_label)
6440 {
6441 ppi_adjust = 0;
6442 break;
6443 }
6444 else if (c == '\'' || c == '"')
6445 maybe_label = 0;
6446 c = *template++;
6447 }
6448 sum += ppi_adjust;
6449 maybe_label = c != ':';
6450 }
6451 while (c);
6452 return sum;
6453 }
6454 return 0;
6455 }
6456 \f
6457 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
6458 isn't protected by a PIC unspec. */
6459 int
6460 nonpic_symbol_mentioned_p (x)
6461 rtx x;
6462 {
6463 register const char *fmt;
6464 register int i;
6465
6466 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
6467 || GET_CODE (x) == PC)
6468 return 1;
6469
6470 /* We don't want to look into the possible MEM location of a
6471 CONST_DOUBLE, since we're not going to use it, in general. */
6472 if (GET_CODE (x) == CONST_DOUBLE)
6473 return 0;
6474
6475 if (GET_CODE (x) == UNSPEC
6476 && (XINT (x, 1) == UNSPEC_PIC
6477 || XINT (x, 1) == UNSPEC_GOT
6478 || XINT (x, 1) == UNSPEC_GOTOFF
6479 || XINT (x, 1) == UNSPEC_GOTPLT
6480 || XINT (x, 1) == UNSPEC_PLT))
6481 return 0;
6482
6483 fmt = GET_RTX_FORMAT (GET_CODE (x));
6484 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6485 {
6486 if (fmt[i] == 'E')
6487 {
6488 register int j;
6489
6490 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6491 if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
6492 return 1;
6493 }
6494 else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
6495 return 1;
6496 }
6497
6498 return 0;
6499 }
6500
6501 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
6502 @GOTOFF in `reg'. */
6503 rtx
6504 legitimize_pic_address (orig, mode, reg)
6505 rtx orig;
6506 enum machine_mode mode ATTRIBUTE_UNUSED;
6507 rtx reg;
6508 {
6509 if (GET_CODE (orig) == LABEL_REF
6510 || (GET_CODE (orig) == SYMBOL_REF
6511 && (CONSTANT_POOL_ADDRESS_P (orig)
6512 /* SYMBOL_REF_FLAG is set on static symbols. */
6513 || SYMBOL_REF_FLAG (orig))))
6514 {
6515 if (reg == 0)
6516 reg = gen_reg_rtx (Pmode);
6517
6518 emit_insn (gen_symGOTOFF2reg (reg, orig));
6519 return reg;
6520 }
6521 else if (GET_CODE (orig) == SYMBOL_REF)
6522 {
6523 if (reg == 0)
6524 reg = gen_reg_rtx (Pmode);
6525
6526 emit_insn (gen_symGOT2reg (reg, orig));
6527 return reg;
6528 }
6529 return orig;
6530 }
6531
6532 /* Mark the use of a constant in the literal table. If the constant
6533 has multiple labels, make it unique. */
6534 static rtx mark_constant_pool_use (x)
6535 rtx x;
6536 {
6537 rtx insn, lab, pattern;
6538
6539 if (x == NULL)
6540 return x;
6541
6542 switch (GET_CODE (x))
6543 {
6544 case LABEL_REF:
6545 x = XEXP (x, 0);
6546 case CODE_LABEL:
6547 break;
6548 default:
6549 return x;
6550 }
6551
6552 /* Get the first label in the list of labels for the same constant
6553 and delete another labels in the list. */
6554 lab = x;
6555 for (insn = PREV_INSN (x); insn; insn = PREV_INSN (insn))
6556 {
6557 if (GET_CODE (insn) != CODE_LABEL
6558 || LABEL_REFS (insn) != NEXT_INSN (insn))
6559 break;
6560 lab = insn;
6561 }
6562
6563 for (insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
6564 INSN_DELETED_P (insn) = 1;
6565
6566 /* Mark constants in a window. */
6567 for (insn = NEXT_INSN (x); insn; insn = NEXT_INSN (insn))
6568 {
6569 if (GET_CODE (insn) != INSN)
6570 continue;
6571
6572 pattern = PATTERN (insn);
6573 if (GET_CODE (pattern) != UNSPEC_VOLATILE)
6574 continue;
6575
6576 switch (XINT (pattern, 1))
6577 {
6578 case UNSPECV_CONST2:
6579 case UNSPECV_CONST4:
6580 case UNSPECV_CONST8:
6581 XVECEXP (pattern, 0, 1) = const1_rtx;
6582 break;
6583 case UNSPECV_WINDOW_END:
6584 if (XVECEXP (pattern, 0, 0) == x)
6585 return lab;
6586 break;
6587 case UNSPECV_CONST_END:
6588 return lab;
6589 default:
6590 break;
6591 }
6592 }
6593
6594 return lab;
6595 }
6596 \f
6597 /* Return true if it's possible to redirect BRANCH1 to the destination
6598 of an unconditional jump BRANCH2. We only want to do this if the
6599 resulting branch will have a short displacement. */
6600 int
6601 sh_can_redirect_branch (branch1, branch2)
6602 rtx branch1;
6603 rtx branch2;
6604 {
6605 if (flag_expensive_optimizations && simplejump_p (branch2))
6606 {
6607 rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
6608 rtx insn;
6609 int distance;
6610
6611 for (distance = 0, insn = NEXT_INSN (branch1);
6612 insn && distance < 256;
6613 insn = PREV_INSN (insn))
6614 {
6615 if (insn == dest)
6616 return 1;
6617 else
6618 distance += get_attr_length (insn);
6619 }
6620 for (distance = 0, insn = NEXT_INSN (branch1);
6621 insn && distance < 256;
6622 insn = NEXT_INSN (insn))
6623 {
6624 if (insn == dest)
6625 return 1;
6626 else
6627 distance += get_attr_length (insn);
6628 }
6629 }
6630 return 0;
6631 }
6632
6633 #ifndef OBJECT_FORMAT_ELF
6634 static void
6635 sh_asm_named_section (name, flags)
6636 const char *name;
6637 unsigned int flags ATTRIBUTE_UNUSED;
6638 {
6639 /* ??? Perhaps we should be using default_coff_asm_named_section. */
6640 fprintf (asm_out_file, "\t.section %s\n", name);
6641 }
6642 #endif /* ! OBJECT_FORMAT_ELF */
6643
6644 /* A C statement (sans semicolon) to update the integer variable COST
6645 based on the relationship between INSN that is dependent on
6646 DEP_INSN through the dependence LINK. The default is to make no
6647 adjustment to COST. This can be used for example to specify to
6648 the scheduler that an output- or anti-dependence does not incur
6649 the same cost as a data-dependence. */
6650 static int
6651 sh_adjust_cost (insn, link, dep_insn, cost)
6652 rtx insn;
6653 rtx link ATTRIBUTE_UNUSED;
6654 rtx dep_insn;
6655 int cost;
6656 {
6657 rtx reg;
6658
6659 if (GET_CODE(insn) == CALL_INSN)
6660 {
6661 /* The only input for a call that is timing-critical is the
6662 function's address. */
6663 rtx call = PATTERN (insn);
6664
6665 if (GET_CODE (call) == PARALLEL)
6666 call = XVECEXP (call, 0 ,0);
6667 if (GET_CODE (call) == SET)
6668 call = SET_SRC (call);
6669 if (GET_CODE (call) == CALL && GET_CODE (XEXP (call, 0)) == MEM
6670 && ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn))
6671 cost = 0;
6672 }
6673 /* All sfunc calls are parallels with at least four components.
6674 Exploit this to avoid unnecessary calls to sfunc_uses_reg. */
6675 else if (GET_CODE (PATTERN (insn)) == PARALLEL
6676 && XVECLEN (PATTERN (insn), 0) >= 4
6677 && (reg = sfunc_uses_reg (insn)))
6678 {
6679 /* Likewise, the most timing critical input for an sfuncs call
6680 is the function address. However, sfuncs typically start
6681 using their arguments pretty quickly.
6682 Assume a four cycle delay before they are needed. */
6683 if (! reg_set_p (reg, dep_insn))
6684 cost -= TARGET_SUPERSCALAR ? 40 : 4;
6685 }
6686 /* Adjust load_si / pcload_si type insns latency. Use the known
6687 nominal latency and form of the insn to speed up the check. */
6688 else if (cost == 3
6689 && GET_CODE (PATTERN (dep_insn)) == SET
6690 /* Latency for dmpy type insns is also 3, so check the that
6691 it's actually a move insn. */
6692 && general_movsrc_operand (SET_SRC (PATTERN (dep_insn)), SImode))
6693 cost = 2;
6694 else if (cost == 30
6695 && GET_CODE (PATTERN (dep_insn)) == SET
6696 && GET_MODE (SET_SRC (PATTERN (dep_insn))) == SImode)
6697 cost = 20;
6698
6699 return cost;
6700 }
6701
6702 /* For use by ALLOCATE_INITIAL_VALUE. Note that sh.md contains some
6703 'special function' patterns (type sfunc) that clobber pr, but that
6704 do not look like function calls to leaf_function_p. Hence we must
6705 do this extra check. */
6706 int
6707 sh_pr_n_sets ()
6708 {
6709 return REG_N_SETS (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
6710 }
6711
6712 /* SHmedia requires registers for branches, so we can't generate new
6713 branches past reload. */
6714 static bool
6715 sh_cannot_modify_jumps_p ()
6716 {
6717 return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
6718 }
6719
6720 static bool
6721 sh_ms_bitfield_layout_p (record_type)
6722 tree record_type ATTRIBUTE_UNUSED;
6723 {
6724 return TARGET_SH5;
6725 }
This page took 0.344057 seconds and 6 git commands to generate.