OSDN Git Service

* (gen_shl_and, in case 1): Fix comparison with mask.
[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       gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, 0));
926       break;
927     case ASHIFT:
928       emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
929       break;
930     }
931 }
932
933 /* Output RTL to split a constant shift into its component SH constant
934    shift instructions.  */
935    
936 int
937 gen_shifty_op (code, operands)
938      int code;
939      rtx *operands;
940 {
941   int value = INTVAL (operands[2]);
942   int max, i;
943
944   /* Truncate the shift count in case it is out of bounds.  */
945   value = value & 0x1f;
946  
947   if (value == 31)
948     {
949       if (code == LSHIFTRT)
950         {
951           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
952           emit_insn (gen_movt (operands[0]));
953           return;
954         }
955       else if (code == ASHIFT)
956         {
957           /* There is a two instruction sequence for 31 bit left shifts,
958              but it requires r0.  */
959           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
960             {
961               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
962               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
963               return;
964             }
965         }
966     }
967   else if (value == 0)
968     {
969       /* This can happen when not optimizing.  We must output something here
970          to prevent the compiler from aborting in final.c after the try_split
971          call.  */
972       emit_insn (gen_nop ());
973       return;
974     }
975
976   max = shift_insns[value];
977   for (i = 0; i < max; i++)
978     gen_ashift (code, shift_amounts[value][i], operands[0]);
979 }
980    
981 /* Same as above, but optimized for values where the topmost bits don't
982    matter.  */
983
984 int
985 gen_shifty_hi_op (code, operands)
986      int code;
987      rtx *operands;
988 {
989   int value = INTVAL (operands[2]);
990   int max, i;
991   void (*gen_fun)();
992
993   /* This operation is used by and_shl for SImode values with a few
994      high bits known to be cleared.  */
995   value &= 31;
996   if (value == 0)
997     {
998       emit_insn (gen_nop ());
999       return;
1000     }
1001
1002   gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
1003   if (code == ASHIFT)
1004     {
1005       max = ext_shift_insns[value];
1006       for (i = 0; i < max; i++)
1007         gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1008     }
1009   else
1010     /* When shifting right, emit the shifts in reverse order, so that
1011        solitary negative values come first.  */
1012     for (i = ext_shift_insns[value] - 1; i >= 0; i--)
1013       gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1014 }
1015
1016 /* Output RTL for an arithmetic right shift.  */
1017
1018 /* ??? Rewrite to use super-optimizer sequences.  */
1019
1020 int
1021 expand_ashiftrt (operands)
1022      rtx *operands;
1023 {
1024   rtx wrk;
1025   char func[18];
1026   tree func_name;
1027   int value;
1028
1029   if (TARGET_SH3)
1030     {
1031       if (GET_CODE (operands[2]) != CONST_INT)
1032         {
1033           rtx count = copy_to_mode_reg (SImode, operands[2]);
1034           emit_insn (gen_negsi2 (count, count));
1035           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1036           return 1;
1037         }
1038       else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
1039                > 1 + SH_DYNAMIC_SHIFT_COST)
1040         {
1041           rtx count
1042             = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
1043           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1044           return 1;
1045         }
1046     }
1047   if (GET_CODE (operands[2]) != CONST_INT)
1048     return 0;
1049
1050   value = INTVAL (operands[2]) & 31;
1051
1052   if (value == 31)
1053     {
1054       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1055       return 1;
1056     }
1057   else if (value >= 16 && value <= 19)
1058     {
1059       wrk = gen_reg_rtx (SImode);
1060       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1061       value -= 16;
1062       while (value--)
1063         gen_ashift (ASHIFTRT, 1, wrk);
1064       emit_move_insn (operands[0], wrk);
1065       return 1;
1066     }
1067   /* Expand a short sequence inline, longer call a magic routine.  */
1068   else if (value <= 5)
1069     {
1070       wrk = gen_reg_rtx (SImode);
1071       emit_move_insn (wrk, operands[1]);
1072       while (value--)
1073         gen_ashift (ASHIFTRT, 1, wrk);
1074       emit_move_insn (operands[0], wrk);
1075       return 1;
1076     }
1077
1078   wrk = gen_reg_rtx (Pmode);
1079
1080   /* Load the value into an arg reg and call a helper.  */
1081   emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
1082   sprintf (func, "__ashiftrt_r4_%d", value);
1083   func_name = get_identifier (func);
1084   emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
1085                                 IDENTIFIER_POINTER (func_name)));
1086   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1087   emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
1088   return 1;
1089 }
1090
1091 int sh_dynamicalize_shift_p (count)
1092      rtx count;
1093 {
1094   return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
1095 }
1096
1097 /* Try to find a good way to implement the combiner pattern
1098   [(set (match_operand:SI 0 "register_operand" "r")
1099         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1100                            (match_operand:SI 2 "const_int_operand" "n"))
1101                 (match_operand:SI 3 "const_int_operand" "n"))) .
1102   LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1103   return 0 for simple right / left or left/right shift combination.
1104   return 1 for a combination of shifts with zero_extend.
1105   return 2 for a combination of shifts with an AND that needs r0.
1106   return 3 for a combination of shifts with an AND that needs an extra
1107     scratch register, when the three highmost bits of the AND mask are clear.
1108   return 4 for a combination of shifts with an AND that needs an extra
1109     scratch register, when any of the three highmost bits of the AND mask
1110     is set.
1111   If ATTRP is set, store an initial right shift width in ATTRP[0],
1112   and the instruction length in ATTRP[1] .  These values are not valid
1113   when returning 0.
1114   When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1115   shift_amounts for the last shift value that is to be used before the
1116   sign extend.  */
1117 int
1118 shl_and_kind (left_rtx, mask_rtx, attrp)
1119      rtx left_rtx, mask_rtx;
1120      int *attrp;
1121 {
1122   unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1123   int left = INTVAL (left_rtx), right;
1124   int best = 0;
1125   int cost, best_cost = 10000;
1126   int best_right = 0, best_len = 0;
1127   int i;
1128   int can_ext;
1129
1130   if (left < 0 || left > 31)
1131     return 0;
1132   if (GET_CODE (mask_rtx) == CONST_INT)
1133     mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
1134   else
1135     mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
1136   /* Can this be expressed as a right shift / left shift pair ? */
1137   lsb = ((mask ^ (mask - 1)) >> 1) + 1;
1138   right = exact_log2 (lsb);
1139   mask2 = ~(mask + lsb - 1);
1140   lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
1141   /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1142   if (! mask2)
1143     best_cost = shift_insns[right] + shift_insns[right + left];
1144   /* mask has no trailing zeroes <==> ! right */
1145   else if (! right && mask2 == ~(lsb2 - 1))
1146     {
1147       int late_right = exact_log2 (lsb2);
1148       best_cost = shift_insns[left + late_right] + shift_insns[late_right];
1149     }
1150   /* Try to use zero extend */
1151   if (mask2 == ~(lsb2 - 1))
1152     {
1153       int width, first;
1154
1155       for (width = 8; width <= 16; width += 8)
1156         {
1157           /* Can we zero-extend right away? */
1158           if (lsb2 == (HOST_WIDE_INT)1 << width)
1159             {
1160               cost
1161                 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
1162               if (cost < best_cost)
1163                 {
1164                   best = 1;
1165                   best_cost = cost;
1166                   best_right = right;
1167                   best_len = cost;
1168                   if (attrp)
1169                     attrp[2] = -1;
1170                 }
1171               continue;
1172             }
1173           /* ??? Could try to put zero extend into initial right shift,
1174              or even shift a bit left before the right shift. */
1175           /* Determine value of first part of left shift, to get to the
1176              zero extend cut-off point.  */
1177           first = width - exact_log2 (lsb2) + right;
1178           if (first >= 0 && right + left - first >= 0)
1179             {
1180               cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
1181                 + ext_shift_insns[right + left - first];
1182               if (cost < best_cost)
1183                 {
1184                   best = 1;
1185                   best_cost = cost;
1186                   best_right = right;
1187                   best_len = cost;
1188                   if (attrp)
1189                     attrp[2] = first;
1190                   }
1191             }
1192         }
1193     }
1194   /* Try to use r0 AND pattern */
1195   for (i = 0; i <= 2; i++)
1196     {
1197       if (i > right)
1198         break;
1199       if (! CONST_OK_FOR_L (mask >> i))
1200         continue;
1201       cost = (i != 0) + 2 + ext_shift_insns[left + i];
1202       if (cost < best_cost)
1203         {
1204           best = 2;
1205           best_cost = cost;
1206           best_right = i;
1207           best_len = cost - 1;
1208         }
1209     }
1210   /* Try to use a scratch register to hold the AND operand.  */
1211   can_ext = ((mask << left) & 0xe0000000) == 0;
1212   for (i = 0; i <= 2; i++)
1213     {
1214       if (i > right)
1215         break;
1216       cost = (i != 0) + (CONST_OK_FOR_I (mask >> i) ? 2 : 3)
1217         + (can_ext ? ext_shift_insns : shift_insns)[left + i];
1218       if (cost < best_cost)
1219         {
1220           best = 4 - can_ext;
1221           best_cost = cost;
1222           best_right = i;
1223           best_len = cost - 1 - ! CONST_OK_FOR_I (mask >> i);
1224         }
1225     }
1226
1227   if (attrp)
1228     {
1229       attrp[0] = best_right;
1230       attrp[1] = best_len;
1231     }
1232   return best;
1233 }
1234
1235 /* This is used in length attributes of the unnamed instructions
1236    corresponding to shl_and_kind return values of 1 and 2.  */
1237 int
1238 shl_and_length (insn)
1239      rtx insn;
1240 {
1241   rtx set_src, left_rtx, mask_rtx;
1242   int attributes[3];
1243
1244   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1245   left_rtx = XEXP (XEXP (set_src, 0), 1);
1246   mask_rtx = XEXP (set_src, 1);
1247   shl_and_kind (left_rtx, mask_rtx, attributes);
1248   return attributes[1];
1249 }
1250
1251 /* This is used in length attribute of the and_shl_scratch instruction.  */
1252
1253 int
1254 shl_and_scr_length (insn)
1255      rtx insn;
1256 {
1257   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1258   int len = shift_insns[INTVAL (XEXP (set_src, 1))];
1259   rtx op = XEXP (set_src, 0);
1260   len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
1261   op = XEXP (XEXP (op, 0), 0);
1262   return len + shift_insns[INTVAL (XEXP (op, 1))];
1263 }
1264
1265 /* Generating rtl? */
1266 extern int rtx_equal_function_value_matters;
1267
1268 /* Generate rtl for instructions for which shl_and_kind advised a particular
1269    method of generating them, i.e. returned zero.  */
1270
1271 int
1272 gen_shl_and (dest, left_rtx, mask_rtx, source)
1273      rtx dest, left_rtx, mask_rtx, source;
1274 {
1275   int attributes[3];
1276   unsigned HOST_WIDE_INT mask;
1277   int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
1278   int right, total_shift;
1279   int (*shift_gen_fun) PROTO((int, rtx*)) = gen_shifty_hi_op;
1280
1281   right = attributes[0];
1282   total_shift = INTVAL (left_rtx) + right;
1283   mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
1284   switch (kind)
1285     {
1286     default:
1287       return -1;
1288     case 1:
1289       {
1290         int first = attributes[2];
1291         rtx operands[3];
1292
1293         if (first < 0)
1294           {
1295             emit_insn ((mask << right) <= 0xff
1296                        ? gen_zero_extendqisi2(dest,
1297                                               gen_lowpart (QImode, source))
1298                        : gen_zero_extendhisi2(dest,
1299                                               gen_lowpart (HImode, source)));
1300             source = dest;
1301           }
1302         if (source != dest)
1303           emit_insn (gen_movsi (dest, source));
1304         operands[0] = dest;
1305         if (right)
1306           {
1307             operands[2] = GEN_INT (right);
1308             gen_shifty_hi_op (LSHIFTRT, operands);
1309           }
1310         if (first > 0)
1311           {
1312             operands[2] = GEN_INT (first);
1313             gen_shifty_hi_op (ASHIFT, operands);
1314             total_shift -= first;
1315             mask <<= first;
1316           }
1317         if (first >= 0)
1318           emit_insn (mask <= 0xff
1319                      ? gen_zero_extendqisi2(dest, gen_lowpart (QImode, dest))
1320                      : gen_zero_extendhisi2(dest, gen_lowpart (HImode, dest)));
1321         if (total_shift > 0)
1322           {
1323             operands[2] = GEN_INT (total_shift);
1324             gen_shifty_hi_op (ASHIFT, operands);
1325           }
1326         break;
1327       }
1328     case 4:
1329       shift_gen_fun = gen_shifty_op;
1330     case 2:
1331     case 3:
1332       /* If the topmost bit that matters is set, set the topmost bits
1333          that don't matter.  This way, we might be able to get a shorter
1334          signed constant.  */
1335       if (mask & ((HOST_WIDE_INT)1 << 31 - total_shift))
1336         mask |= (HOST_WIDE_INT)~0 << (31 - total_shift);
1337       /* Don't expand fine-grained when combining, because that will
1338          make the pattern fail.  */
1339       if (rtx_equal_function_value_matters
1340           || reload_in_progress || reload_completed)
1341         {
1342           rtx operands[3];
1343   
1344           if (right)
1345             {
1346               emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
1347               source = dest;
1348             }
1349           emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
1350           if (total_shift)
1351             {
1352               operands[0] = dest;
1353               operands[1] = dest;
1354               operands[2] = GEN_INT (total_shift);
1355               shift_gen_fun (ASHIFT, operands);
1356             }
1357           break;
1358         }
1359       else
1360         {
1361           int neg = 0;
1362           if (kind != 4 && total_shift < 16)
1363             {
1364               neg = -ext_shift_amounts[total_shift][1];
1365               if (neg > 0)
1366                 neg -= ext_shift_amounts[total_shift][2];
1367               else
1368                 neg = 0;
1369             }
1370           emit_insn (gen_and_shl_scratch (dest, source,
1371                                           GEN_INT (right),
1372                                           GEN_INT (mask),
1373                                           GEN_INT (total_shift + neg),
1374                                           GEN_INT (neg)));
1375           emit_insn (gen_movsi (dest, dest));
1376           break;
1377         }
1378     }
1379   return 0;
1380 }
1381
1382 /* Try to find a good way to implement the combiner pattern
1383   [(set (match_operand:SI 0 "register_operand" "=r")
1384         (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1385                                     (match_operand:SI 2 "const_int_operand" "n")
1386                          (match_operand:SI 3 "const_int_operand" "n")
1387                          (const_int 0)))
1388    (clobber (reg:SI 18))]
1389   LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1390   return 0 for simple left / right shift combination.
1391   return 1 for left shift / 8 bit sign extend / left shift.
1392   return 2 for left shift / 16 bit sign extend / left shift.
1393   return 3 for left shift / 8 bit sign extend / shift / sign extend.
1394   return 4 for left shift / 16 bit sign extend / shift / sign extend.
1395   return 5 for left shift / 16 bit sign extend / right shift
1396   return 6 for < 8 bit sign extend / left shift.
1397   return 7 for < 8 bit sign extend / left shift / single right shift.
1398   If COSTP is nonzero, assign the calculated cost to *COSTP.  */
1399
1400 int
1401 shl_sext_kind (left_rtx, size_rtx, costp)
1402      rtx left_rtx, size_rtx;
1403      int *costp;
1404 {
1405   int left, size, insize, ext;
1406   int cost, best_cost;
1407   int kind;
1408
1409   left = INTVAL (left_rtx);
1410   size = INTVAL (size_rtx);
1411   insize = size - left;
1412   if (insize <= 0)
1413     abort ();
1414   /* Default to left / right shift.  */
1415   kind = 0;
1416   best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
1417   if (size <= 16)
1418     {
1419       /* 16 bit shift / sign extend / 16 bit shift */
1420       cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
1421       /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
1422          below, by alternative 3 or something even better.  */
1423       if (cost < best_cost)
1424         {
1425           kind = 5;
1426           best_cost = cost;
1427         }
1428     }
1429   /* Try a plain sign extend between two shifts.  */
1430   for (ext = 16; ext >= insize; ext -= 8)
1431     {
1432       if (ext <= size)
1433         {
1434           cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
1435           if (cost < best_cost)
1436             {
1437               kind = ext / 8U;
1438               best_cost = cost;
1439             }
1440         }
1441       /* Check if we can do a sloppy shift with a final signed shift
1442          restoring the sign.  */
1443       if (EXT_SHIFT_SIGNED (size - ext))
1444         cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
1445       /* If not, maybe it's still cheaper to do the second shift sloppy,
1446          and do a final sign extend?  */
1447       else if (size <= 16)
1448         cost = ext_shift_insns[ext - insize] + 1
1449           + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
1450       else
1451         continue;
1452       if (cost < best_cost)
1453         {
1454           kind = ext / 8U + 2;
1455           best_cost = cost;
1456         }
1457     }
1458   /* Check if we can sign extend in r0 */
1459   if (insize < 8)
1460     {
1461       cost = 3 + shift_insns[left];
1462       if (cost < best_cost)
1463         {
1464           kind = 6;
1465           best_cost = cost;
1466         }
1467       /* Try the same with a final signed shift.  */
1468       if (left < 31)
1469         {
1470           cost = 3 + ext_shift_insns[left + 1] + 1;
1471           if (cost < best_cost)
1472             {
1473               kind = 7;
1474               best_cost = cost;
1475             }
1476         }
1477     }
1478   if (TARGET_SH3)
1479     {
1480       /* Try to use a dynamic shift.  */
1481       cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
1482       if (cost < best_cost)
1483         {
1484           kind = 0;
1485           best_cost = cost;
1486         }
1487     }
1488   if (costp)
1489     *costp = cost;
1490   return kind;
1491 }
1492
1493 /* Function to be used in the length attribute of the instructions
1494    implementing this pattern.  */
1495
1496 int
1497 shl_sext_length (insn)
1498      rtx insn;
1499 {
1500   rtx set_src, left_rtx, size_rtx;
1501   int cost;
1502
1503   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1504   left_rtx = XEXP (XEXP (set_src, 0), 1);
1505   size_rtx = XEXP (set_src, 1);
1506   shl_sext_kind (left_rtx, size_rtx, &cost);
1507   return cost;
1508 }
1509
1510 /* Generate rtl for this pattern */
1511
1512 int
1513 gen_shl_sext (dest, left_rtx, size_rtx, source)
1514      rtx dest, left_rtx, size_rtx, source;
1515 {
1516   int kind;
1517   int left, size, insize, cost;
1518   rtx operands[3];
1519
1520   kind = shl_sext_kind (left_rtx, size_rtx, &cost);
1521   left = INTVAL (left_rtx);
1522   size = INTVAL (size_rtx);
1523   insize = size - left;
1524   switch (kind)
1525     {
1526     case 1:
1527     case 2:
1528     case 3:
1529     case 4:
1530       {
1531         int ext = kind & 1 ? 8 : 16;
1532         int shift2 = size - ext;
1533
1534         /* Don't expand fine-grained when combining, because that will
1535            make the pattern fail.  */
1536         if (! rtx_equal_function_value_matters
1537             && ! reload_in_progress && ! reload_completed)
1538           {
1539             emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1540             emit_insn (gen_movsi (dest, source));
1541             break;
1542           }
1543         if (dest != source)
1544           emit_insn (gen_movsi (dest, source));
1545         operands[0] = dest;
1546         if (ext - insize)
1547           {
1548             operands[2] = GEN_INT (ext - insize);
1549             gen_shifty_hi_op (ASHIFT, operands);
1550           }
1551         emit_insn (kind & 1
1552                    ? gen_extendqisi2(dest, gen_lowpart (QImode, dest))
1553                    : gen_extendhisi2(dest, gen_lowpart (HImode, dest)));
1554         if (kind <= 2)
1555           {
1556             if (shift2)
1557               {
1558                 operands[2] = GEN_INT (shift2);
1559                 gen_shifty_op (ASHIFT, operands);
1560               }
1561           }
1562         else
1563           {
1564             if (shift2 > 0)
1565               {
1566                 if (EXT_SHIFT_SIGNED (shift2))
1567                   {
1568                     operands[2] = GEN_INT (shift2 + 1);
1569                     gen_shifty_op (ASHIFT, operands);
1570                     operands[2] = GEN_INT (1);
1571                     gen_shifty_op (ASHIFTRT, operands);
1572                     break;
1573                   }
1574                 operands[2] = GEN_INT (shift2);
1575                 gen_shifty_hi_op (ASHIFT, operands);
1576               }
1577             else if (shift2)
1578               {
1579                 operands[2] = GEN_INT (-shift2);
1580                 gen_shifty_hi_op (LSHIFTRT, operands);
1581               }
1582             emit_insn (size <= 8
1583                        ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
1584                        : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
1585           }
1586         break;
1587       }
1588     case 5:
1589       {
1590         int i = 16 - size;
1591         emit_insn (gen_shl_sext_ext (dest, source, GEN_INT (16 - insize),
1592                                      GEN_INT (16)));
1593         /* Don't use gen_ashrsi3 because it generates new pseudos.  */
1594         while (--i >= 0)
1595           gen_ashift (ASHIFTRT, 1, dest);
1596         break;
1597       }
1598     case 6:
1599     case 7:
1600       /* Don't expand fine-grained when combining, because that will
1601          make the pattern fail.  */
1602       if (! rtx_equal_function_value_matters
1603           && ! reload_in_progress && ! reload_completed)
1604         {
1605           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
1606           emit_insn (gen_movsi (dest, source));
1607           break;
1608         }
1609       emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
1610       emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
1611       emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
1612       operands[0] = dest;
1613       operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
1614       gen_shifty_op (ASHIFT, operands);
1615       if (kind == 7)
1616         emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
1617       break;
1618     default:
1619       return -1;
1620     }
1621   return 0;
1622 }
1623 \f
1624 /* The SH cannot load a large constant into a register, constants have to
1625    come from a pc relative load.  The reference of a pc relative load
1626    instruction must be less than 1k infront of the instruction.  This
1627    means that we often have to dump a constant inside a function, and
1628    generate code to branch around it.
1629
1630    It is important to minimize this, since the branches will slow things
1631    down and make things bigger.
1632
1633    Worst case code looks like:
1634
1635    mov.l L1,rn
1636    bra   L2
1637    nop
1638    align
1639    L1:   .long value
1640    L2:
1641    ..
1642
1643    mov.l L3,rn
1644    bra   L4
1645    nop
1646    align
1647    L3:   .long value
1648    L4:
1649    ..
1650
1651    We fix this by performing a scan before scheduling, which notices which
1652    instructions need to have their operands fetched from the constant table
1653    and builds the table.
1654
1655    The algorithm is:
1656
1657    scan, find an instruction which needs a pcrel move.  Look forward, find the
1658    last barrier which is within MAX_COUNT bytes of the requirement.
1659    If there isn't one, make one.  Process all the instructions between
1660    the find and the barrier.
1661
1662    In the above example, we can tell that L3 is within 1k of L1, so
1663    the first move can be shrunk from the 3 insn+constant sequence into
1664    just 1 insn, and the constant moved to L3 to make:
1665
1666    mov.l        L1,rn
1667    ..
1668    mov.l        L3,rn
1669    bra          L4
1670    nop
1671    align
1672    L3:.long value
1673    L4:.long value
1674
1675    Then the second move becomes the target for the shortening process.  */
1676
1677 typedef struct
1678 {
1679   rtx value;                    /* Value in table.  */
1680   rtx label;                    /* Label of value.  */
1681   enum machine_mode mode;       /* Mode of value.  */
1682 } pool_node;
1683
1684 /* The maximum number of constants that can fit into one pool, since
1685    the pc relative range is 0...1020 bytes and constants are at least 4
1686    bytes long.  */
1687
1688 #define MAX_POOL_SIZE (1020/4)
1689 static pool_node pool_vector[MAX_POOL_SIZE];
1690 static int pool_size;
1691
1692 /* ??? If we need a constant in HImode which is the truncated value of a
1693    constant we need in SImode, we could combine the two entries thus saving
1694    two bytes.  Is this common enough to be worth the effort of implementing
1695    it?  */
1696
1697 /* ??? This stuff should be done at the same time that we shorten branches.
1698    As it is now, we must assume that all branches are the maximum size, and
1699    this causes us to almost always output constant pools sooner than
1700    necessary.  */
1701
1702 /* Add a constant to the pool and return its label.  */
1703
1704 static rtx
1705 add_constant (x, mode)
1706      rtx x;
1707      enum machine_mode mode;
1708 {
1709   int i;
1710   rtx lab;
1711
1712   /* First see if we've already got it.  */
1713   for (i = 0; i < pool_size; i++)
1714     {
1715       if (x->code == pool_vector[i].value->code
1716           && mode == pool_vector[i].mode)
1717         {
1718           if (x->code == CODE_LABEL)
1719             {
1720               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1721                 continue;
1722             }
1723           if (rtx_equal_p (x, pool_vector[i].value))
1724             return pool_vector[i].label;
1725         }
1726     }
1727
1728   /* Need a new one.  */
1729   pool_vector[pool_size].value = x;
1730   lab = gen_label_rtx ();
1731   pool_vector[pool_size].mode = mode;
1732   pool_vector[pool_size].label = lab;
1733   pool_size++;
1734   return lab;
1735 }
1736
1737 /* Output the literal table.  */
1738
1739 static void
1740 dump_table (scan)
1741      rtx scan;
1742 {
1743   int i;
1744   int need_align = 1;
1745
1746   /* Do two passes, first time dump out the HI sized constants.  */
1747
1748   for (i = 0; i < pool_size; i++)
1749     {
1750       pool_node *p = &pool_vector[i];
1751
1752       if (p->mode == HImode)
1753         {
1754           if (need_align)
1755             {
1756               scan = emit_insn_after (gen_align_2 (), scan);
1757               need_align = 0;
1758             }
1759           scan = emit_label_after (p->label, scan);
1760           scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1761         }
1762     }
1763
1764   need_align = 1;
1765
1766   for (i = 0; i < pool_size; i++)
1767     {
1768       pool_node *p = &pool_vector[i];
1769
1770       switch (p->mode)
1771         {
1772         case HImode:
1773           break;
1774         case SImode:
1775         case SFmode:
1776           if (need_align)
1777             {
1778               need_align = 0;
1779               scan = emit_label_after (gen_label_rtx (), scan);
1780               scan = emit_insn_after (gen_align_4 (), scan);
1781             }
1782           if (p->label)
1783             scan = emit_label_after (p->label, scan);
1784           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1785           break;
1786         case DFmode:
1787         case DImode:
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_8 (p->value), scan);
1797           break;
1798         default:
1799           abort ();
1800           break;
1801         }
1802     }
1803
1804   scan = emit_insn_after (gen_consttable_end (), scan);
1805   scan = emit_barrier_after (scan);
1806   pool_size = 0;
1807 }
1808
1809 /* Return non-zero if constant would be an ok source for a
1810    mov.w instead of a mov.l.  */
1811
1812 static int
1813 hi_const (src)
1814      rtx src;
1815 {
1816   return (GET_CODE (src) == CONST_INT
1817           && INTVAL (src) >= -32768
1818           && INTVAL (src) <= 32767);
1819 }
1820
1821 /* Non-zero if the insn is a move instruction which needs to be fixed.  */
1822
1823 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1824    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
1825    need to fix it if the input value is CONST_OK_FOR_I.  */
1826
1827 static int
1828 broken_move (insn)
1829      rtx insn;
1830 {
1831   if (GET_CODE (insn) == INSN)
1832     {
1833       rtx pat = PATTERN (insn);
1834       if (GET_CODE (pat) == PARALLEL)
1835         pat = XVECEXP (pat, 0, 0);
1836       if (GET_CODE (pat) == SET
1837           /* We can load any 8 bit value if we don't care what the high
1838              order bits end up as.  */
1839           && GET_MODE (SET_DEST (pat)) != QImode
1840           && CONSTANT_P (SET_SRC (pat))
1841           && ! (TARGET_SH3E
1842                 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
1843                 && (fp_zero_operand (SET_SRC (pat))
1844                     || fp_one_operand (SET_SRC (pat)))
1845                 && GET_CODE (SET_DEST (pat)) == REG
1846                 && REGNO (SET_DEST (pat)) >= FIRST_FP_REG
1847                 && REGNO (SET_DEST (pat)) <= LAST_FP_REG)
1848           && (GET_CODE (SET_SRC (pat)) != CONST_INT
1849               || ! CONST_OK_FOR_I (INTVAL (SET_SRC (pat)))))
1850         return 1;
1851     }
1852
1853   return 0;
1854 }
1855
1856 static int
1857 mova_p (insn)
1858      rtx insn;
1859 {
1860   return (GET_CODE (insn) == INSN
1861           && GET_CODE (PATTERN (insn)) == SET
1862           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
1863           && XINT (SET_SRC (PATTERN (insn)), 1) == 1);
1864 }
1865
1866 /* Find the last barrier from insn FROM which is close enough to hold the
1867    constant pool.  If we can't find one, then create one near the end of
1868    the range.  */
1869
1870 static rtx
1871 find_barrier (num_mova, mova, from)
1872      int num_mova;
1873      rtx mova, from;
1874 {
1875   int count_si = 0;
1876   int count_hi = 0;
1877   int found_hi = 0;
1878   int found_si = 0;
1879   int hi_align = 2;
1880   int si_align = 2;
1881   int leading_mova = num_mova;
1882   rtx barrier_before_mova, found_barrier = 0, good_barrier = 0;
1883   int si_limit;
1884   int hi_limit;
1885
1886   /* For HImode: range is 510, add 4 because pc counts from address of
1887      second instruction after this one, subtract 2 for the jump instruction
1888      that we may need to emit before the table, subtract 2 for the instruction
1889      that fills the jump delay slot (in very rare cases, reorg will take an
1890      instruction from after the constant pool or will leave the delay slot
1891      empty).  This gives 510.
1892      For SImode: range is 1020, add 4 because pc counts from address of
1893      second instruction after this one, subtract 2 in case pc is 2 byte
1894      aligned, subtract 2 for the jump instruction that we may need to emit
1895      before the table, subtract 2 for the instruction that fills the jump
1896      delay slot.  This gives 1018.  */
1897
1898   /* The branch will always be shortened now that the reference address for
1899      forward branches is the successor address, thus we need no longer make
1900      adjustments to the [sh]i_limit for -O0.  */
1901
1902   si_limit = 1018;
1903   hi_limit = 510;
1904
1905   while (from && count_si < si_limit && count_hi < hi_limit)
1906     {
1907       int inc = get_attr_length (from);
1908       int new_align = 1;
1909
1910       if (GET_CODE (from) == CODE_LABEL)
1911         {
1912           if (optimize)
1913             new_align = 1 << label_to_alignment (from);
1914           else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
1915             new_align = 1 << barrier_align (from);
1916           else
1917             new_align = 1;
1918           inc = 0;
1919         }
1920
1921       if (GET_CODE (from) == BARRIER)
1922         {
1923
1924           found_barrier = from;
1925
1926           /* If we are at the end of the function, or in front of an alignment
1927              instruction, we need not insert an extra alignment.  We prefer
1928              this kind of barrier.  */
1929           if (barrier_align (from) > 2)
1930             good_barrier = from;
1931         }
1932
1933       if (broken_move (from))
1934         {
1935           rtx pat, src, dst;
1936           enum machine_mode mode;
1937
1938           pat = PATTERN (from);
1939           if (GET_CODE (pat) == PARALLEL)
1940             pat = XVECEXP (pat, 0, 0);
1941           src = SET_SRC (pat);
1942           dst = SET_DEST (pat);
1943           mode = GET_MODE (dst);
1944
1945           /* We must explicitly check the mode, because sometimes the
1946              front end will generate code to load unsigned constants into
1947              HImode targets without properly sign extending them.  */
1948           if (mode == HImode || (mode == SImode && hi_const (src)))
1949             {
1950               found_hi += 2;
1951               /* We put the short constants before the long constants, so
1952                  we must count the length of short constants in the range
1953                  for the long constants.  */
1954               /* ??? This isn't optimal, but is easy to do.  */
1955               si_limit -= 2;
1956             }
1957           else
1958             {
1959               while (si_align > 2 && found_si + si_align - 2 > count_si)
1960                 si_align >>= 1;
1961               if (found_si > count_si)
1962                 count_si = found_si;
1963               found_si += GET_MODE_SIZE (mode);
1964               if (num_mova)
1965                 si_limit -= GET_MODE_SIZE (mode);
1966             }
1967         }
1968
1969       if (mova_p (from))
1970         {
1971           if (! num_mova++)
1972             {
1973               leading_mova = 0;
1974               mova = from;
1975               barrier_before_mova = good_barrier ? good_barrier : found_barrier;
1976             }
1977           if (found_si > count_si)
1978             count_si = found_si;
1979         }
1980       else if (GET_CODE (from) == JUMP_INSN
1981                && (GET_CODE (PATTERN (from)) == ADDR_VEC
1982                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1983         {
1984           if (num_mova)
1985             num_mova--;
1986           if (barrier_align (next_real_insn (from)) == CACHE_LOG)
1987             {
1988               /* We have just passed the barrier in front front of the
1989                  ADDR_DIFF_VEC, which is stored in found_barrier.  Since
1990                  the ADDR_DIFF_VEC is accessed as data, just like our pool
1991                  constants, this is a good opportunity to accommodate what
1992                  we have gathered so far.
1993                  If we waited any longer, we could end up at a barrier in
1994                  front of code, which gives worse cache usage for separated
1995                  instruction / data caches.  */
1996               good_barrier = found_barrier;
1997               break;
1998             }
1999           else
2000             {
2001               rtx body = PATTERN (from);
2002               inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
2003             }
2004         }
2005
2006       if (found_si)
2007         {
2008           if (new_align > si_align)
2009             {
2010               si_limit -= count_si - 1 & new_align - si_align;
2011               si_align = new_align;
2012             }
2013           count_si = count_si + new_align - 1 & -new_align;
2014           count_si += inc;
2015         }
2016       if (found_hi)
2017         {
2018           if (new_align > hi_align)
2019             {
2020               hi_limit -= count_hi - 1 & new_align - hi_align;
2021               hi_align = new_align;
2022             }
2023           count_hi = count_hi + new_align - 1 & -new_align;
2024           count_hi += inc;
2025         }
2026       from = NEXT_INSN (from);
2027     }
2028
2029   if (num_mova)
2030     if (leading_mova)
2031       {
2032         /* Try as we might, the leading mova is out of range.  Change
2033            it into a load (which will become a pcload) and retry.  */
2034         SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2035         INSN_CODE (mova) = -1;
2036         return find_barrier (0, 0, mova);
2037       }
2038     else
2039       {
2040         /* Insert the constant pool table before the mova instruction,
2041            to prevent the mova label reference from going out of range.  */
2042         from = mova;
2043         good_barrier = found_barrier = barrier_before_mova;
2044       }
2045
2046   if (found_barrier)
2047     {
2048       if (good_barrier && next_real_insn (found_barrier))
2049         found_barrier = good_barrier;
2050     }
2051   else
2052     {
2053       /* We didn't find a barrier in time to dump our stuff,
2054          so we'll make one.  */
2055       rtx label = gen_label_rtx ();
2056
2057       /* If we exceeded the range, then we must back up over the last
2058          instruction we looked at.  Otherwise, we just need to undo the
2059          NEXT_INSN at the end of the loop.  */
2060       if (count_hi > hi_limit || count_si > si_limit)
2061         from = PREV_INSN (PREV_INSN (from));
2062       else
2063         from = PREV_INSN (from);
2064
2065       /* Walk back to be just before any jump or label.
2066          Putting it before a label reduces the number of times the branch
2067          around the constant pool table will be hit.  Putting it before
2068          a jump makes it more likely that the bra delay slot will be
2069          filled.  */
2070       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
2071              || GET_CODE (from) == CODE_LABEL)
2072         from = PREV_INSN (from);
2073
2074       from = emit_jump_insn_after (gen_jump (label), from);
2075       JUMP_LABEL (from) = label;
2076       LABEL_NUSES (label) = 1;
2077       found_barrier = emit_barrier_after (from);
2078       emit_label_after (label, found_barrier);
2079     }
2080
2081   return found_barrier;
2082 }
2083
2084 /* If the instruction INSN is implemented by a special function, and we can
2085    positively find the register that is used to call the sfunc, and this
2086    register is not used anywhere else in this instruction - except as the
2087    destination of a set, return this register; else, return 0.  */
2088 rtx
2089 sfunc_uses_reg (insn)
2090      rtx insn;
2091 {
2092   int i;
2093   rtx pattern, part, reg_part, reg;
2094
2095   if (GET_CODE (insn) != INSN)
2096     return 0;
2097   pattern = PATTERN (insn);
2098   if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
2099     return 0;
2100
2101   for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2102     {
2103       part = XVECEXP (pattern, 0, i);
2104       if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
2105         reg_part = part;
2106     }
2107   if (! reg_part)
2108     return 0;
2109   reg = XEXP (reg_part, 0);
2110   for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2111     {
2112       part = XVECEXP (pattern, 0, i);
2113       if (part == reg_part)
2114         continue;
2115       if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
2116                                   && GET_CODE (SET_DEST (part)) == REG)
2117                                  ? SET_SRC (part) : part)))
2118         return 0;
2119     }
2120   return reg;
2121 }
2122
2123 /* See if the only way in which INSN uses REG is by calling it, or by
2124    setting it while calling it.  Set *SET to a SET rtx if the register
2125    is set by INSN.  */
2126
2127 static int
2128 noncall_uses_reg (reg, insn, set)
2129      rtx reg;
2130      rtx insn;
2131      rtx *set;
2132 {
2133   rtx pattern, reg2;
2134
2135   *set = NULL_RTX;
2136
2137   reg2 = sfunc_uses_reg (insn);
2138   if (reg2 && REGNO (reg2) == REGNO (reg))
2139     {
2140       pattern = single_set (insn);
2141       if (pattern
2142           && GET_CODE (SET_DEST (pattern)) == REG
2143           && REGNO (reg) == REGNO (SET_DEST (pattern)))
2144         *set = pattern;
2145       return 0;
2146     }
2147   if (GET_CODE (insn) != CALL_INSN)
2148     {
2149       /* We don't use rtx_equal_p because we don't care if the mode is
2150          different.  */
2151       pattern = single_set (insn);
2152       if (pattern
2153           && GET_CODE (SET_DEST (pattern)) == REG
2154           && REGNO (reg) == REGNO (SET_DEST (pattern)))
2155         {
2156           rtx par, part;
2157           int i;
2158
2159           *set = pattern;
2160           par = PATTERN (insn);
2161           if (GET_CODE (par) == PARALLEL)
2162             for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
2163               {
2164                 part = XVECEXP (par, 0, i);
2165                 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
2166                   return 1;
2167               }
2168           return reg_mentioned_p (reg, SET_SRC (pattern));
2169         }
2170
2171       return 1;
2172     }
2173
2174   pattern = PATTERN (insn);
2175
2176   if (GET_CODE (pattern) == PARALLEL)
2177     {
2178       int i;
2179
2180       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2181         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
2182           return 1;
2183       pattern = XVECEXP (pattern, 0, 0);
2184     }
2185
2186   if (GET_CODE (pattern) == SET)
2187     {
2188       if (reg_mentioned_p (reg, SET_DEST (pattern)))
2189         {
2190           /* We don't use rtx_equal_p, because we don't care if the
2191              mode is different.  */
2192           if (GET_CODE (SET_DEST (pattern)) != REG
2193               || REGNO (reg) != REGNO (SET_DEST (pattern)))
2194             return 1;
2195
2196           *set = pattern;
2197         }
2198
2199       pattern = SET_SRC (pattern);
2200     }
2201
2202   if (GET_CODE (pattern) != CALL
2203       || GET_CODE (XEXP (pattern, 0)) != MEM
2204       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
2205     return 1;
2206
2207   return 0;
2208 }
2209
2210 /* Given a X, a pattern of an insn or a part of it, return a mask of used
2211    general registers.  Bits 0..15 mean that the respective registers
2212    are used as inputs in the instruction.  Bits 16..31 mean that the
2213    registers 0..15, respectively, are used as outputs, or are clobbered.
2214    IS_DEST should be set to 16 if X is the destination of a SET, else to 0.  */
2215 int
2216 regs_used (x, is_dest)
2217      rtx x; int is_dest;
2218 {
2219   enum rtx_code code;
2220   char *fmt;
2221   int i, used = 0;
2222
2223   if (! x)
2224     return used;
2225   code = GET_CODE (x);
2226   switch (code)
2227     {
2228     case REG:
2229       if (REGNO (x) < 16)
2230         return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2231                 << (REGNO (x) + is_dest));
2232       return 0;
2233     case SUBREG:
2234       {
2235         rtx y = SUBREG_REG (x);
2236      
2237         if (GET_CODE (y) != REG)
2238           break;
2239         if (REGNO (y) < 16)
2240           return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2241                   << (REGNO (y) + SUBREG_WORD (x) + is_dest));
2242         return 0;
2243       }
2244     case SET:
2245       return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
2246     case RETURN:
2247       /* If there was a return value, it must have been indicated with USE.  */
2248       return 0x00ffff00;
2249     case CLOBBER:
2250       is_dest = 1;
2251       break;
2252     case MEM:
2253       is_dest = 0;
2254       break;
2255     case CALL:
2256       used |= 0x00ff00f0;
2257       break;
2258     }
2259
2260   fmt = GET_RTX_FORMAT (code);
2261
2262   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2263     {
2264       if (fmt[i] == 'E')
2265         {
2266           register int j;
2267           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2268             used |= regs_used (XVECEXP (x, i, j), is_dest);
2269         }
2270       else if (fmt[i] == 'e')
2271         used |= regs_used (XEXP (x, i), is_dest);
2272     }
2273   return used;
2274 }
2275
2276 /* Create an instruction that prevents redirection of a conditional branch
2277    to the destination of the JUMP with address ADDR.
2278    If the branch needs to be implemented as an indirect jump, try to find
2279    a scratch register for it.
2280    If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
2281    If any preceding insn that doesn't fit into a delay slot is good enough,
2282    pass 1.  Pass 2 if a definite blocking insn is needed.
2283    -1 is used internally to avoid deep recursion.
2284    If a blocking instruction is made or recognized, return it.  */
2285    
2286 static rtx
2287 gen_block_redirect (jump, addr, need_block)
2288      rtx jump;
2289      int addr, need_block;
2290 {
2291   int dead = 0;
2292   rtx prev = prev_nonnote_insn (jump);
2293   rtx dest;
2294
2295   /* First, check if we already have an instruction that satisfies our need.  */
2296   if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
2297     {
2298       if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
2299         return prev;
2300       if (GET_CODE (PATTERN (prev)) == USE
2301           || GET_CODE (PATTERN (prev)) == CLOBBER
2302           || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
2303         prev = jump;
2304       else if ((need_block &= ~1) < 0)
2305         return prev;
2306       else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
2307         need_block = 0;
2308     }
2309   /* We can't use JUMP_LABEL here because it might be undefined
2310      when not optimizing.  */
2311   dest = XEXP (SET_SRC (PATTERN (jump)), 0);
2312   /* If the branch is out of range, try to find a scratch register for it.  */
2313   if (optimize
2314       && (insn_addresses[INSN_UID (dest)] - addr + 4092U > 4092 + 4098))
2315     {
2316       rtx scan;
2317       /* Don't look for the stack pointer as a scratch register,
2318          it would cause trouble if an interrupt occurred.  */
2319       unsigned try = 0x7fff, used;
2320       int jump_left = flag_expensive_optimizations + 1;
2321     
2322       /* It is likely that the most recent eligible instruction is wanted for
2323          the delay slot.  Therefore, find out which registers it uses, and
2324          try to avoid using them.  */
2325          
2326       for (scan = jump; scan = PREV_INSN (scan); )
2327         {
2328           enum rtx_code code;
2329
2330           if (INSN_DELETED_P (scan))
2331             continue;
2332           code = GET_CODE (scan);
2333           if (code == CODE_LABEL || code == JUMP_INSN)
2334             break;
2335           if (code == INSN
2336               && GET_CODE (PATTERN (scan)) != USE
2337               && GET_CODE (PATTERN (scan)) != CLOBBER
2338               && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
2339             {
2340               try &= ~regs_used (PATTERN (scan), 0);
2341               break;
2342             }
2343         }
2344       for (used = dead = 0, scan = JUMP_LABEL (jump); scan = NEXT_INSN (scan); )
2345         {
2346           enum rtx_code code;
2347
2348           if (INSN_DELETED_P (scan))
2349             continue;
2350           code = GET_CODE (scan);
2351           if (GET_RTX_CLASS (code) == 'i')
2352             {
2353               used |= regs_used (PATTERN (scan), 0);
2354               if (code == CALL_INSN)
2355                 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
2356               dead |= (used >> 16) & ~used;
2357               if (dead & try)
2358                 {
2359                   dead &= try;
2360                   break;
2361                 }
2362               if (code == JUMP_INSN)
2363                 if (jump_left-- && simplejump_p (scan))
2364                   scan = JUMP_LABEL (scan);
2365                 else
2366                   break;
2367             }
2368         }
2369       /* Mask out the stack pointer again, in case it was
2370          the only 'free' register we have found.  */
2371       dead &= 0x7fff;
2372     }
2373   /* If the immediate destination is still in range, check for possible
2374      threading with a jump beyond the delay slot insn.
2375      Don't check if we are called recursively; the jump has been or will be
2376      checked in a different invocation then.  */
2377         
2378   else if (optimize && need_block >= 0)
2379     {
2380       rtx next = next_active_insn (next_active_insn (dest));
2381       if (next && GET_CODE (next) == JUMP_INSN
2382           && GET_CODE (PATTERN (next)) == SET
2383           && recog_memoized (next) == CODE_FOR_jump)
2384         {
2385           dest = JUMP_LABEL (next);
2386           if (dest
2387               && insn_addresses[INSN_UID (dest)] - addr + 4092U > 4092 + 4098)
2388             gen_block_redirect (next, insn_addresses[INSN_UID (next)], -1);
2389         }
2390     }
2391
2392   if (dead)
2393     {
2394       rtx reg = gen_rtx (REG, SImode, exact_log2 (dead & -dead));
2395
2396       /* It would be nice if we could convert the jump into an indirect
2397          jump / far branch right now, and thus exposing all constituent
2398          instructions to further optimization.  However, reorg uses
2399          simplejump_p to determine if there is an unconditional jump where
2400          it should try to schedule instructions from the target of the
2401          branch; simplejump_p fails for indirect jumps even if they have
2402          a JUMP_LABEL.  */
2403       rtx insn = emit_insn_before (gen_indirect_jump_scratch
2404                                    (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
2405                                    , jump);
2406       INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
2407       return insn;
2408     }
2409   else if (need_block)
2410     /* We can't use JUMP_LABEL here because it might be undefined
2411        when not optimizing.  */
2412     return emit_insn_before (gen_block_branch_redirect
2413                       (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
2414                       , jump);
2415   return prev;
2416 }
2417
2418 #define CONDJUMP_MIN -252
2419 #define CONDJUMP_MAX 262
2420 struct far_branch
2421 {
2422   /* A label (to be placed) in front of the jump
2423      that jumps to our ultimate destination.  */
2424   rtx near_label;
2425   /* Where we are going to insert it if we cannot move the jump any farther,
2426      or the jump itself if we have picked up an existing jump.  */
2427   rtx insert_place;
2428   /* The ultimate destination.  */
2429   rtx far_label;
2430   struct far_branch *prev;
2431   /* If the branch has already been created, its address;
2432      else the address of its first prospective user.  */
2433   int address;
2434 };
2435
2436 enum mdep_reorg_phase_e mdep_reorg_phase;
2437 void
2438 gen_far_branch (bp)
2439      struct far_branch *bp;
2440 {
2441   rtx insn = bp->insert_place;
2442   rtx jump;
2443   rtx label = gen_label_rtx ();
2444
2445   emit_label_after (label, insn);
2446   if (bp->far_label)
2447     {
2448       jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
2449       LABEL_NUSES (bp->far_label)++;
2450     }
2451   else
2452     jump = emit_jump_insn_after (gen_return (), insn);
2453   emit_label_after (bp->near_label, insn);
2454   JUMP_LABEL (jump) = bp->far_label;
2455   if (! invert_jump (insn, label))
2456     abort ();
2457   /* Prevent reorg from undoing our splits.  */
2458   gen_block_redirect (jump, bp->address += 2, 2);
2459 }
2460
2461 /* Fix up ADDR_DIFF_VECs.  */
2462 void
2463 fixup_addr_diff_vecs (first)
2464      rtx first;
2465 {
2466   rtx insn;
2467
2468   for (insn = first; insn; insn = NEXT_INSN (insn))
2469     {
2470       rtx vec_lab, pat, prev, prevpat, x;
2471
2472       if (GET_CODE (insn) != JUMP_INSN
2473           || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
2474         continue;
2475       pat = PATTERN (insn);
2476       vec_lab = XEXP (XEXP (pat, 0), 0);
2477
2478       /* Search the matching casesi_jump_2.  */
2479       for (prev = vec_lab; ; prev = PREV_INSN (prev))
2480         {
2481           if (GET_CODE (prev) != JUMP_INSN)
2482             continue;
2483           prevpat = PATTERN (prev);
2484           if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
2485             continue;
2486           x = XVECEXP (prevpat, 0, 1);
2487           if (GET_CODE (x) != USE)
2488             continue;
2489           x = XEXP (x, 0);
2490           if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
2491             break;
2492         }
2493       /* Fix up the ADDR_DIF_VEC to be relative
2494          to the reference address of the braf.  */
2495       XEXP (XEXP (pat, 0), 0)
2496         = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
2497     }
2498 }
2499
2500 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
2501    a barrier.  Return the base 2 logarithm of the desired alignment.  */
2502 int
2503 barrier_align (barrier_or_label)
2504      rtx barrier_or_label;
2505 {
2506   rtx next = next_real_insn (barrier_or_label), pat, prev;
2507   int slot, credit;
2508  
2509   if (! next)
2510     return 0;
2511
2512   pat = PATTERN (next);
2513
2514   if (GET_CODE (pat) == ADDR_DIFF_VEC)
2515     return 2;
2516
2517   if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == 1)
2518     /* This is a barrier in front of a constant table.  */
2519     return 0;
2520
2521   prev = prev_real_insn (barrier_or_label);
2522   if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
2523     {
2524       pat = PATTERN (prev);
2525       /* If this is a very small table, we want to keep the alignment after
2526          the table to the minimum for proper code alignment.  */
2527       return ((TARGET_SMALLCODE
2528                || (XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
2529                    <= 1 << (CACHE_LOG - 2)))
2530               ? 1 : CACHE_LOG);
2531     }
2532
2533   if (TARGET_SMALLCODE)
2534     return 0;
2535
2536   if (! TARGET_SH3 || ! optimize)
2537     return CACHE_LOG;
2538
2539   /* Check if there is an immediately preceding branch to the insn beyond
2540      the barrier.  We must weight the cost of discarding useful information
2541      from the current cache line when executing this branch and there is
2542      an alignment, against that of fetching unneeded insn in front of the
2543      branch target when there is no alignment.  */
2544
2545   /* PREV is presumed to be the JUMP_INSN for the barrier under
2546      investigation.  Skip to the insn before it.  */
2547   prev = prev_real_insn (prev);
2548
2549   for (slot = 2, credit = 1 << (CACHE_LOG - 2) + 2;
2550        credit >= 0 && prev && GET_CODE (prev) == INSN;
2551        prev = prev_real_insn (prev))
2552     {
2553       if (GET_CODE (PATTERN (prev)) == USE
2554           || GET_CODE (PATTERN (prev)) == CLOBBER)
2555         continue;
2556       if (GET_CODE (PATTERN (prev)) == SEQUENCE)
2557         prev = XVECEXP (PATTERN (prev), 0, 1);
2558       if (slot &&
2559           get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
2560         slot = 0;
2561       credit -= get_attr_length (prev);
2562     }
2563   if (prev
2564       && GET_CODE (prev) == JUMP_INSN
2565       && JUMP_LABEL (prev)
2566       && next_real_insn (JUMP_LABEL (prev)) == next_real_insn (barrier_or_label)
2567       && (credit - slot >= (GET_CODE (SET_SRC (PATTERN (prev))) == PC ? 2 : 0)))
2568     return 0;
2569
2570   return CACHE_LOG;
2571 }
2572
2573 /* Exported to toplev.c.
2574
2575    Do a final pass over the function, just before delayed branch
2576    scheduling.  */
2577
2578 void
2579 machine_dependent_reorg (first)
2580      rtx first;
2581 {
2582   rtx insn, mova;
2583   int num_mova;
2584   rtx r0_rtx = gen_rtx (REG, Pmode, 0);
2585   rtx r0_inc_rtx = gen_rtx (POST_INC, Pmode, r0_rtx);
2586
2587   /* If relaxing, generate pseudo-ops to associate function calls with
2588      the symbols they call.  It does no harm to not generate these
2589      pseudo-ops.  However, when we can generate them, it enables to
2590      linker to potentially relax the jsr to a bsr, and eliminate the
2591      register load and, possibly, the constant pool entry.  */
2592
2593   mdep_reorg_phase = SH_INSERT_USES_LABELS;
2594   if (TARGET_RELAX)
2595     {
2596       /* Remove all REG_LABEL notes.  We want to use them for our own
2597          purposes.  This works because none of the remaining passes
2598          need to look at them.
2599
2600          ??? But it may break in the future.  We should use a machine
2601          dependent REG_NOTE, or some other approach entirely.  */
2602       for (insn = first; insn; insn = NEXT_INSN (insn))
2603         {
2604           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2605             {
2606               rtx note;
2607
2608               while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
2609                 remove_note (insn, note);
2610             }
2611         }
2612
2613       for (insn = first; insn; insn = NEXT_INSN (insn))
2614         {
2615           rtx pattern, reg, link, set, scan, dies, label;
2616           int rescan = 0, foundinsn = 0;
2617
2618           if (GET_CODE (insn) == CALL_INSN)
2619             {
2620               pattern = PATTERN (insn);
2621
2622               if (GET_CODE (pattern) == PARALLEL)
2623                 pattern = XVECEXP (pattern, 0, 0);
2624               if (GET_CODE (pattern) == SET)
2625                 pattern = SET_SRC (pattern);
2626
2627               if (GET_CODE (pattern) != CALL
2628                   || GET_CODE (XEXP (pattern, 0)) != MEM)
2629                 continue;
2630
2631               reg = XEXP (XEXP (pattern, 0), 0);
2632             }
2633           else
2634             {
2635               reg = sfunc_uses_reg (insn);
2636               if (! reg)
2637                 continue;
2638             }
2639
2640           if (GET_CODE (reg) != REG)
2641             continue;
2642
2643           /* This is a function call via REG.  If the only uses of REG
2644              between the time that it is set and the time that it dies
2645              are in function calls, then we can associate all the
2646              function calls with the setting of REG.  */
2647
2648           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
2649             {
2650               if (REG_NOTE_KIND (link) != 0)
2651                 continue;
2652               set = single_set (XEXP (link, 0));
2653               if (set && rtx_equal_p (reg, SET_DEST (set)))
2654                 {
2655                   link = XEXP (link, 0);
2656                   break;
2657                 }
2658             }
2659
2660           if (! link)
2661             {
2662               /* ??? Sometimes global register allocation will have
2663                  deleted the insn pointed to by LOG_LINKS.  Try
2664                  scanning backward to find where the register is set.  */
2665               for (scan = PREV_INSN (insn);
2666                    scan && GET_CODE (scan) != CODE_LABEL;
2667                    scan = PREV_INSN (scan))
2668                 {
2669                   if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2670                     continue;
2671
2672                   if (! reg_mentioned_p (reg, scan))
2673                     continue;
2674
2675                   if (noncall_uses_reg (reg, scan, &set))
2676                     break;
2677
2678                   if (set)
2679                     {
2680                       link = scan;
2681                       break;
2682                     }
2683                 }
2684             }
2685
2686           if (! link)
2687             continue;
2688
2689           /* The register is set at LINK.  */
2690
2691           /* We can only optimize the function call if the register is
2692              being set to a symbol.  In theory, we could sometimes
2693              optimize calls to a constant location, but the assembler
2694              and linker do not support that at present.  */
2695           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
2696               && GET_CODE (SET_SRC (set)) != LABEL_REF)
2697             continue;
2698
2699           /* Scan forward from LINK to the place where REG dies, and
2700              make sure that the only insns which use REG are
2701              themselves function calls.  */
2702
2703           /* ??? This doesn't work for call targets that were allocated
2704              by reload, since there may not be a REG_DEAD note for the
2705              register.  */
2706
2707           dies = NULL_RTX;
2708           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
2709             {
2710               rtx scanset;
2711
2712               /* Don't try to trace forward past a CODE_LABEL if we haven't
2713                  seen INSN yet.  Ordinarily, we will only find the setting insn
2714                  in LOG_LINKS if it is in the same basic block.  However,
2715                  cross-jumping can insert code labels in between the load and
2716                  the call, and can result in situations where a single call
2717                  insn may have two targets depending on where we came from.  */
2718
2719               if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
2720                 break;
2721
2722               if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
2723                 continue;
2724
2725               /* Don't try to trace forward past a JUMP.  To optimize
2726                  safely, we would have to check that all the
2727                  instructions at the jump destination did not use REG.  */
2728
2729               if (GET_CODE (scan) == JUMP_INSN)
2730                 break;
2731
2732               if (! reg_mentioned_p (reg, scan))
2733                 continue;
2734
2735               if (noncall_uses_reg (reg, scan, &scanset))
2736                 break;
2737
2738               if (scan == insn)
2739                 foundinsn = 1;
2740
2741               if (scan != insn
2742                   && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
2743                 {
2744                   /* There is a function call to this register other
2745                      than the one we are checking.  If we optimize
2746                      this call, we need to rescan again below.  */
2747                   rescan = 1;
2748                 }
2749
2750               /* ??? We shouldn't have to worry about SCANSET here.
2751                  We should just be able to check for a REG_DEAD note
2752                  on a function call.  However, the REG_DEAD notes are
2753                  apparently not dependable around libcalls; c-torture
2754                  execute/920501-2 is a test case.  If SCANSET is set,
2755                  then this insn sets the register, so it must have
2756                  died earlier.  Unfortunately, this will only handle
2757                  the cases in which the register is, in fact, set in a
2758                  later insn.  */
2759
2760               /* ??? We shouldn't have to use FOUNDINSN here.
2761                  However, the LOG_LINKS fields are apparently not
2762                  entirely reliable around libcalls;
2763                  newlib/libm/math/e_pow.c is a test case.  Sometimes
2764                  an insn will appear in LOG_LINKS even though it is
2765                  not the most recent insn which sets the register. */
2766
2767               if (foundinsn
2768                   && (scanset
2769                       || find_reg_note (scan, REG_DEAD, reg)))
2770                 {
2771                   dies = scan;
2772                   break;
2773                 }
2774             }
2775
2776           if (! dies)
2777             {
2778               /* Either there was a branch, or some insn used REG
2779                  other than as a function call address.  */
2780               continue;
2781             }
2782
2783           /* Create a code label, and put it in a REG_LABEL note on
2784              the insn which sets the register, and on each call insn
2785              which uses the register.  In final_prescan_insn we look
2786              for the REG_LABEL notes, and output the appropriate label
2787              or pseudo-op.  */
2788
2789           label = gen_label_rtx ();
2790           REG_NOTES (link) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2791                                       REG_NOTES (link));
2792           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
2793                                       REG_NOTES (insn));
2794           if (rescan)
2795             {
2796               scan = link;
2797               do
2798                 {
2799                   rtx reg2;
2800
2801                   scan = NEXT_INSN (scan);
2802                   if (scan != insn
2803                       && ((GET_CODE (scan) == CALL_INSN
2804                            && reg_mentioned_p (reg, scan))
2805                           || ((reg2 = sfunc_uses_reg (scan))
2806                               && REGNO (reg2) == REGNO (reg))))
2807                     REG_NOTES (scan) = gen_rtx (EXPR_LIST, REG_LABEL,
2808                                                 label, REG_NOTES (scan));
2809                 }
2810               while (scan != dies);
2811             }
2812         }
2813     }
2814
2815   if (TARGET_SH2)
2816     fixup_addr_diff_vecs (first);
2817
2818   if (optimize)
2819     {
2820       mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
2821       shorten_branches (first);
2822     }
2823   /* Scan the function looking for move instructions which have to be
2824      changed to pc-relative loads and insert the literal tables.  */
2825
2826   mdep_reorg_phase = SH_FIXUP_PCLOAD;
2827   for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
2828     {
2829       if (mova_p (insn))
2830         {
2831           if (! num_mova++)
2832             mova = insn;
2833         }
2834       else if (GET_CODE (insn) == JUMP_INSN
2835                && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2836                && num_mova)
2837         {
2838           rtx scan;
2839           int total;
2840
2841           num_mova--;
2842
2843           /* Some code might have been inserted between the mova and
2844              its ADDR_DIFF_VEC.  Check if the mova is still in range.  */
2845           for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
2846             total += get_attr_length (scan);
2847
2848           /* range of mova is 1020, add 4 because pc counts from address of
2849              second instruction after this one, subtract 2 in case pc is 2
2850              byte aligned.  Possible alignment needed for the ADDR_DIFF_VEC
2851              cancels out with alignment effects of the mova itself.  */
2852           if (total > 1022)
2853             {
2854               /* Change the mova into a load, and restart scanning
2855                  there.  broken_move will then return true for mova.  */
2856               SET_SRC (PATTERN (mova))
2857                 = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2858               INSN_CODE (mova) = -1;
2859               insn = mova;
2860             }
2861         }
2862       if (broken_move (insn))
2863         {
2864           rtx scan;
2865           /* Scan ahead looking for a barrier to stick the constant table
2866              behind.  */
2867           rtx barrier = find_barrier (num_mova, mova, insn);
2868           rtx last_float_move, last_float = 0, *last_float_addr;
2869
2870           if (num_mova && ! mova_p (mova))
2871             {
2872               /* find_barrier had to change the first mova into a
2873                  pcload; thus, we have to start with this new pcload.  */
2874               insn = mova;
2875               num_mova = 0;
2876             }
2877           /* Now find all the moves between the points and modify them.  */
2878           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
2879             {
2880               if (GET_CODE (scan) == CODE_LABEL)
2881                 last_float = 0;
2882               if (broken_move (scan))
2883                 {
2884                   rtx *patp = &PATTERN (scan), pat = *patp;
2885                   rtx src, dst;
2886                   rtx lab;
2887                   rtx newinsn;
2888                   rtx newsrc;
2889                   enum machine_mode mode;
2890
2891                   if (GET_CODE (pat) == PARALLEL)
2892                     patp = &XVECEXP (pat, 0, 0), pat = *patp;
2893                   src = SET_SRC (pat);
2894                   dst = SET_DEST (pat);
2895                   mode = GET_MODE (dst);
2896
2897                   if (mode == SImode && hi_const (src))
2898                     {
2899                       int offset = 0;
2900
2901                       mode = HImode;
2902                       while (GET_CODE (dst) == SUBREG)
2903                         {
2904                           offset += SUBREG_WORD (dst);
2905                           dst = SUBREG_REG (dst);
2906                         }
2907                       dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
2908                     }
2909
2910                   if (GET_CODE (dst) == REG
2911                       && ((REGNO (dst) >= FIRST_FP_REG
2912                            && REGNO (dst) <= LAST_FP_REG)
2913                           || REGNO (dst) == FPUL_REG))
2914                     {
2915                       if (last_float
2916                           && reg_set_between_p (r0_rtx, last_float_move, scan))
2917                         last_float = 0;
2918                       lab = add_constant (src, mode, last_float);
2919                       if (lab)
2920                         emit_insn_before (gen_mova (lab), scan);
2921                       else
2922                         *last_float_addr = r0_inc_rtx;
2923                       last_float_move = scan;
2924                       last_float = src;
2925                       newsrc = gen_rtx (MEM, mode,
2926                                         (REGNO (dst) == FPUL_REG
2927                                          ? r0_inc_rtx
2928                                          : r0_rtx));
2929                       last_float_addr = &XEXP (newsrc, 0);
2930                     }
2931                   else
2932                     {
2933                       lab = add_constant (src, mode, 0);
2934                       newsrc = gen_rtx (MEM, mode,
2935                                         gen_rtx (LABEL_REF, VOIDmode, lab));
2936                     }
2937                   RTX_UNCHANGING_P (newsrc) = 1;
2938                   *patp = gen_rtx (SET, VOIDmode, dst, newsrc);
2939                   INSN_CODE (scan) = -1;
2940                 }
2941             }
2942           dump_table (barrier);
2943           insn = barrier;
2944         }
2945     }
2946
2947   mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
2948   insn_addresses = 0;
2949   split_branches (first);
2950
2951   /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
2952      also has an effect on the register that holds the addres of the sfunc.
2953      Insert an extra dummy insn in front of each sfunc that pretends to
2954      use this register.  */
2955   if (flag_delayed_branch)
2956     {
2957       for (insn = first; insn; insn = NEXT_INSN (insn))
2958         {
2959           rtx reg = sfunc_uses_reg (insn);
2960
2961           if (! reg)
2962             continue;
2963           emit_insn_before (gen_use_sfunc_addr (reg), insn);
2964         }
2965     }
2966   mdep_reorg_phase = SH_AFTER_MDEP_REORG;
2967 }
2968
2969 int
2970 get_dest_uid (label, max_uid)
2971      rtx label;
2972      int max_uid;
2973 {
2974   rtx dest = next_real_insn (label);
2975   int dest_uid;
2976   if (! dest)
2977     /* This can happen for an undefined label.  */
2978     return 0;
2979   dest_uid = INSN_UID (dest);
2980   /* If this is a newly created branch redirection blocking instruction,
2981      we cannot index the branch_uid or insn_addresses arrays with its
2982      uid.  But then, we won't need to, because the actual destination is
2983      the following branch.  */
2984   while (dest_uid >= max_uid)
2985     {
2986       dest = NEXT_INSN (dest);
2987       dest_uid = INSN_UID (dest);
2988     }
2989   if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
2990     return 0;
2991   return dest_uid;
2992 }
2993
2994 /* Split condbranches that are out of range.  Also add clobbers for
2995    scratch registers that are needed in far jumps.
2996    We do this before delay slot scheduling, so that it can take our
2997    newly created instructions into account.  It also allows us to
2998    find branches with common targets more easily.  */
2999
3000 static void
3001 split_branches (first)
3002      rtx first;
3003 {
3004   rtx insn;
3005   struct far_branch **uid_branch, *far_branch_list = 0;
3006   int max_uid = get_max_uid ();
3007
3008   /* Find out which branches are out of range.  */
3009   shorten_branches (first);
3010
3011   uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
3012   bzero ((char *) uid_branch, max_uid * sizeof *uid_branch);
3013
3014   for (insn = first; insn; insn = NEXT_INSN (insn))
3015     if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3016       continue;
3017     else if (INSN_DELETED_P (insn))
3018       {
3019         /* Shorten_branches would split this instruction again,
3020            so transform it into a note.  */
3021         PUT_CODE (insn, NOTE);
3022         NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3023         NOTE_SOURCE_FILE (insn) = 0;
3024       }
3025     else if (GET_CODE (insn) == JUMP_INSN
3026              /* Don't mess with ADDR_DIFF_VEC */
3027              && (GET_CODE (PATTERN (insn)) == SET
3028                  || GET_CODE (PATTERN (insn)) == RETURN))
3029       {
3030         enum attr_type type = get_attr_type (insn);
3031         if (type == TYPE_CBRANCH)
3032           {
3033             rtx next, beyond;
3034     
3035             if (get_attr_length (insn) > 4)
3036               {
3037                 rtx src = SET_SRC (PATTERN (insn));
3038                 rtx cond = XEXP (src, 0);
3039                 rtx olabel = XEXP (XEXP (src, 1), 0);
3040                 rtx jump;
3041                 int addr = insn_addresses[INSN_UID (insn)];
3042                 rtx label = 0;
3043                 int dest_uid = get_dest_uid (olabel, max_uid);
3044                 struct far_branch *bp = uid_branch[dest_uid];
3045     
3046                 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
3047                    the label if the LABEL_NUSES count drops to zero.  There is
3048                    always a jump_optimize pass that sets these values, but it
3049                    proceeds to delete unreferenced code, and then if not
3050                    optimizing, to un-delete the deleted instructions, thus
3051                    leaving labels with too low uses counts.  */
3052                 if (! optimize)
3053                   {
3054                     JUMP_LABEL (insn) = olabel;
3055                     LABEL_NUSES (olabel)++;
3056                   }
3057                 if (! bp)
3058                   {
3059                     bp = (struct far_branch *) alloca (sizeof *bp);
3060                     uid_branch[dest_uid] = bp;
3061                     bp->prev = far_branch_list;
3062                     far_branch_list = bp;
3063                     bp->far_label
3064                       = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
3065                     LABEL_NUSES (bp->far_label)++;
3066                   }
3067                 else
3068                   {
3069                     label = bp->near_label;
3070                     if (! label && bp->address - addr >= CONDJUMP_MIN)
3071                       {
3072                         rtx block = bp->insert_place;
3073
3074                         if (GET_CODE (PATTERN (block)) == RETURN)
3075                           block = PREV_INSN (block);
3076                         else
3077                           block = gen_block_redirect (block,
3078                                                       bp->address, 2);
3079                         label = emit_label_after (gen_label_rtx (),
3080                                                   PREV_INSN (block));
3081                         bp->near_label = label;
3082                       }
3083                     else if (label && ! NEXT_INSN (label))
3084                       if (addr + 2 - bp->address <= CONDJUMP_MAX)
3085                         bp->insert_place = insn;
3086                       else
3087                         gen_far_branch (bp);
3088                   }
3089                 if (! label
3090                     || NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN)
3091                   {
3092                     bp->near_label = label = gen_label_rtx ();
3093                     bp->insert_place = insn;
3094                     bp->address = addr;
3095                   }
3096                 if (! redirect_jump (insn, label))
3097                   abort ();
3098               }
3099             else
3100               {
3101                 /* get_attr_length (insn) == 2 */
3102                 /* Check if we have a pattern where reorg wants to redirect
3103                    the branch to a label from an unconditional branch that
3104                    is too far away.  */
3105                 /* We can't use JUMP_LABEL here because it might be undefined
3106                    when not optimizing.  */
3107                 /* A syntax error might cause beyond to be NULL_RTX.  */
3108                 beyond
3109                   = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
3110                                             0));
3111         
3112                 if (beyond
3113                     && (GET_CODE (beyond) == JUMP_INSN
3114                         || (GET_CODE (beyond = next_active_insn (beyond))
3115                             == JUMP_INSN))
3116                     && GET_CODE (PATTERN (beyond)) == SET
3117                     && recog_memoized (beyond) == CODE_FOR_jump
3118                     && ((insn_addresses[INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0))]
3119                          - insn_addresses[INSN_UID (insn)] + 252U)
3120                         > 252 + 258 + 2))
3121                   gen_block_redirect (beyond,
3122                                       insn_addresses[INSN_UID (beyond)], 1);
3123               }
3124     
3125             next = next_active_insn (insn);
3126
3127             if ((GET_CODE (next) == JUMP_INSN
3128                  || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
3129                 && GET_CODE (PATTERN (next)) == SET
3130                 && recog_memoized (next) == CODE_FOR_jump
3131                 && ((insn_addresses[INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0))]
3132                      - insn_addresses[INSN_UID (insn)] + 252U)
3133                     > 252 + 258 + 2))
3134               gen_block_redirect (next, insn_addresses[INSN_UID (next)], 1);
3135           }
3136         else if (type == TYPE_JUMP || type == TYPE_RETURN)
3137           {
3138             int addr = insn_addresses[INSN_UID (insn)];
3139             rtx far_label = 0;
3140             int dest_uid = 0;
3141             struct far_branch *bp;
3142
3143             if (type == TYPE_JUMP)
3144               {
3145                 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
3146                 dest_uid = get_dest_uid (far_label, max_uid);
3147                 if (! dest_uid)
3148                   {
3149                     /* Parse errors can lead to labels outside
3150                       the insn stream.  */
3151                     if (! NEXT_INSN (far_label))
3152                       continue;
3153
3154                     if (! optimize)
3155                       {
3156                         JUMP_LABEL (insn) = far_label;
3157                         LABEL_NUSES (far_label)++;
3158                       }
3159                     redirect_jump (insn, NULL_RTX);
3160                     far_label = 0;
3161                   }
3162               }
3163             bp = uid_branch[dest_uid];
3164             if (! bp)
3165               {
3166                 bp = (struct far_branch *) alloca (sizeof *bp);
3167                 uid_branch[dest_uid] = bp;
3168                 bp->prev = far_branch_list;
3169                 far_branch_list = bp;
3170                 bp->near_label = 0;
3171                 bp->far_label = far_label;
3172                 if (far_label)
3173                   LABEL_NUSES (far_label)++;
3174               }
3175             else if (bp->near_label && ! NEXT_INSN (bp->near_label))
3176               if (addr - bp->address <= CONDJUMP_MAX)
3177                 emit_label_after (bp->near_label, PREV_INSN (insn));
3178               else
3179                 {
3180                   gen_far_branch (bp);
3181                   bp->near_label = 0;
3182                 }
3183             else
3184               bp->near_label = 0;
3185             bp->address = addr;
3186             bp->insert_place = insn;
3187             if (! far_label)
3188               emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
3189             else
3190               gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
3191           }
3192       }
3193   /* Generate all pending far branches,
3194      and free our references to the far labels.  */
3195   while (far_branch_list)
3196     {
3197       if (far_branch_list->near_label
3198           && ! NEXT_INSN (far_branch_list->near_label))
3199         gen_far_branch (far_branch_list);
3200       if (optimize
3201           && far_branch_list->far_label
3202           && ! --LABEL_NUSES (far_branch_list->far_label))
3203         delete_insn (far_branch_list->far_label);
3204       far_branch_list = far_branch_list->prev;
3205     }
3206
3207   /* Instruction length information is no longer valid due to the new
3208      instructions that have been generated.  */
3209   init_insn_lengths ();
3210 }
3211
3212 /* Dump out instruction addresses, which is useful for debugging the
3213    constant pool table stuff.
3214
3215    If relaxing, output the label and pseudo-ops used to link together
3216    calls and the instruction which set the registers.  */
3217
3218 /* ??? This is unnecessary, and probably should be deleted.  This makes
3219    the insn_addresses declaration above unnecessary.  */
3220
3221 /* ??? The addresses printed by this routine for insns are nonsense for
3222    insns which are inside of a sequence where none of the inner insns have
3223    variable length.  This is because the second pass of shorten_branches
3224    does not bother to update them.  */
3225
3226 void
3227 final_prescan_insn (insn, opvec, noperands)
3228      rtx insn;
3229      rtx *opvec;
3230      int noperands;
3231 {
3232   if (TARGET_DUMPISIZE)
3233     fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
3234
3235   if (TARGET_RELAX)
3236     {
3237       rtx note;
3238
3239       note = find_reg_note (insn, REG_LABEL, NULL_RTX);
3240       if (note)
3241         {
3242           rtx pattern;
3243
3244           pattern = PATTERN (insn);
3245           if (GET_CODE (pattern) == PARALLEL)
3246             pattern = XVECEXP (pattern, 0, 0);
3247           if (GET_CODE (pattern) == CALL
3248               || (GET_CODE (pattern) == SET
3249                   && (GET_CODE (SET_SRC (pattern)) == CALL
3250                       || get_attr_type (insn) == TYPE_SFUNC)))
3251             asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
3252                          CODE_LABEL_NUMBER (XEXP (note, 0)));
3253           else if (GET_CODE (pattern) == SET)
3254             ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
3255                                        CODE_LABEL_NUMBER (XEXP (note, 0)));
3256           else
3257             abort ();
3258         }
3259     }
3260 }
3261
3262 /* Dump out any constants accumulated in the final pass.  These will
3263    will only be labels.  */
3264
3265 char *
3266 output_jump_label_table ()
3267 {
3268   int i;
3269
3270   if (pool_size)
3271     {
3272       fprintf (asm_out_file, "\t.align 2\n");
3273       for (i = 0; i < pool_size; i++)
3274         {
3275           pool_node *p = &pool_vector[i];
3276
3277           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
3278                                      CODE_LABEL_NUMBER (p->label));
3279           output_asm_insn (".long       %O0", &p->value);
3280         }
3281       pool_size = 0;
3282     }
3283
3284   return "";
3285 }
3286 \f
3287 /* A full frame looks like:
3288
3289    arg-5
3290    arg-4
3291    [ if current_function_anonymous_args
3292    arg-3
3293    arg-2
3294    arg-1
3295    arg-0 ]
3296    saved-fp
3297    saved-r10
3298    saved-r11
3299    saved-r12
3300    saved-pr
3301    local-n
3302    ..
3303    local-1
3304    local-0        <- fp points here.  */
3305
3306 /* Number of bytes pushed for anonymous args, used to pass information
3307    between expand_prologue and expand_epilogue.  */
3308
3309 static int extra_push;
3310
3311 /* Adjust the stack by SIZE bytes.  REG holds the rtl of the register
3312   to be adjusted, and TEMP, if nonnegative, holds the register number
3313   of a general register that we may clobber.  */
3314
3315 static void
3316 output_stack_adjust (size, reg, temp)
3317      int size;
3318      rtx reg;
3319      int temp;
3320 {
3321   if (size)
3322     {
3323       if (CONST_OK_FOR_I (size))
3324         emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
3325       /* Try to do it with two partial adjustments; however, we must make
3326          sure that the stack is properly aligned at all times, in case
3327          an interrupt occurs between the two partial adjustments. */
3328       else if (CONST_OK_FOR_I (size / 2 & -4)
3329                && CONST_OK_FOR_I (size - (size / 2 & -4)))
3330         {
3331           emit_insn (gen_addsi3 (reg, reg, GEN_INT (size / 2 & -4)));
3332           emit_insn (gen_addsi3 (reg, reg, GEN_INT (size - (size / 2 & -4))));
3333         }
3334       else
3335         {
3336           rtx const_reg;
3337
3338           /* If TEMP is invalid, we could temporarily save a general
3339              register to MACL.  However, there is currently no need
3340              to handle this case, so just abort when we see it.  */
3341           if (temp < 0)
3342             abort ();
3343           const_reg = gen_rtx (REG, SImode, temp);
3344
3345           /* If SIZE is negative, subtract the positive value.
3346              This sometimes allows a constant pool entry to be shared
3347              between prologue and epilogue code.  */
3348           if (size < 0)
3349             {
3350               emit_insn (gen_movsi (const_reg, GEN_INT (-size)));
3351               emit_insn (gen_subsi3 (reg, reg, const_reg));
3352             }
3353           else
3354             {
3355               emit_insn (gen_movsi (const_reg, GEN_INT (size)));
3356               emit_insn (gen_addsi3 (reg, reg, const_reg));
3357             }
3358         }
3359     }
3360 }
3361
3362 /* Output RTL to push register RN onto the stack.  */
3363
3364 static void
3365 push (rn)
3366      int rn;
3367 {
3368   rtx x;
3369   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
3370       || rn == FPUL_REG)
3371     x = gen_push_e (gen_rtx (REG, SFmode, rn));
3372   else
3373     x = gen_push (gen_rtx (REG, SImode, rn));
3374
3375   x = emit_insn (x);
3376   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
3377                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
3378 }
3379
3380 /* Output RTL to pop register RN from the stack.  */
3381
3382 static void
3383 pop (rn)
3384      int rn;
3385 {
3386   rtx x;
3387   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
3388       || rn == FPUL_REG)
3389     x = gen_pop_e (gen_rtx (REG, SFmode, rn));
3390   else
3391     x = gen_pop (gen_rtx (REG, SImode, rn));
3392     
3393   x = emit_insn (x);
3394   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
3395                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
3396 }
3397
3398 /* Generate code to push the regs specified in the mask.  */
3399
3400 static void
3401 push_regs (mask, mask2)
3402      int mask, mask2;
3403 {
3404   int i;
3405
3406   /* Push PR last; this gives better latencies after the prologue, and
3407      candidates for the return delay slot when there are no general
3408      registers pushed.  */
3409   for (i = 0; i < 32; i++)
3410     if (mask & (1 << i) && i != PR_REG)
3411       push (i);
3412   for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
3413     if (mask2 & (1 << (i - 32)))
3414       push (i);
3415   if (mask & (1 << PR_REG))
3416     push (PR_REG);
3417 }
3418
3419 /* Work out the registers which need to be saved, both as a mask and a
3420    count of saved words.
3421
3422    If doing a pragma interrupt function, then push all regs used by the
3423    function, and if we call another function (we can tell by looking at PR),
3424    make sure that all the regs it clobbers are safe too.  */
3425
3426 static int
3427 calc_live_regs (count_ptr, live_regs_mask2)
3428      int *count_ptr;
3429      int *live_regs_mask2;
3430 {
3431   int reg;
3432   int live_regs_mask = 0;
3433   int count;
3434
3435   *live_regs_mask2 = 0;
3436   for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
3437     {
3438       if ((pragma_interrupt && ! pragma_trapa)
3439           ? (/* Need to save all the regs ever live.  */
3440              (regs_ever_live[reg]
3441               || (call_used_regs[reg]
3442                   && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
3443                   && regs_ever_live[PR_REG]))
3444              && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
3445              && reg != RETURN_ADDRESS_POINTER_REGNUM
3446              && reg != T_REG && reg != GBR_REG)
3447           : (/* Only push those regs which are used and need to be saved.  */
3448              regs_ever_live[reg] && ! call_used_regs[reg]))
3449         {
3450           if (reg >= 32)
3451             *live_regs_mask2 |= 1 << (reg - 32);
3452           else
3453             live_regs_mask |= 1 << reg;
3454           count++;
3455         }
3456     }
3457
3458   *count_ptr = count;
3459   return live_regs_mask;
3460 }
3461
3462 /* Code to generate prologue and epilogue sequences */
3463
3464 void
3465 sh_expand_prologue ()
3466 {
3467   int live_regs_mask;
3468   int d, i;
3469   int live_regs_mask2;
3470
3471   /* We have pretend args if we had an object sent partially in registers
3472      and partially on the stack, e.g. a large structure.  */
3473   output_stack_adjust (-current_function_pretend_args_size,
3474                        stack_pointer_rtx, 3);
3475
3476   extra_push = 0;
3477
3478   /* This is set by SETUP_VARARGS to indicate that this is a varargs
3479      routine.  Clear it here so that the next function isn't affected. */
3480   if (current_function_anonymous_args)
3481     {
3482       current_function_anonymous_args = 0;
3483
3484       /* This is not used by the SH3E calling convention  */
3485       if (!TARGET_SH3E)
3486         {
3487           /* Push arg regs as if they'd been provided by caller in stack.  */
3488           for (i = 0; i < NPARM_REGS(SImode); i++)
3489             {
3490               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
3491               if (i >= (NPARM_REGS(SImode) 
3492                         - current_function_args_info.arg_count[(int) SH_ARG_INT]
3493                         ))
3494                 break;
3495               push (rn);
3496               extra_push += 4;
3497             }
3498         }
3499     }
3500
3501   /* If we're supposed to switch stacks at function entry, do so now.  */
3502   if (sp_switch)
3503     emit_insn (gen_sp_switch_1 ());
3504
3505   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
3506   push_regs (live_regs_mask, live_regs_mask2);
3507
3508   output_stack_adjust (-get_frame_size (), stack_pointer_rtx, 3);
3509
3510   if (frame_pointer_needed)
3511     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
3512 }
3513
3514 void
3515 sh_expand_epilogue ()
3516 {
3517   int live_regs_mask;
3518   int d, i;
3519
3520   int live_regs_mask2;
3521
3522   if (frame_pointer_needed)
3523     {
3524       output_stack_adjust (get_frame_size (), frame_pointer_rtx, 7);
3525
3526       /* We must avoid moving the stack pointer adjustment past code
3527          which reads from the local frame, else an interrupt could
3528          occur after the SP adjustment and clobber data in the local
3529          frame.  */
3530       emit_insn (gen_blockage ());
3531       emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
3532     }
3533   else if (get_frame_size ())
3534     {
3535       /* We must avoid moving the stack pointer adjustment past code
3536          which reads from the local frame, else an interrupt could
3537          occur after the SP adjustment and clobber data in the local
3538          frame.  */
3539       emit_insn (gen_blockage ());
3540       output_stack_adjust (get_frame_size (), stack_pointer_rtx, 7);
3541     }
3542
3543   /* Pop all the registers.  */
3544
3545   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
3546   if (live_regs_mask & (1 << PR_REG))
3547     pop (PR_REG);
3548   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3549     {
3550       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
3551       if (j < 32 && (live_regs_mask & (1 << j)) && j != PR_REG)
3552         pop (j);
3553       else if (j >= 32 && (live_regs_mask2 & (1 << (j - 32))))
3554         pop (j);
3555     }
3556
3557   output_stack_adjust (extra_push + current_function_pretend_args_size,
3558                        stack_pointer_rtx, 7);
3559
3560   /* Switch back to the normal stack if necessary.  */
3561   if (sp_switch)
3562     emit_insn (gen_sp_switch_2 ());
3563 }
3564
3565 /* Clear variables at function end.  */
3566
3567 void
3568 function_epilogue (stream, size)
3569      FILE *stream;
3570      int size;
3571 {
3572   trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
3573   sp_switch = NULL_RTX;
3574 }
3575
3576 rtx
3577 sh_builtin_saveregs (arglist)
3578      tree arglist;
3579 {
3580   tree fntype = TREE_TYPE (current_function_decl);
3581   /* First unnamed integer register.  */
3582   int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
3583   /* Number of integer registers we need to save.  */
3584   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
3585   /* First unnamed SFmode float reg */
3586   int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
3587   /* Number of SFmode float regs to save.  */
3588   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
3589   int ptrsize = GET_MODE_SIZE (Pmode);
3590   rtx valist, regbuf, fpregs;
3591   int bufsize, regno;
3592
3593   /* Allocate block of memory for the regs. */
3594   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
3595      Or can assign_stack_local accept a 0 SIZE argument?  */
3596   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
3597
3598   regbuf = assign_stack_local (BLKmode, bufsize, 0);
3599   MEM_IN_STRUCT_P (regbuf) = 1;
3600
3601   /* Save int args.
3602      This is optimized to only save the regs that are necessary.  Explicitly
3603      named args need not be saved.  */
3604   if (n_intregs > 0)
3605     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
3606                          gen_rtx (MEM, BLKmode, 
3607                                 plus_constant (XEXP (regbuf, 0),
3608                                         n_floatregs * UNITS_PER_WORD)), 
3609                          n_intregs, n_intregs * UNITS_PER_WORD);
3610
3611   /* Save float args.
3612      This is optimized to only save the regs that are necessary.  Explicitly
3613      named args need not be saved.
3614      We explicitly build a pointer to the buffer because it halves the insn
3615      count when not optimizing (otherwise the pointer is built for each reg
3616      saved).
3617      We emit the moves in reverse order so that we can use predecrement.  */
3618
3619   fpregs = gen_reg_rtx (Pmode);
3620   emit_move_insn (fpregs, XEXP (regbuf, 0));
3621   emit_insn (gen_addsi3 (fpregs, fpregs,
3622                          GEN_INT (n_floatregs * UNITS_PER_WORD)));
3623     for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
3624       {
3625         emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
3626         emit_move_insn (gen_rtx (MEM, SFmode, fpregs),
3627                         gen_rtx (REG, SFmode, BASE_ARG_REG (SFmode) + regno));
3628       }
3629
3630   /* Return the address of the regbuf.  */
3631   return XEXP (regbuf, 0);
3632 }
3633
3634 /* Define the offset between two registers, one to be eliminated, and
3635    the other its replacement, at the start of a routine.  */
3636
3637 int
3638 initial_elimination_offset (from, to)
3639      int from;
3640      int to;
3641 {
3642   int regs_saved;
3643   int total_saved_regs_space;
3644   int total_auto_space = get_frame_size ();
3645   int save_flags = target_flags;
3646
3647   int live_regs_mask, live_regs_mask2;
3648   live_regs_mask = calc_live_regs (&regs_saved, &live_regs_mask2);
3649   target_flags = save_flags;
3650
3651   total_saved_regs_space = (regs_saved) * 4;
3652
3653   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
3654     return total_saved_regs_space + total_auto_space;
3655
3656   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
3657     return total_saved_regs_space + total_auto_space;
3658
3659   /* Initial gap between fp and sp is 0.  */
3660   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
3661     return 0;
3662
3663   if (from == RETURN_ADDRESS_POINTER_REGNUM
3664       && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
3665     {
3666       int i, n = total_saved_regs_space;
3667       for (i = PR_REG-1; i >= 0; i--)
3668         if (live_regs_mask & (1 << i))
3669           n -= 4;
3670       return n + total_auto_space;
3671     }
3672
3673   abort ();
3674 }
3675 \f
3676 /* Handle machine specific pragmas to be semi-compatible with Hitachi
3677    compiler.  */
3678
3679 int
3680 handle_pragma (file, t)
3681      FILE *file;
3682      tree t;
3683 {
3684   int retval = 0;
3685   register char *pname;
3686
3687   if (TREE_CODE (t) != IDENTIFIER_NODE)
3688     return 0;
3689
3690   pname = IDENTIFIER_POINTER (t);
3691   if (strcmp (pname, "interrupt") == 0)
3692     pragma_interrupt = retval = 1;
3693   else if (strcmp (pname, "trapa") == 0)
3694     pragma_interrupt = pragma_trapa = retval = 1;
3695   else if (strcmp (pname, "nosave_low_regs") == 0)
3696     pragma_nosave_low_regs = retval = 1;
3697
3698   return retval;
3699 }
3700 /* Return nonzero if ATTR is a valid attribute for DECL.
3701    ATTRIBUTES are any existing attributes and ARGS are the arguments
3702    supplied with ATTR.
3703
3704    Supported attributes:
3705
3706    interrupt_handler -- specifies this function is an interrupt handler.
3707
3708    sp_switch -- specifies an alternate stack for an interrupt handler
3709    to run on.
3710
3711    trap_exit -- use a trapa to exit an interrupt function instead of
3712    an rte instruction.  */
3713
3714 int
3715 sh_valid_machine_decl_attribute (decl, attributes, attr, args)
3716      tree decl;
3717      tree attributes;
3718      tree attr;
3719      tree args;
3720 {
3721   int retval = 0;
3722
3723   if (TREE_CODE (decl) != FUNCTION_DECL)
3724     return 0;
3725
3726   if (is_attribute_p ("interrupt_handler", attr))
3727     {
3728       pragma_interrupt = 1;
3729       return 1;
3730     }
3731
3732   if (is_attribute_p ("sp_switch", attr))
3733     {
3734       /* The sp_switch attribute only has meaning for interrupt functions.  */
3735       if (!pragma_interrupt)
3736         return 0;
3737
3738       /* sp_switch must have an argument.  */
3739       if (!args || TREE_CODE (args) != TREE_LIST)
3740         return 0;
3741
3742       /* The argument must be a constant string.  */
3743       if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
3744         return 0;
3745
3746       sp_switch = gen_rtx (SYMBOL_REF, VOIDmode,
3747                            TREE_STRING_POINTER (TREE_VALUE (args)));
3748       return 1;
3749     }
3750
3751   if (is_attribute_p ("trap_exit", attr))
3752     {
3753       /* The trap_exit attribute only has meaning for interrupt functions.  */
3754       if (!pragma_interrupt)
3755         return 0;
3756
3757       /* trap_exit must have an argument.  */
3758       if (!args || TREE_CODE (args) != TREE_LIST)
3759         return 0;
3760
3761       /* The argument must be a constant integer.  */
3762       if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
3763         return 0;
3764
3765       trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
3766       return 1;
3767     }
3768 }
3769
3770 \f
3771 /* Predicates used by the templates.  */
3772
3773 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
3774    Used only in general_movsrc_operand.  */
3775
3776 int
3777 system_reg_operand (op, mode)
3778      rtx op;
3779      enum machine_mode mode;
3780 {
3781   switch (REGNO (op))
3782     {
3783     case PR_REG:
3784     case MACL_REG:
3785     case MACH_REG:
3786       return 1;
3787     }
3788   return 0;
3789 }
3790
3791 /* Returns 1 if OP can be source of a simple move operation.
3792    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
3793    invalid as are subregs of system registers.  */
3794
3795 int
3796 general_movsrc_operand (op, mode)
3797      rtx op;
3798      enum machine_mode mode;
3799 {
3800   if (GET_CODE (op) == MEM)
3801     {
3802       rtx inside = XEXP (op, 0);
3803       if (GET_CODE (inside) == CONST)
3804         inside = XEXP (inside, 0);
3805
3806       if (GET_CODE (inside) == LABEL_REF)
3807         return 1;
3808
3809       if (GET_CODE (inside) == PLUS
3810           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
3811           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
3812         return 1;
3813
3814       /* Only post inc allowed.  */
3815       if (GET_CODE (inside) == PRE_DEC)
3816         return 0;
3817     }
3818
3819   if ((mode == QImode || mode == HImode)
3820       && (GET_CODE (op) == SUBREG
3821           && GET_CODE (XEXP (op, 0)) == REG
3822           && system_reg_operand (XEXP (op, 0), mode)))
3823     return 0;
3824
3825   return general_operand (op, mode);
3826 }
3827
3828 /* Returns 1 if OP can be a destination of a move.
3829    Same as general_operand, but no preinc allowed.  */
3830
3831 int
3832 general_movdst_operand (op, mode)
3833      rtx op;
3834      enum machine_mode mode;
3835 {
3836   /* Only pre dec allowed.  */
3837   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
3838     return 0;
3839
3840   return general_operand (op, mode);
3841 }
3842
3843 /* Returns 1 if OP is a normal arithmetic register.  */
3844
3845 int
3846 arith_reg_operand (op, mode)
3847      rtx op;
3848      enum machine_mode mode;
3849 {
3850   if (register_operand (op, mode))
3851     {
3852       int regno;
3853
3854       if (GET_CODE (op) == REG)
3855         regno = REGNO (op);
3856       else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
3857         regno = REGNO (SUBREG_REG (op));
3858       else
3859         return 1;
3860
3861       return (regno != T_REG && regno != PR_REG && regno != FPUL_REG
3862               && regno != MACH_REG && regno != MACL_REG);
3863     }
3864   return 0;
3865 }
3866
3867 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
3868
3869 int
3870 arith_operand (op, mode)
3871      rtx op;
3872      enum machine_mode mode;
3873 {
3874   if (arith_reg_operand (op, mode))
3875     return 1;
3876
3877   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
3878     return 1;
3879
3880   return 0;
3881 }
3882
3883 /* Returns 1 if OP is a valid source operand for a compare insn.  */
3884
3885 int
3886 arith_reg_or_0_operand (op, mode)
3887      rtx op;
3888      enum machine_mode mode;
3889 {
3890   if (arith_reg_operand (op, mode))
3891     return 1;
3892
3893   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
3894     return 1;
3895
3896   return 0;
3897 }
3898
3899 /* Returns 1 if OP is a valid source operand for a logical operation.  */
3900
3901 int
3902 logical_operand (op, mode)
3903      rtx op;
3904      enum machine_mode mode;
3905 {
3906   if (arith_reg_operand (op, mode))
3907     return 1;
3908
3909   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
3910     return 1;
3911
3912   return 0;
3913 }
3914
3915 /* Nonzero if OP is a floating point value with value 0.0.  */
3916
3917 int
3918 fp_zero_operand (op)
3919      rtx op;
3920 {
3921   REAL_VALUE_TYPE r;
3922
3923   if (GET_MODE (op) != SFmode)
3924     return 0;
3925
3926   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3927   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
3928 }
3929
3930 /* Nonzero if OP is a floating point value with value 1.0.  */
3931
3932 int
3933 fp_one_operand (op)
3934      rtx op;
3935 {
3936   REAL_VALUE_TYPE r;
3937
3938   if (GET_MODE (op) != SFmode)
3939     return 0;
3940
3941   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
3942   return REAL_VALUES_EQUAL (r, dconst1);
3943 }
3944
3945 int
3946 braf_label_ref_operand(op, mode)
3947      rtx op;
3948      enum machine_mode mode;
3949 {
3950   rtx prev;
3951
3952   if (GET_CODE (op) != LABEL_REF)
3953     return 0;
3954   prev = prev_real_insn (XEXP (op, 0));
3955   if (GET_CODE (prev) != JUMP_INSN)
3956     return 0;
3957   prev = PATTERN (prev);
3958   if (GET_CODE (prev) != PARALLEL || XVECLEN (prev, 0) != 2)
3959     return 0;
3960   prev = XVECEXP (prev, 0, 0);
3961   if (GET_CODE (prev) != SET)
3962     return 0;
3963   prev = SET_SRC (prev);
3964   if (GET_CODE (prev) != PLUS || XEXP (prev, 1) != op)
3965     return 0;
3966 }
3967 \f
3968 /* Return the destination address of a branch.  */
3969    
3970 int
3971 branch_dest (branch)
3972      rtx branch;
3973 {
3974   rtx dest = SET_SRC (PATTERN (branch));
3975   int dest_uid;
3976
3977   if (GET_CODE (dest) == IF_THEN_ELSE)
3978     dest = XEXP (dest, 1);
3979   dest = XEXP (dest, 0);
3980   dest_uid = INSN_UID (dest);
3981   return insn_addresses[dest_uid];
3982 }
3983 \f
3984 /* Return non-zero if REG is not used after INSN.
3985    We assume REG is a reload reg, and therefore does
3986    not live past labels.  It may live past calls or jumps though.  */
3987 int
3988 reg_unused_after (reg, insn)
3989      rtx reg;
3990      rtx insn;
3991 {
3992   enum rtx_code code;
3993   rtx set;
3994
3995   /* If the reg is set by this instruction, then it is safe for our
3996      case.  Disregard the case where this is a store to memory, since
3997      we are checking a register used in the store address.  */
3998   set = single_set (insn);
3999   if (set && GET_CODE (SET_DEST (set)) != MEM
4000       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4001     return 1;
4002
4003   while (insn = NEXT_INSN (insn))
4004     {
4005       code = GET_CODE (insn);
4006
4007 #if 0
4008       /* If this is a label that existed before reload, then the register
4009          if dead here.  However, if this is a label added by reorg, then
4010          the register may still be live here.  We can't tell the difference,
4011          so we just ignore labels completely.  */
4012       if (code == CODE_LABEL)
4013         return 1;
4014       /* else */
4015 #endif
4016
4017       if (code == JUMP_INSN)
4018         return 0;
4019
4020       /* If this is a sequence, we must handle them all at once.
4021          We could have for instance a call that sets the target register,
4022          and a insn in a delay slot that uses the register.  In this case,
4023          we must return 0.  */
4024       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
4025         {
4026           int i;
4027           int retval = 0;
4028
4029           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
4030             {
4031               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
4032               rtx set = single_set (this_insn);
4033
4034               if (GET_CODE (this_insn) == CALL_INSN)
4035                 code = CALL_INSN;
4036               else if (GET_CODE (this_insn) == JUMP_INSN)
4037                 {
4038                   if (INSN_ANNULLED_BRANCH_P (this_insn))
4039                     return 0;
4040                   code = JUMP_INSN;
4041                 }
4042
4043               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
4044                 return 0;
4045               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4046                 {
4047                   if (GET_CODE (SET_DEST (set)) != MEM)
4048                     retval = 1;
4049                   else
4050                     return 0;
4051                 }
4052               if (set == 0
4053                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
4054                 return 0;
4055             }
4056           if (retval == 1)
4057             return 1;
4058           else if (code == JUMP_INSN)
4059             return 0;
4060         }
4061       else if (GET_RTX_CLASS (code) == 'i')
4062         {
4063           rtx set = single_set (insn);
4064
4065           if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
4066             return 0;
4067           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
4068             return GET_CODE (SET_DEST (set)) != MEM;
4069           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
4070             return 0;
4071         }
4072
4073       if (code == CALL_INSN && call_used_regs[REGNO (reg)])
4074         return 1;
4075     }
4076   return 1;
4077 }