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