OSDN Git Service

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