OSDN Git Service

* t-sh (MULTILIB_EXCEPTIONS): Set to ml.
[pf3gnuchains/gcc-fork.git] / gcc / emit-rtl.c
1 /* Emit RTL for the GNU C-Compiler expander.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 /* Middle-to-low level generation of rtx code and insns.
24
25    This file contains the functions `gen_rtx', `gen_reg_rtx'
26    and `gen_label_rtx' that are the usual ways of creating rtl
27    expressions for most purposes.
28
29    It also has the functions for creating insns and linking
30    them in the doubly-linked chain.
31
32    The patterns of the insns are created by machine-dependent
33    routines in insn-emit.c, which is generated automatically from
34    the machine description.  These routines use `gen_rtx' to make
35    the individual rtx's of the pattern; what is machine dependent
36    is the kind of rtx's they make and what arguments they use.  */
37
38 #include "config.h"
39 #include "system.h"
40 #include "toplev.h"
41 #include "rtl.h"
42 #include "tree.h"
43 #include "tm_p.h"
44 #include "flags.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "regs.h"
48 #include "hard-reg-set.h"
49 #include "hashtab.h"
50 #include "insn-config.h"
51 #include "recog.h"
52 #include "real.h"
53 #include "obstack.h"
54 #include "bitmap.h"
55 #include "basic-block.h"
56 #include "ggc.h"
57 #include "debug.h"
58 #include "langhooks.h"
59
60 /* Commonly used modes.  */
61
62 enum machine_mode byte_mode;    /* Mode whose width is BITS_PER_UNIT.  */
63 enum machine_mode word_mode;    /* Mode whose width is BITS_PER_WORD.  */
64 enum machine_mode double_mode;  /* Mode whose width is DOUBLE_TYPE_SIZE.  */
65 enum machine_mode ptr_mode;     /* Mode whose width is POINTER_SIZE.  */
66
67
68 /* This is *not* reset after each function.  It gives each CODE_LABEL
69    in the entire compilation a unique label number.  */
70
71 static int label_num = 1;
72
73 /* Highest label number in current function.
74    Zero means use the value of label_num instead.
75    This is nonzero only when belatedly compiling an inline function.  */
76
77 static int last_label_num;
78
79 /* Value label_num had when set_new_first_and_last_label_number was called.
80    If label_num has not changed since then, last_label_num is valid.  */
81
82 static int base_label_num;
83
84 /* Nonzero means do not generate NOTEs for source line numbers.  */
85
86 static int no_line_numbers;
87
88 /* Commonly used rtx's, so that we only need space for one copy.
89    These are initialized once for the entire compilation.
90    All of these are unique; no other rtx-object will be equal to any
91    of these.  */
92
93 rtx global_rtl[GR_MAX];
94
95 /* Commonly used RTL for hard registers.  These objects are not necessarily
96    unique, so we allocate them separately from global_rtl.  They are
97    initialized once per compilation unit, then copied into regno_reg_rtx
98    at the beginning of each function.  */
99 static GTY(()) rtx static_regno_reg_rtx[FIRST_PSEUDO_REGISTER];
100
101 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
102    the values of 0, 1, and 2.  For the integer entries and VOIDmode, we
103    record a copy of const[012]_rtx.  */
104
105 rtx const_tiny_rtx[3][(int) MAX_MACHINE_MODE];
106
107 rtx const_true_rtx;
108
109 REAL_VALUE_TYPE dconst0;
110 REAL_VALUE_TYPE dconst1;
111 REAL_VALUE_TYPE dconst2;
112 REAL_VALUE_TYPE dconstm1;
113
114 /* All references to the following fixed hard registers go through
115    these unique rtl objects.  On machines where the frame-pointer and
116    arg-pointer are the same register, they use the same unique object.
117
118    After register allocation, other rtl objects which used to be pseudo-regs
119    may be clobbered to refer to the frame-pointer register.
120    But references that were originally to the frame-pointer can be
121    distinguished from the others because they contain frame_pointer_rtx.
122
123    When to use frame_pointer_rtx and hard_frame_pointer_rtx is a little
124    tricky: until register elimination has taken place hard_frame_pointer_rtx
125    should be used if it is being set, and frame_pointer_rtx otherwise.  After
126    register elimination hard_frame_pointer_rtx should always be used.
127    On machines where the two registers are same (most) then these are the
128    same.
129
130    In an inline procedure, the stack and frame pointer rtxs may not be
131    used for anything else.  */
132 rtx struct_value_rtx;           /* (REG:Pmode STRUCT_VALUE_REGNUM) */
133 rtx struct_value_incoming_rtx;  /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */
134 rtx static_chain_rtx;           /* (REG:Pmode STATIC_CHAIN_REGNUM) */
135 rtx static_chain_incoming_rtx;  /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) */
136 rtx pic_offset_table_rtx;       /* (REG:Pmode PIC_OFFSET_TABLE_REGNUM) */
137
138 /* This is used to implement __builtin_return_address for some machines.
139    See for instance the MIPS port.  */
140 rtx return_address_pointer_rtx; /* (REG:Pmode RETURN_ADDRESS_POINTER_REGNUM) */
141
142 /* We make one copy of (const_int C) where C is in
143    [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
144    to save space during the compilation and simplify comparisons of
145    integers.  */
146
147 rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
148
149 /* A hash table storing CONST_INTs whose absolute value is greater
150    than MAX_SAVED_CONST_INT.  */
151
152 static GTY ((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
153      htab_t const_int_htab;
154
155 /* A hash table storing memory attribute structures.  */
156 static GTY ((if_marked ("ggc_marked_p"), param_is (struct mem_attrs)))
157      htab_t mem_attrs_htab;
158
159 /* A hash table storing all CONST_DOUBLEs.  */
160 static GTY ((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
161      htab_t const_double_htab;
162
163 #define first_insn (cfun->emit->x_first_insn)
164 #define last_insn (cfun->emit->x_last_insn)
165 #define cur_insn_uid (cfun->emit->x_cur_insn_uid)
166 #define last_linenum (cfun->emit->x_last_linenum)
167 #define last_filename (cfun->emit->x_last_filename)
168 #define first_label_num (cfun->emit->x_first_label_num)
169
170 static rtx make_jump_insn_raw           PARAMS ((rtx));
171 static rtx make_call_insn_raw           PARAMS ((rtx));
172 static rtx find_line_note               PARAMS ((rtx));
173 static rtx change_address_1             PARAMS ((rtx, enum machine_mode, rtx,
174                                                  int));
175 static void unshare_all_rtl_1           PARAMS ((rtx));
176 static void unshare_all_decls           PARAMS ((tree));
177 static void reset_used_decls            PARAMS ((tree));
178 static void mark_label_nuses            PARAMS ((rtx));
179 static hashval_t const_int_htab_hash    PARAMS ((const void *));
180 static int const_int_htab_eq            PARAMS ((const void *,
181                                                  const void *));
182 static hashval_t const_double_htab_hash PARAMS ((const void *));
183 static int const_double_htab_eq         PARAMS ((const void *,
184                                                  const void *));
185 static rtx lookup_const_double          PARAMS ((rtx));
186 static hashval_t mem_attrs_htab_hash    PARAMS ((const void *));
187 static int mem_attrs_htab_eq            PARAMS ((const void *,
188                                                  const void *));
189 static mem_attrs *get_mem_attrs         PARAMS ((HOST_WIDE_INT, tree, rtx,
190                                                  rtx, unsigned int,
191                                                  enum machine_mode));
192 static tree component_ref_for_mem_expr  PARAMS ((tree));
193 static rtx gen_const_vector_0           PARAMS ((enum machine_mode));
194
195 /* Probability of the conditional branch currently proceeded by try_split.
196    Set to -1 otherwise.  */
197 int split_branch_probability = -1;
198 \f
199 /* Returns a hash code for X (which is a really a CONST_INT).  */
200
201 static hashval_t
202 const_int_htab_hash (x)
203      const void *x;
204 {
205   return (hashval_t) INTVAL ((struct rtx_def *) x);
206 }
207
208 /* Returns non-zero if the value represented by X (which is really a
209    CONST_INT) is the same as that given by Y (which is really a
210    HOST_WIDE_INT *).  */
211
212 static int
213 const_int_htab_eq (x, y)
214      const void *x;
215      const void *y;
216 {
217   return (INTVAL ((rtx) x) == *((const HOST_WIDE_INT *) y));
218 }
219
220 /* Returns a hash code for X (which is really a CONST_DOUBLE).  */
221 static hashval_t
222 const_double_htab_hash (x)
223      const void *x;
224 {
225   hashval_t h = 0;
226   size_t i;
227   rtx value = (rtx) x;
228
229   for (i = 0; i < sizeof(CONST_DOUBLE_FORMAT)-1; i++)
230     h ^= XWINT (value, i);
231   return h;
232 }
233
234 /* Returns non-zero if the value represented by X (really a ...)
235    is the same as that represented by Y (really a ...) */
236 static int
237 const_double_htab_eq (x, y)
238      const void *x;
239      const void *y;
240 {
241   rtx a = (rtx)x, b = (rtx)y;
242   size_t i;
243
244   if (GET_MODE (a) != GET_MODE (b))
245     return 0;
246   for (i = 0; i < sizeof(CONST_DOUBLE_FORMAT)-1; i++)
247     if (XWINT (a, i) != XWINT (b, i))
248       return 0;
249
250   return 1;
251 }
252
253 /* Returns a hash code for X (which is a really a mem_attrs *).  */
254
255 static hashval_t
256 mem_attrs_htab_hash (x)
257      const void *x;
258 {
259   mem_attrs *p = (mem_attrs *) x;
260
261   return (p->alias ^ (p->align * 1000)
262           ^ ((p->offset ? INTVAL (p->offset) : 0) * 50000)
263           ^ ((p->size ? INTVAL (p->size) : 0) * 2500000)
264           ^ (size_t) p->expr);
265 }
266
267 /* Returns non-zero if the value represented by X (which is really a
268    mem_attrs *) is the same as that given by Y (which is also really a
269    mem_attrs *).  */
270
271 static int
272 mem_attrs_htab_eq (x, y)
273      const void *x;
274      const void *y;
275 {
276   mem_attrs *p = (mem_attrs *) x;
277   mem_attrs *q = (mem_attrs *) y;
278
279   return (p->alias == q->alias && p->expr == q->expr && p->offset == q->offset
280           && p->size == q->size && p->align == q->align);
281 }
282
283 /* Allocate a new mem_attrs structure and insert it into the hash table if
284    one identical to it is not already in the table.  We are doing this for
285    MEM of mode MODE.  */
286
287 static mem_attrs *
288 get_mem_attrs (alias, expr, offset, size, align, mode)
289      HOST_WIDE_INT alias;
290      tree expr;
291      rtx offset;
292      rtx size;
293      unsigned int align;
294      enum machine_mode mode;
295 {
296   mem_attrs attrs;
297   void **slot;
298
299   /* If everything is the default, we can just return zero.  */
300   if (alias == 0 && expr == 0 && offset == 0
301       && (size == 0
302           || (mode != BLKmode && GET_MODE_SIZE (mode) == INTVAL (size)))
303       && (align == BITS_PER_UNIT
304           || (STRICT_ALIGNMENT
305               && mode != BLKmode && align == GET_MODE_ALIGNMENT (mode))))
306     return 0;
307
308   attrs.alias = alias;
309   attrs.expr = expr;
310   attrs.offset = offset;
311   attrs.size = size;
312   attrs.align = align;
313
314   slot = htab_find_slot (mem_attrs_htab, &attrs, INSERT);
315   if (*slot == 0)
316     {
317       *slot = ggc_alloc (sizeof (mem_attrs));
318       memcpy (*slot, &attrs, sizeof (mem_attrs));
319     }
320
321   return *slot;
322 }
323
324 /* Generate a new REG rtx.  Make sure ORIGINAL_REGNO is set properly, and
325    don't attempt to share with the various global pieces of rtl (such as
326    frame_pointer_rtx).  */
327
328 rtx
329 gen_raw_REG (mode, regno)
330      enum machine_mode mode;
331      int regno;
332 {
333   rtx x = gen_rtx_raw_REG (mode, regno);
334   ORIGINAL_REGNO (x) = regno;
335   return x;
336 }
337
338 /* There are some RTL codes that require special attention; the generation
339    functions do the raw handling.  If you add to this list, modify
340    special_rtx in gengenrtl.c as well.  */
341
342 rtx
343 gen_rtx_CONST_INT (mode, arg)
344      enum machine_mode mode ATTRIBUTE_UNUSED;
345      HOST_WIDE_INT arg;
346 {
347   void **slot;
348
349   if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
350     return const_int_rtx[arg + MAX_SAVED_CONST_INT];
351
352 #if STORE_FLAG_VALUE != 1 && STORE_FLAG_VALUE != -1
353   if (const_true_rtx && arg == STORE_FLAG_VALUE)
354     return const_true_rtx;
355 #endif
356
357   /* Look up the CONST_INT in the hash table.  */
358   slot = htab_find_slot_with_hash (const_int_htab, &arg,
359                                    (hashval_t) arg, INSERT);
360   if (*slot == 0)
361     *slot = gen_rtx_raw_CONST_INT (VOIDmode, arg);
362
363   return (rtx) *slot;
364 }
365
366 rtx
367 gen_int_mode (c, mode)
368      HOST_WIDE_INT c;
369      enum machine_mode mode;
370 {
371   return GEN_INT (trunc_int_for_mode (c, mode));
372 }
373
374 /* CONST_DOUBLEs might be created from pairs of integers, or from
375    REAL_VALUE_TYPEs.  Also, their length is known only at run time,
376    so we cannot use gen_rtx_raw_CONST_DOUBLE.  */
377
378 /* Determine whether REAL, a CONST_DOUBLE, already exists in the
379    hash table.  If so, return its counterpart; otherwise add it
380    to the hash table and return it.  */
381 static rtx
382 lookup_const_double (real)
383      rtx real;
384 {
385   void **slot = htab_find_slot (const_double_htab, real, INSERT);
386   if (*slot == 0)
387     *slot = real;
388
389   return (rtx) *slot;
390 }
391
392 /* Return a CONST_DOUBLE rtx for a floating-point value specified by
393    VALUE in mode MODE.  */
394 rtx
395 const_double_from_real_value (value, mode)
396      REAL_VALUE_TYPE value;
397      enum machine_mode mode;
398 {
399   rtx real = rtx_alloc (CONST_DOUBLE);
400   PUT_MODE (real, mode);
401
402   memcpy (&CONST_DOUBLE_LOW (real), &value, sizeof (REAL_VALUE_TYPE));
403
404   return lookup_const_double (real);
405 }
406
407 /* Return a CONST_DOUBLE or CONST_INT for a value specified as a pair
408    of ints: I0 is the low-order word and I1 is the high-order word.
409    Do not use this routine for non-integer modes; convert to
410    REAL_VALUE_TYPE and use CONST_DOUBLE_FROM_REAL_VALUE.  */
411
412 rtx
413 immed_double_const (i0, i1, mode)
414      HOST_WIDE_INT i0, i1;
415      enum machine_mode mode;
416 {
417   rtx value;
418   unsigned int i;
419
420   if (mode != VOIDmode)
421     {
422       int width;
423       if (GET_MODE_CLASS (mode) != MODE_INT
424           && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT
425           /* We can get a 0 for an error mark.  */
426           && GET_MODE_CLASS (mode) != MODE_VECTOR_INT
427           && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
428         abort ();
429
430       /* We clear out all bits that don't belong in MODE, unless they and
431          our sign bit are all one.  So we get either a reasonable negative
432          value or a reasonable unsigned value for this mode.  */
433       width = GET_MODE_BITSIZE (mode);
434       if (width < HOST_BITS_PER_WIDE_INT
435           && ((i0 & ((HOST_WIDE_INT) (-1) << (width - 1)))
436               != ((HOST_WIDE_INT) (-1) << (width - 1))))
437         i0 &= ((HOST_WIDE_INT) 1 << width) - 1, i1 = 0;
438       else if (width == HOST_BITS_PER_WIDE_INT
439                && ! (i1 == ~0 && i0 < 0))
440         i1 = 0;
441       else if (width > 2 * HOST_BITS_PER_WIDE_INT)
442         /* We cannot represent this value as a constant.  */
443         abort ();
444
445       /* If this would be an entire word for the target, but is not for
446          the host, then sign-extend on the host so that the number will
447          look the same way on the host that it would on the target.
448
449          For example, when building a 64 bit alpha hosted 32 bit sparc
450          targeted compiler, then we want the 32 bit unsigned value -1 to be
451          represented as a 64 bit value -1, and not as 0x00000000ffffffff.
452          The latter confuses the sparc backend.  */
453
454       if (width < HOST_BITS_PER_WIDE_INT
455           && (i0 & ((HOST_WIDE_INT) 1 << (width - 1))))
456         i0 |= ((HOST_WIDE_INT) (-1) << width);
457
458       /* If MODE fits within HOST_BITS_PER_WIDE_INT, always use a
459          CONST_INT.
460
461          ??? Strictly speaking, this is wrong if we create a CONST_INT for
462          a large unsigned constant with the size of MODE being
463          HOST_BITS_PER_WIDE_INT and later try to interpret that constant
464          in a wider mode.  In that case we will mis-interpret it as a
465          negative number.
466
467          Unfortunately, the only alternative is to make a CONST_DOUBLE for
468          any constant in any mode if it is an unsigned constant larger
469          than the maximum signed integer in an int on the host.  However,
470          doing this will break everyone that always expects to see a
471          CONST_INT for SImode and smaller.
472
473          We have always been making CONST_INTs in this case, so nothing
474          new is being broken.  */
475
476       if (width <= HOST_BITS_PER_WIDE_INT)
477         i1 = (i0 < 0) ? ~(HOST_WIDE_INT) 0 : 0;
478     }
479
480   /* If this integer fits in one word, return a CONST_INT.  */
481   if ((i1 == 0 && i0 >= 0) || (i1 == ~0 && i0 < 0))
482     return GEN_INT (i0);
483
484   /* We use VOIDmode for integers.  */
485   value = rtx_alloc (CONST_DOUBLE);
486   PUT_MODE (value, VOIDmode);
487
488   CONST_DOUBLE_LOW (value) = i0;
489   CONST_DOUBLE_HIGH (value) = i1;
490
491   for (i = 2; i < (sizeof CONST_DOUBLE_FORMAT - 1); i++)
492     XWINT (value, i) = 0;
493
494   return lookup_const_double (value);
495 }
496
497 rtx
498 gen_rtx_REG (mode, regno)
499      enum machine_mode mode;
500      unsigned int regno;
501 {
502   /* In case the MD file explicitly references the frame pointer, have
503      all such references point to the same frame pointer.  This is
504      used during frame pointer elimination to distinguish the explicit
505      references to these registers from pseudos that happened to be
506      assigned to them.
507
508      If we have eliminated the frame pointer or arg pointer, we will
509      be using it as a normal register, for example as a spill
510      register.  In such cases, we might be accessing it in a mode that
511      is not Pmode and therefore cannot use the pre-allocated rtx.
512
513      Also don't do this when we are making new REGs in reload, since
514      we don't want to get confused with the real pointers.  */
515
516   if (mode == Pmode && !reload_in_progress)
517     {
518       if (regno == FRAME_POINTER_REGNUM)
519         return frame_pointer_rtx;
520 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
521       if (regno == HARD_FRAME_POINTER_REGNUM)
522         return hard_frame_pointer_rtx;
523 #endif
524 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM && HARD_FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
525       if (regno == ARG_POINTER_REGNUM)
526         return arg_pointer_rtx;
527 #endif
528 #ifdef RETURN_ADDRESS_POINTER_REGNUM
529       if (regno == RETURN_ADDRESS_POINTER_REGNUM)
530         return return_address_pointer_rtx;
531 #endif
532       if (regno == PIC_OFFSET_TABLE_REGNUM
533           && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
534         return pic_offset_table_rtx;
535       if (regno == STACK_POINTER_REGNUM)
536         return stack_pointer_rtx;
537     }
538
539 #if 0
540   /* If the per-function register table has been set up, try to re-use
541      an existing entry in that table to avoid useless generation of RTL.
542
543      This code is disabled for now until we can fix the various backends
544      which depend on having non-shared hard registers in some cases.   Long
545      term we want to re-enable this code as it can significantly cut down
546      on the amount of useless RTL that gets generated.  */
547   if (cfun
548       && cfun->emit
549       && regno_reg_rtx
550       && regno < FIRST_PSEUDO_REGISTER
551       && reg_raw_mode[regno] == mode)
552     return regno_reg_rtx[regno];
553 #endif
554
555   return gen_raw_REG (mode, regno);
556 }
557
558 rtx
559 gen_rtx_MEM (mode, addr)
560      enum machine_mode mode;
561      rtx addr;
562 {
563   rtx rt = gen_rtx_raw_MEM (mode, addr);
564
565   /* This field is not cleared by the mere allocation of the rtx, so
566      we clear it here.  */
567   MEM_ATTRS (rt) = 0;
568
569   return rt;
570 }
571
572 rtx
573 gen_rtx_SUBREG (mode, reg, offset)
574      enum machine_mode mode;
575      rtx reg;
576      int offset;
577 {
578   /* This is the most common failure type.
579      Catch it early so we can see who does it.  */
580   if ((offset % GET_MODE_SIZE (mode)) != 0)
581     abort ();
582
583   /* This check isn't usable right now because combine will
584      throw arbitrary crap like a CALL into a SUBREG in
585      gen_lowpart_for_combine so we must just eat it.  */
586 #if 0
587   /* Check for this too.  */
588   if (offset >= GET_MODE_SIZE (GET_MODE (reg)))
589     abort ();
590 #endif
591   return gen_rtx_raw_SUBREG (mode, reg, offset);
592 }
593
594 /* Generate a SUBREG representing the least-significant part of REG if MODE
595    is smaller than mode of REG, otherwise paradoxical SUBREG.  */
596
597 rtx
598 gen_lowpart_SUBREG (mode, reg)
599      enum machine_mode mode;
600      rtx reg;
601 {
602   enum machine_mode inmode;
603
604   inmode = GET_MODE (reg);
605   if (inmode == VOIDmode)
606     inmode = mode;
607   return gen_rtx_SUBREG (mode, reg,
608                          subreg_lowpart_offset (mode, inmode));
609 }
610 \f
611 /* rtx gen_rtx (code, mode, [element1, ..., elementn])
612 **
613 **          This routine generates an RTX of the size specified by
614 **      <code>, which is an RTX code.   The RTX structure is initialized
615 **      from the arguments <element1> through <elementn>, which are
616 **      interpreted according to the specific RTX type's format.   The
617 **      special machine mode associated with the rtx (if any) is specified
618 **      in <mode>.
619 **
620 **          gen_rtx can be invoked in a way which resembles the lisp-like
621 **      rtx it will generate.   For example, the following rtx structure:
622 **
623 **            (plus:QI (mem:QI (reg:SI 1))
624 **                     (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))
625 **
626 **              ...would be generated by the following C code:
627 **
628 **              gen_rtx (PLUS, QImode,
629 **                  gen_rtx (MEM, QImode,
630 **                      gen_rtx (REG, SImode, 1)),
631 **                  gen_rtx (MEM, QImode,
632 **                      gen_rtx (PLUS, SImode,
633 **                          gen_rtx (REG, SImode, 2),
634 **                          gen_rtx (REG, SImode, 3)))),
635 */
636
637 /*VARARGS2*/
638 rtx
639 gen_rtx VPARAMS ((enum rtx_code code, enum machine_mode mode, ...))
640 {
641   int i;                /* Array indices...                     */
642   const char *fmt;      /* Current rtx's format...              */
643   rtx rt_val;           /* RTX to return to caller...           */
644
645   VA_OPEN (p, mode);
646   VA_FIXEDARG (p, enum rtx_code, code);
647   VA_FIXEDARG (p, enum machine_mode, mode);
648
649   switch (code)
650     {
651     case CONST_INT:
652       rt_val = gen_rtx_CONST_INT (mode, va_arg (p, HOST_WIDE_INT));
653       break;
654
655     case CONST_DOUBLE:
656       {
657         HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
658         HOST_WIDE_INT arg1 = va_arg (p, HOST_WIDE_INT);
659
660         rt_val = immed_double_const (arg0, arg1, mode);
661       }
662       break;
663
664     case REG:
665       rt_val = gen_rtx_REG (mode, va_arg (p, int));
666       break;
667
668     case MEM:
669       rt_val = gen_rtx_MEM (mode, va_arg (p, rtx));
670       break;
671
672     default:
673       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
674       rt_val->mode = mode;              /* Store the machine mode...  */
675
676       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
677       for (i = 0; i < GET_RTX_LENGTH (code); i++)
678         {
679           switch (*fmt++)
680             {
681             case '0':           /* Unused field.  */
682               break;
683
684             case 'i':           /* An integer?  */
685               XINT (rt_val, i) = va_arg (p, int);
686               break;
687
688             case 'w':           /* A wide integer? */
689               XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
690               break;
691
692             case 's':           /* A string?  */
693               XSTR (rt_val, i) = va_arg (p, char *);
694               break;
695
696             case 'e':           /* An expression?  */
697             case 'u':           /* An insn?  Same except when printing.  */
698               XEXP (rt_val, i) = va_arg (p, rtx);
699               break;
700
701             case 'E':           /* An RTX vector?  */
702               XVEC (rt_val, i) = va_arg (p, rtvec);
703               break;
704
705             case 'b':           /* A bitmap? */
706               XBITMAP (rt_val, i) = va_arg (p, bitmap);
707               break;
708
709             case 't':           /* A tree? */
710               XTREE (rt_val, i) = va_arg (p, tree);
711               break;
712
713             default:
714               abort ();
715             }
716         }
717       break;
718     }
719
720   VA_CLOSE (p);
721   return rt_val;
722 }
723
724 /* gen_rtvec (n, [rt1, ..., rtn])
725 **
726 **          This routine creates an rtvec and stores within it the
727 **      pointers to rtx's which are its arguments.
728 */
729
730 /*VARARGS1*/
731 rtvec
732 gen_rtvec VPARAMS ((int n, ...))
733 {
734   int i, save_n;
735   rtx *vector;
736
737   VA_OPEN (p, n);
738   VA_FIXEDARG (p, int, n);
739
740   if (n == 0)
741     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
742
743   vector = (rtx *) alloca (n * sizeof (rtx));
744
745   for (i = 0; i < n; i++)
746     vector[i] = va_arg (p, rtx);
747
748   /* The definition of VA_* in K&R C causes `n' to go out of scope.  */
749   save_n = n;
750   VA_CLOSE (p);
751
752   return gen_rtvec_v (save_n, vector);
753 }
754
755 rtvec
756 gen_rtvec_v (n, argp)
757      int n;
758      rtx *argp;
759 {
760   int i;
761   rtvec rt_val;
762
763   if (n == 0)
764     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
765
766   rt_val = rtvec_alloc (n);     /* Allocate an rtvec...                 */
767
768   for (i = 0; i < n; i++)
769     rt_val->elem[i] = *argp++;
770
771   return rt_val;
772 }
773 \f
774 /* Generate a REG rtx for a new pseudo register of mode MODE.
775    This pseudo is assigned the next sequential register number.  */
776
777 rtx
778 gen_reg_rtx (mode)
779      enum machine_mode mode;
780 {
781   struct function *f = cfun;
782   rtx val;
783
784   /* Don't let anything called after initial flow analysis create new
785      registers.  */
786   if (no_new_pseudos)
787     abort ();
788
789   if (generating_concat_p
790       && (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
791           || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT))
792     {
793       /* For complex modes, don't make a single pseudo.
794          Instead, make a CONCAT of two pseudos.
795          This allows noncontiguous allocation of the real and imaginary parts,
796          which makes much better code.  Besides, allocating DCmode
797          pseudos overstrains reload on some machines like the 386.  */
798       rtx realpart, imagpart;
799       int size = GET_MODE_UNIT_SIZE (mode);
800       enum machine_mode partmode
801         = mode_for_size (size * BITS_PER_UNIT,
802                          (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
803                           ? MODE_FLOAT : MODE_INT),
804                          0);
805
806       realpart = gen_reg_rtx (partmode);
807       imagpart = gen_reg_rtx (partmode);
808       return gen_rtx_CONCAT (mode, realpart, imagpart);
809     }
810
811   /* Make sure regno_pointer_align, regno_decl, and regno_reg_rtx are large
812      enough to have an element for this pseudo reg number.  */
813
814   if (reg_rtx_no == f->emit->regno_pointer_align_length)
815     {
816       int old_size = f->emit->regno_pointer_align_length;
817       char *new;
818       rtx *new1;
819       tree *new2;
820
821       new = ggc_realloc (f->emit->regno_pointer_align, old_size * 2);
822       memset (new + old_size, 0, old_size);
823       f->emit->regno_pointer_align = (unsigned char *) new;
824
825       new1 = (rtx *) ggc_realloc (f->emit->x_regno_reg_rtx,
826                                   old_size * 2 * sizeof (rtx));
827       memset (new1 + old_size, 0, old_size * sizeof (rtx));
828       regno_reg_rtx = new1;
829
830       new2 = (tree *) ggc_realloc (f->emit->regno_decl,
831                                    old_size * 2 * sizeof (tree));
832       memset (new2 + old_size, 0, old_size * sizeof (tree));
833       f->emit->regno_decl = new2;
834
835       f->emit->regno_pointer_align_length = old_size * 2;
836     }
837
838   val = gen_raw_REG (mode, reg_rtx_no);
839   regno_reg_rtx[reg_rtx_no++] = val;
840   return val;
841 }
842
843 /* Identify REG (which may be a CONCAT) as a user register.  */
844
845 void
846 mark_user_reg (reg)
847      rtx reg;
848 {
849   if (GET_CODE (reg) == CONCAT)
850     {
851       REG_USERVAR_P (XEXP (reg, 0)) = 1;
852       REG_USERVAR_P (XEXP (reg, 1)) = 1;
853     }
854   else if (GET_CODE (reg) == REG)
855     REG_USERVAR_P (reg) = 1;
856   else
857     abort ();
858 }
859
860 /* Identify REG as a probable pointer register and show its alignment
861    as ALIGN, if nonzero.  */
862
863 void
864 mark_reg_pointer (reg, align)
865      rtx reg;
866      int align;
867 {
868   if (! REG_POINTER (reg))
869     {
870       REG_POINTER (reg) = 1;
871
872       if (align)
873         REGNO_POINTER_ALIGN (REGNO (reg)) = align;
874     }
875   else if (align && align < REGNO_POINTER_ALIGN (REGNO (reg)))
876     /* We can no-longer be sure just how aligned this pointer is */
877     REGNO_POINTER_ALIGN (REGNO (reg)) = align;
878 }
879
880 /* Return 1 plus largest pseudo reg number used in the current function.  */
881
882 int
883 max_reg_num ()
884 {
885   return reg_rtx_no;
886 }
887
888 /* Return 1 + the largest label number used so far in the current function.  */
889
890 int
891 max_label_num ()
892 {
893   if (last_label_num && label_num == base_label_num)
894     return last_label_num;
895   return label_num;
896 }
897
898 /* Return first label number used in this function (if any were used).  */
899
900 int
901 get_first_label_num ()
902 {
903   return first_label_num;
904 }
905 \f
906 /* Return the final regno of X, which is a SUBREG of a hard
907    register.  */
908 int
909 subreg_hard_regno (x, check_mode)
910      rtx x;
911      int check_mode;
912 {
913   enum machine_mode mode = GET_MODE (x);
914   unsigned int byte_offset, base_regno, final_regno;
915   rtx reg = SUBREG_REG (x);
916
917   /* This is where we attempt to catch illegal subregs
918      created by the compiler.  */
919   if (GET_CODE (x) != SUBREG
920       || GET_CODE (reg) != REG)
921     abort ();
922   base_regno = REGNO (reg);
923   if (base_regno >= FIRST_PSEUDO_REGISTER)
924     abort ();
925   if (check_mode && ! HARD_REGNO_MODE_OK (base_regno, GET_MODE (reg)))
926     abort ();
927
928   /* Catch non-congruent offsets too.  */
929   byte_offset = SUBREG_BYTE (x);
930   if ((byte_offset % GET_MODE_SIZE (mode)) != 0)
931     abort ();
932
933   final_regno = subreg_regno (x);
934
935   return final_regno;
936 }
937
938 /* Return a value representing some low-order bits of X, where the number
939    of low-order bits is given by MODE.  Note that no conversion is done
940    between floating-point and fixed-point values, rather, the bit
941    representation is returned.
942
943    This function handles the cases in common between gen_lowpart, below,
944    and two variants in cse.c and combine.c.  These are the cases that can
945    be safely handled at all points in the compilation.
946
947    If this is not a case we can handle, return 0.  */
948
949 rtx
950 gen_lowpart_common (mode, x)
951      enum machine_mode mode;
952      rtx x;
953 {
954   int msize = GET_MODE_SIZE (mode);
955   int xsize = GET_MODE_SIZE (GET_MODE (x));
956   int offset = 0;
957
958   if (GET_MODE (x) == mode)
959     return x;
960
961   /* MODE must occupy no more words than the mode of X.  */
962   if (GET_MODE (x) != VOIDmode
963       && ((msize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
964           > ((xsize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
965     return 0;
966
967   offset = subreg_lowpart_offset (mode, GET_MODE (x));
968
969   if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
970       && (GET_MODE_CLASS (mode) == MODE_INT
971           || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
972     {
973       /* If we are getting the low-order part of something that has been
974          sign- or zero-extended, we can either just use the object being
975          extended or make a narrower extension.  If we want an even smaller
976          piece than the size of the object being extended, call ourselves
977          recursively.
978
979          This case is used mostly by combine and cse.  */
980
981       if (GET_MODE (XEXP (x, 0)) == mode)
982         return XEXP (x, 0);
983       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
984         return gen_lowpart_common (mode, XEXP (x, 0));
985       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
986         return gen_rtx_fmt_e (GET_CODE (x), mode, XEXP (x, 0));
987     }
988   else if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG
989            || GET_CODE (x) == CONCAT)
990     return simplify_gen_subreg (mode, x, GET_MODE (x), offset);
991   /* If X is a CONST_INT or a CONST_DOUBLE, extract the appropriate bits
992      from the low-order part of the constant.  */
993   else if ((GET_MODE_CLASS (mode) == MODE_INT
994             || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
995            && GET_MODE (x) == VOIDmode
996            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
997     {
998       /* If MODE is twice the host word size, X is already the desired
999          representation.  Otherwise, if MODE is wider than a word, we can't
1000          do this.  If MODE is exactly a word, return just one CONST_INT.  */
1001
1002       if (GET_MODE_BITSIZE (mode) >= 2 * HOST_BITS_PER_WIDE_INT)
1003         return x;
1004       else if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1005         return 0;
1006       else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
1007         return (GET_CODE (x) == CONST_INT ? x
1008                 : GEN_INT (CONST_DOUBLE_LOW (x)));
1009       else
1010         {
1011           /* MODE must be narrower than HOST_BITS_PER_WIDE_INT.  */
1012           HOST_WIDE_INT val = (GET_CODE (x) == CONST_INT ? INTVAL (x)
1013                                : CONST_DOUBLE_LOW (x));
1014
1015           /* Sign extend to HOST_WIDE_INT.  */
1016           val = trunc_int_for_mode (val, mode);
1017
1018           return (GET_CODE (x) == CONST_INT && INTVAL (x) == val ? x
1019                   : GEN_INT (val));
1020         }
1021     }
1022
1023   /* The floating-point emulator can handle all conversions between
1024      FP and integer operands.  This simplifies reload because it
1025      doesn't have to deal with constructs like (subreg:DI
1026      (const_double:SF ...)) or (subreg:DF (const_int ...)).  */
1027   /* Single-precision floats are always 32-bits and double-precision
1028      floats are always 64-bits.  */
1029
1030   else if (GET_MODE_CLASS (mode) == MODE_FLOAT
1031            && GET_MODE_BITSIZE (mode) == 32
1032            && GET_CODE (x) == CONST_INT)
1033     {
1034       REAL_VALUE_TYPE r;
1035       HOST_WIDE_INT i;
1036
1037       i = INTVAL (x);
1038       r = REAL_VALUE_FROM_TARGET_SINGLE (i);
1039       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
1040     }
1041   else if (GET_MODE_CLASS (mode) == MODE_FLOAT
1042            && GET_MODE_BITSIZE (mode) == 64
1043            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
1044            && GET_MODE (x) == VOIDmode)
1045     {
1046       REAL_VALUE_TYPE r;
1047       HOST_WIDE_INT i[2];
1048       HOST_WIDE_INT low, high;
1049
1050       if (GET_CODE (x) == CONST_INT)
1051         {
1052           low = INTVAL (x);
1053           high = low >> (HOST_BITS_PER_WIDE_INT - 1);
1054         }
1055       else
1056         {
1057           low = CONST_DOUBLE_LOW (x);
1058           high = CONST_DOUBLE_HIGH (x);
1059         }
1060
1061 #if HOST_BITS_PER_WIDE_INT == 32
1062       /* REAL_VALUE_TARGET_DOUBLE takes the addressing order of the
1063          target machine.  */
1064       if (WORDS_BIG_ENDIAN)
1065         i[0] = high, i[1] = low;
1066       else
1067         i[0] = low, i[1] = high;
1068 #else
1069       i[0] = low;
1070 #endif
1071
1072       r = REAL_VALUE_FROM_TARGET_DOUBLE (i);
1073       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
1074     }
1075   else if ((GET_MODE_CLASS (mode) == MODE_INT
1076             || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
1077            && GET_CODE (x) == CONST_DOUBLE
1078            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1079     {
1080       REAL_VALUE_TYPE r;
1081       long i[4];  /* Only the low 32 bits of each 'long' are used.  */
1082       int endian = WORDS_BIG_ENDIAN ? 1 : 0;
1083
1084       /* Convert 'r' into an array of four 32-bit words in target word
1085          order.  */
1086       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1087       switch (GET_MODE_BITSIZE (GET_MODE (x)))
1088         {
1089         case 32:
1090           REAL_VALUE_TO_TARGET_SINGLE (r, i[3 * endian]);
1091           i[1] = 0;
1092           i[2] = 0;
1093           i[3 - 3 * endian] = 0;
1094           break;
1095         case 64:
1096           REAL_VALUE_TO_TARGET_DOUBLE (r, i + 2 * endian);
1097           i[2 - 2 * endian] = 0;
1098           i[3 - 2 * endian] = 0;
1099           break;
1100         case 96:
1101           REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, i + endian);
1102           i[3 - 3 * endian] = 0;
1103           break;
1104         case 128:
1105           REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, i);
1106           break;
1107         default:
1108           abort ();
1109         }
1110       /* Now, pack the 32-bit elements of the array into a CONST_DOUBLE
1111          and return it.  */
1112 #if HOST_BITS_PER_WIDE_INT == 32
1113       return immed_double_const (i[3 * endian], i[1 + endian], mode);
1114 #else
1115       if (HOST_BITS_PER_WIDE_INT != 64)
1116         abort ();
1117
1118       return immed_double_const ((((unsigned long) i[3 * endian])
1119                                   | ((HOST_WIDE_INT) i[1 + endian] << 32)),
1120                                  (((unsigned long) i[2 - endian])
1121                                   | ((HOST_WIDE_INT) i[3 - 3 * endian] << 32)),
1122                                  mode);
1123 #endif
1124     }
1125
1126   /* Otherwise, we can't do this.  */
1127   return 0;
1128 }
1129 \f
1130 /* Return the real part (which has mode MODE) of a complex value X.
1131    This always comes at the low address in memory.  */
1132
1133 rtx
1134 gen_realpart (mode, x)
1135      enum machine_mode mode;
1136      rtx x;
1137 {
1138   if (WORDS_BIG_ENDIAN
1139       && GET_MODE_BITSIZE (mode) < BITS_PER_WORD
1140       && REG_P (x)
1141       && REGNO (x) < FIRST_PSEUDO_REGISTER)
1142     internal_error
1143       ("can't access real part of complex value in hard register");
1144   else if (WORDS_BIG_ENDIAN)
1145     return gen_highpart (mode, x);
1146   else
1147     return gen_lowpart (mode, x);
1148 }
1149
1150 /* Return the imaginary part (which has mode MODE) of a complex value X.
1151    This always comes at the high address in memory.  */
1152
1153 rtx
1154 gen_imagpart (mode, x)
1155      enum machine_mode mode;
1156      rtx x;
1157 {
1158   if (WORDS_BIG_ENDIAN)
1159     return gen_lowpart (mode, x);
1160   else if (! WORDS_BIG_ENDIAN
1161            && GET_MODE_BITSIZE (mode) < BITS_PER_WORD
1162            && REG_P (x)
1163            && REGNO (x) < FIRST_PSEUDO_REGISTER)
1164     internal_error
1165       ("can't access imaginary part of complex value in hard register");
1166   else
1167     return gen_highpart (mode, x);
1168 }
1169
1170 /* Return 1 iff X, assumed to be a SUBREG,
1171    refers to the real part of the complex value in its containing reg.
1172    Complex values are always stored with the real part in the first word,
1173    regardless of WORDS_BIG_ENDIAN.  */
1174
1175 int
1176 subreg_realpart_p (x)
1177      rtx x;
1178 {
1179   if (GET_CODE (x) != SUBREG)
1180     abort ();
1181
1182   return ((unsigned int) SUBREG_BYTE (x)
1183           < GET_MODE_UNIT_SIZE (GET_MODE (SUBREG_REG (x))));
1184 }
1185 \f
1186 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a value,
1187    return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
1188    least-significant part of X.
1189    MODE specifies how big a part of X to return;
1190    it usually should not be larger than a word.
1191    If X is a MEM whose address is a QUEUED, the value may be so also.  */
1192
1193 rtx
1194 gen_lowpart (mode, x)
1195      enum machine_mode mode;
1196      rtx x;
1197 {
1198   rtx result = gen_lowpart_common (mode, x);
1199
1200   if (result)
1201     return result;
1202   else if (GET_CODE (x) == REG)
1203     {
1204       /* Must be a hard reg that's not valid in MODE.  */
1205       result = gen_lowpart_common (mode, copy_to_reg (x));
1206       if (result == 0)
1207         abort ();
1208       return result;
1209     }
1210   else if (GET_CODE (x) == MEM)
1211     {
1212       /* The only additional case we can do is MEM.  */
1213       int offset = 0;
1214       if (WORDS_BIG_ENDIAN)
1215         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
1216                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1217
1218       if (BYTES_BIG_ENDIAN)
1219         /* Adjust the address so that the address-after-the-data
1220            is unchanged.  */
1221         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1222                    - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
1223
1224       return adjust_address (x, mode, offset);
1225     }
1226   else if (GET_CODE (x) == ADDRESSOF)
1227     return gen_lowpart (mode, force_reg (GET_MODE (x), x));
1228   else
1229     abort ();
1230 }
1231
1232 /* Like `gen_lowpart', but refer to the most significant part.
1233    This is used to access the imaginary part of a complex number.  */
1234
1235 rtx
1236 gen_highpart (mode, x)
1237      enum machine_mode mode;
1238      rtx x;
1239 {
1240   unsigned int msize = GET_MODE_SIZE (mode);
1241   rtx result;
1242
1243   /* This case loses if X is a subreg.  To catch bugs early,
1244      complain if an invalid MODE is used even in other cases.  */
1245   if (msize > UNITS_PER_WORD
1246       && msize != GET_MODE_UNIT_SIZE (GET_MODE (x)))
1247     abort ();
1248
1249   result = simplify_gen_subreg (mode, x, GET_MODE (x),
1250                                 subreg_highpart_offset (mode, GET_MODE (x)));
1251
1252   /* simplify_gen_subreg is not guaranteed to return a valid operand for
1253      the target if we have a MEM.  gen_highpart must return a valid operand,
1254      emitting code if necessary to do so.  */
1255   if (result != NULL_RTX && GET_CODE (result) == MEM)
1256     result = validize_mem (result);
1257
1258   if (!result)
1259     abort ();
1260   return result;
1261 }
1262
1263 /* Like gen_highpart_mode, but accept mode of EXP operand in case EXP can
1264    be VOIDmode constant.  */
1265 rtx
1266 gen_highpart_mode (outermode, innermode, exp)
1267      enum machine_mode outermode, innermode;
1268      rtx exp;
1269 {
1270   if (GET_MODE (exp) != VOIDmode)
1271     {
1272       if (GET_MODE (exp) != innermode)
1273         abort ();
1274       return gen_highpart (outermode, exp);
1275     }
1276   return simplify_gen_subreg (outermode, exp, innermode,
1277                               subreg_highpart_offset (outermode, innermode));
1278 }
1279
1280 /* Return offset in bytes to get OUTERMODE low part
1281    of the value in mode INNERMODE stored in memory in target format.  */
1282
1283 unsigned int
1284 subreg_lowpart_offset (outermode, innermode)
1285      enum machine_mode outermode, innermode;
1286 {
1287   unsigned int offset = 0;
1288   int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1289
1290   if (difference > 0)
1291     {
1292       if (WORDS_BIG_ENDIAN)
1293         offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1294       if (BYTES_BIG_ENDIAN)
1295         offset += difference % UNITS_PER_WORD;
1296     }
1297
1298   return offset;
1299 }
1300
1301 /* Return offset in bytes to get OUTERMODE high part
1302    of the value in mode INNERMODE stored in memory in target format.  */
1303 unsigned int
1304 subreg_highpart_offset (outermode, innermode)
1305      enum machine_mode outermode, innermode;
1306 {
1307   unsigned int offset = 0;
1308   int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1309
1310   if (GET_MODE_SIZE (innermode) < GET_MODE_SIZE (outermode))
1311     abort ();
1312
1313   if (difference > 0)
1314     {
1315       if (! WORDS_BIG_ENDIAN)
1316         offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1317       if (! BYTES_BIG_ENDIAN)
1318         offset += difference % UNITS_PER_WORD;
1319     }
1320
1321   return offset;
1322 }
1323
1324 /* Return 1 iff X, assumed to be a SUBREG,
1325    refers to the least significant part of its containing reg.
1326    If X is not a SUBREG, always return 1 (it is its own low part!).  */
1327
1328 int
1329 subreg_lowpart_p (x)
1330      rtx x;
1331 {
1332   if (GET_CODE (x) != SUBREG)
1333     return 1;
1334   else if (GET_MODE (SUBREG_REG (x)) == VOIDmode)
1335     return 0;
1336
1337   return (subreg_lowpart_offset (GET_MODE (x), GET_MODE (SUBREG_REG (x)))
1338           == SUBREG_BYTE (x));
1339 }
1340 \f
1341
1342 /* Helper routine for all the constant cases of operand_subword.
1343    Some places invoke this directly.  */
1344
1345 rtx
1346 constant_subword (op, offset, mode)
1347      rtx op;
1348      int offset;
1349      enum machine_mode mode;
1350 {
1351   int size_ratio = HOST_BITS_PER_WIDE_INT / BITS_PER_WORD;
1352   HOST_WIDE_INT val;
1353
1354   /* If OP is already an integer word, return it.  */
1355   if (GET_MODE_CLASS (mode) == MODE_INT
1356       && GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1357     return op;
1358
1359   /* The output is some bits, the width of the target machine's word.
1360      A wider-word host can surely hold them in a CONST_INT. A narrower-word
1361      host can't.  */
1362   if (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
1363       && GET_MODE_CLASS (mode) == MODE_FLOAT
1364       && GET_MODE_BITSIZE (mode) == 64
1365       && GET_CODE (op) == CONST_DOUBLE)
1366     {
1367       long k[2];
1368       REAL_VALUE_TYPE rv;
1369
1370       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1371       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1372
1373       /* We handle 32-bit and >= 64-bit words here.  Note that the order in
1374          which the words are written depends on the word endianness.
1375          ??? This is a potential portability problem and should
1376          be fixed at some point.
1377
1378          We must exercise caution with the sign bit.  By definition there
1379          are 32 significant bits in K; there may be more in a HOST_WIDE_INT.
1380          Consider a host with a 32-bit long and a 64-bit HOST_WIDE_INT.
1381          So we explicitly mask and sign-extend as necessary.  */
1382       if (BITS_PER_WORD == 32)
1383         {
1384           val = k[offset];
1385           val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1386           return GEN_INT (val);
1387         }
1388 #if HOST_BITS_PER_WIDE_INT >= 64
1389       else if (BITS_PER_WORD >= 64 && offset == 0)
1390         {
1391           val = k[! WORDS_BIG_ENDIAN];
1392           val = (((val & 0xffffffff) ^ 0x80000000) - 0x80000000) << 32;
1393           val |= (HOST_WIDE_INT) k[WORDS_BIG_ENDIAN] & 0xffffffff;
1394           return GEN_INT (val);
1395         }
1396 #endif
1397       else if (BITS_PER_WORD == 16)
1398         {
1399           val = k[offset >> 1];
1400           if ((offset & 1) == ! WORDS_BIG_ENDIAN)
1401             val >>= 16;
1402           val = ((val & 0xffff) ^ 0x8000) - 0x8000;
1403           return GEN_INT (val);
1404         }
1405       else
1406         abort ();
1407     }
1408   else if (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
1409            && GET_MODE_CLASS (mode) == MODE_FLOAT
1410            && GET_MODE_BITSIZE (mode) > 64
1411            && GET_CODE (op) == CONST_DOUBLE)
1412     {
1413       long k[4];
1414       REAL_VALUE_TYPE rv;
1415
1416       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1417       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1418
1419       if (BITS_PER_WORD == 32)
1420         {
1421           val = k[offset];
1422           val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1423           return GEN_INT (val);
1424         }
1425 #if HOST_BITS_PER_WIDE_INT >= 64
1426       else if (BITS_PER_WORD >= 64 && offset <= 1)
1427         {
1428           val = k[offset * 2 + ! WORDS_BIG_ENDIAN];
1429           val = (((val & 0xffffffff) ^ 0x80000000) - 0x80000000) << 32;
1430           val |= (HOST_WIDE_INT) k[offset * 2 + WORDS_BIG_ENDIAN] & 0xffffffff;
1431           return GEN_INT (val);
1432         }
1433 #endif
1434       else
1435         abort ();
1436     }
1437
1438   /* Single word float is a little harder, since single- and double-word
1439      values often do not have the same high-order bits.  We have already
1440      verified that we want the only defined word of the single-word value.  */
1441   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1442       && GET_MODE_BITSIZE (mode) == 32
1443       && GET_CODE (op) == CONST_DOUBLE)
1444     {
1445       long l;
1446       REAL_VALUE_TYPE rv;
1447
1448       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1449       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1450
1451       /* Sign extend from known 32-bit value to HOST_WIDE_INT.  */
1452       val = l;
1453       val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1454
1455       if (BITS_PER_WORD == 16)
1456         {
1457           if ((offset & 1) == ! WORDS_BIG_ENDIAN)
1458             val >>= 16;
1459           val = ((val & 0xffff) ^ 0x8000) - 0x8000;
1460         }
1461
1462       return GEN_INT (val);
1463     }
1464
1465   /* The only remaining cases that we can handle are integers.
1466      Convert to proper endianness now since these cases need it.
1467      At this point, offset == 0 means the low-order word.
1468
1469      We do not want to handle the case when BITS_PER_WORD <= HOST_BITS_PER_INT
1470      in general.  However, if OP is (const_int 0), we can just return
1471      it for any word.  */
1472
1473   if (op == const0_rtx)
1474     return op;
1475
1476   if (GET_MODE_CLASS (mode) != MODE_INT
1477       || (GET_CODE (op) != CONST_INT && GET_CODE (op) != CONST_DOUBLE)
1478       || BITS_PER_WORD > HOST_BITS_PER_WIDE_INT)
1479     return 0;
1480
1481   if (WORDS_BIG_ENDIAN)
1482     offset = GET_MODE_SIZE (mode) / UNITS_PER_WORD - 1 - offset;
1483
1484   /* Find out which word on the host machine this value is in and get
1485      it from the constant.  */
1486   val = (offset / size_ratio == 0
1487          ? (GET_CODE (op) == CONST_INT ? INTVAL (op) : CONST_DOUBLE_LOW (op))
1488          : (GET_CODE (op) == CONST_INT
1489             ? (INTVAL (op) < 0 ? ~0 : 0) : CONST_DOUBLE_HIGH (op)));
1490
1491   /* Get the value we want into the low bits of val.  */
1492   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT)
1493     val = ((val >> ((offset % size_ratio) * BITS_PER_WORD)));
1494
1495   val = trunc_int_for_mode (val, word_mode);
1496
1497   return GEN_INT (val);
1498 }
1499
1500 /* Return subword OFFSET of operand OP.
1501    The word number, OFFSET, is interpreted as the word number starting
1502    at the low-order address.  OFFSET 0 is the low-order word if not
1503    WORDS_BIG_ENDIAN, otherwise it is the high-order word.
1504
1505    If we cannot extract the required word, we return zero.  Otherwise,
1506    an rtx corresponding to the requested word will be returned.
1507
1508    VALIDATE_ADDRESS is nonzero if the address should be validated.  Before
1509    reload has completed, a valid address will always be returned.  After
1510    reload, if a valid address cannot be returned, we return zero.
1511
1512    If VALIDATE_ADDRESS is zero, we simply form the required address; validating
1513    it is the responsibility of the caller.
1514
1515    MODE is the mode of OP in case it is a CONST_INT.
1516
1517    ??? This is still rather broken for some cases.  The problem for the
1518    moment is that all callers of this thing provide no 'goal mode' to
1519    tell us to work with.  This exists because all callers were written
1520    in a word based SUBREG world.
1521    Now use of this function can be deprecated by simplify_subreg in most
1522    cases.
1523  */
1524
1525 rtx
1526 operand_subword (op, offset, validate_address, mode)
1527      rtx op;
1528      unsigned int offset;
1529      int validate_address;
1530      enum machine_mode mode;
1531 {
1532   if (mode == VOIDmode)
1533     mode = GET_MODE (op);
1534
1535   if (mode == VOIDmode)
1536     abort ();
1537
1538   /* If OP is narrower than a word, fail.  */
1539   if (mode != BLKmode
1540       && (GET_MODE_SIZE (mode) < UNITS_PER_WORD))
1541     return 0;
1542
1543   /* If we want a word outside OP, return zero.  */
1544   if (mode != BLKmode
1545       && (offset + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode))
1546     return const0_rtx;
1547
1548   /* Form a new MEM at the requested address.  */
1549   if (GET_CODE (op) == MEM)
1550     {
1551       rtx new = adjust_address_nv (op, word_mode, offset * UNITS_PER_WORD);
1552
1553       if (! validate_address)
1554         return new;
1555
1556       else if (reload_completed)
1557         {
1558           if (! strict_memory_address_p (word_mode, XEXP (new, 0)))
1559             return 0;
1560         }
1561       else
1562         return replace_equiv_address (new, XEXP (new, 0));
1563     }
1564
1565   /* Rest can be handled by simplify_subreg.  */
1566   return simplify_gen_subreg (word_mode, op, mode, (offset * UNITS_PER_WORD));
1567 }
1568
1569 /* Similar to `operand_subword', but never return 0.  If we can't extract
1570    the required subword, put OP into a register and try again.  If that fails,
1571    abort.  We always validate the address in this case.
1572
1573    MODE is the mode of OP, in case it is CONST_INT.  */
1574
1575 rtx
1576 operand_subword_force (op, offset, mode)
1577      rtx op;
1578      unsigned int offset;
1579      enum machine_mode mode;
1580 {
1581   rtx result = operand_subword (op, offset, 1, mode);
1582
1583   if (result)
1584     return result;
1585
1586   if (mode != BLKmode && mode != VOIDmode)
1587     {
1588       /* If this is a register which can not be accessed by words, copy it
1589          to a pseudo register.  */
1590       if (GET_CODE (op) == REG)
1591         op = copy_to_reg (op);
1592       else
1593         op = force_reg (mode, op);
1594     }
1595
1596   result = operand_subword (op, offset, 1, mode);
1597   if (result == 0)
1598     abort ();
1599
1600   return result;
1601 }
1602 \f
1603 /* Given a compare instruction, swap the operands.
1604    A test instruction is changed into a compare of 0 against the operand.  */
1605
1606 void
1607 reverse_comparison (insn)
1608      rtx insn;
1609 {
1610   rtx body = PATTERN (insn);
1611   rtx comp;
1612
1613   if (GET_CODE (body) == SET)
1614     comp = SET_SRC (body);
1615   else
1616     comp = SET_SRC (XVECEXP (body, 0, 0));
1617
1618   if (GET_CODE (comp) == COMPARE)
1619     {
1620       rtx op0 = XEXP (comp, 0);
1621       rtx op1 = XEXP (comp, 1);
1622       XEXP (comp, 0) = op1;
1623       XEXP (comp, 1) = op0;
1624     }
1625   else
1626     {
1627       rtx new = gen_rtx_COMPARE (VOIDmode,
1628                                  CONST0_RTX (GET_MODE (comp)), comp);
1629       if (GET_CODE (body) == SET)
1630         SET_SRC (body) = new;
1631       else
1632         SET_SRC (XVECEXP (body, 0, 0)) = new;
1633     }
1634 }
1635 \f
1636 /* Within a MEM_EXPR, we care about either (1) a component ref of a decl,
1637    or (2) a component ref of something variable.  Represent the later with
1638    a NULL expression.  */
1639
1640 static tree
1641 component_ref_for_mem_expr (ref)
1642      tree ref;
1643 {
1644   tree inner = TREE_OPERAND (ref, 0);
1645
1646   if (TREE_CODE (inner) == COMPONENT_REF)
1647     inner = component_ref_for_mem_expr (inner);
1648   else
1649     {
1650       tree placeholder_ptr = 0;
1651
1652       /* Now remove any conversions: they don't change what the underlying
1653          object is.  Likewise for SAVE_EXPR.  Also handle PLACEHOLDER_EXPR.  */
1654       while (TREE_CODE (inner) == NOP_EXPR || TREE_CODE (inner) == CONVERT_EXPR
1655              || TREE_CODE (inner) == NON_LVALUE_EXPR
1656              || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1657              || TREE_CODE (inner) == SAVE_EXPR
1658              || TREE_CODE (inner) == PLACEHOLDER_EXPR)
1659         if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
1660           inner = find_placeholder (inner, &placeholder_ptr);
1661         else
1662           inner = TREE_OPERAND (inner, 0);
1663
1664       if (! DECL_P (inner))
1665         inner = NULL_TREE;
1666     }
1667
1668   if (inner == TREE_OPERAND (ref, 0))
1669     return ref;
1670   else
1671     return build (COMPONENT_REF, TREE_TYPE (ref), inner,
1672                   TREE_OPERAND (ref, 1));
1673 }
1674
1675 /* Given REF, a MEM, and T, either the type of X or the expression
1676    corresponding to REF, set the memory attributes.  OBJECTP is nonzero
1677    if we are making a new object of this type.  */
1678
1679 void
1680 set_mem_attributes (ref, t, objectp)
1681      rtx ref;
1682      tree t;
1683      int objectp;
1684 {
1685   HOST_WIDE_INT alias = MEM_ALIAS_SET (ref);
1686   tree expr = MEM_EXPR (ref);
1687   rtx offset = MEM_OFFSET (ref);
1688   rtx size = MEM_SIZE (ref);
1689   unsigned int align = MEM_ALIGN (ref);
1690   tree type;
1691
1692   /* It can happen that type_for_mode was given a mode for which there
1693      is no language-level type.  In which case it returns NULL, which
1694      we can see here.  */
1695   if (t == NULL_TREE)
1696     return;
1697
1698   type = TYPE_P (t) ? t : TREE_TYPE (t);
1699
1700   /* If we have already set DECL_RTL = ref, get_alias_set will get the
1701      wrong answer, as it assumes that DECL_RTL already has the right alias
1702      info.  Callers should not set DECL_RTL until after the call to
1703      set_mem_attributes.  */
1704   if (DECL_P (t) && ref == DECL_RTL_IF_SET (t))
1705     abort ();
1706
1707   /* Get the alias set from the expression or type (perhaps using a
1708      front-end routine) and use it.  */
1709   alias = get_alias_set (t);
1710
1711   MEM_VOLATILE_P (ref) = TYPE_VOLATILE (type);
1712   MEM_IN_STRUCT_P (ref) = AGGREGATE_TYPE_P (type);
1713   RTX_UNCHANGING_P (ref)
1714     |= ((lang_hooks.honor_readonly
1715          && (TYPE_READONLY (type) || TREE_READONLY (t)))
1716         || (! TYPE_P (t) && TREE_CONSTANT (t)));
1717
1718   /* If we are making an object of this type, or if this is a DECL, we know
1719      that it is a scalar if the type is not an aggregate.  */
1720   if ((objectp || DECL_P (t)) && ! AGGREGATE_TYPE_P (type))
1721     MEM_SCALAR_P (ref) = 1;
1722
1723   /* We can set the alignment from the type if we are making an object,
1724      this is an INDIRECT_REF, or if TYPE_ALIGN_OK.  */
1725   if (objectp || TREE_CODE (t) == INDIRECT_REF || TYPE_ALIGN_OK (type))
1726     align = MAX (align, TYPE_ALIGN (type));
1727
1728   /* If the size is known, we can set that.  */
1729   if (TYPE_SIZE_UNIT (type) && host_integerp (TYPE_SIZE_UNIT (type), 1))
1730     size = GEN_INT (tree_low_cst (TYPE_SIZE_UNIT (type), 1));
1731
1732   /* If T is not a type, we may be able to deduce some more information about
1733      the expression.  */
1734   if (! TYPE_P (t))
1735     {
1736       maybe_set_unchanging (ref, t);
1737       if (TREE_THIS_VOLATILE (t))
1738         MEM_VOLATILE_P (ref) = 1;
1739
1740       /* Now remove any conversions: they don't change what the underlying
1741          object is.  Likewise for SAVE_EXPR.  */
1742       while (TREE_CODE (t) == NOP_EXPR || TREE_CODE (t) == CONVERT_EXPR
1743              || TREE_CODE (t) == NON_LVALUE_EXPR
1744              || TREE_CODE (t) == VIEW_CONVERT_EXPR
1745              || TREE_CODE (t) == SAVE_EXPR)
1746         t = TREE_OPERAND (t, 0);
1747
1748       /* If this expression can't be addressed (e.g., it contains a reference
1749          to a non-addressable field), show we don't change its alias set.  */
1750       if (! can_address_p (t))
1751         MEM_KEEP_ALIAS_SET_P (ref) = 1;
1752
1753       /* If this is a decl, set the attributes of the MEM from it.  */
1754       if (DECL_P (t))
1755         {
1756           expr = t;
1757           offset = const0_rtx;
1758           size = (DECL_SIZE_UNIT (t)
1759                   && host_integerp (DECL_SIZE_UNIT (t), 1)
1760                   ? GEN_INT (tree_low_cst (DECL_SIZE_UNIT (t), 1)) : 0);
1761           align = DECL_ALIGN (t);
1762         }
1763
1764       /* If this is a constant, we know the alignment.  */
1765       else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'c')
1766         {
1767           align = TYPE_ALIGN (type);
1768 #ifdef CONSTANT_ALIGNMENT
1769           align = CONSTANT_ALIGNMENT (t, align);
1770 #endif
1771         }
1772
1773       /* If this is a field reference and not a bit-field, record it.  */
1774       /* ??? There is some information that can be gleened from bit-fields,
1775          such as the word offset in the structure that might be modified.
1776          But skip it for now.  */
1777       else if (TREE_CODE (t) == COMPONENT_REF
1778                && ! DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
1779         {
1780           expr = component_ref_for_mem_expr (t);
1781           offset = const0_rtx;
1782           /* ??? Any reason the field size would be different than
1783              the size we got from the type?  */
1784         }
1785
1786       /* If this is an array reference, look for an outer field reference.  */
1787       else if (TREE_CODE (t) == ARRAY_REF)
1788         {
1789           tree off_tree = size_zero_node;
1790
1791           do
1792             {
1793               off_tree
1794                 = fold (build (PLUS_EXPR, sizetype,
1795                                fold (build (MULT_EXPR, sizetype,
1796                                             TREE_OPERAND (t, 1),
1797                                             TYPE_SIZE_UNIT (TREE_TYPE (t)))),
1798                                off_tree));
1799               t = TREE_OPERAND (t, 0);
1800             }
1801           while (TREE_CODE (t) == ARRAY_REF);
1802
1803           if (TREE_CODE (t) == COMPONENT_REF)
1804             {
1805               expr = component_ref_for_mem_expr (t);
1806               if (host_integerp (off_tree, 1))
1807                 offset = GEN_INT (tree_low_cst (off_tree, 1));
1808               /* ??? Any reason the field size would be different than
1809                  the size we got from the type?  */
1810             }
1811         }
1812     }
1813
1814   /* Now set the attributes we computed above.  */
1815   MEM_ATTRS (ref)
1816     = get_mem_attrs (alias, expr, offset, size, align, GET_MODE (ref));
1817
1818   /* If this is already known to be a scalar or aggregate, we are done.  */
1819   if (MEM_IN_STRUCT_P (ref) || MEM_SCALAR_P (ref))
1820     return;
1821
1822   /* If it is a reference into an aggregate, this is part of an aggregate.
1823      Otherwise we don't know.  */
1824   else if (TREE_CODE (t) == COMPONENT_REF || TREE_CODE (t) == ARRAY_REF
1825            || TREE_CODE (t) == ARRAY_RANGE_REF
1826            || TREE_CODE (t) == BIT_FIELD_REF)
1827     MEM_IN_STRUCT_P (ref) = 1;
1828 }
1829
1830 /* Set the alias set of MEM to SET.  */
1831
1832 void
1833 set_mem_alias_set (mem, set)
1834      rtx mem;
1835      HOST_WIDE_INT set;
1836 {
1837 #ifdef ENABLE_CHECKING
1838   /* If the new and old alias sets don't conflict, something is wrong.  */
1839   if (!alias_sets_conflict_p (set, MEM_ALIAS_SET (mem)))
1840     abort ();
1841 #endif
1842
1843   MEM_ATTRS (mem) = get_mem_attrs (set, MEM_EXPR (mem), MEM_OFFSET (mem),
1844                                    MEM_SIZE (mem), MEM_ALIGN (mem),
1845                                    GET_MODE (mem));
1846 }
1847
1848 /* Set the alignment of MEM to ALIGN bits.  */
1849
1850 void
1851 set_mem_align (mem, align)
1852      rtx mem;
1853      unsigned int align;
1854 {
1855   MEM_ATTRS (mem) = get_mem_attrs (MEM_ALIAS_SET (mem), MEM_EXPR (mem),
1856                                    MEM_OFFSET (mem), MEM_SIZE (mem), align,
1857                                    GET_MODE (mem));
1858 }
1859
1860 /* Set the expr for MEM to EXPR.  */
1861
1862 void
1863 set_mem_expr (mem, expr)
1864      rtx mem;
1865      tree expr;
1866 {
1867   MEM_ATTRS (mem)
1868     = get_mem_attrs (MEM_ALIAS_SET (mem), expr, MEM_OFFSET (mem),
1869                      MEM_SIZE (mem), MEM_ALIGN (mem), GET_MODE (mem));
1870 }
1871
1872 /* Set the offset of MEM to OFFSET.  */
1873
1874 void
1875 set_mem_offset (mem, offset)
1876      rtx mem, offset;
1877 {
1878   MEM_ATTRS (mem) = get_mem_attrs (MEM_ALIAS_SET (mem), MEM_EXPR (mem),
1879                                    offset, MEM_SIZE (mem), MEM_ALIGN (mem),
1880                                    GET_MODE (mem));
1881 }
1882 \f
1883 /* Return a memory reference like MEMREF, but with its mode changed to MODE
1884    and its address changed to ADDR.  (VOIDmode means don't change the mode.
1885    NULL for ADDR means don't change the address.)  VALIDATE is nonzero if the
1886    returned memory location is required to be valid.  The memory
1887    attributes are not changed.  */
1888
1889 static rtx
1890 change_address_1 (memref, mode, addr, validate)
1891      rtx memref;
1892      enum machine_mode mode;
1893      rtx addr;
1894      int validate;
1895 {
1896   rtx new;
1897
1898   if (GET_CODE (memref) != MEM)
1899     abort ();
1900   if (mode == VOIDmode)
1901     mode = GET_MODE (memref);
1902   if (addr == 0)
1903     addr = XEXP (memref, 0);
1904
1905   if (validate)
1906     {
1907       if (reload_in_progress || reload_completed)
1908         {
1909           if (! memory_address_p (mode, addr))
1910             abort ();
1911         }
1912       else
1913         addr = memory_address (mode, addr);
1914     }
1915
1916   if (rtx_equal_p (addr, XEXP (memref, 0)) && mode == GET_MODE (memref))
1917     return memref;
1918
1919   new = gen_rtx_MEM (mode, addr);
1920   MEM_COPY_ATTRIBUTES (new, memref);
1921   return new;
1922 }
1923
1924 /* Like change_address_1 with VALIDATE nonzero, but we are not saying in what
1925    way we are changing MEMREF, so we only preserve the alias set.  */
1926
1927 rtx
1928 change_address (memref, mode, addr)
1929      rtx memref;
1930      enum machine_mode mode;
1931      rtx addr;
1932 {
1933   rtx new = change_address_1 (memref, mode, addr, 1);
1934   enum machine_mode mmode = GET_MODE (new);
1935
1936   MEM_ATTRS (new)
1937     = get_mem_attrs (MEM_ALIAS_SET (memref), 0, 0,
1938                      mmode == BLKmode ? 0 : GEN_INT (GET_MODE_SIZE (mmode)),
1939                      (mmode == BLKmode ? BITS_PER_UNIT
1940                       : GET_MODE_ALIGNMENT (mmode)),
1941                      mmode);
1942
1943   return new;
1944 }
1945
1946 /* Return a memory reference like MEMREF, but with its mode changed
1947    to MODE and its address offset by OFFSET bytes.  If VALIDATE is
1948    nonzero, the memory address is forced to be valid.
1949    If ADJUST is zero, OFFSET is only used to update MEM_ATTRS
1950    and caller is responsible for adjusting MEMREF base register.  */
1951
1952 rtx
1953 adjust_address_1 (memref, mode, offset, validate, adjust)
1954      rtx memref;
1955      enum machine_mode mode;
1956      HOST_WIDE_INT offset;
1957      int validate, adjust;
1958 {
1959   rtx addr = XEXP (memref, 0);
1960   rtx new;
1961   rtx memoffset = MEM_OFFSET (memref);
1962   rtx size = 0;
1963   unsigned int memalign = MEM_ALIGN (memref);
1964
1965   /* ??? Prefer to create garbage instead of creating shared rtl.
1966      This may happen even if offset is non-zero -- consider
1967      (plus (plus reg reg) const_int) -- so do this always.  */
1968   addr = copy_rtx (addr);
1969
1970   if (adjust)
1971     {
1972       /* If MEMREF is a LO_SUM and the offset is within the alignment of the
1973          object, we can merge it into the LO_SUM.  */
1974       if (GET_MODE (memref) != BLKmode && GET_CODE (addr) == LO_SUM
1975           && offset >= 0
1976           && (unsigned HOST_WIDE_INT) offset
1977               < GET_MODE_ALIGNMENT (GET_MODE (memref)) / BITS_PER_UNIT)
1978         addr = gen_rtx_LO_SUM (Pmode, XEXP (addr, 0),
1979                                plus_constant (XEXP (addr, 1), offset));
1980       else
1981         addr = plus_constant (addr, offset);
1982     }
1983
1984   new = change_address_1 (memref, mode, addr, validate);
1985
1986   /* Compute the new values of the memory attributes due to this adjustment.
1987      We add the offsets and update the alignment.  */
1988   if (memoffset)
1989     memoffset = GEN_INT (offset + INTVAL (memoffset));
1990
1991   /* Compute the new alignment by taking the MIN of the alignment and the
1992      lowest-order set bit in OFFSET, but don't change the alignment if OFFSET
1993      if zero.  */
1994   if (offset != 0)
1995     memalign
1996       = MIN (memalign,
1997              (unsigned HOST_WIDE_INT) (offset & -offset) * BITS_PER_UNIT);
1998
1999   /* We can compute the size in a number of ways.  */
2000   if (GET_MODE (new) != BLKmode)
2001     size = GEN_INT (GET_MODE_SIZE (GET_MODE (new)));
2002   else if (MEM_SIZE (memref))
2003     size = plus_constant (MEM_SIZE (memref), -offset);
2004
2005   MEM_ATTRS (new) = get_mem_attrs (MEM_ALIAS_SET (memref), MEM_EXPR (memref),
2006                                    memoffset, size, memalign, GET_MODE (new));
2007
2008   /* At some point, we should validate that this offset is within the object,
2009      if all the appropriate values are known.  */
2010   return new;
2011 }
2012
2013 /* Return a memory reference like MEMREF, but with its mode changed
2014    to MODE and its address changed to ADDR, which is assumed to be
2015    MEMREF offseted by OFFSET bytes.  If VALIDATE is
2016    nonzero, the memory address is forced to be valid.  */
2017
2018 rtx
2019 adjust_automodify_address_1 (memref, mode, addr, offset, validate)
2020      rtx memref;
2021      enum machine_mode mode;
2022      rtx addr;
2023      HOST_WIDE_INT offset;
2024      int validate;
2025 {
2026   memref = change_address_1 (memref, VOIDmode, addr, validate);
2027   return adjust_address_1 (memref, mode, offset, validate, 0);
2028 }
2029
2030 /* Return a memory reference like MEMREF, but whose address is changed by
2031    adding OFFSET, an RTX, to it.  POW2 is the highest power of two factor
2032    known to be in OFFSET (possibly 1).  */
2033
2034 rtx
2035 offset_address (memref, offset, pow2)
2036      rtx memref;
2037      rtx offset;
2038      HOST_WIDE_INT pow2;
2039 {
2040   rtx new, addr = XEXP (memref, 0);
2041
2042   new = simplify_gen_binary (PLUS, Pmode, addr, offset);
2043
2044   /* At this point we don't know _why_ the address is invalid.  It
2045      could have secondary memory refereces, multiplies or anything.
2046
2047      However, if we did go and rearrange things, we can wind up not
2048      being able to recognize the magic around pic_offset_table_rtx.
2049      This stuff is fragile, and is yet another example of why it is
2050      bad to expose PIC machinery too early.  */
2051   if (! memory_address_p (GET_MODE (memref), new)
2052       && GET_CODE (addr) == PLUS
2053       && XEXP (addr, 0) == pic_offset_table_rtx)
2054     {
2055       addr = force_reg (GET_MODE (addr), addr);
2056       new = simplify_gen_binary (PLUS, Pmode, addr, offset);
2057     }
2058
2059   update_temp_slot_address (XEXP (memref, 0), new);
2060   new = change_address_1 (memref, VOIDmode, new, 1);
2061
2062   /* Update the alignment to reflect the offset.  Reset the offset, which
2063      we don't know.  */
2064   MEM_ATTRS (new)
2065     = get_mem_attrs (MEM_ALIAS_SET (memref), MEM_EXPR (memref), 0, 0,
2066                      MIN (MEM_ALIGN (memref),
2067                           (unsigned HOST_WIDE_INT) pow2 * BITS_PER_UNIT),
2068                      GET_MODE (new));
2069   return new;
2070 }
2071
2072 /* Return a memory reference like MEMREF, but with its address changed to
2073    ADDR.  The caller is asserting that the actual piece of memory pointed
2074    to is the same, just the form of the address is being changed, such as
2075    by putting something into a register.  */
2076
2077 rtx
2078 replace_equiv_address (memref, addr)
2079      rtx memref;
2080      rtx addr;
2081 {
2082   /* change_address_1 copies the memory attribute structure without change
2083      and that's exactly what we want here.  */
2084   update_temp_slot_address (XEXP (memref, 0), addr);
2085   return change_address_1 (memref, VOIDmode, addr, 1);
2086 }
2087
2088 /* Likewise, but the reference is not required to be valid.  */
2089
2090 rtx
2091 replace_equiv_address_nv (memref, addr)
2092      rtx memref;
2093      rtx addr;
2094 {
2095   return change_address_1 (memref, VOIDmode, addr, 0);
2096 }
2097
2098 /* Return a memory reference like MEMREF, but with its mode widened to
2099    MODE and offset by OFFSET.  This would be used by targets that e.g.
2100    cannot issue QImode memory operations and have to use SImode memory
2101    operations plus masking logic.  */
2102
2103 rtx
2104 widen_memory_access (memref, mode, offset)
2105      rtx memref;
2106      enum machine_mode mode;
2107      HOST_WIDE_INT offset;
2108 {
2109   rtx new = adjust_address_1 (memref, mode, offset, 1, 1);
2110   tree expr = MEM_EXPR (new);
2111   rtx memoffset = MEM_OFFSET (new);
2112   unsigned int size = GET_MODE_SIZE (mode);
2113
2114   /* If we don't know what offset we were at within the expression, then
2115      we can't know if we've overstepped the bounds.  */
2116   if (! memoffset)
2117     expr = NULL_TREE;
2118
2119   while (expr)
2120     {
2121       if (TREE_CODE (expr) == COMPONENT_REF)
2122         {
2123           tree field = TREE_OPERAND (expr, 1);
2124
2125           if (! DECL_SIZE_UNIT (field))
2126             {
2127               expr = NULL_TREE;
2128               break;
2129             }
2130
2131           /* Is the field at least as large as the access?  If so, ok,
2132              otherwise strip back to the containing structure.  */
2133           if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
2134               && compare_tree_int (DECL_SIZE_UNIT (field), size) >= 0
2135               && INTVAL (memoffset) >= 0)
2136             break;
2137
2138           if (! host_integerp (DECL_FIELD_OFFSET (field), 1))
2139             {
2140               expr = NULL_TREE;
2141               break;
2142             }
2143
2144           expr = TREE_OPERAND (expr, 0);
2145           memoffset = (GEN_INT (INTVAL (memoffset)
2146                        + tree_low_cst (DECL_FIELD_OFFSET (field), 1)
2147                        + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
2148                           / BITS_PER_UNIT)));
2149         }
2150       /* Similarly for the decl.  */
2151       else if (DECL_P (expr)
2152                && DECL_SIZE_UNIT (expr)
2153                && TREE_CODE (DECL_SIZE_UNIT (expr)) == INTEGER_CST
2154                && compare_tree_int (DECL_SIZE_UNIT (expr), size) >= 0
2155                && (! memoffset || INTVAL (memoffset) >= 0))
2156         break;
2157       else
2158         {
2159           /* The widened memory access overflows the expression, which means
2160              that it could alias another expression.  Zap it.  */
2161           expr = NULL_TREE;
2162           break;
2163         }
2164     }
2165
2166   if (! expr)
2167     memoffset = NULL_RTX;
2168
2169   /* The widened memory may alias other stuff, so zap the alias set.  */
2170   /* ??? Maybe use get_alias_set on any remaining expression.  */
2171
2172   MEM_ATTRS (new) = get_mem_attrs (0, expr, memoffset, GEN_INT (size),
2173                                    MEM_ALIGN (new), mode);
2174
2175   return new;
2176 }
2177 \f
2178 /* Return a newly created CODE_LABEL rtx with a unique label number.  */
2179
2180 rtx
2181 gen_label_rtx ()
2182 {
2183   rtx label;
2184
2185   label = gen_rtx_CODE_LABEL (VOIDmode, 0, NULL_RTX, NULL_RTX,
2186                               NULL, label_num++, NULL, NULL);
2187
2188   LABEL_NUSES (label) = 0;
2189   LABEL_ALTERNATE_NAME (label) = NULL;
2190   return label;
2191 }
2192 \f
2193 /* For procedure integration.  */
2194
2195 /* Install new pointers to the first and last insns in the chain.
2196    Also, set cur_insn_uid to one higher than the last in use.
2197    Used for an inline-procedure after copying the insn chain.  */
2198
2199 void
2200 set_new_first_and_last_insn (first, last)
2201      rtx first, last;
2202 {
2203   rtx insn;
2204
2205   first_insn = first;
2206   last_insn = last;
2207   cur_insn_uid = 0;
2208
2209   for (insn = first; insn; insn = NEXT_INSN (insn))
2210     cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2211
2212   cur_insn_uid++;
2213 }
2214
2215 /* Set the range of label numbers found in the current function.
2216    This is used when belatedly compiling an inline function.  */
2217
2218 void
2219 set_new_first_and_last_label_num (first, last)
2220      int first, last;
2221 {
2222   base_label_num = label_num;
2223   first_label_num = first;
2224   last_label_num = last;
2225 }
2226
2227 /* Set the last label number found in the current function.
2228    This is used when belatedly compiling an inline function.  */
2229
2230 void
2231 set_new_last_label_num (last)
2232      int last;
2233 {
2234   base_label_num = label_num;
2235   last_label_num = last;
2236 }
2237 \f
2238 /* Restore all variables describing the current status from the structure *P.
2239    This is used after a nested function.  */
2240
2241 void
2242 restore_emit_status (p)
2243      struct function *p ATTRIBUTE_UNUSED;
2244 {
2245   last_label_num = 0;
2246 }
2247 \f
2248 /* Go through all the RTL insn bodies and copy any invalid shared
2249    structure.  This routine should only be called once.  */
2250
2251 void
2252 unshare_all_rtl (fndecl, insn)
2253      tree fndecl;
2254      rtx insn;
2255 {
2256   tree decl;
2257
2258   /* Make sure that virtual parameters are not shared.  */
2259   for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
2260     SET_DECL_RTL (decl, copy_rtx_if_shared (DECL_RTL (decl)));
2261
2262   /* Make sure that virtual stack slots are not shared.  */
2263   unshare_all_decls (DECL_INITIAL (fndecl));
2264
2265   /* Unshare just about everything else.  */
2266   unshare_all_rtl_1 (insn);
2267
2268   /* Make sure the addresses of stack slots found outside the insn chain
2269      (such as, in DECL_RTL of a variable) are not shared
2270      with the insn chain.
2271
2272      This special care is necessary when the stack slot MEM does not
2273      actually appear in the insn chain.  If it does appear, its address
2274      is unshared from all else at that point.  */
2275   stack_slot_list = copy_rtx_if_shared (stack_slot_list);
2276 }
2277
2278 /* Go through all the RTL insn bodies and copy any invalid shared
2279    structure, again.  This is a fairly expensive thing to do so it
2280    should be done sparingly.  */
2281
2282 void
2283 unshare_all_rtl_again (insn)
2284      rtx insn;
2285 {
2286   rtx p;
2287   tree decl;
2288
2289   for (p = insn; p; p = NEXT_INSN (p))
2290     if (INSN_P (p))
2291       {
2292         reset_used_flags (PATTERN (p));
2293         reset_used_flags (REG_NOTES (p));
2294         reset_used_flags (LOG_LINKS (p));
2295       }
2296
2297   /* Make sure that virtual stack slots are not shared.  */
2298   reset_used_decls (DECL_INITIAL (cfun->decl));
2299
2300   /* Make sure that virtual parameters are not shared.  */
2301   for (decl = DECL_ARGUMENTS (cfun->decl); decl; decl = TREE_CHAIN (decl))
2302     reset_used_flags (DECL_RTL (decl));
2303
2304   reset_used_flags (stack_slot_list);
2305
2306   unshare_all_rtl (cfun->decl, insn);
2307 }
2308
2309 /* Go through all the RTL insn bodies and copy any invalid shared structure.
2310    Assumes the mark bits are cleared at entry.  */
2311
2312 static void
2313 unshare_all_rtl_1 (insn)
2314      rtx insn;
2315 {
2316   for (; insn; insn = NEXT_INSN (insn))
2317     if (INSN_P (insn))
2318       {
2319         PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
2320         REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
2321         LOG_LINKS (insn) = copy_rtx_if_shared (LOG_LINKS (insn));
2322       }
2323 }
2324
2325 /* Go through all virtual stack slots of a function and copy any
2326    shared structure.  */
2327 static void
2328 unshare_all_decls (blk)
2329      tree blk;
2330 {
2331   tree t;
2332
2333   /* Copy shared decls.  */
2334   for (t = BLOCK_VARS (blk); t; t = TREE_CHAIN (t))
2335     if (DECL_RTL_SET_P (t))
2336       SET_DECL_RTL (t, copy_rtx_if_shared (DECL_RTL (t)));
2337
2338   /* Now process sub-blocks.  */
2339   for (t = BLOCK_SUBBLOCKS (blk); t; t = TREE_CHAIN (t))
2340     unshare_all_decls (t);
2341 }
2342
2343 /* Go through all virtual stack slots of a function and mark them as
2344    not shared.  */
2345 static void
2346 reset_used_decls (blk)
2347      tree blk;
2348 {
2349   tree t;
2350
2351   /* Mark decls.  */
2352   for (t = BLOCK_VARS (blk); t; t = TREE_CHAIN (t))
2353     if (DECL_RTL_SET_P (t))
2354       reset_used_flags (DECL_RTL (t));
2355
2356   /* Now process sub-blocks.  */
2357   for (t = BLOCK_SUBBLOCKS (blk); t; t = TREE_CHAIN (t))
2358     reset_used_decls (t);
2359 }
2360
2361 /* Similar to `copy_rtx' except that if MAY_SHARE is present, it is
2362    placed in the result directly, rather than being copied.  MAY_SHARE is
2363    either a MEM of an EXPR_LIST of MEMs.  */
2364
2365 rtx
2366 copy_most_rtx (orig, may_share)
2367      rtx orig;
2368      rtx may_share;
2369 {
2370   rtx copy;
2371   int i, j;
2372   RTX_CODE code;
2373   const char *format_ptr;
2374
2375   if (orig == may_share
2376       || (GET_CODE (may_share) == EXPR_LIST
2377           && in_expr_list_p (may_share, orig)))
2378     return orig;
2379
2380   code = GET_CODE (orig);
2381
2382   switch (code)
2383     {
2384     case REG:
2385     case QUEUED:
2386     case CONST_INT:
2387     case CONST_DOUBLE:
2388     case CONST_VECTOR:
2389     case SYMBOL_REF:
2390     case CODE_LABEL:
2391     case PC:
2392     case CC0:
2393       return orig;
2394     default:
2395       break;
2396     }
2397
2398   copy = rtx_alloc (code);
2399   PUT_MODE (copy, GET_MODE (orig));
2400   RTX_FLAG (copy, in_struct) = RTX_FLAG (orig, in_struct);
2401   RTX_FLAG (copy, volatil) = RTX_FLAG (orig, volatil);
2402   RTX_FLAG (copy, unchanging) = RTX_FLAG (orig, unchanging);
2403   RTX_FLAG (copy, integrated) = RTX_FLAG (orig, integrated);
2404   RTX_FLAG (copy, frame_related) = RTX_FLAG (orig, frame_related);
2405
2406   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
2407
2408   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
2409     {
2410       switch (*format_ptr++)
2411         {
2412         case 'e':
2413           XEXP (copy, i) = XEXP (orig, i);
2414           if (XEXP (orig, i) != NULL && XEXP (orig, i) != may_share)
2415             XEXP (copy, i) = copy_most_rtx (XEXP (orig, i), may_share);
2416           break;
2417
2418         case 'u':
2419           XEXP (copy, i) = XEXP (orig, i);
2420           break;
2421
2422         case 'E':
2423         case 'V':
2424           XVEC (copy, i) = XVEC (orig, i);
2425           if (XVEC (orig, i) != NULL)
2426             {
2427               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
2428               for (j = 0; j < XVECLEN (copy, i); j++)
2429                 XVECEXP (copy, i, j)
2430                   = copy_most_rtx (XVECEXP (orig, i, j), may_share);
2431             }
2432           break;
2433
2434         case 'w':
2435           XWINT (copy, i) = XWINT (orig, i);
2436           break;
2437
2438         case 'n':
2439         case 'i':
2440           XINT (copy, i) = XINT (orig, i);
2441           break;
2442
2443         case 't':
2444           XTREE (copy, i) = XTREE (orig, i);
2445           break;
2446
2447         case 's':
2448         case 'S':
2449           XSTR (copy, i) = XSTR (orig, i);
2450           break;
2451
2452         case '0':
2453           /* Copy this through the wide int field; that's safest.  */
2454           X0WINT (copy, i) = X0WINT (orig, i);
2455           break;
2456
2457         default:
2458           abort ();
2459         }
2460     }
2461   return copy;
2462 }
2463
2464 /* Mark ORIG as in use, and return a copy of it if it was already in use.
2465    Recursively does the same for subexpressions.  */
2466
2467 rtx
2468 copy_rtx_if_shared (orig)
2469      rtx orig;
2470 {
2471   rtx x = orig;
2472   int i;
2473   enum rtx_code code;
2474   const char *format_ptr;
2475   int copied = 0;
2476
2477   if (x == 0)
2478     return 0;
2479
2480   code = GET_CODE (x);
2481
2482   /* These types may be freely shared.  */
2483
2484   switch (code)
2485     {
2486     case REG:
2487     case QUEUED:
2488     case CONST_INT:
2489     case CONST_DOUBLE:
2490     case CONST_VECTOR:
2491     case SYMBOL_REF:
2492     case CODE_LABEL:
2493     case PC:
2494     case CC0:
2495     case SCRATCH:
2496       /* SCRATCH must be shared because they represent distinct values.  */
2497       return x;
2498
2499     case CONST:
2500       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
2501          a LABEL_REF, it isn't sharable.  */
2502       if (GET_CODE (XEXP (x, 0)) == PLUS
2503           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
2504           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
2505         return x;
2506       break;
2507
2508     case INSN:
2509     case JUMP_INSN:
2510     case CALL_INSN:
2511     case NOTE:
2512     case BARRIER:
2513       /* The chain of insns is not being copied.  */
2514       return x;
2515
2516     case MEM:
2517       /* A MEM is allowed to be shared if its address is constant.
2518
2519          We used to allow sharing of MEMs which referenced
2520          virtual_stack_vars_rtx or virtual_incoming_args_rtx, but
2521          that can lose.  instantiate_virtual_regs will not unshare
2522          the MEMs, and combine may change the structure of the address
2523          because it looks safe and profitable in one context, but
2524          in some other context it creates unrecognizable RTL.  */
2525       if (CONSTANT_ADDRESS_P (XEXP (x, 0)))
2526         return x;
2527
2528       break;
2529
2530     default:
2531       break;
2532     }
2533
2534   /* This rtx may not be shared.  If it has already been seen,
2535      replace it with a copy of itself.  */
2536
2537   if (RTX_FLAG (x, used))
2538     {
2539       rtx copy;
2540
2541       copy = rtx_alloc (code);
2542       memcpy (copy, x,
2543              (sizeof (*copy) - sizeof (copy->fld)
2544               + sizeof (copy->fld[0]) * GET_RTX_LENGTH (code)));
2545       x = copy;
2546       copied = 1;
2547     }
2548   RTX_FLAG (x, used) = 1;
2549
2550   /* Now scan the subexpressions recursively.
2551      We can store any replaced subexpressions directly into X
2552      since we know X is not shared!  Any vectors in X
2553      must be copied if X was copied.  */
2554
2555   format_ptr = GET_RTX_FORMAT (code);
2556
2557   for (i = 0; i < GET_RTX_LENGTH (code); i++)
2558     {
2559       switch (*format_ptr++)
2560         {
2561         case 'e':
2562           XEXP (x, i) = copy_rtx_if_shared (XEXP (x, i));
2563           break;
2564
2565         case 'E':
2566           if (XVEC (x, i) != NULL)
2567             {
2568               int j;
2569               int len = XVECLEN (x, i);
2570
2571               if (copied && len > 0)
2572                 XVEC (x, i) = gen_rtvec_v (len, XVEC (x, i)->elem);
2573               for (j = 0; j < len; j++)
2574                 XVECEXP (x, i, j) = copy_rtx_if_shared (XVECEXP (x, i, j));
2575             }
2576           break;
2577         }
2578     }
2579   return x;
2580 }
2581
2582 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
2583    to look for shared sub-parts.  */
2584
2585 void
2586 reset_used_flags (x)
2587      rtx x;
2588 {
2589   int i, j;
2590   enum rtx_code code;
2591   const char *format_ptr;
2592
2593   if (x == 0)
2594     return;
2595
2596   code = GET_CODE (x);
2597
2598   /* These types may be freely shared so we needn't do any resetting
2599      for them.  */
2600
2601   switch (code)
2602     {
2603     case REG:
2604     case QUEUED:
2605     case CONST_INT:
2606     case CONST_DOUBLE:
2607     case CONST_VECTOR:
2608     case SYMBOL_REF:
2609     case CODE_LABEL:
2610     case PC:
2611     case CC0:
2612       return;
2613
2614     case INSN:
2615     case JUMP_INSN:
2616     case CALL_INSN:
2617     case NOTE:
2618     case LABEL_REF:
2619     case BARRIER:
2620       /* The chain of insns is not being copied.  */
2621       return;
2622
2623     default:
2624       break;
2625     }
2626
2627   RTX_FLAG (x, used) = 0;
2628
2629   format_ptr = GET_RTX_FORMAT (code);
2630   for (i = 0; i < GET_RTX_LENGTH (code); i++)
2631     {
2632       switch (*format_ptr++)
2633         {
2634         case 'e':
2635           reset_used_flags (XEXP (x, i));
2636           break;
2637
2638         case 'E':
2639           for (j = 0; j < XVECLEN (x, i); j++)
2640             reset_used_flags (XVECEXP (x, i, j));
2641           break;
2642         }
2643     }
2644 }
2645 \f
2646 /* Copy X if necessary so that it won't be altered by changes in OTHER.
2647    Return X or the rtx for the pseudo reg the value of X was copied into.
2648    OTHER must be valid as a SET_DEST.  */
2649
2650 rtx
2651 make_safe_from (x, other)
2652      rtx x, other;
2653 {
2654   while (1)
2655     switch (GET_CODE (other))
2656       {
2657       case SUBREG:
2658         other = SUBREG_REG (other);
2659         break;
2660       case STRICT_LOW_PART:
2661       case SIGN_EXTEND:
2662       case ZERO_EXTEND:
2663         other = XEXP (other, 0);
2664         break;
2665       default:
2666         goto done;
2667       }
2668  done:
2669   if ((GET_CODE (other) == MEM
2670        && ! CONSTANT_P (x)
2671        && GET_CODE (x) != REG
2672        && GET_CODE (x) != SUBREG)
2673       || (GET_CODE (other) == REG
2674           && (REGNO (other) < FIRST_PSEUDO_REGISTER
2675               || reg_mentioned_p (other, x))))
2676     {
2677       rtx temp = gen_reg_rtx (GET_MODE (x));
2678       emit_move_insn (temp, x);
2679       return temp;
2680     }
2681   return x;
2682 }
2683 \f
2684 /* Emission of insns (adding them to the doubly-linked list).  */
2685
2686 /* Return the first insn of the current sequence or current function.  */
2687
2688 rtx
2689 get_insns ()
2690 {
2691   return first_insn;
2692 }
2693
2694 /* Specify a new insn as the first in the chain.  */
2695
2696 void
2697 set_first_insn (insn)
2698      rtx insn;
2699 {
2700   if (PREV_INSN (insn) != 0)
2701     abort ();
2702   first_insn = insn;
2703 }
2704
2705 /* Return the last insn emitted in current sequence or current function.  */
2706
2707 rtx
2708 get_last_insn ()
2709 {
2710   return last_insn;
2711 }
2712
2713 /* Specify a new insn as the last in the chain.  */
2714
2715 void
2716 set_last_insn (insn)
2717      rtx insn;
2718 {
2719   if (NEXT_INSN (insn) != 0)
2720     abort ();
2721   last_insn = insn;
2722 }
2723
2724 /* Return the last insn emitted, even if it is in a sequence now pushed.  */
2725
2726 rtx
2727 get_last_insn_anywhere ()
2728 {
2729   struct sequence_stack *stack;
2730   if (last_insn)
2731     return last_insn;
2732   for (stack = seq_stack; stack; stack = stack->next)
2733     if (stack->last != 0)
2734       return stack->last;
2735   return 0;
2736 }
2737
2738 /* Return the first nonnote insn emitted in current sequence or current
2739    function.  This routine looks inside SEQUENCEs.  */
2740
2741 rtx
2742 get_first_nonnote_insn ()
2743 {
2744   rtx insn = first_insn;
2745
2746   while (insn)
2747     {
2748       insn = next_insn (insn);
2749       if (insn == 0 || GET_CODE (insn) != NOTE)
2750         break;
2751     }
2752
2753   return insn;
2754 }
2755
2756 /* Return the last nonnote insn emitted in current sequence or current
2757    function.  This routine looks inside SEQUENCEs.  */
2758
2759 rtx
2760 get_last_nonnote_insn ()
2761 {
2762   rtx insn = last_insn;
2763
2764   while (insn)
2765     {
2766       insn = previous_insn (insn);
2767       if (insn == 0 || GET_CODE (insn) != NOTE)
2768         break;
2769     }
2770
2771   return insn;
2772 }
2773
2774 /* Return a number larger than any instruction's uid in this function.  */
2775
2776 int
2777 get_max_uid ()
2778 {
2779   return cur_insn_uid;
2780 }
2781
2782 /* Renumber instructions so that no instruction UIDs are wasted.  */
2783
2784 void
2785 renumber_insns (stream)
2786      FILE *stream;
2787 {
2788   rtx insn;
2789
2790   /* If we're not supposed to renumber instructions, don't.  */
2791   if (!flag_renumber_insns)
2792     return;
2793
2794   /* If there aren't that many instructions, then it's not really
2795      worth renumbering them.  */
2796   if (flag_renumber_insns == 1 && get_max_uid () < 25000)
2797     return;
2798
2799   cur_insn_uid = 1;
2800
2801   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2802     {
2803       if (stream)
2804         fprintf (stream, "Renumbering insn %d to %d\n",
2805                  INSN_UID (insn), cur_insn_uid);
2806       INSN_UID (insn) = cur_insn_uid++;
2807     }
2808 }
2809 \f
2810 /* Return the next insn.  If it is a SEQUENCE, return the first insn
2811    of the sequence.  */
2812
2813 rtx
2814 next_insn (insn)
2815      rtx insn;
2816 {
2817   if (insn)
2818     {
2819       insn = NEXT_INSN (insn);
2820       if (insn && GET_CODE (insn) == INSN
2821           && GET_CODE (PATTERN (insn)) == SEQUENCE)
2822         insn = XVECEXP (PATTERN (insn), 0, 0);
2823     }
2824
2825   return insn;
2826 }
2827
2828 /* Return the previous insn.  If it is a SEQUENCE, return the last insn
2829    of the sequence.  */
2830
2831 rtx
2832 previous_insn (insn)
2833      rtx insn;
2834 {
2835   if (insn)
2836     {
2837       insn = PREV_INSN (insn);
2838       if (insn && GET_CODE (insn) == INSN
2839           && GET_CODE (PATTERN (insn)) == SEQUENCE)
2840         insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
2841     }
2842
2843   return insn;
2844 }
2845
2846 /* Return the next insn after INSN that is not a NOTE.  This routine does not
2847    look inside SEQUENCEs.  */
2848
2849 rtx
2850 next_nonnote_insn (insn)
2851      rtx insn;
2852 {
2853   while (insn)
2854     {
2855       insn = NEXT_INSN (insn);
2856       if (insn == 0 || GET_CODE (insn) != NOTE)
2857         break;
2858     }
2859
2860   return insn;
2861 }
2862
2863 /* Return the previous insn before INSN that is not a NOTE.  This routine does
2864    not look inside SEQUENCEs.  */
2865
2866 rtx
2867 prev_nonnote_insn (insn)
2868      rtx insn;
2869 {
2870   while (insn)
2871     {
2872       insn = PREV_INSN (insn);
2873       if (insn == 0 || GET_CODE (insn) != NOTE)
2874         break;
2875     }
2876
2877   return insn;
2878 }
2879
2880 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
2881    or 0, if there is none.  This routine does not look inside
2882    SEQUENCEs.  */
2883
2884 rtx
2885 next_real_insn (insn)
2886      rtx insn;
2887 {
2888   while (insn)
2889     {
2890       insn = NEXT_INSN (insn);
2891       if (insn == 0 || GET_CODE (insn) == INSN
2892           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
2893         break;
2894     }
2895
2896   return insn;
2897 }
2898
2899 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
2900    or 0, if there is none.  This routine does not look inside
2901    SEQUENCEs.  */
2902
2903 rtx
2904 prev_real_insn (insn)
2905      rtx insn;
2906 {
2907   while (insn)
2908     {
2909       insn = PREV_INSN (insn);
2910       if (insn == 0 || GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
2911           || GET_CODE (insn) == JUMP_INSN)
2912         break;
2913     }
2914
2915   return insn;
2916 }
2917
2918 /* Find the next insn after INSN that really does something.  This routine
2919    does not look inside SEQUENCEs.  Until reload has completed, this is the
2920    same as next_real_insn.  */
2921
2922 int
2923 active_insn_p (insn)
2924      rtx insn;
2925 {
2926   return (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
2927           || (GET_CODE (insn) == INSN
2928               && (! reload_completed
2929                   || (GET_CODE (PATTERN (insn)) != USE
2930                       && GET_CODE (PATTERN (insn)) != CLOBBER))));
2931 }
2932
2933 rtx
2934 next_active_insn (insn)
2935      rtx insn;
2936 {
2937   while (insn)
2938     {
2939       insn = NEXT_INSN (insn);
2940       if (insn == 0 || active_insn_p (insn))
2941         break;
2942     }
2943
2944   return insn;
2945 }
2946
2947 /* Find the last insn before INSN that really does something.  This routine
2948    does not look inside SEQUENCEs.  Until reload has completed, this is the
2949    same as prev_real_insn.  */
2950
2951 rtx
2952 prev_active_insn (insn)
2953      rtx insn;
2954 {
2955   while (insn)
2956     {
2957       insn = PREV_INSN (insn);
2958       if (insn == 0 || active_insn_p (insn))
2959         break;
2960     }
2961
2962   return insn;
2963 }
2964
2965 /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none.  */
2966
2967 rtx
2968 next_label (insn)
2969      rtx insn;
2970 {
2971   while (insn)
2972     {
2973       insn = NEXT_INSN (insn);
2974       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
2975         break;
2976     }
2977
2978   return insn;
2979 }
2980
2981 /* Return the last CODE_LABEL before the insn INSN, or 0 if there is none.  */
2982
2983 rtx
2984 prev_label (insn)
2985      rtx insn;
2986 {
2987   while (insn)
2988     {
2989       insn = PREV_INSN (insn);
2990       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
2991         break;
2992     }
2993
2994   return insn;
2995 }
2996 \f
2997 #ifdef HAVE_cc0
2998 /* INSN uses CC0 and is being moved into a delay slot.  Set up REG_CC_SETTER
2999    and REG_CC_USER notes so we can find it.  */
3000
3001 void
3002 link_cc0_insns (insn)
3003      rtx insn;
3004 {
3005   rtx user = next_nonnote_insn (insn);
3006
3007   if (GET_CODE (user) == INSN && GET_CODE (PATTERN (user)) == SEQUENCE)
3008     user = XVECEXP (PATTERN (user), 0, 0);
3009
3010   REG_NOTES (user) = gen_rtx_INSN_LIST (REG_CC_SETTER, insn,
3011                                         REG_NOTES (user));
3012   REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_CC_USER, user, REG_NOTES (insn));
3013 }
3014
3015 /* Return the next insn that uses CC0 after INSN, which is assumed to
3016    set it.  This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
3017    applied to the result of this function should yield INSN).
3018
3019    Normally, this is simply the next insn.  However, if a REG_CC_USER note
3020    is present, it contains the insn that uses CC0.
3021
3022    Return 0 if we can't find the insn.  */
3023
3024 rtx
3025 next_cc0_user (insn)
3026      rtx insn;
3027 {
3028   rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
3029
3030   if (note)
3031     return XEXP (note, 0);
3032
3033   insn = next_nonnote_insn (insn);
3034   if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
3035     insn = XVECEXP (PATTERN (insn), 0, 0);
3036
3037   if (insn && INSN_P (insn) && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
3038     return insn;
3039
3040   return 0;
3041 }
3042
3043 /* Find the insn that set CC0 for INSN.  Unless INSN has a REG_CC_SETTER
3044    note, it is the previous insn.  */
3045
3046 rtx
3047 prev_cc0_setter (insn)
3048      rtx insn;
3049 {
3050   rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3051
3052   if (note)
3053     return XEXP (note, 0);
3054
3055   insn = prev_nonnote_insn (insn);
3056   if (! sets_cc0_p (PATTERN (insn)))
3057     abort ();
3058
3059   return insn;
3060 }
3061 #endif
3062
3063 /* Increment the label uses for all labels present in rtx.  */
3064
3065 static void
3066 mark_label_nuses (x)
3067      rtx x;
3068 {
3069   enum rtx_code code;
3070   int i, j;
3071   const char *fmt;
3072
3073   code = GET_CODE (x);
3074   if (code == LABEL_REF)
3075     LABEL_NUSES (XEXP (x, 0))++;
3076
3077   fmt = GET_RTX_FORMAT (code);
3078   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3079     {
3080       if (fmt[i] == 'e')
3081         mark_label_nuses (XEXP (x, i));
3082       else if (fmt[i] == 'E')
3083         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3084           mark_label_nuses (XVECEXP (x, i, j));
3085     }
3086 }
3087
3088 \f
3089 /* Try splitting insns that can be split for better scheduling.
3090    PAT is the pattern which might split.
3091    TRIAL is the insn providing PAT.
3092    LAST is non-zero if we should return the last insn of the sequence produced.
3093
3094    If this routine succeeds in splitting, it returns the first or last
3095    replacement insn depending on the value of LAST.  Otherwise, it
3096    returns TRIAL.  If the insn to be returned can be split, it will be.  */
3097
3098 rtx
3099 try_split (pat, trial, last)
3100      rtx pat, trial;
3101      int last;
3102 {
3103   rtx before = PREV_INSN (trial);
3104   rtx after = NEXT_INSN (trial);
3105   int has_barrier = 0;
3106   rtx tem;
3107   rtx note, seq;
3108   int probability;
3109
3110   if (any_condjump_p (trial)
3111       && (note = find_reg_note (trial, REG_BR_PROB, 0)))
3112     split_branch_probability = INTVAL (XEXP (note, 0));
3113   probability = split_branch_probability;
3114
3115   seq = split_insns (pat, trial);
3116
3117   split_branch_probability = -1;
3118
3119   /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
3120      We may need to handle this specially.  */
3121   if (after && GET_CODE (after) == BARRIER)
3122     {
3123       has_barrier = 1;
3124       after = NEXT_INSN (after);
3125     }
3126
3127   if (seq)
3128     {
3129       /* Sometimes there will be only one insn in that list, this case will
3130          normally arise only when we want it in turn to be split (SFmode on
3131          the 29k is an example).  */
3132       if (NEXT_INSN (seq) != NULL_RTX)
3133         {
3134           rtx insn_last, insn;
3135           int njumps = 0;
3136
3137           /* Avoid infinite loop if any insn of the result matches
3138              the original pattern.  */
3139           insn_last = seq;
3140           while (1)
3141             {
3142               if (INSN_P (insn_last)
3143                   && rtx_equal_p (PATTERN (insn_last), pat))
3144                 return trial;
3145               if (NEXT_INSN (insn_last) == NULL_RTX)
3146                 break;
3147               insn_last = NEXT_INSN (insn_last);
3148             }
3149
3150           /* Mark labels.  */
3151           insn = insn_last;
3152           while (insn != NULL_RTX)
3153             {
3154               if (GET_CODE (insn) == JUMP_INSN)
3155                 {
3156                   mark_jump_label (PATTERN (insn), insn, 0);
3157                   njumps++;
3158                   if (probability != -1
3159                       && any_condjump_p (insn)
3160                       && !find_reg_note (insn, REG_BR_PROB, 0))
3161                     {
3162                       /* We can preserve the REG_BR_PROB notes only if exactly
3163                          one jump is created, otherwise the machine description
3164                          is responsible for this step using
3165                          split_branch_probability variable.  */
3166                       if (njumps != 1)
3167                         abort ();
3168                       REG_NOTES (insn)
3169                         = gen_rtx_EXPR_LIST (REG_BR_PROB,
3170                                              GEN_INT (probability),
3171                                              REG_NOTES (insn));
3172                     }
3173                 }
3174
3175               insn = PREV_INSN (insn);
3176             }
3177
3178           /* If we are splitting a CALL_INSN, look for the CALL_INSN
3179              in SEQ and copy our CALL_INSN_FUNCTION_USAGE to it.  */
3180           if (GET_CODE (trial) == CALL_INSN)
3181             {
3182               insn = insn_last;
3183               while (insn != NULL_RTX)
3184                 {
3185                   if (GET_CODE (insn) == CALL_INSN)
3186                     CALL_INSN_FUNCTION_USAGE (insn)
3187                       = CALL_INSN_FUNCTION_USAGE (trial);
3188
3189                   insn = PREV_INSN (insn);
3190                 }
3191             }
3192
3193           /* Copy notes, particularly those related to the CFG.  */
3194           for (note = REG_NOTES (trial); note; note = XEXP (note, 1))
3195             {
3196               switch (REG_NOTE_KIND (note))
3197                 {
3198                 case REG_EH_REGION:
3199                   insn = insn_last;
3200                   while (insn != NULL_RTX)
3201                     {
3202                       if (GET_CODE (insn) == CALL_INSN
3203                           || (flag_non_call_exceptions
3204                               && may_trap_p (PATTERN (insn))))
3205                         REG_NOTES (insn)
3206                           = gen_rtx_EXPR_LIST (REG_EH_REGION,
3207                                                XEXP (note, 0),
3208                                                REG_NOTES (insn));
3209                       insn = PREV_INSN (insn);
3210                     }
3211                   break;
3212
3213                 case REG_NORETURN:
3214                 case REG_SETJMP:
3215                 case REG_ALWAYS_RETURN:
3216                   insn = insn_last;
3217                   while (insn != NULL_RTX)
3218                     {
3219                       if (GET_CODE (insn) == CALL_INSN)
3220                         REG_NOTES (insn)
3221                           = gen_rtx_EXPR_LIST (REG_NOTE_KIND (note),
3222                                                XEXP (note, 0),
3223                                                REG_NOTES (insn));
3224                       insn = PREV_INSN (insn);
3225                     }
3226                   break;
3227
3228                 case REG_NON_LOCAL_GOTO:
3229                   insn = insn_last;
3230                   while (insn != NULL_RTX)
3231                     {
3232                       if (GET_CODE (insn) == JUMP_INSN)
3233                         REG_NOTES (insn)
3234                           = gen_rtx_EXPR_LIST (REG_NOTE_KIND (note),
3235                                                XEXP (note, 0),
3236                                                REG_NOTES (insn));
3237                       insn = PREV_INSN (insn);
3238                     }
3239                   break;
3240
3241                 default:
3242                   break;
3243                 }
3244             }
3245
3246           /* If there are LABELS inside the split insns increment the
3247              usage count so we don't delete the label.  */
3248           if (GET_CODE (trial) == INSN)
3249             {
3250               insn = insn_last;
3251               while (insn != NULL_RTX)
3252                 {
3253                   if (GET_CODE (insn) == INSN)
3254                     mark_label_nuses (PATTERN (insn));
3255
3256                   insn = PREV_INSN (insn);
3257                 }
3258             }
3259
3260           tem = emit_insn_after_scope (seq, trial, INSN_SCOPE (trial));
3261
3262           delete_insn (trial);
3263           if (has_barrier)
3264             emit_barrier_after (tem);
3265
3266           /* Recursively call try_split for each new insn created; by the
3267              time control returns here that insn will be fully split, so
3268              set LAST and continue from the insn after the one returned.
3269              We can't use next_active_insn here since AFTER may be a note.
3270              Ignore deleted insns, which can be occur if not optimizing.  */
3271           for (tem = NEXT_INSN (before); tem != after; tem = NEXT_INSN (tem))
3272             if (! INSN_DELETED_P (tem) && INSN_P (tem))
3273               tem = try_split (PATTERN (tem), tem, 1);
3274         }
3275       /* Avoid infinite loop if the result matches the original pattern.  */
3276       else if (rtx_equal_p (PATTERN (seq), pat))
3277         return trial;
3278       else
3279         {
3280           PATTERN (trial) = PATTERN (seq);
3281           INSN_CODE (trial) = -1;
3282           try_split (PATTERN (trial), trial, last);
3283         }
3284
3285       /* Return either the first or the last insn, depending on which was
3286          requested.  */
3287       return last
3288                 ? (after ? PREV_INSN (after) : last_insn)
3289                 : NEXT_INSN (before);
3290     }
3291
3292   return trial;
3293 }
3294 \f
3295 /* Make and return an INSN rtx, initializing all its slots.
3296    Store PATTERN in the pattern slots.  */
3297
3298 rtx
3299 make_insn_raw (pattern)
3300      rtx pattern;
3301 {
3302   rtx insn;
3303
3304   insn = rtx_alloc (INSN);
3305
3306   INSN_UID (insn) = cur_insn_uid++;
3307   PATTERN (insn) = pattern;
3308   INSN_CODE (insn) = -1;
3309   LOG_LINKS (insn) = NULL;
3310   REG_NOTES (insn) = NULL;
3311   INSN_SCOPE (insn) = NULL;
3312   BLOCK_FOR_INSN (insn) = NULL;
3313
3314 #ifdef ENABLE_RTL_CHECKING
3315   if (insn
3316       && INSN_P (insn)
3317       && (returnjump_p (insn)
3318           || (GET_CODE (insn) == SET
3319               && SET_DEST (insn) == pc_rtx)))
3320     {
3321       warning ("ICE: emit_insn used where emit_jump_insn needed:\n");
3322       debug_rtx (insn);
3323     }
3324 #endif
3325
3326   return insn;
3327 }
3328
3329 /* Like `make_insn_raw' but make a JUMP_INSN instead of an insn.  */
3330
3331 static rtx
3332 make_jump_insn_raw (pattern)
3333      rtx pattern;
3334 {
3335   rtx insn;
3336
3337   insn = rtx_alloc (JUMP_INSN);
3338   INSN_UID (insn) = cur_insn_uid++;
3339
3340   PATTERN (insn) = pattern;
3341   INSN_CODE (insn) = -1;
3342   LOG_LINKS (insn) = NULL;
3343   REG_NOTES (insn) = NULL;
3344   JUMP_LABEL (insn) = NULL;
3345   INSN_SCOPE (insn) = NULL;
3346   BLOCK_FOR_INSN (insn) = NULL;
3347
3348   return insn;
3349 }
3350
3351 /* Like `make_insn_raw' but make a CALL_INSN instead of an insn.  */
3352
3353 static rtx
3354 make_call_insn_raw (pattern)
3355      rtx pattern;
3356 {
3357   rtx insn;
3358
3359   insn = rtx_alloc (CALL_INSN);
3360   INSN_UID (insn) = cur_insn_uid++;
3361
3362   PATTERN (insn) = pattern;
3363   INSN_CODE (insn) = -1;
3364   LOG_LINKS (insn) = NULL;
3365   REG_NOTES (insn) = NULL;
3366   CALL_INSN_FUNCTION_USAGE (insn) = NULL;
3367   INSN_SCOPE (insn) = NULL;
3368   BLOCK_FOR_INSN (insn) = NULL;
3369
3370   return insn;
3371 }
3372 \f
3373 /* Add INSN to the end of the doubly-linked list.
3374    INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE.  */
3375
3376 void
3377 add_insn (insn)
3378      rtx insn;
3379 {
3380   PREV_INSN (insn) = last_insn;
3381   NEXT_INSN (insn) = 0;
3382
3383   if (NULL != last_insn)
3384     NEXT_INSN (last_insn) = insn;
3385
3386   if (NULL == first_insn)
3387     first_insn = insn;
3388
3389   last_insn = insn;
3390 }
3391
3392 /* Add INSN into the doubly-linked list after insn AFTER.  This and
3393    the next should be the only functions called to insert an insn once
3394    delay slots have been filled since only they know how to update a
3395    SEQUENCE.  */
3396
3397 void
3398 add_insn_after (insn, after)
3399      rtx insn, after;
3400 {
3401   rtx next = NEXT_INSN (after);
3402   basic_block bb;
3403
3404   if (optimize && INSN_DELETED_P (after))
3405     abort ();
3406
3407   NEXT_INSN (insn) = next;
3408   PREV_INSN (insn) = after;
3409
3410   if (next)
3411     {
3412       PREV_INSN (next) = insn;
3413       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3414         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
3415     }
3416   else if (last_insn == after)
3417     last_insn = insn;
3418   else
3419     {
3420       struct sequence_stack *stack = seq_stack;
3421       /* Scan all pending sequences too.  */
3422       for (; stack; stack = stack->next)
3423         if (after == stack->last)
3424           {
3425             stack->last = insn;
3426             break;
3427           }
3428
3429       if (stack == 0)
3430         abort ();
3431     }
3432
3433   if (GET_CODE (after) != BARRIER
3434       && GET_CODE (insn) != BARRIER
3435       && (bb = BLOCK_FOR_INSN (after)))
3436     {
3437       set_block_for_insn (insn, bb);
3438       if (INSN_P (insn))
3439         bb->flags |= BB_DIRTY;
3440       /* Should not happen as first in the BB is always
3441          either NOTE or LABEL.  */
3442       if (bb->end == after
3443           /* Avoid clobbering of structure when creating new BB.  */
3444           && GET_CODE (insn) != BARRIER
3445           && (GET_CODE (insn) != NOTE
3446               || NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK))
3447         bb->end = insn;
3448     }
3449
3450   NEXT_INSN (after) = insn;
3451   if (GET_CODE (after) == INSN && GET_CODE (PATTERN (after)) == SEQUENCE)
3452     {
3453       rtx sequence = PATTERN (after);
3454       NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
3455     }
3456 }
3457
3458 /* Add INSN into the doubly-linked list before insn BEFORE.  This and
3459    the previous should be the only functions called to insert an insn once
3460    delay slots have been filled since only they know how to update a
3461    SEQUENCE.  */
3462
3463 void
3464 add_insn_before (insn, before)
3465      rtx insn, before;
3466 {
3467   rtx prev = PREV_INSN (before);
3468   basic_block bb;
3469
3470   if (optimize && INSN_DELETED_P (before))
3471     abort ();
3472
3473   PREV_INSN (insn) = prev;
3474   NEXT_INSN (insn) = before;
3475
3476   if (prev)
3477     {
3478       NEXT_INSN (prev) = insn;
3479       if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3480         {
3481           rtx sequence = PATTERN (prev);
3482           NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
3483         }
3484     }
3485   else if (first_insn == before)
3486     first_insn = insn;
3487   else
3488     {
3489       struct sequence_stack *stack = seq_stack;
3490       /* Scan all pending sequences too.  */
3491       for (; stack; stack = stack->next)
3492         if (before == stack->first)
3493           {
3494             stack->first = insn;
3495             break;
3496           }
3497
3498       if (stack == 0)
3499         abort ();
3500     }
3501
3502   if (GET_CODE (before) != BARRIER
3503       && GET_CODE (insn) != BARRIER
3504       && (bb = BLOCK_FOR_INSN (before)))
3505     {
3506       set_block_for_insn (insn, bb);
3507       if (INSN_P (insn))
3508         bb->flags |= BB_DIRTY;
3509       /* Should not happen as first in the BB is always
3510          either NOTE or LABEl.  */
3511       if (bb->head == insn
3512           /* Avoid clobbering of structure when creating new BB.  */
3513           && GET_CODE (insn) != BARRIER
3514           && (GET_CODE (insn) != NOTE
3515               || NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK))
3516         abort ();
3517     }
3518
3519   PREV_INSN (before) = insn;
3520   if (GET_CODE (before) == INSN && GET_CODE (PATTERN (before)) == SEQUENCE)
3521     PREV_INSN (XVECEXP (PATTERN (before), 0, 0)) = insn;
3522 }
3523
3524 /* Remove an insn from its doubly-linked list.  This function knows how
3525    to handle sequences.  */
3526 void
3527 remove_insn (insn)
3528      rtx insn;
3529 {
3530   rtx next = NEXT_INSN (insn);
3531   rtx prev = PREV_INSN (insn);
3532   basic_block bb;
3533
3534   if (prev)
3535     {
3536       NEXT_INSN (prev) = next;
3537       if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3538         {
3539           rtx sequence = PATTERN (prev);
3540           NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = next;
3541         }
3542     }
3543   else if (first_insn == insn)
3544     first_insn = next;
3545   else
3546     {
3547       struct sequence_stack *stack = seq_stack;
3548       /* Scan all pending sequences too.  */
3549       for (; stack; stack = stack->next)
3550         if (insn == stack->first)
3551           {
3552             stack->first = next;
3553             break;
3554           }
3555
3556       if (stack == 0)
3557         abort ();
3558     }
3559
3560   if (next)
3561     {
3562       PREV_INSN (next) = prev;
3563       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3564         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3565     }
3566   else if (last_insn == insn)
3567     last_insn = prev;
3568   else
3569     {
3570       struct sequence_stack *stack = seq_stack;
3571       /* Scan all pending sequences too.  */
3572       for (; stack; stack = stack->next)
3573         if (insn == stack->last)
3574           {
3575             stack->last = prev;
3576             break;
3577           }
3578
3579       if (stack == 0)
3580         abort ();
3581     }
3582   if (GET_CODE (insn) != BARRIER
3583       && (bb = BLOCK_FOR_INSN (insn)))
3584     {
3585       if (INSN_P (insn))
3586         bb->flags |= BB_DIRTY;
3587       if (bb->head == insn)
3588         {
3589           /* Never ever delete the basic block note without deleting whole
3590              basic block.  */
3591           if (GET_CODE (insn) == NOTE)
3592             abort ();
3593           bb->head = next;
3594         }
3595       if (bb->end == insn)
3596         bb->end = prev;
3597     }
3598 }
3599
3600 /* Delete all insns made since FROM.
3601    FROM becomes the new last instruction.  */
3602
3603 void
3604 delete_insns_since (from)
3605      rtx from;
3606 {
3607   if (from == 0)
3608     first_insn = 0;
3609   else
3610     NEXT_INSN (from) = 0;
3611   last_insn = from;
3612 }
3613
3614 /* This function is deprecated, please use sequences instead.
3615
3616    Move a consecutive bunch of insns to a different place in the chain.
3617    The insns to be moved are those between FROM and TO.
3618    They are moved to a new position after the insn AFTER.
3619    AFTER must not be FROM or TO or any insn in between.
3620
3621    This function does not know about SEQUENCEs and hence should not be
3622    called after delay-slot filling has been done.  */
3623
3624 void
3625 reorder_insns_nobb (from, to, after)
3626      rtx from, to, after;
3627 {
3628   /* Splice this bunch out of where it is now.  */
3629   if (PREV_INSN (from))
3630     NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
3631   if (NEXT_INSN (to))
3632     PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
3633   if (last_insn == to)
3634     last_insn = PREV_INSN (from);
3635   if (first_insn == from)
3636     first_insn = NEXT_INSN (to);
3637
3638   /* Make the new neighbors point to it and it to them.  */
3639   if (NEXT_INSN (after))
3640     PREV_INSN (NEXT_INSN (after)) = to;
3641
3642   NEXT_INSN (to) = NEXT_INSN (after);
3643   PREV_INSN (from) = after;
3644   NEXT_INSN (after) = from;
3645   if (after == last_insn)
3646     last_insn = to;
3647 }
3648
3649 /* Same as function above, but take care to update BB boundaries.  */
3650 void
3651 reorder_insns (from, to, after)
3652      rtx from, to, after;
3653 {
3654   rtx prev = PREV_INSN (from);
3655   basic_block bb, bb2;
3656
3657   reorder_insns_nobb (from, to, after);
3658
3659   if (GET_CODE (after) != BARRIER
3660       && (bb = BLOCK_FOR_INSN (after)))
3661     {
3662       rtx x;
3663       bb->flags |= BB_DIRTY;
3664
3665       if (GET_CODE (from) != BARRIER
3666           && (bb2 = BLOCK_FOR_INSN (from)))
3667         {
3668           if (bb2->end == to)
3669             bb2->end = prev;
3670           bb2->flags |= BB_DIRTY;
3671         }
3672
3673       if (bb->end == after)
3674         bb->end = to;
3675
3676       for (x = from; x != NEXT_INSN (to); x = NEXT_INSN (x))
3677         set_block_for_insn (x, bb);
3678     }
3679 }
3680
3681 /* Return the line note insn preceding INSN.  */
3682
3683 static rtx
3684 find_line_note (insn)
3685      rtx insn;
3686 {
3687   if (no_line_numbers)
3688     return 0;
3689
3690   for (; insn; insn = PREV_INSN (insn))
3691     if (GET_CODE (insn) == NOTE
3692         && NOTE_LINE_NUMBER (insn) >= 0)
3693       break;
3694
3695   return insn;
3696 }
3697
3698 /* Like reorder_insns, but inserts line notes to preserve the line numbers
3699    of the moved insns when debugging.  This may insert a note between AFTER
3700    and FROM, and another one after TO.  */
3701
3702 void
3703 reorder_insns_with_line_notes (from, to, after)
3704      rtx from, to, after;
3705 {
3706   rtx from_line = find_line_note (from);
3707   rtx after_line = find_line_note (after);
3708
3709   reorder_insns (from, to, after);
3710
3711   if (from_line == after_line)
3712     return;
3713
3714   if (from_line)
3715     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
3716                           NOTE_LINE_NUMBER (from_line),
3717                           after);
3718   if (after_line)
3719     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
3720                           NOTE_LINE_NUMBER (after_line),
3721                           to);
3722 }
3723
3724 /* Remove unnecessary notes from the instruction stream.  */
3725
3726 void
3727 remove_unnecessary_notes ()
3728 {
3729   rtx block_stack = NULL_RTX;
3730   rtx eh_stack = NULL_RTX;
3731   rtx insn;
3732   rtx next;
3733   rtx tmp;
3734
3735   /* We must not remove the first instruction in the function because
3736      the compiler depends on the first instruction being a note.  */
3737   for (insn = NEXT_INSN (get_insns ()); insn; insn = next)
3738     {
3739       /* Remember what's next.  */
3740       next = NEXT_INSN (insn);
3741
3742       /* We're only interested in notes.  */
3743       if (GET_CODE (insn) != NOTE)
3744         continue;
3745
3746       switch (NOTE_LINE_NUMBER (insn))
3747         {
3748         case NOTE_INSN_DELETED:
3749         case NOTE_INSN_LOOP_END_TOP_COND:
3750           remove_insn (insn);
3751           break;
3752
3753         case NOTE_INSN_EH_REGION_BEG:
3754           eh_stack = alloc_INSN_LIST (insn, eh_stack);
3755           break;
3756
3757         case NOTE_INSN_EH_REGION_END:
3758           /* Too many end notes.  */
3759           if (eh_stack == NULL_RTX)
3760             abort ();
3761           /* Mismatched nesting.  */
3762           if (NOTE_EH_HANDLER (XEXP (eh_stack, 0)) != NOTE_EH_HANDLER (insn))
3763             abort ();
3764           tmp = eh_stack;
3765           eh_stack = XEXP (eh_stack, 1);
3766           free_INSN_LIST_node (tmp);
3767           break;
3768
3769         case NOTE_INSN_BLOCK_BEG:
3770           /* By now, all notes indicating lexical blocks should have
3771              NOTE_BLOCK filled in.  */
3772           if (NOTE_BLOCK (insn) == NULL_TREE)
3773             abort ();
3774           block_stack = alloc_INSN_LIST (insn, block_stack);
3775           break;
3776
3777         case NOTE_INSN_BLOCK_END:
3778           /* Too many end notes.  */
3779           if (block_stack == NULL_RTX)
3780             abort ();
3781           /* Mismatched nesting.  */
3782           if (NOTE_BLOCK (XEXP (block_stack, 0)) != NOTE_BLOCK (insn))
3783             abort ();
3784           tmp = block_stack;
3785           block_stack = XEXP (block_stack, 1);
3786           free_INSN_LIST_node (tmp);
3787
3788           /* Scan back to see if there are any non-note instructions
3789              between INSN and the beginning of this block.  If not,
3790              then there is no PC range in the generated code that will
3791              actually be in this block, so there's no point in
3792              remembering the existence of the block.  */
3793           for (tmp = PREV_INSN (insn); tmp; tmp = PREV_INSN (tmp))
3794             {
3795               /* This block contains a real instruction.  Note that we
3796                  don't include labels; if the only thing in the block
3797                  is a label, then there are still no PC values that
3798                  lie within the block.  */
3799               if (INSN_P (tmp))
3800                 break;
3801
3802               /* We're only interested in NOTEs.  */
3803               if (GET_CODE (tmp) != NOTE)
3804                 continue;
3805
3806               if (NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BLOCK_BEG)
3807                 {
3808                   /* We just verified that this BLOCK matches us with
3809                      the block_stack check above.  Never delete the
3810                      BLOCK for the outermost scope of the function; we
3811                      can refer to names from that scope even if the
3812                      block notes are messed up.  */
3813                   if (! is_body_block (NOTE_BLOCK (insn))
3814                       && (*debug_hooks->ignore_block) (NOTE_BLOCK (insn)))
3815                     {
3816                       remove_insn (tmp);
3817                       remove_insn (insn);
3818                     }
3819                   break;
3820                 }
3821               else if (NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BLOCK_END)
3822                 /* There's a nested block.  We need to leave the
3823                    current block in place since otherwise the debugger
3824                    wouldn't be able to show symbols from our block in
3825                    the nested block.  */
3826                 break;
3827             }
3828         }
3829     }
3830
3831   /* Too many begin notes.  */
3832   if (block_stack || eh_stack)
3833     abort ();
3834 }
3835
3836 \f
3837 /* Emit insn(s) of given code and pattern
3838    at a specified place within the doubly-linked list.
3839
3840    All of the emit_foo global entry points accept an object
3841    X which is either an insn list or a PATTERN of a single
3842    instruction.
3843
3844    There are thus a few canonical ways to generate code and
3845    emit it at a specific place in the instruction stream.  For
3846    example, consider the instruction named SPOT and the fact that
3847    we would like to emit some instructions before SPOT.  We might
3848    do it like this:
3849
3850         start_sequence ();
3851         ... emit the new instructions ...
3852         insns_head = get_insns ();
3853         end_sequence ();
3854
3855         emit_insn_before (insns_head, SPOT);
3856
3857    It used to be common to generate SEQUENCE rtl instead, but that
3858    is a relic of the past which no longer occurs.  The reason is that
3859    SEQUENCE rtl results in much fragmented RTL memory since the SEQUENCE
3860    generated would almost certainly die right after it was created.  */
3861
3862 /* Make X be output before the instruction BEFORE.  */
3863
3864 rtx
3865 emit_insn_before (x, before)
3866      rtx x, before;
3867 {
3868   rtx last = before;
3869   rtx insn;
3870
3871 #ifdef ENABLE_RTL_CHECKING
3872   if (before == NULL_RTX)
3873     abort ();
3874 #endif
3875
3876   if (x == NULL_RTX)
3877     return last;
3878
3879   switch (GET_CODE (x))
3880     {
3881     case INSN:
3882     case JUMP_INSN:
3883     case CALL_INSN:
3884     case CODE_LABEL:
3885     case BARRIER:
3886     case NOTE:
3887       insn = x;
3888       while (insn)
3889         {
3890           rtx next = NEXT_INSN (insn);
3891           add_insn_before (insn, before);
3892           last = insn;
3893           insn = next;
3894         }
3895       break;
3896
3897 #ifdef ENABLE_RTL_CHECKING
3898     case SEQUENCE:
3899       abort ();
3900       break;
3901 #endif
3902
3903     default:
3904       last = make_insn_raw (x);
3905       add_insn_before (last, before);
3906       break;
3907     }
3908
3909   return last;
3910 }
3911
3912 /* Make an instruction with body X and code JUMP_INSN
3913    and output it before the instruction BEFORE.  */
3914
3915 rtx
3916 emit_jump_insn_before (x, before)
3917      rtx x, before;
3918 {
3919   rtx insn, last;
3920
3921 #ifdef ENABLE_RTL_CHECKING
3922   if (before == NULL_RTX)
3923     abort ();
3924 #endif
3925
3926   switch (GET_CODE (x))
3927     {
3928     case INSN:
3929     case JUMP_INSN:
3930     case CALL_INSN:
3931     case CODE_LABEL:
3932     case BARRIER:
3933     case NOTE:
3934       insn = x;
3935       while (insn)
3936         {
3937           rtx next = NEXT_INSN (insn);
3938           add_insn_before (insn, before);
3939           last = insn;
3940           insn = next;
3941         }
3942       break;
3943
3944 #ifdef ENABLE_RTL_CHECKING
3945     case SEQUENCE:
3946       abort ();
3947       break;
3948 #endif
3949
3950     default:
3951       last = make_jump_insn_raw (x);
3952       add_insn_before (last, before);
3953       break;
3954     }
3955
3956   return last;
3957 }
3958
3959 /* Make an instruction with body X and code CALL_INSN
3960    and output it before the instruction BEFORE.  */
3961
3962 rtx
3963 emit_call_insn_before (x, before)
3964      rtx x, before;
3965 {
3966   rtx last, insn;
3967
3968 #ifdef ENABLE_RTL_CHECKING
3969   if (before == NULL_RTX)
3970     abort ();
3971 #endif
3972
3973   switch (GET_CODE (x))
3974     {
3975     case INSN:
3976     case JUMP_INSN:
3977     case CALL_INSN:
3978     case CODE_LABEL:
3979     case BARRIER:
3980     case NOTE:
3981       insn = x;
3982       while (insn)
3983         {
3984           rtx next = NEXT_INSN (insn);
3985           add_insn_before (insn, before);
3986           last = insn;
3987           insn = next;
3988         }
3989       break;
3990
3991 #ifdef ENABLE_RTL_CHECKING
3992     case SEQUENCE:
3993       abort ();
3994       break;
3995 #endif
3996
3997     default:
3998       last = make_call_insn_raw (x);
3999       add_insn_before (last, before);
4000       break;
4001     }
4002
4003   return last;
4004 }
4005
4006 /* Make an insn of code BARRIER
4007    and output it before the insn BEFORE.  */
4008
4009 rtx
4010 emit_barrier_before (before)
4011      rtx before;
4012 {
4013   rtx insn = rtx_alloc (BARRIER);
4014
4015   INSN_UID (insn) = cur_insn_uid++;
4016
4017   add_insn_before (insn, before);
4018   return insn;
4019 }
4020
4021 /* Emit the label LABEL before the insn BEFORE.  */
4022
4023 rtx
4024 emit_label_before (label, before)
4025      rtx label, before;
4026 {
4027   /* This can be called twice for the same label as a result of the
4028      confusion that follows a syntax error!  So make it harmless.  */
4029   if (INSN_UID (label) == 0)
4030     {
4031       INSN_UID (label) = cur_insn_uid++;
4032       add_insn_before (label, before);
4033     }
4034
4035   return label;
4036 }
4037
4038 /* Emit a note of subtype SUBTYPE before the insn BEFORE.  */
4039
4040 rtx
4041 emit_note_before (subtype, before)
4042      int subtype;
4043      rtx before;
4044 {
4045   rtx note = rtx_alloc (NOTE);
4046   INSN_UID (note) = cur_insn_uid++;
4047   NOTE_SOURCE_FILE (note) = 0;
4048   NOTE_LINE_NUMBER (note) = subtype;
4049   BLOCK_FOR_INSN (note) = NULL;
4050
4051   add_insn_before (note, before);
4052   return note;
4053 }
4054 \f
4055 /* Helper for emit_insn_after, handles lists of instructions
4056    efficiently.  */
4057
4058 static rtx emit_insn_after_1 PARAMS ((rtx, rtx));
4059
4060 static rtx
4061 emit_insn_after_1 (first, after)
4062      rtx first, after;
4063 {
4064   rtx last;
4065   rtx after_after;
4066   basic_block bb;
4067
4068   if (GET_CODE (after) != BARRIER
4069       && (bb = BLOCK_FOR_INSN (after)))
4070     {
4071       bb->flags |= BB_DIRTY;
4072       for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4073         if (GET_CODE (last) != BARRIER)
4074           set_block_for_insn (last, bb);
4075       if (GET_CODE (last) != BARRIER)
4076         set_block_for_insn (last, bb);
4077       if (bb->end == after)
4078         bb->end = last;
4079     }
4080   else
4081     for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4082       continue;
4083
4084   after_after = NEXT_INSN (after);
4085
4086   NEXT_INSN (after) = first;
4087   PREV_INSN (first) = after;
4088   NEXT_INSN (last) = after_after;
4089   if (after_after)
4090     PREV_INSN (after_after) = last;
4091
4092   if (after == last_insn)
4093     last_insn = last;
4094   return last;
4095 }
4096
4097 /* Make X be output after the insn AFTER.  */
4098
4099 rtx
4100 emit_insn_after (x, after)
4101      rtx x, after;
4102 {
4103   rtx last = after;
4104
4105 #ifdef ENABLE_RTL_CHECKING
4106   if (after == NULL_RTX)
4107     abort ();
4108 #endif
4109
4110   if (x == NULL_RTX)
4111     return last;
4112
4113   switch (GET_CODE (x))
4114     {
4115     case INSN:
4116     case JUMP_INSN:
4117     case CALL_INSN:
4118     case CODE_LABEL:
4119     case BARRIER:
4120     case NOTE:
4121       last = emit_insn_after_1 (x, after);
4122       break;
4123
4124 #ifdef ENABLE_RTL_CHECKING
4125     case SEQUENCE:
4126       abort ();
4127       break;
4128 #endif
4129
4130     default:
4131       last = make_insn_raw (x);
4132       add_insn_after (last, after);
4133       break;
4134     }
4135
4136   return last;
4137 }
4138
4139 /* Similar to emit_insn_after, except that line notes are to be inserted so
4140    as to act as if this insn were at FROM.  */
4141
4142 void
4143 emit_insn_after_with_line_notes (x, after, from)
4144      rtx x, after, from;
4145 {
4146   rtx from_line = find_line_note (from);
4147   rtx after_line = find_line_note (after);
4148   rtx insn = emit_insn_after (x, after);
4149
4150   if (from_line)
4151     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
4152                           NOTE_LINE_NUMBER (from_line),
4153                           after);
4154
4155   if (after_line)
4156     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
4157                           NOTE_LINE_NUMBER (after_line),
4158                           insn);
4159 }
4160
4161 /* Make an insn of code JUMP_INSN with body X
4162    and output it after the insn AFTER.  */
4163
4164 rtx
4165 emit_jump_insn_after (x, after)
4166      rtx x, after;
4167 {
4168   rtx last;
4169
4170 #ifdef ENABLE_RTL_CHECKING
4171   if (after == NULL_RTX)
4172     abort ();
4173 #endif
4174
4175   switch (GET_CODE (x))
4176     {
4177     case INSN:
4178     case JUMP_INSN:
4179     case CALL_INSN:
4180     case CODE_LABEL:
4181     case BARRIER:
4182     case NOTE:
4183       last = emit_insn_after_1 (x, after);
4184       break;
4185
4186 #ifdef ENABLE_RTL_CHECKING
4187     case SEQUENCE:
4188       abort ();
4189       break;
4190 #endif
4191
4192     default:
4193       last = make_jump_insn_raw (x);
4194       add_insn_after (last, after);
4195       break;
4196     }
4197
4198   return last;
4199 }
4200
4201 /* Make an instruction with body X and code CALL_INSN
4202    and output it after the instruction AFTER.  */
4203
4204 rtx
4205 emit_call_insn_after (x, after)
4206      rtx x, after;
4207 {
4208   rtx last;
4209
4210 #ifdef ENABLE_RTL_CHECKING
4211   if (after == NULL_RTX)
4212     abort ();
4213 #endif
4214
4215   switch (GET_CODE (x))
4216     {
4217     case INSN:
4218     case JUMP_INSN:
4219     case CALL_INSN:
4220     case CODE_LABEL:
4221     case BARRIER:
4222     case NOTE:
4223       last = emit_insn_after_1 (x, after);
4224       break;
4225
4226 #ifdef ENABLE_RTL_CHECKING
4227     case SEQUENCE:
4228       abort ();
4229       break;
4230 #endif
4231
4232     default:
4233       last = make_call_insn_raw (x);
4234       add_insn_after (last, after);
4235       break;
4236     }
4237
4238   return last;
4239 }
4240
4241 /* Make an insn of code BARRIER
4242    and output it after the insn AFTER.  */
4243
4244 rtx
4245 emit_barrier_after (after)
4246      rtx after;
4247 {
4248   rtx insn = rtx_alloc (BARRIER);
4249
4250   INSN_UID (insn) = cur_insn_uid++;
4251
4252   add_insn_after (insn, after);
4253   return insn;
4254 }
4255
4256 /* Emit the label LABEL after the insn AFTER.  */
4257
4258 rtx
4259 emit_label_after (label, after)
4260      rtx label, after;
4261 {
4262   /* This can be called twice for the same label
4263      as a result of the confusion that follows a syntax error!
4264      So make it harmless.  */
4265   if (INSN_UID (label) == 0)
4266     {
4267       INSN_UID (label) = cur_insn_uid++;
4268       add_insn_after (label, after);
4269     }
4270
4271   return label;
4272 }
4273
4274 /* Emit a note of subtype SUBTYPE after the insn AFTER.  */
4275
4276 rtx
4277 emit_note_after (subtype, after)
4278      int subtype;
4279      rtx after;
4280 {
4281   rtx note = rtx_alloc (NOTE);
4282   INSN_UID (note) = cur_insn_uid++;
4283   NOTE_SOURCE_FILE (note) = 0;
4284   NOTE_LINE_NUMBER (note) = subtype;
4285   BLOCK_FOR_INSN (note) = NULL;
4286   add_insn_after (note, after);
4287   return note;
4288 }
4289
4290 /* Emit a line note for FILE and LINE after the insn AFTER.  */
4291
4292 rtx
4293 emit_line_note_after (file, line, after)
4294      const char *file;
4295      int line;
4296      rtx after;
4297 {
4298   rtx note;
4299
4300   if (no_line_numbers && line > 0)
4301     {
4302       cur_insn_uid++;
4303       return 0;
4304     }
4305
4306   note = rtx_alloc (NOTE);
4307   INSN_UID (note) = cur_insn_uid++;
4308   NOTE_SOURCE_FILE (note) = file;
4309   NOTE_LINE_NUMBER (note) = line;
4310   BLOCK_FOR_INSN (note) = NULL;
4311   add_insn_after (note, after);
4312   return note;
4313 }
4314 \f
4315 /* Like emit_insn_after, but set INSN_SCOPE according to SCOPE.  */
4316 rtx
4317 emit_insn_after_scope (pattern, after, scope)
4318      rtx pattern, after;
4319      tree scope;
4320 {
4321   rtx last = emit_insn_after (pattern, after);
4322
4323   after = NEXT_INSN (after);
4324   while (1)
4325     {
4326       if (active_insn_p (after))
4327         INSN_SCOPE (after) = scope;
4328       if (after == last)
4329         break;
4330       after = NEXT_INSN (after);
4331     }
4332   return last;
4333 }
4334
4335 /* Like emit_jump_insn_after, but set INSN_SCOPE according to SCOPE.  */
4336 rtx
4337 emit_jump_insn_after_scope (pattern, after, scope)
4338      rtx pattern, after;
4339      tree scope;
4340 {
4341   rtx last = emit_jump_insn_after (pattern, after);
4342
4343   after = NEXT_INSN (after);
4344   while (1)
4345     {
4346       if (active_insn_p (after))
4347         INSN_SCOPE (after) = scope;
4348       if (after == last)
4349         break;
4350       after = NEXT_INSN (after);
4351     }
4352   return last;
4353 }
4354
4355 /* Like emit_call_insn_after, but set INSN_SCOPE according to SCOPE.  */
4356 rtx
4357 emit_call_insn_after_scope (pattern, after, scope)
4358      rtx pattern, after;
4359      tree scope;
4360 {
4361   rtx last = emit_call_insn_after (pattern, after);
4362
4363   after = NEXT_INSN (after);
4364   while (1)
4365     {
4366       if (active_insn_p (after))
4367         INSN_SCOPE (after) = scope;
4368       if (after == last)
4369         break;
4370       after = NEXT_INSN (after);
4371     }
4372   return last;
4373 }
4374
4375 /* Like emit_insn_before, but set INSN_SCOPE according to SCOPE.  */
4376 rtx
4377 emit_insn_before_scope (pattern, before, scope)
4378      rtx pattern, before;
4379      tree scope;
4380 {
4381   rtx first = PREV_INSN (before);
4382   rtx last = emit_insn_before (pattern, before);
4383
4384   first = NEXT_INSN (first);
4385   while (1)
4386     {
4387       if (active_insn_p (first))
4388         INSN_SCOPE (first) = scope;
4389       if (first == last)
4390         break;
4391       first = NEXT_INSN (first);
4392     }
4393   return last;
4394 }
4395 \f
4396 /* Take X and emit it at the end of the doubly-linked
4397    INSN list.
4398
4399    Returns the last insn emitted.  */
4400
4401 rtx
4402 emit_insn (x)
4403      rtx x;
4404 {
4405   rtx last = last_insn;
4406   rtx insn;
4407
4408   if (x == NULL_RTX)
4409     return last;
4410
4411   switch (GET_CODE (x))
4412     {
4413     case INSN:
4414     case JUMP_INSN:
4415     case CALL_INSN:
4416     case CODE_LABEL:
4417     case BARRIER:
4418     case NOTE:
4419       insn = x;
4420       while (insn)
4421         {
4422           rtx next = NEXT_INSN (insn);
4423           add_insn (insn);
4424           last = insn;
4425           insn = next;
4426         }
4427       break;
4428
4429 #ifdef ENABLE_RTL_CHECKING
4430     case SEQUENCE:
4431       abort ();
4432       break;
4433 #endif
4434
4435     default:
4436       last = make_insn_raw (x);
4437       add_insn (last);
4438       break;
4439     }
4440
4441   return last;
4442 }
4443
4444 /* Make an insn of code JUMP_INSN with pattern X
4445    and add it to the end of the doubly-linked list.  */
4446
4447 rtx
4448 emit_jump_insn (x)
4449      rtx x;
4450 {
4451   rtx last, insn;
4452
4453   switch (GET_CODE (x))
4454     {
4455     case INSN:
4456     case JUMP_INSN:
4457     case CALL_INSN:
4458     case CODE_LABEL:
4459     case BARRIER:
4460     case NOTE:
4461       insn = x;
4462       while (insn)
4463         {
4464           rtx next = NEXT_INSN (insn);
4465           add_insn (insn);
4466           last = insn;
4467           insn = next;
4468         }
4469       break;
4470
4471 #ifdef ENABLE_RTL_CHECKING
4472     case SEQUENCE:
4473       abort ();
4474       break;
4475 #endif
4476
4477     default:
4478       last = make_jump_insn_raw (x);
4479       add_insn (last);
4480       break;
4481     }
4482
4483   return last;
4484 }
4485
4486 /* Make an insn of code CALL_INSN with pattern X
4487    and add it to the end of the doubly-linked list.  */
4488
4489 rtx
4490 emit_call_insn (x)
4491      rtx x;
4492 {
4493   rtx insn;
4494
4495   switch (GET_CODE (x))
4496     {
4497     case INSN:
4498     case JUMP_INSN:
4499     case CALL_INSN:
4500     case CODE_LABEL:
4501     case BARRIER:
4502     case NOTE:
4503       insn = emit_insn (x);
4504       break;
4505
4506 #ifdef ENABLE_RTL_CHECKING
4507     case SEQUENCE:
4508       abort ();
4509       break;
4510 #endif
4511
4512     default:
4513       insn = make_call_insn_raw (x);
4514       add_insn (insn);
4515       break;
4516     }
4517
4518   return insn;
4519 }
4520
4521 /* Add the label LABEL to the end of the doubly-linked list.  */
4522
4523 rtx
4524 emit_label (label)
4525      rtx label;
4526 {
4527   /* This can be called twice for the same label
4528      as a result of the confusion that follows a syntax error!
4529      So make it harmless.  */
4530   if (INSN_UID (label) == 0)
4531     {
4532       INSN_UID (label) = cur_insn_uid++;
4533       add_insn (label);
4534     }
4535   return label;
4536 }
4537
4538 /* Make an insn of code BARRIER
4539    and add it to the end of the doubly-linked list.  */
4540
4541 rtx
4542 emit_barrier ()
4543 {
4544   rtx barrier = rtx_alloc (BARRIER);
4545   INSN_UID (barrier) = cur_insn_uid++;
4546   add_insn (barrier);
4547   return barrier;
4548 }
4549
4550 /* Make an insn of code NOTE
4551    with data-fields specified by FILE and LINE
4552    and add it to the end of the doubly-linked list,
4553    but only if line-numbers are desired for debugging info.  */
4554
4555 rtx
4556 emit_line_note (file, line)
4557      const char *file;
4558      int line;
4559 {
4560   set_file_and_line_for_stmt (file, line);
4561
4562 #if 0
4563   if (no_line_numbers)
4564     return 0;
4565 #endif
4566
4567   return emit_note (file, line);
4568 }
4569
4570 /* Make an insn of code NOTE
4571    with data-fields specified by FILE and LINE
4572    and add it to the end of the doubly-linked list.
4573    If it is a line-number NOTE, omit it if it matches the previous one.  */
4574
4575 rtx
4576 emit_note (file, line)
4577      const char *file;
4578      int line;
4579 {
4580   rtx note;
4581
4582   if (line > 0)
4583     {
4584       if (file && last_filename && !strcmp (file, last_filename)
4585           && line == last_linenum)
4586         return 0;
4587       last_filename = file;
4588       last_linenum = line;
4589     }
4590
4591   if (no_line_numbers && line > 0)
4592     {
4593       cur_insn_uid++;
4594       return 0;
4595     }
4596
4597   note = rtx_alloc (NOTE);
4598   INSN_UID (note) = cur_insn_uid++;
4599   NOTE_SOURCE_FILE (note) = file;
4600   NOTE_LINE_NUMBER (note) = line;
4601   BLOCK_FOR_INSN (note) = NULL;
4602   add_insn (note);
4603   return note;
4604 }
4605
4606 /* Emit a NOTE, and don't omit it even if LINE is the previous note.  */
4607
4608 rtx
4609 emit_line_note_force (file, line)
4610      const char *file;
4611      int line;
4612 {
4613   last_linenum = -1;
4614   return emit_line_note (file, line);
4615 }
4616
4617 /* Cause next statement to emit a line note even if the line number
4618    has not changed.  This is used at the beginning of a function.  */
4619
4620 void
4621 force_next_line_note ()
4622 {
4623   last_linenum = -1;
4624 }
4625
4626 /* Place a note of KIND on insn INSN with DATUM as the datum. If a
4627    note of this type already exists, remove it first.  */
4628
4629 rtx
4630 set_unique_reg_note (insn, kind, datum)
4631      rtx insn;
4632      enum reg_note kind;
4633      rtx datum;
4634 {
4635   rtx note = find_reg_note (insn, kind, NULL_RTX);
4636
4637   switch (kind)
4638     {
4639     case REG_EQUAL:
4640     case REG_EQUIV:
4641       /* Don't add REG_EQUAL/REG_EQUIV notes if the insn
4642          has multiple sets (some callers assume single_set
4643          means the insn only has one set, when in fact it
4644          means the insn only has one * useful * set).  */
4645       if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
4646         {
4647           if (note)
4648             abort ();
4649           return NULL_RTX;
4650         }
4651
4652       /* Don't add ASM_OPERAND REG_EQUAL/REG_EQUIV notes.
4653          It serves no useful purpose and breaks eliminate_regs.  */
4654       if (GET_CODE (datum) == ASM_OPERANDS)
4655         return NULL_RTX;
4656       break;
4657
4658     default:
4659       break;
4660     }
4661
4662   if (note)
4663     {
4664       XEXP (note, 0) = datum;
4665       return note;
4666     }
4667
4668   REG_NOTES (insn) = gen_rtx_EXPR_LIST (kind, datum, REG_NOTES (insn));
4669   return REG_NOTES (insn);
4670 }
4671 \f
4672 /* Return an indication of which type of insn should have X as a body.
4673    The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN.  */
4674
4675 enum rtx_code
4676 classify_insn (x)
4677      rtx x;
4678 {
4679   if (GET_CODE (x) == CODE_LABEL)
4680     return CODE_LABEL;
4681   if (GET_CODE (x) == CALL)
4682     return CALL_INSN;
4683   if (GET_CODE (x) == RETURN)
4684     return JUMP_INSN;
4685   if (GET_CODE (x) == SET)
4686     {
4687       if (SET_DEST (x) == pc_rtx)
4688         return JUMP_INSN;
4689       else if (GET_CODE (SET_SRC (x)) == CALL)
4690         return CALL_INSN;
4691       else
4692         return INSN;
4693     }
4694   if (GET_CODE (x) == PARALLEL)
4695     {
4696       int j;
4697       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
4698         if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
4699           return CALL_INSN;
4700         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
4701                  && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
4702           return JUMP_INSN;
4703         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
4704                  && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
4705           return CALL_INSN;
4706     }
4707   return INSN;
4708 }
4709
4710 /* Emit the rtl pattern X as an appropriate kind of insn.
4711    If X is a label, it is simply added into the insn chain.  */
4712
4713 rtx
4714 emit (x)
4715      rtx x;
4716 {
4717   enum rtx_code code = classify_insn (x);
4718
4719   if (code == CODE_LABEL)
4720     return emit_label (x);
4721   else if (code == INSN)
4722     return emit_insn (x);
4723   else if (code == JUMP_INSN)
4724     {
4725       rtx insn = emit_jump_insn (x);
4726       if (any_uncondjump_p (insn) || GET_CODE (x) == RETURN)
4727         return emit_barrier ();
4728       return insn;
4729     }
4730   else if (code == CALL_INSN)
4731     return emit_call_insn (x);
4732   else
4733     abort ();
4734 }
4735 \f
4736 /* Space for free sequence stack entries.  */
4737 static GTY ((deletable (""))) struct sequence_stack *free_sequence_stack;
4738
4739 /* Begin emitting insns to a sequence which can be packaged in an
4740    RTL_EXPR.  If this sequence will contain something that might cause
4741    the compiler to pop arguments to function calls (because those
4742    pops have previously been deferred; see INHIBIT_DEFER_POP for more
4743    details), use do_pending_stack_adjust before calling this function.
4744    That will ensure that the deferred pops are not accidentally
4745    emitted in the middle of this sequence.  */
4746
4747 void
4748 start_sequence ()
4749 {
4750   struct sequence_stack *tem;
4751
4752   if (free_sequence_stack != NULL)
4753     {
4754       tem = free_sequence_stack;
4755       free_sequence_stack = tem->next;
4756     }
4757   else
4758     tem = (struct sequence_stack *) ggc_alloc (sizeof (struct sequence_stack));
4759
4760   tem->next = seq_stack;
4761   tem->first = first_insn;
4762   tem->last = last_insn;
4763   tem->sequence_rtl_expr = seq_rtl_expr;
4764
4765   seq_stack = tem;
4766
4767   first_insn = 0;
4768   last_insn = 0;
4769 }
4770
4771 /* Similarly, but indicate that this sequence will be placed in T, an
4772    RTL_EXPR.  See the documentation for start_sequence for more
4773    information about how to use this function.  */
4774
4775 void
4776 start_sequence_for_rtl_expr (t)
4777      tree t;
4778 {
4779   start_sequence ();
4780
4781   seq_rtl_expr = t;
4782 }
4783
4784 /* Set up the insn chain starting with FIRST as the current sequence,
4785    saving the previously current one.  See the documentation for
4786    start_sequence for more information about how to use this function.  */
4787
4788 void
4789 push_to_sequence (first)
4790      rtx first;
4791 {
4792   rtx last;
4793
4794   start_sequence ();
4795
4796   for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last));
4797
4798   first_insn = first;
4799   last_insn = last;
4800 }
4801
4802 /* Set up the insn chain from a chain stort in FIRST to LAST.  */
4803
4804 void
4805 push_to_full_sequence (first, last)
4806      rtx first, last;
4807 {
4808   start_sequence ();
4809   first_insn = first;
4810   last_insn = last;
4811   /* We really should have the end of the insn chain here.  */
4812   if (last && NEXT_INSN (last))
4813     abort ();
4814 }
4815
4816 /* Set up the outer-level insn chain
4817    as the current sequence, saving the previously current one.  */
4818
4819 void
4820 push_topmost_sequence ()
4821 {
4822   struct sequence_stack *stack, *top = NULL;
4823
4824   start_sequence ();
4825
4826   for (stack = seq_stack; stack; stack = stack->next)
4827     top = stack;
4828
4829   first_insn = top->first;
4830   last_insn = top->last;
4831   seq_rtl_expr = top->sequence_rtl_expr;
4832 }
4833
4834 /* After emitting to the outer-level insn chain, update the outer-level
4835    insn chain, and restore the previous saved state.  */
4836
4837 void
4838 pop_topmost_sequence ()
4839 {
4840   struct sequence_stack *stack, *top = NULL;
4841
4842   for (stack = seq_stack; stack; stack = stack->next)
4843     top = stack;
4844
4845   top->first = first_insn;
4846   top->last = last_insn;
4847   /* ??? Why don't we save seq_rtl_expr here?  */
4848
4849   end_sequence ();
4850 }
4851
4852 /* After emitting to a sequence, restore previous saved state.
4853
4854    To get the contents of the sequence just made, you must call
4855    `get_insns' *before* calling here.
4856
4857    If the compiler might have deferred popping arguments while
4858    generating this sequence, and this sequence will not be immediately
4859    inserted into the instruction stream, use do_pending_stack_adjust
4860    before calling get_insns.  That will ensure that the deferred
4861    pops are inserted into this sequence, and not into some random
4862    location in the instruction stream.  See INHIBIT_DEFER_POP for more
4863    information about deferred popping of arguments.  */
4864
4865 void
4866 end_sequence ()
4867 {
4868   struct sequence_stack *tem = seq_stack;
4869
4870   first_insn = tem->first;
4871   last_insn = tem->last;
4872   seq_rtl_expr = tem->sequence_rtl_expr;
4873   seq_stack = tem->next;
4874
4875   memset (tem, 0, sizeof (*tem));
4876   tem->next = free_sequence_stack;
4877   free_sequence_stack = tem;
4878 }
4879
4880 /* This works like end_sequence, but records the old sequence in FIRST
4881    and LAST.  */
4882
4883 void
4884 end_full_sequence (first, last)
4885      rtx *first, *last;
4886 {
4887   *first = first_insn;
4888   *last = last_insn;
4889   end_sequence ();
4890 }
4891
4892 /* Return 1 if currently emitting into a sequence.  */
4893
4894 int
4895 in_sequence_p ()
4896 {
4897   return seq_stack != 0;
4898 }
4899 \f
4900 /* Put the various virtual registers into REGNO_REG_RTX.  */
4901
4902 void
4903 init_virtual_regs (es)
4904      struct emit_status *es;
4905 {
4906   rtx *ptr = es->x_regno_reg_rtx;
4907   ptr[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
4908   ptr[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
4909   ptr[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
4910   ptr[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
4911   ptr[VIRTUAL_CFA_REGNUM] = virtual_cfa_rtx;
4912 }
4913
4914 \f
4915 /* Used by copy_insn_1 to avoid copying SCRATCHes more than once.  */
4916 static rtx copy_insn_scratch_in[MAX_RECOG_OPERANDS];
4917 static rtx copy_insn_scratch_out[MAX_RECOG_OPERANDS];
4918 static int copy_insn_n_scratches;
4919
4920 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
4921    copied an ASM_OPERANDS.
4922    In that case, it is the original input-operand vector.  */
4923 static rtvec orig_asm_operands_vector;
4924
4925 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
4926    copied an ASM_OPERANDS.
4927    In that case, it is the copied input-operand vector.  */
4928 static rtvec copy_asm_operands_vector;
4929
4930 /* Likewise for the constraints vector.  */
4931 static rtvec orig_asm_constraints_vector;
4932 static rtvec copy_asm_constraints_vector;
4933
4934 /* Recursively create a new copy of an rtx for copy_insn.
4935    This function differs from copy_rtx in that it handles SCRATCHes and
4936    ASM_OPERANDs properly.
4937    Normally, this function is not used directly; use copy_insn as front end.
4938    However, you could first copy an insn pattern with copy_insn and then use
4939    this function afterwards to properly copy any REG_NOTEs containing
4940    SCRATCHes.  */
4941
4942 rtx
4943 copy_insn_1 (orig)
4944      rtx orig;
4945 {
4946   rtx copy;
4947   int i, j;
4948   RTX_CODE code;
4949   const char *format_ptr;
4950
4951   code = GET_CODE (orig);
4952
4953   switch (code)
4954     {
4955     case REG:
4956     case QUEUED:
4957     case CONST_INT:
4958     case CONST_DOUBLE:
4959     case CONST_VECTOR:
4960     case SYMBOL_REF:
4961     case CODE_LABEL:
4962     case PC:
4963     case CC0:
4964     case ADDRESSOF:
4965       return orig;
4966
4967     case SCRATCH:
4968       for (i = 0; i < copy_insn_n_scratches; i++)
4969         if (copy_insn_scratch_in[i] == orig)
4970           return copy_insn_scratch_out[i];
4971       break;
4972
4973     case CONST:
4974       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
4975          a LABEL_REF, it isn't sharable.  */
4976       if (GET_CODE (XEXP (orig, 0)) == PLUS
4977           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
4978           && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
4979         return orig;
4980       break;
4981
4982       /* A MEM with a constant address is not sharable.  The problem is that
4983          the constant address may need to be reloaded.  If the mem is shared,
4984          then reloading one copy of this mem will cause all copies to appear
4985          to have been reloaded.  */
4986
4987     default:
4988       break;
4989     }
4990
4991   copy = rtx_alloc (code);
4992
4993   /* Copy the various flags, and other information.  We assume that
4994      all fields need copying, and then clear the fields that should
4995      not be copied.  That is the sensible default behavior, and forces
4996      us to explicitly document why we are *not* copying a flag.  */
4997   memcpy (copy, orig, sizeof (struct rtx_def) - sizeof (rtunion));
4998
4999   /* We do not copy the USED flag, which is used as a mark bit during
5000      walks over the RTL.  */
5001   RTX_FLAG (copy, used) = 0;
5002
5003   /* We do not copy JUMP, CALL, or FRAME_RELATED for INSNs.  */
5004   if (GET_RTX_CLASS (code) == 'i')
5005     {
5006       RTX_FLAG (copy, jump) = 0;
5007       RTX_FLAG (copy, call) = 0;
5008       RTX_FLAG (copy, frame_related) = 0;
5009     }
5010
5011   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
5012
5013   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
5014     {
5015       copy->fld[i] = orig->fld[i];
5016       switch (*format_ptr++)
5017         {
5018         case 'e':
5019           if (XEXP (orig, i) != NULL)
5020             XEXP (copy, i) = copy_insn_1 (XEXP (orig, i));
5021           break;
5022
5023         case 'E':
5024         case 'V':
5025           if (XVEC (orig, i) == orig_asm_constraints_vector)
5026             XVEC (copy, i) = copy_asm_constraints_vector;
5027           else if (XVEC (orig, i) == orig_asm_operands_vector)
5028             XVEC (copy, i) = copy_asm_operands_vector;
5029           else if (XVEC (orig, i) != NULL)
5030             {
5031               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
5032               for (j = 0; j < XVECLEN (copy, i); j++)
5033                 XVECEXP (copy, i, j) = copy_insn_1 (XVECEXP (orig, i, j));
5034             }
5035           break;
5036
5037         case 't':
5038         case 'w':
5039         case 'i':
5040         case 's':
5041         case 'S':
5042         case 'u':
5043         case '0':
5044           /* These are left unchanged.  */
5045           break;
5046
5047         default:
5048           abort ();
5049         }
5050     }
5051
5052   if (code == SCRATCH)
5053     {
5054       i = copy_insn_n_scratches++;
5055       if (i >= MAX_RECOG_OPERANDS)
5056         abort ();
5057       copy_insn_scratch_in[i] = orig;
5058       copy_insn_scratch_out[i] = copy;
5059     }
5060   else if (code == ASM_OPERANDS)
5061     {
5062       orig_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (orig);
5063       copy_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (copy);
5064       orig_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (orig);
5065       copy_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (copy);
5066     }
5067
5068   return copy;
5069 }
5070
5071 /* Create a new copy of an rtx.
5072    This function differs from copy_rtx in that it handles SCRATCHes and
5073    ASM_OPERANDs properly.
5074    INSN doesn't really have to be a full INSN; it could be just the
5075    pattern.  */
5076 rtx
5077 copy_insn (insn)
5078      rtx insn;
5079 {
5080   copy_insn_n_scratches = 0;
5081   orig_asm_operands_vector = 0;
5082   orig_asm_constraints_vector = 0;
5083   copy_asm_operands_vector = 0;
5084   copy_asm_constraints_vector = 0;
5085   return copy_insn_1 (insn);
5086 }
5087
5088 /* Initialize data structures and variables in this file
5089    before generating rtl for each function.  */
5090
5091 void
5092 init_emit ()
5093 {
5094   struct function *f = cfun;
5095
5096   f->emit = (struct emit_status *) ggc_alloc (sizeof (struct emit_status));
5097   first_insn = NULL;
5098   last_insn = NULL;
5099   seq_rtl_expr = NULL;
5100   cur_insn_uid = 1;
5101   reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
5102   last_linenum = 0;
5103   last_filename = 0;
5104   first_label_num = label_num;
5105   last_label_num = 0;
5106   seq_stack = NULL;
5107
5108   /* Init the tables that describe all the pseudo regs.  */
5109
5110   f->emit->regno_pointer_align_length = LAST_VIRTUAL_REGISTER + 101;
5111
5112   f->emit->regno_pointer_align
5113     = (unsigned char *) ggc_alloc_cleared (f->emit->regno_pointer_align_length
5114                                            * sizeof (unsigned char));
5115
5116   regno_reg_rtx
5117     = (rtx *) ggc_alloc_cleared (f->emit->regno_pointer_align_length
5118                                  * sizeof (rtx));
5119
5120   f->emit->regno_decl
5121     = (tree *) ggc_alloc_cleared (f->emit->regno_pointer_align_length
5122                                   * sizeof (tree));
5123
5124   /* Put copies of all the hard registers into regno_reg_rtx.  */
5125   memcpy (regno_reg_rtx,
5126           static_regno_reg_rtx,
5127           FIRST_PSEUDO_REGISTER * sizeof (rtx));
5128
5129   /* Put copies of all the virtual register rtx into regno_reg_rtx.  */
5130   init_virtual_regs (f->emit);
5131
5132   /* Indicate that the virtual registers and stack locations are
5133      all pointers.  */
5134   REG_POINTER (stack_pointer_rtx) = 1;
5135   REG_POINTER (frame_pointer_rtx) = 1;
5136   REG_POINTER (hard_frame_pointer_rtx) = 1;
5137   REG_POINTER (arg_pointer_rtx) = 1;
5138
5139   REG_POINTER (virtual_incoming_args_rtx) = 1;
5140   REG_POINTER (virtual_stack_vars_rtx) = 1;
5141   REG_POINTER (virtual_stack_dynamic_rtx) = 1;
5142   REG_POINTER (virtual_outgoing_args_rtx) = 1;
5143   REG_POINTER (virtual_cfa_rtx) = 1;
5144
5145 #ifdef STACK_BOUNDARY
5146   REGNO_POINTER_ALIGN (STACK_POINTER_REGNUM) = STACK_BOUNDARY;
5147   REGNO_POINTER_ALIGN (FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5148   REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5149   REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) = STACK_BOUNDARY;
5150
5151   REGNO_POINTER_ALIGN (VIRTUAL_INCOMING_ARGS_REGNUM) = STACK_BOUNDARY;
5152   REGNO_POINTER_ALIGN (VIRTUAL_STACK_VARS_REGNUM) = STACK_BOUNDARY;
5153   REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM) = STACK_BOUNDARY;
5154   REGNO_POINTER_ALIGN (VIRTUAL_OUTGOING_ARGS_REGNUM) = STACK_BOUNDARY;
5155   REGNO_POINTER_ALIGN (VIRTUAL_CFA_REGNUM) = BITS_PER_WORD;
5156 #endif
5157
5158 #ifdef INIT_EXPANDERS
5159   INIT_EXPANDERS;
5160 #endif
5161 }
5162
5163 /* Generate the constant 0.  */
5164
5165 static rtx
5166 gen_const_vector_0 (mode)
5167      enum machine_mode mode;
5168 {
5169   rtx tem;
5170   rtvec v;
5171   int units, i;
5172   enum machine_mode inner;
5173
5174   units = GET_MODE_NUNITS (mode);
5175   inner = GET_MODE_INNER (mode);
5176
5177   v = rtvec_alloc (units);
5178
5179   /* We need to call this function after we to set CONST0_RTX first.  */
5180   if (!CONST0_RTX (inner))
5181     abort ();
5182
5183   for (i = 0; i < units; ++i)
5184     RTVEC_ELT (v, i) = CONST0_RTX (inner);
5185
5186   tem = gen_rtx_CONST_VECTOR (mode, v);
5187   return tem;
5188 }
5189
5190 /* Create some permanent unique rtl objects shared between all functions.
5191    LINE_NUMBERS is nonzero if line numbers are to be generated.  */
5192
5193 void
5194 init_emit_once (line_numbers)
5195      int line_numbers;
5196 {
5197   int i;
5198   enum machine_mode mode;
5199   enum machine_mode double_mode;
5200
5201   /* Initialize the CONST_INT, CONST_DOUBLE, and memory attribute hash
5202      tables.  */
5203   const_int_htab = htab_create (37, const_int_htab_hash,
5204                                 const_int_htab_eq, NULL);
5205
5206   const_double_htab = htab_create (37, const_double_htab_hash,
5207                                    const_double_htab_eq, NULL);
5208
5209   mem_attrs_htab = htab_create (37, mem_attrs_htab_hash,
5210                                 mem_attrs_htab_eq, NULL);
5211
5212   no_line_numbers = ! line_numbers;
5213
5214   /* Compute the word and byte modes.  */
5215
5216   byte_mode = VOIDmode;
5217   word_mode = VOIDmode;
5218   double_mode = VOIDmode;
5219
5220   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
5221        mode = GET_MODE_WIDER_MODE (mode))
5222     {
5223       if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
5224           && byte_mode == VOIDmode)
5225         byte_mode = mode;
5226
5227       if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
5228           && word_mode == VOIDmode)
5229         word_mode = mode;
5230     }
5231
5232   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
5233        mode = GET_MODE_WIDER_MODE (mode))
5234     {
5235       if (GET_MODE_BITSIZE (mode) == DOUBLE_TYPE_SIZE
5236           && double_mode == VOIDmode)
5237         double_mode = mode;
5238     }
5239
5240   ptr_mode = mode_for_size (POINTER_SIZE, GET_MODE_CLASS (Pmode), 0);
5241
5242   /* Assign register numbers to the globally defined register rtx.
5243      This must be done at runtime because the register number field
5244      is in a union and some compilers can't initialize unions.  */
5245
5246   pc_rtx = gen_rtx (PC, VOIDmode);
5247   cc0_rtx = gen_rtx (CC0, VOIDmode);
5248   stack_pointer_rtx = gen_raw_REG (Pmode, STACK_POINTER_REGNUM);
5249   frame_pointer_rtx = gen_raw_REG (Pmode, FRAME_POINTER_REGNUM);
5250   if (hard_frame_pointer_rtx == 0)
5251     hard_frame_pointer_rtx = gen_raw_REG (Pmode,
5252                                           HARD_FRAME_POINTER_REGNUM);
5253   if (arg_pointer_rtx == 0)
5254     arg_pointer_rtx = gen_raw_REG (Pmode, ARG_POINTER_REGNUM);
5255   virtual_incoming_args_rtx =
5256     gen_raw_REG (Pmode, VIRTUAL_INCOMING_ARGS_REGNUM);
5257   virtual_stack_vars_rtx =
5258     gen_raw_REG (Pmode, VIRTUAL_STACK_VARS_REGNUM);
5259   virtual_stack_dynamic_rtx =
5260     gen_raw_REG (Pmode, VIRTUAL_STACK_DYNAMIC_REGNUM);
5261   virtual_outgoing_args_rtx =
5262     gen_raw_REG (Pmode, VIRTUAL_OUTGOING_ARGS_REGNUM);
5263   virtual_cfa_rtx = gen_raw_REG (Pmode, VIRTUAL_CFA_REGNUM);
5264
5265   /* Initialize RTL for commonly used hard registers.  These are
5266      copied into regno_reg_rtx as we begin to compile each function.  */
5267   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5268     static_regno_reg_rtx[i] = gen_raw_REG (reg_raw_mode[i], i);
5269
5270 #ifdef INIT_EXPANDERS
5271   /* This is to initialize {init|mark|free}_machine_status before the first
5272      call to push_function_context_to.  This is needed by the Chill front
5273      end which calls push_function_context_to before the first call to
5274      init_function_start.  */
5275   INIT_EXPANDERS;
5276 #endif
5277
5278   /* Create the unique rtx's for certain rtx codes and operand values.  */
5279
5280   /* Don't use gen_rtx here since gen_rtx in this case
5281      tries to use these variables.  */
5282   for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
5283     const_int_rtx[i + MAX_SAVED_CONST_INT] =
5284       gen_rtx_raw_CONST_INT (VOIDmode, i);
5285
5286   if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT
5287       && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT)
5288     const_true_rtx = const_int_rtx[STORE_FLAG_VALUE + MAX_SAVED_CONST_INT];
5289   else
5290     const_true_rtx = gen_rtx_CONST_INT (VOIDmode, STORE_FLAG_VALUE);
5291
5292   REAL_VALUE_FROM_INT (dconst0,   0,  0, double_mode);
5293   REAL_VALUE_FROM_INT (dconst1,   1,  0, double_mode);
5294   REAL_VALUE_FROM_INT (dconst2,   2,  0, double_mode);
5295   REAL_VALUE_FROM_INT (dconstm1, -1, -1, double_mode);
5296
5297   for (i = 0; i <= 2; i++)
5298     {
5299       REAL_VALUE_TYPE *r =
5300         (i == 0 ? &dconst0 : i == 1 ? &dconst1 : &dconst2);
5301
5302       for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
5303            mode = GET_MODE_WIDER_MODE (mode))
5304         const_tiny_rtx[i][(int) mode] =
5305           CONST_DOUBLE_FROM_REAL_VALUE (*r, mode);
5306
5307       const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
5308
5309       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
5310            mode = GET_MODE_WIDER_MODE (mode))
5311         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5312
5313       for (mode = GET_CLASS_NARROWEST_MODE (MODE_PARTIAL_INT);
5314            mode != VOIDmode;
5315            mode = GET_MODE_WIDER_MODE (mode))
5316         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5317     }
5318
5319   for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
5320        mode != VOIDmode;
5321        mode = GET_MODE_WIDER_MODE (mode))
5322     const_tiny_rtx[0][(int) mode] = gen_const_vector_0 (mode);
5323
5324   for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
5325        mode != VOIDmode;
5326        mode = GET_MODE_WIDER_MODE (mode))
5327     const_tiny_rtx[0][(int) mode] = gen_const_vector_0 (mode);
5328
5329   for (i = (int) CCmode; i < (int) MAX_MACHINE_MODE; ++i)
5330     if (GET_MODE_CLASS ((enum machine_mode) i) == MODE_CC)
5331       const_tiny_rtx[0][i] = const0_rtx;
5332
5333   const_tiny_rtx[0][(int) BImode] = const0_rtx;
5334   if (STORE_FLAG_VALUE == 1)
5335     const_tiny_rtx[1][(int) BImode] = const1_rtx;
5336
5337 #ifdef RETURN_ADDRESS_POINTER_REGNUM
5338   return_address_pointer_rtx
5339     = gen_raw_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM);
5340 #endif
5341
5342 #ifdef STRUCT_VALUE
5343   struct_value_rtx = STRUCT_VALUE;
5344 #else
5345   struct_value_rtx = gen_rtx_REG (Pmode, STRUCT_VALUE_REGNUM);
5346 #endif
5347
5348 #ifdef STRUCT_VALUE_INCOMING
5349   struct_value_incoming_rtx = STRUCT_VALUE_INCOMING;
5350 #else
5351 #ifdef STRUCT_VALUE_INCOMING_REGNUM
5352   struct_value_incoming_rtx
5353     = gen_rtx_REG (Pmode, STRUCT_VALUE_INCOMING_REGNUM);
5354 #else
5355   struct_value_incoming_rtx = struct_value_rtx;
5356 #endif
5357 #endif
5358
5359 #ifdef STATIC_CHAIN_REGNUM
5360   static_chain_rtx = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
5361
5362 #ifdef STATIC_CHAIN_INCOMING_REGNUM
5363   if (STATIC_CHAIN_INCOMING_REGNUM != STATIC_CHAIN_REGNUM)
5364     static_chain_incoming_rtx
5365       = gen_rtx_REG (Pmode, STATIC_CHAIN_INCOMING_REGNUM);
5366   else
5367 #endif
5368     static_chain_incoming_rtx = static_chain_rtx;
5369 #endif
5370
5371 #ifdef STATIC_CHAIN
5372   static_chain_rtx = STATIC_CHAIN;
5373
5374 #ifdef STATIC_CHAIN_INCOMING
5375   static_chain_incoming_rtx = STATIC_CHAIN_INCOMING;
5376 #else
5377   static_chain_incoming_rtx = static_chain_rtx;
5378 #endif
5379 #endif
5380
5381   if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
5382     pic_offset_table_rtx = gen_raw_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
5383 }
5384 \f
5385 /* Query and clear/ restore no_line_numbers.  This is used by the
5386    switch / case handling in stmt.c to give proper line numbers in
5387    warnings about unreachable code.  */
5388
5389 int
5390 force_line_numbers ()
5391 {
5392   int old = no_line_numbers;
5393
5394   no_line_numbers = 0;
5395   if (old)
5396     force_next_line_note ();
5397   return old;
5398 }
5399
5400 void
5401 restore_line_number_status (old_value)
5402      int old_value;
5403 {
5404   no_line_numbers = old_value;
5405 }
5406
5407 /* Produce exact duplicate of insn INSN after AFTER.
5408    Care updating of libcall regions if present.  */
5409
5410 rtx
5411 emit_copy_of_insn_after (insn, after)
5412      rtx insn, after;
5413 {
5414   rtx new;
5415   rtx note1, note2, link;
5416
5417   switch (GET_CODE (insn))
5418     {
5419     case INSN:
5420       new = emit_insn_after (copy_insn (PATTERN (insn)), after);
5421       break;
5422
5423     case JUMP_INSN:
5424       new = emit_jump_insn_after (copy_insn (PATTERN (insn)), after);
5425       break;
5426
5427     case CALL_INSN:
5428       new = emit_call_insn_after (copy_insn (PATTERN (insn)), after);
5429       if (CALL_INSN_FUNCTION_USAGE (insn))
5430         CALL_INSN_FUNCTION_USAGE (new)
5431           = copy_insn (CALL_INSN_FUNCTION_USAGE (insn));
5432       SIBLING_CALL_P (new) = SIBLING_CALL_P (insn);
5433       CONST_OR_PURE_CALL_P (new) = CONST_OR_PURE_CALL_P (insn);
5434       break;
5435
5436     default:
5437       abort ();
5438     }
5439
5440   /* Update LABEL_NUSES.  */
5441   mark_jump_label (PATTERN (new), new, 0);
5442
5443   INSN_SCOPE (new) = INSN_SCOPE (insn);
5444
5445   /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
5446      make them.  */
5447   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
5448     if (REG_NOTE_KIND (link) != REG_LABEL)
5449       {
5450         if (GET_CODE (link) == EXPR_LIST)
5451           REG_NOTES (new)
5452             = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
5453                                               XEXP (link, 0),
5454                                               REG_NOTES (new)));
5455         else
5456           REG_NOTES (new)
5457             = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link),
5458                                               XEXP (link, 0),
5459                                               REG_NOTES (new)));
5460       }
5461
5462   /* Fix the libcall sequences.  */
5463   if ((note1 = find_reg_note (new, REG_RETVAL, NULL_RTX)) != NULL)
5464     {
5465       rtx p = new;
5466       while ((note2 = find_reg_note (p, REG_LIBCALL, NULL_RTX)) == NULL)
5467         p = PREV_INSN (p);
5468       XEXP (note1, 0) = p;
5469       XEXP (note2, 0) = new;
5470     }
5471   return new;
5472 }
5473
5474 #include "gt-emit-rtl.h"