]> gcc.gnu.org Git - gcc.git/blame - gcc/config/sh/sh.c
89th Cygnus<->FSF quick merge
[gcc.git] / gcc / config / sh / sh.c
CommitLineData
16bea517 1/* Output routines for GCC for Hitachi Super-H.
05a81fe5 2 Copyright (C) 1993, 1994, 1995, 1996 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
00f8ff66
SC
38#define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
39#define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
40
20b04867 41/* ??? The pragma interrupt support will not work for SH3. */
8aa2a305
JW
42/* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
43 output code for the next function appropriate for an interrupt handler. */
0d7e008e 44int pragma_interrupt;
bc45ade3 45
8aa2a305
JW
46/* This is set by #pragma trapa, and is similar to the above, except that
47 the compiler doesn't emit code to preserve all registers. */
48static int pragma_trapa;
49
956a5206
JW
50/* This is set by #pragma nosave_low_regs. This is useful on the SH3,
51 which has a separate set of low regs for User and Supervisor modes.
52 This should only be used for the lowest level of interrupts. Higher levels
53 of interrupts must save the registers in case they themselves are
54 interrupted. */
55int pragma_nosave_low_regs;
56
8aa2a305
JW
57/* This is used for communication between SETUP_INCOMING_VARARGS and
58 sh_expand_prologue. */
bc45ade3 59int current_function_anonymous_args;
16bea517 60
8aa2a305
JW
61/* Global variables from toplev.c and final.c that are used within, but
62 not declared in any header file. */
b9654711 63extern char *version_string;
8aa2a305 64extern int *insn_addresses;
00f8ff66 65
bc45ade3
SC
66/* Global variables for machine-dependent things. */
67
16bea517
JW
68/* Which cpu are we scheduling for. */
69enum processor_type sh_cpu;
70
bc45ade3 71/* Saved operands from the last compare to use when we generate an scc
16bea517 72 or bcc insn. */
bc45ade3
SC
73
74rtx sh_compare_op0;
75rtx sh_compare_op1;
76
77/* Provides the class number of the smallest class containing
16bea517 78 reg number. */
bc45ade3
SC
79
80int regno_reg_class[FIRST_PSEUDO_REGISTER] =
81{
8e87e161 82 R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
bc45ade3
SC
83 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
84 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
85 GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
8e87e161 86 GENERAL_REGS, PR_REGS, T_REGS, NO_REGS,
2afeea0f 87 MAC_REGS, MAC_REGS, FPUL_REGS, GENERAL_REGS,
1a95a963
JW
88 FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
89 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
90 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
91 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
bc45ade3
SC
92};
93
94/* Provide reg_class from a letter such as appears in the machine
16bea517 95 description. */
bc45ade3
SC
96
97enum reg_class reg_class_from_letter[] =
98{
99 /* a */ NO_REGS, /* b */ NO_REGS, /* c */ NO_REGS, /* d */ NO_REGS,
1a95a963 100 /* e */ NO_REGS, /* f */ FP_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
bc45ade3
SC
101 /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ PR_REGS,
102 /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
103 /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
1a95a963
JW
104 /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
105 /* y */ FPUL_REGS, /* z */ R0_REGS
bc45ade3 106};
b9654711 107\f
16bea517 108/* Print the operand address in x to the stream. */
bc45ade3
SC
109
110void
111print_operand_address (stream, x)
112 FILE *stream;
113 rtx x;
114{
115 switch (GET_CODE (x))
116 {
117 case REG:
118 fprintf (stream, "@%s", reg_names[REGNO (x)]);
119 break;
8aa2a305 120
bc45ade3
SC
121 case PLUS:
122 {
123 rtx base = XEXP (x, 0);
124 rtx index = XEXP (x, 1);
125
bc45ade3
SC
126 switch (GET_CODE (index))
127 {
128 case CONST_INT:
8aa2a305 129 fprintf (stream, "@(%d,%s)", INTVAL (index),
bc45ade3
SC
130 reg_names[REGNO (base)]);
131 break;
132
133 case REG:
b9654711
SC
134 fprintf (stream, "@(r0,%s)",
135 reg_names[MAX (REGNO (base), REGNO (index))]);
bc45ade3
SC
136 break;
137
138 default:
b9654711 139 debug_rtx (x);
bc45ade3
SC
140 abort ();
141 }
142 }
bc45ade3 143 break;
8aa2a305 144
bc45ade3
SC
145 case PRE_DEC:
146 fprintf (stream, "@-%s", reg_names[REGNO (XEXP (x, 0))]);
147 break;
148
149 case POST_INC:
150 fprintf (stream, "@%s+", reg_names[REGNO (XEXP (x, 0))]);
151 break;
152
153 default:
154 output_addr_const (stream, x);
155 break;
156 }
157}
158
159/* Print operand x (an rtx) in assembler syntax to file stream
160 according to modifier code.
161
b9654711 162 '.' print a .s if insn needs delay slot
d3ae8277 163 '@' print rte or rts depending upon pragma interruptness
8aa2a305
JW
164 '#' output a nop if there is nothing to put in the delay slot
165 'O' print a constant without the #
00f8ff66 166 'R' print the LSW of a dp value - changes if in little endian
00f8ff66 167 'S' print the MSW of a dp value - changes if in little endian
8aa2a305 168 'T' print the next word of a dp value - same as 'R' in big endian mode. */
bc45ade3
SC
169
170void
171print_operand (stream, x, code)
172 FILE *stream;
173 rtx x;
174 int code;
175{
176 switch (code)
177 {
b9654711 178 case '.':
79b2746a
JW
179 if (final_sequence
180 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
b9654711
SC
181 fprintf (stream, ".s");
182 break;
d3ae8277
SC
183 case '@':
184 if (pragma_interrupt)
8e87e161 185 fprintf (stream, "rte");
d3ae8277 186 else
8e87e161 187 fprintf (stream, "rts");
d3ae8277 188 break;
bc45ade3 189 case '#':
16bea517 190 /* Output a nop if there's nothing in the delay slot. */
bc45ade3 191 if (dbr_sequence_length () == 0)
8aa2a305 192 fprintf (stream, "\n\tnop");
bc45ade3 193 break;
b9654711 194 case 'O':
0d7e008e 195 output_addr_const (stream, x);
bc45ade3 196 break;
bc45ade3 197 case 'R':
8aa2a305
JW
198 fputs (reg_names[REGNO (x) + LSW], (stream));
199 break;
200 case 'S':
201 fputs (reg_names[REGNO (x) + MSW], (stream));
00f8ff66
SC
202 break;
203 case 'T':
16bea517 204 /* Next word of a double. */
bc45ade3
SC
205 switch (GET_CODE (x))
206 {
207 case REG:
208 fputs (reg_names[REGNO (x) + 1], (stream));
209 break;
210 case MEM:
8aa2a305
JW
211 print_operand_address (stream,
212 XEXP (adj_offsettable_operand (x, 4), 0));
bc45ade3
SC
213 break;
214 }
215 break;
bc45ade3
SC
216 default:
217 switch (GET_CODE (x))
218 {
219 case REG:
220 fputs (reg_names[REGNO (x)], (stream));
221 break;
222 case MEM:
223 output_address (XEXP (x, 0));
224 break;
225 default:
226 fputc ('#', stream);
227 output_addr_const (stream, x);
228 break;
bc45ade3
SC
229 }
230 break;
231 }
232}
bc45ade3 233\f
0d7e008e
SC
234/* Emit code to perform a block move. Choose the best method.
235
236 OPERANDS[0] is the destination.
237 OPERANDS[1] is the source.
238 OPERANDS[2] is the size.
239 OPERANDS[3] is the alignment safe to use. */
240
0d7e008e
SC
241int
242expand_block_move (operands)
243 rtx *operands;
244{
245 int align = INTVAL (operands[3]);
246 int constp = (GET_CODE (operands[2]) == CONST_INT);
247 int bytes = (constp ? INTVAL (operands[2]) : 0);
0d7e008e 248
8aa2a305
JW
249 /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
250 alignment, or if it isn't a multiple of 4 bytes, then fail. */
251 if (! constp || align < 4 || (bytes % 4 != 0))
d3ae8277
SC
252 return 0;
253
8aa2a305 254 if (bytes < 64)
0d7e008e
SC
255 {
256 char entry[30];
257 tree entry_name;
258 rtx func_addr_rtx;
259 rtx r4 = gen_rtx (REG, SImode, 4);
260 rtx r5 = gen_rtx (REG, SImode, 5);
8aa2a305
JW
261
262 sprintf (entry, "__movstrSI%d", bytes);
0d7e008e
SC
263 entry_name = get_identifier (entry);
264
8aa2a305
JW
265 func_addr_rtx
266 = copy_to_mode_reg (Pmode,
267 gen_rtx (SYMBOL_REF, Pmode,
268 IDENTIFIER_POINTER (entry_name)));
0d7e008e
SC
269 emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
270 emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
271 emit_insn (gen_block_move_real (func_addr_rtx));
272 return 1;
273 }
8aa2a305
JW
274
275 /* This is the same number of bytes as a memcpy call, but to a different
276 less common function name, so this will occasionally use more space. */
277 if (! TARGET_SMALLCODE)
0d7e008e 278 {
0d7e008e
SC
279 tree entry_name;
280 rtx func_addr_rtx;
8aa2a305 281 int final_switch, while_loop;
0d7e008e
SC
282 rtx r4 = gen_rtx (REG, SImode, 4);
283 rtx r5 = gen_rtx (REG, SImode, 5);
284 rtx r6 = gen_rtx (REG, SImode, 6);
0d7e008e 285
8aa2a305
JW
286 entry_name = get_identifier ("__movstr");
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
8aa2a305
JW
294 /* r6 controls the size of the move. 16 is decremented from it
295 for each 64 bytes moved. Then the negative bit left over is used
296 as an index into a list of move instructions. e.g., a 72 byte move
297 would be set up with size(r6) = 14, for one iteration through the
298 big while loop, and a switch of -2 for the last part. */
0d7e008e 299
8aa2a305
JW
300 final_switch = 16 - ((bytes / 4) % 16);
301 while_loop = ((bytes / 4) / 16 - 1) * 16;
302 emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
303 emit_insn (gen_block_lump_real (func_addr_rtx));
304 return 1;
0d7e008e 305 }
0d7e008e 306
d3ae8277 307 return 0;
0d7e008e
SC
308}
309
bc45ade3 310/* Prepare operands for a move define_expand; specifically, one of the
8aa2a305 311 operands must be in a register. */
bc45ade3 312
b9654711 313int
bc45ade3
SC
314prepare_move_operands (operands, mode)
315 rtx operands[];
316 enum machine_mode mode;
317{
e22856e8
JW
318 if (! reload_in_progress && ! reload_completed)
319 {
320 /* Copy the source to a register if both operands aren't registers. */
321 if (! register_operand (operands[0], mode)
322 && ! register_operand (operands[1], mode))
323 operands[1] = copy_to_mode_reg (mode, operands[1]);
324
325 /* This case can happen while generating code to move the result
326 of a library call to the target. Reject `st r0,@(rX,rY)' because
327 reload will fail to find a spill register for rX, since r0 is already
328 being used for the source. */
329 else if (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 0
330 && GET_CODE (operands[0]) == MEM
331 && GET_CODE (XEXP (operands[0], 0)) == PLUS
332 && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
333 operands[1] = copy_to_mode_reg (mode, operands[1]);
334 }
8aa2a305
JW
335
336 return 0;
337}
338
339/* Prepare the operands for an scc instruction; make sure that the
340 compare has been done. */
341rtx
342prepare_scc_operands (code)
343 enum rtx_code code;
344{
345 rtx t_reg = gen_rtx (REG, SImode, T_REG);
346 enum rtx_code oldcode = code;
9374bd85 347 enum machine_mode mode;
8aa2a305
JW
348
349 /* First need a compare insn. */
350 switch (code)
bc45ade3 351 {
8aa2a305
JW
352 case NE:
353 /* It isn't possible to handle this case. */
354 abort ();
355 case LT:
356 code = GT;
357 break;
358 case LE:
359 code = GE;
360 break;
361 case LTU:
362 code = GTU;
363 break;
364 case LEU:
365 code = GEU;
366 break;
bc45ade3 367 }
8aa2a305 368 if (code != oldcode)
b9654711 369 {
8aa2a305
JW
370 rtx tmp = sh_compare_op0;
371 sh_compare_op0 = sh_compare_op1;
372 sh_compare_op1 = tmp;
0d7e008e 373 }
b9654711 374
9374bd85
RK
375 mode = GET_MODE (sh_compare_op0);
376 if (mode == VOIDmode)
377 mode = GET_MODE (sh_compare_op1);
378
379 sh_compare_op0 = force_reg (mode, sh_compare_op0);
8aa2a305
JW
380 if (code != EQ && code != NE
381 && (sh_compare_op1 != const0_rtx
382 || code == GTU || code == GEU || code == LTU || code == LEU))
9374bd85 383 sh_compare_op1 = force_reg (mode, sh_compare_op1);
0d7e008e 384
1a95a963
JW
385 /* ??? This should be `mode' not `SImode' in the compare, but that would
386 require fixing the branch patterns too. */
8aa2a305
JW
387 emit_insn (gen_rtx (SET, VOIDmode, t_reg,
388 gen_rtx (code, SImode, sh_compare_op0,
389 sh_compare_op1)));
0d7e008e 390
8aa2a305 391 return t_reg;
bc45ade3
SC
392}
393
8aa2a305
JW
394/* Called from the md file, set up the operands of a compare instruction. */
395
396void
397from_compare (operands, code)
398 rtx *operands;
8e87e161 399 int code;
bc45ade3 400{
8aa2a305 401 if (code != EQ && code != NE)
bc45ade3 402 {
24a25d45
JW
403 enum machine_mode mode = GET_MODE (sh_compare_op0);
404 if (mode == VOIDmode)
405 mode = GET_MODE (sh_compare_op1);
406
8aa2a305 407 /* Force args into regs, since we can't use constants here. */
24a25d45 408 sh_compare_op0 = force_reg (mode, sh_compare_op0);
8aa2a305
JW
409 if (sh_compare_op1 != const0_rtx
410 || code == GTU || code == GEU || code == LTU || code == LEU)
24a25d45 411 sh_compare_op1 = force_reg (mode, sh_compare_op1);
bc45ade3 412 }
8aa2a305
JW
413 operands[1] = sh_compare_op0;
414 operands[2] = sh_compare_op1;
bc45ade3
SC
415}
416\f
16bea517 417/* Functions to output assembly code. */
bc45ade3 418
b9654711 419/* Return a sequence of instructions to perform DI or DF move.
bc45ade3 420
b9654711 421 Since the SH cannot move a DI or DF in one instruction, we have
16bea517 422 to take care when we see overlapping source and dest registers. */
0d7e008e 423
bc45ade3 424char *
0d7e008e
SC
425output_movedouble (insn, operands, mode)
426 rtx insn;
bc45ade3
SC
427 rtx operands[];
428 enum machine_mode mode;
429{
b9654711
SC
430 rtx dst = operands[0];
431 rtx src = operands[1];
b9654711 432
0d7e008e 433 if (GET_CODE (dst) == MEM
16bea517 434 && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
8aa2a305
JW
435 return "mov.l %T1,%0\n\tmov.l %1,%0";
436
b9654711
SC
437 if (register_operand (dst, mode)
438 && register_operand (src, mode))
bc45ade3 439 {
b9654711 440 if (REGNO (src) == MACH_REG)
00f8ff66 441 return "sts mach,%S0\n\tsts macl,%R0";
bc45ade3 442
8aa2a305
JW
443 /* When mov.d r1,r2 do r2->r3 then r1->r2;
444 when mov.d r1,r0 do r1->r0 then r2->r1. */
b9654711
SC
445
446 if (REGNO (src) + 1 == REGNO (dst))
5325c0fa 447 return "mov %T1,%T0\n\tmov %1,%0";
b9654711 448 else
5325c0fa 449 return "mov %1,%0\n\tmov %T1,%T0";
b9654711
SC
450 }
451 else if (GET_CODE (src) == CONST_INT)
bc45ade3 452 {
8aa2a305
JW
453 if (INTVAL (src) < 0)
454 output_asm_insn ("mov #-1,%S0", operands);
bc45ade3 455 else
8aa2a305 456 output_asm_insn ("mov #0,%S0", operands);
bc45ade3 457
8aa2a305 458 return "mov %1,%R0";
0d7e008e 459 }
b9654711 460 else if (GET_CODE (src) == MEM)
bc45ade3 461 {
8aa2a305 462 int ptrreg = -1;
b9654711
SC
463 int dreg = REGNO (dst);
464 rtx inside = XEXP (src, 0);
bc45ade3
SC
465
466 if (GET_CODE (inside) == REG)
8aa2a305 467 ptrreg = REGNO (inside);
79b2746a
JW
468 else if (GET_CODE (inside) == SUBREG)
469 ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
bc45ade3
SC
470 else if (GET_CODE (inside) == PLUS)
471 {
8aa2a305
JW
472 ptrreg = REGNO (XEXP (inside, 0));
473 /* ??? A r0+REG address shouldn't be possible here, because it isn't
474 an offsettable address. Unfortunately, offsettable addresses use
475 QImode to check the offset, and a QImode offsettable address
476 requires r0 for the other operand, which is not currently
477 supported, so we can't use the 'o' constraint.
478 Thus we must check for and handle r0+REG addresses here.
479 We punt for now, since this is likely very rare. */
480 if (GET_CODE (XEXP (inside, 1)) == REG)
481 abort ();
bc45ade3 482 }
0d7e008e 483 else if (GET_CODE (inside) == LABEL_REF)
8aa2a305 484 return "mov.l %1,%0\n\tmov.l %1+4,%T0";
8e87e161 485 else if (GET_CODE (inside) == POST_INC)
8aa2a305 486 return "mov.l %1,%0\n\tmov.l %1,%T0";
bc45ade3
SC
487 else
488 abort ();
489
8aa2a305
JW
490 /* Work out the safe way to copy. Copy into the second half first. */
491 if (dreg == ptrreg)
492 return "mov.l %T1,%T0\n\tmov.l %1,%0";
bc45ade3
SC
493 }
494
00f8ff66 495 return "mov.l %1,%0\n\tmov.l %T1,%T0";
bc45ade3
SC
496}
497
8aa2a305
JW
498/* Print an instruction which would have gone into a delay slot after
499 another instruction, but couldn't because the other instruction expanded
500 into a sequence where putting the slot insn at the end wouldn't work. */
0d7e008e 501
8aa2a305
JW
502static void
503print_slot (insn)
504 rtx insn;
505{
506 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
b9654711 507
8aa2a305
JW
508 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
509}
0d7e008e 510
8aa2a305
JW
511/* We can't tell if we need a register as a scratch for the jump
512 until after branch shortening, and then it's too late to allocate a
513 register the 'proper' way. These instruction sequences are rare
514 anyway, so to avoid always using a reg up from our limited set, we'll
515 grab one when we need one on output. */
0d7e008e 516
8aa2a305
JW
517/* ??? Should fix compiler so that using a clobber scratch in jump
518 instructions works, and then this will be unnecessary. */
0d7e008e
SC
519
520char *
521output_far_jump (insn, op)
522 rtx insn;
523 rtx op;
524{
525 rtx thislab = gen_label_rtx ();
526
8aa2a305 527 /* Output the delay slot insn first if any. */
0d7e008e 528 if (dbr_sequence_length ())
8aa2a305 529 print_slot (final_sequence);
b9654711 530
8aa2a305
JW
531 output_asm_insn ("mov.l r13,@-r15", 0);
532 output_asm_insn ("mov.l %O0,r13", &thislab);
533 output_asm_insn ("jmp @r13", 0);
534 output_asm_insn ("mov.l @r15+,r13", 0);
0d7e008e 535 output_asm_insn (".align 2", 0);
d3ae8277 536 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (thislab));
0d7e008e
SC
537 output_asm_insn (".long %O0", &op);
538 return "";
539}
bc45ade3 540
8aa2a305
JW
541/* Local label counter, used for constants in the pool and inside
542 pattern branches. */
543
544static int lf = 100;
545
546/* Output code for ordinary branches. */
547
bc45ade3 548char *
8aa2a305 549output_branch (logic, insn, operands)
bc45ade3 550 int logic;
b9654711 551 rtx insn;
8aa2a305 552 rtx *operands;
bc45ade3 553{
bc45ade3 554 int label = lf++;
e4fa6b06
ILT
555 int length = get_attr_length (insn);
556 int adjusted_length;
8e87e161 557
e4fa6b06 558 /* Undo the effects of ADJUST_INSN_LENGTH, so that we get the real
9c0e838f
ILT
559 length. If NEXT_INSN (PREV_INSN (insn)) != insn, then the insn
560 is inside a sequence, and ADJUST_INSN_LENGTH was not called on
561 it. */
562 if (PREV_INSN (insn) == NULL
563 || NEXT_INSN (PREV_INSN (insn)) == insn)
564 {
565 adjusted_length = length;
566 ADJUST_INSN_LENGTH (insn, adjusted_length);
567 length -= (adjusted_length - length);
568 }
e4fa6b06
ILT
569
570 switch (length)
bc45ade3
SC
571 {
572 case 2:
22e1ebf1
JW
573 /* A branch with an unfilled delay slot. */
574 case 4:
575 /* Simple branch in range -252..+258 bytes */
b9654711 576 return logic ? "bt%. %l0" : "bf%. %l0";
bc45ade3
SC
577
578 case 6:
22e1ebf1
JW
579 /* A branch with an unfilled delay slot. */
580 case 8:
16bea517 581 /* Branch in range -4092..+4098 bytes. */
b9654711 582 {
8aa2a305
JW
583 /* The call to print_slot will clobber the operands. */
584 rtx op0 = operands[0];
b9654711 585
79b2746a
JW
586 /* If the instruction in the delay slot is annulled (true), then
587 there is no delay slot where we can put it now. The only safe
588 place for it is after the label. */
589
16bea517 590 if (final_sequence)
b9654711 591 {
79b2746a
JW
592 fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
593 INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
594 ? "" : ".s", label);
595 if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
596 print_slot (final_sequence);
b9654711 597 }
b9654711 598 else
8aa2a305 599 fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
b9654711 600
8aa2a305
JW
601 output_asm_insn ("bra %l0", &op0);
602 fprintf (asm_out_file, "\tnop\n");
b9654711 603 fprintf (asm_out_file, "LF%d:\n", label);
79b2746a
JW
604
605 if (final_sequence
606 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
607 print_slot (final_sequence);
b9654711 608 }
bc45ade3
SC
609 return "";
610
8aa2a305
JW
611 case 16:
612 /* A branch with an unfilled delay slot. */
613 case 18:
614 /* Branches a long way away. */
615 {
616 /* The call to print_slot will clobber the operands. */
617 rtx op0 = operands[0];
bc45ade3 618
79b2746a
JW
619 /* If the instruction in the delay slot is annulled (true), then
620 there is no delay slot where we can put it now. The only safe
621 place for it is after the label. */
622
8aa2a305
JW
623 if (final_sequence)
624 {
79b2746a
JW
625 fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
626 INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
627 ? "" : ".s", label);
628 if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
629 print_slot (final_sequence);
8aa2a305
JW
630 }
631 else
632 fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
bc45ade3 633
8aa2a305
JW
634 output_far_jump (insn, op0);
635 fprintf (asm_out_file, "LF%d:\n", label);
79b2746a
JW
636
637 if (final_sequence
638 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
639 print_slot (final_sequence);
8aa2a305 640 }
79b2746a 641 return "";
bc45ade3 642 }
e4fa6b06
ILT
643
644 abort ();
bc45ade3 645}
8e87e161 646\f
8aa2a305
JW
647/* Output to FILE the start of the assembler file. */
648
0d7e008e 649void
adade868 650output_file_start (file)
0d7e008e 651 FILE *file;
bc45ade3 652{
0d7e008e 653 register int pos;
b9654711 654
0d7e008e 655 output_file_directive (file, main_input_filename);
b9654711 656
0d7e008e
SC
657 /* Switch to the data section so that the coffsem symbol and the
658 gcc2_compiled. symbol aren't in the text section. */
659 data_section ();
b9654711 660
8aa2a305
JW
661 if (TARGET_LITTLE_ENDIAN)
662 fprintf (file, "\t.little\n");
bc45ade3 663}
0d7e008e 664\f
16bea517 665/* Actual number of instructions used to make a shift by N. */
8aa2a305 666static char ashiftrt_insns[] =
16bea517
JW
667 { 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};
668
669/* Left shift and logical right shift are the same. */
8aa2a305 670static char shift_insns[] =
16bea517 671 { 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 672
16bea517
JW
673/* Individual shift amounts needed to get the above length sequences.
674 One bit right shifts clobber the T bit, so when possible, put one bit
675 shifts in the middle of the sequence, so the ends are eligible for
676 branch delay slots. */
8aa2a305 677static short shift_amounts[32][5] = {
16bea517
JW
678 {0}, {1}, {2}, {2, 1},
679 {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
680 {8}, {8, 1}, {8, 2}, {8, 1, 2},
681 {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
682 {16}, {16, 1}, {16, 2}, {16, 1, 2},
683 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
684 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1d3534c0 685 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
16bea517 686
8d481241
JW
687/* Likewise, but for shift amounts < 16, up to three highmost bits
688 might be clobbered. This is typically used when combined with some
689 kind of sign or zero extension. */
690
691static char ext_shift_insns[] =
692 { 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};
693
694static short ext_shift_amounts[32][4] = {
695 {0}, {1}, {2}, {2, 1},
696 {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
697 {8}, {8, 1}, {8, 2}, {8, 1, 2},
698 {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
699 {16}, {16, 1}, {16, 2}, {16, 1, 2},
700 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
701 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
702 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
703
16bea517
JW
704/* This is used in length attributes in sh.md to help compute the length
705 of arbitrary constant shift instructions. */
bc45ade3 706
16bea517
JW
707int
708shift_insns_rtx (insn)
709 rtx insn;
710{
711 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
712 int shift_count = INTVAL (XEXP (set_src, 1));
713 enum rtx_code shift_code = GET_CODE (set_src);
00f8ff66 714
16bea517
JW
715 switch (shift_code)
716 {
717 case ASHIFTRT:
718 return ashiftrt_insns[shift_count];
719 case LSHIFTRT:
720 case ASHIFT:
721 return shift_insns[shift_count];
722 default:
723 abort();
724 }
725}
726
16bea517 727/* Return the cost of a shift. */
bc45ade3 728
0d7e008e 729int
8aa2a305
JW
730shiftcosts (x)
731 rtx x;
bc45ade3 732{
8aa2a305
JW
733 int value = INTVAL (XEXP (x, 1));
734
16bea517 735 /* If shift by a non constant, then this will be expensive. */
8aa2a305 736 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
00f8ff66 737 {
20b04867
JW
738 if (TARGET_SH3)
739 return 2;
740 /* If not an sh3 then we don't even have an instruction for it. */
00f8ff66
SC
741 return 20;
742 }
bc45ade3 743
16bea517 744 /* Otherwise, return the true cost in instructions. */
8aa2a305 745 if (GET_CODE (x) == ASHIFTRT)
49b6d06b
JW
746 {
747 int cost = ashiftrt_insns[value];
748 /* If SH3, then we put the constant in a reg and use shad. */
749 if (TARGET_SH3 && cost > 3)
750 cost = 3;
751 return cost;
752 }
8aa2a305
JW
753 else
754 return shift_insns[value];
0d7e008e 755}
b9654711 756
8aa2a305
JW
757/* Return the cost of an AND operation. */
758
8e87e161 759int
8aa2a305
JW
760andcosts (x)
761 rtx x;
0d7e008e
SC
762{
763 int i;
8aa2a305 764
a6f71af5 765 /* Anding with a register is a single cycle and instruction. */
8aa2a305 766 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
a6f71af5 767 return 1;
8aa2a305
JW
768
769 i = INTVAL (XEXP (x, 1));
a6f71af5 770 /* These constants are single cycle extu.[bw] instructions. */
0d7e008e 771 if (i == 0xff || i == 0xffff)
a6f71af5
JW
772 return 1;
773 /* Constants that can be used in an and immediate instruction is a single
774 cycle, but this requires r0, so make it a little more expensive. */
775 if (CONST_OK_FOR_L (i))
0d7e008e 776 return 2;
a6f71af5
JW
777 /* Constants that can be loaded with a mov immediate and an and.
778 This case is probably unnecessary. */
0d7e008e 779 if (CONST_OK_FOR_I (i))
a6f71af5
JW
780 return 2;
781 /* Any other constants requires a 2 cycle pc-relative load plus an and.
782 This case is probably unnecessary. */
783 return 3;
0d7e008e 784}
d3ae8277 785
16bea517 786/* Return the cost of a multiply. */
0d7e008e 787int
8aa2a305
JW
788multcosts (x)
789 rtx x;
0d7e008e
SC
790{
791 if (TARGET_SH2)
d3ae8277
SC
792 {
793 /* We have a mul insn, so we can never take more than the mul and the
a7771f78 794 read of the mac reg, but count more because of the latency and extra
16bea517 795 reg usage. */
d3ae8277 796 if (TARGET_SMALLCODE)
8e87e161 797 return 2;
a7771f78 798 return 3;
d3ae8277
SC
799 }
800
a7771f78 801 /* If we're aiming at small code, then just count the number of
16bea517 802 insns in a multiply call sequence. */
8e87e161 803 if (TARGET_SMALLCODE)
8aa2a305 804 return 5;
d3ae8277 805
16bea517 806 /* Otherwise count all the insns in the routine we'd be calling too. */
d3ae8277 807 return 20;
0d7e008e 808}
b9654711 809
16bea517 810/* Code to expand a shift. */
b9654711 811
0d7e008e
SC
812void
813gen_ashift (type, n, reg)
814 int type;
815 int n;
816 rtx reg;
817{
16bea517
JW
818 /* Negative values here come from the shift_amounts array. */
819 if (n < 0)
820 {
821 if (type == ASHIFT)
822 type = LSHIFTRT;
823 else
824 type = ASHIFT;
825 n = -n;
826 }
827
0d7e008e 828 switch (type)
bc45ade3 829 {
0d7e008e
SC
830 case ASHIFTRT:
831 emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
832 break;
833 case LSHIFTRT:
16bea517
JW
834 if (n == 1)
835 emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
836 else
837 emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
0d7e008e
SC
838 break;
839 case ASHIFT:
5325c0fa 840 emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
0d7e008e 841 break;
bc45ade3 842 }
bc45ade3 843}
bc45ade3 844
8d481241
JW
845/* Same for HImode */
846
847void
848gen_ashift_hi (type, n, reg)
849 int type;
850 int n;
851 rtx reg;
852{
853 /* Negative values here come from the shift_amounts array. */
854 if (n < 0)
855 {
856 if (type == ASHIFT)
857 type = LSHIFTRT;
858 else
859 type = ASHIFT;
860 n = -n;
861 }
862
863 switch (type)
864 {
865 case ASHIFTRT:
866 emit_insn (gen_ashrhi3_k (reg, reg, GEN_INT (n)));
867 break;
868 case LSHIFTRT:
869 if (n == 1)
870 emit_insn (gen_lshrhi3_m (reg, reg, GEN_INT (n)));
871 else
872 emit_insn (gen_lshrhi3_k (reg, reg, GEN_INT (n)));
873 break;
874 case ASHIFT:
875 emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
876 break;
877 }
878}
879
8aa2a305
JW
880/* Output RTL to split a constant shift into its component SH constant
881 shift instructions. */
882
0d7e008e
SC
883int
884gen_shifty_op (code, operands)
885 int code;
886 rtx *operands;
bc45ade3 887{
16bea517 888 int value = INTVAL (operands[2]);
8aa2a305 889 int max, i;
00f8ff66 890
cff3d762
JW
891 /* Truncate the shift count in case it is out of bounds. */
892 value = value & 0x1f;
893
8aa2a305 894 if (value == 31)
16bea517 895 {
8aa2a305 896 if (code == LSHIFTRT)
0d7e008e 897 {
8aa2a305
JW
898 emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
899 emit_insn (gen_movt (operands[0]));
900 return;
16bea517 901 }
8aa2a305 902 else if (code == ASHIFT)
16bea517 903 {
8aa2a305
JW
904 /* There is a two instruction sequence for 31 bit left shifts,
905 but it requires r0. */
906 if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
0d7e008e 907 {
8aa2a305
JW
908 emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
909 emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
910 return;
0d7e008e 911 }
16bea517 912 }
8aa2a305 913 }
c17f53a8
JW
914 else if (value == 0)
915 {
916 /* This can happen when not optimizing. We must output something here
917 to prevent the compiler from aborting in final.c after the try_split
918 call. */
919 emit_insn (gen_nop ());
920 return;
921 }
a9f71ad8 922
8aa2a305
JW
923 max = shift_insns[value];
924 for (i = 0; i < max; i++)
925 gen_ashift (code, shift_amounts[value][i], operands[0]);
926}
8d481241
JW
927
928/* Same as above, but optimized for values where the topmost bits don't
929 matter. */
930
931int
932gen_shifty_hi_op (code, operands)
933 int code;
934 rtx *operands;
935{
936 int value = INTVAL (operands[2]);
937 int max, i;
938 void (*gen_fun)();
939
940 /* This operation is used by and_shl for SImode values with a few
941 high bits known to be cleared. */
942 value &= 31;
943 if (value == 0)
944 {
945 emit_insn (gen_nop ());
946 return;
947 }
948
949 gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
950 if (code == ASHIFT)
951 {
952 max = ext_shift_insns[value];
953 for (i = 0; i < max; i++)
954 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
955 }
956 else
957 /* When shifting right, emit the shifts in reverse order, so that
958 solitary negative values come first. */
959 for (i = ext_shift_insns[value] - 1; i >= 0; i--)
960 gen_fun (code, ext_shift_amounts[value][i], operands[0]);
961}
8aa2a305
JW
962
963/* Output RTL for an arithmetic right shift. */
964
965/* ??? Rewrite to use super-optimizer sequences. */
966
967int
968expand_ashiftrt (operands)
969 rtx *operands;
970{
971 rtx wrk;
972 char func[18];
973 tree func_name;
974 int value;
975
49b6d06b 976 if (TARGET_SH3)
20b04867 977 {
49b6d06b
JW
978 if (GET_CODE (operands[2]) != CONST_INT)
979 {
980 rtx count = copy_to_mode_reg (SImode, operands[2]);
981 emit_insn (gen_negsi2 (count, count));
982 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
983 return 1;
984 }
985 else if (ashiftrt_insns[INTVAL (operands[2])] > 3)
986 {
987 rtx count = force_reg (SImode, GEN_INT (- INTVAL (operands[2])));
988 emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
989 return 1;
990 }
20b04867 991 }
8aa2a305
JW
992 if (GET_CODE (operands[2]) != CONST_INT)
993 return 0;
994
995 value = INTVAL (operands[2]);
996
997 if (value == 31)
998 {
999 emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1000 return 1;
1001 }
1002 else if (value >= 16 && value <= 19)
1003 {
1004 wrk = gen_reg_rtx (SImode);
1005 emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1006 value -= 16;
1007 while (value--)
1008 gen_ashift (ASHIFTRT, 1, wrk);
1009 emit_move_insn (operands[0], wrk);
1010 return 1;
a9f71ad8 1011 }
8aa2a305
JW
1012 /* Expand a short sequence inline, longer call a magic routine. */
1013 else if (value <= 5)
1014 {
1015 wrk = gen_reg_rtx (SImode);
1016 emit_move_insn (wrk, operands[1]);
1017 while (value--)
1018 gen_ashift (ASHIFTRT, 1, wrk);
1019 emit_move_insn (operands[0], wrk);
1020 return 1;
1021 }
1022
1023 wrk = gen_reg_rtx (Pmode);
1024
1025 /* Load the value into an arg reg and call a helper. */
1026 emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
1027 sprintf (func, "__ashiftrt_r4_%d", value);
1028 func_name = get_identifier (func);
1029 emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
1030 IDENTIFIER_POINTER (func_name)));
1031 emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1032 emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
1033 return 1;
bc45ade3 1034}
8d481241
JW
1035
1036/* Try to find a good way to implement the combiner pattern
1037 [(set (match_operand:SI 0 "register_operand" "r")
1038 (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1039 (match_operand:SI 2 "const_int_operand" "n"))
1040 (match_operand:SI 3 "const_int_operand" "n"))) .
1041 LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1042 return 0 for simple right / left or left/right shift combination.
1043 return 1 for a combination of shifts with zero_extend.
1044 return 2 for a combination of shifts with an AND that needs r0.
1045 return 3 for a combination of shifts with an AND that needs an extra
1046 scratch register, when the three highmost bits of the AND mask are clear.
1047 return 4 for a combination of shifts with an AND that needs an extra
1048 scratch register, when any of the three highmost bits of the AND mask
1049 is set.
1050 If ATTRP is set, store an initial right shift width in ATTRP[0],
1051 and the instruction length in ATTRP[1] . These values are not valid
1052 when returning 0.
1053 When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1054 shift_amounts for the last shift value that is to be used before the
1055 sign extend. */
1056int
1057shl_and_kind (left_rtx, mask_rtx, attrp)
1058 rtx left_rtx, mask_rtx;
1059 int *attrp;
1060{
1061 unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1062 int left = INTVAL (left_rtx), right;
1063 int best = 0;
1064 int cost, best_cost = 10000;
1065 int best_right = 0, best_len = 0;
1066 int i;
1067 int can_ext;
1068
1069 if (left < 0 || left > 31)
1070 return 0;
1071 if (GET_CODE (mask_rtx) == CONST_INT)
1072 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
1073 else
1074 mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
1075 /* Can this be expressed as a right shift / left shift pair ? */
1076 lsb = ((mask ^ (mask - 1)) >> 1) + 1;
1077 right = exact_log2 (lsb);
1078 mask2 = ~(mask + lsb - 1);
1079 lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
1080 /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1081 if (! mask2)
1082 best_cost = shift_insns[right] + shift_insns[right + left];
1083 /* mask has no trailing zeroes <==> ! right */
1084 else if (! right && mask2 == ~(lsb2 - 1))
1085 {
1086 int late_right = exact_log2 (lsb2);
1087 best_cost = shift_insns[left + late_right] + shift_insns[late_right];
1088 }
1089 /* Try to use zero extend */
1090 if (mask2 == ~(lsb2 - 1))
1091 {
1092 int width, first;
1093
1094 for (width = 8; width <= 16; width += 8)
1095 {
1096 /* Can we zero-extend right away? */
1097 if (lsb2 == (HOST_WIDE_INT)1 << width)
1098 {
1099 cost
1100 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
1101 if (cost < best_cost)
1102 {
1103 best = 1;
1104 best_cost = cost;
1105 best_right = right;
1106 best_len = cost;
1107 if (attrp)
1108 attrp[2] = -1;
1109 }
1110 continue;
1111 }
1112 /* ??? Could try to put zero extend into initial right shift,
1113 or even shift a bit left before the right shift. */
1114 /* Determine value of first part of left shift, to get to the
1115 zero extend cut-off point. */
1116 first = width - exact_log2 (lsb2) + right;
1117 if (first >= 0 && right + left - first >= 0)
1118 {
1119 cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
1120 + ext_shift_insns[right + left - first];
1121 if (cost < best_cost)
1122 {
1123 best = 1;
1124 best_cost = cost;
1125 best_right = right;
1126 best_len = cost;
1127 if (attrp)
1128 attrp[2] = first;
1129 }
1130 }
1131 }
1132 }
1133 /* Try to use r0 AND pattern */
1134 for (i = 0; i <= 2; i++)
1135 {
1136 if (i > right)
1137 break;
1138 if (! CONST_OK_FOR_L (mask >> i))
1139 continue;
1140 cost = (i != 0) + 2 + ext_shift_insns[left + i];
1141 if (cost < best_cost)
1142 {
1143 best = 2;
1144 best_cost = cost;
1145 best_right = i;
1146 best_len = cost - 1;
1147 }
1148 }
1149 /* Try to use a scratch register to hold the AND operand. */
1150 can_ext = ((mask << left) & 0xe0000000) == 0;
1151 for (i = 0; i <= 2; i++)
1152 {
1153 if (i > right)
1154 break;
1155 cost = (i != 0) + (CONST_OK_FOR_I (mask >> i) ? 2 : 3)
1156 + (can_ext ? ext_shift_insns : shift_insns)[left];
1157 if (cost < best_cost)
1158 {
1159 best = 4 - can_ext;
1160 best_cost = cost;
1161 best_right = i;
1162 best_len = cost - 1 - ! CONST_OK_FOR_I (mask >> i);
1163 }
1164 }
1165
1166 if (attrp)
1167 {
1168 attrp[0] = best_right;
1169 attrp[1] = best_len;
1170 }
1171 return best;
1172}
1173
1174/* This is used in length attributes of the unnamed instructions
1175 corresponding to shl_and_kind return values of 1 and 2. */
1176int
1177shl_and_length (insn)
1178 rtx insn;
1179{
1180 rtx set_src, left_rtx, mask_rtx;
1181 int attributes[3];
1182
1183 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1184 left_rtx = XEXP (XEXP (set_src, 0), 1);
1185 mask_rtx = XEXP (set_src, 1);
1186 shl_and_kind (left_rtx, mask_rtx, attributes);
1187 return attributes[1];
1188}
1189
1190/* This is used in length attribute of the and_shl_scratch instruction. */
1191
1192int
1193shl_and_scr_length (insn)
1194 rtx insn;
1195{
1196 rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1197 int len = shift_insns[INTVAL (XEXP (set_src, 1))];
1198 rtx op = XEXP (set_src, 0);
1199 len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
1200 op = XEXP (XEXP (op, 0), 0);
1201 return len + shift_insns[INTVAL (XEXP (op, 1))];
1202}
1203
1204/* Generating rtl? */
1205extern int rtx_equal_function_value_matters;
1206
1207/* Generate rtl for instructions for which shl_and_kind advised a particular
1208 method of generating them, i.e. returned zero. */
1209
1210int
1211gen_shl_and (dest, left_rtx, mask_rtx, source)
1212 rtx dest, left_rtx, mask_rtx, source;
1213{
1214 int attributes[3];
1215 unsigned HOST_WIDE_INT mask;
1216 int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
1217 int right, total_shift;
1218 int (*shift_gen_fun) PROTO((int, rtx*)) = gen_shifty_hi_op;
1219
1220 right = attributes[0];
1221 total_shift = INTVAL (left_rtx) + right;
1222 mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
1223 switch (kind)
1224 {
1225 default:
1226 return -1;
1227 case 1:
1228 {
1229 int first = attributes[2];
1230 rtx operands[3];
1231
1232 if (first < 0)
1233 {
1234 emit_insn ((mask << right) == 0xff
1235 ? gen_zero_extendqisi2(dest, gen_rtx (SUBREG, QImode,
1236 source, 0))
1237 : gen_zero_extendhisi2(dest, gen_rtx (SUBREG, HImode,
1238 source, 0)));
1239 source = dest;
1240 }
1241 if (source != dest)
1242 emit_insn (gen_movsi (dest, source));
1243 operands[0] = dest;
1244 if (right)
1245 {
1246 operands[2] = GEN_INT (right);
1247 gen_shifty_hi_op (LSHIFTRT, operands);
1248 }
1249 if (first > 0)
1250 {
1251 operands[2] = GEN_INT (first);
1252 gen_shifty_hi_op (ASHIFT, operands);
1253 total_shift -= first;
1254 mask <<= first;
1255 }
1256 if (first >= 0)
1257 emit_insn (mask == 0xff
1258 ? gen_zero_extendqisi2(dest, gen_rtx (SUBREG, QImode,
1259 dest, 0))
1260 : gen_zero_extendhisi2(dest, gen_rtx (SUBREG, HImode,
1261 dest, 0)));
1262 if (total_shift > 0)
1263 {
1264 operands[2] = GEN_INT (total_shift);
1265 gen_shifty_hi_op (ASHIFT, operands);
1266 }
1267 break;
1268 }
1269 case 4:
1270 shift_gen_fun = gen_shifty_op;
1271 case 2:
1272 case 3:
1273 /* Don't expand fine-grained when combining, because that will
1274 make the pattern fail. */
1275 if (rtx_equal_function_value_matters
1276 || reload_in_progress || reload_completed)
1277 {
1278 rtx operands[3];
1279
1280 if (right)
1281 {
1282 emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
1283 source = dest;
1284 }
1285 emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
1286 operands[0] = dest;
1287 operands[1] = dest;
1288 operands[2] = GEN_INT (total_shift);
1289 shift_gen_fun (ASHIFT, operands);
1290 break;
1291 }
1292 else
1293 {
1294 int neg = 0;
1295 if (kind != 4 && total_shift < 16)
1296 {
1297 neg = -ext_shift_amounts[total_shift][1];
1298 if (neg > 0)
1299 neg -= ext_shift_amounts[total_shift][2];
1300 else
1301 neg = 0;
1302 }
1303 emit_insn (gen_and_shl_scratch (dest, source,
1304 GEN_INT (right),
1305 GEN_INT (mask),
1306 GEN_INT (total_shift + neg),
1307 GEN_INT (neg)));
1308 emit_insn (gen_movsi (dest, dest));
1309 break;
1310 }
1311 }
1312 return 0;
1313}
1314
1315/* Try to find a good way to implement the combiner pattern
1316 [(set (match_operand:SI 0 "register_operand" "=r")
1317 (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1318 (match_operand:SI 2 "const_int_operand" "n")
1319 (match_operand:SI 3 "const_int_operand" "n")
1320 (const_int 0)))
1321 (clobber (reg:SI 18))]
1322 LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1323 return 0 for simple left / right shift combination.
1324 return 1 for left shift / 8 bit sign extend / left shift.
1325 return 2 for left shift / 16 bit sign extend / left shift.
1326 return 3 for left shift / 8 bit sign extend / shift / sign extend.
1327 return 4 for left shift / 16 bit sign extend / shift / sign extend.
1328 return 5 for left shift / 16 bit sign extend / right shift
1329 return 6 for < 8 bit sign extend / left shift.
1330 return 7 for < 8 bit sign extend / left shift / single right shift.
1331 If COSTP is nonzero, assign the calculated cost to *COSTP. */
1332
1333int
1334shl_sext_kind (left_rtx, size_rtx, costp)
1335 rtx left_rtx, size_rtx;
1336 int *costp;
1337{
1338 int left, size, insize, ext;
1339 int cost, best_cost;
1340 int kind;
1341
1342 left = INTVAL (left_rtx);
1343 size = INTVAL (size_rtx);
1344 insize = size - left;
1345 if (insize <= 0)
1346 abort ();
1347 /* Default to left / right shift. */
1348 kind = 0;
1349 best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
1350 if (size <= 16)
1351 {
1352 /* 16 bit shift / sign extend / 16 bit shift */
1353 cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
1354 if (cost < best_cost)
1355 {
1356 kind = 5;
1357 best_cost = cost;
1358 }
1359 }
1360 /* Try a plain sign extend between two shifts. */
1361 for (ext = 16; ext >= insize; ext -= 8)
1362 {
1363 if (ext <= size)
1364 {
1365 cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
1366 if (cost < best_cost)
1367 {
1368 kind = ext / 8U;
1369 best_cost = cost;
1370 }
1371 }
1372 if (size <= 16)
1373 {
1374 /* Maybe it's cheaper to do the second shift sloppy, and do a
1375 final sign extend? */
1376 cost = ext_shift_insns[ext - insize] + 1
1377 + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
1378 if (cost < best_cost)
1379 {
1380 kind = ext / 8U + 2;
1381 best_cost = cost;
1382 }
1383 }
1384 }
1385 /* Check if we can sign extend in r0 */
1386 if (insize < 8)
1387 {
1388 cost = 3 + shift_insns[left];
1389 if (cost < best_cost)
1390 {
1391 kind = 6;
1392 best_cost = cost;
1393 }
1394 /* Try the same with a final signed shift. */
1395 if (left < 31)
1396 {
1397 cost = 3 + ext_shift_insns[left + 1] + 1;
1398 if (cost < best_cost)
1399 {
1400 kind = 7;
1401 best_cost = cost;
1402 }
1403 }
1404 }
1405 if (TARGET_SH3)
1406 {
1407 /* Try to use a dynamic shift. */
1408 cost = shift_insns[32 - insize] + 3;
1409 if (cost < best_cost)
1410 {
1411 kind = 0;
1412 best_cost = cost;
1413 }
1414 }
1415 if (costp)
1416 *costp = cost;
1417 return kind;
1418}
1419
1420/* Function to be used in the length attribute of the instructions
1421 implementing this pattern. */
1422
1423int
1424shl_sext_length (insn)
1425 rtx insn;
1426{
1427 rtx set_src, left_rtx, size_rtx;
1428 int cost;
1429
1430 set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1431 left_rtx = XEXP (XEXP (set_src, 0), 1);
1432 size_rtx = XEXP (set_src, 1);
1433 shl_sext_kind (left_rtx, size_rtx, &cost);
1434 return cost;
1435}
1436
1437/* Generate rtl for this pattern */
1438
1439int
1440gen_shl_sext (dest, left_rtx, size_rtx, source)
1441 rtx dest, left_rtx, size_rtx, source;
1442{
1443 int kind;
1444 int left, size, insize;
1445 rtx operands[3];
1446
1447 kind = shl_sext_kind (left_rtx, size_rtx);
1448 left = INTVAL (left_rtx);
1449 size = INTVAL (size_rtx);
1450 insize = size - left;
1451 switch (kind)
1452 {
1453 case 1:
1454 case 2:
1455 case 3:
1456 case 4:
1457 {
1458 int ext = kind & 1 ? 8 : 16;
1459 int shift2 = size - ext;
1460
1461 /* Don't expand fine-grained when combining, because that will
1462 make the pattern fail. */
1463 if (! rtx_equal_function_value_matters
1464 && ! reload_in_progress && ! reload_completed)
1465 {
1466 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1467 emit_insn (gen_movsi (dest, source));
1468 break;
1469 }
1470 if (dest != source)
1471 emit_insn (gen_movsi (dest, source));
1472 operands[0] = dest;
1473 operands[2] = GEN_INT (ext - insize);
1474 gen_shifty_hi_op (ASHIFT, operands);
1475 emit_insn (kind & 1
1476 ? gen_extendqisi2(dest, gen_rtx (SUBREG, QImode, dest, 0))
1477 : gen_extendhisi2(dest, gen_rtx (SUBREG, HImode, dest, 0)));
1478 if (kind <= 2)
1479 {
1480 operands[2] = GEN_INT (shift2);
1481 gen_shifty_op (ASHIFT, operands);
1482 }
1483 else
1484 {
1485 if (shift2 >= 0)
1486 {
1487 operands[2] = GEN_INT (shift2);
1488 gen_shifty_hi_op (ASHIFT, operands);
1489 }
1490 else
1491 {
1492 operands[2] = GEN_INT (-shift2);
1493 gen_shifty_hi_op (LSHIFTRT, operands);
1494 }
1495 emit_insn (size <= 8
1496 ? gen_extendqisi2 (dest,
1497 gen_rtx (SUBREG, QImode, dest, 0))
1498 : gen_extendhisi2 (dest,
1499 gen_rtx (SUBREG, HImode, dest, 0)));
1500 }
1501 break;
1502 }
1503 case 5:
1504 emit_insn (gen_shl_sext_ext (dest, source, GEN_INT (16 - insize),
1505 GEN_INT (16)));
1506 emit_insn (gen_ashrsi3 (dest, dest, GEN_INT (16 - size)));
1507 break;
1508 case 6:
1509 case 7:
1510 /* Don't expand fine-grained when combining, because that will
1511 make the pattern fail. */
1512 if (! rtx_equal_function_value_matters
1513 && ! reload_in_progress && ! reload_completed)
1514 {
1515 emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1516 emit_insn (gen_movsi (dest, source));
1517 break;
1518 }
1519 emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
1520 emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
1521 emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
1522 operands[0] = dest;
1523 operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
1524 gen_shifty_op (ASHIFT, operands);
1525 if (kind == 7)
1526 emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
1527 break;
1528 default:
1529 return -1;
1530 }
1531 return 0;
1532}
8aa2a305
JW
1533\f
1534/* The SH cannot load a large constant into a register, constants have to
1535 come from a pc relative load. The reference of a pc relative load
1536 instruction must be less than 1k infront of the instruction. This
1537 means that we often have to dump a constant inside a function, and
1538 generate code to branch around it.
bc45ade3 1539
8aa2a305
JW
1540 It is important to minimize this, since the branches will slow things
1541 down and make things bigger.
1542
1543 Worst case code looks like:
1544
1545 mov.l L1,rn
1546 bra L2
1547 nop
1548 align
1549 L1: .long value
1550 L2:
1551 ..
1552
1553 mov.l L3,rn
1554 bra L4
1555 nop
1556 align
1557 L3: .long value
1558 L4:
1559 ..
1560
1561 We fix this by performing a scan before scheduling, which notices which
1562 instructions need to have their operands fetched from the constant table
1563 and builds the table.
1564
1565 The algorithm is:
1566
1567 scan, find an instruction which needs a pcrel move. Look forward, find the
1568 last barrier which is within MAX_COUNT bytes of the requirement.
1569 If there isn't one, make one. Process all the instructions between
1570 the find and the barrier.
1571
1572 In the above example, we can tell that L3 is within 1k of L1, so
1573 the first move can be shrunk from the 3 insn+constant sequence into
1574 just 1 insn, and the constant moved to L3 to make:
1575
1576 mov.l L1,rn
1577 ..
1578 mov.l L3,rn
1579 bra L4
1580 nop
1581 align
1582 L3:.long value
1583 L4:.long value
1584
1585 Then the second move becomes the target for the shortening process. */
1586
1587typedef struct
1588{
1589 rtx value; /* Value in table. */
1590 rtx label; /* Label of value. */
1591 enum machine_mode mode; /* Mode of value. */
1592} pool_node;
1593
1594/* The maximum number of constants that can fit into one pool, since
1595 the pc relative range is 0...1020 bytes and constants are at least 4
1596 bytes long. */
1597
1598#define MAX_POOL_SIZE (1020/4)
1599static pool_node pool_vector[MAX_POOL_SIZE];
1600static int pool_size;
1601
1602/* ??? If we need a constant in HImode which is the truncated value of a
1603 constant we need in SImode, we could combine the two entries thus saving
1604 two bytes. Is this common enough to be worth the effort of implementing
1605 it? */
1606
1607/* ??? This stuff should be done at the same time that we shorten branches.
1608 As it is now, we must assume that all branches are the maximum size, and
1609 this causes us to almost always output constant pools sooner than
1610 necessary. */
1611
1612/* Add a constant to the pool and return its label. */
1613
1614static rtx
1615add_constant (x, mode)
1616 rtx x;
1617 enum machine_mode mode;
0d7e008e
SC
1618{
1619 int i;
8aa2a305
JW
1620 rtx lab;
1621
1622 /* First see if we've already got it. */
1623 for (i = 0; i < pool_size; i++)
0d7e008e 1624 {
8aa2a305
JW
1625 if (x->code == pool_vector[i].value->code
1626 && mode == pool_vector[i].mode)
0d7e008e 1627 {
8aa2a305
JW
1628 if (x->code == CODE_LABEL)
1629 {
1630 if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1631 continue;
1632 }
1633 if (rtx_equal_p (x, pool_vector[i].value))
1634 return pool_vector[i].label;
0d7e008e 1635 }
0d7e008e 1636 }
b9654711 1637
8aa2a305
JW
1638 /* Need a new one. */
1639 pool_vector[pool_size].value = x;
1640 lab = gen_label_rtx ();
1641 pool_vector[pool_size].mode = mode;
1642 pool_vector[pool_size].label = lab;
1643 pool_size++;
1644 return lab;
0d7e008e 1645}
16bea517
JW
1646
1647/* Output the literal table. */
b9654711 1648
b9654711 1649static void
0d7e008e
SC
1650dump_table (scan)
1651 rtx scan;
b9654711 1652{
0d7e008e 1653 int i;
0d7e008e 1654 int need_align = 1;
b9654711 1655
16bea517 1656 /* Do two passes, first time dump out the HI sized constants. */
b9654711 1657
0d7e008e 1658 for (i = 0; i < pool_size; i++)
b9654711 1659 {
8aa2a305
JW
1660 pool_node *p = &pool_vector[i];
1661
0d7e008e
SC
1662 if (p->mode == HImode)
1663 {
1664 if (need_align)
1665 {
1666 scan = emit_insn_after (gen_align_2 (), scan);
1667 need_align = 0;
1668 }
1669 scan = emit_label_after (p->label, scan);
1670 scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1671 }
b9654711 1672 }
8aa2a305 1673
0d7e008e 1674 need_align = 1;
b9654711 1675
0d7e008e 1676 for (i = 0; i < pool_size; i++)
b9654711 1677 {
8aa2a305 1678 pool_node *p = &pool_vector[i];
b9654711 1679
0d7e008e 1680 switch (p->mode)
b9654711 1681 {
0d7e008e
SC
1682 case HImode:
1683 break;
1684 case SImode:
1685 if (need_align)
b9654711 1686 {
0d7e008e 1687 need_align = 0;
d3ae8277 1688 scan = emit_label_after (gen_label_rtx (), scan);
0d7e008e 1689 scan = emit_insn_after (gen_align_4 (), scan);
b9654711 1690 }
0d7e008e
SC
1691 scan = emit_label_after (p->label, scan);
1692 scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1693 break;
1694 case DImode:
1695 if (need_align)
1696 {
1697 need_align = 0;
d3ae8277 1698 scan = emit_label_after (gen_label_rtx (), scan);
0d7e008e
SC
1699 scan = emit_insn_after (gen_align_4 (), scan);
1700 }
1701 scan = emit_label_after (p->label, scan);
1702 scan = emit_insn_after (gen_consttable_8 (p->value), scan);
1703 break;
1704 default:
1705 abort ();
1706 break;
b9654711
SC
1707 }
1708 }
b9654711 1709
0d7e008e
SC
1710 scan = emit_insn_after (gen_consttable_end (), scan);
1711 scan = emit_barrier_after (scan);
1712 pool_size = 0;
1713}
b9654711 1714
8aa2a305
JW
1715/* Return non-zero if constant would be an ok source for a
1716 mov.w instead of a mov.l. */
b9654711 1717
16bea517 1718static int
8aa2a305 1719hi_const (src)
0d7e008e 1720 rtx src;
0d7e008e 1721{
8aa2a305
JW
1722 return (GET_CODE (src) == CONST_INT
1723 && INTVAL (src) >= -32768
1724 && INTVAL (src) <= 32767);
b9654711
SC
1725}
1726
8aa2a305
JW
1727/* Non-zero if the insn is a move instruction which needs to be fixed. */
1728
1729/* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1730 CONST_DOUBLE input value is CONST_OK_FOR_I. For a SFmode move, we don't
1731 need to fix it if the input value is CONST_OK_FOR_I. */
1732
1733static int
1734broken_move (insn)
1735 rtx insn;
b9654711 1736{
8aa2a305
JW
1737 if (GET_CODE (insn) == INSN
1738 && GET_CODE (PATTERN (insn)) == SET
1739 /* We can load any 8 bit value if we don't care what the high
1740 order bits end up as. */
1741 && GET_MODE (SET_DEST (PATTERN (insn))) != QImode
1742 && CONSTANT_P (SET_SRC (PATTERN (insn)))
1a95a963
JW
1743 && ! (GET_CODE (SET_SRC (PATTERN (insn))) == CONST_DOUBLE
1744 && (fp_zero_operand (SET_SRC (PATTERN (insn)))
1745 || fp_one_operand (SET_SRC (PATTERN (insn))))
1746 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
1747 && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_FP_REG
1748 && REGNO (SET_DEST (PATTERN (insn))) <= LAST_FP_REG)
8aa2a305
JW
1749 && (GET_CODE (SET_SRC (PATTERN (insn))) != CONST_INT
1750 || ! CONST_OK_FOR_I (INTVAL (SET_SRC (PATTERN (insn))))))
d3ae8277
SC
1751 return 1;
1752
8aa2a305 1753 return 0;
b9654711 1754}
b9654711 1755
8aa2a305
JW
1756/* Find the last barrier from insn FROM which is close enough to hold the
1757 constant pool. If we can't find one, then create one near the end of
1758 the range. */
b9654711 1759
5325c0fa
JW
1760/* ??? It would be good to put constant pool tables between a case jump and
1761 the jump table. This fails for two reasons. First, there is no
1762 barrier after the case jump. This is a bug in the casesi pattern.
1763 Second, inserting the table here may break the mova instruction that
1764 loads the jump table address, by moving the jump table too far away.
1765 We fix that problem by never outputting the constant pool between a mova
1766 and its label. */
1767
8aa2a305 1768static rtx
06c386ea
SC
1769find_barrier (from)
1770 rtx from;
b9654711 1771{
0d7e008e
SC
1772 int count_si = 0;
1773 int count_hi = 0;
1774 int found_hi = 0;
1775 int found_si = 0;
1776 rtx found_barrier = 0;
5325c0fa 1777 rtx found_mova = 0;
c17f53a8
JW
1778 int si_limit;
1779 int hi_limit;
8aa2a305
JW
1780
1781 /* For HImode: range is 510, add 4 because pc counts from address of
1782 second instruction after this one, subtract 2 for the jump instruction
3adbde60
JW
1783 that we may need to emit before the table, subtract 2 for the instruction
1784 that fills the jump delay slot (in very rare cases, reorg will take an
1785 instruction from after the constant pool or will leave the delay slot
1786 empty). This gives 510.
8aa2a305
JW
1787 For SImode: range is 1020, add 4 because pc counts from address of
1788 second instruction after this one, subtract 2 in case pc is 2 byte
1789 aligned, subtract 2 for the jump instruction that we may need to emit
3adbde60
JW
1790 before the table, subtract 2 for the instruction that fills the jump
1791 delay slot. This gives 1018. */
c17f53a8
JW
1792
1793 /* If not optimizing, then it is possible that the jump instruction we add
1794 won't be shortened, and thus will have a length of 14 instead of 2.
1795 We must adjust the limits downwards to account for this, giving a limit
1796 of 1008 for SImode and 500 for HImode. */
1797
1798 if (optimize)
1799 {
3adbde60
JW
1800 si_limit = 1018;
1801 hi_limit = 510;
c17f53a8
JW
1802 }
1803 else
1804 {
1805 si_limit = 1008;
1806 hi_limit = 500;
1807 }
1808
e4fa6b06
ILT
1809 /* If not optimizing for space, then the constant pool will be
1810 aligned to a 4 to 16 byte boundary. We must make room for that
1811 alignment that by reducing the limits.
1812 ??? It would be better to not align the constant pool, but
1813 ASM_OUTPUT_ALIGN_CODE does not make any provision for basing the
1814 alignment on the instruction. */
1815
1816 if (! TARGET_SMALLCODE)
1817 {
1818 if (TARGET_SH3 || TARGET_SH3E)
1819 {
1820 si_limit -= 14;
1821 hi_limit -= 14;
1822 }
1823 else
1824 {
1825 si_limit -= 2;
1826 hi_limit -= 2;
1827 }
1828 }
1829
c17f53a8 1830 while (from && count_si < si_limit && count_hi < hi_limit)
0d7e008e 1831 {
5dbcc9c0 1832 int inc = get_attr_length (from);
8aa2a305 1833
0d7e008e 1834 if (GET_CODE (from) == BARRIER)
8aa2a305
JW
1835 found_barrier = from;
1836
8aa2a305 1837 if (broken_move (from))
0d7e008e 1838 {
c17f53a8
JW
1839 rtx pat = PATTERN (from);
1840 rtx src = SET_SRC (pat);
1841 rtx dst = SET_DEST (pat);
1842 enum machine_mode mode = GET_MODE (dst);
1843
1844 /* We must explicitly check the mode, because sometimes the
1845 front end will generate code to load unsigned constants into
1846 HImode targets without properly sign extending them. */
1847 if (mode == HImode || (mode == SImode && hi_const (src)))
00e94d65
RK
1848 {
1849 found_hi = 1;
1850 /* We put the short constants before the long constants, so
1851 we must count the length of short constants in the range
1852 for the long constants. */
1853 /* ??? This isn't optimal, but is easy to do. */
1854 if (found_si)
1855 count_si += 2;
1856 }
0d7e008e
SC
1857 else
1858 found_si = 1;
0d7e008e 1859 }
5325c0fa 1860
5325c0fa
JW
1861 if (GET_CODE (from) == INSN
1862 && GET_CODE (PATTERN (from)) == SET
16bea517
JW
1863 && GET_CODE (SET_SRC (PATTERN (from))) == UNSPEC
1864 && XINT (SET_SRC (PATTERN (from)), 1) == 1)
5325c0fa
JW
1865 found_mova = from;
1866 else if (GET_CODE (from) == JUMP_INSN
1867 && (GET_CODE (PATTERN (from)) == ADDR_VEC
1868 || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1869 found_mova = 0;
1870
0d7e008e
SC
1871 if (found_si)
1872 count_si += inc;
1873 if (found_hi)
1874 count_hi += inc;
1875 from = NEXT_INSN (from);
1876 }
1877
5325c0fa
JW
1878 /* Insert the constant pool table before the mova instruction, to prevent
1879 the mova label reference from going out of range. */
1880 if (found_mova)
1881 from = found_mova;
1882
8aa2a305 1883 if (! found_barrier)
b9654711 1884 {
8aa2a305
JW
1885 /* We didn't find a barrier in time to dump our stuff,
1886 so we'll make one. */
0d7e008e 1887 rtx label = gen_label_rtx ();
8aa2a305 1888
5dbcc9c0
RK
1889 /* If we exceeded the range, then we must back up over the last
1890 instruction we looked at. Otherwise, we just need to undo the
1891 NEXT_INSN at the end of the loop. */
c17f53a8 1892 if (count_hi > hi_limit || count_si > si_limit)
5dbcc9c0
RK
1893 from = PREV_INSN (PREV_INSN (from));
1894 else
1895 from = PREV_INSN (from);
1896
8aa2a305
JW
1897 /* Walk back to be just before any jump or label.
1898 Putting it before a label reduces the number of times the branch
1899 around the constant pool table will be hit. Putting it before
1900 a jump makes it more likely that the bra delay slot will be
1901 filled. */
1902 while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
8e87e161 1903 || GET_CODE (from) == CODE_LABEL)
8aa2a305
JW
1904 from = PREV_INSN (from);
1905
0d7e008e
SC
1906 from = emit_jump_insn_after (gen_jump (label), from);
1907 JUMP_LABEL (from) = label;
e081ed26 1908 LABEL_NUSES (label) = 1;
0d7e008e
SC
1909 found_barrier = emit_barrier_after (from);
1910 emit_label_after (label, found_barrier);
b9654711 1911 }
b9654711 1912
8aa2a305 1913 return found_barrier;
0d7e008e 1914}
b9654711 1915
933c3ba3
JW
1916/* See if the only way in which INSN uses REG is by calling it, or by
1917 setting it while calling it. Set *SET to a SET rtx if the register
1918 is set by INSN. */
1919
1920static int
1921noncall_uses_reg (reg, insn, set)
1922 rtx reg;
1923 rtx insn;
1924 rtx *set;
1925{
1926 rtx pattern;
1927
1928 *set = NULL_RTX;
1929
1930 if (GET_CODE (insn) != CALL_INSN)
1931 {
1932 /* We don't use rtx_equal_p because we don't care if the mode is
1933 different. */
1934 pattern = single_set (insn);
1935 if (pattern
1936 && GET_CODE (SET_DEST (pattern)) == REG
1937 && REGNO (reg) == REGNO (SET_DEST (pattern)))
1938 {
1939 *set = pattern;
1940 return 0;
1941 }
1942
1943 return 1;
1944 }
1945
1946 pattern = PATTERN (insn);
1947
1948 if (GET_CODE (pattern) == PARALLEL)
1949 {
1950 int i;
1951
1952 for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
1953 if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
1954 return 1;
1955 pattern = XVECEXP (pattern, 0, 0);
1956 }
1957
1958 if (GET_CODE (pattern) == SET)
1959 {
1960 if (reg_mentioned_p (reg, SET_DEST (pattern)))
1961 {
1962 /* We don't use rtx_equal_p, because we don't care if the
1963 mode is different. */
1964 if (GET_CODE (SET_DEST (pattern)) != REG
1965 || REGNO (reg) != REGNO (SET_DEST (pattern)))
1966 return 1;
1967
1968 *set = pattern;
1969 }
1970
1971 pattern = SET_SRC (pattern);
1972 }
1973
1974 if (GET_CODE (pattern) != CALL
1975 || GET_CODE (XEXP (pattern, 0)) != MEM
1976 || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
1977 return 1;
1978
1979 return 0;
1980}
1981
8aa2a305 1982/* Exported to toplev.c.
b9654711 1983
933c3ba3
JW
1984 Do a final pass over the function, just before delayed branch
1985 scheduling. */
b9654711 1986
0d7e008e
SC
1987void
1988machine_dependent_reorg (first)
1989 rtx first;
1990{
1991 rtx insn;
8aa2a305 1992
933c3ba3
JW
1993 /* If relaxing, generate pseudo-ops to associate function calls with
1994 the symbols they call. It does no harm to not generate these
1995 pseudo-ops. However, when we can generate them, it enables to
1996 linker to potentially relax the jsr to a bsr, and eliminate the
1997 register load and, possibly, the constant pool entry. */
1998
1999 if (TARGET_RELAX)
2000 {
2001 /* Remove all REG_LABEL notes. We want to use them for our own
2002 purposes. This works because none of the remaining passes
2003 need to look at them.
2004
2005 ??? But it may break in the future. We should use a machine
2006 dependent REG_NOTE, or some other approach entirely. */
2007 for (insn = first; insn; insn = NEXT_INSN (insn))
2008 {
2009 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2010 {
2011 rtx note;
2012
2013 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
2014 remove_note (insn, note);
2015 }
2016 }
2017
2018 for (insn = first; insn; insn = NEXT_INSN (insn))
2019 {
2020 rtx pattern, reg, link, set, scan, dies, label;
2021 int rescan = 0, foundinsn = 0;
2022
2023 if (GET_CODE (insn) != CALL_INSN)
2024 continue;
2025
2026 pattern = PATTERN (insn);
2027
2028 if (GET_CODE (pattern) == PARALLEL)
2029 pattern = XVECEXP (pattern, 0, 0);
2030 if (GET_CODE (pattern) == SET)
2031 pattern = SET_SRC (pattern);
2032
2033 if (GET_CODE (pattern) != CALL
2034 || GET_CODE (XEXP (pattern, 0)) != MEM)
2035 continue;
2036
2037 reg = XEXP (XEXP (pattern, 0), 0);
2038 if (GET_CODE (reg) != REG)
2039 continue;
2040
2041 /* This is a function call via REG. If the only uses of REG
2042 between the time that it is set and the time that it dies
2043 are in function calls, then we can associate all the
2044 function calls with the setting of REG. */
2045
2046 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
2047 {
1075deda
ILT
2048 if (REG_NOTE_KIND (link) != 0)
2049 continue;
933c3ba3
JW
2050 set = single_set (XEXP (link, 0));
2051 if (set && rtx_equal_p (reg, SET_DEST (set)))
2052 {
2053 link = XEXP (link, 0);
2054 break;
2055 }
2056 }
2057
2058 if (! link)
2059 {
2060 /* ??? Sometimes global register allocation will have
2061 deleted the insn pointed to by LOG_LINKS. Try
2062 scanning backward to find where the register is set. */
2063 for (scan = PREV_INSN (insn);
2064 scan && GET_CODE (scan) != CODE_LABEL;
2065 scan = PREV_INSN (scan))
2066 {
2067 if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2068 continue;
2069
2070 if (! reg_mentioned_p (reg, scan))
2071 continue;
2072
2073 if (noncall_uses_reg (reg, scan, &set))
2074 break;
2075
2076 if (set)
2077 {
2078 link = scan;
2079 break;
2080 }
2081 }
2082 }
2083
2084 if (! link)
2085 continue;
2086
2087 /* The register is set at LINK. */
2088
2089 /* We can only optimize the function call if the register is
2090 being set to a symbol. In theory, we could sometimes
2091 optimize calls to a constant location, but the assembler
2092 and linker do not support that at present. */
2093 if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
2094 && GET_CODE (SET_SRC (set)) != LABEL_REF)
2095 continue;
2096
2097 /* Scan forward from LINK to the place where REG dies, and
2098 make sure that the only insns which use REG are
2099 themselves function calls. */
2100
c1e8e6bd
JW
2101 /* ??? This doesn't work for call targets that were allocated
2102 by reload, since there may not be a REG_DEAD note for the
2103 register. */
2104
933c3ba3
JW
2105 dies = NULL_RTX;
2106 for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
2107 {
2108 rtx scanset;
2109
c1e8e6bd
JW
2110 /* Don't try to trace forward past a CODE_LABEL if we haven't
2111 seen INSN yet. Ordinarily, we will only find the setting insn
2112 in LOG_LINKS if it is in the same basic block. However,
2113 cross-jumping can insert code labels in between the load and
2114 the call, and can result in situations where a single call
2115 insn may have two targets depending on where we came from. */
2116
2117 if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
2118 break;
2119
933c3ba3
JW
2120 if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2121 continue;
2122
2123 /* Don't try to trace forward past a JUMP. To optimize
2124 safely, we would have to check that all the
c1e8e6bd 2125 instructions at the jump destination did not use REG. */
933c3ba3 2126
c1e8e6bd 2127 if (GET_CODE (scan) == JUMP_INSN)
933c3ba3
JW
2128 break;
2129
2130 if (! reg_mentioned_p (reg, scan))
2131 continue;
2132
2133 if (noncall_uses_reg (reg, scan, &scanset))
2134 break;
2135
2136 if (scan == insn)
2137 foundinsn = 1;
2138
2139 if (scan != insn && GET_CODE (scan) == CALL_INSN)
2140 {
2141 /* There is a function call to this register other
2142 than the one we are checking. If we optimize
2143 this call, we need to rescan again below. */
2144 rescan = 1;
2145 }
2146
2147 /* ??? We shouldn't have to worry about SCANSET here.
2148 We should just be able to check for a REG_DEAD note
2149 on a function call. However, the REG_DEAD notes are
2150 apparently not dependable around libcalls; c-torture
2151 execute/920501-2 is a test case. If SCANSET is set,
2152 then this insn sets the register, so it must have
2153 died earlier. Unfortunately, this will only handle
2154 the cases in which the register is, in fact, set in a
2155 later insn. */
2156
2157 /* ??? We shouldn't have to use FOUNDINSN here.
2158 However, the LOG_LINKS fields are apparently not
2159 entirely reliable around libcalls;
2160 newlib/libm/math/e_pow.c is a test case. Sometimes
2161 an insn will appear in LOG_LINKS even though it is
2162 not the most recent insn which sets the register. */
2163
2164 if (foundinsn
2165 && (scanset
2166 || find_reg_note (scan, REG_DEAD, reg)))
2167 {
2168 dies = scan;
2169 break;
2170 }
2171 }
2172
2173 if (! dies)
2174 {
2175 /* Either there was a branch, or some insn used REG
2176 other than as a function call address. */
2177 continue;
2178 }
2179
2180 /* Create a code label, and put it in a REG_LABEL note on
2181 the insn which sets the register, and on each call insn
2182 which uses the register. In final_prescan_insn we look
2183 for the REG_LABEL notes, and output the appropriate label
2184 or pseudo-op. */
2185
2186 label = gen_label_rtx ();
2187 REG_NOTES (link) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2188 REG_NOTES (link));
2189 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2190 REG_NOTES (insn));
2191 if (rescan)
2192 {
2193 scan = link;
2194 do
2195 {
2196 scan = NEXT_INSN (scan);
2197 if (scan != insn
2198 && GET_CODE (scan) == CALL_INSN
2199 && reg_mentioned_p (reg, scan))
2200 REG_NOTES (scan) = gen_rtx (EXPR_LIST, REG_LABEL,
2201 label, REG_NOTES (scan));
2202 }
2203 while (scan != dies);
2204 }
2205 }
2206 }
2207
2208 /* Scan the function looking for move instructions which have to be
2209 changed to pc-relative loads and insert the literal tables. */
2210
0d7e008e
SC
2211 for (insn = first; insn; insn = NEXT_INSN (insn))
2212 {
2213 if (broken_move (insn))
2214 {
0d7e008e 2215 rtx scan;
8aa2a305
JW
2216 /* Scan ahead looking for a barrier to stick the constant table
2217 behind. */
0d7e008e 2218 rtx barrier = find_barrier (insn);
b9654711 2219
16bea517 2220 /* Now find all the moves between the points and modify them. */
0d7e008e
SC
2221 for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
2222 {
2223 if (broken_move (scan))
2224 {
2225 rtx pat = PATTERN (scan);
2226 rtx src = SET_SRC (pat);
2227 rtx dst = SET_DEST (pat);
2228 enum machine_mode mode = GET_MODE (dst);
2229 rtx lab;
2230 rtx newinsn;
2231 rtx newsrc;
0d7e008e
SC
2232
2233 if (mode == SImode && hi_const (src))
2234 {
23ed92ca 2235 int offset = 0;
8aa2a305 2236
0d7e008e 2237 mode = HImode;
d3ae8277 2238 while (GET_CODE (dst) == SUBREG)
23ed92ca
JW
2239 {
2240 offset += SUBREG_WORD (dst);
2241 dst = SUBREG_REG (dst);
2242 }
2243 dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
0d7e008e 2244 }
8aa2a305 2245
0d7e008e
SC
2246 lab = add_constant (src, mode);
2247 newsrc = gen_rtx (MEM, mode,
2248 gen_rtx (LABEL_REF, VOIDmode, lab));
8aa2a305
JW
2249 RTX_UNCHANGING_P (newsrc) = 1;
2250 newinsn = emit_insn_after (gen_rtx (SET, VOIDmode,
2251 dst, newsrc), scan);
933c3ba3
JW
2252 REG_NOTES (newinsn) = REG_NOTES (scan);
2253 REG_NOTES (scan) = NULL_RTX;
c17f53a8
JW
2254 /* If not optimizing, then delete_insn doesn't remove the
2255 insn from the chain, and hence is not useful. We
2256 convert the instruction to a NOTE in that case. */
2257 if (optimize)
2258 delete_insn (scan);
2259 else
2260 {
2261 PUT_CODE (scan, NOTE);
2262 NOTE_LINE_NUMBER (scan) = NOTE_INSN_DELETED;
2263 NOTE_SOURCE_FILE (insn) = 0;
2264 }
0d7e008e
SC
2265 scan = newinsn;
2266 }
2267 }
2268 dump_table (barrier);
2269 }
2270 }
b9654711
SC
2271}
2272
8aa2a305 2273/* Dump out instruction addresses, which is useful for debugging the
933c3ba3
JW
2274 constant pool table stuff.
2275
2276 If relaxing, output the label and pseudo-ops used to link together
2277 calls and the instruction which set the registers. */
8aa2a305
JW
2278
2279/* ??? This is unnecessary, and probably should be deleted. This makes
2280 the insn_addresses declaration above unnecessary. */
2281
2282/* ??? The addresses printed by this routine for insns are nonsense for
2283 insns which are inside of a sequence where none of the inner insns have
2284 variable length. This is because the second pass of shorten_branches
2285 does not bother to update them. */
0d7e008e 2286
8e87e161 2287void
8aa2a305
JW
2288final_prescan_insn (insn, opvec, noperands)
2289 rtx insn;
2290 rtx *opvec;
2291 int noperands;
b9654711 2292{
8aa2a305
JW
2293 if (TARGET_DUMPISIZE)
2294 fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
933c3ba3
JW
2295
2296 if (TARGET_RELAX)
2297 {
2298 rtx note;
2299
2300 note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2301 if (note)
2302 {
2303 rtx pattern;
2304
2305 pattern = PATTERN (insn);
2306 if (GET_CODE (pattern) == PARALLEL)
2307 pattern = XVECEXP (pattern, 0, 0);
2308 if (GET_CODE (pattern) == CALL
2309 || (GET_CODE (pattern) == SET
2310 && GET_CODE (SET_SRC (pattern)) == CALL))
2311 fprintf (asm_out_file, "\t.uses L%d\n",
2312 CODE_LABEL_NUMBER (XEXP (note, 0)));
2313 else if (GET_CODE (pattern) == SET)
2314 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
2315 CODE_LABEL_NUMBER (XEXP (note, 0)));
2316 else
2317 abort ();
2318 }
2319 }
0d7e008e 2320}
b9654711 2321
8aa2a305
JW
2322/* Dump out any constants accumulated in the final pass. These will
2323 will only be labels. */
b9654711 2324
8aa2a305
JW
2325char *
2326output_jump_label_table ()
0d7e008e 2327{
8aa2a305
JW
2328 int i;
2329
2330 if (pool_size)
2331 {
2332 fprintf (asm_out_file, "\t.align 2\n");
2333 for (i = 0; i < pool_size; i++)
2334 {
2335 pool_node *p = &pool_vector[i];
2336
2337 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
2338 CODE_LABEL_NUMBER (p->label));
2339 output_asm_insn (".long %O0", &p->value);
2340 }
2341 pool_size = 0;
2342 }
b9654711 2343
8aa2a305
JW
2344 return "";
2345}
2346\f
2347/* A full frame looks like:
16bea517
JW
2348
2349 arg-5
2350 arg-4
2351 [ if current_function_anonymous_args
2352 arg-3
2353 arg-2
2354 arg-1
2355 arg-0 ]
2356 saved-fp
2357 saved-r10
2358 saved-r11
2359 saved-r12
2360 saved-pr
2361 local-n
2362 ..
2363 local-1
2364 local-0 <- fp points here. */
8e87e161 2365
8aa2a305
JW
2366/* Number of bytes pushed for anonymous args, used to pass information
2367 between expand_prologue and expand_epilogue. */
2368
2369static int extra_push;
2370
2371/* Adjust the stack and return the number of bytes taken to do it. */
2372
2373static void
46d81ffa 2374output_stack_adjust (size, reg)
8aa2a305 2375 int size;
46d81ffa 2376 rtx reg;
8aa2a305
JW
2377{
2378 if (size)
2379 {
2380 rtx val = GEN_INT (size);
2381 rtx insn;
2382
2383 if (! CONST_OK_FOR_I (size))
2384 {
2385 rtx reg = gen_rtx (REG, SImode, 3);
2386 emit_insn (gen_movsi (reg, val));
2387 val = reg;
2388 }
2389
46d81ffa 2390 insn = gen_addsi3 (reg, reg, val);
8aa2a305
JW
2391 emit_insn (insn);
2392 }
2393}
2394
2395/* Output RTL to push register RN onto the stack. */
2396
2397static void
2398push (rn)
2399 int rn;
2400{
2401 rtx x;
1a95a963
JW
2402 if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
2403 || rn == FPUL_REG)
2404 x = emit_insn (gen_push_e (gen_rtx (REG, SFmode, rn)));
2405 else
2406 x = emit_insn (gen_push (gen_rtx (REG, SImode, rn)));
2407
8aa2a305
JW
2408 REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
2409 gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
2410}
2411
2412/* Output RTL to pop register RN from the stack. */
2413
2414static void
2415pop (rn)
2416 int rn;
2417{
2418 rtx x;
1a95a963
JW
2419 if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
2420 || rn == FPUL_REG)
2421 x = emit_insn (gen_pop_e (gen_rtx (REG, SFmode, rn)));
2422 else
2423 x = emit_insn (gen_pop (gen_rtx (REG, SImode, rn)));
2424
8aa2a305
JW
2425 REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
2426 gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
2427}
2428
2429/* Generate code to push the regs specified in the mask, and return
2430 the number of bytes the insns take. */
2431
2432static void
1a95a963
JW
2433push_regs (mask, mask2)
2434 int mask, mask2;
8aa2a305
JW
2435{
2436 int i;
2437
1a95a963 2438 for (i = 0; i < 32; i++)
8aa2a305
JW
2439 if (mask & (1 << i))
2440 push (i);
1a95a963
JW
2441 for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
2442 if (mask2 & (1 << (i - 32)))
2443 push (i);
8aa2a305
JW
2444}
2445
2446/* Work out the registers which need to be saved, both as a mask and a
2447 count.
2448
2449 If doing a pragma interrupt function, then push all regs used by the
2450 function, and if we call another function (we can tell by looking at PR),
2451 make sure that all the regs it clobbers are safe too. */
2452
2453static int
1a95a963 2454calc_live_regs (count_ptr, live_regs_mask2)
8aa2a305 2455 int *count_ptr;
1a95a963 2456 int *live_regs_mask2;
8aa2a305
JW
2457{
2458 int reg;
2459 int live_regs_mask = 0;
2460 int count = 0;
2461
1a95a963 2462 *live_regs_mask2 = 0;
8aa2a305
JW
2463 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
2464 {
2465 if (pragma_interrupt && ! pragma_trapa)
2466 {
1a95a963 2467 /* Need to save all the regs ever live. */
8aa2a305 2468 if ((regs_ever_live[reg]
20fef67b
JW
2469 || (call_used_regs[reg] && ! fixed_regs[reg]
2470 && regs_ever_live[PR_REG])
2471 || reg == MACH_REG
2472 || reg == MACL_REG)
8aa2a305 2473 && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
1a95a963 2474 && reg != T_REG && reg != GBR_REG)
8aa2a305 2475 {
1a95a963
JW
2476 if (reg >= 32)
2477 *live_regs_mask2 |= 1 << (reg - 32);
2478 else
2479 live_regs_mask |= 1 << reg;
8aa2a305
JW
2480 count++;
2481 }
2482 }
2483 else
2484 {
2485 /* Only push those regs which are used and need to be saved. */
2486 if (regs_ever_live[reg] && ! call_used_regs[reg])
2487 {
1a95a963
JW
2488 if (reg >= 32)
2489 *live_regs_mask2 |= 1 << (reg - 32);
2490 else
2491 live_regs_mask |= (1 << reg);
8aa2a305
JW
2492 count++;
2493 }
2494 }
2495 }
2496
2497 *count_ptr = count;
2498 return live_regs_mask;
2499}
2500
2501/* Code to generate prologue and epilogue sequences */
b9654711
SC
2502
2503void
2504sh_expand_prologue ()
2505{
2506 int live_regs_mask;
40d2032b 2507 int d, i;
1a95a963
JW
2508 int live_regs_mask2;
2509 live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
b9654711 2510
0d7e008e 2511 /* We have pretend args if we had an object sent partially in registers
8aa2a305 2512 and partially on the stack, e.g. a large structure. */
46d81ffa 2513 output_stack_adjust (-current_function_pretend_args_size, stack_pointer_rtx);
b9654711 2514
40d2032b
JW
2515 extra_push = 0;
2516
2517 /* This is set by SETUP_VARARGS to indicate that this is a varargs
1a95a963 2518 routine. Clear it here so that the next function isn't affected. */
b9654711
SC
2519 if (current_function_anonymous_args)
2520 {
40d2032b
JW
2521 current_function_anonymous_args = 0;
2522
1a95a963
JW
2523 /* This is not used by the SH3E calling convention */
2524 if (!TARGET_SH3E)
2525 {
2526 /* Push arg regs as if they'd been provided by caller in stack. */
2527 for (i = 0; i < NPARM_REGS(SImode); i++)
2528 {
2529 int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
2530 if (i > (NPARM_REGS(SImode)
2531 - current_function_args_info.arg_count[(int) SH_ARG_INT]
2532 - current_function_varargs))
2533 break;
2534 push (rn);
2535 extra_push += 4;
2536 }
2537 }
b9654711 2538 }
1a95a963
JW
2539
2540 push_regs (live_regs_mask, live_regs_mask2);
2541
46d81ffa 2542 output_stack_adjust (-get_frame_size (), stack_pointer_rtx);
b9654711
SC
2543
2544 if (frame_pointer_needed)
8aa2a305 2545 emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
b9654711
SC
2546}
2547
2548void
2549sh_expand_epilogue ()
2550{
2551 int live_regs_mask;
40d2032b 2552 int d, i;
b9654711 2553
1a95a963
JW
2554 int live_regs_mask2;
2555 live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
16bea517 2556
b9654711 2557 if (frame_pointer_needed)
46d81ffa
RK
2558 {
2559 /* We deliberately make the add dependent on the frame_pointer,
2560 to ensure that instruction scheduling won't move the stack pointer
2561 adjust before instructions reading from the frame. This can fail
2562 if there is an interrupt which then writes to the stack. */
2563 output_stack_adjust (get_frame_size (), frame_pointer_rtx);
2564 emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2565 }
2566 else
2567 output_stack_adjust (get_frame_size (), stack_pointer_rtx);
b9654711 2568
16bea517 2569 /* Pop all the registers. */
0d7e008e 2570
b9654711
SC
2571 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2572 {
2573 int j = (FIRST_PSEUDO_REGISTER - 1) - i;
1a95a963
JW
2574 if (j < 32 && (live_regs_mask & (1 << j)))
2575 pop (j);
2576 else if (j >= 32 && (live_regs_mask2 & (1 << (j - 32))))
8aa2a305 2577 pop (j);
b9654711 2578 }
b9654711 2579
46d81ffa
RK
2580 output_stack_adjust (extra_push + current_function_pretend_args_size,
2581 stack_pointer_rtx);
b9654711
SC
2582}
2583
8aa2a305
JW
2584/* Clear variables at function end. */
2585
2586void
2587function_epilogue (stream, size)
2588 FILE *stream;
2589 int size;
2590{
956a5206 2591 pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
8aa2a305
JW
2592}
2593
1a95a963
JW
2594rtx
2595sh_builtin_saveregs (arglist)
2596 tree arglist;
2597{
2598 tree fntype = TREE_TYPE (current_function_decl);
2599 /* First unnamed integer register. */
2600 int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
2601 /* Number of integer registers we need to save. */
2602 int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
2603 /* First unnamed SFmode float reg */
2604 int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
2605 /* Number of SFmode float regs to save. */
2606 int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
2607 int ptrsize = GET_MODE_SIZE (Pmode);
2608 rtx valist, regbuf, fpregs;
2609 int bufsize, regno;
2610
2611 /* Allocate block of memory for the regs. */
2612 /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
2613 Or can assign_stack_local accept a 0 SIZE argument? */
2614 bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
2615
2616 regbuf = assign_stack_local (BLKmode, bufsize, 0);
2617 MEM_IN_STRUCT_P (regbuf) = 1;
2618
2619 /* Save int args.
2620 This is optimized to only save the regs that are necessary. Explicitly
2621 named args need not be saved. */
2622 if (n_intregs > 0)
2623 move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
2624 gen_rtx (MEM, BLKmode,
2625 plus_constant (XEXP (regbuf, 0),
2626 n_floatregs * UNITS_PER_WORD)),
2627 n_intregs, n_intregs * UNITS_PER_WORD);
2628
2629 /* Save float args.
2630 This is optimized to only save the regs that are necessary. Explicitly
2631 named args need not be saved.
2632 We explicitly build a pointer to the buffer because it halves the insn
2633 count when not optimizing (otherwise the pointer is built for each reg
2634 saved). */
2635
2636 fpregs = gen_reg_rtx (Pmode);
2637 emit_move_insn (fpregs, XEXP (regbuf, 0));
2638 for (regno = first_floatreg; regno < NPARM_REGS (SFmode); regno ++)
2639 emit_move_insn (gen_rtx (MEM, SFmode,
2640 plus_constant (fpregs,
2641 GET_MODE_SIZE (SFmode)
2642 * (regno - first_floatreg))),
2643 gen_rtx (REG, SFmode,
2644 BASE_ARG_REG (SFmode) + regno));
2645
2646 /* Return the address of the regbuf. */
2647 return XEXP (regbuf, 0);
2648}
2649
0d7e008e
SC
2650/* Define the offset between two registers, one to be eliminated, and
2651 the other its replacement, at the start of a routine. */
2652
2653int
2654initial_elimination_offset (from, to)
8e87e161
SC
2655 int from;
2656 int to;
0d7e008e
SC
2657{
2658 int regs_saved;
0d7e008e
SC
2659 int total_saved_regs_space;
2660 int total_auto_space = get_frame_size ();
8e87e161 2661
2afeea0f
JW
2662 int live_regs_mask, live_regs_mask2;
2663 live_regs_mask = calc_live_regs (&regs_saved, &live_regs_mask2);
1a95a963 2664
0d7e008e 2665 total_saved_regs_space = (regs_saved) * 4;
b9654711 2666
0d7e008e 2667 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
8aa2a305
JW
2668 return total_saved_regs_space + total_auto_space;
2669
0d7e008e 2670 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
8aa2a305
JW
2671 return total_saved_regs_space + total_auto_space;
2672
2673 /* Initial gap between fp and sp is 0. */
0d7e008e 2674 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
8aa2a305
JW
2675 return 0;
2676
2afeea0f
JW
2677 if (from == RETURN_ADDRESS_POINTER_REGNUM
2678 && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
2679 {
2680 int i, n = 0;
2681 for (i = PR_REG+1; i < 32; i++)
2682 if (live_regs_mask & (1 << i))
2683 n += 4;
2684 for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
2685 if (live_regs_mask2 & (1 << (i - 32)))
2686 n += 4;
2687 return n + total_auto_space;
2688 }
2689
0d7e008e
SC
2690 abort ();
2691}
8aa2a305 2692\f
0d7e008e 2693/* Handle machine specific pragmas to be semi-compatible with Hitachi
16bea517 2694 compiler. */
b9654711
SC
2695
2696int
119d0c36 2697handle_pragma (file, t)
0d7e008e 2698 FILE *file;
119d0c36 2699 tree t;
b9654711 2700{
119d0c36
JW
2701 int retval = 0;
2702 register char *pname;
b9654711 2703
119d0c36
JW
2704 if (TREE_CODE (t) != IDENTIFIER_NODE)
2705 return 0;
05a81fe5 2706
119d0c36
JW
2707 pname = IDENTIFIER_POINTER (t);
2708 if (strcmp (pname, "interrupt") == 0)
2709 pragma_interrupt = retval = 1;
2710 else if (strcmp (pname, "trapa") == 0)
2711 pragma_interrupt = pragma_trapa = retval = 1;
2712 else if (strcmp (pname, "nosave_low_regs") == 0)
2713 pragma_nosave_low_regs = retval = 1;
05a81fe5 2714
119d0c36 2715 return retval;
0d7e008e
SC
2716}
2717\f
8aa2a305 2718/* Predicates used by the templates. */
0d7e008e 2719
8aa2a305
JW
2720/* Returns 1 if OP is MACL, MACH or PR. The input must be a REG rtx.
2721 Used only in general_movsrc_operand. */
0d7e008e 2722
8aa2a305
JW
2723int
2724system_reg_operand (op, mode)
2725 rtx op;
2726 enum machine_mode mode;
0d7e008e 2727{
8aa2a305 2728 switch (REGNO (op))
0d7e008e 2729 {
8aa2a305
JW
2730 case PR_REG:
2731 case MACL_REG:
2732 case MACH_REG:
2733 return 1;
0d7e008e 2734 }
8aa2a305 2735 return 0;
0d7e008e 2736}
0d7e008e
SC
2737
2738/* Returns 1 if OP can be source of a simple move operation.
2739 Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
16bea517 2740 invalid as are subregs of system registers. */
0d7e008e
SC
2741
2742int
2743general_movsrc_operand (op, mode)
2744 rtx op;
2745 enum machine_mode mode;
2746{
06c386ea
SC
2747 if (GET_CODE (op) == MEM)
2748 {
2749 rtx inside = XEXP (op, 0);
2750 if (GET_CODE (inside) == CONST)
2751 inside = XEXP (inside, 0);
0d7e008e 2752
06c386ea
SC
2753 if (GET_CODE (inside) == LABEL_REF)
2754 return 1;
8e87e161 2755
06c386ea 2756 if (GET_CODE (inside) == PLUS
8aa2a305
JW
2757 && GET_CODE (XEXP (inside, 0)) == LABEL_REF
2758 && GET_CODE (XEXP (inside, 1)) == CONST_INT)
06c386ea 2759 return 1;
16bea517
JW
2760
2761 /* Only post inc allowed. */
97f8690b 2762 if (GET_CODE (inside) == PRE_DEC)
06c386ea 2763 return 0;
06c386ea 2764 }
0d7e008e
SC
2765
2766 if ((mode == QImode || mode == HImode)
2767 && (GET_CODE (op) == SUBREG
2768 && GET_CODE (XEXP (op, 0)) == REG
2769 && system_reg_operand (XEXP (op, 0), mode)))
2770 return 0;
2771
0d7e008e 2772 return general_operand (op, mode);
b9654711
SC
2773}
2774
0d7e008e
SC
2775/* Returns 1 if OP can be a destination of a move.
2776 Same as general_operand, but no preinc allowed. */
2777
b9654711 2778int
0d7e008e
SC
2779general_movdst_operand (op, mode)
2780 rtx op;
2781 enum machine_mode mode;
b9654711 2782{
16bea517 2783 /* Only pre dec allowed. */
97f8690b 2784 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
0d7e008e 2785 return 0;
d3ae8277 2786
0d7e008e
SC
2787 return general_operand (op, mode);
2788}
2789
0d7e008e
SC
2790/* Returns 1 if OP is a normal arithmetic register. */
2791
2792int
2793arith_reg_operand (op, mode)
2794 rtx op;
2795 enum machine_mode mode;
2796{
2797 if (register_operand (op, mode))
2798 {
519164a9
JW
2799 int regno;
2800
0d7e008e 2801 if (GET_CODE (op) == REG)
519164a9
JW
2802 regno = REGNO (op);
2803 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
2804 regno = REGNO (SUBREG_REG (op));
2805 else
2806 return 1;
2807
2808 return (regno != T_REG && regno != PR_REG && regno != FPUL_REG
2809 && regno != MACH_REG && regno != MACL_REG);
0d7e008e
SC
2810 }
2811 return 0;
2812}
2813
0d7e008e
SC
2814/* Returns 1 if OP is a valid source operand for an arithmetic insn. */
2815
2816int
2817arith_operand (op, mode)
2818 rtx op;
2819 enum machine_mode mode;
2820{
2821 if (arith_reg_operand (op, mode))
2822 return 1;
2823
8aa2a305
JW
2824 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
2825 return 1;
2826
0d7e008e
SC
2827 return 0;
2828}
2829
22e1ebf1
JW
2830/* Returns 1 if OP is a valid source operand for a compare insn. */
2831
2832int
2833arith_reg_or_0_operand (op, mode)
2834 rtx op;
2835 enum machine_mode mode;
2836{
2837 if (arith_reg_operand (op, mode))
2838 return 1;
2839
8aa2a305
JW
2840 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
2841 return 1;
2842
22e1ebf1
JW
2843 return 0;
2844}
2845
16bea517 2846/* Returns 1 if OP is a valid source operand for a logical operation. */
0d7e008e
SC
2847
2848int
2849logical_operand (op, mode)
2850 rtx op;
2851 enum machine_mode mode;
2852{
2853 if (arith_reg_operand (op, mode))
2854 return 1;
2855
8aa2a305
JW
2856 if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
2857 return 1;
2858
0d7e008e 2859 return 0;
b9654711 2860}
d3ae8277 2861
1a95a963
JW
2862/* Nonzero if OP is a floating point value with value 0.0. */
2863
2864int
2865fp_zero_operand (op)
2866 rtx op;
d3ae8277 2867{
1a95a963 2868 REAL_VALUE_TYPE r;
d3ae8277 2869
1a95a963
JW
2870 if (GET_MODE (op) != SFmode)
2871 return 0;
2872
2873 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
e4fa6b06 2874 return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
d3ae8277
SC
2875}
2876
1a95a963 2877/* Nonzero if OP is a floating point value with value 1.0. */
d3ae8277
SC
2878
2879int
1a95a963
JW
2880fp_one_operand (op)
2881 rtx op;
d3ae8277 2882{
1a95a963
JW
2883 REAL_VALUE_TYPE r;
2884
2885 if (GET_MODE (op) != SFmode)
2886 return 0;
2887
2888 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2889 return REAL_VALUES_EQUAL (r, dconst1);
d3ae8277 2890}
a55e9d2b
RK
2891\f
2892/* Return non-zero if REG is not used after INSN.
2893 We assume REG is a reload reg, and therefore does
8b760293 2894 not live past labels. It may live past calls or jumps though. */
a55e9d2b
RK
2895int
2896reg_unused_after (reg, insn)
2897 rtx reg;
2898 rtx insn;
2899{
8783b15e 2900 enum rtx_code code;
a55e9d2b
RK
2901 rtx set;
2902
2903 /* If the reg is set by this instruction, then it is safe for our
2904 case. Disregard the case where this is a store to memory, since
2905 we are checking a register used in the store address. */
2906 set = single_set (insn);
2907 if (set && GET_CODE (SET_DEST (set)) != MEM
2908 && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2909 return 1;
2910
2911 while (insn = NEXT_INSN (insn))
2912 {
a55e9d2b 2913 code = GET_CODE (insn);
8783b15e 2914
c8f6f18d
RK
2915#if 0
2916 /* If this is a label that existed before reload, then the register
2917 if dead here. However, if this is a label added by reorg, then
2918 the register may still be live here. We can't tell the difference,
2919 so we just ignore labels completely. */
8783b15e 2920 if (code == CODE_LABEL)
a55e9d2b 2921 return 1;
c8f6f18d
RK
2922 /* else */
2923#endif
a55e9d2b 2924
8b760293
JW
2925 if (code == JUMP_INSN)
2926 return 0;
2927
8783b15e
RK
2928 /* If this is a sequence, we must handle them all at once.
2929 We could have for instance a call that sets the target register,
2930 and a insn in a delay slot that uses the register. In this case,
2931 we must return 0. */
8b760293 2932 else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
a55e9d2b 2933 {
8783b15e
RK
2934 int i;
2935 int retval = 0;
2936
2937 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2938 {
2939 rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
2940 rtx set = single_set (this_insn);
2941
2942 if (GET_CODE (this_insn) == CALL_INSN)
2943 code = CALL_INSN;
38f35781
JW
2944 else if (GET_CODE (this_insn) == JUMP_INSN)
2945 {
2946 if (INSN_ANNULLED_BRANCH_P (this_insn))
2947 return 0;
2948 code = JUMP_INSN;
2949 }
a55e9d2b 2950
8783b15e
RK
2951 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2952 return 0;
2953 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2954 {
2955 if (GET_CODE (SET_DEST (set)) != MEM)
2956 retval = 1;
2957 else
2958 return 0;
2959 }
2960 if (set == 0
2961 && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
2962 return 0;
2963 }
2964 if (retval == 1)
2965 return 1;
38f35781
JW
2966 else if (code == JUMP_INSN)
2967 return 0;
8783b15e
RK
2968 }
2969 else if (GET_RTX_CLASS (code) == 'i')
a55e9d2b
RK
2970 {
2971 rtx set = single_set (insn);
2972
2973 if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2974 return 0;
2975 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2976 return GET_CODE (SET_DEST (set)) != MEM;
2977 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
2978 return 0;
2979 }
8783b15e
RK
2980
2981 if (code == CALL_INSN && call_used_regs[REGNO (reg)])
2982 return 1;
a55e9d2b
RK
2983 }
2984 return 1;
2985}
This page took 0.609333 seconds and 5 git commands to generate.