]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/sh/sh.c
typo typo fixes fixes
[thirdparty/gcc.git] / gcc / config / sh / sh.c
CommitLineData
16bea517 1/* Output routines for GCC for Hitachi Super-H.
956d6950 2 Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
bc45ade3 3
c15c9075
RK
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
bc45ade3 20
8aa2a305
JW
21/* Contributed by Steve Chamberlain (sac@cygnus.com).
22 Improved by Jim Wilson (wilson@cygnus.com). */
bc45ade3 23
bc45ade3 24#include "config.h"
8aa2a305
JW
25
26#include <stdio.h>
27
bc45ade3 28#include "rtl.h"
bc45ade3 29#include "tree.h"
bc45ade3 30#include "flags.h"
8aa2a305 31#include "insn-flags.h"
bc45ade3 32#include "expr.h"
8aa2a305
JW
33#include "regs.h"
34#include "hard-reg-set.h"
35#include "output.h"
956a5206 36#include "insn-attr.h"
bc45ade3 37
1245df60
R
38int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
39
00f8ff66
SC
40#define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
41#define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
42
20b04867 43/* ??? The pragma interrupt support will not work for SH3. */
8aa2a305
JW
44/* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
45 output code for the next function appropriate for an interrupt handler. */
0d7e008e 46int pragma_interrupt;
bc45ade3 47
4408efce
JL
48/* This is set by the trap_exit attribute for functions. It specifies
49 a trap number to be used in a trapa instruction at function exit
50 (instead of an rte instruction). */
51int trap_exit;
52
53/* This is used by the sp_switch attribute for functions. It specifies
54 a variable holding the address of the stack the interrupt function
55 should switch to/from at entry/exit. */
56rtx sp_switch;
57
8aa2a305
JW
58/* This is set by #pragma trapa, and is similar to the above, except that
59 the compiler doesn't emit code to preserve all registers. */
60static int pragma_trapa;
61
956a5206
JW
62/* This is set by #pragma nosave_low_regs. This is useful on the SH3,
63 which has a separate set of low regs for User and Supervisor modes.
64 This should only be used for the lowest level of interrupts. Higher levels
65 of interrupts must save the registers in case they themselves are
66 interrupted. */
67int pragma_nosave_low_regs;
68
8aa2a305
JW
69/* This is used for communication between SETUP_INCOMING_VARARGS and
70 sh_expand_prologue. */
bc45ade3 71int current_function_anonymous_args;
16bea517 72
8aa2a305
JW
73/* Global variables from toplev.c and final.c that are used within, but
74 not declared in any header file. */
b9654711 75extern char *version_string;
8aa2a305 76extern int *insn_addresses;
00f8ff66 77
bc45ade3
SC
78/* Global variables for machine-dependent things. */
79
16bea517
JW
80/* Which cpu are we scheduling for. */
81enum processor_type sh_cpu;
82
bc45ade3 83/* Saved operands from the last compare to use when we generate an scc
16bea517 84 or bcc insn. */
bc45ade3
SC
85
86rtx sh_compare_op0;
87rtx sh_compare_op1;
88
1245df60 89enum machine_mode sh_addr_diff_vec_mode;
1245df60 90
bc45ade3 91/* Provides the class number of the smallest class containing
16bea517 92 reg number. */
bc45ade3
SC
93
94int regno_reg_class[FIRST_PSEUDO_REGISTER] =
95{
8e87e161 96 R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
bc45ade3
SC
97 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
98 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
99 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
8e87e161 100 GENERAL_REGS, PR_REGS, T_REGS, NO_REGS,
2afeea0f 101 MAC_REGS, MAC_REGS, FPUL_REGS, GENERAL_REGS,
1a95a963
JW
102 FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
103 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
104 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
105 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
bc45ade3
SC
106};
107
108/* Provide reg_class from a letter such as appears in the machine
16bea517 109 description. */
bc45ade3
SC
110
111enum reg_class reg_class_from_letter[] =
112{
113 /* a */ NO_REGS, /* b */ NO_REGS, /* c */ NO_REGS, /* d */ NO_REGS,
1a95a963 114 /* e */ NO_REGS, /* f */ FP_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
bc45ade3
SC
115 /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ PR_REGS,
116 /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
117 /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
1a95a963
JW
118 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
119 /* y */ FPUL_REGS, /* z */ R0_REGS
bc45ade3 120};
1245df60
R
121
122static void split_branches PROTO ((rtx));
b9654711 123\f
16bea517 124/* Print the operand address in x to the stream. */
bc45ade3
SC
125
126void
127print_operand_address (stream, x)
128 FILE *stream;
129 rtx x;
130{
131 switch (GET_CODE (x))
132 {
133 case REG:
134 fprintf (stream, "@%s", reg_names[REGNO (x)]);
135 break;
8aa2a305 136
bc45ade3
SC
137 case PLUS:
138 {
139 rtx base = XEXP (x, 0);
140 rtx index = XEXP (x, 1);
141
bc45ade3
SC
142 switch (GET_CODE (index))
143 {
144 case CONST_INT:
8aa2a305 145 fprintf (stream, "@(%d,%s)", INTVAL (index),
bc45ade3
SC
146 reg_names[REGNO (base)]);
147 break;
148
149 case REG:
b9654711
SC
150 fprintf (stream, "@(r0,%s)",
151 reg_names[MAX (REGNO (base), REGNO (index))]);
bc45ade3
SC
152 break;
153
154 default:
b9654711 155 debug_rtx (x);
bc45ade3
SC
156 abort ();
157 }
158 }
bc45ade3 159 break;
8aa2a305 160
bc45ade3
SC
161 case PRE_DEC:
162 fprintf (stream, "@-%s", reg_names[REGNO (XEXP (x, 0))]);
163 break;
164
165 case POST_INC:
166 fprintf (stream, "@%s+", reg_names[REGNO (XEXP (x, 0))]);
167 break;
168
169 default:
170 output_addr_const (stream, x);
171 break;
172 }
173}
174
175/* Print operand x (an rtx) in assembler syntax to file stream
176 according to modifier code.
177
b9654711 178 '.' print a .s if insn needs delay slot
1245df60 179 ',' print LOCAL_LABEL_PREFIX
4408efce 180 '@' print trap, rte or rts depending upon pragma interruptness
8aa2a305
JW
181 '#' output a nop if there is nothing to put in the delay slot
182 'O' print a constant without the #
00f8ff66 183 'R' print the LSW of a dp value - changes if in little endian
00f8ff66 184 'S' print the MSW of a dp value - changes if in little endian
8aa2a305 185 'T' print the next word of a dp value - same as 'R' in big endian mode. */
bc45ade3
SC
186
187void
188print_operand (stream, x, code)
189 FILE *stream;
190 rtx x;
191 int code;
192{
193 switch (code)
194 {
b9654711 195 case '.':
79b2746a
JW
196 if (final_sequence
197 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1245df60
R
198 fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
199 break;
200 case ',':
201 fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
b9654711 202 break;
d3ae8277 203 case '@':
4408efce
JL
204 if (trap_exit)
205 fprintf (stream, "trapa #%d", trap_exit);
206 else if (pragma_interrupt)
8e87e161 207 fprintf (stream, "rte");
d3ae8277 208 else
8e87e161 209 fprintf (stream, "rts");
d3ae8277 210 break;
bc45ade3 211 case '#':
16bea517 212 /* Output a nop if there's nothing in the delay slot. */
bc45ade3 213 if (dbr_sequence_length () == 0)
8aa2a305 214 fprintf (stream, "\n\tnop");
bc45ade3 215 break;
b9654711 216 case 'O':
0d7e008e 217 output_addr_const (stream, x);
bc45ade3 218 break;
bc45ade3 219 case 'R':
8aa2a305
JW
220 fputs (reg_names[REGNO (x) + LSW], (stream));
221 break;
222 case 'S':
223 fputs (reg_names[REGNO (x) + MSW], (stream));
00f8ff66
SC
224 break;
225 case 'T':
16bea517 226 /* Next word of a double. */
bc45ade3
SC
227 switch (GET_CODE (x))
228 {
229 case REG:
230 fputs (reg_names[REGNO (x) + 1], (stream));
231 break;
232 case MEM:
8aa2a305
JW
233 print_operand_address (stream,
234 XEXP (adj_offsettable_operand (x, 4), 0));
bc45ade3
SC
235 break;
236 }
237 break;
bc45ade3
SC
238 default:
239 switch (GET_CODE (x))
240 {
241 case REG:
242 fputs (reg_names[REGNO (x)], (stream));
243 break;
244 case MEM:
245 output_address (XEXP (x, 0));
246 break;
247 default:
248 fputc ('#', stream);
249 output_addr_const (stream, x);
250 break;
bc45ade3
SC
251 }
252 break;
253 }
254}
bc45ade3 255\f
0d7e008e
SC
256/* Emit code to perform a block move. Choose the best method.
257
258 OPERANDS[0] is the destination.
259 OPERANDS[1] is the source.
260 OPERANDS[2] is the size.
261 OPERANDS[3] is the alignment safe to use. */
262
0d7e008e
SC
263int
264expand_block_move (operands)
265 rtx *operands;
266{
267 int align = INTVAL (operands[3]);
268 int constp = (GET_CODE (operands[2]) == CONST_INT);
269 int bytes = (constp ? INTVAL (operands[2]) : 0);
0d7e008e 270
8aa2a305
JW
271 /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
272 alignment, or if it isn't a multiple of 4 bytes, then fail. */
273 if (! constp || align < 4 || (bytes % 4 != 0))
d3ae8277
SC
274 return 0;
275
8aa2a305 276 if (bytes < 64)
0d7e008e
SC
277 {
278 char entry[30];
279 tree entry_name;
280 rtx func_addr_rtx;
281 rtx r4 = gen_rtx (REG, SImode, 4);
282 rtx r5 = gen_rtx (REG, SImode, 5);
8aa2a305
JW
283
284 sprintf (entry, "__movstrSI%d", bytes);
0d7e008e
SC
285 entry_name = get_identifier (entry);
286
8aa2a305
JW
287 func_addr_rtx
288 = copy_to_mode_reg (Pmode,
289 gen_rtx (SYMBOL_REF, Pmode,
290 IDENTIFIER_POINTER (entry_name)));
0d7e008e
SC
291 emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
292 emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
293 emit_insn (gen_block_move_real (func_addr_rtx));
294 return 1;
295 }
8aa2a305
JW
296
297 /* This is the same number of bytes as a memcpy call, but to a different
298 less common function name, so this will occasionally use more space. */
299 if (! TARGET_SMALLCODE)
0d7e008e 300 {
0d7e008e
SC
301 tree entry_name;
302 rtx func_addr_rtx;
8aa2a305 303 int final_switch, while_loop;
0d7e008e
SC
304 rtx r4 = gen_rtx (REG, SImode, 4);
305 rtx r5 = gen_rtx (REG, SImode, 5);
306 rtx r6 = gen_rtx (REG, SImode, 6);
0d7e008e 307
8aa2a305
JW
308 entry_name = get_identifier ("__movstr");
309 func_addr_rtx
310 = copy_to_mode_reg (Pmode,
311 gen_rtx (SYMBOL_REF, Pmode,
312 IDENTIFIER_POINTER (entry_name)));
0d7e008e
SC
313 emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
314 emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
315
8aa2a305
JW
316 /* r6 controls the size of the move. 16 is decremented from it
317 for each 64 bytes moved. Then the negative bit left over is used
318 as an index into a list of move instructions. e.g., a 72 byte move
319 would be set up with size(r6) = 14, for one iteration through the
320 big while loop, and a switch of -2 for the last part. */
0d7e008e 321
8aa2a305
JW
322 final_switch = 16 - ((bytes / 4) % 16);
323 while_loop = ((bytes / 4) / 16 - 1) * 16;
324 emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
325 emit_insn (gen_block_lump_real (func_addr_rtx));
326 return 1;
0d7e008e 327 }
0d7e008e 328
d3ae8277 329 return 0;
0d7e008e
SC
330}
331
bc45ade3 332/* Prepare operands for a move define_expand; specifically, one of the
8aa2a305 333 operands must be in a register. */
bc45ade3 334
b9654711 335int
bc45ade3
SC
336prepare_move_operands (operands, mode)
337 rtx operands[];
338 enum machine_mode mode;
339{
e22856e8
JW
340 if (! reload_in_progress && ! reload_completed)
341 {
342 /* Copy the source to a register if both operands aren't registers. */
343 if (! register_operand (operands[0], mode)
344 && ! register_operand (operands[1], mode))
345 operands[1] = copy_to_mode_reg (mode, operands[1]);
346
347 /* This case can happen while generating code to move the result
348 of a library call to the target. Reject `st r0,@(rX,rY)' because
349 reload will fail to find a spill register for rX, since r0 is already
350 being used for the source. */
351 else if (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 0
352 && GET_CODE (operands[0]) == MEM
353 && GET_CODE (XEXP (operands[0], 0)) == PLUS
354 && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
355 operands[1] = copy_to_mode_reg (mode, operands[1]);
356 }
8aa2a305
JW
357
358 return 0;
359}
360
361/* Prepare the operands for an scc instruction; make sure that the
362 compare has been done. */
363rtx
364prepare_scc_operands (code)
365 enum rtx_code code;
366{
367 rtx t_reg = gen_rtx (REG, SImode, T_REG);
368 enum rtx_code oldcode = code;
9374bd85 369 enum machine_mode mode;
8aa2a305
JW
370
371 /* First need a compare insn. */
372 switch (code)
bc45ade3 373 {
8aa2a305
JW
374 case NE:
375 /* It isn't possible to handle this case. */
376 abort ();
377 case LT:
378 code = GT;
379 break;
380 case LE:
381 code = GE;
382 break;
383 case LTU:
384 code = GTU;
385 break;
386 case LEU:
387 code = GEU;
388 break;
bc45ade3 389 }
8aa2a305 390 if (code != oldcode)
b9654711 391 {
8aa2a305
JW
392 rtx tmp = sh_compare_op0;
393 sh_compare_op0 = sh_compare_op1;
394 sh_compare_op1 = tmp;
0d7e008e 395 }
b9654711 396
9374bd85
RK
397 mode = GET_MODE (sh_compare_op0);
398 if (mode == VOIDmode)
399 mode = GET_MODE (sh_compare_op1);
400
401 sh_compare_op0 = force_reg (mode, sh_compare_op0);
1245df60
R
402 if ((code != EQ && code != NE
403 && (sh_compare_op1 != const0_rtx
404 || code == GTU || code == GEU || code == LTU || code == LEU))
405 || TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT)
9374bd85 406 sh_compare_op1 = force_reg (mode, sh_compare_op1);
0d7e008e 407
8aa2a305
JW
408 emit_insn (gen_rtx (SET, VOIDmode, t_reg,
409 gen_rtx (code, SImode, sh_compare_op0,
410 sh_compare_op1)));
0d7e008e 411
8aa2a305 412 return t_reg;
bc45ade3
SC
413}
414
8aa2a305
JW
415/* Called from the md file, set up the operands of a compare instruction. */
416
417void
418from_compare (operands, code)
419 rtx *operands;
8e87e161 420 int code;
bc45ade3 421{
1245df60
R
422 enum machine_mode mode = GET_MODE (sh_compare_op0);
423 rtx insn;
424 if (mode == VOIDmode)
425 mode = GET_MODE (sh_compare_op1);
426 if (code != EQ
427 || mode == DImode
428 || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
bc45ade3 429 {
8aa2a305 430 /* Force args into regs, since we can't use constants here. */
24a25d45 431 sh_compare_op0 = force_reg (mode, sh_compare_op0);
8aa2a305 432 if (sh_compare_op1 != const0_rtx
1245df60
R
433 || code == GTU || code == GEU
434 || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
24a25d45 435 sh_compare_op1 = force_reg (mode, sh_compare_op1);
bc45ade3 436 }
1245df60
R
437 if (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT && code == GE)
438 {
439 from_compare (operands, GT);
440 insn = gen_ieee_ccmpeqsf_t (sh_compare_op0, sh_compare_op1);
441 }
442 else
443 insn = gen_rtx (SET, VOIDmode,
444 gen_rtx (REG, SImode, 18),
445 gen_rtx (code, SImode, sh_compare_op0, sh_compare_op1));
446 emit_insn (insn);
bc45ade3
SC
447}
448\f
16bea517 449/* Functions to output assembly code. */
bc45ade3 450
b9654711 451/* Return a sequence of instructions to perform DI or DF move.
bc45ade3 452
b9654711 453 Since the SH cannot move a DI or DF in one instruction, we have
16bea517 454 to take care when we see overlapping source and dest registers. */
0d7e008e 455
bc45ade3 456char *
0d7e008e
SC
457output_movedouble (insn, operands, mode)
458 rtx insn;
bc45ade3
SC
459 rtx operands[];
460 enum machine_mode mode;
461{
b9654711
SC
462 rtx dst = operands[0];
463 rtx src = operands[1];
b9654711 464
0d7e008e 465 if (GET_CODE (dst) == MEM
16bea517 466 && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
8aa2a305
JW
467 return "mov.l %T1,%0\n\tmov.l %1,%0";
468
b9654711
SC
469 if (register_operand (dst, mode)
470 && register_operand (src, mode))
bc45ade3 471 {
b9654711 472 if (REGNO (src) == MACH_REG)
00f8ff66 473 return "sts mach,%S0\n\tsts macl,%R0";
bc45ade3 474
8aa2a305
JW
475 /* When mov.d r1,r2 do r2->r3 then r1->r2;
476 when mov.d r1,r0 do r1->r0 then r2->r1. */
b9654711
SC
477
478 if (REGNO (src) + 1 == REGNO (dst))
5325c0fa 479 return "mov %T1,%T0\n\tmov %1,%0";
b9654711 480 else
5325c0fa 481 return "mov %1,%0\n\tmov %T1,%T0";
b9654711
SC
482 }
483 else if (GET_CODE (src) == CONST_INT)
bc45ade3 484 {
8aa2a305
JW
485 if (INTVAL (src) < 0)
486 output_asm_insn ("mov #-1,%S0", operands);
bc45ade3 487 else
8aa2a305 488 output_asm_insn ("mov #0,%S0", operands);
bc45ade3 489
8aa2a305 490 return "mov %1,%R0";
0d7e008e 491 }
b9654711 492 else if (GET_CODE (src) == MEM)
bc45ade3 493 {
8aa2a305 494 int ptrreg = -1;
b9654711
SC
495 int dreg = REGNO (dst);
496 rtx inside = XEXP (src, 0);
bc45ade3
SC
497
498 if (GET_CODE (inside) == REG)
8aa2a305 499 ptrreg = REGNO (inside);
79b2746a
JW
500 else if (GET_CODE (inside) == SUBREG)
501 ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
bc45ade3
SC
502 else if (GET_CODE (inside) == PLUS)
503 {
8aa2a305
JW
504 ptrreg = REGNO (XEXP (inside, 0));
505 /* ??? A r0+REG address shouldn't be possible here, because it isn't
506 an offsettable address. Unfortunately, offsettable addresses use
507 QImode to check the offset, and a QImode offsettable address
508 requires r0 for the other operand, which is not currently
509 supported, so we can't use the 'o' constraint.
510 Thus we must check for and handle r0+REG addresses here.
511 We punt for now, since this is likely very rare. */
512 if (GET_CODE (XEXP (inside, 1)) == REG)
513 abort ();
bc45ade3 514 }
0d7e008e 515 else if (GET_CODE (inside) == LABEL_REF)
8aa2a305 516 return "mov.l %1,%0\n\tmov.l %1+4,%T0";
8e87e161 517 else if (GET_CODE (inside) == POST_INC)
8aa2a305 518 return "mov.l %1,%0\n\tmov.l %1,%T0";
bc45ade3
SC
519 else
520 abort ();
521
8aa2a305
JW
522 /* Work out the safe way to copy. Copy into the second half first. */
523 if (dreg == ptrreg)
524 return "mov.l %T1,%T0\n\tmov.l %1,%0";
bc45ade3
SC
525 }
526
00f8ff66 527 return "mov.l %1,%0\n\tmov.l %T1,%T0";
bc45ade3
SC
528}
529
8aa2a305
JW
530/* Print an instruction which would have gone into a delay slot after
531 another instruction, but couldn't because the other instruction expanded
532 into a sequence where putting the slot insn at the end wouldn't work. */
0d7e008e 533
8aa2a305
JW
534static void
535print_slot (insn)
536 rtx insn;
537{
538 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
b9654711 539
8aa2a305
JW
540 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
541}
0d7e008e 542
0d7e008e
SC
543char *
544output_far_jump (insn, op)
545 rtx insn;
546 rtx op;
547{
1245df60
R
548 struct { rtx lab, reg, op; } this;
549 char *jump;
550 int far;
33f7f353 551 int offset = branch_dest (insn) - insn_addresses[INSN_UID (insn)];
0d7e008e 552
1245df60 553 this.lab = gen_label_rtx ();
b9654711 554
33f7f353 555 if (offset >= -32764 && offset - get_attr_length (insn) <= 32766)
1245df60
R
556 {
557 far = 0;
558 jump = "mov.w %O0,%1;braf %1";
559 }
560 else
561 {
562 far = 1;
563 jump = "mov.l %O0,%1;jmp @%1";
564 }
565 /* If we have a scratch register available, use it. */
566 if (GET_CODE (PREV_INSN (insn)) == INSN
567 && INSN_CODE (PREV_INSN (insn)) == CODE_FOR_indirect_jump_scratch)
568 {
569 this.reg = SET_DEST (PATTERN (PREV_INSN (insn)));
570 output_asm_insn (jump, &this.lab);
571 if (dbr_sequence_length ())
572 print_slot (final_sequence);
573 else
574 output_asm_insn ("nop", 0);
575 }
576 else
577 {
578 /* Output the delay slot insn first if any. */
579 if (dbr_sequence_length ())
580 print_slot (final_sequence);
581
582 this.reg = gen_rtx (REG, SImode, 13);
583 output_asm_insn ("mov.l r13,@-r15", 0);
584 output_asm_insn (jump, &this.lab);
585 output_asm_insn ("mov.l @r15+,r13", 0);
586 }
587 if (far)
588 output_asm_insn (".align 2", 0);
589 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (this.lab));
590 this.op = op;
591 output_asm_insn (far ? ".long %O2" : ".word %O2-%O0", &this.lab);
0d7e008e
SC
592 return "";
593}
bc45ade3 594
8aa2a305
JW
595/* Local label counter, used for constants in the pool and inside
596 pattern branches. */
597
598static int lf = 100;
599
600/* Output code for ordinary branches. */
601
bc45ade3 602char *
8aa2a305 603output_branch (logic, insn, operands)
bc45ade3 604 int logic;
b9654711 605 rtx insn;
8aa2a305 606 rtx *operands;
bc45ade3 607{
33f7f353 608 switch (get_attr_length (insn))
bc45ade3 609 {
33f7f353
JR
610 case 6:
611 /* This can happen if filling the delay slot has caused a forward
612 branch to exceed its range (we could reverse it, but only
613 when we know we won't overextend other branches; this should
614 best be handled by relaxation).
615 It can also happen when other condbranches hoist delay slot insn
1245df60
R
616 from their destination, thus leading to code size increase.
617 But the branch will still be in the range -4092..+4098 bytes. */
bc45ade3 618
33f7f353 619 if (! TARGET_RELAX)
1245df60 620 {
33f7f353
JR
621 int label = lf++;
622 /* The call to print_slot will clobber the operands. */
623 rtx op0 = operands[0];
624
625 /* If the instruction in the delay slot is annulled (true), then
626 there is no delay slot where we can put it now. The only safe
627 place for it is after the label. final will do that by default. */
628
629 if (final_sequence
630 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
631 {
632 asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
633 ASSEMBLER_DIALECT ? "/" : ".", label);
634 print_slot (final_sequence);
635 }
636 else
637 asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
638
639 output_asm_insn ("bra\t%l0", &op0);
640 fprintf (asm_out_file, "\tnop\n");
641 ASM_OUTPUT_INTERNAL_LABEL(asm_out_file, "LF", label);
642
643 return "";
1245df60 644 }
33f7f353
JR
645 /* When relaxing, handle this like a short branch. The linker
646 will fix it up if it still doesn't fit after relaxation. */
647 case 2:
648 return logic ? "bt%.\t%l0" : "bf%.\t%l0";
649 default:
650 abort ();
1245df60 651 }
1245df60 652}
bc45ade3 653
1245df60
R
654char *
655output_branchy_insn (code, template, insn, operands)
656 char *template;
657 enum rtx_code code;
658 rtx insn;
659 rtx *operands;
660{
661 rtx next_insn = NEXT_INSN (insn);
662 int label_nr;
79b2746a 663
1245df60
R
664 if (next_insn && GET_CODE (next_insn) == JUMP_INSN && condjump_p (next_insn))
665 {
666 rtx src = SET_SRC (PATTERN (next_insn));
667 if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
668 {
669 /* Following branch not taken */
670 operands[9] = gen_label_rtx ();
671 emit_label_after (operands[9], next_insn);
672 return template;
673 }
674 else
675 {
33f7f353
JR
676 int offset = (branch_dest (next_insn)
677 - insn_addresses[INSN_UID (next_insn)] + 4);
678 if (offset >= -252 && offset <= 258)
1245df60
R
679 {
680 if (GET_CODE (src) == IF_THEN_ELSE)
681 /* branch_true */
682 src = XEXP (src, 1);
683 operands[9] = src;
684 return template;
685 }
686 }
bc45ade3 687 }
1245df60
R
688 operands[9] = gen_label_rtx ();
689 emit_label_after (operands[9], insn);
690 return template;
691}
e4fa6b06 692
1245df60
R
693char *
694output_ieee_ccmpeq (insn, operands)
695 rtx insn, operands;
696{
697 output_branchy_insn (NE, "bt\t%l9\\;fcmp/eq\t%1,%0", insn, operands);
bc45ade3 698}
8e87e161 699\f
8aa2a305
JW
700/* Output to FILE the start of the assembler file. */
701
0d7e008e 702void
adade868 703output_file_start (file)
0d7e008e 704 FILE *file;
bc45ade3 705{
0d7e008e 706 register int pos;
b9654711 707
0d7e008e 708 output_file_directive (file, main_input_filename);
b9654711 709
0d7e008e
SC
710 /* Switch to the data section so that the coffsem symbol and the
711 gcc2_compiled. symbol aren't in the text section. */
712 data_section ();
b9654711 713
8aa2a305
JW
714 if (TARGET_LITTLE_ENDIAN)
715 fprintf (file, "\t.little\n");
bc45ade3 716}
0d7e008e 717\f
16bea517 718/* Actual number of instructions used to make a shift by N. */
8aa2a305 719static char ashiftrt_insns[] =
16bea517
JW
720 { 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};
721
722/* Left shift and logical right shift are the same. */
8aa2a305 723static char shift_insns[] =
16bea517 724 { 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};
8aa2a305 725
16bea517
JW
726/* Individual shift amounts needed to get the above length sequences.
727 One bit right shifts clobber the T bit, so when possible, put one bit
728 shifts in the middle of the sequence, so the ends are eligible for
729 branch delay slots. */
8aa2a305 730static short shift_amounts[32][5] = {
16bea517
JW
731 {0}, {1}, {2}, {2, 1},
732 {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
733 {8}, {8, 1}, {8, 2}, {8, 1, 2},
734 {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
735 {16}, {16, 1}, {16, 2}, {16, 1, 2},
736 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
737 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1d3534c0 738 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
16bea517 739
8d481241
JW
740/* Likewise, but for shift amounts < 16, up to three highmost bits
741 might be clobbered. This is typically used when combined with some
742 kind of sign or zero extension. */
743
744static char ext_shift_insns[] =
745 { 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};
746
747static short ext_shift_amounts[32][4] = {
748 {0}, {1}, {2}, {2, 1},
749 {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
750 {8}, {8, 1}, {8, 2}, {8, 1, 2},
751 {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
752 {16}, {16, 1}, {16, 2}, {16, 1, 2},
753 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
754 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
755 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
756
ae9d19c0
JR
757/* Assuming we have a value that has been sign-extended by at least one bit,
758 can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
759 to shift it by N without data loss, and quicker than by other means? */
760#define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
761
16bea517
JW
762/* This is used in length attributes in sh.md to help compute the length
763 of arbitrary constant shift instructions. */
bc45ade3 764
16bea517
JW
765int
766shift_insns_rtx (insn)
767 rtx insn;
768{
769 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
770 int shift_count = INTVAL (XEXP (set_src, 1));
771 enum rtx_code shift_code = GET_CODE (set_src);
00f8ff66 772
16bea517
JW
773 switch (shift_code)
774 {
775 case ASHIFTRT:
776 return ashiftrt_insns[shift_count];
777 case LSHIFTRT:
778 case ASHIFT:
779 return shift_insns[shift_count];
780 default:
781 abort();
782 }
783}
784
16bea517 785/* Return the cost of a shift. */
bc45ade3 786
0d7e008e 787int
8aa2a305
JW
788shiftcosts (x)
789 rtx x;
bc45ade3 790{
8aa2a305
JW
791 int value = INTVAL (XEXP (x, 1));
792
16bea517 793 /* If shift by a non constant, then this will be expensive. */
8aa2a305 794 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1245df60 795 return SH_DYNAMIC_SHIFT_COST;
bc45ade3 796
16bea517 797 /* Otherwise, return the true cost in instructions. */
8aa2a305 798 if (GET_CODE (x) == ASHIFTRT)
49b6d06b
JW
799 {
800 int cost = ashiftrt_insns[value];
801 /* If SH3, then we put the constant in a reg and use shad. */
1245df60
R
802 if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
803 cost = 1 + SH_DYNAMIC_SHIFT_COST;
49b6d06b
JW
804 return cost;
805 }
8aa2a305
JW
806 else
807 return shift_insns[value];
0d7e008e 808}
b9654711 809
8aa2a305
JW
810/* Return the cost of an AND operation. */
811
8e87e161 812int
8aa2a305
JW
813andcosts (x)
814 rtx x;
0d7e008e
SC
815{
816 int i;
8aa2a305 817
a6f71af5 818 /* Anding with a register is a single cycle and instruction. */
8aa2a305 819 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
a6f71af5 820 return 1;
8aa2a305
JW
821
822 i = INTVAL (XEXP (x, 1));
a6f71af5 823 /* These constants are single cycle extu.[bw] instructions. */
0d7e008e 824 if (i == 0xff || i == 0xffff)
a6f71af5
JW
825 return 1;
826 /* Constants that can be used in an and immediate instruction is a single
827 cycle, but this requires r0, so make it a little more expensive. */
828 if (CONST_OK_FOR_L (i))
0d7e008e 829 return 2;
a6f71af5
JW
830 /* Constants that can be loaded with a mov immediate and an and.
831 This case is probably unnecessary. */
0d7e008e 832 if (CONST_OK_FOR_I (i))
a6f71af5
JW
833 return 2;
834 /* Any other constants requires a 2 cycle pc-relative load plus an and.
835 This case is probably unnecessary. */
836 return 3;
0d7e008e 837}
d3ae8277 838
16bea517 839/* Return the cost of a multiply. */
0d7e008e 840int
8aa2a305
JW
841multcosts (x)
842 rtx x;
0d7e008e
SC
843{
844 if (TARGET_SH2)
d3ae8277
SC
845 {
846 /* We have a mul insn, so we can never take more than the mul and the
a7771f78 847 read of the mac reg, but count more because of the latency and extra
16bea517 848 reg usage. */
d3ae8277 849 if (TARGET_SMALLCODE)
8e87e161 850 return 2;
a7771f78 851 return 3;
d3ae8277
SC
852 }
853
a7771f78 854 /* If we're aiming at small code, then just count the number of
16bea517 855 insns in a multiply call sequence. */
8e87e161 856 if (TARGET_SMALLCODE)
8aa2a305 857 return 5;
d3ae8277 858
16bea517 859 /* Otherwise count all the insns in the routine we'd be calling too. */
d3ae8277 860 return 20;
0d7e008e 861}
b9654711 862
16bea517 863/* Code to expand a shift. */
b9654711 864
0d7e008e
SC
865void
866gen_ashift (type, n, reg)
867 int type;
868 int n;
869 rtx reg;
870{
16bea517
JW
871 /* Negative values here come from the shift_amounts array. */
872 if (n < 0)
873 {
874 if (type == ASHIFT)
875 type = LSHIFTRT;
876 else
877 type = ASHIFT;
878 n = -n;
879 }
880
0d7e008e 881 switch (type)
bc45ade3 882 {
0d7e008e
SC
883 case ASHIFTRT:
884 emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
885 break;
886 case LSHIFTRT:
16bea517
JW
887 if (n == 1)
888 emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
889 else
890 emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
0d7e008e
SC
891 break;
892 case ASHIFT:
5325c0fa 893 emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
0d7e008e 894 break;
bc45ade3 895 }
bc45ade3 896}
bc45ade3 897
8d481241
JW
898/* Same for HImode */
899
900void
901gen_ashift_hi (type, n, reg)
902 int type;
903 int n;
904 rtx reg;
905{
906 /* Negative values here come from the shift_amounts array. */
907 if (n < 0)
908 {
909 if (type == ASHIFT)
910 type = LSHIFTRT;
911 else
912 type = ASHIFT;
913 n = -n;
914 }
915
916 switch (type)
917 {
918 case ASHIFTRT:
8d481241 919 case LSHIFTRT:
d0c42859
R
920 /* We don't have HImode right shift operations because using the
921 ordinary 32 bit shift instructions for that doesn't generate proper
922 zero/sign extension.
923 gen_ashift_hi is only called in contexts where we know that the
924 sign extension works out correctly. */
97d6fd65
R
925 {
926 int word = 0;
927 if (GET_CODE (reg) == SUBREG)
928 {
929 word = SUBREG_WORD (reg);
930 reg = SUBREG_REG (reg);
931 }
932 gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, word));
933 break;
934 }
8d481241
JW
935 case ASHIFT:
936 emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
937 break;
938 }
939}
940
8aa2a305
JW
941/* Output RTL to split a constant shift into its component SH constant
942 shift instructions. */
943
0d7e008e
SC
944int
945gen_shifty_op (code, operands)
946 int code;
947 rtx *operands;
bc45ade3 948{
16bea517 949 int value = INTVAL (operands[2]);
8aa2a305 950 int max, i;
00f8ff66 951
cff3d762
JW
952 /* Truncate the shift count in case it is out of bounds. */
953 value = value & 0x1f;
954
8aa2a305 955 if (value == 31)
16bea517 956 {
8aa2a305 957 if (code == LSHIFTRT)
0d7e008e 958 {
8aa2a305
JW
959 emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
960 emit_insn (gen_movt (operands[0]));
961 return;
16bea517 962 }
8aa2a305 963 else if (code == ASHIFT)
16bea517 964 {
8aa2a305
JW
965 /* There is a two instruction sequence for 31 bit left shifts,
966 but it requires r0. */
967 if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
0d7e008e 968 {
8aa2a305
JW
969 emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
970 emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
971 return;
0d7e008e 972 }
16bea517 973 }
8aa2a305 974 }
c17f53a8
JW
975 else if (value == 0)
976 {
977 /* This can happen when not optimizing. We must output something here
978 to prevent the compiler from aborting in final.c after the try_split
979 call. */
980 emit_insn (gen_nop ());
981 return;
982 }
a9f71ad8 983
8aa2a305
JW
984 max = shift_insns[value];
985 for (i = 0; i < max; i++)
986 gen_ashift (code, shift_amounts[value][i], operands[0]);
987}
8d481241
JW
988
989/* Same as above, but optimized for values where the topmost bits don't
990 matter. */
991
992int
993gen_shifty_hi_op (code, operands)
994 int code;
995 rtx *operands;
996{
997 int value = INTVAL (operands[2]);
998 int max, i;
999 void (*gen_fun)();
1000
1001 /* This operation is used by and_shl for SImode values with a few
1002 high bits known to be cleared. */
1003 value &= 31;
1004 if (value == 0)
1005 {
1006 emit_insn (gen_nop ());
1007 return;
1008 }
1009
1010 gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
1011 if (code == ASHIFT)
1012 {
1013 max = ext_shift_insns[value];
1014 for (i = 0; i < max; i++)
1015 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1016 }
1017 else
1018 /* When shifting right, emit the shifts in reverse order, so that
1019 solitary negative values come first. */
1020 for (i = ext_shift_insns[value] - 1; i >= 0; i--)
1021 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1022}
8aa2a305
JW
1023
1024/* Output RTL for an arithmetic right shift. */
1025
1026/* ??? Rewrite to use super-optimizer sequences. */
1027
1028int
1029expand_ashiftrt (operands)
1030 rtx *operands;
1031{
1032 rtx wrk;
1033 char func[18];
1034 tree func_name;
1035 int value;
1036
49b6d06b 1037 if (TARGET_SH3)
20b04867 1038 {
49b6d06b
JW
1039 if (GET_CODE (operands[2]) != CONST_INT)
1040 {
1041 rtx count = copy_to_mode_reg (SImode, operands[2]);
1042 emit_insn (gen_negsi2 (count, count));
1043 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1044 return 1;
1045 }
1245df60
R
1046 else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
1047 > 1 + SH_DYNAMIC_SHIFT_COST)
49b6d06b 1048 {
1245df60
R
1049 rtx count
1050 = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
49b6d06b
JW
1051 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1052 return 1;
1053 }
20b04867 1054 }
8aa2a305
JW
1055 if (GET_CODE (operands[2]) != CONST_INT)
1056 return 0;
1057
1245df60 1058 value = INTVAL (operands[2]) & 31;
8aa2a305
JW
1059
1060 if (value == 31)
1061 {
1062 emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1063 return 1;
1064 }
1065 else if (value >= 16 && value <= 19)
1066 {
1067 wrk = gen_reg_rtx (SImode);
1068 emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1069 value -= 16;
1070 while (value--)
1071 gen_ashift (ASHIFTRT, 1, wrk);
1072 emit_move_insn (operands[0], wrk);
1073 return 1;
a9f71ad8 1074 }
8aa2a305
JW
1075 /* Expand a short sequence inline, longer call a magic routine. */
1076 else if (value <= 5)
1077 {
1078 wrk = gen_reg_rtx (SImode);
1079 emit_move_insn (wrk, operands[1]);
1080 while (value--)
1081 gen_ashift (ASHIFTRT, 1, wrk);
1082 emit_move_insn (operands[0], wrk);
1083 return 1;
1084 }
1085
1086 wrk = gen_reg_rtx (Pmode);
1087
1088 /* Load the value into an arg reg and call a helper. */
1089 emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
1090 sprintf (func, "__ashiftrt_r4_%d", value);
1091 func_name = get_identifier (func);
1092 emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
1093 IDENTIFIER_POINTER (func_name)));
1094 emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1095 emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
1096 return 1;
bc45ade3 1097}
8d481241 1098
1245df60
R
1099int sh_dynamicalize_shift_p (count)
1100 rtx count;
1101{
1102 return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
1103}
1104
8d481241
JW
1105/* Try to find a good way to implement the combiner pattern
1106 [(set (match_operand:SI 0 "register_operand" "r")
1107 (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1108 (match_operand:SI 2 "const_int_operand" "n"))
1109 (match_operand:SI 3 "const_int_operand" "n"))) .
1110 LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1111 return 0 for simple right / left or left/right shift combination.
1112 return 1 for a combination of shifts with zero_extend.
1113 return 2 for a combination of shifts with an AND that needs r0.
1114 return 3 for a combination of shifts with an AND that needs an extra
1115 scratch register, when the three highmost bits of the AND mask are clear.
1116 return 4 for a combination of shifts with an AND that needs an extra
1117 scratch register, when any of the three highmost bits of the AND mask
1118 is set.
1119 If ATTRP is set, store an initial right shift width in ATTRP[0],
1120 and the instruction length in ATTRP[1] . These values are not valid
1121 when returning 0.
1122 When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1123 shift_amounts for the last shift value that is to be used before the
1124 sign extend. */
1125int
1126shl_and_kind (left_rtx, mask_rtx, attrp)
1127 rtx left_rtx, mask_rtx;
1128 int *attrp;
1129{
1130 unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1131 int left = INTVAL (left_rtx), right;
1132 int best = 0;
1133 int cost, best_cost = 10000;
1134 int best_right = 0, best_len = 0;
1135 int i;
1136 int can_ext;
1137
1138 if (left < 0 || left > 31)
1139 return 0;
1140 if (GET_CODE (mask_rtx) == CONST_INT)
1141 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
1142 else
1143 mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
1144 /* Can this be expressed as a right shift / left shift pair ? */
1145 lsb = ((mask ^ (mask - 1)) >> 1) + 1;
1146 right = exact_log2 (lsb);
1147 mask2 = ~(mask + lsb - 1);
1148 lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
1149 /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1150 if (! mask2)
1151 best_cost = shift_insns[right] + shift_insns[right + left];
1152 /* mask has no trailing zeroes <==> ! right */
1153 else if (! right && mask2 == ~(lsb2 - 1))
1154 {
1155 int late_right = exact_log2 (lsb2);
1156 best_cost = shift_insns[left + late_right] + shift_insns[late_right];
1157 }
1158 /* Try to use zero extend */
1159 if (mask2 == ~(lsb2 - 1))
1160 {
1161 int width, first;
1162
1163 for (width = 8; width <= 16; width += 8)
1164 {
1165 /* Can we zero-extend right away? */
1166 if (lsb2 == (HOST_WIDE_INT)1 << width)
1167 {
1168 cost
1169 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
1170 if (cost < best_cost)
1171 {
1172 best = 1;
1173 best_cost = cost;
1174 best_right = right;
1175 best_len = cost;
1176 if (attrp)
1177 attrp[2] = -1;
1178 }
1179 continue;
1180 }
1181 /* ??? Could try to put zero extend into initial right shift,
1182 or even shift a bit left before the right shift. */
1183 /* Determine value of first part of left shift, to get to the
1184 zero extend cut-off point. */
1185 first = width - exact_log2 (lsb2) + right;
1186 if (first >= 0 && right + left - first >= 0)
1187 {
1188 cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
1189 + ext_shift_insns[right + left - first];
1190 if (cost < best_cost)
1191 {
1192 best = 1;
1193 best_cost = cost;
1194 best_right = right;
1195 best_len = cost;
1196 if (attrp)
1197 attrp[2] = first;
1198 }
1199 }
1200 }
1201 }
1202 /* Try to use r0 AND pattern */
1203 for (i = 0; i <= 2; i++)
1204 {
1205 if (i > right)
1206 break;
1207 if (! CONST_OK_FOR_L (mask >> i))
1208 continue;
1209 cost = (i != 0) + 2 + ext_shift_insns[left + i];
1210 if (cost < best_cost)
1211 {
1212 best = 2;
1213 best_cost = cost;
1214 best_right = i;
1215 best_len = cost - 1;
1216 }
1217 }
1218 /* Try to use a scratch register to hold the AND operand. */
1219 can_ext = ((mask << left) & 0xe0000000) == 0;
1220 for (i = 0; i <= 2; i++)
1221 {
1222 if (i > right)
1223 break;
1224 cost = (i != 0) + (CONST_OK_FOR_I (mask >> i) ? 2 : 3)
6ab911bb 1225 + (can_ext ? ext_shift_insns : shift_insns)[left + i];
8d481241
JW
1226 if (cost < best_cost)
1227 {
1228 best = 4 - can_ext;
1229 best_cost = cost;
1230 best_right = i;
1231 best_len = cost - 1 - ! CONST_OK_FOR_I (mask >> i);
1232 }
1233 }
1234
1235 if (attrp)
1236 {
1237 attrp[0] = best_right;
1238 attrp[1] = best_len;
1239 }
1240 return best;
1241}
1242
1243/* This is used in length attributes of the unnamed instructions
1244 corresponding to shl_and_kind return values of 1 and 2. */
1245int
1246shl_and_length (insn)
1247 rtx insn;
1248{
1249 rtx set_src, left_rtx, mask_rtx;
1250 int attributes[3];
1251
1252 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1253 left_rtx = XEXP (XEXP (set_src, 0), 1);
1254 mask_rtx = XEXP (set_src, 1);
1255 shl_and_kind (left_rtx, mask_rtx, attributes);
1256 return attributes[1];
1257}
1258
1259/* This is used in length attribute of the and_shl_scratch instruction. */
1260
1261int
1262shl_and_scr_length (insn)
1263 rtx insn;
1264{
1265 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1266 int len = shift_insns[INTVAL (XEXP (set_src, 1))];
1267 rtx op = XEXP (set_src, 0);
1268 len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
1269 op = XEXP (XEXP (op, 0), 0);
1270 return len + shift_insns[INTVAL (XEXP (op, 1))];
1271}
1272
1273/* Generating rtl? */
1274extern int rtx_equal_function_value_matters;
1275
1276/* Generate rtl for instructions for which shl_and_kind advised a particular
1277 method of generating them, i.e. returned zero. */
1278
1279int
1280gen_shl_and (dest, left_rtx, mask_rtx, source)
1281 rtx dest, left_rtx, mask_rtx, source;
1282{
1283 int attributes[3];
1284 unsigned HOST_WIDE_INT mask;
1285 int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
1286 int right, total_shift;
1287 int (*shift_gen_fun) PROTO((int, rtx*)) = gen_shifty_hi_op;
1288
1289 right = attributes[0];
1290 total_shift = INTVAL (left_rtx) + right;
1291 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
1292 switch (kind)
1293 {
1294 default:
1295 return -1;
1296 case 1:
1297 {
1298 int first = attributes[2];
1299 rtx operands[3];
1300
1301 if (first < 0)
1302 {
7174c937 1303 emit_insn ((mask << right) <= 0xff
afad3d2c
JW
1304 ? gen_zero_extendqisi2(dest,
1305 gen_lowpart (QImode, source))
1306 : gen_zero_extendhisi2(dest,
1307 gen_lowpart (HImode, source)));
8d481241
JW
1308 source = dest;
1309 }
1310 if (source != dest)
1311 emit_insn (gen_movsi (dest, source));
1312 operands[0] = dest;
1313 if (right)
1314 {
1315 operands[2] = GEN_INT (right);
1316 gen_shifty_hi_op (LSHIFTRT, operands);
1317 }
1318 if (first > 0)
1319 {
1320 operands[2] = GEN_INT (first);
1321 gen_shifty_hi_op (ASHIFT, operands);
1322 total_shift -= first;
1323 mask <<= first;
1324 }
1325 if (first >= 0)
7174c937 1326 emit_insn (mask <= 0xff
afad3d2c
JW
1327 ? gen_zero_extendqisi2(dest, gen_lowpart (QImode, dest))
1328 : gen_zero_extendhisi2(dest, gen_lowpart (HImode, dest)));
8d481241
JW
1329 if (total_shift > 0)
1330 {
1331 operands[2] = GEN_INT (total_shift);
1332 gen_shifty_hi_op (ASHIFT, operands);
1333 }
1334 break;
1335 }
1336 case 4:
1337 shift_gen_fun = gen_shifty_op;
1338 case 2:
1339 case 3:
24c50999
JR
1340 /* If the topmost bit that matters is set, set the topmost bits
1341 that don't matter. This way, we might be able to get a shorter
1342 signed constant. */
1343 if (mask & ((HOST_WIDE_INT)1 << 31 - total_shift))
1344 mask |= (HOST_WIDE_INT)~0 << (31 - total_shift);
8d481241
JW
1345 /* Don't expand fine-grained when combining, because that will
1346 make the pattern fail. */
1347 if (rtx_equal_function_value_matters
1348 || reload_in_progress || reload_completed)
1349 {
1350 rtx operands[3];
1351
1352 if (right)
1353 {
1354 emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
1355 source = dest;
1356 }
1357 emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
afad3d2c
JW
1358 if (total_shift)
1359 {
1360 operands[0] = dest;
1361 operands[1] = dest;
1362 operands[2] = GEN_INT (total_shift);
1363 shift_gen_fun (ASHIFT, operands);
1364 }
8d481241
JW
1365 break;
1366 }
1367 else
1368 {
1369 int neg = 0;
1370 if (kind != 4 && total_shift < 16)
1371 {
1372 neg = -ext_shift_amounts[total_shift][1];
1373 if (neg > 0)
1374 neg -= ext_shift_amounts[total_shift][2];
1375 else
1376 neg = 0;
1377 }
1378 emit_insn (gen_and_shl_scratch (dest, source,
1379 GEN_INT (right),
1380 GEN_INT (mask),
1381 GEN_INT (total_shift + neg),
1382 GEN_INT (neg)));
1383 emit_insn (gen_movsi (dest, dest));
1384 break;
1385 }
1386 }
1387 return 0;
1388}
1389
1390/* Try to find a good way to implement the combiner pattern
1391 [(set (match_operand:SI 0 "register_operand" "=r")
1392 (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1393 (match_operand:SI 2 "const_int_operand" "n")
1394 (match_operand:SI 3 "const_int_operand" "n")
1395 (const_int 0)))
1396 (clobber (reg:SI 18))]
1397 LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1398 return 0 for simple left / right shift combination.
1399 return 1 for left shift / 8 bit sign extend / left shift.
1400 return 2 for left shift / 16 bit sign extend / left shift.
1401 return 3 for left shift / 8 bit sign extend / shift / sign extend.
1402 return 4 for left shift / 16 bit sign extend / shift / sign extend.
1403 return 5 for left shift / 16 bit sign extend / right shift
1404 return 6 for < 8 bit sign extend / left shift.
1405 return 7 for < 8 bit sign extend / left shift / single right shift.
1406 If COSTP is nonzero, assign the calculated cost to *COSTP. */
1407
1408int
1409shl_sext_kind (left_rtx, size_rtx, costp)
1410 rtx left_rtx, size_rtx;
1411 int *costp;
1412{
1413 int left, size, insize, ext;
1414 int cost, best_cost;
1415 int kind;
1416
1417 left = INTVAL (left_rtx);
1418 size = INTVAL (size_rtx);
1419 insize = size - left;
1420 if (insize <= 0)
1421 abort ();
1422 /* Default to left / right shift. */
1423 kind = 0;
1424 best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
1425 if (size <= 16)
1426 {
1427 /* 16 bit shift / sign extend / 16 bit shift */
1428 cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
afad3d2c
JW
1429 /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
1430 below, by alternative 3 or something even better. */
8d481241
JW
1431 if (cost < best_cost)
1432 {
1433 kind = 5;
1434 best_cost = cost;
1435 }
1436 }
1437 /* Try a plain sign extend between two shifts. */
1438 for (ext = 16; ext >= insize; ext -= 8)
1439 {
1440 if (ext <= size)
1441 {
1442 cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
1443 if (cost < best_cost)
1444 {
1445 kind = ext / 8U;
1446 best_cost = cost;
1447 }
1448 }
ae9d19c0
JR
1449 /* Check if we can do a sloppy shift with a final signed shift
1450 restoring the sign. */
1451 if (EXT_SHIFT_SIGNED (size - ext))
1452 cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
1453 /* If not, maybe it's still cheaper to do the second shift sloppy,
1454 and do a final sign extend? */
1455 else if (size <= 16)
1456 cost = ext_shift_insns[ext - insize] + 1
1457 + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
1458 else
1459 continue;
1460 if (cost < best_cost)
8d481241 1461 {
ae9d19c0
JR
1462 kind = ext / 8U + 2;
1463 best_cost = cost;
8d481241
JW
1464 }
1465 }
1466 /* Check if we can sign extend in r0 */
1467 if (insize < 8)
1468 {
1469 cost = 3 + shift_insns[left];
1470 if (cost < best_cost)
1471 {
1472 kind = 6;
1473 best_cost = cost;
1474 }
1475 /* Try the same with a final signed shift. */
1476 if (left < 31)
1477 {
1478 cost = 3 + ext_shift_insns[left + 1] + 1;
1479 if (cost < best_cost)
1480 {
1481 kind = 7;
1482 best_cost = cost;
1483 }
1484 }
1485 }
1486 if (TARGET_SH3)
1487 {
1488 /* Try to use a dynamic shift. */
1245df60 1489 cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
8d481241
JW
1490 if (cost < best_cost)
1491 {
1492 kind = 0;
1493 best_cost = cost;
1494 }
1495 }
1496 if (costp)
1497 *costp = cost;
1498 return kind;
1499}
1500
1501/* Function to be used in the length attribute of the instructions
1502 implementing this pattern. */
1503
1504int
1505shl_sext_length (insn)
1506 rtx insn;
1507{
1508 rtx set_src, left_rtx, size_rtx;
1509 int cost;
1510
1511 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1512 left_rtx = XEXP (XEXP (set_src, 0), 1);
1513 size_rtx = XEXP (set_src, 1);
1514 shl_sext_kind (left_rtx, size_rtx, &cost);
1515 return cost;
1516}
1517
1518/* Generate rtl for this pattern */
1519
1520int
1521gen_shl_sext (dest, left_rtx, size_rtx, source)
1522 rtx dest, left_rtx, size_rtx, source;
1523{
1524 int kind;
d00d338c 1525 int left, size, insize, cost;
8d481241
JW
1526 rtx operands[3];
1527
d00d338c 1528 kind = shl_sext_kind (left_rtx, size_rtx, &cost);
8d481241
JW
1529 left = INTVAL (left_rtx);
1530 size = INTVAL (size_rtx);
1531 insize = size - left;
1532 switch (kind)
1533 {
1534 case 1:
1535 case 2:
1536 case 3:
1537 case 4:
1538 {
1539 int ext = kind & 1 ? 8 : 16;
1540 int shift2 = size - ext;
1541
1542 /* Don't expand fine-grained when combining, because that will
1543 make the pattern fail. */
1544 if (! rtx_equal_function_value_matters
1545 && ! reload_in_progress && ! reload_completed)
1546 {
1547 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1548 emit_insn (gen_movsi (dest, source));
1549 break;
1550 }
1551 if (dest != source)
1552 emit_insn (gen_movsi (dest, source));
1553 operands[0] = dest;
afad3d2c
JW
1554 if (ext - insize)
1555 {
1556 operands[2] = GEN_INT (ext - insize);
1557 gen_shifty_hi_op (ASHIFT, operands);
1558 }
8d481241 1559 emit_insn (kind & 1
afad3d2c
JW
1560 ? gen_extendqisi2(dest, gen_lowpart (QImode, dest))
1561 : gen_extendhisi2(dest, gen_lowpart (HImode, dest)));
8d481241
JW
1562 if (kind <= 2)
1563 {
afad3d2c
JW
1564 if (shift2)
1565 {
1566 operands[2] = GEN_INT (shift2);
1567 gen_shifty_op (ASHIFT, operands);
1568 }
8d481241
JW
1569 }
1570 else
1571 {
afad3d2c 1572 if (shift2 > 0)
8d481241 1573 {
ae9d19c0
JR
1574 if (EXT_SHIFT_SIGNED (shift2))
1575 {
1576 operands[2] = GEN_INT (shift2 + 1);
1577 gen_shifty_op (ASHIFT, operands);
1578 operands[2] = GEN_INT (1);
1579 gen_shifty_op (ASHIFTRT, operands);
1580 break;
1581 }
8d481241
JW
1582 operands[2] = GEN_INT (shift2);
1583 gen_shifty_hi_op (ASHIFT, operands);
1584 }
afad3d2c 1585 else if (shift2)
8d481241
JW
1586 {
1587 operands[2] = GEN_INT (-shift2);
1588 gen_shifty_hi_op (LSHIFTRT, operands);
1589 }
1590 emit_insn (size <= 8
afad3d2c
JW
1591 ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
1592 : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
8d481241
JW
1593 }
1594 break;
1595 }
1596 case 5:
afad3d2c
JW
1597 {
1598 int i = 16 - size;
1599 emit_insn (gen_shl_sext_ext (dest, source, GEN_INT (16 - insize),
1600 GEN_INT (16)));
1601 /* Don't use gen_ashrsi3 because it generates new pseudos. */
1602 while (--i >= 0)
1603 gen_ashift (ASHIFTRT, 1, dest);
1604 break;
1605 }
8d481241
JW
1606 case 6:
1607 case 7:
1608 /* Don't expand fine-grained when combining, because that will
1609 make the pattern fail. */
1610 if (! rtx_equal_function_value_matters
1611 && ! reload_in_progress && ! reload_completed)
1612 {
1613 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1614 emit_insn (gen_movsi (dest, source));
1615 break;
1616 }
1617 emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
1618 emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
1619 emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
1620 operands[0] = dest;
1621 operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
1622 gen_shifty_op (ASHIFT, operands);
1623 if (kind == 7)
1624 emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
1625 break;
1626 default:
1627 return -1;
1628 }
1629 return 0;
1630}
8aa2a305
JW
1631\f
1632/* The SH cannot load a large constant into a register, constants have to
1633 come from a pc relative load. The reference of a pc relative load
1634 instruction must be less than 1k infront of the instruction. This
1635 means that we often have to dump a constant inside a function, and
1636 generate code to branch around it.
bc45ade3 1637
8aa2a305
JW
1638 It is important to minimize this, since the branches will slow things
1639 down and make things bigger.
1640
1641 Worst case code looks like:
1642
1643 mov.l L1,rn
1644 bra L2
1645 nop
1646 align
1647 L1: .long value
1648 L2:
1649 ..
1650
1651 mov.l L3,rn
1652 bra L4
1653 nop
1654 align
1655 L3: .long value
1656 L4:
1657 ..
1658
1659 We fix this by performing a scan before scheduling, which notices which
1660 instructions need to have their operands fetched from the constant table
1661 and builds the table.
1662
1663 The algorithm is:
1664
1665 scan, find an instruction which needs a pcrel move. Look forward, find the
1666 last barrier which is within MAX_COUNT bytes of the requirement.
1667 If there isn't one, make one. Process all the instructions between
1668 the find and the barrier.
1669
1670 In the above example, we can tell that L3 is within 1k of L1, so
1671 the first move can be shrunk from the 3 insn+constant sequence into
1672 just 1 insn, and the constant moved to L3 to make:
1673
1674 mov.l L1,rn
1675 ..
1676 mov.l L3,rn
1677 bra L4
1678 nop
1679 align
1680 L3:.long value
1681 L4:.long value
1682
1683 Then the second move becomes the target for the shortening process. */
1684
1685typedef struct
1686{
1687 rtx value; /* Value in table. */
1688 rtx label; /* Label of value. */
1689 enum machine_mode mode; /* Mode of value. */
1690} pool_node;
1691
1692/* The maximum number of constants that can fit into one pool, since
1693 the pc relative range is 0...1020 bytes and constants are at least 4
1694 bytes long. */
1695
1696#define MAX_POOL_SIZE (1020/4)
1697static pool_node pool_vector[MAX_POOL_SIZE];
1698static int pool_size;
1699
1700/* ??? If we need a constant in HImode which is the truncated value of a
1701 constant we need in SImode, we could combine the two entries thus saving
1702 two bytes. Is this common enough to be worth the effort of implementing
1703 it? */
1704
1705/* ??? This stuff should be done at the same time that we shorten branches.
1706 As it is now, we must assume that all branches are the maximum size, and
1707 this causes us to almost always output constant pools sooner than
1708 necessary. */
1709
1710/* Add a constant to the pool and return its label. */
1711
1712static rtx
1713add_constant (x, mode)
1714 rtx x;
1715 enum machine_mode mode;
0d7e008e
SC
1716{
1717 int i;
8aa2a305
JW
1718 rtx lab;
1719
1720 /* First see if we've already got it. */
1721 for (i = 0; i < pool_size; i++)
0d7e008e 1722 {
8aa2a305
JW
1723 if (x->code == pool_vector[i].value->code
1724 && mode == pool_vector[i].mode)
0d7e008e 1725 {
8aa2a305
JW
1726 if (x->code == CODE_LABEL)
1727 {
1728 if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1729 continue;
1730 }
1731 if (rtx_equal_p (x, pool_vector[i].value))
1732 return pool_vector[i].label;
0d7e008e 1733 }
0d7e008e 1734 }
b9654711 1735
8aa2a305
JW
1736 /* Need a new one. */
1737 pool_vector[pool_size].value = x;
1738 lab = gen_label_rtx ();
1739 pool_vector[pool_size].mode = mode;
1740 pool_vector[pool_size].label = lab;
1741 pool_size++;
1742 return lab;
0d7e008e 1743}
16bea517
JW
1744
1745/* Output the literal table. */
b9654711 1746
b9654711 1747static void
0d7e008e
SC
1748dump_table (scan)
1749 rtx scan;
b9654711 1750{
0d7e008e 1751 int i;
0d7e008e 1752 int need_align = 1;
b9654711 1753
16bea517 1754 /* Do two passes, first time dump out the HI sized constants. */
b9654711 1755
0d7e008e 1756 for (i = 0; i < pool_size; i++)
b9654711 1757 {
8aa2a305
JW
1758 pool_node *p = &pool_vector[i];
1759
0d7e008e
SC
1760 if (p->mode == HImode)
1761 {
1762 if (need_align)
1763 {
1764 scan = emit_insn_after (gen_align_2 (), scan);
1765 need_align = 0;
1766 }
1767 scan = emit_label_after (p->label, scan);
1768 scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1769 }
b9654711 1770 }
8aa2a305 1771
0d7e008e 1772 need_align = 1;
b9654711 1773
0d7e008e 1774 for (i = 0; i < pool_size; i++)
b9654711 1775 {
8aa2a305 1776 pool_node *p = &pool_vector[i];
b9654711 1777
0d7e008e 1778 switch (p->mode)
b9654711 1779 {
0d7e008e
SC
1780 case HImode:
1781 break;
1782 case SImode:
e577c183 1783 case SFmode:
0d7e008e 1784 if (need_align)
b9654711 1785 {
0d7e008e 1786 need_align = 0;
d3ae8277 1787 scan = emit_label_after (gen_label_rtx (), scan);
0d7e008e 1788 scan = emit_insn_after (gen_align_4 (), scan);
b9654711 1789 }
1245df60
R
1790 if (p->label)
1791 scan = emit_label_after (p->label, scan);
0d7e008e
SC
1792 scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1793 break;
e577c183 1794 case DFmode:
0d7e008e
SC
1795 case DImode:
1796 if (need_align)
1797 {
1798 need_align = 0;
d3ae8277 1799 scan = emit_label_after (gen_label_rtx (), scan);
0d7e008e
SC
1800 scan = emit_insn_after (gen_align_4 (), scan);
1801 }
1245df60
R
1802 if (p->label)
1803 scan = emit_label_after (p->label, scan);
0d7e008e
SC
1804 scan = emit_insn_after (gen_consttable_8 (p->value), scan);
1805 break;
1806 default:
1807 abort ();
1808 break;
b9654711
SC
1809 }
1810 }
b9654711 1811
0d7e008e
SC
1812 scan = emit_insn_after (gen_consttable_end (), scan);
1813 scan = emit_barrier_after (scan);
1814 pool_size = 0;
1815}
b9654711 1816
8aa2a305
JW
1817/* Return non-zero if constant would be an ok source for a
1818 mov.w instead of a mov.l. */
b9654711 1819
16bea517 1820static int
8aa2a305 1821hi_const (src)
0d7e008e 1822 rtx src;
0d7e008e 1823{
8aa2a305
JW
1824 return (GET_CODE (src) == CONST_INT
1825 && INTVAL (src) >= -32768
1826 && INTVAL (src) <= 32767);
b9654711
SC
1827}
1828
8aa2a305
JW
1829/* Non-zero if the insn is a move instruction which needs to be fixed. */
1830
1831/* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1832 CONST_DOUBLE input value is CONST_OK_FOR_I. For a SFmode move, we don't
1833 need to fix it if the input value is CONST_OK_FOR_I. */
1834
1835static int
1836broken_move (insn)
1837 rtx insn;
b9654711 1838{
e577c183
JR
1839 if (GET_CODE (insn) == INSN)
1840 {
1841 rtx pat = PATTERN (insn);
1842 if (GET_CODE (pat) == PARALLEL)
1843 pat = XVECEXP (pat, 0, 0);
1844 if (GET_CODE (pat) == SET
1845 /* We can load any 8 bit value if we don't care what the high
1846 order bits end up as. */
1847 && GET_MODE (SET_DEST (pat)) != QImode
1848 && CONSTANT_P (SET_SRC (pat))
1245df60
R
1849 && ! (TARGET_SH3E
1850 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
e577c183
JR
1851 && (fp_zero_operand (SET_SRC (pat))
1852 || fp_one_operand (SET_SRC (pat)))
1853 && GET_CODE (SET_DEST (pat)) == REG
1854 && REGNO (SET_DEST (pat)) >= FIRST_FP_REG
1855 && REGNO (SET_DEST (pat)) <= LAST_FP_REG)
1856 && (GET_CODE (SET_SRC (pat)) != CONST_INT
1857 || ! CONST_OK_FOR_I (INTVAL (SET_SRC (pat)))))
1858 return 1;
1859 }
d3ae8277 1860
8aa2a305 1861 return 0;
b9654711 1862}
b9654711 1863
1245df60
R
1864static int
1865mova_p (insn)
1866 rtx insn;
1867{
1868 return (GET_CODE (insn) == INSN
1869 && GET_CODE (PATTERN (insn)) == SET
1870 && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
1871 && XINT (SET_SRC (PATTERN (insn)), 1) == 1);
1872}
1873
8aa2a305
JW
1874/* Find the last barrier from insn FROM which is close enough to hold the
1875 constant pool. If we can't find one, then create one near the end of
1876 the range. */
b9654711 1877
8aa2a305 1878static rtx
1245df60
R
1879find_barrier (num_mova, mova, from)
1880 int num_mova;
1881 rtx mova, from;
b9654711 1882{
0d7e008e
SC
1883 int count_si = 0;
1884 int count_hi = 0;
1885 int found_hi = 0;
1886 int found_si = 0;
33f7f353
JR
1887 int hi_align = 2;
1888 int si_align = 2;
1245df60
R
1889 int leading_mova = num_mova;
1890 rtx barrier_before_mova, found_barrier = 0, good_barrier = 0;
c17f53a8
JW
1891 int si_limit;
1892 int hi_limit;
8aa2a305
JW
1893
1894 /* For HImode: range is 510, add 4 because pc counts from address of
1895 second instruction after this one, subtract 2 for the jump instruction
3adbde60
JW
1896 that we may need to emit before the table, subtract 2 for the instruction
1897 that fills the jump delay slot (in very rare cases, reorg will take an
1898 instruction from after the constant pool or will leave the delay slot
1899 empty). This gives 510.
8aa2a305
JW
1900 For SImode: range is 1020, add 4 because pc counts from address of
1901 second instruction after this one, subtract 2 in case pc is 2 byte
1902 aligned, subtract 2 for the jump instruction that we may need to emit
3adbde60
JW
1903 before the table, subtract 2 for the instruction that fills the jump
1904 delay slot. This gives 1018. */
c17f53a8 1905
1245df60 1906 /* The branch will always be shortened now that the reference address for
956d6950 1907 forward branches is the successor address, thus we need no longer make
1245df60 1908 adjustments to the [sh]i_limit for -O0. */
c17f53a8 1909
1245df60
R
1910 si_limit = 1018;
1911 hi_limit = 510;
e4fa6b06 1912
c17f53a8 1913 while (from && count_si < si_limit && count_hi < hi_limit)
0d7e008e 1914 {
33f7f353
JR
1915 int inc = get_attr_length (from);
1916 int new_align = 1;
1245df60 1917
33f7f353 1918 if (GET_CODE (from) == CODE_LABEL)
77008a44
R
1919 {
1920 if (optimize)
1921 new_align = 1 << label_to_alignment (from);
1922 else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
1923 new_align = 1 << barrier_align (from);
1924 else
1925 new_align = 1;
1926 inc = 0;
1927 }
8aa2a305 1928
0d7e008e 1929 if (GET_CODE (from) == BARRIER)
1245df60 1930 {
33f7f353 1931
1245df60 1932 found_barrier = from;
33f7f353 1933
956d6950 1934 /* If we are at the end of the function, or in front of an alignment
1245df60
R
1935 instruction, we need not insert an extra alignment. We prefer
1936 this kind of barrier. */
33f7f353 1937 if (barrier_align (from) > 2)
1245df60
R
1938 good_barrier = from;
1939 }
8aa2a305 1940
8aa2a305 1941 if (broken_move (from))
0d7e008e 1942 {
1245df60
R
1943 rtx pat, src, dst;
1944 enum machine_mode mode;
1945
1946 pat = PATTERN (from);
1947 if (GET_CODE (pat) == PARALLEL)
1948 pat = XVECEXP (pat, 0, 0);
1949 src = SET_SRC (pat);
1950 dst = SET_DEST (pat);
1951 mode = GET_MODE (dst);
c17f53a8
JW
1952
1953 /* We must explicitly check the mode, because sometimes the
1954 front end will generate code to load unsigned constants into
1955 HImode targets without properly sign extending them. */
1956 if (mode == HImode || (mode == SImode && hi_const (src)))
00e94d65 1957 {
1245df60 1958 found_hi += 2;
00e94d65
RK
1959 /* We put the short constants before the long constants, so
1960 we must count the length of short constants in the range
1961 for the long constants. */
1962 /* ??? This isn't optimal, but is easy to do. */
1245df60 1963 si_limit -= 2;
00e94d65 1964 }
0d7e008e 1965 else
1245df60 1966 {
33f7f353
JR
1967 while (si_align > 2 && found_si + si_align - 2 > count_si)
1968 si_align >>= 1;
1245df60
R
1969 if (found_si > count_si)
1970 count_si = found_si;
1971 found_si += GET_MODE_SIZE (mode);
1972 if (num_mova)
1973 si_limit -= GET_MODE_SIZE (mode);
1974 }
0d7e008e 1975 }
5325c0fa 1976
33f7f353 1977 if (mova_p (from))
1245df60
R
1978 {
1979 if (! num_mova++)
1980 {
1981 leading_mova = 0;
1982 mova = from;
1983 barrier_before_mova = good_barrier ? good_barrier : found_barrier;
1984 }
1985 if (found_si > count_si)
1986 count_si = found_si;
1987 }
5325c0fa
JW
1988 else if (GET_CODE (from) == JUMP_INSN
1989 && (GET_CODE (PATTERN (from)) == ADDR_VEC
1990 || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1245df60
R
1991 {
1992 if (num_mova)
1993 num_mova--;
a0798779 1994 if (barrier_align (next_real_insn (from)) == CACHE_LOG)
1245df60 1995 {
38e01259 1996 /* We have just passed the barrier in front of the
a0798779
R
1997 ADDR_DIFF_VEC, which is stored in found_barrier. Since
1998 the ADDR_DIFF_VEC is accessed as data, just like our pool
1999 constants, this is a good opportunity to accommodate what
2000 we have gathered so far.
1245df60
R
2001 If we waited any longer, we could end up at a barrier in
2002 front of code, which gives worse cache usage for separated
2003 instruction / data caches. */
a0798779 2004 good_barrier = found_barrier;
1245df60
R
2005 break;
2006 }
a0798779
R
2007 else
2008 {
2009 rtx body = PATTERN (from);
2010 inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
2011 }
1245df60 2012 }
5325c0fa 2013
0d7e008e 2014 if (found_si)
33f7f353
JR
2015 {
2016 if (new_align > si_align)
2017 {
1f3fa20e 2018 si_limit -= count_si - 1 & new_align - si_align;
33f7f353
JR
2019 si_align = new_align;
2020 }
1f3fa20e 2021 count_si = count_si + new_align - 1 & -new_align;
33f7f353
JR
2022 count_si += inc;
2023 }
0d7e008e 2024 if (found_hi)
33f7f353
JR
2025 {
2026 if (new_align > hi_align)
2027 {
1f3fa20e 2028 hi_limit -= count_hi - 1 & new_align - hi_align;
33f7f353
JR
2029 hi_align = new_align;
2030 }
1f3fa20e 2031 count_hi = count_hi + new_align - 1 & -new_align;
33f7f353
JR
2032 count_hi += inc;
2033 }
0d7e008e
SC
2034 from = NEXT_INSN (from);
2035 }
2036
1245df60
R
2037 if (num_mova)
2038 if (leading_mova)
2039 {
2040 /* Try as we might, the leading mova is out of range. Change
2041 it into a load (which will become a pcload) and retry. */
2042 SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2043 INSN_CODE (mova) = -1;
2044 return find_barrier (0, 0, mova);
2045 }
2046 else
2047 {
2048 /* Insert the constant pool table before the mova instruction,
2049 to prevent the mova label reference from going out of range. */
2050 from = mova;
2051 good_barrier = found_barrier = barrier_before_mova;
2052 }
5325c0fa 2053
1245df60
R
2054 if (found_barrier)
2055 {
33f7f353 2056 if (good_barrier && next_real_insn (found_barrier))
1245df60 2057 found_barrier = good_barrier;
1245df60
R
2058 }
2059 else
b9654711 2060 {
8aa2a305
JW
2061 /* We didn't find a barrier in time to dump our stuff,
2062 so we'll make one. */
0d7e008e 2063 rtx label = gen_label_rtx ();
8aa2a305 2064
5dbcc9c0
RK
2065 /* If we exceeded the range, then we must back up over the last
2066 instruction we looked at. Otherwise, we just need to undo the
2067 NEXT_INSN at the end of the loop. */
c17f53a8 2068 if (count_hi > hi_limit || count_si > si_limit)
5dbcc9c0
RK
2069 from = PREV_INSN (PREV_INSN (from));
2070 else
2071 from = PREV_INSN (from);
2072
8aa2a305
JW
2073 /* Walk back to be just before any jump or label.
2074 Putting it before a label reduces the number of times the branch
2075 around the constant pool table will be hit. Putting it before
2076 a jump makes it more likely that the bra delay slot will be
2077 filled. */
2078 while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
8e87e161 2079 || GET_CODE (from) == CODE_LABEL)
8aa2a305
JW
2080 from = PREV_INSN (from);
2081
0d7e008e
SC
2082 from = emit_jump_insn_after (gen_jump (label), from);
2083 JUMP_LABEL (from) = label;
e081ed26 2084 LABEL_NUSES (label) = 1;
0d7e008e
SC
2085 found_barrier = emit_barrier_after (from);
2086 emit_label_after (label, found_barrier);
b9654711 2087 }
b9654711 2088
8aa2a305 2089 return found_barrier;
0d7e008e 2090}
b9654711 2091
4787bce0
JW
2092/* If the instruction INSN is implemented by a special function, and we can
2093 positively find the register that is used to call the sfunc, and this
2094 register is not used anywhere else in this instruction - except as the
2095 destination of a set, return this register; else, return 0. */
1245df60 2096rtx
4787bce0
JW
2097sfunc_uses_reg (insn)
2098 rtx insn;
2099{
2100 int i;
2101 rtx pattern, part, reg_part, reg;
2102
2103 if (GET_CODE (insn) != INSN)
2104 return 0;
2105 pattern = PATTERN (insn);
2106 if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
2107 return 0;
2108
2109 for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2110 {
2111 part = XVECEXP (pattern, 0, i);
1245df60 2112 if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
4787bce0
JW
2113 reg_part = part;
2114 }
2115 if (! reg_part)
2116 return 0;
2117 reg = XEXP (reg_part, 0);
2118 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2119 {
2120 part = XVECEXP (pattern, 0, i);
2121 if (part == reg_part)
2122 continue;
2123 if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
2124 && GET_CODE (SET_DEST (part)) == REG)
2125 ? SET_SRC (part) : part)))
2126 return 0;
2127 }
2128 return reg;
2129}
2130
933c3ba3
JW
2131/* See if the only way in which INSN uses REG is by calling it, or by
2132 setting it while calling it. Set *SET to a SET rtx if the register
2133 is set by INSN. */
2134
2135static int
2136noncall_uses_reg (reg, insn, set)
2137 rtx reg;
2138 rtx insn;
2139 rtx *set;
2140{
4787bce0 2141 rtx pattern, reg2;
933c3ba3
JW
2142
2143 *set = NULL_RTX;
2144
4787bce0
JW
2145 reg2 = sfunc_uses_reg (insn);
2146 if (reg2 && REGNO (reg2) == REGNO (reg))
2147 {
2148 pattern = single_set (insn);
2149 if (pattern
2150 && GET_CODE (SET_DEST (pattern)) == REG
2151 && REGNO (reg) == REGNO (SET_DEST (pattern)))
2152 *set = pattern;
2153 return 0;
2154 }
933c3ba3
JW
2155 if (GET_CODE (insn) != CALL_INSN)
2156 {
2157 /* We don't use rtx_equal_p because we don't care if the mode is
2158 different. */
2159 pattern = single_set (insn);
2160 if (pattern
2161 && GET_CODE (SET_DEST (pattern)) == REG
2162 && REGNO (reg) == REGNO (SET_DEST (pattern)))
2163 {
4787bce0
JW
2164 rtx par, part;
2165 int i;
2166
933c3ba3 2167 *set = pattern;
4787bce0
JW
2168 par = PATTERN (insn);
2169 if (GET_CODE (par) == PARALLEL)
2170 for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
2171 {
2172 part = XVECEXP (par, 0, i);
2173 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
2174 return 1;
2175 }
2176 return reg_mentioned_p (reg, SET_SRC (pattern));
933c3ba3
JW
2177 }
2178
2179 return 1;
2180 }
2181
2182 pattern = PATTERN (insn);
2183
2184 if (GET_CODE (pattern) == PARALLEL)
2185 {
2186 int i;
2187
2188 for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2189 if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
2190 return 1;
2191 pattern = XVECEXP (pattern, 0, 0);
2192 }
2193
2194 if (GET_CODE (pattern) == SET)
2195 {
2196 if (reg_mentioned_p (reg, SET_DEST (pattern)))
2197 {
2198 /* We don't use rtx_equal_p, because we don't care if the
2199 mode is different. */
2200 if (GET_CODE (SET_DEST (pattern)) != REG
2201 || REGNO (reg) != REGNO (SET_DEST (pattern)))
2202 return 1;
2203
2204 *set = pattern;
2205 }
2206
2207 pattern = SET_SRC (pattern);
2208 }
2209
2210 if (GET_CODE (pattern) != CALL
2211 || GET_CODE (XEXP (pattern, 0)) != MEM
2212 || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
2213 return 1;
2214
2215 return 0;
2216}
2217
1245df60
R
2218/* Given a X, a pattern of an insn or a part of it, return a mask of used
2219 general registers. Bits 0..15 mean that the respective registers
2220 are used as inputs in the instruction. Bits 16..31 mean that the
2221 registers 0..15, respectively, are used as outputs, or are clobbered.
2222 IS_DEST should be set to 16 if X is the destination of a SET, else to 0. */
2223int
2224regs_used (x, is_dest)
2225 rtx x; int is_dest;
2226{
2227 enum rtx_code code;
2228 char *fmt;
2229 int i, used = 0;
2230
2231 if (! x)
2232 return used;
2233 code = GET_CODE (x);
2234 switch (code)
2235 {
2236 case REG:
2237 if (REGNO (x) < 16)
2238 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2239 << (REGNO (x) + is_dest));
2240 return 0;
2241 case SUBREG:
2242 {
2243 rtx y = SUBREG_REG (x);
2244
2245 if (GET_CODE (y) != REG)
2246 break;
2247 if (REGNO (y) < 16)
2248 return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2249 << (REGNO (y) + SUBREG_WORD (x) + is_dest));
2250 return 0;
2251 }
2252 case SET:
2253 return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
2254 case RETURN:
2255 /* If there was a return value, it must have been indicated with USE. */
2256 return 0x00ffff00;
2257 case CLOBBER:
2258 is_dest = 1;
2259 break;
2260 case MEM:
2261 is_dest = 0;
2262 break;
2263 case CALL:
2264 used |= 0x00ff00f0;
2265 break;
2266 }
2267
2268 fmt = GET_RTX_FORMAT (code);
2269
2270 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2271 {
2272 if (fmt[i] == 'E')
2273 {
2274 register int j;
2275 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2276 used |= regs_used (XVECEXP (x, i, j), is_dest);
2277 }
2278 else if (fmt[i] == 'e')
2279 used |= regs_used (XEXP (x, i), is_dest);
2280 }
2281 return used;
2282}
2283
2284/* Create an instruction that prevents redirection of a conditional branch
956d6950 2285 to the destination of the JUMP with address ADDR.
1245df60
R
2286 If the branch needs to be implemented as an indirect jump, try to find
2287 a scratch register for it.
2288 If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
2289 If any preceding insn that doesn't fit into a delay slot is good enough,
2290 pass 1. Pass 2 if a definite blocking insn is needed.
2291 -1 is used internally to avoid deep recursion.
2292 If a blocking instruction is made or recognized, return it. */
2293
2294static rtx
2295gen_block_redirect (jump, addr, need_block)
2296 rtx jump;
2297 int addr, need_block;
2298{
2299 int dead = 0;
2300 rtx prev = prev_nonnote_insn (jump);
2301 rtx dest;
2302
2303 /* First, check if we already have an instruction that satisfies our need. */
2304 if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
2305 {
2306 if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
2307 return prev;
2308 if (GET_CODE (PATTERN (prev)) == USE
2309 || GET_CODE (PATTERN (prev)) == CLOBBER
2310 || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
2311 prev = jump;
2312 else if ((need_block &= ~1) < 0)
2313 return prev;
2314 else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
2315 need_block = 0;
2316 }
2317 /* We can't use JUMP_LABEL here because it might be undefined
2318 when not optimizing. */
2319 dest = XEXP (SET_SRC (PATTERN (jump)), 0);
2320 /* If the branch is out of range, try to find a scratch register for it. */
2321 if (optimize
2322 && (insn_addresses[INSN_UID (dest)] - addr + 4092U > 4092 + 4098))
2323 {
2324 rtx scan;
2325 /* Don't look for the stack pointer as a scratch register,
956d6950 2326 it would cause trouble if an interrupt occurred. */
1245df60
R
2327 unsigned try = 0x7fff, used;
2328 int jump_left = flag_expensive_optimizations + 1;
2329
2330 /* It is likely that the most recent eligible instruction is wanted for
2331 the delay slot. Therefore, find out which registers it uses, and
2332 try to avoid using them. */
2333
2334 for (scan = jump; scan = PREV_INSN (scan); )
2335 {
2336 enum rtx_code code;
2337
2338 if (INSN_DELETED_P (scan))
2339 continue;
2340 code = GET_CODE (scan);
2341 if (code == CODE_LABEL || code == JUMP_INSN)
2342 break;
2343 if (code == INSN
2344 && GET_CODE (PATTERN (scan)) != USE
2345 && GET_CODE (PATTERN (scan)) != CLOBBER
2346 && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
2347 {
2348 try &= ~regs_used (PATTERN (scan), 0);
2349 break;
2350 }
2351 }
2352 for (used = dead = 0, scan = JUMP_LABEL (jump); scan = NEXT_INSN (scan); )
2353 {
2354 enum rtx_code code;
2355
2356 if (INSN_DELETED_P (scan))
2357 continue;
2358 code = GET_CODE (scan);
2359 if (GET_RTX_CLASS (code) == 'i')
2360 {
2361 used |= regs_used (PATTERN (scan), 0);
2362 if (code == CALL_INSN)
2363 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
2364 dead |= (used >> 16) & ~used;
2365 if (dead & try)
2366 {
2367 dead &= try;
2368 break;
2369 }
2370 if (code == JUMP_INSN)
2371 if (jump_left-- && simplejump_p (scan))
2372 scan = JUMP_LABEL (scan);
2373 else
2374 break;
2375 }
2376 }
2377 /* Mask out the stack pointer again, in case it was
2378 the only 'free' register we have found. */
2379 dead &= 0x7fff;
2380 }
2381 /* If the immediate destination is still in range, check for possible
2382 threading with a jump beyond the delay slot insn.
2383 Don't check if we are called recursively; the jump has been or will be
956d6950 2384 checked in a different invocation then. */
1245df60
R
2385
2386 else if (optimize && need_block >= 0)
2387 {
2388 rtx next = next_active_insn (next_active_insn (dest));
2389 if (next && GET_CODE (next) == JUMP_INSN
2390 && GET_CODE (PATTERN (next)) == SET
2391 && recog_memoized (next) == CODE_FOR_jump)
2392 {
2393 dest = JUMP_LABEL (next);
2394 if (dest
2395 && insn_addresses[INSN_UID (dest)] - addr + 4092U > 4092 + 4098)
2396 gen_block_redirect (next, insn_addresses[INSN_UID (next)], -1);
2397 }
2398 }
2399
2400 if (dead)
2401 {
2402 rtx reg = gen_rtx (REG, SImode, exact_log2 (dead & -dead));
2403
2404 /* It would be nice if we could convert the jump into an indirect
956d6950 2405 jump / far branch right now, and thus exposing all constituent
1245df60
R
2406 instructions to further optimization. However, reorg uses
2407 simplejump_p to determine if there is an unconditional jump where
2408 it should try to schedule instructions from the target of the
2409 branch; simplejump_p fails for indirect jumps even if they have
2410 a JUMP_LABEL. */
2411 rtx insn = emit_insn_before (gen_indirect_jump_scratch
2412 (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
2413 , jump);
2414 INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
2415 return insn;
2416 }
2417 else if (need_block)
2418 /* We can't use JUMP_LABEL here because it might be undefined
2419 when not optimizing. */
2420 return emit_insn_before (gen_block_branch_redirect
2421 (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
2422 , jump);
2423 return prev;
2424}
2425
2426#define CONDJUMP_MIN -252
2427#define CONDJUMP_MAX 262
2428struct far_branch
2429{
2430 /* A label (to be placed) in front of the jump
2431 that jumps to our ultimate destination. */
2432 rtx near_label;
2433 /* Where we are going to insert it if we cannot move the jump any farther,
2434 or the jump itself if we have picked up an existing jump. */
2435 rtx insert_place;
2436 /* The ultimate destination. */
2437 rtx far_label;
2438 struct far_branch *prev;
2439 /* If the branch has already been created, its address;
2440 else the address of its first prospective user. */
2441 int address;
2442};
2443
2444enum mdep_reorg_phase_e mdep_reorg_phase;
2445void
2446gen_far_branch (bp)
2447 struct far_branch *bp;
2448{
2449 rtx insn = bp->insert_place;
2450 rtx jump;
2451 rtx label = gen_label_rtx ();
2452
2453 emit_label_after (label, insn);
2454 if (bp->far_label)
2455 {
2456 jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
2457 LABEL_NUSES (bp->far_label)++;
2458 }
2459 else
2460 jump = emit_jump_insn_after (gen_return (), insn);
2461 emit_label_after (bp->near_label, insn);
2462 JUMP_LABEL (jump) = bp->far_label;
2463 if (! invert_jump (insn, label))
2464 abort ();
2465 /* Prevent reorg from undoing our splits. */
2466 gen_block_redirect (jump, bp->address += 2, 2);
2467}
2468
1245df60
R
2469/* Fix up ADDR_DIFF_VECs. */
2470void
2471fixup_addr_diff_vecs (first)
2472 rtx first;
2473{
2474 rtx insn;
33f7f353 2475
1245df60
R
2476 for (insn = first; insn; insn = NEXT_INSN (insn))
2477 {
33f7f353 2478 rtx vec_lab, pat, prev, prevpat, x;
1245df60
R
2479
2480 if (GET_CODE (insn) != JUMP_INSN
2481 || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
2482 continue;
2483 pat = PATTERN (insn);
33f7f353 2484 vec_lab = XEXP (XEXP (pat, 0), 0);
1245df60 2485
33f7f353
JR
2486 /* Search the matching casesi_jump_2. */
2487 for (prev = vec_lab; ; prev = PREV_INSN (prev))
1245df60 2488 {
33f7f353
JR
2489 if (GET_CODE (prev) != JUMP_INSN)
2490 continue;
2491 prevpat = PATTERN (prev);
2492 if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
2493 continue;
2494 x = XVECEXP (prevpat, 0, 1);
2495 if (GET_CODE (x) != USE)
2496 continue;
2497 x = XEXP (x, 0);
2498 if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
2499 break;
1245df60 2500 }
33f7f353
JR
2501 /* Fix up the ADDR_DIF_VEC to be relative
2502 to the reference address of the braf. */
2503 XEXP (XEXP (pat, 0), 0)
2504 = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
1245df60 2505 }
1245df60
R
2506}
2507
33f7f353
JR
2508/* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
2509 a barrier. Return the base 2 logarithm of the desired alignment. */
1245df60 2510int
33f7f353
JR
2511barrier_align (barrier_or_label)
2512 rtx barrier_or_label;
1245df60 2513{
33f7f353
JR
2514 rtx next = next_real_insn (barrier_or_label), pat, prev;
2515 int slot, credit;
2516
2517 if (! next)
1245df60 2518 return 0;
1245df60 2519
33f7f353 2520 pat = PATTERN (next);
1245df60 2521
33f7f353
JR
2522 if (GET_CODE (pat) == ADDR_DIFF_VEC)
2523 return 2;
1245df60 2524
33f7f353
JR
2525 if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == 1)
2526 /* This is a barrier in front of a constant table. */
2527 return 0;
2528
2529 prev = prev_real_insn (barrier_or_label);
2530 if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
1245df60 2531 {
33f7f353
JR
2532 pat = PATTERN (prev);
2533 /* If this is a very small table, we want to keep the alignment after
2534 the table to the minimum for proper code alignment. */
2535 return ((TARGET_SMALLCODE
2536 || (XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
2537 <= 1 << (CACHE_LOG - 2)))
2538 ? 1 : CACHE_LOG);
1245df60 2539 }
33f7f353
JR
2540
2541 if (TARGET_SMALLCODE)
2542 return 0;
2543
2544 if (! TARGET_SH3 || ! optimize)
2545 return CACHE_LOG;
2546
2547 /* Check if there is an immediately preceding branch to the insn beyond
2548 the barrier. We must weight the cost of discarding useful information
2549 from the current cache line when executing this branch and there is
2550 an alignment, against that of fetching unneeded insn in front of the
2551 branch target when there is no alignment. */
2552
2553 /* PREV is presumed to be the JUMP_INSN for the barrier under
2554 investigation. Skip to the insn before it. */
2555 prev = prev_real_insn (prev);
2556
2557 for (slot = 2, credit = 1 << (CACHE_LOG - 2) + 2;
2558 credit >= 0 && prev && GET_CODE (prev) == INSN;
2559 prev = prev_real_insn (prev))
1245df60 2560 {
33f7f353
JR
2561 if (GET_CODE (PATTERN (prev)) == USE
2562 || GET_CODE (PATTERN (prev)) == CLOBBER)
2563 continue;
2564 if (GET_CODE (PATTERN (prev)) == SEQUENCE)
2565 prev = XVECEXP (PATTERN (prev), 0, 1);
2566 if (slot &&
2567 get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
2568 slot = 0;
2569 credit -= get_attr_length (prev);
2570 }
2571 if (prev
2572 && GET_CODE (prev) == JUMP_INSN
2573 && JUMP_LABEL (prev)
2574 && next_real_insn (JUMP_LABEL (prev)) == next_real_insn (barrier_or_label)
2575 && (credit - slot >= (GET_CODE (SET_SRC (PATTERN (prev))) == PC ? 2 : 0)))
2576 return 0;
2577
2578 return CACHE_LOG;
1245df60
R
2579}
2580
8aa2a305 2581/* Exported to toplev.c.
b9654711 2582
933c3ba3
JW
2583 Do a final pass over the function, just before delayed branch
2584 scheduling. */
b9654711 2585
0d7e008e
SC
2586void
2587machine_dependent_reorg (first)
2588 rtx first;
2589{
1245df60
R
2590 rtx insn, mova;
2591 int num_mova;
2592 rtx r0_rtx = gen_rtx (REG, Pmode, 0);
2593 rtx r0_inc_rtx = gen_rtx (POST_INC, Pmode, r0_rtx);
8aa2a305 2594
933c3ba3
JW
2595 /* If relaxing, generate pseudo-ops to associate function calls with
2596 the symbols they call. It does no harm to not generate these
2597 pseudo-ops. However, when we can generate them, it enables to
2598 linker to potentially relax the jsr to a bsr, and eliminate the
2599 register load and, possibly, the constant pool entry. */
2600
1245df60 2601 mdep_reorg_phase = SH_INSERT_USES_LABELS;
933c3ba3
JW
2602 if (TARGET_RELAX)
2603 {
2604 /* Remove all REG_LABEL notes. We want to use them for our own
2605 purposes. This works because none of the remaining passes
2606 need to look at them.
2607
2608 ??? But it may break in the future. We should use a machine
2609 dependent REG_NOTE, or some other approach entirely. */
2610 for (insn = first; insn; insn = NEXT_INSN (insn))
2611 {
2612 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2613 {
2614 rtx note;
2615
2616 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
2617 remove_note (insn, note);
2618 }
2619 }
2620
2621 for (insn = first; insn; insn = NEXT_INSN (insn))
2622 {
2623 rtx pattern, reg, link, set, scan, dies, label;
2624 int rescan = 0, foundinsn = 0;
2625
4787bce0
JW
2626 if (GET_CODE (insn) == CALL_INSN)
2627 {
2628 pattern = PATTERN (insn);
933c3ba3 2629
4787bce0
JW
2630 if (GET_CODE (pattern) == PARALLEL)
2631 pattern = XVECEXP (pattern, 0, 0);
2632 if (GET_CODE (pattern) == SET)
2633 pattern = SET_SRC (pattern);
933c3ba3 2634
4787bce0
JW
2635 if (GET_CODE (pattern) != CALL
2636 || GET_CODE (XEXP (pattern, 0)) != MEM)
2637 continue;
933c3ba3 2638
4787bce0
JW
2639 reg = XEXP (XEXP (pattern, 0), 0);
2640 }
2641 else
2642 {
2643 reg = sfunc_uses_reg (insn);
2644 if (! reg)
2645 continue;
2646 }
933c3ba3 2647
933c3ba3
JW
2648 if (GET_CODE (reg) != REG)
2649 continue;
2650
2651 /* This is a function call via REG. If the only uses of REG
2652 between the time that it is set and the time that it dies
2653 are in function calls, then we can associate all the
2654 function calls with the setting of REG. */
2655
2656 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
2657 {
1075deda
ILT
2658 if (REG_NOTE_KIND (link) != 0)
2659 continue;
933c3ba3
JW
2660 set = single_set (XEXP (link, 0));
2661 if (set && rtx_equal_p (reg, SET_DEST (set)))
2662 {
2663 link = XEXP (link, 0);
2664 break;
2665 }
2666 }
2667
2668 if (! link)
2669 {
2670 /* ??? Sometimes global register allocation will have
2671 deleted the insn pointed to by LOG_LINKS. Try
2672 scanning backward to find where the register is set. */
2673 for (scan = PREV_INSN (insn);
2674 scan && GET_CODE (scan) != CODE_LABEL;
2675 scan = PREV_INSN (scan))
2676 {
2677 if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2678 continue;
2679
2680 if (! reg_mentioned_p (reg, scan))
2681 continue;
2682
2683 if (noncall_uses_reg (reg, scan, &set))
2684 break;
2685
2686 if (set)
2687 {
2688 link = scan;
2689 break;
2690 }
2691 }
2692 }
2693
2694 if (! link)
2695 continue;
2696
2697 /* The register is set at LINK. */
2698
2699 /* We can only optimize the function call if the register is
2700 being set to a symbol. In theory, we could sometimes
2701 optimize calls to a constant location, but the assembler
2702 and linker do not support that at present. */
2703 if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
2704 && GET_CODE (SET_SRC (set)) != LABEL_REF)
2705 continue;
2706
2707 /* Scan forward from LINK to the place where REG dies, and
2708 make sure that the only insns which use REG are
2709 themselves function calls. */
2710
c1e8e6bd
JW
2711 /* ??? This doesn't work for call targets that were allocated
2712 by reload, since there may not be a REG_DEAD note for the
2713 register. */
2714
933c3ba3
JW
2715 dies = NULL_RTX;
2716 for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
2717 {
2718 rtx scanset;
2719
c1e8e6bd
JW
2720 /* Don't try to trace forward past a CODE_LABEL if we haven't
2721 seen INSN yet. Ordinarily, we will only find the setting insn
2722 in LOG_LINKS if it is in the same basic block. However,
2723 cross-jumping can insert code labels in between the load and
2724 the call, and can result in situations where a single call
2725 insn may have two targets depending on where we came from. */
2726
2727 if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
2728 break;
2729
933c3ba3
JW
2730 if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2731 continue;
2732
2733 /* Don't try to trace forward past a JUMP. To optimize
2734 safely, we would have to check that all the
c1e8e6bd 2735 instructions at the jump destination did not use REG. */
933c3ba3 2736
c1e8e6bd 2737 if (GET_CODE (scan) == JUMP_INSN)
933c3ba3
JW
2738 break;
2739
2740 if (! reg_mentioned_p (reg, scan))
2741 continue;
2742
2743 if (noncall_uses_reg (reg, scan, &scanset))
2744 break;
2745
2746 if (scan == insn)
2747 foundinsn = 1;
2748
4787bce0
JW
2749 if (scan != insn
2750 && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
933c3ba3
JW
2751 {
2752 /* There is a function call to this register other
2753 than the one we are checking. If we optimize
2754 this call, we need to rescan again below. */
2755 rescan = 1;
2756 }
2757
2758 /* ??? We shouldn't have to worry about SCANSET here.
2759 We should just be able to check for a REG_DEAD note
2760 on a function call. However, the REG_DEAD notes are
2761 apparently not dependable around libcalls; c-torture
2762 execute/920501-2 is a test case. If SCANSET is set,
2763 then this insn sets the register, so it must have
2764 died earlier. Unfortunately, this will only handle
2765 the cases in which the register is, in fact, set in a
2766 later insn. */
2767
2768 /* ??? We shouldn't have to use FOUNDINSN here.
2769 However, the LOG_LINKS fields are apparently not
2770 entirely reliable around libcalls;
2771 newlib/libm/math/e_pow.c is a test case. Sometimes
2772 an insn will appear in LOG_LINKS even though it is
2773 not the most recent insn which sets the register. */
2774
2775 if (foundinsn
2776 && (scanset
2777 || find_reg_note (scan, REG_DEAD, reg)))
2778 {
2779 dies = scan;
2780 break;
2781 }
2782 }
2783
2784 if (! dies)
2785 {
2786 /* Either there was a branch, or some insn used REG
2787 other than as a function call address. */
2788 continue;
2789 }
2790
2791 /* Create a code label, and put it in a REG_LABEL note on
2792 the insn which sets the register, and on each call insn
2793 which uses the register. In final_prescan_insn we look
2794 for the REG_LABEL notes, and output the appropriate label
2795 or pseudo-op. */
2796
2797 label = gen_label_rtx ();
2798 REG_NOTES (link) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2799 REG_NOTES (link));
2800 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2801 REG_NOTES (insn));
2802 if (rescan)
2803 {
2804 scan = link;
2805 do
2806 {
4787bce0
JW
2807 rtx reg2;
2808
933c3ba3
JW
2809 scan = NEXT_INSN (scan);
2810 if (scan != insn
4787bce0
JW
2811 && ((GET_CODE (scan) == CALL_INSN
2812 && reg_mentioned_p (reg, scan))
2813 || ((reg2 = sfunc_uses_reg (scan))
2814 && REGNO (reg2) == REGNO (reg))))
933c3ba3
JW
2815 REG_NOTES (scan) = gen_rtx (EXPR_LIST, REG_LABEL,
2816 label, REG_NOTES (scan));
2817 }
2818 while (scan != dies);
2819 }
2820 }
2821 }
2822
33f7f353
JR
2823 if (TARGET_SH2)
2824 fixup_addr_diff_vecs (first);
1245df60
R
2825
2826 if (optimize)
2827 {
1245df60
R
2828 mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
2829 shorten_branches (first);
2830 }
933c3ba3
JW
2831 /* Scan the function looking for move instructions which have to be
2832 changed to pc-relative loads and insert the literal tables. */
2833
1245df60
R
2834 mdep_reorg_phase = SH_FIXUP_PCLOAD;
2835 for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
0d7e008e 2836 {
1245df60
R
2837 if (mova_p (insn))
2838 {
2839 if (! num_mova++)
2840 mova = insn;
2841 }
2842 else if (GET_CODE (insn) == JUMP_INSN
2843 && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2844 && num_mova)
2845 {
2846 rtx scan;
2847 int total;
2848
2849 num_mova--;
2850
2851 /* Some code might have been inserted between the mova and
2852 its ADDR_DIFF_VEC. Check if the mova is still in range. */
2853 for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
33f7f353 2854 total += get_attr_length (scan);
1245df60
R
2855
2856 /* range of mova is 1020, add 4 because pc counts from address of
2857 second instruction after this one, subtract 2 in case pc is 2
2858 byte aligned. Possible alignment needed for the ADDR_DIFF_VEC
956d6950 2859 cancels out with alignment effects of the mova itself. */
1245df60
R
2860 if (total > 1022)
2861 {
2862 /* Change the mova into a load, and restart scanning
2863 there. broken_move will then return true for mova. */
2864 SET_SRC (PATTERN (mova))
2865 = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2866 INSN_CODE (mova) = -1;
2867 insn = mova;
2868 }
2869 }
0d7e008e
SC
2870 if (broken_move (insn))
2871 {
0d7e008e 2872 rtx scan;
8aa2a305
JW
2873 /* Scan ahead looking for a barrier to stick the constant table
2874 behind. */
1245df60
R
2875 rtx barrier = find_barrier (num_mova, mova, insn);
2876 rtx last_float_move, last_float = 0, *last_float_addr;
b9654711 2877
1245df60
R
2878 if (num_mova && ! mova_p (mova))
2879 {
2880 /* find_barrier had to change the first mova into a
2881 pcload; thus, we have to start with this new pcload. */
2882 insn = mova;
2883 num_mova = 0;
2884 }
16bea517 2885 /* Now find all the moves between the points and modify them. */
0d7e008e
SC
2886 for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
2887 {
1245df60
R
2888 if (GET_CODE (scan) == CODE_LABEL)
2889 last_float = 0;
0d7e008e
SC
2890 if (broken_move (scan))
2891 {
e577c183
JR
2892 rtx *patp = &PATTERN (scan), pat = *patp;
2893 rtx src, dst;
0d7e008e
SC
2894 rtx lab;
2895 rtx newinsn;
2896 rtx newsrc;
e577c183
JR
2897 enum machine_mode mode;
2898
2899 if (GET_CODE (pat) == PARALLEL)
2900 patp = &XVECEXP (pat, 0, 0), pat = *patp;
2901 src = SET_SRC (pat);
2902 dst = SET_DEST (pat);
2903 mode = GET_MODE (dst);
0d7e008e
SC
2904
2905 if (mode == SImode && hi_const (src))
2906 {
23ed92ca 2907 int offset = 0;
8aa2a305 2908
0d7e008e 2909 mode = HImode;
d3ae8277 2910 while (GET_CODE (dst) == SUBREG)
23ed92ca
JW
2911 {
2912 offset += SUBREG_WORD (dst);
2913 dst = SUBREG_REG (dst);
2914 }
2915 dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
0d7e008e 2916 }
8aa2a305 2917
1245df60
R
2918 if (GET_CODE (dst) == REG
2919 && ((REGNO (dst) >= FIRST_FP_REG
2920 && REGNO (dst) <= LAST_FP_REG)
2921 || REGNO (dst) == FPUL_REG))
2922 {
2923 if (last_float
2924 && reg_set_between_p (r0_rtx, last_float_move, scan))
2925 last_float = 0;
2926 lab = add_constant (src, mode, last_float);
2927 if (lab)
2928 emit_insn_before (gen_mova (lab), scan);
2929 else
2930 *last_float_addr = r0_inc_rtx;
2931 last_float_move = scan;
2932 last_float = src;
2933 newsrc = gen_rtx (MEM, mode,
2934 (REGNO (dst) == FPUL_REG
2935 ? r0_inc_rtx
2936 : r0_rtx));
2937 last_float_addr = &XEXP (newsrc, 0);
2938 }
2939 else
2940 {
2941 lab = add_constant (src, mode, 0);
2942 newsrc = gen_rtx (MEM, mode,
2943 gen_rtx (LABEL_REF, VOIDmode, lab));
2944 }
8aa2a305 2945 RTX_UNCHANGING_P (newsrc) = 1;
e577c183
JR
2946 *patp = gen_rtx (SET, VOIDmode, dst, newsrc);
2947 INSN_CODE (scan) = -1;
0d7e008e
SC
2948 }
2949 }
2950 dump_table (barrier);
1245df60 2951 insn = barrier;
0d7e008e
SC
2952 }
2953 }
1245df60
R
2954
2955 mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
2956 insn_addresses = 0;
2957 split_branches (first);
2958
2959 /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
2960 also has an effect on the register that holds the addres of the sfunc.
2961 Insert an extra dummy insn in front of each sfunc that pretends to
2962 use this register. */
2963 if (flag_delayed_branch)
2964 {
2965 for (insn = first; insn; insn = NEXT_INSN (insn))
2966 {
2967 rtx reg = sfunc_uses_reg (insn);
2968
2969 if (! reg)
2970 continue;
2971 emit_insn_before (gen_use_sfunc_addr (reg), insn);
2972 }
2973 }
2974 mdep_reorg_phase = SH_AFTER_MDEP_REORG;
2975}
2976
2977int
2978get_dest_uid (label, max_uid)
2979 rtx label;
2980 int max_uid;
2981{
2982 rtx dest = next_real_insn (label);
2983 int dest_uid;
2984 if (! dest)
2985 /* This can happen for an undefined label. */
2986 return 0;
2987 dest_uid = INSN_UID (dest);
2988 /* If this is a newly created branch redirection blocking instruction,
2989 we cannot index the branch_uid or insn_addresses arrays with its
2990 uid. But then, we won't need to, because the actual destination is
2991 the following branch. */
2992 while (dest_uid >= max_uid)
2993 {
2994 dest = NEXT_INSN (dest);
2995 dest_uid = INSN_UID (dest);
2996 }
2997 if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
2998 return 0;
2999 return dest_uid;
3000}
3001
3002/* Split condbranches that are out of range. Also add clobbers for
3003 scratch registers that are needed in far jumps.
3004 We do this before delay slot scheduling, so that it can take our
3005 newly created instructions into account. It also allows us to
3006 find branches with common targets more easily. */
3007
3008static void
3009split_branches (first)
3010 rtx first;
3011{
3012 rtx insn;
3013 struct far_branch **uid_branch, *far_branch_list = 0;
3014 int max_uid = get_max_uid ();
3015
3016 /* Find out which branches are out of range. */
1245df60
R
3017 shorten_branches (first);
3018
3019 uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
3020 bzero ((char *) uid_branch, max_uid * sizeof *uid_branch);
3021
3022 for (insn = first; insn; insn = NEXT_INSN (insn))
3023 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3024 continue;
3025 else if (INSN_DELETED_P (insn))
3026 {
3027 /* Shorten_branches would split this instruction again,
3028 so transform it into a note. */
3029 PUT_CODE (insn, NOTE);
3030 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3031 NOTE_SOURCE_FILE (insn) = 0;
3032 }
3033 else if (GET_CODE (insn) == JUMP_INSN
3034 /* Don't mess with ADDR_DIFF_VEC */
3035 && (GET_CODE (PATTERN (insn)) == SET
3036 || GET_CODE (PATTERN (insn)) == RETURN))
3037 {
3038 enum attr_type type = get_attr_type (insn);
3039 if (type == TYPE_CBRANCH)
3040 {
3041 rtx next, beyond;
3042
3043 if (get_attr_length (insn) > 4)
3044 {
3045 rtx src = SET_SRC (PATTERN (insn));
3046 rtx cond = XEXP (src, 0);
3047 rtx olabel = XEXP (XEXP (src, 1), 0);
3048 rtx jump;
3049 int addr = insn_addresses[INSN_UID (insn)];
3050 rtx label = 0;
3051 int dest_uid = get_dest_uid (olabel, max_uid);
3052 struct far_branch *bp = uid_branch[dest_uid];
3053
3054 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
25938114 3055 the label if the LABEL_NUSES count drops to zero. There is
1245df60
R
3056 always a jump_optimize pass that sets these values, but it
3057 proceeds to delete unreferenced code, and then if not
956d6950 3058 optimizing, to un-delete the deleted instructions, thus
1245df60
R
3059 leaving labels with too low uses counts. */
3060 if (! optimize)
3061 {
3062 JUMP_LABEL (insn) = olabel;
3063 LABEL_NUSES (olabel)++;
3064 }
3065 if (! bp)
3066 {
3067 bp = (struct far_branch *) alloca (sizeof *bp);
3068 uid_branch[dest_uid] = bp;
3069 bp->prev = far_branch_list;
3070 far_branch_list = bp;
3071 bp->far_label
3072 = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
3073 LABEL_NUSES (bp->far_label)++;
3074 }
3075 else
3076 {
3077 label = bp->near_label;
3078 if (! label && bp->address - addr >= CONDJUMP_MIN)
3079 {
3080 rtx block = bp->insert_place;
3081
3082 if (GET_CODE (PATTERN (block)) == RETURN)
3083 block = PREV_INSN (block);
3084 else
3085 block = gen_block_redirect (block,
3086 bp->address, 2);
3087 label = emit_label_after (gen_label_rtx (),
3088 PREV_INSN (block));
3089 bp->near_label = label;
3090 }
3091 else if (label && ! NEXT_INSN (label))
3092 if (addr + 2 - bp->address <= CONDJUMP_MAX)
3093 bp->insert_place = insn;
3094 else
3095 gen_far_branch (bp);
3096 }
3097 if (! label
3098 || NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN)
3099 {
3100 bp->near_label = label = gen_label_rtx ();
3101 bp->insert_place = insn;
3102 bp->address = addr;
3103 }
3104 if (! redirect_jump (insn, label))
3105 abort ();
3106 }
3107 else
3108 {
3109 /* get_attr_length (insn) == 2 */
3110 /* Check if we have a pattern where reorg wants to redirect
3111 the branch to a label from an unconditional branch that
3112 is too far away. */
3113 /* We can't use JUMP_LABEL here because it might be undefined
3114 when not optimizing. */
33f7f353 3115 /* A syntax error might cause beyond to be NULL_RTX. */
1245df60
R
3116 beyond
3117 = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
3118 0));
3119
33f7f353
JR
3120 if (beyond
3121 && (GET_CODE (beyond) == JUMP_INSN
3122 || (GET_CODE (beyond = next_active_insn (beyond))
3123 == JUMP_INSN))
1245df60
R
3124 && GET_CODE (PATTERN (beyond)) == SET
3125 && recog_memoized (beyond) == CODE_FOR_jump
3126 && ((insn_addresses[INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0))]
3127 - insn_addresses[INSN_UID (insn)] + 252U)
3128 > 252 + 258 + 2))
3129 gen_block_redirect (beyond,
3130 insn_addresses[INSN_UID (beyond)], 1);
3131 }
3132
3133 next = next_active_insn (insn);
3134
3135 if ((GET_CODE (next) == JUMP_INSN
3136 || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
3137 && GET_CODE (PATTERN (next)) == SET
3138 && recog_memoized (next) == CODE_FOR_jump
3139 && ((insn_addresses[INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0))]
3140 - insn_addresses[INSN_UID (insn)] + 252U)
3141 > 252 + 258 + 2))
3142 gen_block_redirect (next, insn_addresses[INSN_UID (next)], 1);
3143 }
3144 else if (type == TYPE_JUMP || type == TYPE_RETURN)
3145 {
3146 int addr = insn_addresses[INSN_UID (insn)];
3147 rtx far_label = 0;
3148 int dest_uid = 0;
3149 struct far_branch *bp;
3150
3151 if (type == TYPE_JUMP)
3152 {
3153 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
3154 dest_uid = get_dest_uid (far_label, max_uid);
3155 if (! dest_uid)
3156 {
3157 /* Parse errors can lead to labels outside
3158 the insn stream. */
3159 if (! NEXT_INSN (far_label))
3160 continue;
3161
3162 if (! optimize)
3163 {
3164 JUMP_LABEL (insn) = far_label;
3165 LABEL_NUSES (far_label)++;
3166 }
3167 redirect_jump (insn, NULL_RTX);
3168 far_label = 0;
3169 }
3170 }
3171 bp = uid_branch[dest_uid];
3172 if (! bp)
3173 {
3174 bp = (struct far_branch *) alloca (sizeof *bp);
3175 uid_branch[dest_uid] = bp;
3176 bp->prev = far_branch_list;
3177 far_branch_list = bp;
3178 bp->near_label = 0;
3179 bp->far_label = far_label;
3180 if (far_label)
3181 LABEL_NUSES (far_label)++;
3182 }
3183 else if (bp->near_label && ! NEXT_INSN (bp->near_label))
3184 if (addr - bp->address <= CONDJUMP_MAX)
3185 emit_label_after (bp->near_label, PREV_INSN (insn));
3186 else
3187 {
3188 gen_far_branch (bp);
3189 bp->near_label = 0;
3190 }
3191 else
3192 bp->near_label = 0;
3193 bp->address = addr;
3194 bp->insert_place = insn;
3195 if (! far_label)
3196 emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
3197 else
3198 gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
3199 }
3200 }
3201 /* Generate all pending far branches,
3202 and free our references to the far labels. */
3203 while (far_branch_list)
3204 {
3205 if (far_branch_list->near_label
3206 && ! NEXT_INSN (far_branch_list->near_label))
3207 gen_far_branch (far_branch_list);
3208 if (optimize
3209 && far_branch_list->far_label
3210 && ! --LABEL_NUSES (far_branch_list->far_label))
3211 delete_insn (far_branch_list->far_label);
3212 far_branch_list = far_branch_list->prev;
3213 }
a0798779
R
3214
3215 /* Instruction length information is no longer valid due to the new
3216 instructions that have been generated. */
3217 init_insn_lengths ();
b9654711
SC
3218}
3219
8aa2a305 3220/* Dump out instruction addresses, which is useful for debugging the
933c3ba3
JW
3221 constant pool table stuff.
3222
3223 If relaxing, output the label and pseudo-ops used to link together
3224 calls and the instruction which set the registers. */
8aa2a305
JW
3225
3226/* ??? This is unnecessary, and probably should be deleted. This makes
3227 the insn_addresses declaration above unnecessary. */
3228
3229/* ??? The addresses printed by this routine for insns are nonsense for
3230 insns which are inside of a sequence where none of the inner insns have
3231 variable length. This is because the second pass of shorten_branches
3232 does not bother to update them. */
0d7e008e 3233
8e87e161 3234void
8aa2a305
JW
3235final_prescan_insn (insn, opvec, noperands)
3236 rtx insn;
3237 rtx *opvec;
3238 int noperands;
b9654711 3239{
8aa2a305
JW
3240 if (TARGET_DUMPISIZE)
3241 fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
933c3ba3
JW
3242
3243 if (TARGET_RELAX)
3244 {
3245 rtx note;
3246
3247 note = find_reg_note (insn, REG_LABEL, NULL_RTX);
3248 if (note)
3249 {
3250 rtx pattern;
3251
3252 pattern = PATTERN (insn);
3253 if (GET_CODE (pattern) == PARALLEL)
3254 pattern = XVECEXP (pattern, 0, 0);
3255 if (GET_CODE (pattern) == CALL
3256 || (GET_CODE (pattern) == SET
4787bce0
JW
3257 && (GET_CODE (SET_SRC (pattern)) == CALL
3258 || get_attr_type (insn) == TYPE_SFUNC)))
4d7b7169
R
3259 asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
3260 CODE_LABEL_NUMBER (XEXP (note, 0)));
933c3ba3
JW
3261 else if (GET_CODE (pattern) == SET)
3262 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
3263 CODE_LABEL_NUMBER (XEXP (note, 0)));
3264 else
3265 abort ();
3266 }
3267 }
0d7e008e 3268}
b9654711 3269
8aa2a305 3270/* Dump out any constants accumulated in the final pass. These will
38e01259 3271 only be labels. */
b9654711 3272
8aa2a305
JW
3273char *
3274output_jump_label_table ()
0d7e008e 3275{
8aa2a305
JW
3276 int i;
3277
3278 if (pool_size)
3279 {
3280 fprintf (asm_out_file, "\t.align 2\n");
3281 for (i = 0; i < pool_size; i++)
3282 {
3283 pool_node *p = &pool_vector[i];
3284
3285 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
3286 CODE_LABEL_NUMBER (p->label));
3287 output_asm_insn (".long %O0", &p->value);
3288 }
3289 pool_size = 0;
3290 }
b9654711 3291
8aa2a305
JW
3292 return "";
3293}
3294\f
3295/* A full frame looks like:
16bea517
JW
3296
3297 arg-5
3298 arg-4
3299 [ if current_function_anonymous_args
3300 arg-3
3301 arg-2
3302 arg-1
3303 arg-0 ]
3304 saved-fp
3305 saved-r10
3306 saved-r11
3307 saved-r12
3308 saved-pr
3309 local-n
3310 ..
3311 local-1
3312 local-0 <- fp points here. */
8e87e161 3313
8aa2a305
JW
3314/* Number of bytes pushed for anonymous args, used to pass information
3315 between expand_prologue and expand_epilogue. */
3316
3317static int extra_push;
3318
885c9a39
JR
3319/* Adjust the stack by SIZE bytes. REG holds the rtl of the register
3320 to be adjusted, and TEMP, if nonnegative, holds the register number
3321 of a general register that we may clobber. */
8aa2a305
JW
3322
3323static void
885c9a39 3324output_stack_adjust (size, reg, temp)
8aa2a305 3325 int size;
46d81ffa 3326 rtx reg;
885c9a39 3327 int temp;
8aa2a305
JW
3328{
3329 if (size)
3330 {
f3cd5375
JW
3331 if (CONST_OK_FOR_I (size))
3332 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
3333 /* Try to do it with two partial adjustments; however, we must make
3334 sure that the stack is properly aligned at all times, in case
3335 an interrupt occurs between the two partial adjustments. */
3336 else if (CONST_OK_FOR_I (size / 2 & -4)
3337 && CONST_OK_FOR_I (size - (size / 2 & -4)))
3338 {
3339 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size / 2 & -4)));
3340 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size - (size / 2 & -4))));
3341 }
3342 else
8aa2a305 3343 {
f3cd5375
JW
3344 rtx const_reg;
3345
3346 /* If TEMP is invalid, we could temporarily save a general
3347 register to MACL. However, there is currently no need
3348 to handle this case, so just abort when we see it. */
3349 if (temp < 0)
3350 abort ();
3351 const_reg = gen_rtx (REG, SImode, temp);
3352
3353 /* If SIZE is negative, subtract the positive value.
3354 This sometimes allows a constant pool entry to be shared
3355 between prologue and epilogue code. */
3356 if (size < 0)
885c9a39 3357 {
f3cd5375
JW
3358 emit_insn (gen_movsi (const_reg, GEN_INT (-size)));
3359 emit_insn (gen_subsi3 (reg, reg, const_reg));
885c9a39
JR
3360 }
3361 else
3362 {
f3cd5375
JW
3363 emit_insn (gen_movsi (const_reg, GEN_INT (size)));
3364 emit_insn (gen_addsi3 (reg, reg, const_reg));
885c9a39 3365 }
8aa2a305 3366 }
8aa2a305
JW
3367 }
3368}
3369
3370/* Output RTL to push register RN onto the stack. */
3371
3372static void
3373push (rn)
3374 int rn;
3375{
3376 rtx x;
1a95a963
JW
3377 if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
3378 || rn == FPUL_REG)
1245df60 3379 x = gen_push_e (gen_rtx (REG, SFmode, rn));
1a95a963 3380 else
1245df60 3381 x = gen_push (gen_rtx (REG, SImode, rn));
1a95a963 3382
1245df60 3383 x = emit_insn (x);
8aa2a305
JW
3384 REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
3385 gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
3386}
3387
3388/* Output RTL to pop register RN from the stack. */
3389
3390static void
3391pop (rn)
3392 int rn;
3393{
3394 rtx x;
1a95a963
JW
3395 if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
3396 || rn == FPUL_REG)
1245df60 3397 x = gen_pop_e (gen_rtx (REG, SFmode, rn));
1a95a963 3398 else
1245df60 3399 x = gen_pop (gen_rtx (REG, SImode, rn));
1a95a963 3400
1245df60 3401 x = emit_insn (x);
8aa2a305
JW
3402 REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
3403 gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
3404}
3405
1245df60 3406/* Generate code to push the regs specified in the mask. */
8aa2a305
JW
3407
3408static void
1a95a963
JW
3409push_regs (mask, mask2)
3410 int mask, mask2;
8aa2a305
JW
3411{
3412 int i;
3413
1245df60
R
3414 /* Push PR last; this gives better latencies after the prologue, and
3415 candidates for the return delay slot when there are no general
3416 registers pushed. */
1a95a963 3417 for (i = 0; i < 32; i++)
1245df60 3418 if (mask & (1 << i) && i != PR_REG)
8aa2a305 3419 push (i);
1a95a963
JW
3420 for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
3421 if (mask2 & (1 << (i - 32)))
3422 push (i);
1245df60
R
3423 if (mask & (1 << PR_REG))
3424 push (PR_REG);
8aa2a305
JW
3425}
3426
3427/* Work out the registers which need to be saved, both as a mask and a
1245df60 3428 count of saved words.
8aa2a305
JW
3429
3430 If doing a pragma interrupt function, then push all regs used by the
3431 function, and if we call another function (we can tell by looking at PR),
3432 make sure that all the regs it clobbers are safe too. */
3433
3434static int
1a95a963 3435calc_live_regs (count_ptr, live_regs_mask2)
8aa2a305 3436 int *count_ptr;
1a95a963 3437 int *live_regs_mask2;
8aa2a305
JW
3438{
3439 int reg;
3440 int live_regs_mask = 0;
1245df60 3441 int count;
8aa2a305 3442
1a95a963 3443 *live_regs_mask2 = 0;
1245df60 3444 for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
8aa2a305 3445 {
1245df60
R
3446 if ((pragma_interrupt && ! pragma_trapa)
3447 ? (/* Need to save all the regs ever live. */
3448 (regs_ever_live[reg]
3449 || (call_used_regs[reg]
3450 && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
3451 && regs_ever_live[PR_REG]))
3452 && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
3453 && reg != RETURN_ADDRESS_POINTER_REGNUM
3454 && reg != T_REG && reg != GBR_REG)
3455 : (/* Only push those regs which are used and need to be saved. */
3456 regs_ever_live[reg] && ! call_used_regs[reg]))
8aa2a305 3457 {
1245df60
R
3458 if (reg >= 32)
3459 *live_regs_mask2 |= 1 << (reg - 32);
3460 else
3461 live_regs_mask |= 1 << reg;
3462 count++;
8aa2a305
JW
3463 }
3464 }
3465
3466 *count_ptr = count;
3467 return live_regs_mask;
3468}
3469
3470/* Code to generate prologue and epilogue sequences */
b9654711
SC
3471
3472void
3473sh_expand_prologue ()
3474{
3475 int live_regs_mask;
40d2032b 3476 int d, i;
1a95a963 3477 int live_regs_mask2;
3d5a0820 3478 int double_align = 0;
b9654711 3479
0d7e008e 3480 /* We have pretend args if we had an object sent partially in registers
8aa2a305 3481 and partially on the stack, e.g. a large structure. */
885c9a39
JR
3482 output_stack_adjust (-current_function_pretend_args_size,
3483 stack_pointer_rtx, 3);
b9654711 3484
40d2032b
JW
3485 extra_push = 0;
3486
3487 /* This is set by SETUP_VARARGS to indicate that this is a varargs
1a95a963 3488 routine. Clear it here so that the next function isn't affected. */
b9654711
SC
3489 if (current_function_anonymous_args)
3490 {
40d2032b
JW
3491 current_function_anonymous_args = 0;
3492
1a95a963
JW
3493 /* This is not used by the SH3E calling convention */
3494 if (!TARGET_SH3E)
1245df60 3495 {
1a95a963
JW
3496 /* Push arg regs as if they'd been provided by caller in stack. */
3497 for (i = 0; i < NPARM_REGS(SImode); i++)
3498 {
3499 int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
aec373f1
R
3500 if (i >= (NPARM_REGS(SImode)
3501 - current_function_args_info.arg_count[(int) SH_ARG_INT]
3502 ))
1a95a963
JW
3503 break;
3504 push (rn);
3505 extra_push += 4;
3506 }
1245df60 3507 }
b9654711 3508 }
1a95a963 3509
4408efce
JL
3510 /* If we're supposed to switch stacks at function entry, do so now. */
3511 if (sp_switch)
3512 emit_insn (gen_sp_switch_1 ());
3513
1245df60 3514 live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
1a95a963
JW
3515 push_regs (live_regs_mask, live_regs_mask2);
3516
3d5a0820
R
3517 if (TARGET_ALIGN_DOUBLE && d & 1)
3518 double_align = 4;
3519
3520 output_stack_adjust (-get_frame_size () - double_align,
3521 stack_pointer_rtx, 3);
b9654711
SC
3522
3523 if (frame_pointer_needed)
8aa2a305 3524 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
b9654711
SC
3525}
3526
3527void
3528sh_expand_epilogue ()
3529{
3530 int live_regs_mask;
40d2032b 3531 int d, i;
b9654711 3532
1a95a963 3533 int live_regs_mask2;
3d5a0820
R
3534 int frame_size = get_frame_size ();
3535
3536 live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
3537
3538 if (TARGET_ALIGN_DOUBLE && d & 1)
3539 frame_size += 4;
16bea517 3540
b9654711 3541 if (frame_pointer_needed)
46d81ffa 3542 {
3d5a0820 3543 output_stack_adjust (frame_size, frame_pointer_rtx, 7);
07f5b9aa
JL
3544
3545 /* We must avoid moving the stack pointer adjustment past code
3546 which reads from the local frame, else an interrupt could
3547 occur after the SP adjustment and clobber data in the local
3548 frame. */
3549 emit_insn (gen_blockage ());
46d81ffa
RK
3550 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
3551 }
3d5a0820 3552 else if (frame_size)
07f5b9aa
JL
3553 {
3554 /* We must avoid moving the stack pointer adjustment past code
3555 which reads from the local frame, else an interrupt could
3556 occur after the SP adjustment and clobber data in the local
3557 frame. */
3558 emit_insn (gen_blockage ());
3d5a0820 3559 output_stack_adjust (frame_size, stack_pointer_rtx, 7);
07f5b9aa 3560 }
b9654711 3561
16bea517 3562 /* Pop all the registers. */
0d7e008e 3563
1245df60
R
3564 live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
3565 if (live_regs_mask & (1 << PR_REG))
3566 pop (PR_REG);
b9654711
SC
3567 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3568 {
3569 int j = (FIRST_PSEUDO_REGISTER - 1) - i;
1245df60 3570 if (j < 32 && (live_regs_mask & (1 << j)) && j != PR_REG)
1a95a963
JW
3571 pop (j);
3572 else if (j >= 32 && (live_regs_mask2 & (1 << (j - 32))))
8aa2a305 3573 pop (j);
b9654711 3574 }
b9654711 3575
46d81ffa 3576 output_stack_adjust (extra_push + current_function_pretend_args_size,
885c9a39 3577 stack_pointer_rtx, 7);
4408efce
JL
3578
3579 /* Switch back to the normal stack if necessary. */
3580 if (sp_switch)
3581 emit_insn (gen_sp_switch_2 ());
b9654711
SC
3582}
3583
8aa2a305
JW
3584/* Clear variables at function end. */
3585
3586void
3587function_epilogue (stream, size)
3588 FILE *stream;
3589 int size;
3590{
4408efce
JL
3591 trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
3592 sp_switch = NULL_RTX;
8aa2a305
JW
3593}
3594
1a95a963
JW
3595rtx
3596sh_builtin_saveregs (arglist)
3597 tree arglist;
3598{
3599 tree fntype = TREE_TYPE (current_function_decl);
3600 /* First unnamed integer register. */
3601 int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
3602 /* Number of integer registers we need to save. */
3603 int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
3604 /* First unnamed SFmode float reg */
3605 int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
3606 /* Number of SFmode float regs to save. */
3607 int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
3608 int ptrsize = GET_MODE_SIZE (Pmode);
3609 rtx valist, regbuf, fpregs;
3610 int bufsize, regno;
3611
3612 /* Allocate block of memory for the regs. */
3613 /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
3614 Or can assign_stack_local accept a 0 SIZE argument? */
3615 bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
3616
3617 regbuf = assign_stack_local (BLKmode, bufsize, 0);
3618 MEM_IN_STRUCT_P (regbuf) = 1;
3619
3620 /* Save int args.
3621 This is optimized to only save the regs that are necessary. Explicitly
3622 named args need not be saved. */
3623 if (n_intregs > 0)
3624 move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
3625 gen_rtx (MEM, BLKmode,
3626 plus_constant (XEXP (regbuf, 0),
3627 n_floatregs * UNITS_PER_WORD)),
3628 n_intregs, n_intregs * UNITS_PER_WORD);
3629
3630 /* Save float args.
3631 This is optimized to only save the regs that are necessary. Explicitly
3632 named args need not be saved.
3633 We explicitly build a pointer to the buffer because it halves the insn
3634 count when not optimizing (otherwise the pointer is built for each reg
1245df60
R
3635 saved).
3636 We emit the moves in reverse order so that we can use predecrement. */
1a95a963
JW
3637
3638 fpregs = gen_reg_rtx (Pmode);
3639 emit_move_insn (fpregs, XEXP (regbuf, 0));
1245df60
R
3640 emit_insn (gen_addsi3 (fpregs, fpregs,
3641 GEN_INT (n_floatregs * UNITS_PER_WORD)));
3642 for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
3643 {
3644 emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
3645 emit_move_insn (gen_rtx (MEM, SFmode, fpregs),
3646 gen_rtx (REG, SFmode, BASE_ARG_REG (SFmode) + regno));
3647 }
1a95a963
JW
3648
3649 /* Return the address of the regbuf. */
3650 return XEXP (regbuf, 0);
3651}
3652
0d7e008e
SC
3653/* Define the offset between two registers, one to be eliminated, and
3654 the other its replacement, at the start of a routine. */
3655
3656int
3657initial_elimination_offset (from, to)
8e87e161
SC
3658 int from;
3659 int to;
0d7e008e
SC
3660{
3661 int regs_saved;
0d7e008e
SC
3662 int total_saved_regs_space;
3663 int total_auto_space = get_frame_size ();
1245df60 3664 int save_flags = target_flags;
8e87e161 3665
2afeea0f
JW
3666 int live_regs_mask, live_regs_mask2;
3667 live_regs_mask = calc_live_regs (&regs_saved, &live_regs_mask2);
1245df60 3668 target_flags = save_flags;
1a95a963 3669
0d7e008e 3670 total_saved_regs_space = (regs_saved) * 4;
b9654711 3671
0d7e008e 3672 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
8aa2a305
JW
3673 return total_saved_regs_space + total_auto_space;
3674
0d7e008e 3675 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
8aa2a305
JW
3676 return total_saved_regs_space + total_auto_space;
3677
3678 /* Initial gap between fp and sp is 0. */
0d7e008e 3679 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
8aa2a305
JW
3680 return 0;
3681
2afeea0f
JW
3682 if (from == RETURN_ADDRESS_POINTER_REGNUM
3683 && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
3684 {
1245df60
R
3685 int i, n = total_saved_regs_space;
3686 for (i = PR_REG-1; i >= 0; i--)
2afeea0f 3687 if (live_regs_mask & (1 << i))
1245df60 3688 n -= 4;
2afeea0f
JW
3689 return n + total_auto_space;
3690 }
3691
0d7e008e
SC
3692 abort ();
3693}
8aa2a305 3694\f
0d7e008e 3695/* Handle machine specific pragmas to be semi-compatible with Hitachi
16bea517 3696 compiler. */
b9654711
SC
3697
3698int
119d0c36 3699handle_pragma (file, t)
0d7e008e 3700 FILE *file;
119d0c36 3701 tree t;
b9654711 3702{
119d0c36
JW
3703 int retval = 0;
3704 register char *pname;
b9654711 3705
119d0c36
JW
3706 if (TREE_CODE (t) != IDENTIFIER_NODE)
3707 return 0;
05a81fe5 3708
119d0c36
JW
3709 pname = IDENTIFIER_POINTER (t);
3710 if (strcmp (pname, "interrupt") == 0)
3711 pragma_interrupt = retval = 1;
3712 else if (strcmp (pname, "trapa") == 0)
3713 pragma_interrupt = pragma_trapa = retval = 1;
3714 else if (strcmp (pname, "nosave_low_regs") == 0)
3715 pragma_nosave_low_regs = retval = 1;
05a81fe5 3716
119d0c36 3717 return retval;
0d7e008e 3718}
4408efce
JL
3719/* Return nonzero if ATTR is a valid attribute for DECL.
3720 ATTRIBUTES are any existing attributes and ARGS are the arguments
3721 supplied with ATTR.
3722
3723 Supported attributes:
3724
3725 interrupt_handler -- specifies this function is an interrupt handler.
3726
3727 sp_switch -- specifies an alternate stack for an interrupt handler
3728 to run on.
3729
956d6950 3730 trap_exit -- use a trapa to exit an interrupt function instead of
4408efce
JL
3731 an rte instruction. */
3732
3733int
3734sh_valid_machine_decl_attribute (decl, attributes, attr, args)
3735 tree decl;
3736 tree attributes;
3737 tree attr;
3738 tree args;
3739{
3740 int retval = 0;
3741
3742 if (TREE_CODE (decl) != FUNCTION_DECL)
3743 return 0;
3744
3745 if (is_attribute_p ("interrupt_handler", attr))
3746 {
3747 pragma_interrupt = 1;
3748 return 1;
3749 }
3750
3751 if (is_attribute_p ("sp_switch", attr))
3752 {
3753 /* The sp_switch attribute only has meaning for interrupt functions. */
3754 if (!pragma_interrupt)
3755 return 0;
3756
3757 /* sp_switch must have an argument. */
3758 if (!args || TREE_CODE (args) != TREE_LIST)
3759 return 0;
3760
3761 /* The argument must be a constant string. */
3762 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
3763 return 0;
3764
3765 sp_switch = gen_rtx (SYMBOL_REF, VOIDmode,
3766 TREE_STRING_POINTER (TREE_VALUE (args)));
3767 return 1;
3768 }
3769
3770 if (is_attribute_p ("trap_exit", attr))
3771 {
3772 /* The trap_exit attribute only has meaning for interrupt functions. */
3773 if (!pragma_interrupt)
3774 return 0;
3775
3776 /* trap_exit must have an argument. */
3777 if (!args || TREE_CODE (args) != TREE_LIST)
3778 return 0;
3779
3780 /* The argument must be a constant integer. */
3781 if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
3782 return 0;
3783
3784 trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
3785 return 1;
3786 }
3787}
3788
0d7e008e 3789\f
8aa2a305 3790/* Predicates used by the templates. */
0d7e008e 3791
8aa2a305
JW
3792/* Returns 1 if OP is MACL, MACH or PR. The input must be a REG rtx.
3793 Used only in general_movsrc_operand. */
0d7e008e 3794
8aa2a305
JW
3795int
3796system_reg_operand (op, mode)
3797 rtx op;
3798 enum machine_mode mode;
0d7e008e 3799{
8aa2a305 3800 switch (REGNO (op))
0d7e008e 3801 {
8aa2a305
JW
3802 case PR_REG:
3803 case MACL_REG:
3804 case MACH_REG:
3805 return 1;
0d7e008e 3806 }
8aa2a305 3807 return 0;
0d7e008e 3808}
0d7e008e
SC
3809
3810/* Returns 1 if OP can be source of a simple move operation.
3811 Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
16bea517 3812 invalid as are subregs of system registers. */
0d7e008e
SC
3813
3814int
3815general_movsrc_operand (op, mode)
3816 rtx op;
3817 enum machine_mode mode;
3818{
06c386ea
SC
3819 if (GET_CODE (op) == MEM)
3820 {
3821 rtx inside = XEXP (op, 0);
3822 if (GET_CODE (inside) == CONST)
3823 inside = XEXP (inside, 0);
0d7e008e 3824
06c386ea
SC
3825 if (GET_CODE (inside) == LABEL_REF)
3826 return 1;
8e87e161 3827
06c386ea 3828 if (GET_CODE (inside) == PLUS
8aa2a305
JW
3829 && GET_CODE (XEXP (inside, 0)) == LABEL_REF
3830 && GET_CODE (XEXP (inside, 1)) == CONST_INT)
06c386ea 3831 return 1;
16bea517
JW
3832
3833 /* Only post inc allowed. */
97f8690b 3834 if (GET_CODE (inside) == PRE_DEC)
06c386ea 3835 return 0;
06c386ea 3836 }
0d7e008e
SC
3837
3838 if ((mode == QImode || mode == HImode)
3839 && (GET_CODE (op) == SUBREG
3840 && GET_CODE (XEXP (op, 0)) == REG
3841 && system_reg_operand (XEXP (op, 0), mode)))
3842 return 0;
3843
0d7e008e 3844 return general_operand (op, mode);
b9654711
SC
3845}
3846
0d7e008e
SC
3847/* Returns 1 if OP can be a destination of a move.
3848 Same as general_operand, but no preinc allowed. */
3849
b9654711 3850int
0d7e008e
SC
3851general_movdst_operand (op, mode)
3852 rtx op;
3853 enum machine_mode mode;
b9654711 3854{
16bea517 3855 /* Only pre dec allowed. */
97f8690b 3856 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
0d7e008e 3857 return 0;
d3ae8277 3858
0d7e008e
SC
3859 return general_operand (op, mode);
3860}
3861
0d7e008e
SC
3862/* Returns 1 if OP is a normal arithmetic register. */
3863
3864int
3865arith_reg_operand (op, mode)
3866 rtx op;
3867 enum machine_mode mode;
3868{
3869 if (register_operand (op, mode))
3870 {
519164a9
JW
3871 int regno;
3872
0d7e008e 3873 if (GET_CODE (op) == REG)
519164a9
JW
3874 regno = REGNO (op);
3875 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
3876 regno = REGNO (SUBREG_REG (op));
3877 else
3878 return 1;
3879
3880 return (regno != T_REG && regno != PR_REG && regno != FPUL_REG
3881 && regno != MACH_REG && regno != MACL_REG);
0d7e008e
SC
3882 }
3883 return 0;
3884}
3885
0d7e008e
SC
3886/* Returns 1 if OP is a valid source operand for an arithmetic insn. */
3887
3888int
3889arith_operand (op, mode)
3890 rtx op;
3891 enum machine_mode mode;
3892{
3893 if (arith_reg_operand (op, mode))
3894 return 1;
3895
8aa2a305
JW
3896 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
3897 return 1;
3898
0d7e008e
SC
3899 return 0;
3900}
3901
22e1ebf1
JW
3902/* Returns 1 if OP is a valid source operand for a compare insn. */
3903
3904int
3905arith_reg_or_0_operand (op, mode)
3906 rtx op;
3907 enum machine_mode mode;
3908{
3909 if (arith_reg_operand (op, mode))
3910 return 1;
3911
8aa2a305
JW
3912 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
3913 return 1;
3914
22e1ebf1
JW
3915 return 0;
3916}
3917
16bea517 3918/* Returns 1 if OP is a valid source operand for a logical operation. */
0d7e008e
SC
3919
3920int
3921logical_operand (op, mode)
3922 rtx op;
3923 enum machine_mode mode;
3924{
3925 if (arith_reg_operand (op, mode))
3926 return 1;
3927
8aa2a305
JW
3928 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
3929 return 1;
3930
0d7e008e 3931 return 0;
b9654711 3932}
d3ae8277 3933
1a95a963
JW
3934/* Nonzero if OP is a floating point value with value 0.0. */
3935
3936int
3937fp_zero_operand (op)
3938 rtx op;
d3ae8277 3939{
1a95a963 3940 REAL_VALUE_TYPE r;
d3ae8277 3941
1a95a963
JW
3942 if (GET_MODE (op) != SFmode)
3943 return 0;
3944
3945 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
e4fa6b06 3946 return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
d3ae8277
SC
3947}
3948
1a95a963 3949/* Nonzero if OP is a floating point value with value 1.0. */
d3ae8277
SC
3950
3951int
1a95a963
JW
3952fp_one_operand (op)
3953 rtx op;
d3ae8277 3954{
1a95a963
JW
3955 REAL_VALUE_TYPE r;
3956
3957 if (GET_MODE (op) != SFmode)
3958 return 0;
3959
3960 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3961 return REAL_VALUES_EQUAL (r, dconst1);
d3ae8277 3962}
1245df60
R
3963
3964int
3965braf_label_ref_operand(op, mode)
3966 rtx op;
3967 enum machine_mode mode;
3968{
3969 rtx prev;
3970
3971 if (GET_CODE (op) != LABEL_REF)
3972 return 0;
3973 prev = prev_real_insn (XEXP (op, 0));
3974 if (GET_CODE (prev) != JUMP_INSN)
3975 return 0;
3976 prev = PATTERN (prev);
3977 if (GET_CODE (prev) != PARALLEL || XVECLEN (prev, 0) != 2)
3978 return 0;
3979 prev = XVECEXP (prev, 0, 0);
3980 if (GET_CODE (prev) != SET)
3981 return 0;
3982 prev = SET_SRC (prev);
3983 if (GET_CODE (prev) != PLUS || XEXP (prev, 1) != op)
3984 return 0;
3985}
3986\f
33f7f353 3987/* Return the destination address of a branch. */
1245df60
R
3988
3989int
33f7f353 3990branch_dest (branch)
1245df60
R
3991 rtx branch;
3992{
33f7f353
JR
3993 rtx dest = SET_SRC (PATTERN (branch));
3994 int dest_uid;
1245df60
R
3995
3996 if (GET_CODE (dest) == IF_THEN_ELSE)
3997 dest = XEXP (dest, 1);
3998 dest = XEXP (dest, 0);
3999 dest_uid = INSN_UID (dest);
33f7f353 4000 return insn_addresses[dest_uid];
1245df60 4001}
a55e9d2b
RK
4002\f
4003/* Return non-zero if REG is not used after INSN.
4004 We assume REG is a reload reg, and therefore does
8b760293 4005 not live past labels. It may live past calls or jumps though. */
a55e9d2b
RK
4006int
4007reg_unused_after (reg, insn)
4008 rtx reg;
4009 rtx insn;
4010{
8783b15e 4011 enum rtx_code code;
a55e9d2b
RK
4012 rtx set;
4013
4014 /* If the reg is set by this instruction, then it is safe for our
4015 case. Disregard the case where this is a store to memory, since
4016 we are checking a register used in the store address. */
4017 set = single_set (insn);
4018 if (set && GET_CODE (SET_DEST (set)) != MEM
4019 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4020 return 1;
4021
4022 while (insn = NEXT_INSN (insn))
4023 {
a55e9d2b 4024 code = GET_CODE (insn);
8783b15e 4025
c8f6f18d
RK
4026#if 0
4027 /* If this is a label that existed before reload, then the register
4028 if dead here. However, if this is a label added by reorg, then
4029 the register may still be live here. We can't tell the difference,
4030 so we just ignore labels completely. */
8783b15e 4031 if (code == CODE_LABEL)
a55e9d2b 4032 return 1;
c8f6f18d
RK
4033 /* else */
4034#endif
a55e9d2b 4035
8b760293
JW
4036 if (code == JUMP_INSN)
4037 return 0;
4038
8783b15e
RK
4039 /* If this is a sequence, we must handle them all at once.
4040 We could have for instance a call that sets the target register,
4041 and a insn in a delay slot that uses the register. In this case,
4042 we must return 0. */
8b760293 4043 else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
a55e9d2b 4044 {
8783b15e
RK
4045 int i;
4046 int retval = 0;
4047
4048 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
4049 {
4050 rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
4051 rtx set = single_set (this_insn);
4052
4053 if (GET_CODE (this_insn) == CALL_INSN)
4054 code = CALL_INSN;
38f35781
JW
4055 else if (GET_CODE (this_insn) == JUMP_INSN)
4056 {
4057 if (INSN_ANNULLED_BRANCH_P (this_insn))
4058 return 0;
4059 code = JUMP_INSN;
4060 }
a55e9d2b 4061
8783b15e
RK
4062 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
4063 return 0;
4064 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4065 {
4066 if (GET_CODE (SET_DEST (set)) != MEM)
4067 retval = 1;
4068 else
4069 return 0;
4070 }
4071 if (set == 0
4072 && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
4073 return 0;
4074 }
4075 if (retval == 1)
4076 return 1;
38f35781
JW
4077 else if (code == JUMP_INSN)
4078 return 0;
8783b15e
RK
4079 }
4080 else if (GET_RTX_CLASS (code) == 'i')
a55e9d2b
RK
4081 {
4082 rtx set = single_set (insn);
4083
4084 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
4085 return 0;
4086 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4087 return GET_CODE (SET_DEST (set)) != MEM;
4088 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
4089 return 0;
4090 }
8783b15e
RK
4091
4092 if (code == CALL_INSN && call_used_regs[REGNO (reg)])
4093 return 1;
a55e9d2b
RK
4094 }
4095 return 1;
4096}