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