OSDN Git Service

*** empty log message ***
[pf3gnuchains/gcc-fork.git] / gcc / emit-rtl.c
1 /* Emit RTL for the GNU C-Compiler expander.
2    Copyright (C) 1987, 1988, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* Middle-to-low level generation of rtx code and insns.
22
23    This file contains the functions `gen_rtx', `gen_reg_rtx'
24    and `gen_label_rtx' that are the usual ways of creating rtl
25    expressions for most purposes.
26
27    It also has the functions for creating insns and linking
28    them in the doubly-linked chain.
29
30    The patterns of the insns are created by machine-dependent
31    routines in insn-emit.c, which is generated automatically from
32    the machine description.  These routines use `gen_rtx' to make
33    the individual rtx's of the pattern; what is machine dependent
34    is the kind of rtx's they make and what arguments they use.  */
35
36 #include "config.h"
37 #include <stdio.h>
38 #include "gvarargs.h"
39 #include "rtl.h"
40 #include "flags.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "regs.h"
44 #include "insn-config.h"
45 #include "real.h"
46
47 /* This is reset to LAST_VIRTUAL_REGISTER + 1 at the start of each function.
48    After rtl generation, it is 1 plus the largest register number used.  */
49
50 int reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
51
52 /* This is *not* reset after each function.  It gives each CODE_LABEL
53    in the entire compilation a unique label number.  */
54
55 static int label_num = 1;
56
57 /* Lowest label number in current function.  */
58
59 static int first_label_num;
60
61 /* Highest label number in current function.
62    Zero means use the value of label_num instead.
63    This is nonzero only when belatedly compiling an inline function.  */
64
65 static int last_label_num;
66
67 /* Value label_num had when set_new_first_and_last_label_number was called.
68    If label_num has not changed since then, last_label_num is valid.  */
69
70 static int base_label_num;
71
72 /* Nonzero means do not generate NOTEs for source line numbers.  */
73
74 static int no_line_numbers;
75
76 /* Commonly used rtx's, so that we only need space for one copy.
77    These are initialized once for the entire compilation.
78    All of these except perhaps the floating-point CONST_DOUBLEs
79    are unique; no other rtx-object will be equal to any of these.  */
80
81 rtx pc_rtx;                     /* (PC) */
82 rtx cc0_rtx;                    /* (CC0) */
83 rtx cc1_rtx;                    /* (CC1) (not actually used nowadays) */
84 rtx const0_rtx;                 /* (CONST_INT 0) */
85 rtx const1_rtx;                 /* (CONST_INT 1) */
86 rtx const2_rtx;                 /* (CONST_INT 2) */
87 rtx constm1_rtx;                /* (CONST_INT -1) */
88 rtx const_true_rtx;             /* (CONST_INT STORE_FLAG_VALUE) */
89
90 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
91    the values of 0, 1, and 2.  For the integer entries and VOIDmode, we
92    record a copy of const[012]_rtx.  */
93
94 rtx const_tiny_rtx[3][(int) MAX_MACHINE_MODE];
95
96 REAL_VALUE_TYPE dconst0;
97 REAL_VALUE_TYPE dconst1;
98 REAL_VALUE_TYPE dconst2;
99 REAL_VALUE_TYPE dconstm1;
100
101 /* All references to the following fixed hard registers go through
102    these unique rtl objects.  On machines where the frame-pointer and
103    arg-pointer are the same register, they use the same unique object.
104
105    After register allocation, other rtl objects which used to be pseudo-regs
106    may be clobbered to refer to the frame-pointer register.
107    But references that were originally to the frame-pointer can be
108    distinguished from the others because they contain frame_pointer_rtx.
109
110    In an inline procedure, the stack and frame pointer rtxs may not be
111    used for anything else.  */
112 rtx stack_pointer_rtx;          /* (REG:Pmode STACK_POINTER_REGNUM) */
113 rtx frame_pointer_rtx;          /* (REG:Pmode FRAME_POINTER_REGNUM) */
114 rtx arg_pointer_rtx;            /* (REG:Pmode ARG_POINTER_REGNUM) */
115 rtx struct_value_rtx;           /* (REG:Pmode STRUCT_VALUE_REGNUM) */
116 rtx struct_value_incoming_rtx;  /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */
117 rtx static_chain_rtx;           /* (REG:Pmode STATIC_CHAIN_REGNUM) */
118 rtx static_chain_incoming_rtx;  /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) */
119 rtx pic_offset_table_rtx;       /* (REG:Pmode PIC_OFFSET_TABLE_REGNUM) */
120
121 rtx virtual_incoming_args_rtx;  /* (REG:Pmode VIRTUAL_INCOMING_ARGS_REGNUM) */
122 rtx virtual_stack_vars_rtx;     /* (REG:Pmode VIRTUAL_STACK_VARS_REGNUM) */
123 rtx virtual_stack_dynamic_rtx;  /* (REG:Pmode VIRTUAL_STACK_DYNAMIC_REGNUM) */
124 rtx virtual_outgoing_args_rtx;  /* (REG:Pmode VIRTUAL_OUTGOING_ARGS_REGNUM) */
125
126 /* We make one copy of (const_int C) where C is in
127    [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
128    to save space during the compilation and simplify comparisons of
129    integers.  */
130
131 #define MAX_SAVED_CONST_INT 64
132
133 static rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
134
135 /* The ends of the doubly-linked chain of rtl for the current function.
136    Both are reset to null at the start of rtl generation for the function.
137    
138    start_sequence saves both of these on `sequence_stack' and then
139    starts a new, nested sequence of insns.  */
140
141 static rtx first_insn = NULL;
142 static rtx last_insn = NULL;
143
144 /* INSN_UID for next insn emitted.
145    Reset to 1 for each function compiled.  */
146
147 static int cur_insn_uid = 1;
148
149 /* Line number and source file of the last line-number NOTE emitted.
150    This is used to avoid generating duplicates.  */
151
152 static int last_linenum = 0;
153 static char *last_filename = 0;
154
155 /* A vector indexed by pseudo reg number.  The allocated length
156    of this vector is regno_pointer_flag_length.  Since this
157    vector is needed during the expansion phase when the total
158    number of registers in the function is not yet known,
159    it is copied and made bigger when necessary.  */
160
161 char *regno_pointer_flag;
162 int regno_pointer_flag_length;
163
164 /* Indexed by pseudo register number, gives the rtx for that pseudo.
165    Allocated in parallel with regno_pointer_flag.  */
166
167 rtx *regno_reg_rtx;
168
169 /* Stack of pending (incomplete) sequences saved by `start_sequence'.
170    Each element describes one pending sequence.
171    The main insn-chain is saved in the last element of the chain,
172    unless the chain is empty.  */
173
174 struct sequence_stack *sequence_stack;
175
176 /* start_sequence and gen_sequence can make a lot of rtx expressions which are
177    shortly thrown away.  We use two mechanisms to prevent this waste:
178
179    First, we keep a list of the expressions used to represent the sequence
180    stack in sequence_element_free_list.
181
182    Second, for sizes up to 5 elements, we keep a SEQUENCE and its associated
183    rtvec for use by gen_sequence.  One entry for each size is sufficient
184    because most cases are calls to gen_sequence followed by immediately
185    emitting the SEQUENCE.  Reuse is safe since emitting a sequence is
186    destructive on the insn in it anyway and hence can't be redone.
187
188    We do not bother to save this cached data over nested function calls.
189    Instead, we just reinitialize them.  */
190
191 #define SEQUENCE_RESULT_SIZE 5
192
193 static struct sequence_stack *sequence_element_free_list;
194 static rtx sequence_result[SEQUENCE_RESULT_SIZE];
195
196 extern int rtx_equal_function_value_matters;
197
198 /* Filename and line number of last line-number note,
199    whether we actually emitted it or not.  */
200 extern char *emit_filename;
201 extern int emit_lineno;
202
203 rtx change_address ();
204 void init_emit ();
205 \f
206 /* rtx gen_rtx (code, mode, [element1, ..., elementn])
207 **
208 **          This routine generates an RTX of the size specified by
209 **      <code>, which is an RTX code.   The RTX structure is initialized
210 **      from the arguments <element1> through <elementn>, which are
211 **      interpreted according to the specific RTX type's format.   The
212 **      special machine mode associated with the rtx (if any) is specified
213 **      in <mode>.
214 **
215 **          gen_rtx() can be invoked in a way which resembles the lisp-like
216 **      rtx it will generate.   For example, the following rtx structure:
217 **
218 **            (plus:QI (mem:QI (reg:SI 1))
219 **                     (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))
220 **
221 **              ...would be generated by the following C code:
222 **
223 **              gen_rtx (PLUS, QImode,
224 **                  gen_rtx (MEM, QImode,
225 **                      gen_rtx (REG, SImode, 1)),
226 **                  gen_rtx (MEM, QImode,
227 **                      gen_rtx (PLUS, SImode,
228 **                          gen_rtx (REG, SImode, 2),
229 **                          gen_rtx (REG, SImode, 3)))),
230 */
231
232 /*VARARGS2*/
233 rtx
234 gen_rtx (va_alist)
235      va_dcl
236 {
237   va_list p;
238   enum rtx_code code;
239   enum machine_mode mode;
240   register int i;               /* Array indices...                     */
241   register char *fmt;           /* Current rtx's format...              */
242   register rtx rt_val;          /* RTX to return to caller...           */
243
244   va_start (p);
245   code = va_arg (p, enum rtx_code);
246   mode = va_arg (p, enum machine_mode);
247
248   if (code == CONST_INT)
249     {
250       int arg = va_arg (p, int);
251
252       if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
253         return const_int_rtx[arg + MAX_SAVED_CONST_INT];
254
255       if (const_true_rtx && arg == STORE_FLAG_VALUE)
256         return const_true_rtx;
257
258       rt_val = rtx_alloc (code);
259       INTVAL (rt_val) = arg;
260     }
261   else if (code == REG)
262     {
263       int regno = va_arg (p, int);
264
265       /* In case the MD file explicitly references the frame pointer, have
266          all such references point to the same frame pointer.  This is used
267          during frame pointer elimination to distinguish the explicit
268          references to these registers from pseudos that happened to be
269          assigned to them.
270
271          If we have eliminated the frame pointer or arg pointer, we will
272          be using it as a normal register, for example as a spill register.
273          In such cases, we might be accessing it in a mode that is not
274          Pmode and therefore cannot use the pre-allocated rtx.  */
275
276       if (frame_pointer_rtx && regno == FRAME_POINTER_REGNUM && mode == Pmode)
277         return frame_pointer_rtx;
278 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
279       if (arg_pointer_rtx && regno == ARG_POINTER_REGNUM && mode == Pmode)
280         return arg_pointer_rtx;
281 #endif
282       if (stack_pointer_rtx && regno == STACK_POINTER_REGNUM && mode == Pmode)
283         return stack_pointer_rtx;
284       else
285         {
286           rt_val = rtx_alloc (code);
287           rt_val->mode = mode;
288           REGNO (rt_val) = regno;
289           return rt_val;
290         }
291     }
292   else
293     {
294       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
295       rt_val->mode = mode;              /* Store the machine mode...  */
296
297       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
298       for (i = 0; i < GET_RTX_LENGTH (code); i++)
299         {
300           switch (*fmt++)
301             {
302             case '0':           /* Unused field.  */
303               break;
304
305             case 'i':           /* An integer?  */
306               XINT (rt_val, i) = va_arg (p, int);
307               break;
308
309             case 's':           /* A string?  */
310               XSTR (rt_val, i) = va_arg (p, char *);
311               break;
312
313             case 'e':           /* An expression?  */
314             case 'u':           /* An insn?  Same except when printing.  */
315               XEXP (rt_val, i) = va_arg (p, rtx);
316               break;
317
318             case 'E':           /* An RTX vector?  */
319               XVEC (rt_val, i) = va_arg (p, rtvec);
320               break;
321
322             default:
323               abort();
324             }
325         }
326     }
327   va_end (p);
328   return rt_val;                /* Return the new RTX...                */
329 }
330
331 /* gen_rtvec (n, [rt1, ..., rtn])
332 **
333 **          This routine creates an rtvec and stores within it the
334 **      pointers to rtx's which are its arguments.
335 */
336
337 /*VARARGS1*/
338 rtvec
339 gen_rtvec (va_alist)
340      va_dcl
341 {
342   int n, i;
343   va_list p;
344   rtx *vector;
345
346   va_start (p);
347   n = va_arg (p, int);
348
349   if (n == 0)
350     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
351
352   vector = (rtx *) alloca (n * sizeof (rtx));
353   for (i = 0; i < n; i++)
354     vector[i] = va_arg (p, rtx);
355   va_end (p);
356
357   return gen_rtvec_v (n, vector);
358 }
359
360 rtvec
361 gen_rtvec_v (n, argp)
362      int n;
363      rtx *argp;
364 {
365   register int i;
366   register rtvec rt_val;
367
368   if (n == 0)
369     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
370
371   rt_val = rtvec_alloc (n);     /* Allocate an rtvec...                 */
372
373   for (i = 0; i < n; i++)
374     rt_val->elem[i].rtx = *argp++;
375
376   return rt_val;
377 }
378 \f
379 /* Generate a REG rtx for a new pseudo register of mode MODE.
380    This pseudo is assigned the next sequential register number.  */
381
382 rtx
383 gen_reg_rtx (mode)
384      enum machine_mode mode;
385 {
386   register rtx val;
387
388   /* Don't let anything called by or after reload create new registers
389      (actually, registers can't be created after flow, but this is a good
390      approximation).  */
391
392   if (reload_in_progress || reload_completed)
393     abort ();
394
395   /* Make sure regno_pointer_flag and regno_reg_rtx are large
396      enough to have an element for this pseudo reg number.  */
397
398   if (reg_rtx_no == regno_pointer_flag_length)
399     {
400       rtx *new1;
401       char *new =
402         (char *) oballoc (regno_pointer_flag_length * 2);
403       bzero (new, regno_pointer_flag_length * 2);
404       bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
405       regno_pointer_flag = new;
406
407       new1 = (rtx *) oballoc (regno_pointer_flag_length * 2 * sizeof (rtx));
408       bzero (new1, regno_pointer_flag_length * 2 * sizeof (rtx));
409       bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
410       regno_reg_rtx = new1;
411
412       regno_pointer_flag_length *= 2;
413     }
414
415   val = gen_rtx (REG, mode, reg_rtx_no);
416   regno_reg_rtx[reg_rtx_no++] = val;
417   return val;
418 }
419
420 /* Identify REG as a probable pointer register.  */
421
422 void
423 mark_reg_pointer (reg)
424      rtx reg;
425 {
426   REGNO_POINTER_FLAG (REGNO (reg)) = 1;
427 }
428
429 /* Return 1 plus largest pseudo reg number used in the current function.  */
430
431 int
432 max_reg_num ()
433 {
434   return reg_rtx_no;
435 }
436
437 /* Return 1 + the largest label number used so far in the current function.  */
438
439 int
440 max_label_num ()
441 {
442   if (last_label_num && label_num == base_label_num)
443     return last_label_num;
444   return label_num;
445 }
446
447 /* Return first label number used in this function (if any were used).  */
448
449 int
450 get_first_label_num ()
451 {
452   return first_label_num;
453 }
454 \f
455 /* Return a value representing some low-order bits of X, where the number
456    of low-order bits is given by MODE.  Note that no conversion is done
457    between floating-point and fixed-point values, rather, the bit 
458    representation is returned.
459
460    This function handles the cases in common between gen_lowpart, below,
461    and two variants in cse.c and combine.c.  These are the cases that can
462    be safely handled at all points in the compilation.
463
464    If this is not a case we can handle, return 0.  */
465
466 rtx
467 gen_lowpart_common (mode, x)
468      enum machine_mode mode;
469      register rtx x;
470 {
471   int word = 0;
472
473   if (GET_MODE (x) == mode)
474     return x;
475
476   /* MODE must occupy no more words than the mode of X.  */
477   if (GET_MODE (x) != VOIDmode
478       && ((GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
479           > ((GET_MODE_SIZE (GET_MODE (x)) + (UNITS_PER_WORD - 1))
480              / UNITS_PER_WORD)))
481     return 0;
482
483   if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
484     word = ((GET_MODE_SIZE (GET_MODE (x))
485              - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
486             / UNITS_PER_WORD);
487
488   if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
489       && GET_MODE_CLASS (mode) == MODE_INT)
490     {
491       /* If we are getting the low-order part of something that has been
492          sign- or zero-extended, we can either just use the object being
493          extended or make a narrower extension.  If we want an even smaller
494          piece than the size of the object being extended, call ourselves
495          recursively.
496
497          This case is used mostly by combine and cse.  */
498
499       if (GET_MODE (XEXP (x, 0)) == mode)
500         return XEXP (x, 0);
501       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
502         return gen_lowpart_common (mode, XEXP (x, 0));
503       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
504         return gen_rtx (GET_CODE (x), mode, XEXP (x, 0));
505     }
506   else if (GET_CODE (x) == SUBREG
507            && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
508                || GET_MODE_SIZE (mode) == GET_MODE_UNIT_SIZE (GET_MODE (x))))
509     return (GET_MODE (SUBREG_REG (x)) == mode && SUBREG_WORD (x) == 0
510             ? SUBREG_REG (x)
511             : gen_rtx (SUBREG, mode, SUBREG_REG (x), SUBREG_WORD (x)));
512   else if (GET_CODE (x) == REG)
513     {
514       /* If the register is not valid for MODE, return 0.  If we don't
515          do this, there is no way to fix up the resulting REG later.  */
516       if (REGNO (x) < FIRST_PSEUDO_REGISTER
517           && ! HARD_REGNO_MODE_OK (REGNO (x) + word, mode))
518         return 0;
519       else if (REGNO (x) < FIRST_PSEUDO_REGISTER
520                /* integrate.c can't handle parts of a return value register. */
521                && (! REG_FUNCTION_VALUE_P (x)
522                    || ! rtx_equal_function_value_matters))
523         return gen_rtx (REG, mode, REGNO (x) + word);
524       else
525         return gen_rtx (SUBREG, mode, x, word);
526     }
527
528   /* If X is a CONST_INT or a CONST_DOUBLE, extract the appropriate bits
529      from the low-order part of the constant.  */
530   else if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (x) == VOIDmode
531            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
532     {
533       /* If MODE is twice the host word size, X is already the desired
534          representation.  Otherwise, if MODE is wider than a word, we can't
535          do this.  If MODE is exactly a word, return just one CONST_INT.
536          If MODE is smaller than a word, clear the bits that don't belong
537          in our mode, unless they and our sign bit are all one.  So we get
538          either a reasonable negative value or a reasonable unsigned value
539          for this mode.  */
540
541       if (GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_INT)
542         return x;
543       else if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_INT)
544         return 0;
545       else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_INT)
546         return (GET_CODE (x) == CONST_INT ? x
547                 : gen_rtx (CONST_INT, VOIDmode, CONST_DOUBLE_LOW (x)));
548       else
549         {
550           /* MODE must be narrower than HOST_BITS_PER_INT.  */
551           int width = GET_MODE_BITSIZE (mode);
552           int val = (GET_CODE (x) == CONST_INT ? INTVAL (x)
553                      : CONST_DOUBLE_LOW (x));
554
555           if (((val & ((-1) << (width - 1))) != ((-1) << (width - 1))))
556             val &= (1 << width) - 1;
557
558           return (GET_CODE (x) == CONST_INT && INTVAL (x) == val ? x
559                   : gen_rtx (CONST_INT, VOIDmode, val));
560         }
561     }
562
563   /* Otherwise, we can't do this.  */
564   return 0;
565 }
566 \f
567 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a value,
568    return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
569    least-significant part of X.
570    MODE specifies how big a part of X to return;
571    it usually should not be larger than a word.
572    If X is a MEM whose address is a QUEUED, the value may be so also.  */
573
574 rtx
575 gen_lowpart (mode, x)
576      enum machine_mode mode;
577      register rtx x;
578 {
579   rtx result = gen_lowpart_common (mode, x);
580
581   if (result)
582     return result;
583   else if (GET_CODE (x) == MEM)
584     {
585       /* The only additional case we can do is MEM.  */
586       register int offset = 0;
587       if (WORDS_BIG_ENDIAN)
588         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
589                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
590
591       if (BYTES_BIG_ENDIAN)
592         /* Adjust the address so that the address-after-the-data
593            is unchanged.  */
594         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
595                    - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
596
597       return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
598     }
599   else
600     abort ();
601 }
602
603 /* Return 1 iff X, assumed to be a SUBREG,
604    refers to the least significant part of its containing reg.
605    If X is not a SUBREG, always return 1 (it is its own low part!).  */
606
607 int
608 subreg_lowpart_p (x)
609      rtx x;
610 {
611   if (GET_CODE (x) != SUBREG)
612     return 1;
613
614   if (WORDS_BIG_ENDIAN
615       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD)
616     return (SUBREG_WORD (x)
617             == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
618                  - MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD))
619                 / UNITS_PER_WORD));
620
621   return SUBREG_WORD (x) == 0;
622 }
623 \f
624 /* Return subword I of operand OP.
625    The word number, I, is interpreted as the word number starting at the
626    low-order address.  Word 0 is the low-order word if not WORDS_BIG_ENDIAN,
627    otherwise it is the high-order word.
628
629    If we cannot extract the required word, we return zero.  Otherwise, an
630    rtx corresponding to the requested word will be returned.
631
632    VALIDATE_ADDRESS is nonzero if the address should be validated.  Before
633    reload has completed, a valid address will always be returned.  After
634    reload, if a valid address cannot be returned, we return zero.
635
636    If VALIDATE_ADDRESS is zero, we simply form the required address; validating
637    it is the responsibility of the caller.
638
639    MODE is the mode of OP in case it is a CONST_INT.  */
640
641 rtx
642 operand_subword (op, i, validate_address, mode)
643      rtx op;
644      int i;
645      int validate_address;
646      enum machine_mode mode;
647 {
648   int val;
649   int size_ratio = HOST_BITS_PER_INT / BITS_PER_WORD;
650
651   if (mode == VOIDmode)
652     mode = GET_MODE (op);
653
654   if (mode == VOIDmode)
655     abort ();
656
657   /* If OP is narrower than a word or if we want a word outside OP, fail.  */
658   if (mode != BLKmode
659       && (GET_MODE_SIZE (mode) < UNITS_PER_WORD
660           || (i + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode)))
661     return 0;
662
663   /* If OP is already an integer word, return it.  */
664   if (GET_MODE_CLASS (mode) == MODE_INT
665       && GET_MODE_SIZE (mode) == UNITS_PER_WORD)
666     return op;
667
668   /* If OP is a REG or SUBREG, we can handle it very simply.  */
669   if (GET_CODE (op) == REG)
670     {
671       /* If the register is not valid for MODE, return 0.  If we don't
672          do this, there is no way to fix up the resulting REG later.  */
673       if (REGNO (op) < FIRST_PSEUDO_REGISTER
674           && ! HARD_REGNO_MODE_OK (REGNO (op) + i, word_mode))
675         return 0;
676       else if (REGNO (op) >= FIRST_PSEUDO_REGISTER
677                || (REG_FUNCTION_VALUE_P (op)
678                    && rtx_equal_function_value_matters))
679         return gen_rtx (SUBREG, word_mode, op, i);
680       else
681         return gen_rtx (REG, word_mode, REGNO (op) + i);
682     }
683   else if (GET_CODE (op) == SUBREG)
684     return gen_rtx (SUBREG, word_mode, SUBREG_REG (op), i + SUBREG_WORD (op));
685
686   /* Form a new MEM at the requested address.  */
687   if (GET_CODE (op) == MEM)
688     {
689       rtx addr = plus_constant (XEXP (op, 0), i * UNITS_PER_WORD);
690       rtx new;
691
692       if (validate_address)
693         {
694           if (reload_completed)
695             {
696               if (! strict_memory_address_p (word_mode, addr))
697                 return 0;
698             }
699           else
700             addr = memory_address (word_mode, addr);
701         }
702
703       new = gen_rtx (MEM, word_mode, addr);
704
705       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (op);
706       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (op);
707       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (op);
708
709       return new;
710     }
711
712   /* The only remaining cases are when OP is a constant.  If the host and
713      target floating formats are the same, handling two-word floating
714      constants are easy.  */
715   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
716         && HOST_BITS_PER_INT == BITS_PER_WORD)
717        || flag_pretend_float)
718       && GET_MODE_CLASS (mode) == MODE_FLOAT
719       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
720       && GET_CODE (op) == CONST_DOUBLE)
721     return gen_rtx (CONST_INT, VOIDmode,
722                     i ^ (WORDS_BIG_ENDIAN !=
723 /* The constant is stored in the host's word-ordering,
724    but we want to access it in the target's word-ordering.  */
725 #ifdef HOST_WORDS_BIG_ENDIAN
726                          1
727 #else
728                          0
729 #endif
730                          ) ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
731
732   /* Single word float is a little harder, since single- and double-word
733      values often do not have the same high-order bits.  We have already
734      verified that we want the only defined word of the single-word value.  */
735   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
736         && HOST_BITS_PER_INT == BITS_PER_WORD)
737        || flag_pretend_float)
738       && GET_MODE_CLASS (mode) == MODE_FLOAT
739       && GET_MODE_SIZE (mode) == UNITS_PER_WORD
740       && GET_CODE (op) == CONST_DOUBLE)
741     {
742       double d;
743       union {float f; int i; } u;
744
745       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
746
747       u.f = d;
748       return gen_rtx (CONST_INT, VOIDmode, u.i);
749     }
750       
751   /* The only remaining cases that we can handle are integers.
752      Convert to proper endianness now since these cases need it.
753      At this point, i == 0 means the low-order word.  
754
755      Note that it must be that BITS_PER_WORD <= HOST_BITS_PER_INT.
756      This is because if it were greater, it could only have been two
757      times greater since we do not support making wider constants.  In
758      that case, it MODE would have already been the proper size and
759      it would have been handled above.  This means we do not have to
760      worry about the case where we would be returning a CONST_DOUBLE.  */
761
762   if (GET_MODE_CLASS (mode) != MODE_INT
763       || (GET_CODE (op) != CONST_INT && GET_CODE (op) != CONST_DOUBLE))
764     return 0;
765
766   if (WORDS_BIG_ENDIAN)
767     i = GET_MODE_SIZE (mode) / UNITS_PER_WORD - 1 - i;
768
769   /* Find out which word on the host machine this value is in and get
770      it from the constant.  */
771   val = (i / size_ratio == 0
772          ? (GET_CODE (op) == CONST_INT ? INTVAL (op) : CONST_DOUBLE_LOW (op))
773          : (GET_CODE (op) == CONST_INT
774             ? (INTVAL (op) < 0 ? ~0 : 0) : CONST_DOUBLE_HIGH (op)));
775
776   /* If BITS_PER_WORD is smaller than an int, get the appropriate bits.  */
777   if (BITS_PER_WORD < HOST_BITS_PER_INT)
778     val = ((val >> ((i % size_ratio) * BITS_PER_WORD))
779            & ((1 << (BITS_PER_WORD % HOST_BITS_PER_INT)) - 1));
780
781   return gen_rtx (CONST_INT, VOIDmode, val);
782 }
783
784 /* Similar to `operand_subword', but never return 0.  If we can't extract
785    the required subword, put OP into a register and try again.  If that fails,
786    abort.  We always validate the address in this case.  It is not valid
787    to call this function after reload; it is mostly meant for RTL
788    generation. 
789
790    MODE is the mode of OP, in case it is CONST_INT.  */
791
792 rtx
793 operand_subword_force (op, i, mode)
794      rtx op;
795      int i;
796      enum machine_mode mode;
797 {
798   rtx result = operand_subword (op, i, 1, mode);
799
800   if (result)
801     return result;
802
803   if (mode != BLKmode && mode != VOIDmode)
804     op = force_reg (mode, op);
805
806   result = operand_subword (op, i, 1, mode);
807   if (result == 0)
808     abort ();
809
810   return result;
811 }
812 \f
813 /* Given a compare instruction, swap the operands.
814    A test instruction is changed into a compare of 0 against the operand.  */
815
816 void
817 reverse_comparison (insn)
818      rtx insn;
819 {
820   rtx body = PATTERN (insn);
821   rtx comp;
822
823   if (GET_CODE (body) == SET)
824     comp = SET_SRC (body);
825   else
826     comp = SET_SRC (XVECEXP (body, 0, 0));
827
828   if (GET_CODE (comp) == COMPARE)
829     {
830       rtx op0 = XEXP (comp, 0);
831       rtx op1 = XEXP (comp, 1);
832       XEXP (comp, 0) = op1;
833       XEXP (comp, 1) = op0;
834     }
835   else
836     {
837       rtx new = gen_rtx (COMPARE, VOIDmode,
838                          CONST0_RTX (GET_MODE (comp)), comp);
839       if (GET_CODE (body) == SET)
840         SET_SRC (body) = new;
841       else
842         SET_SRC (XVECEXP (body, 0, 0)) = new;
843     }
844 }
845 \f
846 /* Return a memory reference like MEMREF, but with its mode changed
847    to MODE and its address changed to ADDR.
848    (VOIDmode means don't change the mode.
849    NULL for ADDR means don't change the address.)  */
850
851 rtx
852 change_address (memref, mode, addr)
853      rtx memref;
854      enum machine_mode mode;
855      rtx addr;
856 {
857   rtx new;
858
859   if (GET_CODE (memref) != MEM)
860     abort ();
861   if (mode == VOIDmode)
862     mode = GET_MODE (memref);
863   if (addr == 0)
864     addr = XEXP (memref, 0);
865
866   /* If reload is in progress or has completed, ADDR must be valid.
867      Otherwise, we can call memory_address to make it valid.  */
868   if (reload_completed || reload_in_progress)
869     {
870       if (! memory_address_p (mode, addr))
871         abort ();
872     }
873   else
874     addr = memory_address (mode, addr);
875         
876   new = gen_rtx (MEM, mode, addr);
877   MEM_VOLATILE_P (new) = MEM_VOLATILE_P (memref);
878   RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (memref);
879   MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (memref);
880   return new;
881 }
882 \f
883 /* Return a newly created CODE_LABEL rtx with a unique label number.  */
884
885 rtx
886 gen_label_rtx ()
887 {
888   register rtx label = gen_rtx (CODE_LABEL, VOIDmode, 0, 0, 0, label_num++, 0);
889   LABEL_NUSES (label) = 0;
890   return label;
891 }
892 \f
893 /* For procedure integration.  */
894
895 /* Return a newly created INLINE_HEADER rtx.  Should allocate this
896    from a permanent obstack when the opportunity arises.  */
897
898 rtx
899 gen_inline_header_rtx (first_insn, first_parm_insn, first_labelno,
900                        last_labelno, max_parm_regnum, max_regnum, args_size,
901                        pops_args, stack_slots, function_flags,
902                        outgoing_args_size, original_arg_vector,
903                        original_decl_initial)
904      rtx first_insn, first_parm_insn;
905      int first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size;
906      int pops_args;
907      rtx stack_slots;
908      int function_flags;
909      int outgoing_args_size;
910      rtvec original_arg_vector;
911      rtx original_decl_initial;
912 {
913   rtx header = gen_rtx (INLINE_HEADER, VOIDmode,
914                         cur_insn_uid++, NULL,
915                         first_insn, first_parm_insn,
916                         first_labelno, last_labelno,
917                         max_parm_regnum, max_regnum, args_size, pops_args,
918                         stack_slots, function_flags, outgoing_args_size,
919                         original_arg_vector, original_decl_initial);
920   return header;
921 }
922
923 /* Install new pointers to the first and last insns in the chain.
924    Used for an inline-procedure after copying the insn chain.  */
925
926 void
927 set_new_first_and_last_insn (first, last)
928      rtx first, last;
929 {
930   first_insn = first;
931   last_insn = last;
932 }
933
934 /* Set the range of label numbers found in the current function.
935    This is used when belatedly compiling an inline function.  */
936
937 void
938 set_new_first_and_last_label_num (first, last)
939      int first, last;
940 {
941   base_label_num = label_num;
942   first_label_num = first;
943   last_label_num = last;
944 }
945 \f
946 /* Save all variables describing the current status into the structure *P.
947    This is used before starting a nested function.  */
948
949 void
950 save_emit_status (p)
951      struct function *p;
952 {
953   p->reg_rtx_no = reg_rtx_no;
954   p->first_label_num = first_label_num;
955   p->first_insn = first_insn;
956   p->last_insn = last_insn;
957   p->sequence_stack = sequence_stack;
958   p->cur_insn_uid = cur_insn_uid;
959   p->last_linenum = last_linenum;
960   p->last_filename = last_filename;
961   p->regno_pointer_flag = regno_pointer_flag;
962   p->regno_pointer_flag_length = regno_pointer_flag_length;
963   p->regno_reg_rtx = regno_reg_rtx;
964 }
965
966 /* Restore all variables describing the current status from the structure *P.
967    This is used after a nested function.  */
968
969 void
970 restore_emit_status (p)
971      struct function *p;
972 {
973   int i;
974
975   reg_rtx_no = p->reg_rtx_no;
976   first_label_num = p->first_label_num;
977   first_insn = p->first_insn;
978   last_insn = p->last_insn;
979   sequence_stack = p->sequence_stack;
980   cur_insn_uid = p->cur_insn_uid;
981   last_linenum = p->last_linenum;
982   last_filename = p->last_filename;
983   regno_pointer_flag = p->regno_pointer_flag;
984   regno_pointer_flag_length = p->regno_pointer_flag_length;
985   regno_reg_rtx = p->regno_reg_rtx;
986
987   /* Clear our cache of rtx expressions for start_sequence and gen_sequence. */
988   sequence_element_free_list = 0;
989   for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
990     sequence_result[i] = 0;
991 }
992 \f
993 /* Go through all the RTL insn bodies and copy any invalid shared structure.
994    It does not work to do this twice, because the mark bits set here
995    are not cleared afterwards.  */
996
997 void
998 unshare_all_rtl (insn)
999      register rtx insn;
1000 {
1001   for (; insn; insn = NEXT_INSN (insn))
1002     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
1003         || GET_CODE (insn) == CALL_INSN)
1004       {
1005         PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
1006         REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
1007         LOG_LINKS (insn) = copy_rtx_if_shared (LOG_LINKS (insn));
1008       }
1009
1010   /* Make sure the addresses of stack slots found outside the insn chain
1011      (such as, in DECL_RTL of a variable) are not shared
1012      with the insn chain.
1013
1014      This special care is necessary when the stack slot MEM does not
1015      actually appear in the insn chain.  If it does appear, its address
1016      is unshared from all else at that point.  */
1017
1018   copy_rtx_if_shared (stack_slot_list);
1019 }
1020
1021 /* Mark ORIG as in use, and return a copy of it if it was already in use.
1022    Recursively does the same for subexpressions.  */
1023
1024 rtx
1025 copy_rtx_if_shared (orig)
1026      rtx orig;
1027 {
1028   register rtx x = orig;
1029   register int i;
1030   register enum rtx_code code;
1031   register char *format_ptr;
1032   int copied = 0;
1033
1034   if (x == 0)
1035     return 0;
1036
1037   code = GET_CODE (x);
1038
1039   /* These types may be freely shared.  */
1040
1041   switch (code)
1042     {
1043     case REG:
1044     case QUEUED:
1045     case CONST_INT:
1046     case CONST_DOUBLE:
1047     case SYMBOL_REF:
1048     case CODE_LABEL:
1049     case PC:
1050     case CC0:
1051     case SCRATCH:
1052       /* SCRATCH must be shared because they represent distinct values. */
1053       return x;
1054
1055     case INSN:
1056     case JUMP_INSN:
1057     case CALL_INSN:
1058     case NOTE:
1059     case LABEL_REF:
1060     case BARRIER:
1061       /* The chain of insns is not being copied.  */
1062       return x;
1063
1064     case MEM:
1065       /* A MEM is allowed to be shared if its address is constant
1066          or is a constant plus one of the special registers.  */
1067       if (CONSTANT_ADDRESS_P (XEXP (x, 0))
1068           || XEXP (x, 0) == virtual_stack_vars_rtx
1069           || XEXP (x, 0) == virtual_incoming_args_rtx)
1070         return x;
1071
1072       if (GET_CODE (XEXP (x, 0)) == PLUS
1073           && (XEXP (XEXP (x, 0), 0) == virtual_stack_vars_rtx
1074               || XEXP (XEXP (x, 0), 0) == virtual_incoming_args_rtx)
1075           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
1076         {
1077           /* This MEM can appear in more than one place,
1078              but its address better not be shared with anything else.  */
1079           if (! x->used)
1080             XEXP (x, 0) = copy_rtx_if_shared (XEXP (x, 0));
1081           x->used = 1;
1082           return x;
1083         }
1084     }
1085
1086   /* This rtx may not be shared.  If it has already been seen,
1087      replace it with a copy of itself.  */
1088
1089   if (x->used)
1090     {
1091       register rtx copy;
1092
1093       copy = rtx_alloc (code);
1094       bcopy (x, copy, (sizeof (*copy) - sizeof (copy->fld)
1095                        + sizeof (copy->fld[0]) * GET_RTX_LENGTH (code)));
1096       x = copy;
1097       copied = 1;
1098     }
1099   x->used = 1;
1100
1101   /* Now scan the subexpressions recursively.
1102      We can store any replaced subexpressions directly into X
1103      since we know X is not shared!  Any vectors in X
1104      must be copied if X was copied.  */
1105
1106   format_ptr = GET_RTX_FORMAT (code);
1107
1108   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1109     {
1110       switch (*format_ptr++)
1111         {
1112         case 'e':
1113           XEXP (x, i) = copy_rtx_if_shared (XEXP (x, i));
1114           break;
1115
1116         case 'E':
1117           if (XVEC (x, i) != NULL)
1118             {
1119               register int j;
1120
1121               if (copied)
1122                 XVEC (x, i) = gen_rtvec_v (XVECLEN (x, i), &XVECEXP (x, i, 0));
1123               for (j = 0; j < XVECLEN (x, i); j++)
1124                 XVECEXP (x, i, j)
1125                   = copy_rtx_if_shared (XVECEXP (x, i, j));
1126             }
1127           break;
1128         }
1129     }
1130   return x;
1131 }
1132
1133 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
1134    to look for shared sub-parts.  */
1135
1136 void
1137 reset_used_flags (x)
1138      rtx x;
1139 {
1140   register int i, j;
1141   register enum rtx_code code;
1142   register char *format_ptr;
1143   int copied = 0;
1144
1145   if (x == 0)
1146     return;
1147
1148   code = GET_CODE (x);
1149
1150   /* These types may be freely shared so we needn't do any reseting
1151      for them.  */
1152
1153   switch (code)
1154     {
1155     case REG:
1156     case QUEUED:
1157     case CONST_INT:
1158     case CONST_DOUBLE:
1159     case SYMBOL_REF:
1160     case CODE_LABEL:
1161     case PC:
1162     case CC0:
1163       return;
1164
1165     case INSN:
1166     case JUMP_INSN:
1167     case CALL_INSN:
1168     case NOTE:
1169     case LABEL_REF:
1170     case BARRIER:
1171       /* The chain of insns is not being copied.  */
1172       return;
1173     }
1174
1175   x->used = 0;
1176
1177   format_ptr = GET_RTX_FORMAT (code);
1178   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1179     {
1180       switch (*format_ptr++)
1181         {
1182         case 'e':
1183           reset_used_flags (XEXP (x, i));
1184           break;
1185
1186         case 'E':
1187           for (j = 0; j < XVECLEN (x, i); j++)
1188             reset_used_flags (XVECEXP (x, i, j));
1189           break;
1190         }
1191     }
1192 }
1193 \f
1194 /* Copy X if necessary so that it won't be altered by changes in OTHER.
1195    Return X or the rtx for the pseudo reg the value of X was copied into.
1196    OTHER must be valid as a SET_DEST.  */
1197
1198 rtx
1199 make_safe_from (x, other)
1200      rtx x, other;
1201 {
1202   while (1)
1203     switch (GET_CODE (other))
1204       {
1205       case SUBREG:
1206         other = SUBREG_REG (other);
1207         break;
1208       case STRICT_LOW_PART:
1209       case SIGN_EXTEND:
1210       case ZERO_EXTEND:
1211         other = XEXP (other, 0);
1212         break;
1213       default:
1214         goto done;
1215       }
1216  done:
1217   if ((GET_CODE (other) == MEM
1218        && ! CONSTANT_P (x)
1219        && GET_CODE (x) != REG
1220        && GET_CODE (x) != SUBREG)
1221       || (GET_CODE (other) == REG
1222           && (REGNO (other) < FIRST_PSEUDO_REGISTER
1223               || reg_mentioned_p (other, x))))
1224     {
1225       rtx temp = gen_reg_rtx (GET_MODE (x));
1226       emit_move_insn (temp, x);
1227       return temp;
1228     }
1229   return x;
1230 }
1231 \f
1232 /* Emission of insns (adding them to the doubly-linked list).  */
1233
1234 /* Return the first insn of the current sequence or current function.  */
1235
1236 rtx
1237 get_insns ()
1238 {
1239   return first_insn;
1240 }
1241
1242 /* Return the last insn emitted in current sequence or current function.  */
1243
1244 rtx
1245 get_last_insn ()
1246 {
1247   return last_insn;
1248 }
1249
1250 /* Specify a new insn as the last in the chain.  */
1251
1252 void
1253 set_last_insn (insn)
1254      rtx insn;
1255 {
1256   if (NEXT_INSN (insn) != 0)
1257     abort ();
1258   last_insn = insn;
1259 }
1260
1261 /* Return the last insn emitted, even if it is in a sequence now pushed.  */
1262
1263 rtx
1264 get_last_insn_anywhere ()
1265 {
1266   struct sequence_stack *stack;
1267   if (last_insn)
1268     return last_insn;
1269   for (stack = sequence_stack; stack; stack = stack->next)
1270     if (stack->last != 0)
1271       return stack->last;
1272   return 0;
1273 }
1274
1275 /* Return a number larger than any instruction's uid in this function.  */
1276
1277 int
1278 get_max_uid ()
1279 {
1280   return cur_insn_uid;
1281 }
1282 \f
1283 /* Return the next insn.  If it is a SEQUENCE, return the first insn
1284    of the sequence.  */
1285
1286 rtx
1287 next_insn (insn)
1288      rtx insn;
1289 {
1290   if (insn)
1291     {
1292       insn = NEXT_INSN (insn);
1293       if (insn && GET_CODE (insn) == INSN
1294           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1295         insn = XVECEXP (PATTERN (insn), 0, 0);
1296     }
1297
1298   return insn;
1299 }
1300
1301 /* Return the previous insn.  If it is a SEQUENCE, return the last insn
1302    of the sequence.  */
1303
1304 rtx
1305 previous_insn (insn)
1306      rtx insn;
1307 {
1308   if (insn)
1309     {
1310       insn = PREV_INSN (insn);
1311       if (insn && GET_CODE (insn) == INSN
1312           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1313         insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
1314     }
1315
1316   return insn;
1317 }
1318
1319 /* Return the next insn after INSN that is not a NOTE.  This routine does not
1320    look inside SEQUENCEs.  */
1321
1322 rtx
1323 next_nonnote_insn (insn)
1324      rtx insn;
1325 {
1326   while (insn)
1327     {
1328       insn = NEXT_INSN (insn);
1329       if (insn == 0 || GET_CODE (insn) != NOTE)
1330         break;
1331     }
1332
1333   return insn;
1334 }
1335
1336 /* Return the previous insn before INSN that is not a NOTE.  This routine does
1337    not look inside SEQUENCEs.  */
1338
1339 rtx
1340 prev_nonnote_insn (insn)
1341      rtx insn;
1342 {
1343   while (insn)
1344     {
1345       insn = PREV_INSN (insn);
1346       if (insn == 0 || GET_CODE (insn) != NOTE)
1347         break;
1348     }
1349
1350   return insn;
1351 }
1352
1353 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
1354    or 0, if there is none.  This routine does not look inside
1355    SEQUENCEs. */
1356
1357 rtx
1358 next_real_insn (insn)
1359      rtx insn;
1360 {
1361   while (insn)
1362     {
1363       insn = NEXT_INSN (insn);
1364       if (insn == 0 || GET_CODE (insn) == INSN
1365           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
1366         break;
1367     }
1368
1369   return insn;
1370 }
1371
1372 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
1373    or 0, if there is none.  This routine does not look inside
1374    SEQUENCEs.  */
1375
1376 rtx
1377 prev_real_insn (insn)
1378      rtx insn;
1379 {
1380   while (insn)
1381     {
1382       insn = PREV_INSN (insn);
1383       if (insn == 0 || GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
1384           || GET_CODE (insn) == JUMP_INSN)
1385         break;
1386     }
1387
1388   return insn;
1389 }
1390
1391 /* Find the next insn after INSN that really does something.  This routine
1392    does not look inside SEQUENCEs.  Until reload has completed, this is the
1393    same as next_real_insn.  */
1394
1395 rtx
1396 next_active_insn (insn)
1397      rtx insn;
1398 {
1399   while (insn)
1400     {
1401       insn = NEXT_INSN (insn);
1402       if (insn == 0
1403           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1404           || (GET_CODE (insn) == INSN
1405               && (! reload_completed
1406                   || (GET_CODE (PATTERN (insn)) != USE
1407                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
1408         break;
1409     }
1410
1411   return insn;
1412 }
1413
1414 /* Find the last insn before INSN that really does something.  This routine
1415    does not look inside SEQUENCEs.  Until reload has completed, this is the
1416    same as prev_real_insn.  */
1417
1418 rtx
1419 prev_active_insn (insn)
1420      rtx insn;
1421 {
1422   while (insn)
1423     {
1424       insn = PREV_INSN (insn);
1425       if (insn == 0
1426           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1427           || (GET_CODE (insn) == INSN
1428               && (! reload_completed
1429                   || (GET_CODE (PATTERN (insn)) != USE
1430                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
1431         break;
1432     }
1433
1434   return insn;
1435 }
1436
1437 /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none.  */
1438
1439 rtx
1440 next_label (insn)
1441      rtx insn;
1442 {
1443   while (insn)
1444     {
1445       insn = NEXT_INSN (insn);
1446       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1447         break;
1448     }
1449
1450   return insn;
1451 }
1452
1453 /* Return the last CODE_LABEL before the insn INSN, or 0 if there is none.  */
1454
1455 rtx
1456 prev_label (insn)
1457      rtx insn;
1458 {
1459   while (insn)
1460     {
1461       insn = PREV_INSN (insn);
1462       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1463         break;
1464     }
1465
1466   return insn;
1467 }
1468 \f
1469 #ifdef HAVE_cc0
1470 /* Return the next insn that uses CC0 after INSN, which is assumed to
1471    set it.  This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
1472    applied to the result of this function should yield INSN).
1473
1474    Normally, this is simply the next insn.  However, if a REG_CC_USER note
1475    is present, it contains the insn that uses CC0.
1476
1477    Return 0 if we can't find the insn.  */
1478
1479 rtx
1480 next_cc0_user (insn)
1481      rtx insn;
1482 {
1483   rtx note = find_reg_note (insn, REG_CC_USER, 0);
1484
1485   if (note)
1486     return XEXP (note, 0);
1487
1488   insn = next_nonnote_insn (insn);
1489   if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
1490     insn = XVECEXP (PATTERN (insn), 0, 0);
1491
1492   if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1493       && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
1494     return insn;
1495
1496   return 0;
1497 }
1498
1499 /* Find the insn that set CC0 for INSN.  Unless INSN has a REG_CC_SETTER
1500    note, it is the previous insn.  */
1501
1502 rtx
1503 prev_cc0_setter (insn)
1504      rtx insn;
1505 {
1506   rtx note = find_reg_note (insn, REG_CC_SETTER, 0);
1507   rtx link;
1508
1509   if (note)
1510     return XEXP (note, 0);
1511
1512   insn = prev_nonnote_insn (insn);
1513   if (! sets_cc0_p (PATTERN (insn)))
1514     abort ();
1515
1516   return insn;
1517 }
1518 #endif
1519 \f
1520 /* Try splitting insns that can be split for better scheduling.
1521    PAT is the pattern which might split.
1522    TRIAL is the insn providing PAT.
1523    BACKWARDS is non-zero if we are scanning insns from last to first.
1524
1525    If this routine succeeds in splitting, it returns the first or last
1526    replacement insn depending on the value of BACKWARDS.  Otherwise, it
1527    returns TRIAL.  If the insn to be returned can be split, it will be.  */
1528
1529 rtx
1530 try_split (pat, trial, backwards)
1531      rtx pat, trial;
1532      int backwards;
1533 {
1534   rtx before = PREV_INSN (trial);
1535   rtx after = NEXT_INSN (trial);
1536   rtx seq = split_insns (pat, trial);
1537   int has_barrier = 0;
1538   rtx tem;
1539
1540   /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
1541      We may need to handle this specially.  */
1542   if (after && GET_CODE (after) == BARRIER)
1543     {
1544       has_barrier = 1;
1545       after = NEXT_INSN (after);
1546     }
1547
1548   if (seq)
1549     {
1550       /* SEQ can either be a SEQUENCE or the pattern of a single insn.
1551          The latter case will normally arise only when being done so that
1552          it, in turn, will be split (SFmode on the 29k is an example).  */
1553       if (GET_CODE (seq) == SEQUENCE)
1554         {
1555           /* If we are splitting a JUMP_INSN, look for the JUMP_INSN in
1556              SEQ and copy our JUMP_LABEL to it.  If JUMP_LABEL is non-zero,
1557              increment the usage count so we don't delete the label.  */
1558           int i;
1559
1560           if (GET_CODE (trial) == JUMP_INSN)
1561             for (i = XVECLEN (seq, 0) - 1; i >= 0; i--)
1562               if (GET_CODE (XVECEXP (seq, 0, i)) == JUMP_INSN)
1563                 {
1564                   JUMP_LABEL (XVECEXP (seq, 0, i)) = JUMP_LABEL (trial);
1565
1566                   if (JUMP_LABEL (trial))
1567                     LABEL_NUSES (JUMP_LABEL (trial))++;
1568                 }
1569
1570           tem = emit_insn_after (seq, before);
1571
1572           delete_insn (trial);
1573           if (has_barrier)
1574             emit_barrier_after (tem);
1575         }
1576       /* Avoid infinite loop if the result matches the original pattern.  */
1577       else if (rtx_equal_p (seq, pat))
1578         return trial;
1579       else
1580         {
1581           PATTERN (trial) = seq;
1582           INSN_CODE (trial) = -1;
1583         }
1584
1585       /* Set TEM to the insn we should return.  */
1586       tem = backwards ? prev_active_insn (after) : next_active_insn (before);
1587       return try_split (PATTERN (tem), tem, backwards);
1588     }
1589
1590   return trial;
1591 }
1592 \f
1593 /* Make and return an INSN rtx, initializing all its slots.
1594    Store PATTERN in the pattern slots.
1595    PAT_FORMALS is an idea that never really went anywhere.  */
1596
1597 rtx
1598 make_insn_raw (pattern, pat_formals)
1599      rtx pattern;
1600      rtvec pat_formals;
1601 {
1602   register rtx insn;
1603
1604   insn = rtx_alloc(INSN);
1605   INSN_UID(insn) = cur_insn_uid++;
1606
1607   PATTERN (insn) = pattern;
1608   INSN_CODE (insn) = -1;
1609   LOG_LINKS(insn) = NULL;
1610   REG_NOTES(insn) = NULL;
1611
1612   return insn;
1613 }
1614
1615 /* Like `make_insn' but make a JUMP_INSN instead of an insn.  */
1616
1617 static rtx
1618 make_jump_insn_raw (pattern, pat_formals)
1619      rtx pattern;
1620      rtvec pat_formals;
1621 {
1622   register rtx insn;
1623
1624   insn = rtx_alloc(JUMP_INSN);
1625   INSN_UID(insn) = cur_insn_uid++;
1626
1627   PATTERN (insn) = pattern;
1628   INSN_CODE (insn) = -1;
1629   LOG_LINKS(insn) = NULL;
1630   REG_NOTES(insn) = NULL;
1631   JUMP_LABEL(insn) = NULL;
1632
1633   return insn;
1634 }
1635 \f
1636 /* Add INSN to the end of the doubly-linked list.
1637    INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE.  */
1638
1639 void
1640 add_insn (insn)
1641      register rtx insn;
1642 {
1643   PREV_INSN (insn) = last_insn;
1644   NEXT_INSN (insn) = 0;
1645
1646   if (NULL != last_insn)
1647     NEXT_INSN (last_insn) = insn;
1648
1649   if (NULL == first_insn)
1650     first_insn = insn;
1651
1652   last_insn = insn;
1653 }
1654
1655 /* Add INSN into the doubly-linked list after insn AFTER.  This should be the
1656    only function called to insert an insn once delay slots have been filled
1657    since only it knows how to update a SEQUENCE.  */
1658
1659 void
1660 add_insn_after (insn, after)
1661      rtx insn, after;
1662 {
1663   rtx next = NEXT_INSN (after);
1664
1665   NEXT_INSN (insn) = next;
1666   PREV_INSN (insn) = after;
1667
1668   if (next)
1669     {
1670       PREV_INSN (next) = insn;
1671       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
1672         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
1673     }
1674   else if (last_insn == after)
1675     last_insn = insn;
1676   else
1677     {
1678       struct sequence_stack *stack = sequence_stack;
1679       /* Scan all pending sequences too.  */
1680       for (; stack; stack = stack->next)
1681         if (after == stack->last)
1682           stack->last = insn;
1683     }
1684
1685   NEXT_INSN (after) = insn;
1686   if (GET_CODE (after) == INSN && GET_CODE (PATTERN (after)) == SEQUENCE)
1687     {
1688       rtx sequence = PATTERN (after);
1689       NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
1690     }
1691 }
1692
1693 /* Delete all insns made since FROM.
1694    FROM becomes the new last instruction.  */
1695
1696 void
1697 delete_insns_since (from)
1698      rtx from;
1699 {
1700   if (from == 0)
1701     first_insn = 0;
1702   else
1703     NEXT_INSN (from) = 0;
1704   last_insn = from;
1705 }
1706
1707 /* Move a consecutive bunch of insns to a different place in the chain.
1708    The insns to be moved are those between FROM and TO.
1709    They are moved to a new position after the insn AFTER.
1710    AFTER must not be FROM or TO or any insn in between.
1711
1712    This function does not know about SEQUENCEs and hence should not be
1713    called after delay-slot filling has been done.  */
1714
1715 void
1716 reorder_insns (from, to, after)
1717      rtx from, to, after;
1718 {
1719   /* Splice this bunch out of where it is now.  */
1720   if (PREV_INSN (from))
1721     NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
1722   if (NEXT_INSN (to))
1723     PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
1724   if (last_insn == to)
1725     last_insn = PREV_INSN (from);
1726   if (first_insn == from)
1727     first_insn = NEXT_INSN (to);
1728
1729   /* Make the new neighbors point to it and it to them.  */
1730   if (NEXT_INSN (after))
1731     PREV_INSN (NEXT_INSN (after)) = to;
1732
1733   NEXT_INSN (to) = NEXT_INSN (after);
1734   PREV_INSN (from) = after;
1735   NEXT_INSN (after) = from;
1736   if (after == last_insn)
1737     last_insn = to;
1738 }
1739
1740 /* Return the line note insn preceding INSN.  */
1741
1742 static rtx
1743 find_line_note (insn)
1744      rtx insn;
1745 {
1746   if (no_line_numbers)
1747     return 0;
1748
1749   for (; insn; insn = PREV_INSN (insn))
1750     if (GET_CODE (insn) == NOTE
1751         && NOTE_LINE_NUMBER (insn) >= 0)
1752       break;
1753
1754   return insn;
1755 }
1756
1757 /* Like reorder_insns, but inserts line notes to preserve the line numbers
1758    of the moved insns when debugging.  This may insert a note between AFTER
1759    and FROM, and another one after TO.  */
1760
1761 void
1762 reorder_insns_with_line_notes (from, to, after)
1763      rtx from, to, after;
1764 {
1765   rtx from_line = find_line_note (from);
1766   rtx after_line = find_line_note (after);
1767
1768   reorder_insns (from, to, after);
1769
1770   if (from_line == after_line)
1771     return;
1772
1773   if (from_line)
1774     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
1775                           NOTE_LINE_NUMBER (from_line),
1776                           after);
1777   if (after_line)
1778     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
1779                           NOTE_LINE_NUMBER (after_line),
1780                           to);
1781 }
1782 \f
1783 /* Emit an insn of given code and pattern
1784    at a specified place within the doubly-linked list.  */
1785
1786 /* Make an instruction with body PATTERN
1787    and output it before the instruction BEFORE.  */
1788
1789 rtx
1790 emit_insn_before (pattern, before)
1791      register rtx pattern, before;
1792 {
1793   register rtx insn = before;
1794
1795   if (GET_CODE (pattern) == SEQUENCE)
1796     {
1797       register int i;
1798
1799       for (i = 0; i < XVECLEN (pattern, 0); i++)
1800         {
1801           insn = XVECEXP (pattern, 0, i);
1802           add_insn_after (insn, PREV_INSN (before));
1803         }
1804       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
1805         sequence_result[XVECLEN (pattern, 0)] = pattern;
1806     }
1807   else
1808     {
1809       insn = make_insn_raw (pattern, 0);
1810       add_insn_after (insn, PREV_INSN (before));
1811     }
1812
1813   return insn;
1814 }
1815
1816 /* Make an instruction with body PATTERN and code JUMP_INSN
1817    and output it before the instruction BEFORE.  */
1818
1819 rtx
1820 emit_jump_insn_before (pattern, before)
1821      register rtx pattern, before;
1822 {
1823   register rtx insn;
1824
1825   if (GET_CODE (pattern) == SEQUENCE)
1826     insn = emit_insn_before (pattern, before);
1827   else
1828     {
1829       insn = make_jump_insn_raw (pattern, 0);
1830       add_insn_after (insn, PREV_INSN (before));
1831     }
1832
1833   return insn;
1834 }
1835
1836 /* Make an instruction with body PATTERN and code CALL_INSN
1837    and output it before the instruction BEFORE.  */
1838
1839 rtx
1840 emit_call_insn_before (pattern, before)
1841      register rtx pattern, before;
1842 {
1843   rtx insn = emit_insn_before (pattern, before);
1844   PUT_CODE (insn, CALL_INSN);
1845   return insn;
1846 }
1847
1848 /* Make an insn of code BARRIER
1849    and output it before the insn AFTER.  */
1850
1851 rtx
1852 emit_barrier_before (before)
1853      register rtx before;
1854 {
1855   register rtx insn = rtx_alloc (BARRIER);
1856
1857   INSN_UID (insn) = cur_insn_uid++;
1858
1859   add_insn_after (insn, PREV_INSN (before));
1860   return insn;
1861 }
1862
1863 /* Emit a note of subtype SUBTYPE before the insn BEFORE.  */
1864
1865 rtx
1866 emit_note_before (subtype, before)
1867      int subtype;
1868      rtx before;
1869 {
1870   register rtx note = rtx_alloc (NOTE);
1871   INSN_UID (note) = cur_insn_uid++;
1872   NOTE_SOURCE_FILE (note) = 0;
1873   NOTE_LINE_NUMBER (note) = subtype;
1874
1875   add_insn_after (note, PREV_INSN (before));
1876   return note;
1877 }
1878 \f
1879 /* Make an insn of code INSN with body PATTERN
1880    and output it after the insn AFTER.  */
1881
1882 rtx
1883 emit_insn_after (pattern, after)
1884      register rtx pattern, after;
1885 {
1886   register rtx insn = after;
1887
1888   if (GET_CODE (pattern) == SEQUENCE)
1889     {
1890       register int i;
1891
1892       for (i = 0; i < XVECLEN (pattern, 0); i++)
1893         {
1894           insn = XVECEXP (pattern, 0, i);
1895           add_insn_after (insn, after);
1896           after = insn;
1897         }
1898       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
1899         sequence_result[XVECLEN (pattern, 0)] = pattern;
1900     }
1901   else
1902     {
1903       insn = make_insn_raw (pattern, 0);
1904       add_insn_after (insn, after);
1905     }
1906
1907   return insn;
1908 }
1909
1910 /* Make an insn of code JUMP_INSN with body PATTERN
1911    and output it after the insn AFTER.  */
1912
1913 rtx
1914 emit_jump_insn_after (pattern, after)
1915      register rtx pattern, after;
1916 {
1917   register rtx insn;
1918
1919   if (GET_CODE (pattern) == SEQUENCE)
1920     insn = emit_insn_after (pattern, after);
1921   else
1922     {
1923       insn = make_jump_insn_raw (pattern, 0);
1924       add_insn_after (insn, after);
1925     }
1926
1927   return insn;
1928 }
1929
1930 /* Make an insn of code BARRIER
1931    and output it after the insn AFTER.  */
1932
1933 rtx
1934 emit_barrier_after (after)
1935      register rtx after;
1936 {
1937   register rtx insn = rtx_alloc (BARRIER);
1938
1939   INSN_UID (insn) = cur_insn_uid++;
1940
1941   add_insn_after (insn, after);
1942   return insn;
1943 }
1944
1945 /* Emit the label LABEL after the insn AFTER.  */
1946
1947 rtx
1948 emit_label_after (label, after)
1949      rtx label, after;
1950 {
1951   /* This can be called twice for the same label
1952      as a result of the confusion that follows a syntax error!
1953      So make it harmless.  */
1954   if (INSN_UID (label) == 0)
1955     {
1956       INSN_UID (label) = cur_insn_uid++;
1957       add_insn_after (label, after);
1958     }
1959
1960   return label;
1961 }
1962
1963 /* Emit a note of subtype SUBTYPE after the insn AFTER.  */
1964
1965 rtx
1966 emit_note_after (subtype, after)
1967      int subtype;
1968      rtx after;
1969 {
1970   register rtx note = rtx_alloc (NOTE);
1971   INSN_UID (note) = cur_insn_uid++;
1972   NOTE_SOURCE_FILE (note) = 0;
1973   NOTE_LINE_NUMBER (note) = subtype;
1974   add_insn_after (note, after);
1975   return note;
1976 }
1977
1978 /* Emit a line note for FILE and LINE after the insn AFTER.  */
1979
1980 rtx
1981 emit_line_note_after (file, line, after)
1982      char *file;
1983      int line;
1984      rtx after;
1985 {
1986   register rtx note;
1987
1988   if (no_line_numbers && line > 0)
1989     {
1990       cur_insn_uid++;
1991       return 0;
1992     }
1993
1994   note  = rtx_alloc (NOTE);
1995   INSN_UID (note) = cur_insn_uid++;
1996   NOTE_SOURCE_FILE (note) = file;
1997   NOTE_LINE_NUMBER (note) = line;
1998   add_insn_after (note, after);
1999   return note;
2000 }
2001 \f
2002 /* Make an insn of code INSN with pattern PATTERN
2003    and add it to the end of the doubly-linked list.
2004    If PATTERN is a SEQUENCE, take the elements of it
2005    and emit an insn for each element.
2006
2007    Returns the last insn emitted.  */
2008
2009 rtx
2010 emit_insn (pattern)
2011      rtx pattern;
2012 {
2013   rtx insn = last_insn;
2014
2015   if (GET_CODE (pattern) == SEQUENCE)
2016     {
2017       register int i;
2018
2019       for (i = 0; i < XVECLEN (pattern, 0); i++)
2020         {
2021           insn = XVECEXP (pattern, 0, i);
2022           add_insn (insn);
2023         }
2024       if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2025         sequence_result[XVECLEN (pattern, 0)] = pattern;
2026     }
2027   else
2028     {
2029       insn = make_insn_raw (pattern, NULL);
2030       add_insn (insn);
2031     }
2032
2033   return insn;
2034 }
2035
2036 /* Emit the insns in a chain starting with INSN.
2037    Return the last insn emitted.  */
2038
2039 rtx
2040 emit_insns (insn)
2041      rtx insn;
2042 {
2043   rtx last = 0;
2044
2045   while (insn)
2046     {
2047       rtx next = NEXT_INSN (insn);
2048       add_insn (insn);
2049       last = insn;
2050       insn = next;
2051     }
2052
2053   return last;
2054 }
2055
2056 /* Emit the insns in a chain starting with INSN and place them in front of
2057    the insn BEFORE.  Return the last insn emitted.  */
2058
2059 rtx
2060 emit_insns_before (insn, before)
2061      rtx insn;
2062      rtx before;
2063 {
2064   rtx last = 0;
2065
2066   while (insn)
2067     {
2068       rtx next = NEXT_INSN (insn);
2069       add_insn_after (insn, PREV_INSN (before));
2070       last = insn;
2071       insn = next;
2072     }
2073
2074   return last;
2075 }
2076
2077 /* Make an insn of code JUMP_INSN with pattern PATTERN
2078    and add it to the end of the doubly-linked list.  */
2079
2080 rtx
2081 emit_jump_insn (pattern)
2082      rtx pattern;
2083 {
2084   if (GET_CODE (pattern) == SEQUENCE)
2085     return emit_insn (pattern);
2086   else
2087     {
2088       register rtx insn = make_jump_insn_raw (pattern, NULL);
2089       add_insn (insn);
2090       return insn;
2091     }
2092 }
2093
2094 /* Make an insn of code CALL_INSN with pattern PATTERN
2095    and add it to the end of the doubly-linked list.  */
2096
2097 rtx
2098 emit_call_insn (pattern)
2099      rtx pattern;
2100 {
2101   if (GET_CODE (pattern) == SEQUENCE)
2102     return emit_insn (pattern);
2103   else
2104     {
2105       register rtx insn = make_insn_raw (pattern, NULL);
2106       add_insn (insn);
2107       PUT_CODE (insn, CALL_INSN);
2108       return insn;
2109     }
2110 }
2111
2112 /* Add the label LABEL to the end of the doubly-linked list.  */
2113
2114 rtx
2115 emit_label (label)
2116      rtx label;
2117 {
2118   /* This can be called twice for the same label
2119      as a result of the confusion that follows a syntax error!
2120      So make it harmless.  */
2121   if (INSN_UID (label) == 0)
2122     {
2123       INSN_UID (label) = cur_insn_uid++;
2124       add_insn (label);
2125     }
2126   return label;
2127 }
2128
2129 /* Make an insn of code BARRIER
2130    and add it to the end of the doubly-linked list.  */
2131
2132 rtx
2133 emit_barrier ()
2134 {
2135   register rtx barrier = rtx_alloc (BARRIER);
2136   INSN_UID (barrier) = cur_insn_uid++;
2137   add_insn (barrier);
2138   return barrier;
2139 }
2140
2141 /* Make an insn of code NOTE
2142    with data-fields specified by FILE and LINE
2143    and add it to the end of the doubly-linked list,
2144    but only if line-numbers are desired for debugging info.  */
2145
2146 rtx
2147 emit_line_note (file, line)
2148      char *file;
2149      int line;
2150 {
2151   emit_filename = file;
2152   emit_lineno = line;
2153
2154 #if 0
2155   if (no_line_numbers)
2156     return 0;
2157 #endif
2158
2159   return emit_note (file, line);
2160 }
2161
2162 /* Make an insn of code NOTE
2163    with data-fields specified by FILE and LINE
2164    and add it to the end of the doubly-linked list.
2165    If it is a line-number NOTE, omit it if it matches the previous one.  */
2166
2167 rtx
2168 emit_note (file, line)
2169      char *file;
2170      int line;
2171 {
2172   register rtx note;
2173
2174   if (line > 0)
2175     {
2176       if (file && last_filename && !strcmp (file, last_filename)
2177           && line == last_linenum)
2178         return 0;
2179       last_filename = file;
2180       last_linenum = line;
2181     }
2182
2183   if (no_line_numbers && line > 0)
2184     {
2185       cur_insn_uid++;
2186       return 0;
2187     }
2188
2189   note = rtx_alloc (NOTE);
2190   INSN_UID (note) = cur_insn_uid++;
2191   NOTE_SOURCE_FILE (note) = file;
2192   NOTE_LINE_NUMBER (note) = line;
2193   add_insn (note);
2194   return note;
2195 }
2196
2197 /* Emit a NOTE, and don't omit it even if LINE it the previous note.  */
2198
2199 rtx
2200 emit_line_note_force (file, line)
2201      char *file;
2202      int line;
2203 {
2204   last_linenum = -1;
2205   return emit_line_note (file, line);
2206 }
2207
2208 /* Cause next statement to emit a line note even if the line number
2209    has not changed.  This is used at the beginning of a function.  */
2210
2211 void
2212 force_next_line_note ()
2213 {
2214   last_linenum = -1;
2215 }
2216 \f
2217 /* Return an indication of which type of insn should have X as a body.
2218    The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN.  */
2219
2220 enum rtx_code
2221 classify_insn (x)
2222      rtx x;
2223 {
2224   if (GET_CODE (x) == CODE_LABEL)
2225     return CODE_LABEL;
2226   if (GET_CODE (x) == CALL)
2227     return CALL_INSN;
2228   if (GET_CODE (x) == RETURN)
2229     return JUMP_INSN;
2230   if (GET_CODE (x) == SET)
2231     {
2232       if (SET_DEST (x) == pc_rtx)
2233         return JUMP_INSN;
2234       else if (GET_CODE (SET_SRC (x)) == CALL)
2235         return CALL_INSN;
2236       else
2237         return INSN;
2238     }
2239   if (GET_CODE (x) == PARALLEL)
2240     {
2241       register int j;
2242       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
2243         if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
2244           return CALL_INSN;
2245         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2246                  && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
2247           return JUMP_INSN;
2248         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2249                  && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
2250           return CALL_INSN;
2251     }
2252   return INSN;
2253 }
2254
2255 /* Emit the rtl pattern X as an appropriate kind of insn.
2256    If X is a label, it is simply added into the insn chain.  */
2257
2258 rtx
2259 emit (x)
2260      rtx x;
2261 {
2262   enum rtx_code code = classify_insn (x);
2263
2264   if (code == CODE_LABEL)
2265     return emit_label (x);
2266   else if (code == INSN)
2267     return emit_insn (x);
2268   else if (code == JUMP_INSN)
2269     {
2270       register rtx insn = emit_jump_insn (x);
2271       if (simplejump_p (insn) || GET_CODE (x) == RETURN)
2272         return emit_barrier ();
2273       return insn;
2274     }
2275   else if (code == CALL_INSN)
2276     return emit_call_insn (x);
2277   else
2278     abort ();
2279 }
2280 \f
2281 /* Begin emitting insns to a sequence which can be packaged in an RTL_EXPR.  */
2282
2283 void
2284 start_sequence ()
2285 {
2286   struct sequence_stack *tem;
2287
2288   if (sequence_element_free_list)
2289     {
2290       /* Reuse a previously-saved struct sequence_stack.  */
2291       tem = sequence_element_free_list;
2292       sequence_element_free_list = tem->next;
2293     }
2294   else
2295     tem = (struct sequence_stack *) permalloc (sizeof (struct sequence_stack));
2296
2297   tem->next = sequence_stack;
2298   tem->first = first_insn;
2299   tem->last = last_insn;
2300
2301   sequence_stack = tem;
2302
2303   first_insn = 0;
2304   last_insn = 0;
2305 }
2306
2307 /* Set up the insn chain starting with FIRST
2308    as the current sequence, saving the previously current one.  */
2309
2310 void
2311 push_to_sequence (first)
2312      rtx first;
2313 {
2314   rtx last;
2315
2316   start_sequence ();
2317
2318   for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last));
2319
2320   first_insn = first;
2321   last_insn = last;
2322 }
2323
2324 /* After emitting to a sequence, restore previous saved state.
2325
2326    To get the contents of the sequence just made,
2327    you must call `gen_sequence' *before* calling here.  */
2328
2329 void
2330 end_sequence ()
2331 {
2332   struct sequence_stack *tem = sequence_stack;
2333
2334   first_insn = tem->first;
2335   last_insn = tem->last;
2336   sequence_stack = tem->next;
2337
2338   tem->next = sequence_element_free_list;
2339   sequence_element_free_list = tem;
2340 }
2341
2342 /* Return 1 if currently emitting into a sequence.  */
2343
2344 int
2345 in_sequence_p ()
2346 {
2347   return sequence_stack != 0;
2348 }
2349
2350 /* Generate a SEQUENCE rtx containing the insns already emitted
2351    to the current sequence.
2352
2353    This is how the gen_... function from a DEFINE_EXPAND
2354    constructs the SEQUENCE that it returns.  */
2355
2356 rtx
2357 gen_sequence ()
2358 {
2359   rtx result;
2360   rtx tem;
2361   rtvec newvec;
2362   int i;
2363   int len;
2364
2365   /* Count the insns in the chain.  */
2366   len = 0;
2367   for (tem = first_insn; tem; tem = NEXT_INSN (tem))
2368     len++;
2369
2370   /* If only one insn, return its pattern rather than a SEQUENCE.
2371      (Now that we cache SEQUENCE expressions, it isn't worth special-casing
2372      the case of an empty list.)  */
2373   if (len == 1
2374       && (GET_CODE (first_insn) == INSN
2375           || GET_CODE (first_insn) == JUMP_INSN
2376           || GET_CODE (first_insn) == CALL_INSN))
2377     return PATTERN (first_insn);
2378
2379   /* Put them in a vector.  See if we already have a SEQUENCE of the
2380      appropriate length around.  */
2381   if (len < SEQUENCE_RESULT_SIZE && (result = sequence_result[len]) != 0)
2382     sequence_result[len] = 0;
2383   else
2384     {
2385       /* Ensure that this rtl goes in saveable_obstack, since we may be
2386          caching it.  */
2387       int in_current_obstack = rtl_in_saveable_obstack ();
2388       result = gen_rtx (SEQUENCE, VOIDmode, rtvec_alloc (len));
2389       if (in_current_obstack)
2390         rtl_in_current_obstack ();
2391     }
2392
2393   for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
2394     XVECEXP (result, 0, i) = tem;
2395
2396   return result;
2397 }
2398 \f
2399 /* Set up regno_reg_rtx, reg_rtx_no and regno_pointer_flag
2400    according to the chain of insns starting with FIRST.
2401
2402    Also set cur_insn_uid to exceed the largest uid in that chain.
2403
2404    This is used when an inline function's rtl is saved
2405    and passed to rest_of_compilation later.  */
2406
2407 static void restore_reg_data_1 ();
2408
2409 void
2410 restore_reg_data (first)
2411      rtx first;
2412 {
2413   register rtx insn;
2414   int i;
2415   register int max_uid = 0;
2416
2417   for (insn = first; insn; insn = NEXT_INSN (insn))
2418     {
2419       if (INSN_UID (insn) >= max_uid)
2420         max_uid = INSN_UID (insn);
2421
2422       switch (GET_CODE (insn))
2423         {
2424         case NOTE:
2425         case CODE_LABEL:
2426         case BARRIER:
2427           break;
2428
2429         case JUMP_INSN:
2430         case CALL_INSN:
2431         case INSN:
2432           restore_reg_data_1 (PATTERN (insn));
2433           break;
2434         }
2435     }
2436
2437   /* Don't duplicate the uids already in use.  */
2438   cur_insn_uid = max_uid + 1;
2439
2440   /* If any regs are missing, make them up.  
2441
2442      ??? word_mode is not necessarily the right mode.  Most likely these REGs
2443      are never used.  At some point this should be checked.  */
2444
2445   for (i = FIRST_PSEUDO_REGISTER; i < reg_rtx_no; i++)
2446     if (regno_reg_rtx[i] == 0)
2447       regno_reg_rtx[i] = gen_rtx (REG, word_mode, i);
2448 }
2449
2450 static void
2451 restore_reg_data_1 (orig)
2452      rtx orig;
2453 {
2454   register rtx x = orig;
2455   register int i;
2456   register enum rtx_code code;
2457   register char *format_ptr;
2458
2459   code = GET_CODE (x);
2460
2461   switch (code)
2462     {
2463     case QUEUED:
2464     case CONST_INT:
2465     case CONST_DOUBLE:
2466     case SYMBOL_REF:
2467     case CODE_LABEL:
2468     case PC:
2469     case CC0:
2470     case LABEL_REF:
2471       return;
2472
2473     case REG:
2474       if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
2475         {
2476           /* Make sure regno_pointer_flag and regno_reg_rtx are large
2477              enough to have an element for this pseudo reg number.  */
2478           if (REGNO (x) >= reg_rtx_no)
2479             {
2480               reg_rtx_no = REGNO (x);
2481
2482               if (reg_rtx_no >= regno_pointer_flag_length)
2483                 {
2484                   int newlen = MAX (regno_pointer_flag_length * 2,
2485                                     reg_rtx_no + 30);
2486                   rtx *new1;
2487                   char *new = (char *) oballoc (newlen);
2488                   bzero (new, newlen);
2489                   bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
2490
2491                   new1 = (rtx *) oballoc (newlen * sizeof (rtx));
2492                   bzero (new1, newlen * sizeof (rtx));
2493                   bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
2494
2495                   regno_pointer_flag = new;
2496                   regno_reg_rtx = new1;
2497                   regno_pointer_flag_length = newlen;
2498                 }
2499               reg_rtx_no ++;
2500             }
2501           regno_reg_rtx[REGNO (x)] = x;
2502         }
2503       return;
2504
2505     case MEM:
2506       if (GET_CODE (XEXP (x, 0)) == REG)
2507         mark_reg_pointer (XEXP (x, 0));
2508       restore_reg_data_1 (XEXP (x, 0));
2509       return;
2510     }
2511
2512   /* Now scan the subexpressions recursively.  */
2513
2514   format_ptr = GET_RTX_FORMAT (code);
2515
2516   for (i = 0; i < GET_RTX_LENGTH (code); i++)
2517     {
2518       switch (*format_ptr++)
2519         {
2520         case 'e':
2521           restore_reg_data_1 (XEXP (x, i));
2522           break;
2523
2524         case 'E':
2525           if (XVEC (x, i) != NULL)
2526             {
2527               register int j;
2528
2529               for (j = 0; j < XVECLEN (x, i); j++)
2530                 restore_reg_data_1 (XVECEXP (x, i, j));
2531             }
2532           break;
2533         }
2534     }
2535 }
2536 \f
2537 /* Initialize data structures and variables in this file
2538    before generating rtl for each function.  */
2539
2540 void
2541 init_emit ()
2542 {
2543   int i;
2544
2545   first_insn = NULL;
2546   last_insn = NULL;
2547   cur_insn_uid = 1;
2548   reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
2549   last_linenum = 0;
2550   last_filename = 0;
2551   first_label_num = label_num;
2552   last_label_num = 0;
2553
2554   /* Clear the start_sequence/gen_sequence cache.  */
2555   sequence_element_free_list = 0;
2556   for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
2557     sequence_result[i] = 0;
2558
2559   /* Init the tables that describe all the pseudo regs.  */
2560
2561   regno_pointer_flag_length = LAST_VIRTUAL_REGISTER + 101;
2562
2563   regno_pointer_flag 
2564     = (char *) oballoc (regno_pointer_flag_length);
2565   bzero (regno_pointer_flag, regno_pointer_flag_length);
2566
2567   regno_reg_rtx 
2568     = (rtx *) oballoc (regno_pointer_flag_length * sizeof (rtx));
2569   bzero (regno_reg_rtx, regno_pointer_flag_length * sizeof (rtx));
2570
2571   /* Put copies of all the virtual register rtx into regno_reg_rtx.  */
2572   regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
2573   regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
2574   regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
2575   regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
2576 }
2577
2578 /* Create some permanent unique rtl objects shared between all functions.
2579    LINE_NUMBERS is nonzero if line numbers are to be generated.  */
2580
2581 void
2582 init_emit_once (line_numbers)
2583      int line_numbers;
2584 {
2585   int i;
2586   enum machine_mode mode;
2587
2588   no_line_numbers = ! line_numbers;
2589
2590   sequence_stack = NULL;
2591
2592   /* Create the unique rtx's for certain rtx codes and operand values.  */
2593
2594   pc_rtx = gen_rtx (PC, VOIDmode);
2595   cc0_rtx = gen_rtx (CC0, VOIDmode);
2596
2597   /* Don't use gen_rtx here since gen_rtx in this case
2598      tries to use these variables.  */
2599   for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
2600     {
2601       const_int_rtx[i + MAX_SAVED_CONST_INT] = rtx_alloc (CONST_INT);
2602       PUT_MODE (const_int_rtx[i + MAX_SAVED_CONST_INT], VOIDmode);
2603       INTVAL (const_int_rtx[i + MAX_SAVED_CONST_INT]) = i;
2604     }
2605
2606   /* These four calls obtain some of the rtx expressions made above.  */
2607   const0_rtx = gen_rtx (CONST_INT, VOIDmode, 0);
2608   const1_rtx = gen_rtx (CONST_INT, VOIDmode, 1);
2609   const2_rtx = gen_rtx (CONST_INT, VOIDmode, 2);
2610   constm1_rtx = gen_rtx (CONST_INT, VOIDmode, -1);
2611
2612   /* This will usually be one of the above constants, but may be a new rtx.  */
2613   const_true_rtx = gen_rtx (CONST_INT, VOIDmode, STORE_FLAG_VALUE);
2614
2615   dconst0 = REAL_VALUE_ATOF ("0");
2616   dconst1 = REAL_VALUE_ATOF ("1");
2617   dconst2 = REAL_VALUE_ATOF ("2");
2618   dconstm1 = REAL_VALUE_ATOF ("-1");
2619
2620   for (i = 0; i <= 2; i++)
2621     {
2622       for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
2623            mode = GET_MODE_WIDER_MODE (mode))
2624         {
2625           rtx tem = rtx_alloc (CONST_DOUBLE);
2626           union real_extract u;
2627
2628           bzero (&u, sizeof u);  /* Zero any holes in a structure.  */
2629           u.d = i == 0 ? dconst0 : i == 1 ? dconst1 : dconst2;
2630
2631           bcopy (&u, &CONST_DOUBLE_LOW (tem), sizeof u);
2632           CONST_DOUBLE_MEM (tem) = cc0_rtx;
2633           PUT_MODE (tem, mode);
2634
2635           const_tiny_rtx[i][(int) mode] = tem;
2636         }
2637
2638       const_tiny_rtx[i][(int) VOIDmode] = gen_rtx (CONST_INT, VOIDmode, i);
2639
2640       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2641            mode = GET_MODE_WIDER_MODE (mode))
2642         const_tiny_rtx[i][(int) mode] = gen_rtx (CONST_INT, VOIDmode, i);
2643     }
2644
2645   stack_pointer_rtx = gen_rtx (REG, Pmode, STACK_POINTER_REGNUM);
2646   frame_pointer_rtx = gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM);
2647
2648   if (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM)
2649     arg_pointer_rtx = frame_pointer_rtx;
2650   else if (STACK_POINTER_REGNUM == ARG_POINTER_REGNUM)
2651     arg_pointer_rtx = stack_pointer_rtx;
2652   else
2653     arg_pointer_rtx = gen_rtx (REG, Pmode, ARG_POINTER_REGNUM);
2654
2655   /* Create the virtual registers.  Do so here since the following objects
2656      might reference them.  */
2657
2658   virtual_incoming_args_rtx = gen_rtx (REG, Pmode,
2659                                        VIRTUAL_INCOMING_ARGS_REGNUM);
2660   virtual_stack_vars_rtx = gen_rtx (REG, Pmode,
2661                                     VIRTUAL_STACK_VARS_REGNUM);
2662   virtual_stack_dynamic_rtx = gen_rtx (REG, Pmode,
2663                                        VIRTUAL_STACK_DYNAMIC_REGNUM);
2664   virtual_outgoing_args_rtx = gen_rtx (REG, Pmode,
2665                                        VIRTUAL_OUTGOING_ARGS_REGNUM);
2666
2667 #ifdef STRUCT_VALUE
2668   struct_value_rtx = STRUCT_VALUE;
2669 #else
2670   struct_value_rtx = gen_rtx (REG, Pmode, STRUCT_VALUE_REGNUM);
2671 #endif
2672
2673 #ifdef STRUCT_VALUE_INCOMING
2674   struct_value_incoming_rtx = STRUCT_VALUE_INCOMING;
2675 #else
2676 #ifdef STRUCT_VALUE_INCOMING_REGNUM
2677   struct_value_incoming_rtx
2678     = gen_rtx (REG, Pmode, STRUCT_VALUE_INCOMING_REGNUM);
2679 #else
2680   struct_value_incoming_rtx = struct_value_rtx;
2681 #endif
2682 #endif
2683
2684 #ifdef STATIC_CHAIN_REGNUM
2685   static_chain_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_REGNUM);
2686
2687 #ifdef STATIC_CHAIN_INCOMING_REGNUM
2688   if (STATIC_CHAIN_INCOMING_REGNUM != STATIC_CHAIN_REGNUM)
2689     static_chain_incoming_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_INCOMING_REGNUM);
2690   else
2691 #endif
2692     static_chain_incoming_rtx = static_chain_rtx;
2693 #endif
2694
2695 #ifdef STATIC_CHAIN
2696   static_chain_rtx = STATIC_CHAIN;
2697
2698 #ifdef STATIC_CHAIN_INCOMING
2699   static_chain_incoming_rtx = STATIC_CHAIN_INCOMING;
2700 #else
2701   static_chain_incoming_rtx = static_chain_rtx;
2702 #endif
2703 #endif
2704
2705 #ifdef PIC_OFFSET_TABLE_REGNUM
2706   pic_offset_table_rtx = gen_rtx (REG, Pmode, PIC_OFFSET_TABLE_REGNUM);
2707 #endif
2708 }