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