OSDN Git Service

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