[15/77] Add scalar_int_mode
[official-gcc.git] / gcc / emit-rtl.c
blob399c5d6b895438dbb73c7417fce346b7ee90bb87
1 /* Emit RTL for the GCC expander.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
21 /* Middle-to-low level generation of rtx code and insns.
23 This file contains support functions for creating rtl expressions
24 and manipulating them in the doubly-linked chain of insns.
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. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "memmodel.h"
38 #include "backend.h"
39 #include "target.h"
40 #include "rtl.h"
41 #include "tree.h"
42 #include "df.h"
43 #include "tm_p.h"
44 #include "stringpool.h"
45 #include "insn-config.h"
46 #include "regs.h"
47 #include "emit-rtl.h"
48 #include "recog.h"
49 #include "diagnostic-core.h"
50 #include "alias.h"
51 #include "fold-const.h"
52 #include "varasm.h"
53 #include "cfgrtl.h"
54 #include "tree-eh.h"
55 #include "explow.h"
56 #include "expr.h"
57 #include "params.h"
58 #include "builtins.h"
59 #include "rtl-iter.h"
60 #include "stor-layout.h"
61 #include "opts.h"
62 #include "predict.h"
64 struct target_rtl default_target_rtl;
65 #if SWITCHABLE_TARGET
66 struct target_rtl *this_target_rtl = &default_target_rtl;
67 #endif
69 #define initial_regno_reg_rtx (this_target_rtl->x_initial_regno_reg_rtx)
71 /* Commonly used modes. */
73 scalar_int_mode byte_mode; /* Mode whose width is BITS_PER_UNIT. */
74 scalar_int_mode word_mode; /* Mode whose width is BITS_PER_WORD. */
75 scalar_int_mode ptr_mode; /* Mode whose width is POINTER_SIZE. */
77 /* Datastructures maintained for currently processed function in RTL form. */
79 struct rtl_data x_rtl;
81 /* Indexed by pseudo register number, gives the rtx for that pseudo.
82 Allocated in parallel with regno_pointer_align.
83 FIXME: We could put it into emit_status struct, but gengtype is not able to deal
84 with length attribute nested in top level structures. */
86 rtx * regno_reg_rtx;
88 /* This is *not* reset after each function. It gives each CODE_LABEL
89 in the entire compilation a unique label number. */
91 static GTY(()) int label_num = 1;
93 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
94 the values of 0, 1, and 2. For the integer entries and VOIDmode, we
95 record a copy of const[012]_rtx and constm1_rtx. CONSTM1_RTX
96 is set only for MODE_INT and MODE_VECTOR_INT modes. */
98 rtx const_tiny_rtx[4][(int) MAX_MACHINE_MODE];
100 rtx const_true_rtx;
102 REAL_VALUE_TYPE dconst0;
103 REAL_VALUE_TYPE dconst1;
104 REAL_VALUE_TYPE dconst2;
105 REAL_VALUE_TYPE dconstm1;
106 REAL_VALUE_TYPE dconsthalf;
108 /* Record fixed-point constant 0 and 1. */
109 FIXED_VALUE_TYPE fconst0[MAX_FCONST0];
110 FIXED_VALUE_TYPE fconst1[MAX_FCONST1];
112 /* We make one copy of (const_int C) where C is in
113 [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
114 to save space during the compilation and simplify comparisons of
115 integers. */
117 rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
119 /* Standard pieces of rtx, to be substituted directly into things. */
120 rtx pc_rtx;
121 rtx ret_rtx;
122 rtx simple_return_rtx;
123 rtx cc0_rtx;
125 /* Marker used for denoting an INSN, which should never be accessed (i.e.,
126 this pointer should normally never be dereferenced), but is required to be
127 distinct from NULL_RTX. Currently used by peephole2 pass. */
128 rtx_insn *invalid_insn_rtx;
130 /* A hash table storing CONST_INTs whose absolute value is greater
131 than MAX_SAVED_CONST_INT. */
133 struct const_int_hasher : ggc_cache_ptr_hash<rtx_def>
135 typedef HOST_WIDE_INT compare_type;
137 static hashval_t hash (rtx i);
138 static bool equal (rtx i, HOST_WIDE_INT h);
141 static GTY ((cache)) hash_table<const_int_hasher> *const_int_htab;
143 struct const_wide_int_hasher : ggc_cache_ptr_hash<rtx_def>
145 static hashval_t hash (rtx x);
146 static bool equal (rtx x, rtx y);
149 static GTY ((cache)) hash_table<const_wide_int_hasher> *const_wide_int_htab;
151 /* A hash table storing register attribute structures. */
152 struct reg_attr_hasher : ggc_cache_ptr_hash<reg_attrs>
154 static hashval_t hash (reg_attrs *x);
155 static bool equal (reg_attrs *a, reg_attrs *b);
158 static GTY ((cache)) hash_table<reg_attr_hasher> *reg_attrs_htab;
160 /* A hash table storing all CONST_DOUBLEs. */
161 struct const_double_hasher : ggc_cache_ptr_hash<rtx_def>
163 static hashval_t hash (rtx x);
164 static bool equal (rtx x, rtx y);
167 static GTY ((cache)) hash_table<const_double_hasher> *const_double_htab;
169 /* A hash table storing all CONST_FIXEDs. */
170 struct const_fixed_hasher : ggc_cache_ptr_hash<rtx_def>
172 static hashval_t hash (rtx x);
173 static bool equal (rtx x, rtx y);
176 static GTY ((cache)) hash_table<const_fixed_hasher> *const_fixed_htab;
178 #define cur_insn_uid (crtl->emit.x_cur_insn_uid)
179 #define cur_debug_insn_uid (crtl->emit.x_cur_debug_insn_uid)
180 #define first_label_num (crtl->emit.x_first_label_num)
182 static void set_used_decls (tree);
183 static void mark_label_nuses (rtx);
184 #if TARGET_SUPPORTS_WIDE_INT
185 static rtx lookup_const_wide_int (rtx);
186 #endif
187 static rtx lookup_const_double (rtx);
188 static rtx lookup_const_fixed (rtx);
189 static reg_attrs *get_reg_attrs (tree, int);
190 static rtx gen_const_vector (machine_mode, int);
191 static void copy_rtx_if_shared_1 (rtx *orig);
193 /* Probability of the conditional branch currently proceeded by try_split. */
194 profile_probability split_branch_probability;
196 /* Returns a hash code for X (which is a really a CONST_INT). */
198 hashval_t
199 const_int_hasher::hash (rtx x)
201 return (hashval_t) INTVAL (x);
204 /* Returns nonzero if the value represented by X (which is really a
205 CONST_INT) is the same as that given by Y (which is really a
206 HOST_WIDE_INT *). */
208 bool
209 const_int_hasher::equal (rtx x, HOST_WIDE_INT y)
211 return (INTVAL (x) == y);
214 #if TARGET_SUPPORTS_WIDE_INT
215 /* Returns a hash code for X (which is a really a CONST_WIDE_INT). */
217 hashval_t
218 const_wide_int_hasher::hash (rtx x)
220 int i;
221 unsigned HOST_WIDE_INT hash = 0;
222 const_rtx xr = x;
224 for (i = 0; i < CONST_WIDE_INT_NUNITS (xr); i++)
225 hash += CONST_WIDE_INT_ELT (xr, i);
227 return (hashval_t) hash;
230 /* Returns nonzero if the value represented by X (which is really a
231 CONST_WIDE_INT) is the same as that given by Y (which is really a
232 CONST_WIDE_INT). */
234 bool
235 const_wide_int_hasher::equal (rtx x, rtx y)
237 int i;
238 const_rtx xr = x;
239 const_rtx yr = y;
240 if (CONST_WIDE_INT_NUNITS (xr) != CONST_WIDE_INT_NUNITS (yr))
241 return false;
243 for (i = 0; i < CONST_WIDE_INT_NUNITS (xr); i++)
244 if (CONST_WIDE_INT_ELT (xr, i) != CONST_WIDE_INT_ELT (yr, i))
245 return false;
247 return true;
249 #endif
251 /* Returns a hash code for X (which is really a CONST_DOUBLE). */
252 hashval_t
253 const_double_hasher::hash (rtx x)
255 const_rtx const value = x;
256 hashval_t h;
258 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (value) == VOIDmode)
259 h = CONST_DOUBLE_LOW (value) ^ CONST_DOUBLE_HIGH (value);
260 else
262 h = real_hash (CONST_DOUBLE_REAL_VALUE (value));
263 /* MODE is used in the comparison, so it should be in the hash. */
264 h ^= GET_MODE (value);
266 return h;
269 /* Returns nonzero if the value represented by X (really a ...)
270 is the same as that represented by Y (really a ...) */
271 bool
272 const_double_hasher::equal (rtx x, rtx y)
274 const_rtx const a = x, b = y;
276 if (GET_MODE (a) != GET_MODE (b))
277 return 0;
278 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (a) == VOIDmode)
279 return (CONST_DOUBLE_LOW (a) == CONST_DOUBLE_LOW (b)
280 && CONST_DOUBLE_HIGH (a) == CONST_DOUBLE_HIGH (b));
281 else
282 return real_identical (CONST_DOUBLE_REAL_VALUE (a),
283 CONST_DOUBLE_REAL_VALUE (b));
286 /* Returns a hash code for X (which is really a CONST_FIXED). */
288 hashval_t
289 const_fixed_hasher::hash (rtx x)
291 const_rtx const value = x;
292 hashval_t h;
294 h = fixed_hash (CONST_FIXED_VALUE (value));
295 /* MODE is used in the comparison, so it should be in the hash. */
296 h ^= GET_MODE (value);
297 return h;
300 /* Returns nonzero if the value represented by X is the same as that
301 represented by Y. */
303 bool
304 const_fixed_hasher::equal (rtx x, rtx y)
306 const_rtx const a = x, b = y;
308 if (GET_MODE (a) != GET_MODE (b))
309 return 0;
310 return fixed_identical (CONST_FIXED_VALUE (a), CONST_FIXED_VALUE (b));
313 /* Return true if the given memory attributes are equal. */
315 bool
316 mem_attrs_eq_p (const struct mem_attrs *p, const struct mem_attrs *q)
318 if (p == q)
319 return true;
320 if (!p || !q)
321 return false;
322 return (p->alias == q->alias
323 && p->offset_known_p == q->offset_known_p
324 && (!p->offset_known_p || p->offset == q->offset)
325 && p->size_known_p == q->size_known_p
326 && (!p->size_known_p || p->size == q->size)
327 && p->align == q->align
328 && p->addrspace == q->addrspace
329 && (p->expr == q->expr
330 || (p->expr != NULL_TREE && q->expr != NULL_TREE
331 && operand_equal_p (p->expr, q->expr, 0))));
334 /* Set MEM's memory attributes so that they are the same as ATTRS. */
336 static void
337 set_mem_attrs (rtx mem, mem_attrs *attrs)
339 /* If everything is the default, we can just clear the attributes. */
340 if (mem_attrs_eq_p (attrs, mode_mem_attrs[(int) GET_MODE (mem)]))
342 MEM_ATTRS (mem) = 0;
343 return;
346 if (!MEM_ATTRS (mem)
347 || !mem_attrs_eq_p (attrs, MEM_ATTRS (mem)))
349 MEM_ATTRS (mem) = ggc_alloc<mem_attrs> ();
350 memcpy (MEM_ATTRS (mem), attrs, sizeof (mem_attrs));
354 /* Returns a hash code for X (which is a really a reg_attrs *). */
356 hashval_t
357 reg_attr_hasher::hash (reg_attrs *x)
359 const reg_attrs *const p = x;
361 return ((p->offset * 1000) ^ (intptr_t) p->decl);
364 /* Returns nonzero if the value represented by X is the same as that given by
365 Y. */
367 bool
368 reg_attr_hasher::equal (reg_attrs *x, reg_attrs *y)
370 const reg_attrs *const p = x;
371 const reg_attrs *const q = y;
373 return (p->decl == q->decl && p->offset == q->offset);
375 /* Allocate a new reg_attrs structure and insert it into the hash table if
376 one identical to it is not already in the table. We are doing this for
377 MEM of mode MODE. */
379 static reg_attrs *
380 get_reg_attrs (tree decl, int offset)
382 reg_attrs attrs;
384 /* If everything is the default, we can just return zero. */
385 if (decl == 0 && offset == 0)
386 return 0;
388 attrs.decl = decl;
389 attrs.offset = offset;
391 reg_attrs **slot = reg_attrs_htab->find_slot (&attrs, INSERT);
392 if (*slot == 0)
394 *slot = ggc_alloc<reg_attrs> ();
395 memcpy (*slot, &attrs, sizeof (reg_attrs));
398 return *slot;
402 #if !HAVE_blockage
403 /* Generate an empty ASM_INPUT, which is used to block attempts to schedule,
404 and to block register equivalences to be seen across this insn. */
407 gen_blockage (void)
409 rtx x = gen_rtx_ASM_INPUT (VOIDmode, "");
410 MEM_VOLATILE_P (x) = true;
411 return x;
413 #endif
416 /* Set the mode and register number of X to MODE and REGNO. */
418 void
419 set_mode_and_regno (rtx x, machine_mode mode, unsigned int regno)
421 unsigned int nregs = (HARD_REGISTER_NUM_P (regno)
422 ? hard_regno_nregs[regno][mode]
423 : 1);
424 PUT_MODE_RAW (x, mode);
425 set_regno_raw (x, regno, nregs);
428 /* Generate a new REG rtx. Make sure ORIGINAL_REGNO is set properly, and
429 don't attempt to share with the various global pieces of rtl (such as
430 frame_pointer_rtx). */
433 gen_raw_REG (machine_mode mode, unsigned int regno)
435 rtx x = rtx_alloc (REG MEM_STAT_INFO);
436 set_mode_and_regno (x, mode, regno);
437 REG_ATTRS (x) = NULL;
438 ORIGINAL_REGNO (x) = regno;
439 return x;
442 /* There are some RTL codes that require special attention; the generation
443 functions do the raw handling. If you add to this list, modify
444 special_rtx in gengenrtl.c as well. */
446 rtx_expr_list *
447 gen_rtx_EXPR_LIST (machine_mode mode, rtx expr, rtx expr_list)
449 return as_a <rtx_expr_list *> (gen_rtx_fmt_ee (EXPR_LIST, mode, expr,
450 expr_list));
453 rtx_insn_list *
454 gen_rtx_INSN_LIST (machine_mode mode, rtx insn, rtx insn_list)
456 return as_a <rtx_insn_list *> (gen_rtx_fmt_ue (INSN_LIST, mode, insn,
457 insn_list));
460 rtx_insn *
461 gen_rtx_INSN (machine_mode mode, rtx_insn *prev_insn, rtx_insn *next_insn,
462 basic_block bb, rtx pattern, int location, int code,
463 rtx reg_notes)
465 return as_a <rtx_insn *> (gen_rtx_fmt_uuBeiie (INSN, mode,
466 prev_insn, next_insn,
467 bb, pattern, location, code,
468 reg_notes));
472 gen_rtx_CONST_INT (machine_mode mode ATTRIBUTE_UNUSED, HOST_WIDE_INT arg)
474 if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
475 return const_int_rtx[arg + MAX_SAVED_CONST_INT];
477 #if STORE_FLAG_VALUE != 1 && STORE_FLAG_VALUE != -1
478 if (const_true_rtx && arg == STORE_FLAG_VALUE)
479 return const_true_rtx;
480 #endif
482 /* Look up the CONST_INT in the hash table. */
483 rtx *slot = const_int_htab->find_slot_with_hash (arg, (hashval_t) arg,
484 INSERT);
485 if (*slot == 0)
486 *slot = gen_rtx_raw_CONST_INT (VOIDmode, arg);
488 return *slot;
492 gen_int_mode (HOST_WIDE_INT c, machine_mode mode)
494 return GEN_INT (trunc_int_for_mode (c, mode));
497 /* CONST_DOUBLEs might be created from pairs of integers, or from
498 REAL_VALUE_TYPEs. Also, their length is known only at run time,
499 so we cannot use gen_rtx_raw_CONST_DOUBLE. */
501 /* Determine whether REAL, a CONST_DOUBLE, already exists in the
502 hash table. If so, return its counterpart; otherwise add it
503 to the hash table and return it. */
504 static rtx
505 lookup_const_double (rtx real)
507 rtx *slot = const_double_htab->find_slot (real, INSERT);
508 if (*slot == 0)
509 *slot = real;
511 return *slot;
514 /* Return a CONST_DOUBLE rtx for a floating-point value specified by
515 VALUE in mode MODE. */
517 const_double_from_real_value (REAL_VALUE_TYPE value, machine_mode mode)
519 rtx real = rtx_alloc (CONST_DOUBLE);
520 PUT_MODE (real, mode);
522 real->u.rv = value;
524 return lookup_const_double (real);
527 /* Determine whether FIXED, a CONST_FIXED, already exists in the
528 hash table. If so, return its counterpart; otherwise add it
529 to the hash table and return it. */
531 static rtx
532 lookup_const_fixed (rtx fixed)
534 rtx *slot = const_fixed_htab->find_slot (fixed, INSERT);
535 if (*slot == 0)
536 *slot = fixed;
538 return *slot;
541 /* Return a CONST_FIXED rtx for a fixed-point value specified by
542 VALUE in mode MODE. */
545 const_fixed_from_fixed_value (FIXED_VALUE_TYPE value, machine_mode mode)
547 rtx fixed = rtx_alloc (CONST_FIXED);
548 PUT_MODE (fixed, mode);
550 fixed->u.fv = value;
552 return lookup_const_fixed (fixed);
555 #if TARGET_SUPPORTS_WIDE_INT == 0
556 /* Constructs double_int from rtx CST. */
558 double_int
559 rtx_to_double_int (const_rtx cst)
561 double_int r;
563 if (CONST_INT_P (cst))
564 r = double_int::from_shwi (INTVAL (cst));
565 else if (CONST_DOUBLE_AS_INT_P (cst))
567 r.low = CONST_DOUBLE_LOW (cst);
568 r.high = CONST_DOUBLE_HIGH (cst);
570 else
571 gcc_unreachable ();
573 return r;
575 #endif
577 #if TARGET_SUPPORTS_WIDE_INT
578 /* Determine whether CONST_WIDE_INT WINT already exists in the hash table.
579 If so, return its counterpart; otherwise add it to the hash table and
580 return it. */
582 static rtx
583 lookup_const_wide_int (rtx wint)
585 rtx *slot = const_wide_int_htab->find_slot (wint, INSERT);
586 if (*slot == 0)
587 *slot = wint;
589 return *slot;
591 #endif
593 /* Return an rtx constant for V, given that the constant has mode MODE.
594 The returned rtx will be a CONST_INT if V fits, otherwise it will be
595 a CONST_DOUBLE (if !TARGET_SUPPORTS_WIDE_INT) or a CONST_WIDE_INT
596 (if TARGET_SUPPORTS_WIDE_INT). */
599 immed_wide_int_const (const wide_int_ref &v, machine_mode mode)
601 unsigned int len = v.get_len ();
602 unsigned int prec = GET_MODE_PRECISION (mode);
604 /* Allow truncation but not extension since we do not know if the
605 number is signed or unsigned. */
606 gcc_assert (prec <= v.get_precision ());
608 if (len < 2 || prec <= HOST_BITS_PER_WIDE_INT)
609 return gen_int_mode (v.elt (0), mode);
611 #if TARGET_SUPPORTS_WIDE_INT
613 unsigned int i;
614 rtx value;
615 unsigned int blocks_needed
616 = (prec + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
618 if (len > blocks_needed)
619 len = blocks_needed;
621 value = const_wide_int_alloc (len);
623 /* It is so tempting to just put the mode in here. Must control
624 myself ... */
625 PUT_MODE (value, VOIDmode);
626 CWI_PUT_NUM_ELEM (value, len);
628 for (i = 0; i < len; i++)
629 CONST_WIDE_INT_ELT (value, i) = v.elt (i);
631 return lookup_const_wide_int (value);
633 #else
634 return immed_double_const (v.elt (0), v.elt (1), mode);
635 #endif
638 #if TARGET_SUPPORTS_WIDE_INT == 0
639 /* Return a CONST_DOUBLE or CONST_INT for a value specified as a pair
640 of ints: I0 is the low-order word and I1 is the high-order word.
641 For values that are larger than HOST_BITS_PER_DOUBLE_INT, the
642 implied upper bits are copies of the high bit of i1. The value
643 itself is neither signed nor unsigned. Do not use this routine for
644 non-integer modes; convert to REAL_VALUE_TYPE and use
645 const_double_from_real_value. */
648 immed_double_const (HOST_WIDE_INT i0, HOST_WIDE_INT i1, machine_mode mode)
650 rtx value;
651 unsigned int i;
653 /* There are the following cases (note that there are no modes with
654 HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode) < HOST_BITS_PER_DOUBLE_INT):
656 1) If GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT, then we use
657 gen_int_mode.
658 2) If the value of the integer fits into HOST_WIDE_INT anyway
659 (i.e., i1 consists only from copies of the sign bit, and sign
660 of i0 and i1 are the same), then we return a CONST_INT for i0.
661 3) Otherwise, we create a CONST_DOUBLE for i0 and i1. */
662 if (mode != VOIDmode)
664 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
665 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT
666 /* We can get a 0 for an error mark. */
667 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
668 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
669 || GET_MODE_CLASS (mode) == MODE_POINTER_BOUNDS);
671 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
672 return gen_int_mode (i0, mode);
675 /* If this integer fits in one word, return a CONST_INT. */
676 if ((i1 == 0 && i0 >= 0) || (i1 == ~0 && i0 < 0))
677 return GEN_INT (i0);
679 /* We use VOIDmode for integers. */
680 value = rtx_alloc (CONST_DOUBLE);
681 PUT_MODE (value, VOIDmode);
683 CONST_DOUBLE_LOW (value) = i0;
684 CONST_DOUBLE_HIGH (value) = i1;
686 for (i = 2; i < (sizeof CONST_DOUBLE_FORMAT - 1); i++)
687 XWINT (value, i) = 0;
689 return lookup_const_double (value);
691 #endif
694 gen_rtx_REG (machine_mode mode, unsigned int regno)
696 /* In case the MD file explicitly references the frame pointer, have
697 all such references point to the same frame pointer. This is
698 used during frame pointer elimination to distinguish the explicit
699 references to these registers from pseudos that happened to be
700 assigned to them.
702 If we have eliminated the frame pointer or arg pointer, we will
703 be using it as a normal register, for example as a spill
704 register. In such cases, we might be accessing it in a mode that
705 is not Pmode and therefore cannot use the pre-allocated rtx.
707 Also don't do this when we are making new REGs in reload, since
708 we don't want to get confused with the real pointers. */
710 if (mode == Pmode && !reload_in_progress && !lra_in_progress)
712 if (regno == FRAME_POINTER_REGNUM
713 && (!reload_completed || frame_pointer_needed))
714 return frame_pointer_rtx;
716 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
717 && regno == HARD_FRAME_POINTER_REGNUM
718 && (!reload_completed || frame_pointer_needed))
719 return hard_frame_pointer_rtx;
720 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
721 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
722 && regno == ARG_POINTER_REGNUM)
723 return arg_pointer_rtx;
724 #endif
725 #ifdef RETURN_ADDRESS_POINTER_REGNUM
726 if (regno == RETURN_ADDRESS_POINTER_REGNUM)
727 return return_address_pointer_rtx;
728 #endif
729 if (regno == (unsigned) PIC_OFFSET_TABLE_REGNUM
730 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
731 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
732 return pic_offset_table_rtx;
733 if (regno == STACK_POINTER_REGNUM)
734 return stack_pointer_rtx;
737 #if 0
738 /* If the per-function register table has been set up, try to re-use
739 an existing entry in that table to avoid useless generation of RTL.
741 This code is disabled for now until we can fix the various backends
742 which depend on having non-shared hard registers in some cases. Long
743 term we want to re-enable this code as it can significantly cut down
744 on the amount of useless RTL that gets generated.
746 We'll also need to fix some code that runs after reload that wants to
747 set ORIGINAL_REGNO. */
749 if (cfun
750 && cfun->emit
751 && regno_reg_rtx
752 && regno < FIRST_PSEUDO_REGISTER
753 && reg_raw_mode[regno] == mode)
754 return regno_reg_rtx[regno];
755 #endif
757 return gen_raw_REG (mode, regno);
761 gen_rtx_MEM (machine_mode mode, rtx addr)
763 rtx rt = gen_rtx_raw_MEM (mode, addr);
765 /* This field is not cleared by the mere allocation of the rtx, so
766 we clear it here. */
767 MEM_ATTRS (rt) = 0;
769 return rt;
772 /* Generate a memory referring to non-trapping constant memory. */
775 gen_const_mem (machine_mode mode, rtx addr)
777 rtx mem = gen_rtx_MEM (mode, addr);
778 MEM_READONLY_P (mem) = 1;
779 MEM_NOTRAP_P (mem) = 1;
780 return mem;
783 /* Generate a MEM referring to fixed portions of the frame, e.g., register
784 save areas. */
787 gen_frame_mem (machine_mode mode, rtx addr)
789 rtx mem = gen_rtx_MEM (mode, addr);
790 MEM_NOTRAP_P (mem) = 1;
791 set_mem_alias_set (mem, get_frame_alias_set ());
792 return mem;
795 /* Generate a MEM referring to a temporary use of the stack, not part
796 of the fixed stack frame. For example, something which is pushed
797 by a target splitter. */
799 gen_tmp_stack_mem (machine_mode mode, rtx addr)
801 rtx mem = gen_rtx_MEM (mode, addr);
802 MEM_NOTRAP_P (mem) = 1;
803 if (!cfun->calls_alloca)
804 set_mem_alias_set (mem, get_frame_alias_set ());
805 return mem;
808 /* We want to create (subreg:OMODE (obj:IMODE) OFFSET). Return true if
809 this construct would be valid, and false otherwise. */
811 bool
812 validate_subreg (machine_mode omode, machine_mode imode,
813 const_rtx reg, unsigned int offset)
815 unsigned int isize = GET_MODE_SIZE (imode);
816 unsigned int osize = GET_MODE_SIZE (omode);
818 /* All subregs must be aligned. */
819 if (offset % osize != 0)
820 return false;
822 /* The subreg offset cannot be outside the inner object. */
823 if (offset >= isize)
824 return false;
826 /* ??? This should not be here. Temporarily continue to allow word_mode
827 subregs of anything. The most common offender is (subreg:SI (reg:DF)).
828 Generally, backends are doing something sketchy but it'll take time to
829 fix them all. */
830 if (omode == word_mode)
832 /* ??? Similarly, e.g. with (subreg:DF (reg:TI)). Though store_bit_field
833 is the culprit here, and not the backends. */
834 else if (osize >= UNITS_PER_WORD && isize >= osize)
836 /* Allow component subregs of complex and vector. Though given the below
837 extraction rules, it's not always clear what that means. */
838 else if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
839 && GET_MODE_INNER (imode) == omode)
841 /* ??? x86 sse code makes heavy use of *paradoxical* vector subregs,
842 i.e. (subreg:V4SF (reg:SF) 0). This surely isn't the cleanest way to
843 represent this. It's questionable if this ought to be represented at
844 all -- why can't this all be hidden in post-reload splitters that make
845 arbitrarily mode changes to the registers themselves. */
846 else if (VECTOR_MODE_P (omode) && GET_MODE_INNER (omode) == imode)
848 /* Subregs involving floating point modes are not allowed to
849 change size. Therefore (subreg:DI (reg:DF) 0) is fine, but
850 (subreg:SI (reg:DF) 0) isn't. */
851 else if (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))
853 if (! (isize == osize
854 /* LRA can use subreg to store a floating point value in
855 an integer mode. Although the floating point and the
856 integer modes need the same number of hard registers,
857 the size of floating point mode can be less than the
858 integer mode. LRA also uses subregs for a register
859 should be used in different mode in on insn. */
860 || lra_in_progress))
861 return false;
864 /* Paradoxical subregs must have offset zero. */
865 if (osize > isize)
866 return offset == 0;
868 /* This is a normal subreg. Verify that the offset is representable. */
870 /* For hard registers, we already have most of these rules collected in
871 subreg_offset_representable_p. */
872 if (reg && REG_P (reg) && HARD_REGISTER_P (reg))
874 unsigned int regno = REGNO (reg);
876 #ifdef CANNOT_CHANGE_MODE_CLASS
877 if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
878 && GET_MODE_INNER (imode) == omode)
880 else if (REG_CANNOT_CHANGE_MODE_P (regno, imode, omode))
881 return false;
882 #endif
884 return subreg_offset_representable_p (regno, imode, offset, omode);
887 /* For pseudo registers, we want most of the same checks. Namely:
888 If the register no larger than a word, the subreg must be lowpart.
889 If the register is larger than a word, the subreg must be the lowpart
890 of a subword. A subreg does *not* perform arbitrary bit extraction.
891 Given that we've already checked mode/offset alignment, we only have
892 to check subword subregs here. */
893 if (osize < UNITS_PER_WORD
894 && ! (lra_in_progress && (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))))
896 machine_mode wmode = isize > UNITS_PER_WORD ? word_mode : imode;
897 unsigned int low_off = subreg_lowpart_offset (omode, wmode);
898 if (offset % UNITS_PER_WORD != low_off)
899 return false;
901 return true;
905 gen_rtx_SUBREG (machine_mode mode, rtx reg, int offset)
907 gcc_assert (validate_subreg (mode, GET_MODE (reg), reg, offset));
908 return gen_rtx_raw_SUBREG (mode, reg, offset);
911 /* Generate a SUBREG representing the least-significant part of REG if MODE
912 is smaller than mode of REG, otherwise paradoxical SUBREG. */
915 gen_lowpart_SUBREG (machine_mode mode, rtx reg)
917 machine_mode inmode;
919 inmode = GET_MODE (reg);
920 if (inmode == VOIDmode)
921 inmode = mode;
922 return gen_rtx_SUBREG (mode, reg,
923 subreg_lowpart_offset (mode, inmode));
927 gen_rtx_VAR_LOCATION (machine_mode mode, tree decl, rtx loc,
928 enum var_init_status status)
930 rtx x = gen_rtx_fmt_te (VAR_LOCATION, mode, decl, loc);
931 PAT_VAR_LOCATION_STATUS (x) = status;
932 return x;
936 /* Create an rtvec and stores within it the RTXen passed in the arguments. */
938 rtvec
939 gen_rtvec (int n, ...)
941 int i;
942 rtvec rt_val;
943 va_list p;
945 va_start (p, n);
947 /* Don't allocate an empty rtvec... */
948 if (n == 0)
950 va_end (p);
951 return NULL_RTVEC;
954 rt_val = rtvec_alloc (n);
956 for (i = 0; i < n; i++)
957 rt_val->elem[i] = va_arg (p, rtx);
959 va_end (p);
960 return rt_val;
963 rtvec
964 gen_rtvec_v (int n, rtx *argp)
966 int i;
967 rtvec rt_val;
969 /* Don't allocate an empty rtvec... */
970 if (n == 0)
971 return NULL_RTVEC;
973 rt_val = rtvec_alloc (n);
975 for (i = 0; i < n; i++)
976 rt_val->elem[i] = *argp++;
978 return rt_val;
981 rtvec
982 gen_rtvec_v (int n, rtx_insn **argp)
984 int i;
985 rtvec rt_val;
987 /* Don't allocate an empty rtvec... */
988 if (n == 0)
989 return NULL_RTVEC;
991 rt_val = rtvec_alloc (n);
993 for (i = 0; i < n; i++)
994 rt_val->elem[i] = *argp++;
996 return rt_val;
1000 /* Return the number of bytes between the start of an OUTER_MODE
1001 in-memory value and the start of an INNER_MODE in-memory value,
1002 given that the former is a lowpart of the latter. It may be a
1003 paradoxical lowpart, in which case the offset will be negative
1004 on big-endian targets. */
1007 byte_lowpart_offset (machine_mode outer_mode,
1008 machine_mode inner_mode)
1010 if (paradoxical_subreg_p (outer_mode, inner_mode))
1011 return -subreg_lowpart_offset (inner_mode, outer_mode);
1012 else
1013 return subreg_lowpart_offset (outer_mode, inner_mode);
1016 /* Generate a REG rtx for a new pseudo register of mode MODE.
1017 This pseudo is assigned the next sequential register number. */
1020 gen_reg_rtx (machine_mode mode)
1022 rtx val;
1023 unsigned int align = GET_MODE_ALIGNMENT (mode);
1025 gcc_assert (can_create_pseudo_p ());
1027 /* If a virtual register with bigger mode alignment is generated,
1028 increase stack alignment estimation because it might be spilled
1029 to stack later. */
1030 if (SUPPORTS_STACK_ALIGNMENT
1031 && crtl->stack_alignment_estimated < align
1032 && !crtl->stack_realign_processed)
1034 unsigned int min_align = MINIMUM_ALIGNMENT (NULL, mode, align);
1035 if (crtl->stack_alignment_estimated < min_align)
1036 crtl->stack_alignment_estimated = min_align;
1039 if (generating_concat_p
1040 && (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
1041 || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT))
1043 /* For complex modes, don't make a single pseudo.
1044 Instead, make a CONCAT of two pseudos.
1045 This allows noncontiguous allocation of the real and imaginary parts,
1046 which makes much better code. Besides, allocating DCmode
1047 pseudos overstrains reload on some machines like the 386. */
1048 rtx realpart, imagpart;
1049 machine_mode partmode = GET_MODE_INNER (mode);
1051 realpart = gen_reg_rtx (partmode);
1052 imagpart = gen_reg_rtx (partmode);
1053 return gen_rtx_CONCAT (mode, realpart, imagpart);
1056 /* Do not call gen_reg_rtx with uninitialized crtl. */
1057 gcc_assert (crtl->emit.regno_pointer_align_length);
1059 crtl->emit.ensure_regno_capacity ();
1060 gcc_assert (reg_rtx_no < crtl->emit.regno_pointer_align_length);
1062 val = gen_raw_REG (mode, reg_rtx_no);
1063 regno_reg_rtx[reg_rtx_no++] = val;
1064 return val;
1067 /* Make sure m_regno_pointer_align, and regno_reg_rtx are large
1068 enough to have elements in the range 0 <= idx <= reg_rtx_no. */
1070 void
1071 emit_status::ensure_regno_capacity ()
1073 int old_size = regno_pointer_align_length;
1075 if (reg_rtx_no < old_size)
1076 return;
1078 int new_size = old_size * 2;
1079 while (reg_rtx_no >= new_size)
1080 new_size *= 2;
1082 char *tmp = XRESIZEVEC (char, regno_pointer_align, new_size);
1083 memset (tmp + old_size, 0, new_size - old_size);
1084 regno_pointer_align = (unsigned char *) tmp;
1086 rtx *new1 = GGC_RESIZEVEC (rtx, regno_reg_rtx, new_size);
1087 memset (new1 + old_size, 0, (new_size - old_size) * sizeof (rtx));
1088 regno_reg_rtx = new1;
1090 crtl->emit.regno_pointer_align_length = new_size;
1093 /* Return TRUE if REG is a PARM_DECL, FALSE otherwise. */
1095 bool
1096 reg_is_parm_p (rtx reg)
1098 tree decl;
1100 gcc_assert (REG_P (reg));
1101 decl = REG_EXPR (reg);
1102 return (decl && TREE_CODE (decl) == PARM_DECL);
1105 /* Update NEW with the same attributes as REG, but with OFFSET added
1106 to the REG_OFFSET. */
1108 static void
1109 update_reg_offset (rtx new_rtx, rtx reg, int offset)
1111 REG_ATTRS (new_rtx) = get_reg_attrs (REG_EXPR (reg),
1112 REG_OFFSET (reg) + offset);
1115 /* Generate a register with same attributes as REG, but with OFFSET
1116 added to the REG_OFFSET. */
1119 gen_rtx_REG_offset (rtx reg, machine_mode mode, unsigned int regno,
1120 int offset)
1122 rtx new_rtx = gen_rtx_REG (mode, regno);
1124 update_reg_offset (new_rtx, reg, offset);
1125 return new_rtx;
1128 /* Generate a new pseudo-register with the same attributes as REG, but
1129 with OFFSET added to the REG_OFFSET. */
1132 gen_reg_rtx_offset (rtx reg, machine_mode mode, int offset)
1134 rtx new_rtx = gen_reg_rtx (mode);
1136 update_reg_offset (new_rtx, reg, offset);
1137 return new_rtx;
1140 /* Adjust REG in-place so that it has mode MODE. It is assumed that the
1141 new register is a (possibly paradoxical) lowpart of the old one. */
1143 void
1144 adjust_reg_mode (rtx reg, machine_mode mode)
1146 update_reg_offset (reg, reg, byte_lowpart_offset (mode, GET_MODE (reg)));
1147 PUT_MODE (reg, mode);
1150 /* Copy REG's attributes from X, if X has any attributes. If REG and X
1151 have different modes, REG is a (possibly paradoxical) lowpart of X. */
1153 void
1154 set_reg_attrs_from_value (rtx reg, rtx x)
1156 int offset;
1157 bool can_be_reg_pointer = true;
1159 /* Don't call mark_reg_pointer for incompatible pointer sign
1160 extension. */
1161 while (GET_CODE (x) == SIGN_EXTEND
1162 || GET_CODE (x) == ZERO_EXTEND
1163 || GET_CODE (x) == TRUNCATE
1164 || (GET_CODE (x) == SUBREG && subreg_lowpart_p (x)))
1166 #if defined(POINTERS_EXTEND_UNSIGNED)
1167 if (((GET_CODE (x) == SIGN_EXTEND && POINTERS_EXTEND_UNSIGNED)
1168 || (GET_CODE (x) == ZERO_EXTEND && ! POINTERS_EXTEND_UNSIGNED)
1169 || (paradoxical_subreg_p (x)
1170 && ! (SUBREG_PROMOTED_VAR_P (x)
1171 && SUBREG_CHECK_PROMOTED_SIGN (x,
1172 POINTERS_EXTEND_UNSIGNED))))
1173 && !targetm.have_ptr_extend ())
1174 can_be_reg_pointer = false;
1175 #endif
1176 x = XEXP (x, 0);
1179 /* Hard registers can be reused for multiple purposes within the same
1180 function, so setting REG_ATTRS, REG_POINTER and REG_POINTER_ALIGN
1181 on them is wrong. */
1182 if (HARD_REGISTER_P (reg))
1183 return;
1185 offset = byte_lowpart_offset (GET_MODE (reg), GET_MODE (x));
1186 if (MEM_P (x))
1188 if (MEM_OFFSET_KNOWN_P (x))
1189 REG_ATTRS (reg) = get_reg_attrs (MEM_EXPR (x),
1190 MEM_OFFSET (x) + offset);
1191 if (can_be_reg_pointer && MEM_POINTER (x))
1192 mark_reg_pointer (reg, 0);
1194 else if (REG_P (x))
1196 if (REG_ATTRS (x))
1197 update_reg_offset (reg, x, offset);
1198 if (can_be_reg_pointer && REG_POINTER (x))
1199 mark_reg_pointer (reg, REGNO_POINTER_ALIGN (REGNO (x)));
1203 /* Generate a REG rtx for a new pseudo register, copying the mode
1204 and attributes from X. */
1207 gen_reg_rtx_and_attrs (rtx x)
1209 rtx reg = gen_reg_rtx (GET_MODE (x));
1210 set_reg_attrs_from_value (reg, x);
1211 return reg;
1214 /* Set the register attributes for registers contained in PARM_RTX.
1215 Use needed values from memory attributes of MEM. */
1217 void
1218 set_reg_attrs_for_parm (rtx parm_rtx, rtx mem)
1220 if (REG_P (parm_rtx))
1221 set_reg_attrs_from_value (parm_rtx, mem);
1222 else if (GET_CODE (parm_rtx) == PARALLEL)
1224 /* Check for a NULL entry in the first slot, used to indicate that the
1225 parameter goes both on the stack and in registers. */
1226 int i = XEXP (XVECEXP (parm_rtx, 0, 0), 0) ? 0 : 1;
1227 for (; i < XVECLEN (parm_rtx, 0); i++)
1229 rtx x = XVECEXP (parm_rtx, 0, i);
1230 if (REG_P (XEXP (x, 0)))
1231 REG_ATTRS (XEXP (x, 0))
1232 = get_reg_attrs (MEM_EXPR (mem),
1233 INTVAL (XEXP (x, 1)));
1238 /* Set the REG_ATTRS for registers in value X, given that X represents
1239 decl T. */
1241 void
1242 set_reg_attrs_for_decl_rtl (tree t, rtx x)
1244 if (!t)
1245 return;
1246 tree tdecl = t;
1247 if (GET_CODE (x) == SUBREG)
1249 gcc_assert (subreg_lowpart_p (x));
1250 x = SUBREG_REG (x);
1252 if (REG_P (x))
1253 REG_ATTRS (x)
1254 = get_reg_attrs (t, byte_lowpart_offset (GET_MODE (x),
1255 DECL_P (tdecl)
1256 ? DECL_MODE (tdecl)
1257 : TYPE_MODE (TREE_TYPE (tdecl))));
1258 if (GET_CODE (x) == CONCAT)
1260 if (REG_P (XEXP (x, 0)))
1261 REG_ATTRS (XEXP (x, 0)) = get_reg_attrs (t, 0);
1262 if (REG_P (XEXP (x, 1)))
1263 REG_ATTRS (XEXP (x, 1))
1264 = get_reg_attrs (t, GET_MODE_UNIT_SIZE (GET_MODE (XEXP (x, 0))));
1266 if (GET_CODE (x) == PARALLEL)
1268 int i, start;
1270 /* Check for a NULL entry, used to indicate that the parameter goes
1271 both on the stack and in registers. */
1272 if (XEXP (XVECEXP (x, 0, 0), 0))
1273 start = 0;
1274 else
1275 start = 1;
1277 for (i = start; i < XVECLEN (x, 0); i++)
1279 rtx y = XVECEXP (x, 0, i);
1280 if (REG_P (XEXP (y, 0)))
1281 REG_ATTRS (XEXP (y, 0)) = get_reg_attrs (t, INTVAL (XEXP (y, 1)));
1286 /* Assign the RTX X to declaration T. */
1288 void
1289 set_decl_rtl (tree t, rtx x)
1291 DECL_WRTL_CHECK (t)->decl_with_rtl.rtl = x;
1292 if (x)
1293 set_reg_attrs_for_decl_rtl (t, x);
1296 /* Assign the RTX X to parameter declaration T. BY_REFERENCE_P is true
1297 if the ABI requires the parameter to be passed by reference. */
1299 void
1300 set_decl_incoming_rtl (tree t, rtx x, bool by_reference_p)
1302 DECL_INCOMING_RTL (t) = x;
1303 if (x && !by_reference_p)
1304 set_reg_attrs_for_decl_rtl (t, x);
1307 /* Identify REG (which may be a CONCAT) as a user register. */
1309 void
1310 mark_user_reg (rtx reg)
1312 if (GET_CODE (reg) == CONCAT)
1314 REG_USERVAR_P (XEXP (reg, 0)) = 1;
1315 REG_USERVAR_P (XEXP (reg, 1)) = 1;
1317 else
1319 gcc_assert (REG_P (reg));
1320 REG_USERVAR_P (reg) = 1;
1324 /* Identify REG as a probable pointer register and show its alignment
1325 as ALIGN, if nonzero. */
1327 void
1328 mark_reg_pointer (rtx reg, int align)
1330 if (! REG_POINTER (reg))
1332 REG_POINTER (reg) = 1;
1334 if (align)
1335 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1337 else if (align && align < REGNO_POINTER_ALIGN (REGNO (reg)))
1338 /* We can no-longer be sure just how aligned this pointer is. */
1339 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1342 /* Return 1 plus largest pseudo reg number used in the current function. */
1345 max_reg_num (void)
1347 return reg_rtx_no;
1350 /* Return 1 + the largest label number used so far in the current function. */
1353 max_label_num (void)
1355 return label_num;
1358 /* Return first label number used in this function (if any were used). */
1361 get_first_label_num (void)
1363 return first_label_num;
1366 /* If the rtx for label was created during the expansion of a nested
1367 function, then first_label_num won't include this label number.
1368 Fix this now so that array indices work later. */
1370 void
1371 maybe_set_first_label_num (rtx_code_label *x)
1373 if (CODE_LABEL_NUMBER (x) < first_label_num)
1374 first_label_num = CODE_LABEL_NUMBER (x);
1377 /* For use by the RTL function loader, when mingling with normal
1378 functions.
1379 Ensure that label_num is greater than the label num of X, to avoid
1380 duplicate labels in the generated assembler. */
1382 void
1383 maybe_set_max_label_num (rtx_code_label *x)
1385 if (CODE_LABEL_NUMBER (x) >= label_num)
1386 label_num = CODE_LABEL_NUMBER (x) + 1;
1390 /* Return a value representing some low-order bits of X, where the number
1391 of low-order bits is given by MODE. Note that no conversion is done
1392 between floating-point and fixed-point values, rather, the bit
1393 representation is returned.
1395 This function handles the cases in common between gen_lowpart, below,
1396 and two variants in cse.c and combine.c. These are the cases that can
1397 be safely handled at all points in the compilation.
1399 If this is not a case we can handle, return 0. */
1402 gen_lowpart_common (machine_mode mode, rtx x)
1404 int msize = GET_MODE_SIZE (mode);
1405 int xsize;
1406 machine_mode innermode;
1408 /* Unfortunately, this routine doesn't take a parameter for the mode of X,
1409 so we have to make one up. Yuk. */
1410 innermode = GET_MODE (x);
1411 if (CONST_INT_P (x)
1412 && msize * BITS_PER_UNIT <= HOST_BITS_PER_WIDE_INT)
1413 innermode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1414 else if (innermode == VOIDmode)
1415 innermode = mode_for_size (HOST_BITS_PER_DOUBLE_INT, MODE_INT, 0);
1417 xsize = GET_MODE_SIZE (innermode);
1419 gcc_assert (innermode != VOIDmode && innermode != BLKmode);
1421 if (innermode == mode)
1422 return x;
1424 /* MODE must occupy no more words than the mode of X. */
1425 if ((msize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
1426 > ((xsize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
1427 return 0;
1429 /* Don't allow generating paradoxical FLOAT_MODE subregs. */
1430 if (SCALAR_FLOAT_MODE_P (mode) && msize > xsize)
1431 return 0;
1433 if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
1434 && (GET_MODE_CLASS (mode) == MODE_INT
1435 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
1437 /* If we are getting the low-order part of something that has been
1438 sign- or zero-extended, we can either just use the object being
1439 extended or make a narrower extension. If we want an even smaller
1440 piece than the size of the object being extended, call ourselves
1441 recursively.
1443 This case is used mostly by combine and cse. */
1445 if (GET_MODE (XEXP (x, 0)) == mode)
1446 return XEXP (x, 0);
1447 else if (msize < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
1448 return gen_lowpart_common (mode, XEXP (x, 0));
1449 else if (msize < xsize)
1450 return gen_rtx_fmt_e (GET_CODE (x), mode, XEXP (x, 0));
1452 else if (GET_CODE (x) == SUBREG || REG_P (x)
1453 || GET_CODE (x) == CONCAT || GET_CODE (x) == CONST_VECTOR
1454 || CONST_DOUBLE_AS_FLOAT_P (x) || CONST_SCALAR_INT_P (x))
1455 return lowpart_subreg (mode, x, innermode);
1457 /* Otherwise, we can't do this. */
1458 return 0;
1462 gen_highpart (machine_mode mode, rtx x)
1464 unsigned int msize = GET_MODE_SIZE (mode);
1465 rtx result;
1467 /* This case loses if X is a subreg. To catch bugs early,
1468 complain if an invalid MODE is used even in other cases. */
1469 gcc_assert (msize <= UNITS_PER_WORD
1470 || msize == (unsigned int) GET_MODE_UNIT_SIZE (GET_MODE (x)));
1472 result = simplify_gen_subreg (mode, x, GET_MODE (x),
1473 subreg_highpart_offset (mode, GET_MODE (x)));
1474 gcc_assert (result);
1476 /* simplify_gen_subreg is not guaranteed to return a valid operand for
1477 the target if we have a MEM. gen_highpart must return a valid operand,
1478 emitting code if necessary to do so. */
1479 if (MEM_P (result))
1481 result = validize_mem (result);
1482 gcc_assert (result);
1485 return result;
1488 /* Like gen_highpart, but accept mode of EXP operand in case EXP can
1489 be VOIDmode constant. */
1491 gen_highpart_mode (machine_mode outermode, machine_mode innermode, rtx exp)
1493 if (GET_MODE (exp) != VOIDmode)
1495 gcc_assert (GET_MODE (exp) == innermode);
1496 return gen_highpart (outermode, exp);
1498 return simplify_gen_subreg (outermode, exp, innermode,
1499 subreg_highpart_offset (outermode, innermode));
1502 /* Return the SUBREG_BYTE for a lowpart subreg whose outer mode has
1503 OUTER_BYTES bytes and whose inner mode has INNER_BYTES bytes. */
1505 unsigned int
1506 subreg_size_lowpart_offset (unsigned int outer_bytes, unsigned int inner_bytes)
1508 if (outer_bytes > inner_bytes)
1509 /* Paradoxical subregs always have a SUBREG_BYTE of 0. */
1510 return 0;
1512 if (BYTES_BIG_ENDIAN && WORDS_BIG_ENDIAN)
1513 return inner_bytes - outer_bytes;
1514 else if (!BYTES_BIG_ENDIAN && !WORDS_BIG_ENDIAN)
1515 return 0;
1516 else
1517 return subreg_size_offset_from_lsb (outer_bytes, inner_bytes, 0);
1520 /* Return the SUBREG_BYTE for a highpart subreg whose outer mode has
1521 OUTER_BYTES bytes and whose inner mode has INNER_BYTES bytes. */
1523 unsigned int
1524 subreg_size_highpart_offset (unsigned int outer_bytes,
1525 unsigned int inner_bytes)
1527 gcc_assert (inner_bytes >= outer_bytes);
1529 if (BYTES_BIG_ENDIAN && WORDS_BIG_ENDIAN)
1530 return 0;
1531 else if (!BYTES_BIG_ENDIAN && !WORDS_BIG_ENDIAN)
1532 return inner_bytes - outer_bytes;
1533 else
1534 return subreg_size_offset_from_lsb (outer_bytes, inner_bytes,
1535 (inner_bytes - outer_bytes)
1536 * BITS_PER_UNIT);
1539 /* Return 1 iff X, assumed to be a SUBREG,
1540 refers to the least significant part of its containing reg.
1541 If X is not a SUBREG, always return 1 (it is its own low part!). */
1544 subreg_lowpart_p (const_rtx x)
1546 if (GET_CODE (x) != SUBREG)
1547 return 1;
1548 else if (GET_MODE (SUBREG_REG (x)) == VOIDmode)
1549 return 0;
1551 return (subreg_lowpart_offset (GET_MODE (x), GET_MODE (SUBREG_REG (x)))
1552 == SUBREG_BYTE (x));
1555 /* Return subword OFFSET of operand OP.
1556 The word number, OFFSET, is interpreted as the word number starting
1557 at the low-order address. OFFSET 0 is the low-order word if not
1558 WORDS_BIG_ENDIAN, otherwise it is the high-order word.
1560 If we cannot extract the required word, we return zero. Otherwise,
1561 an rtx corresponding to the requested word will be returned.
1563 VALIDATE_ADDRESS is nonzero if the address should be validated. Before
1564 reload has completed, a valid address will always be returned. After
1565 reload, if a valid address cannot be returned, we return zero.
1567 If VALIDATE_ADDRESS is zero, we simply form the required address; validating
1568 it is the responsibility of the caller.
1570 MODE is the mode of OP in case it is a CONST_INT.
1572 ??? This is still rather broken for some cases. The problem for the
1573 moment is that all callers of this thing provide no 'goal mode' to
1574 tell us to work with. This exists because all callers were written
1575 in a word based SUBREG world.
1576 Now use of this function can be deprecated by simplify_subreg in most
1577 cases.
1581 operand_subword (rtx op, unsigned int offset, int validate_address, machine_mode mode)
1583 if (mode == VOIDmode)
1584 mode = GET_MODE (op);
1586 gcc_assert (mode != VOIDmode);
1588 /* If OP is narrower than a word, fail. */
1589 if (mode != BLKmode
1590 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD))
1591 return 0;
1593 /* If we want a word outside OP, return zero. */
1594 if (mode != BLKmode
1595 && (offset + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode))
1596 return const0_rtx;
1598 /* Form a new MEM at the requested address. */
1599 if (MEM_P (op))
1601 rtx new_rtx = adjust_address_nv (op, word_mode, offset * UNITS_PER_WORD);
1603 if (! validate_address)
1604 return new_rtx;
1606 else if (reload_completed)
1608 if (! strict_memory_address_addr_space_p (word_mode,
1609 XEXP (new_rtx, 0),
1610 MEM_ADDR_SPACE (op)))
1611 return 0;
1613 else
1614 return replace_equiv_address (new_rtx, XEXP (new_rtx, 0));
1617 /* Rest can be handled by simplify_subreg. */
1618 return simplify_gen_subreg (word_mode, op, mode, (offset * UNITS_PER_WORD));
1621 /* Similar to `operand_subword', but never return 0. If we can't
1622 extract the required subword, put OP into a register and try again.
1623 The second attempt must succeed. We always validate the address in
1624 this case.
1626 MODE is the mode of OP, in case it is CONST_INT. */
1629 operand_subword_force (rtx op, unsigned int offset, machine_mode mode)
1631 rtx result = operand_subword (op, offset, 1, mode);
1633 if (result)
1634 return result;
1636 if (mode != BLKmode && mode != VOIDmode)
1638 /* If this is a register which can not be accessed by words, copy it
1639 to a pseudo register. */
1640 if (REG_P (op))
1641 op = copy_to_reg (op);
1642 else
1643 op = force_reg (mode, op);
1646 result = operand_subword (op, offset, 1, mode);
1647 gcc_assert (result);
1649 return result;
1652 /* Returns 1 if both MEM_EXPR can be considered equal
1653 and 0 otherwise. */
1656 mem_expr_equal_p (const_tree expr1, const_tree expr2)
1658 if (expr1 == expr2)
1659 return 1;
1661 if (! expr1 || ! expr2)
1662 return 0;
1664 if (TREE_CODE (expr1) != TREE_CODE (expr2))
1665 return 0;
1667 return operand_equal_p (expr1, expr2, 0);
1670 /* Return OFFSET if XEXP (MEM, 0) - OFFSET is known to be ALIGN
1671 bits aligned for 0 <= OFFSET < ALIGN / BITS_PER_UNIT, or
1672 -1 if not known. */
1675 get_mem_align_offset (rtx mem, unsigned int align)
1677 tree expr;
1678 unsigned HOST_WIDE_INT offset;
1680 /* This function can't use
1681 if (!MEM_EXPR (mem) || !MEM_OFFSET_KNOWN_P (mem)
1682 || (MAX (MEM_ALIGN (mem),
1683 MAX (align, get_object_alignment (MEM_EXPR (mem))))
1684 < align))
1685 return -1;
1686 else
1687 return (- MEM_OFFSET (mem)) & (align / BITS_PER_UNIT - 1);
1688 for two reasons:
1689 - COMPONENT_REFs in MEM_EXPR can have NULL first operand,
1690 for <variable>. get_inner_reference doesn't handle it and
1691 even if it did, the alignment in that case needs to be determined
1692 from DECL_FIELD_CONTEXT's TYPE_ALIGN.
1693 - it would do suboptimal job for COMPONENT_REFs, even if MEM_EXPR
1694 isn't sufficiently aligned, the object it is in might be. */
1695 gcc_assert (MEM_P (mem));
1696 expr = MEM_EXPR (mem);
1697 if (expr == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
1698 return -1;
1700 offset = MEM_OFFSET (mem);
1701 if (DECL_P (expr))
1703 if (DECL_ALIGN (expr) < align)
1704 return -1;
1706 else if (INDIRECT_REF_P (expr))
1708 if (TYPE_ALIGN (TREE_TYPE (expr)) < (unsigned int) align)
1709 return -1;
1711 else if (TREE_CODE (expr) == COMPONENT_REF)
1713 while (1)
1715 tree inner = TREE_OPERAND (expr, 0);
1716 tree field = TREE_OPERAND (expr, 1);
1717 tree byte_offset = component_ref_field_offset (expr);
1718 tree bit_offset = DECL_FIELD_BIT_OFFSET (field);
1720 if (!byte_offset
1721 || !tree_fits_uhwi_p (byte_offset)
1722 || !tree_fits_uhwi_p (bit_offset))
1723 return -1;
1725 offset += tree_to_uhwi (byte_offset);
1726 offset += tree_to_uhwi (bit_offset) / BITS_PER_UNIT;
1728 if (inner == NULL_TREE)
1730 if (TYPE_ALIGN (DECL_FIELD_CONTEXT (field))
1731 < (unsigned int) align)
1732 return -1;
1733 break;
1735 else if (DECL_P (inner))
1737 if (DECL_ALIGN (inner) < align)
1738 return -1;
1739 break;
1741 else if (TREE_CODE (inner) != COMPONENT_REF)
1742 return -1;
1743 expr = inner;
1746 else
1747 return -1;
1749 return offset & ((align / BITS_PER_UNIT) - 1);
1752 /* Given REF (a MEM) and T, either the type of X or the expression
1753 corresponding to REF, set the memory attributes. OBJECTP is nonzero
1754 if we are making a new object of this type. BITPOS is nonzero if
1755 there is an offset outstanding on T that will be applied later. */
1757 void
1758 set_mem_attributes_minus_bitpos (rtx ref, tree t, int objectp,
1759 HOST_WIDE_INT bitpos)
1761 HOST_WIDE_INT apply_bitpos = 0;
1762 tree type;
1763 struct mem_attrs attrs, *defattrs, *refattrs;
1764 addr_space_t as;
1766 /* It can happen that type_for_mode was given a mode for which there
1767 is no language-level type. In which case it returns NULL, which
1768 we can see here. */
1769 if (t == NULL_TREE)
1770 return;
1772 type = TYPE_P (t) ? t : TREE_TYPE (t);
1773 if (type == error_mark_node)
1774 return;
1776 /* If we have already set DECL_RTL = ref, get_alias_set will get the
1777 wrong answer, as it assumes that DECL_RTL already has the right alias
1778 info. Callers should not set DECL_RTL until after the call to
1779 set_mem_attributes. */
1780 gcc_assert (!DECL_P (t) || ref != DECL_RTL_IF_SET (t));
1782 memset (&attrs, 0, sizeof (attrs));
1784 /* Get the alias set from the expression or type (perhaps using a
1785 front-end routine) and use it. */
1786 attrs.alias = get_alias_set (t);
1788 MEM_VOLATILE_P (ref) |= TYPE_VOLATILE (type);
1789 MEM_POINTER (ref) = POINTER_TYPE_P (type);
1791 /* Default values from pre-existing memory attributes if present. */
1792 refattrs = MEM_ATTRS (ref);
1793 if (refattrs)
1795 /* ??? Can this ever happen? Calling this routine on a MEM that
1796 already carries memory attributes should probably be invalid. */
1797 attrs.expr = refattrs->expr;
1798 attrs.offset_known_p = refattrs->offset_known_p;
1799 attrs.offset = refattrs->offset;
1800 attrs.size_known_p = refattrs->size_known_p;
1801 attrs.size = refattrs->size;
1802 attrs.align = refattrs->align;
1805 /* Otherwise, default values from the mode of the MEM reference. */
1806 else
1808 defattrs = mode_mem_attrs[(int) GET_MODE (ref)];
1809 gcc_assert (!defattrs->expr);
1810 gcc_assert (!defattrs->offset_known_p);
1812 /* Respect mode size. */
1813 attrs.size_known_p = defattrs->size_known_p;
1814 attrs.size = defattrs->size;
1815 /* ??? Is this really necessary? We probably should always get
1816 the size from the type below. */
1818 /* Respect mode alignment for STRICT_ALIGNMENT targets if T is a type;
1819 if T is an object, always compute the object alignment below. */
1820 if (TYPE_P (t))
1821 attrs.align = defattrs->align;
1822 else
1823 attrs.align = BITS_PER_UNIT;
1824 /* ??? If T is a type, respecting mode alignment may *also* be wrong
1825 e.g. if the type carries an alignment attribute. Should we be
1826 able to simply always use TYPE_ALIGN? */
1829 /* We can set the alignment from the type if we are making an object or if
1830 this is an INDIRECT_REF. */
1831 if (objectp || TREE_CODE (t) == INDIRECT_REF)
1832 attrs.align = MAX (attrs.align, TYPE_ALIGN (type));
1834 /* If the size is known, we can set that. */
1835 tree new_size = TYPE_SIZE_UNIT (type);
1837 /* The address-space is that of the type. */
1838 as = TYPE_ADDR_SPACE (type);
1840 /* If T is not a type, we may be able to deduce some more information about
1841 the expression. */
1842 if (! TYPE_P (t))
1844 tree base;
1846 if (TREE_THIS_VOLATILE (t))
1847 MEM_VOLATILE_P (ref) = 1;
1849 /* Now remove any conversions: they don't change what the underlying
1850 object is. Likewise for SAVE_EXPR. */
1851 while (CONVERT_EXPR_P (t)
1852 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1853 || TREE_CODE (t) == SAVE_EXPR)
1854 t = TREE_OPERAND (t, 0);
1856 /* Note whether this expression can trap. */
1857 MEM_NOTRAP_P (ref) = !tree_could_trap_p (t);
1859 base = get_base_address (t);
1860 if (base)
1862 if (DECL_P (base)
1863 && TREE_READONLY (base)
1864 && (TREE_STATIC (base) || DECL_EXTERNAL (base))
1865 && !TREE_THIS_VOLATILE (base))
1866 MEM_READONLY_P (ref) = 1;
1868 /* Mark static const strings readonly as well. */
1869 if (TREE_CODE (base) == STRING_CST
1870 && TREE_READONLY (base)
1871 && TREE_STATIC (base))
1872 MEM_READONLY_P (ref) = 1;
1874 /* Address-space information is on the base object. */
1875 if (TREE_CODE (base) == MEM_REF
1876 || TREE_CODE (base) == TARGET_MEM_REF)
1877 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (base,
1878 0))));
1879 else
1880 as = TYPE_ADDR_SPACE (TREE_TYPE (base));
1883 /* If this expression uses it's parent's alias set, mark it such
1884 that we won't change it. */
1885 if (component_uses_parent_alias_set_from (t) != NULL_TREE)
1886 MEM_KEEP_ALIAS_SET_P (ref) = 1;
1888 /* If this is a decl, set the attributes of the MEM from it. */
1889 if (DECL_P (t))
1891 attrs.expr = t;
1892 attrs.offset_known_p = true;
1893 attrs.offset = 0;
1894 apply_bitpos = bitpos;
1895 new_size = DECL_SIZE_UNIT (t);
1898 /* ??? If we end up with a constant here do record a MEM_EXPR. */
1899 else if (CONSTANT_CLASS_P (t))
1902 /* If this is a field reference, record it. */
1903 else if (TREE_CODE (t) == COMPONENT_REF)
1905 attrs.expr = t;
1906 attrs.offset_known_p = true;
1907 attrs.offset = 0;
1908 apply_bitpos = bitpos;
1909 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
1910 new_size = DECL_SIZE_UNIT (TREE_OPERAND (t, 1));
1913 /* If this is an array reference, look for an outer field reference. */
1914 else if (TREE_CODE (t) == ARRAY_REF)
1916 tree off_tree = size_zero_node;
1917 /* We can't modify t, because we use it at the end of the
1918 function. */
1919 tree t2 = t;
1923 tree index = TREE_OPERAND (t2, 1);
1924 tree low_bound = array_ref_low_bound (t2);
1925 tree unit_size = array_ref_element_size (t2);
1927 /* We assume all arrays have sizes that are a multiple of a byte.
1928 First subtract the lower bound, if any, in the type of the
1929 index, then convert to sizetype and multiply by the size of
1930 the array element. */
1931 if (! integer_zerop (low_bound))
1932 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
1933 index, low_bound);
1935 off_tree = size_binop (PLUS_EXPR,
1936 size_binop (MULT_EXPR,
1937 fold_convert (sizetype,
1938 index),
1939 unit_size),
1940 off_tree);
1941 t2 = TREE_OPERAND (t2, 0);
1943 while (TREE_CODE (t2) == ARRAY_REF);
1945 if (DECL_P (t2)
1946 || (TREE_CODE (t2) == COMPONENT_REF
1947 /* For trailing arrays t2 doesn't have a size that
1948 covers all valid accesses. */
1949 && ! array_at_struct_end_p (t)))
1951 attrs.expr = t2;
1952 attrs.offset_known_p = false;
1953 if (tree_fits_uhwi_p (off_tree))
1955 attrs.offset_known_p = true;
1956 attrs.offset = tree_to_uhwi (off_tree);
1957 apply_bitpos = bitpos;
1960 /* Else do not record a MEM_EXPR. */
1963 /* If this is an indirect reference, record it. */
1964 else if (TREE_CODE (t) == MEM_REF
1965 || TREE_CODE (t) == TARGET_MEM_REF)
1967 attrs.expr = t;
1968 attrs.offset_known_p = true;
1969 attrs.offset = 0;
1970 apply_bitpos = bitpos;
1973 /* Compute the alignment. */
1974 unsigned int obj_align;
1975 unsigned HOST_WIDE_INT obj_bitpos;
1976 get_object_alignment_1 (t, &obj_align, &obj_bitpos);
1977 obj_bitpos = (obj_bitpos - bitpos) & (obj_align - 1);
1978 if (obj_bitpos != 0)
1979 obj_align = least_bit_hwi (obj_bitpos);
1980 attrs.align = MAX (attrs.align, obj_align);
1983 if (tree_fits_uhwi_p (new_size))
1985 attrs.size_known_p = true;
1986 attrs.size = tree_to_uhwi (new_size);
1989 /* If we modified OFFSET based on T, then subtract the outstanding
1990 bit position offset. Similarly, increase the size of the accessed
1991 object to contain the negative offset. */
1992 if (apply_bitpos)
1994 gcc_assert (attrs.offset_known_p);
1995 attrs.offset -= apply_bitpos / BITS_PER_UNIT;
1996 if (attrs.size_known_p)
1997 attrs.size += apply_bitpos / BITS_PER_UNIT;
2000 /* Now set the attributes we computed above. */
2001 attrs.addrspace = as;
2002 set_mem_attrs (ref, &attrs);
2005 void
2006 set_mem_attributes (rtx ref, tree t, int objectp)
2008 set_mem_attributes_minus_bitpos (ref, t, objectp, 0);
2011 /* Set the alias set of MEM to SET. */
2013 void
2014 set_mem_alias_set (rtx mem, alias_set_type set)
2016 struct mem_attrs attrs;
2018 /* If the new and old alias sets don't conflict, something is wrong. */
2019 gcc_checking_assert (alias_sets_conflict_p (set, MEM_ALIAS_SET (mem)));
2020 attrs = *get_mem_attrs (mem);
2021 attrs.alias = set;
2022 set_mem_attrs (mem, &attrs);
2025 /* Set the address space of MEM to ADDRSPACE (target-defined). */
2027 void
2028 set_mem_addr_space (rtx mem, addr_space_t addrspace)
2030 struct mem_attrs attrs;
2032 attrs = *get_mem_attrs (mem);
2033 attrs.addrspace = addrspace;
2034 set_mem_attrs (mem, &attrs);
2037 /* Set the alignment of MEM to ALIGN bits. */
2039 void
2040 set_mem_align (rtx mem, unsigned int align)
2042 struct mem_attrs attrs;
2044 attrs = *get_mem_attrs (mem);
2045 attrs.align = align;
2046 set_mem_attrs (mem, &attrs);
2049 /* Set the expr for MEM to EXPR. */
2051 void
2052 set_mem_expr (rtx mem, tree expr)
2054 struct mem_attrs attrs;
2056 attrs = *get_mem_attrs (mem);
2057 attrs.expr = expr;
2058 set_mem_attrs (mem, &attrs);
2061 /* Set the offset of MEM to OFFSET. */
2063 void
2064 set_mem_offset (rtx mem, HOST_WIDE_INT offset)
2066 struct mem_attrs attrs;
2068 attrs = *get_mem_attrs (mem);
2069 attrs.offset_known_p = true;
2070 attrs.offset = offset;
2071 set_mem_attrs (mem, &attrs);
2074 /* Clear the offset of MEM. */
2076 void
2077 clear_mem_offset (rtx mem)
2079 struct mem_attrs attrs;
2081 attrs = *get_mem_attrs (mem);
2082 attrs.offset_known_p = false;
2083 set_mem_attrs (mem, &attrs);
2086 /* Set the size of MEM to SIZE. */
2088 void
2089 set_mem_size (rtx mem, HOST_WIDE_INT size)
2091 struct mem_attrs attrs;
2093 attrs = *get_mem_attrs (mem);
2094 attrs.size_known_p = true;
2095 attrs.size = size;
2096 set_mem_attrs (mem, &attrs);
2099 /* Clear the size of MEM. */
2101 void
2102 clear_mem_size (rtx mem)
2104 struct mem_attrs attrs;
2106 attrs = *get_mem_attrs (mem);
2107 attrs.size_known_p = false;
2108 set_mem_attrs (mem, &attrs);
2111 /* Return a memory reference like MEMREF, but with its mode changed to MODE
2112 and its address changed to ADDR. (VOIDmode means don't change the mode.
2113 NULL for ADDR means don't change the address.) VALIDATE is nonzero if the
2114 returned memory location is required to be valid. INPLACE is true if any
2115 changes can be made directly to MEMREF or false if MEMREF must be treated
2116 as immutable.
2118 The memory attributes are not changed. */
2120 static rtx
2121 change_address_1 (rtx memref, machine_mode mode, rtx addr, int validate,
2122 bool inplace)
2124 addr_space_t as;
2125 rtx new_rtx;
2127 gcc_assert (MEM_P (memref));
2128 as = MEM_ADDR_SPACE (memref);
2129 if (mode == VOIDmode)
2130 mode = GET_MODE (memref);
2131 if (addr == 0)
2132 addr = XEXP (memref, 0);
2133 if (mode == GET_MODE (memref) && addr == XEXP (memref, 0)
2134 && (!validate || memory_address_addr_space_p (mode, addr, as)))
2135 return memref;
2137 /* Don't validate address for LRA. LRA can make the address valid
2138 by itself in most efficient way. */
2139 if (validate && !lra_in_progress)
2141 if (reload_in_progress || reload_completed)
2142 gcc_assert (memory_address_addr_space_p (mode, addr, as));
2143 else
2144 addr = memory_address_addr_space (mode, addr, as);
2147 if (rtx_equal_p (addr, XEXP (memref, 0)) && mode == GET_MODE (memref))
2148 return memref;
2150 if (inplace)
2152 XEXP (memref, 0) = addr;
2153 return memref;
2156 new_rtx = gen_rtx_MEM (mode, addr);
2157 MEM_COPY_ATTRIBUTES (new_rtx, memref);
2158 return new_rtx;
2161 /* Like change_address_1 with VALIDATE nonzero, but we are not saying in what
2162 way we are changing MEMREF, so we only preserve the alias set. */
2165 change_address (rtx memref, machine_mode mode, rtx addr)
2167 rtx new_rtx = change_address_1 (memref, mode, addr, 1, false);
2168 machine_mode mmode = GET_MODE (new_rtx);
2169 struct mem_attrs attrs, *defattrs;
2171 attrs = *get_mem_attrs (memref);
2172 defattrs = mode_mem_attrs[(int) mmode];
2173 attrs.expr = NULL_TREE;
2174 attrs.offset_known_p = false;
2175 attrs.size_known_p = defattrs->size_known_p;
2176 attrs.size = defattrs->size;
2177 attrs.align = defattrs->align;
2179 /* If there are no changes, just return the original memory reference. */
2180 if (new_rtx == memref)
2182 if (mem_attrs_eq_p (get_mem_attrs (memref), &attrs))
2183 return new_rtx;
2185 new_rtx = gen_rtx_MEM (mmode, XEXP (memref, 0));
2186 MEM_COPY_ATTRIBUTES (new_rtx, memref);
2189 set_mem_attrs (new_rtx, &attrs);
2190 return new_rtx;
2193 /* Return a memory reference like MEMREF, but with its mode changed
2194 to MODE and its address offset by OFFSET bytes. If VALIDATE is
2195 nonzero, the memory address is forced to be valid.
2196 If ADJUST_ADDRESS is zero, OFFSET is only used to update MEM_ATTRS
2197 and the caller is responsible for adjusting MEMREF base register.
2198 If ADJUST_OBJECT is zero, the underlying object associated with the
2199 memory reference is left unchanged and the caller is responsible for
2200 dealing with it. Otherwise, if the new memory reference is outside
2201 the underlying object, even partially, then the object is dropped.
2202 SIZE, if nonzero, is the size of an access in cases where MODE
2203 has no inherent size. */
2206 adjust_address_1 (rtx memref, machine_mode mode, HOST_WIDE_INT offset,
2207 int validate, int adjust_address, int adjust_object,
2208 HOST_WIDE_INT size)
2210 rtx addr = XEXP (memref, 0);
2211 rtx new_rtx;
2212 machine_mode address_mode;
2213 int pbits;
2214 struct mem_attrs attrs = *get_mem_attrs (memref), *defattrs;
2215 unsigned HOST_WIDE_INT max_align;
2216 #ifdef POINTERS_EXTEND_UNSIGNED
2217 machine_mode pointer_mode
2218 = targetm.addr_space.pointer_mode (attrs.addrspace);
2219 #endif
2221 /* VOIDmode means no mode change for change_address_1. */
2222 if (mode == VOIDmode)
2223 mode = GET_MODE (memref);
2225 /* Take the size of non-BLKmode accesses from the mode. */
2226 defattrs = mode_mem_attrs[(int) mode];
2227 if (defattrs->size_known_p)
2228 size = defattrs->size;
2230 /* If there are no changes, just return the original memory reference. */
2231 if (mode == GET_MODE (memref) && !offset
2232 && (size == 0 || (attrs.size_known_p && attrs.size == size))
2233 && (!validate || memory_address_addr_space_p (mode, addr,
2234 attrs.addrspace)))
2235 return memref;
2237 /* ??? Prefer to create garbage instead of creating shared rtl.
2238 This may happen even if offset is nonzero -- consider
2239 (plus (plus reg reg) const_int) -- so do this always. */
2240 addr = copy_rtx (addr);
2242 /* Convert a possibly large offset to a signed value within the
2243 range of the target address space. */
2244 address_mode = get_address_mode (memref);
2245 pbits = GET_MODE_BITSIZE (address_mode);
2246 if (HOST_BITS_PER_WIDE_INT > pbits)
2248 int shift = HOST_BITS_PER_WIDE_INT - pbits;
2249 offset = (((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) offset << shift))
2250 >> shift);
2253 if (adjust_address)
2255 /* If MEMREF is a LO_SUM and the offset is within the alignment of the
2256 object, we can merge it into the LO_SUM. */
2257 if (GET_MODE (memref) != BLKmode && GET_CODE (addr) == LO_SUM
2258 && offset >= 0
2259 && (unsigned HOST_WIDE_INT) offset
2260 < GET_MODE_ALIGNMENT (GET_MODE (memref)) / BITS_PER_UNIT)
2261 addr = gen_rtx_LO_SUM (address_mode, XEXP (addr, 0),
2262 plus_constant (address_mode,
2263 XEXP (addr, 1), offset));
2264 #ifdef POINTERS_EXTEND_UNSIGNED
2265 /* If MEMREF is a ZERO_EXTEND from pointer_mode and the offset is valid
2266 in that mode, we merge it into the ZERO_EXTEND. We take advantage of
2267 the fact that pointers are not allowed to overflow. */
2268 else if (POINTERS_EXTEND_UNSIGNED > 0
2269 && GET_CODE (addr) == ZERO_EXTEND
2270 && GET_MODE (XEXP (addr, 0)) == pointer_mode
2271 && trunc_int_for_mode (offset, pointer_mode) == offset)
2272 addr = gen_rtx_ZERO_EXTEND (address_mode,
2273 plus_constant (pointer_mode,
2274 XEXP (addr, 0), offset));
2275 #endif
2276 else
2277 addr = plus_constant (address_mode, addr, offset);
2280 new_rtx = change_address_1 (memref, mode, addr, validate, false);
2282 /* If the address is a REG, change_address_1 rightfully returns memref,
2283 but this would destroy memref's MEM_ATTRS. */
2284 if (new_rtx == memref && offset != 0)
2285 new_rtx = copy_rtx (new_rtx);
2287 /* Conservatively drop the object if we don't know where we start from. */
2288 if (adjust_object && (!attrs.offset_known_p || !attrs.size_known_p))
2290 attrs.expr = NULL_TREE;
2291 attrs.alias = 0;
2294 /* Compute the new values of the memory attributes due to this adjustment.
2295 We add the offsets and update the alignment. */
2296 if (attrs.offset_known_p)
2298 attrs.offset += offset;
2300 /* Drop the object if the new left end is not within its bounds. */
2301 if (adjust_object && attrs.offset < 0)
2303 attrs.expr = NULL_TREE;
2304 attrs.alias = 0;
2308 /* Compute the new alignment by taking the MIN of the alignment and the
2309 lowest-order set bit in OFFSET, but don't change the alignment if OFFSET
2310 if zero. */
2311 if (offset != 0)
2313 max_align = least_bit_hwi (offset) * BITS_PER_UNIT;
2314 attrs.align = MIN (attrs.align, max_align);
2317 if (size)
2319 /* Drop the object if the new right end is not within its bounds. */
2320 if (adjust_object && (offset + size) > attrs.size)
2322 attrs.expr = NULL_TREE;
2323 attrs.alias = 0;
2325 attrs.size_known_p = true;
2326 attrs.size = size;
2328 else if (attrs.size_known_p)
2330 gcc_assert (!adjust_object);
2331 attrs.size -= offset;
2332 /* ??? The store_by_pieces machinery generates negative sizes,
2333 so don't assert for that here. */
2336 set_mem_attrs (new_rtx, &attrs);
2338 return new_rtx;
2341 /* Return a memory reference like MEMREF, but with its mode changed
2342 to MODE and its address changed to ADDR, which is assumed to be
2343 MEMREF offset by OFFSET bytes. If VALIDATE is
2344 nonzero, the memory address is forced to be valid. */
2347 adjust_automodify_address_1 (rtx memref, machine_mode mode, rtx addr,
2348 HOST_WIDE_INT offset, int validate)
2350 memref = change_address_1 (memref, VOIDmode, addr, validate, false);
2351 return adjust_address_1 (memref, mode, offset, validate, 0, 0, 0);
2354 /* Return a memory reference like MEMREF, but whose address is changed by
2355 adding OFFSET, an RTX, to it. POW2 is the highest power of two factor
2356 known to be in OFFSET (possibly 1). */
2359 offset_address (rtx memref, rtx offset, unsigned HOST_WIDE_INT pow2)
2361 rtx new_rtx, addr = XEXP (memref, 0);
2362 machine_mode address_mode;
2363 struct mem_attrs attrs, *defattrs;
2365 attrs = *get_mem_attrs (memref);
2366 address_mode = get_address_mode (memref);
2367 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2369 /* At this point we don't know _why_ the address is invalid. It
2370 could have secondary memory references, multiplies or anything.
2372 However, if we did go and rearrange things, we can wind up not
2373 being able to recognize the magic around pic_offset_table_rtx.
2374 This stuff is fragile, and is yet another example of why it is
2375 bad to expose PIC machinery too early. */
2376 if (! memory_address_addr_space_p (GET_MODE (memref), new_rtx,
2377 attrs.addrspace)
2378 && GET_CODE (addr) == PLUS
2379 && XEXP (addr, 0) == pic_offset_table_rtx)
2381 addr = force_reg (GET_MODE (addr), addr);
2382 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2385 update_temp_slot_address (XEXP (memref, 0), new_rtx);
2386 new_rtx = change_address_1 (memref, VOIDmode, new_rtx, 1, false);
2388 /* If there are no changes, just return the original memory reference. */
2389 if (new_rtx == memref)
2390 return new_rtx;
2392 /* Update the alignment to reflect the offset. Reset the offset, which
2393 we don't know. */
2394 defattrs = mode_mem_attrs[(int) GET_MODE (new_rtx)];
2395 attrs.offset_known_p = false;
2396 attrs.size_known_p = defattrs->size_known_p;
2397 attrs.size = defattrs->size;
2398 attrs.align = MIN (attrs.align, pow2 * BITS_PER_UNIT);
2399 set_mem_attrs (new_rtx, &attrs);
2400 return new_rtx;
2403 /* Return a memory reference like MEMREF, but with its address changed to
2404 ADDR. The caller is asserting that the actual piece of memory pointed
2405 to is the same, just the form of the address is being changed, such as
2406 by putting something into a register. INPLACE is true if any changes
2407 can be made directly to MEMREF or false if MEMREF must be treated as
2408 immutable. */
2411 replace_equiv_address (rtx memref, rtx addr, bool inplace)
2413 /* change_address_1 copies the memory attribute structure without change
2414 and that's exactly what we want here. */
2415 update_temp_slot_address (XEXP (memref, 0), addr);
2416 return change_address_1 (memref, VOIDmode, addr, 1, inplace);
2419 /* Likewise, but the reference is not required to be valid. */
2422 replace_equiv_address_nv (rtx memref, rtx addr, bool inplace)
2424 return change_address_1 (memref, VOIDmode, addr, 0, inplace);
2427 /* Return a memory reference like MEMREF, but with its mode widened to
2428 MODE and offset by OFFSET. This would be used by targets that e.g.
2429 cannot issue QImode memory operations and have to use SImode memory
2430 operations plus masking logic. */
2433 widen_memory_access (rtx memref, machine_mode mode, HOST_WIDE_INT offset)
2435 rtx new_rtx = adjust_address_1 (memref, mode, offset, 1, 1, 0, 0);
2436 struct mem_attrs attrs;
2437 unsigned int size = GET_MODE_SIZE (mode);
2439 /* If there are no changes, just return the original memory reference. */
2440 if (new_rtx == memref)
2441 return new_rtx;
2443 attrs = *get_mem_attrs (new_rtx);
2445 /* If we don't know what offset we were at within the expression, then
2446 we can't know if we've overstepped the bounds. */
2447 if (! attrs.offset_known_p)
2448 attrs.expr = NULL_TREE;
2450 while (attrs.expr)
2452 if (TREE_CODE (attrs.expr) == COMPONENT_REF)
2454 tree field = TREE_OPERAND (attrs.expr, 1);
2455 tree offset = component_ref_field_offset (attrs.expr);
2457 if (! DECL_SIZE_UNIT (field))
2459 attrs.expr = NULL_TREE;
2460 break;
2463 /* Is the field at least as large as the access? If so, ok,
2464 otherwise strip back to the containing structure. */
2465 if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
2466 && compare_tree_int (DECL_SIZE_UNIT (field), size) >= 0
2467 && attrs.offset >= 0)
2468 break;
2470 if (! tree_fits_uhwi_p (offset))
2472 attrs.expr = NULL_TREE;
2473 break;
2476 attrs.expr = TREE_OPERAND (attrs.expr, 0);
2477 attrs.offset += tree_to_uhwi (offset);
2478 attrs.offset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
2479 / BITS_PER_UNIT);
2481 /* Similarly for the decl. */
2482 else if (DECL_P (attrs.expr)
2483 && DECL_SIZE_UNIT (attrs.expr)
2484 && TREE_CODE (DECL_SIZE_UNIT (attrs.expr)) == INTEGER_CST
2485 && compare_tree_int (DECL_SIZE_UNIT (attrs.expr), size) >= 0
2486 && (! attrs.offset_known_p || attrs.offset >= 0))
2487 break;
2488 else
2490 /* The widened memory access overflows the expression, which means
2491 that it could alias another expression. Zap it. */
2492 attrs.expr = NULL_TREE;
2493 break;
2497 if (! attrs.expr)
2498 attrs.offset_known_p = false;
2500 /* The widened memory may alias other stuff, so zap the alias set. */
2501 /* ??? Maybe use get_alias_set on any remaining expression. */
2502 attrs.alias = 0;
2503 attrs.size_known_p = true;
2504 attrs.size = size;
2505 set_mem_attrs (new_rtx, &attrs);
2506 return new_rtx;
2509 /* A fake decl that is used as the MEM_EXPR of spill slots. */
2510 static GTY(()) tree spill_slot_decl;
2512 tree
2513 get_spill_slot_decl (bool force_build_p)
2515 tree d = spill_slot_decl;
2516 rtx rd;
2517 struct mem_attrs attrs;
2519 if (d || !force_build_p)
2520 return d;
2522 d = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2523 VAR_DECL, get_identifier ("%sfp"), void_type_node);
2524 DECL_ARTIFICIAL (d) = 1;
2525 DECL_IGNORED_P (d) = 1;
2526 TREE_USED (d) = 1;
2527 spill_slot_decl = d;
2529 rd = gen_rtx_MEM (BLKmode, frame_pointer_rtx);
2530 MEM_NOTRAP_P (rd) = 1;
2531 attrs = *mode_mem_attrs[(int) BLKmode];
2532 attrs.alias = new_alias_set ();
2533 attrs.expr = d;
2534 set_mem_attrs (rd, &attrs);
2535 SET_DECL_RTL (d, rd);
2537 return d;
2540 /* Given MEM, a result from assign_stack_local, fill in the memory
2541 attributes as appropriate for a register allocator spill slot.
2542 These slots are not aliasable by other memory. We arrange for
2543 them all to use a single MEM_EXPR, so that the aliasing code can
2544 work properly in the case of shared spill slots. */
2546 void
2547 set_mem_attrs_for_spill (rtx mem)
2549 struct mem_attrs attrs;
2550 rtx addr;
2552 attrs = *get_mem_attrs (mem);
2553 attrs.expr = get_spill_slot_decl (true);
2554 attrs.alias = MEM_ALIAS_SET (DECL_RTL (attrs.expr));
2555 attrs.addrspace = ADDR_SPACE_GENERIC;
2557 /* We expect the incoming memory to be of the form:
2558 (mem:MODE (plus (reg sfp) (const_int offset)))
2559 with perhaps the plus missing for offset = 0. */
2560 addr = XEXP (mem, 0);
2561 attrs.offset_known_p = true;
2562 attrs.offset = 0;
2563 if (GET_CODE (addr) == PLUS
2564 && CONST_INT_P (XEXP (addr, 1)))
2565 attrs.offset = INTVAL (XEXP (addr, 1));
2567 set_mem_attrs (mem, &attrs);
2568 MEM_NOTRAP_P (mem) = 1;
2571 /* Return a newly created CODE_LABEL rtx with a unique label number. */
2573 rtx_code_label *
2574 gen_label_rtx (void)
2576 return as_a <rtx_code_label *> (
2577 gen_rtx_CODE_LABEL (VOIDmode, NULL_RTX, NULL_RTX,
2578 NULL, label_num++, NULL));
2581 /* For procedure integration. */
2583 /* Install new pointers to the first and last insns in the chain.
2584 Also, set cur_insn_uid to one higher than the last in use.
2585 Used for an inline-procedure after copying the insn chain. */
2587 void
2588 set_new_first_and_last_insn (rtx_insn *first, rtx_insn *last)
2590 rtx_insn *insn;
2592 set_first_insn (first);
2593 set_last_insn (last);
2594 cur_insn_uid = 0;
2596 if (MIN_NONDEBUG_INSN_UID || MAY_HAVE_DEBUG_INSNS)
2598 int debug_count = 0;
2600 cur_insn_uid = MIN_NONDEBUG_INSN_UID - 1;
2601 cur_debug_insn_uid = 0;
2603 for (insn = first; insn; insn = NEXT_INSN (insn))
2604 if (INSN_UID (insn) < MIN_NONDEBUG_INSN_UID)
2605 cur_debug_insn_uid = MAX (cur_debug_insn_uid, INSN_UID (insn));
2606 else
2608 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2609 if (DEBUG_INSN_P (insn))
2610 debug_count++;
2613 if (debug_count)
2614 cur_debug_insn_uid = MIN_NONDEBUG_INSN_UID + debug_count;
2615 else
2616 cur_debug_insn_uid++;
2618 else
2619 for (insn = first; insn; insn = NEXT_INSN (insn))
2620 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2622 cur_insn_uid++;
2625 /* Go through all the RTL insn bodies and copy any invalid shared
2626 structure. This routine should only be called once. */
2628 static void
2629 unshare_all_rtl_1 (rtx_insn *insn)
2631 /* Unshare just about everything else. */
2632 unshare_all_rtl_in_chain (insn);
2634 /* Make sure the addresses of stack slots found outside the insn chain
2635 (such as, in DECL_RTL of a variable) are not shared
2636 with the insn chain.
2638 This special care is necessary when the stack slot MEM does not
2639 actually appear in the insn chain. If it does appear, its address
2640 is unshared from all else at that point. */
2641 unsigned int i;
2642 rtx temp;
2643 FOR_EACH_VEC_SAFE_ELT (stack_slot_list, i, temp)
2644 (*stack_slot_list)[i] = copy_rtx_if_shared (temp);
2647 /* Go through all the RTL insn bodies and copy any invalid shared
2648 structure, again. This is a fairly expensive thing to do so it
2649 should be done sparingly. */
2651 void
2652 unshare_all_rtl_again (rtx_insn *insn)
2654 rtx_insn *p;
2655 tree decl;
2657 for (p = insn; p; p = NEXT_INSN (p))
2658 if (INSN_P (p))
2660 reset_used_flags (PATTERN (p));
2661 reset_used_flags (REG_NOTES (p));
2662 if (CALL_P (p))
2663 reset_used_flags (CALL_INSN_FUNCTION_USAGE (p));
2666 /* Make sure that virtual stack slots are not shared. */
2667 set_used_decls (DECL_INITIAL (cfun->decl));
2669 /* Make sure that virtual parameters are not shared. */
2670 for (decl = DECL_ARGUMENTS (cfun->decl); decl; decl = DECL_CHAIN (decl))
2671 set_used_flags (DECL_RTL (decl));
2673 rtx temp;
2674 unsigned int i;
2675 FOR_EACH_VEC_SAFE_ELT (stack_slot_list, i, temp)
2676 reset_used_flags (temp);
2678 unshare_all_rtl_1 (insn);
2681 unsigned int
2682 unshare_all_rtl (void)
2684 unshare_all_rtl_1 (get_insns ());
2686 for (tree decl = DECL_ARGUMENTS (cfun->decl); decl; decl = DECL_CHAIN (decl))
2688 if (DECL_RTL_SET_P (decl))
2689 SET_DECL_RTL (decl, copy_rtx_if_shared (DECL_RTL (decl)));
2690 DECL_INCOMING_RTL (decl) = copy_rtx_if_shared (DECL_INCOMING_RTL (decl));
2693 return 0;
2697 /* Check that ORIG is not marked when it should not be and mark ORIG as in use,
2698 Recursively does the same for subexpressions. */
2700 static void
2701 verify_rtx_sharing (rtx orig, rtx insn)
2703 rtx x = orig;
2704 int i;
2705 enum rtx_code code;
2706 const char *format_ptr;
2708 if (x == 0)
2709 return;
2711 code = GET_CODE (x);
2713 /* These types may be freely shared. */
2715 switch (code)
2717 case REG:
2718 case DEBUG_EXPR:
2719 case VALUE:
2720 CASE_CONST_ANY:
2721 case SYMBOL_REF:
2722 case LABEL_REF:
2723 case CODE_LABEL:
2724 case PC:
2725 case CC0:
2726 case RETURN:
2727 case SIMPLE_RETURN:
2728 case SCRATCH:
2729 /* SCRATCH must be shared because they represent distinct values. */
2730 return;
2731 case CLOBBER:
2732 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2733 clobbers or clobbers of hard registers that originated as pseudos.
2734 This is needed to allow safe register renaming. */
2735 if (REG_P (XEXP (x, 0))
2736 && HARD_REGISTER_NUM_P (REGNO (XEXP (x, 0)))
2737 && HARD_REGISTER_NUM_P (ORIGINAL_REGNO (XEXP (x, 0))))
2738 return;
2739 break;
2741 case CONST:
2742 if (shared_const_p (orig))
2743 return;
2744 break;
2746 case MEM:
2747 /* A MEM is allowed to be shared if its address is constant. */
2748 if (CONSTANT_ADDRESS_P (XEXP (x, 0))
2749 || reload_completed || reload_in_progress)
2750 return;
2752 break;
2754 default:
2755 break;
2758 /* This rtx may not be shared. If it has already been seen,
2759 replace it with a copy of itself. */
2760 if (flag_checking && RTX_FLAG (x, used))
2762 error ("invalid rtl sharing found in the insn");
2763 debug_rtx (insn);
2764 error ("shared rtx");
2765 debug_rtx (x);
2766 internal_error ("internal consistency failure");
2768 gcc_assert (!RTX_FLAG (x, used));
2770 RTX_FLAG (x, used) = 1;
2772 /* Now scan the subexpressions recursively. */
2774 format_ptr = GET_RTX_FORMAT (code);
2776 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2778 switch (*format_ptr++)
2780 case 'e':
2781 verify_rtx_sharing (XEXP (x, i), insn);
2782 break;
2784 case 'E':
2785 if (XVEC (x, i) != NULL)
2787 int j;
2788 int len = XVECLEN (x, i);
2790 for (j = 0; j < len; j++)
2792 /* We allow sharing of ASM_OPERANDS inside single
2793 instruction. */
2794 if (j && GET_CODE (XVECEXP (x, i, j)) == SET
2795 && (GET_CODE (SET_SRC (XVECEXP (x, i, j)))
2796 == ASM_OPERANDS))
2797 verify_rtx_sharing (SET_DEST (XVECEXP (x, i, j)), insn);
2798 else
2799 verify_rtx_sharing (XVECEXP (x, i, j), insn);
2802 break;
2805 return;
2808 /* Reset used-flags for INSN. */
2810 static void
2811 reset_insn_used_flags (rtx insn)
2813 gcc_assert (INSN_P (insn));
2814 reset_used_flags (PATTERN (insn));
2815 reset_used_flags (REG_NOTES (insn));
2816 if (CALL_P (insn))
2817 reset_used_flags (CALL_INSN_FUNCTION_USAGE (insn));
2820 /* Go through all the RTL insn bodies and clear all the USED bits. */
2822 static void
2823 reset_all_used_flags (void)
2825 rtx_insn *p;
2827 for (p = get_insns (); p; p = NEXT_INSN (p))
2828 if (INSN_P (p))
2830 rtx pat = PATTERN (p);
2831 if (GET_CODE (pat) != SEQUENCE)
2832 reset_insn_used_flags (p);
2833 else
2835 gcc_assert (REG_NOTES (p) == NULL);
2836 for (int i = 0; i < XVECLEN (pat, 0); i++)
2838 rtx insn = XVECEXP (pat, 0, i);
2839 if (INSN_P (insn))
2840 reset_insn_used_flags (insn);
2846 /* Verify sharing in INSN. */
2848 static void
2849 verify_insn_sharing (rtx insn)
2851 gcc_assert (INSN_P (insn));
2852 verify_rtx_sharing (PATTERN (insn), insn);
2853 verify_rtx_sharing (REG_NOTES (insn), insn);
2854 if (CALL_P (insn))
2855 verify_rtx_sharing (CALL_INSN_FUNCTION_USAGE (insn), insn);
2858 /* Go through all the RTL insn bodies and check that there is no unexpected
2859 sharing in between the subexpressions. */
2861 DEBUG_FUNCTION void
2862 verify_rtl_sharing (void)
2864 rtx_insn *p;
2866 timevar_push (TV_VERIFY_RTL_SHARING);
2868 reset_all_used_flags ();
2870 for (p = get_insns (); p; p = NEXT_INSN (p))
2871 if (INSN_P (p))
2873 rtx pat = PATTERN (p);
2874 if (GET_CODE (pat) != SEQUENCE)
2875 verify_insn_sharing (p);
2876 else
2877 for (int i = 0; i < XVECLEN (pat, 0); i++)
2879 rtx insn = XVECEXP (pat, 0, i);
2880 if (INSN_P (insn))
2881 verify_insn_sharing (insn);
2885 reset_all_used_flags ();
2887 timevar_pop (TV_VERIFY_RTL_SHARING);
2890 /* Go through all the RTL insn bodies and copy any invalid shared structure.
2891 Assumes the mark bits are cleared at entry. */
2893 void
2894 unshare_all_rtl_in_chain (rtx_insn *insn)
2896 for (; insn; insn = NEXT_INSN (insn))
2897 if (INSN_P (insn))
2899 PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
2900 REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
2901 if (CALL_P (insn))
2902 CALL_INSN_FUNCTION_USAGE (insn)
2903 = copy_rtx_if_shared (CALL_INSN_FUNCTION_USAGE (insn));
2907 /* Go through all virtual stack slots of a function and mark them as
2908 shared. We never replace the DECL_RTLs themselves with a copy,
2909 but expressions mentioned into a DECL_RTL cannot be shared with
2910 expressions in the instruction stream.
2912 Note that reload may convert pseudo registers into memories in-place.
2913 Pseudo registers are always shared, but MEMs never are. Thus if we
2914 reset the used flags on MEMs in the instruction stream, we must set
2915 them again on MEMs that appear in DECL_RTLs. */
2917 static void
2918 set_used_decls (tree blk)
2920 tree t;
2922 /* Mark decls. */
2923 for (t = BLOCK_VARS (blk); t; t = DECL_CHAIN (t))
2924 if (DECL_RTL_SET_P (t))
2925 set_used_flags (DECL_RTL (t));
2927 /* Now process sub-blocks. */
2928 for (t = BLOCK_SUBBLOCKS (blk); t; t = BLOCK_CHAIN (t))
2929 set_used_decls (t);
2932 /* Mark ORIG as in use, and return a copy of it if it was already in use.
2933 Recursively does the same for subexpressions. Uses
2934 copy_rtx_if_shared_1 to reduce stack space. */
2937 copy_rtx_if_shared (rtx orig)
2939 copy_rtx_if_shared_1 (&orig);
2940 return orig;
2943 /* Mark *ORIG1 as in use, and set it to a copy of it if it was already in
2944 use. Recursively does the same for subexpressions. */
2946 static void
2947 copy_rtx_if_shared_1 (rtx *orig1)
2949 rtx x;
2950 int i;
2951 enum rtx_code code;
2952 rtx *last_ptr;
2953 const char *format_ptr;
2954 int copied = 0;
2955 int length;
2957 /* Repeat is used to turn tail-recursion into iteration. */
2958 repeat:
2959 x = *orig1;
2961 if (x == 0)
2962 return;
2964 code = GET_CODE (x);
2966 /* These types may be freely shared. */
2968 switch (code)
2970 case REG:
2971 case DEBUG_EXPR:
2972 case VALUE:
2973 CASE_CONST_ANY:
2974 case SYMBOL_REF:
2975 case LABEL_REF:
2976 case CODE_LABEL:
2977 case PC:
2978 case CC0:
2979 case RETURN:
2980 case SIMPLE_RETURN:
2981 case SCRATCH:
2982 /* SCRATCH must be shared because they represent distinct values. */
2983 return;
2984 case CLOBBER:
2985 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2986 clobbers or clobbers of hard registers that originated as pseudos.
2987 This is needed to allow safe register renaming. */
2988 if (REG_P (XEXP (x, 0))
2989 && HARD_REGISTER_NUM_P (REGNO (XEXP (x, 0)))
2990 && HARD_REGISTER_NUM_P (ORIGINAL_REGNO (XEXP (x, 0))))
2991 return;
2992 break;
2994 case CONST:
2995 if (shared_const_p (x))
2996 return;
2997 break;
2999 case DEBUG_INSN:
3000 case INSN:
3001 case JUMP_INSN:
3002 case CALL_INSN:
3003 case NOTE:
3004 case BARRIER:
3005 /* The chain of insns is not being copied. */
3006 return;
3008 default:
3009 break;
3012 /* This rtx may not be shared. If it has already been seen,
3013 replace it with a copy of itself. */
3015 if (RTX_FLAG (x, used))
3017 x = shallow_copy_rtx (x);
3018 copied = 1;
3020 RTX_FLAG (x, used) = 1;
3022 /* Now scan the subexpressions recursively.
3023 We can store any replaced subexpressions directly into X
3024 since we know X is not shared! Any vectors in X
3025 must be copied if X was copied. */
3027 format_ptr = GET_RTX_FORMAT (code);
3028 length = GET_RTX_LENGTH (code);
3029 last_ptr = NULL;
3031 for (i = 0; i < length; i++)
3033 switch (*format_ptr++)
3035 case 'e':
3036 if (last_ptr)
3037 copy_rtx_if_shared_1 (last_ptr);
3038 last_ptr = &XEXP (x, i);
3039 break;
3041 case 'E':
3042 if (XVEC (x, i) != NULL)
3044 int j;
3045 int len = XVECLEN (x, i);
3047 /* Copy the vector iff I copied the rtx and the length
3048 is nonzero. */
3049 if (copied && len > 0)
3050 XVEC (x, i) = gen_rtvec_v (len, XVEC (x, i)->elem);
3052 /* Call recursively on all inside the vector. */
3053 for (j = 0; j < len; j++)
3055 if (last_ptr)
3056 copy_rtx_if_shared_1 (last_ptr);
3057 last_ptr = &XVECEXP (x, i, j);
3060 break;
3063 *orig1 = x;
3064 if (last_ptr)
3066 orig1 = last_ptr;
3067 goto repeat;
3069 return;
3072 /* Set the USED bit in X and its non-shareable subparts to FLAG. */
3074 static void
3075 mark_used_flags (rtx x, int flag)
3077 int i, j;
3078 enum rtx_code code;
3079 const char *format_ptr;
3080 int length;
3082 /* Repeat is used to turn tail-recursion into iteration. */
3083 repeat:
3084 if (x == 0)
3085 return;
3087 code = GET_CODE (x);
3089 /* These types may be freely shared so we needn't do any resetting
3090 for them. */
3092 switch (code)
3094 case REG:
3095 case DEBUG_EXPR:
3096 case VALUE:
3097 CASE_CONST_ANY:
3098 case SYMBOL_REF:
3099 case CODE_LABEL:
3100 case PC:
3101 case CC0:
3102 case RETURN:
3103 case SIMPLE_RETURN:
3104 return;
3106 case DEBUG_INSN:
3107 case INSN:
3108 case JUMP_INSN:
3109 case CALL_INSN:
3110 case NOTE:
3111 case LABEL_REF:
3112 case BARRIER:
3113 /* The chain of insns is not being copied. */
3114 return;
3116 default:
3117 break;
3120 RTX_FLAG (x, used) = flag;
3122 format_ptr = GET_RTX_FORMAT (code);
3123 length = GET_RTX_LENGTH (code);
3125 for (i = 0; i < length; i++)
3127 switch (*format_ptr++)
3129 case 'e':
3130 if (i == length-1)
3132 x = XEXP (x, i);
3133 goto repeat;
3135 mark_used_flags (XEXP (x, i), flag);
3136 break;
3138 case 'E':
3139 for (j = 0; j < XVECLEN (x, i); j++)
3140 mark_used_flags (XVECEXP (x, i, j), flag);
3141 break;
3146 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
3147 to look for shared sub-parts. */
3149 void
3150 reset_used_flags (rtx x)
3152 mark_used_flags (x, 0);
3155 /* Set all the USED bits in X to allow copy_rtx_if_shared to be used
3156 to look for shared sub-parts. */
3158 void
3159 set_used_flags (rtx x)
3161 mark_used_flags (x, 1);
3164 /* Copy X if necessary so that it won't be altered by changes in OTHER.
3165 Return X or the rtx for the pseudo reg the value of X was copied into.
3166 OTHER must be valid as a SET_DEST. */
3169 make_safe_from (rtx x, rtx other)
3171 while (1)
3172 switch (GET_CODE (other))
3174 case SUBREG:
3175 other = SUBREG_REG (other);
3176 break;
3177 case STRICT_LOW_PART:
3178 case SIGN_EXTEND:
3179 case ZERO_EXTEND:
3180 other = XEXP (other, 0);
3181 break;
3182 default:
3183 goto done;
3185 done:
3186 if ((MEM_P (other)
3187 && ! CONSTANT_P (x)
3188 && !REG_P (x)
3189 && GET_CODE (x) != SUBREG)
3190 || (REG_P (other)
3191 && (REGNO (other) < FIRST_PSEUDO_REGISTER
3192 || reg_mentioned_p (other, x))))
3194 rtx temp = gen_reg_rtx (GET_MODE (x));
3195 emit_move_insn (temp, x);
3196 return temp;
3198 return x;
3201 /* Emission of insns (adding them to the doubly-linked list). */
3203 /* Return the last insn emitted, even if it is in a sequence now pushed. */
3205 rtx_insn *
3206 get_last_insn_anywhere (void)
3208 struct sequence_stack *seq;
3209 for (seq = get_current_sequence (); seq; seq = seq->next)
3210 if (seq->last != 0)
3211 return seq->last;
3212 return 0;
3215 /* Return the first nonnote insn emitted in current sequence or current
3216 function. This routine looks inside SEQUENCEs. */
3218 rtx_insn *
3219 get_first_nonnote_insn (void)
3221 rtx_insn *insn = get_insns ();
3223 if (insn)
3225 if (NOTE_P (insn))
3226 for (insn = next_insn (insn);
3227 insn && NOTE_P (insn);
3228 insn = next_insn (insn))
3229 continue;
3230 else
3232 if (NONJUMP_INSN_P (insn)
3233 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3234 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3238 return insn;
3241 /* Return the last nonnote insn emitted in current sequence or current
3242 function. This routine looks inside SEQUENCEs. */
3244 rtx_insn *
3245 get_last_nonnote_insn (void)
3247 rtx_insn *insn = get_last_insn ();
3249 if (insn)
3251 if (NOTE_P (insn))
3252 for (insn = previous_insn (insn);
3253 insn && NOTE_P (insn);
3254 insn = previous_insn (insn))
3255 continue;
3256 else
3258 if (NONJUMP_INSN_P (insn))
3259 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3260 insn = seq->insn (seq->len () - 1);
3264 return insn;
3267 /* Return the number of actual (non-debug) insns emitted in this
3268 function. */
3271 get_max_insn_count (void)
3273 int n = cur_insn_uid;
3275 /* The table size must be stable across -g, to avoid codegen
3276 differences due to debug insns, and not be affected by
3277 -fmin-insn-uid, to avoid excessive table size and to simplify
3278 debugging of -fcompare-debug failures. */
3279 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3280 n -= cur_debug_insn_uid;
3281 else
3282 n -= MIN_NONDEBUG_INSN_UID;
3284 return n;
3288 /* Return the next insn. If it is a SEQUENCE, return the first insn
3289 of the sequence. */
3291 rtx_insn *
3292 next_insn (rtx_insn *insn)
3294 if (insn)
3296 insn = NEXT_INSN (insn);
3297 if (insn && NONJUMP_INSN_P (insn)
3298 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3299 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3302 return insn;
3305 /* Return the previous insn. If it is a SEQUENCE, return the last insn
3306 of the sequence. */
3308 rtx_insn *
3309 previous_insn (rtx_insn *insn)
3311 if (insn)
3313 insn = PREV_INSN (insn);
3314 if (insn && NONJUMP_INSN_P (insn))
3315 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3316 insn = seq->insn (seq->len () - 1);
3319 return insn;
3322 /* Return the next insn after INSN that is not a NOTE. This routine does not
3323 look inside SEQUENCEs. */
3325 rtx_insn *
3326 next_nonnote_insn (rtx_insn *insn)
3328 while (insn)
3330 insn = NEXT_INSN (insn);
3331 if (insn == 0 || !NOTE_P (insn))
3332 break;
3335 return insn;
3338 /* Return the next insn after INSN that is not a NOTE, but stop the
3339 search before we enter another basic block. This routine does not
3340 look inside SEQUENCEs. */
3342 rtx_insn *
3343 next_nonnote_insn_bb (rtx_insn *insn)
3345 while (insn)
3347 insn = NEXT_INSN (insn);
3348 if (insn == 0 || !NOTE_P (insn))
3349 break;
3350 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3351 return NULL;
3354 return insn;
3357 /* Return the previous insn before INSN that is not a NOTE. This routine does
3358 not look inside SEQUENCEs. */
3360 rtx_insn *
3361 prev_nonnote_insn (rtx_insn *insn)
3363 while (insn)
3365 insn = PREV_INSN (insn);
3366 if (insn == 0 || !NOTE_P (insn))
3367 break;
3370 return insn;
3373 /* Return the previous insn before INSN that is not a NOTE, but stop
3374 the search before we enter another basic block. This routine does
3375 not look inside SEQUENCEs. */
3377 rtx_insn *
3378 prev_nonnote_insn_bb (rtx_insn *insn)
3381 while (insn)
3383 insn = PREV_INSN (insn);
3384 if (insn == 0 || !NOTE_P (insn))
3385 break;
3386 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3387 return NULL;
3390 return insn;
3393 /* Return the next insn after INSN that is not a DEBUG_INSN. This
3394 routine does not look inside SEQUENCEs. */
3396 rtx_insn *
3397 next_nondebug_insn (rtx_insn *insn)
3399 while (insn)
3401 insn = NEXT_INSN (insn);
3402 if (insn == 0 || !DEBUG_INSN_P (insn))
3403 break;
3406 return insn;
3409 /* Return the previous insn before INSN that is not a DEBUG_INSN.
3410 This routine does not look inside SEQUENCEs. */
3412 rtx_insn *
3413 prev_nondebug_insn (rtx_insn *insn)
3415 while (insn)
3417 insn = PREV_INSN (insn);
3418 if (insn == 0 || !DEBUG_INSN_P (insn))
3419 break;
3422 return insn;
3425 /* Return the next insn after INSN that is not a NOTE nor DEBUG_INSN.
3426 This routine does not look inside SEQUENCEs. */
3428 rtx_insn *
3429 next_nonnote_nondebug_insn (rtx_insn *insn)
3431 while (insn)
3433 insn = NEXT_INSN (insn);
3434 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3435 break;
3438 return insn;
3441 /* Return the previous insn before INSN that is not a NOTE nor DEBUG_INSN.
3442 This routine does not look inside SEQUENCEs. */
3444 rtx_insn *
3445 prev_nonnote_nondebug_insn (rtx_insn *insn)
3447 while (insn)
3449 insn = PREV_INSN (insn);
3450 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3451 break;
3454 return insn;
3457 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
3458 or 0, if there is none. This routine does not look inside
3459 SEQUENCEs. */
3461 rtx_insn *
3462 next_real_insn (rtx uncast_insn)
3464 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3466 while (insn)
3468 insn = NEXT_INSN (insn);
3469 if (insn == 0 || INSN_P (insn))
3470 break;
3473 return insn;
3476 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
3477 or 0, if there is none. This routine does not look inside
3478 SEQUENCEs. */
3480 rtx_insn *
3481 prev_real_insn (rtx_insn *insn)
3483 while (insn)
3485 insn = PREV_INSN (insn);
3486 if (insn == 0 || INSN_P (insn))
3487 break;
3490 return insn;
3493 /* Return the last CALL_INSN in the current list, or 0 if there is none.
3494 This routine does not look inside SEQUENCEs. */
3496 rtx_call_insn *
3497 last_call_insn (void)
3499 rtx_insn *insn;
3501 for (insn = get_last_insn ();
3502 insn && !CALL_P (insn);
3503 insn = PREV_INSN (insn))
3506 return safe_as_a <rtx_call_insn *> (insn);
3509 /* Find the next insn after INSN that really does something. This routine
3510 does not look inside SEQUENCEs. After reload this also skips over
3511 standalone USE and CLOBBER insn. */
3514 active_insn_p (const rtx_insn *insn)
3516 return (CALL_P (insn) || JUMP_P (insn)
3517 || JUMP_TABLE_DATA_P (insn) /* FIXME */
3518 || (NONJUMP_INSN_P (insn)
3519 && (! reload_completed
3520 || (GET_CODE (PATTERN (insn)) != USE
3521 && GET_CODE (PATTERN (insn)) != CLOBBER))));
3524 rtx_insn *
3525 next_active_insn (rtx_insn *insn)
3527 while (insn)
3529 insn = NEXT_INSN (insn);
3530 if (insn == 0 || active_insn_p (insn))
3531 break;
3534 return insn;
3537 /* Find the last insn before INSN that really does something. This routine
3538 does not look inside SEQUENCEs. After reload this also skips over
3539 standalone USE and CLOBBER insn. */
3541 rtx_insn *
3542 prev_active_insn (rtx_insn *insn)
3544 while (insn)
3546 insn = PREV_INSN (insn);
3547 if (insn == 0 || active_insn_p (insn))
3548 break;
3551 return insn;
3554 /* Return the next insn that uses CC0 after INSN, which is assumed to
3555 set it. This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
3556 applied to the result of this function should yield INSN).
3558 Normally, this is simply the next insn. However, if a REG_CC_USER note
3559 is present, it contains the insn that uses CC0.
3561 Return 0 if we can't find the insn. */
3563 rtx_insn *
3564 next_cc0_user (rtx_insn *insn)
3566 rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
3568 if (note)
3569 return safe_as_a <rtx_insn *> (XEXP (note, 0));
3571 insn = next_nonnote_insn (insn);
3572 if (insn && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3573 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3575 if (insn && INSN_P (insn) && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
3576 return insn;
3578 return 0;
3581 /* Find the insn that set CC0 for INSN. Unless INSN has a REG_CC_SETTER
3582 note, it is the previous insn. */
3584 rtx_insn *
3585 prev_cc0_setter (rtx_insn *insn)
3587 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3589 if (note)
3590 return safe_as_a <rtx_insn *> (XEXP (note, 0));
3592 insn = prev_nonnote_insn (insn);
3593 gcc_assert (sets_cc0_p (PATTERN (insn)));
3595 return insn;
3598 /* Find a RTX_AUTOINC class rtx which matches DATA. */
3600 static int
3601 find_auto_inc (const_rtx x, const_rtx reg)
3603 subrtx_iterator::array_type array;
3604 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
3606 const_rtx x = *iter;
3607 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC
3608 && rtx_equal_p (reg, XEXP (x, 0)))
3609 return true;
3611 return false;
3614 /* Increment the label uses for all labels present in rtx. */
3616 static void
3617 mark_label_nuses (rtx x)
3619 enum rtx_code code;
3620 int i, j;
3621 const char *fmt;
3623 code = GET_CODE (x);
3624 if (code == LABEL_REF && LABEL_P (label_ref_label (x)))
3625 LABEL_NUSES (label_ref_label (x))++;
3627 fmt = GET_RTX_FORMAT (code);
3628 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3630 if (fmt[i] == 'e')
3631 mark_label_nuses (XEXP (x, i));
3632 else if (fmt[i] == 'E')
3633 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3634 mark_label_nuses (XVECEXP (x, i, j));
3639 /* Try splitting insns that can be split for better scheduling.
3640 PAT is the pattern which might split.
3641 TRIAL is the insn providing PAT.
3642 LAST is nonzero if we should return the last insn of the sequence produced.
3644 If this routine succeeds in splitting, it returns the first or last
3645 replacement insn depending on the value of LAST. Otherwise, it
3646 returns TRIAL. If the insn to be returned can be split, it will be. */
3648 rtx_insn *
3649 try_split (rtx pat, rtx_insn *trial, int last)
3651 rtx_insn *before, *after;
3652 rtx note;
3653 rtx_insn *seq, *tem;
3654 profile_probability probability;
3655 rtx_insn *insn_last, *insn;
3656 int njumps = 0;
3657 rtx_insn *call_insn = NULL;
3659 /* We're not good at redistributing frame information. */
3660 if (RTX_FRAME_RELATED_P (trial))
3661 return trial;
3663 if (any_condjump_p (trial)
3664 && (note = find_reg_note (trial, REG_BR_PROB, 0)))
3665 split_branch_probability
3666 = profile_probability::from_reg_br_prob_note (XINT (note, 0));
3667 else
3668 split_branch_probability = profile_probability::uninitialized ();
3670 probability = split_branch_probability;
3672 seq = split_insns (pat, trial);
3674 split_branch_probability = profile_probability::uninitialized ();
3676 if (!seq)
3677 return trial;
3679 /* Avoid infinite loop if any insn of the result matches
3680 the original pattern. */
3681 insn_last = seq;
3682 while (1)
3684 if (INSN_P (insn_last)
3685 && rtx_equal_p (PATTERN (insn_last), pat))
3686 return trial;
3687 if (!NEXT_INSN (insn_last))
3688 break;
3689 insn_last = NEXT_INSN (insn_last);
3692 /* We will be adding the new sequence to the function. The splitters
3693 may have introduced invalid RTL sharing, so unshare the sequence now. */
3694 unshare_all_rtl_in_chain (seq);
3696 /* Mark labels and copy flags. */
3697 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3699 if (JUMP_P (insn))
3701 if (JUMP_P (trial))
3702 CROSSING_JUMP_P (insn) = CROSSING_JUMP_P (trial);
3703 mark_jump_label (PATTERN (insn), insn, 0);
3704 njumps++;
3705 if (probability.initialized_p ()
3706 && any_condjump_p (insn)
3707 && !find_reg_note (insn, REG_BR_PROB, 0))
3709 /* We can preserve the REG_BR_PROB notes only if exactly
3710 one jump is created, otherwise the machine description
3711 is responsible for this step using
3712 split_branch_probability variable. */
3713 gcc_assert (njumps == 1);
3714 add_reg_br_prob_note (insn, probability);
3719 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3720 in SEQ and copy any additional information across. */
3721 if (CALL_P (trial))
3723 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3724 if (CALL_P (insn))
3726 rtx_insn *next;
3727 rtx *p;
3729 gcc_assert (call_insn == NULL_RTX);
3730 call_insn = insn;
3732 /* Add the old CALL_INSN_FUNCTION_USAGE to whatever the
3733 target may have explicitly specified. */
3734 p = &CALL_INSN_FUNCTION_USAGE (insn);
3735 while (*p)
3736 p = &XEXP (*p, 1);
3737 *p = CALL_INSN_FUNCTION_USAGE (trial);
3739 /* If the old call was a sibling call, the new one must
3740 be too. */
3741 SIBLING_CALL_P (insn) = SIBLING_CALL_P (trial);
3743 /* If the new call is the last instruction in the sequence,
3744 it will effectively replace the old call in-situ. Otherwise
3745 we must move any following NOTE_INSN_CALL_ARG_LOCATION note
3746 so that it comes immediately after the new call. */
3747 if (NEXT_INSN (insn))
3748 for (next = NEXT_INSN (trial);
3749 next && NOTE_P (next);
3750 next = NEXT_INSN (next))
3751 if (NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
3753 remove_insn (next);
3754 add_insn_after (next, insn, NULL);
3755 break;
3760 /* Copy notes, particularly those related to the CFG. */
3761 for (note = REG_NOTES (trial); note; note = XEXP (note, 1))
3763 switch (REG_NOTE_KIND (note))
3765 case REG_EH_REGION:
3766 copy_reg_eh_region_note_backward (note, insn_last, NULL);
3767 break;
3769 case REG_NORETURN:
3770 case REG_SETJMP:
3771 case REG_TM:
3772 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3774 if (CALL_P (insn))
3775 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3777 break;
3779 case REG_NON_LOCAL_GOTO:
3780 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3782 if (JUMP_P (insn))
3783 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3785 break;
3787 case REG_INC:
3788 if (!AUTO_INC_DEC)
3789 break;
3791 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3793 rtx reg = XEXP (note, 0);
3794 if (!FIND_REG_INC_NOTE (insn, reg)
3795 && find_auto_inc (PATTERN (insn), reg))
3796 add_reg_note (insn, REG_INC, reg);
3798 break;
3800 case REG_ARGS_SIZE:
3801 fixup_args_size_notes (NULL, insn_last, INTVAL (XEXP (note, 0)));
3802 break;
3804 case REG_CALL_DECL:
3805 gcc_assert (call_insn != NULL_RTX);
3806 add_reg_note (call_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3807 break;
3809 default:
3810 break;
3814 /* If there are LABELS inside the split insns increment the
3815 usage count so we don't delete the label. */
3816 if (INSN_P (trial))
3818 insn = insn_last;
3819 while (insn != NULL_RTX)
3821 /* JUMP_P insns have already been "marked" above. */
3822 if (NONJUMP_INSN_P (insn))
3823 mark_label_nuses (PATTERN (insn));
3825 insn = PREV_INSN (insn);
3829 before = PREV_INSN (trial);
3830 after = NEXT_INSN (trial);
3832 tem = emit_insn_after_setloc (seq, trial, INSN_LOCATION (trial));
3834 delete_insn (trial);
3836 /* Recursively call try_split for each new insn created; by the
3837 time control returns here that insn will be fully split, so
3838 set LAST and continue from the insn after the one returned.
3839 We can't use next_active_insn here since AFTER may be a note.
3840 Ignore deleted insns, which can be occur if not optimizing. */
3841 for (tem = NEXT_INSN (before); tem != after; tem = NEXT_INSN (tem))
3842 if (! tem->deleted () && INSN_P (tem))
3843 tem = try_split (PATTERN (tem), tem, 1);
3845 /* Return either the first or the last insn, depending on which was
3846 requested. */
3847 return last
3848 ? (after ? PREV_INSN (after) : get_last_insn ())
3849 : NEXT_INSN (before);
3852 /* Make and return an INSN rtx, initializing all its slots.
3853 Store PATTERN in the pattern slots. */
3855 rtx_insn *
3856 make_insn_raw (rtx pattern)
3858 rtx_insn *insn;
3860 insn = as_a <rtx_insn *> (rtx_alloc (INSN));
3862 INSN_UID (insn) = cur_insn_uid++;
3863 PATTERN (insn) = pattern;
3864 INSN_CODE (insn) = -1;
3865 REG_NOTES (insn) = NULL;
3866 INSN_LOCATION (insn) = curr_insn_location ();
3867 BLOCK_FOR_INSN (insn) = NULL;
3869 #ifdef ENABLE_RTL_CHECKING
3870 if (insn
3871 && INSN_P (insn)
3872 && (returnjump_p (insn)
3873 || (GET_CODE (insn) == SET
3874 && SET_DEST (insn) == pc_rtx)))
3876 warning (0, "ICE: emit_insn used where emit_jump_insn needed:\n");
3877 debug_rtx (insn);
3879 #endif
3881 return insn;
3884 /* Like `make_insn_raw' but make a DEBUG_INSN instead of an insn. */
3886 static rtx_insn *
3887 make_debug_insn_raw (rtx pattern)
3889 rtx_debug_insn *insn;
3891 insn = as_a <rtx_debug_insn *> (rtx_alloc (DEBUG_INSN));
3892 INSN_UID (insn) = cur_debug_insn_uid++;
3893 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3894 INSN_UID (insn) = cur_insn_uid++;
3896 PATTERN (insn) = pattern;
3897 INSN_CODE (insn) = -1;
3898 REG_NOTES (insn) = NULL;
3899 INSN_LOCATION (insn) = curr_insn_location ();
3900 BLOCK_FOR_INSN (insn) = NULL;
3902 return insn;
3905 /* Like `make_insn_raw' but make a JUMP_INSN instead of an insn. */
3907 static rtx_insn *
3908 make_jump_insn_raw (rtx pattern)
3910 rtx_jump_insn *insn;
3912 insn = as_a <rtx_jump_insn *> (rtx_alloc (JUMP_INSN));
3913 INSN_UID (insn) = cur_insn_uid++;
3915 PATTERN (insn) = pattern;
3916 INSN_CODE (insn) = -1;
3917 REG_NOTES (insn) = NULL;
3918 JUMP_LABEL (insn) = NULL;
3919 INSN_LOCATION (insn) = curr_insn_location ();
3920 BLOCK_FOR_INSN (insn) = NULL;
3922 return insn;
3925 /* Like `make_insn_raw' but make a CALL_INSN instead of an insn. */
3927 static rtx_insn *
3928 make_call_insn_raw (rtx pattern)
3930 rtx_call_insn *insn;
3932 insn = as_a <rtx_call_insn *> (rtx_alloc (CALL_INSN));
3933 INSN_UID (insn) = cur_insn_uid++;
3935 PATTERN (insn) = pattern;
3936 INSN_CODE (insn) = -1;
3937 REG_NOTES (insn) = NULL;
3938 CALL_INSN_FUNCTION_USAGE (insn) = NULL;
3939 INSN_LOCATION (insn) = curr_insn_location ();
3940 BLOCK_FOR_INSN (insn) = NULL;
3942 return insn;
3945 /* Like `make_insn_raw' but make a NOTE instead of an insn. */
3947 static rtx_note *
3948 make_note_raw (enum insn_note subtype)
3950 /* Some notes are never created this way at all. These notes are
3951 only created by patching out insns. */
3952 gcc_assert (subtype != NOTE_INSN_DELETED_LABEL
3953 && subtype != NOTE_INSN_DELETED_DEBUG_LABEL);
3955 rtx_note *note = as_a <rtx_note *> (rtx_alloc (NOTE));
3956 INSN_UID (note) = cur_insn_uid++;
3957 NOTE_KIND (note) = subtype;
3958 BLOCK_FOR_INSN (note) = NULL;
3959 memset (&NOTE_DATA (note), 0, sizeof (NOTE_DATA (note)));
3960 return note;
3963 /* Add INSN to the end of the doubly-linked list, between PREV and NEXT.
3964 INSN may be any object that can appear in the chain: INSN_P and NOTE_P objects,
3965 but also BARRIERs and JUMP_TABLE_DATAs. PREV and NEXT may be NULL. */
3967 static inline void
3968 link_insn_into_chain (rtx_insn *insn, rtx_insn *prev, rtx_insn *next)
3970 SET_PREV_INSN (insn) = prev;
3971 SET_NEXT_INSN (insn) = next;
3972 if (prev != NULL)
3974 SET_NEXT_INSN (prev) = insn;
3975 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
3977 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (prev));
3978 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = insn;
3981 if (next != NULL)
3983 SET_PREV_INSN (next) = insn;
3984 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
3986 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (next));
3987 SET_PREV_INSN (sequence->insn (0)) = insn;
3991 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3993 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (insn));
3994 SET_PREV_INSN (sequence->insn (0)) = prev;
3995 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = next;
3999 /* Add INSN to the end of the doubly-linked list.
4000 INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE. */
4002 void
4003 add_insn (rtx_insn *insn)
4005 rtx_insn *prev = get_last_insn ();
4006 link_insn_into_chain (insn, prev, NULL);
4007 if (NULL == get_insns ())
4008 set_first_insn (insn);
4009 set_last_insn (insn);
4012 /* Add INSN into the doubly-linked list after insn AFTER. */
4014 static void
4015 add_insn_after_nobb (rtx_insn *insn, rtx_insn *after)
4017 rtx_insn *next = NEXT_INSN (after);
4019 gcc_assert (!optimize || !after->deleted ());
4021 link_insn_into_chain (insn, after, next);
4023 if (next == NULL)
4025 struct sequence_stack *seq;
4027 for (seq = get_current_sequence (); seq; seq = seq->next)
4028 if (after == seq->last)
4030 seq->last = insn;
4031 break;
4036 /* Add INSN into the doubly-linked list before insn BEFORE. */
4038 static void
4039 add_insn_before_nobb (rtx_insn *insn, rtx_insn *before)
4041 rtx_insn *prev = PREV_INSN (before);
4043 gcc_assert (!optimize || !before->deleted ());
4045 link_insn_into_chain (insn, prev, before);
4047 if (prev == NULL)
4049 struct sequence_stack *seq;
4051 for (seq = get_current_sequence (); seq; seq = seq->next)
4052 if (before == seq->first)
4054 seq->first = insn;
4055 break;
4058 gcc_assert (seq);
4062 /* Like add_insn_after_nobb, but try to set BLOCK_FOR_INSN.
4063 If BB is NULL, an attempt is made to infer the bb from before.
4065 This and the next function should be the only functions called
4066 to insert an insn once delay slots have been filled since only
4067 they know how to update a SEQUENCE. */
4069 void
4070 add_insn_after (rtx uncast_insn, rtx uncast_after, basic_block bb)
4072 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4073 rtx_insn *after = as_a <rtx_insn *> (uncast_after);
4074 add_insn_after_nobb (insn, after);
4075 if (!BARRIER_P (after)
4076 && !BARRIER_P (insn)
4077 && (bb = BLOCK_FOR_INSN (after)))
4079 set_block_for_insn (insn, bb);
4080 if (INSN_P (insn))
4081 df_insn_rescan (insn);
4082 /* Should not happen as first in the BB is always
4083 either NOTE or LABEL. */
4084 if (BB_END (bb) == after
4085 /* Avoid clobbering of structure when creating new BB. */
4086 && !BARRIER_P (insn)
4087 && !NOTE_INSN_BASIC_BLOCK_P (insn))
4088 BB_END (bb) = insn;
4092 /* Like add_insn_before_nobb, but try to set BLOCK_FOR_INSN.
4093 If BB is NULL, an attempt is made to infer the bb from before.
4095 This and the previous function should be the only functions called
4096 to insert an insn once delay slots have been filled since only
4097 they know how to update a SEQUENCE. */
4099 void
4100 add_insn_before (rtx uncast_insn, rtx uncast_before, basic_block bb)
4102 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4103 rtx_insn *before = as_a <rtx_insn *> (uncast_before);
4104 add_insn_before_nobb (insn, before);
4106 if (!bb
4107 && !BARRIER_P (before)
4108 && !BARRIER_P (insn))
4109 bb = BLOCK_FOR_INSN (before);
4111 if (bb)
4113 set_block_for_insn (insn, bb);
4114 if (INSN_P (insn))
4115 df_insn_rescan (insn);
4116 /* Should not happen as first in the BB is always either NOTE or
4117 LABEL. */
4118 gcc_assert (BB_HEAD (bb) != insn
4119 /* Avoid clobbering of structure when creating new BB. */
4120 || BARRIER_P (insn)
4121 || NOTE_INSN_BASIC_BLOCK_P (insn));
4125 /* Replace insn with an deleted instruction note. */
4127 void
4128 set_insn_deleted (rtx insn)
4130 if (INSN_P (insn))
4131 df_insn_delete (as_a <rtx_insn *> (insn));
4132 PUT_CODE (insn, NOTE);
4133 NOTE_KIND (insn) = NOTE_INSN_DELETED;
4137 /* Unlink INSN from the insn chain.
4139 This function knows how to handle sequences.
4141 This function does not invalidate data flow information associated with
4142 INSN (i.e. does not call df_insn_delete). That makes this function
4143 usable for only disconnecting an insn from the chain, and re-emit it
4144 elsewhere later.
4146 To later insert INSN elsewhere in the insn chain via add_insn and
4147 similar functions, PREV_INSN and NEXT_INSN must be nullified by
4148 the caller. Nullifying them here breaks many insn chain walks.
4150 To really delete an insn and related DF information, use delete_insn. */
4152 void
4153 remove_insn (rtx uncast_insn)
4155 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4156 rtx_insn *next = NEXT_INSN (insn);
4157 rtx_insn *prev = PREV_INSN (insn);
4158 basic_block bb;
4160 if (prev)
4162 SET_NEXT_INSN (prev) = next;
4163 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
4165 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (prev));
4166 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = next;
4169 else
4171 struct sequence_stack *seq;
4173 for (seq = get_current_sequence (); seq; seq = seq->next)
4174 if (insn == seq->first)
4176 seq->first = next;
4177 break;
4180 gcc_assert (seq);
4183 if (next)
4185 SET_PREV_INSN (next) = prev;
4186 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
4188 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (next));
4189 SET_PREV_INSN (sequence->insn (0)) = prev;
4192 else
4194 struct sequence_stack *seq;
4196 for (seq = get_current_sequence (); seq; seq = seq->next)
4197 if (insn == seq->last)
4199 seq->last = prev;
4200 break;
4203 gcc_assert (seq);
4206 /* Fix up basic block boundaries, if necessary. */
4207 if (!BARRIER_P (insn)
4208 && (bb = BLOCK_FOR_INSN (insn)))
4210 if (BB_HEAD (bb) == insn)
4212 /* Never ever delete the basic block note without deleting whole
4213 basic block. */
4214 gcc_assert (!NOTE_P (insn));
4215 BB_HEAD (bb) = next;
4217 if (BB_END (bb) == insn)
4218 BB_END (bb) = prev;
4222 /* Append CALL_FUSAGE to the CALL_INSN_FUNCTION_USAGE for CALL_INSN. */
4224 void
4225 add_function_usage_to (rtx call_insn, rtx call_fusage)
4227 gcc_assert (call_insn && CALL_P (call_insn));
4229 /* Put the register usage information on the CALL. If there is already
4230 some usage information, put ours at the end. */
4231 if (CALL_INSN_FUNCTION_USAGE (call_insn))
4233 rtx link;
4235 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
4236 link = XEXP (link, 1))
4239 XEXP (link, 1) = call_fusage;
4241 else
4242 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
4245 /* Delete all insns made since FROM.
4246 FROM becomes the new last instruction. */
4248 void
4249 delete_insns_since (rtx_insn *from)
4251 if (from == 0)
4252 set_first_insn (0);
4253 else
4254 SET_NEXT_INSN (from) = 0;
4255 set_last_insn (from);
4258 /* This function is deprecated, please use sequences instead.
4260 Move a consecutive bunch of insns to a different place in the chain.
4261 The insns to be moved are those between FROM and TO.
4262 They are moved to a new position after the insn AFTER.
4263 AFTER must not be FROM or TO or any insn in between.
4265 This function does not know about SEQUENCEs and hence should not be
4266 called after delay-slot filling has been done. */
4268 void
4269 reorder_insns_nobb (rtx_insn *from, rtx_insn *to, rtx_insn *after)
4271 if (flag_checking)
4273 for (rtx_insn *x = from; x != to; x = NEXT_INSN (x))
4274 gcc_assert (after != x);
4275 gcc_assert (after != to);
4278 /* Splice this bunch out of where it is now. */
4279 if (PREV_INSN (from))
4280 SET_NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
4281 if (NEXT_INSN (to))
4282 SET_PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
4283 if (get_last_insn () == to)
4284 set_last_insn (PREV_INSN (from));
4285 if (get_insns () == from)
4286 set_first_insn (NEXT_INSN (to));
4288 /* Make the new neighbors point to it and it to them. */
4289 if (NEXT_INSN (after))
4290 SET_PREV_INSN (NEXT_INSN (after)) = to;
4292 SET_NEXT_INSN (to) = NEXT_INSN (after);
4293 SET_PREV_INSN (from) = after;
4294 SET_NEXT_INSN (after) = from;
4295 if (after == get_last_insn ())
4296 set_last_insn (to);
4299 /* Same as function above, but take care to update BB boundaries. */
4300 void
4301 reorder_insns (rtx_insn *from, rtx_insn *to, rtx_insn *after)
4303 rtx_insn *prev = PREV_INSN (from);
4304 basic_block bb, bb2;
4306 reorder_insns_nobb (from, to, after);
4308 if (!BARRIER_P (after)
4309 && (bb = BLOCK_FOR_INSN (after)))
4311 rtx_insn *x;
4312 df_set_bb_dirty (bb);
4314 if (!BARRIER_P (from)
4315 && (bb2 = BLOCK_FOR_INSN (from)))
4317 if (BB_END (bb2) == to)
4318 BB_END (bb2) = prev;
4319 df_set_bb_dirty (bb2);
4322 if (BB_END (bb) == after)
4323 BB_END (bb) = to;
4325 for (x = from; x != NEXT_INSN (to); x = NEXT_INSN (x))
4326 if (!BARRIER_P (x))
4327 df_insn_change_bb (x, bb);
4332 /* Emit insn(s) of given code and pattern
4333 at a specified place within the doubly-linked list.
4335 All of the emit_foo global entry points accept an object
4336 X which is either an insn list or a PATTERN of a single
4337 instruction.
4339 There are thus a few canonical ways to generate code and
4340 emit it at a specific place in the instruction stream. For
4341 example, consider the instruction named SPOT and the fact that
4342 we would like to emit some instructions before SPOT. We might
4343 do it like this:
4345 start_sequence ();
4346 ... emit the new instructions ...
4347 insns_head = get_insns ();
4348 end_sequence ();
4350 emit_insn_before (insns_head, SPOT);
4352 It used to be common to generate SEQUENCE rtl instead, but that
4353 is a relic of the past which no longer occurs. The reason is that
4354 SEQUENCE rtl results in much fragmented RTL memory since the SEQUENCE
4355 generated would almost certainly die right after it was created. */
4357 static rtx_insn *
4358 emit_pattern_before_noloc (rtx x, rtx before, rtx last, basic_block bb,
4359 rtx_insn *(*make_raw) (rtx))
4361 rtx_insn *insn;
4363 gcc_assert (before);
4365 if (x == NULL_RTX)
4366 return safe_as_a <rtx_insn *> (last);
4368 switch (GET_CODE (x))
4370 case DEBUG_INSN:
4371 case INSN:
4372 case JUMP_INSN:
4373 case CALL_INSN:
4374 case CODE_LABEL:
4375 case BARRIER:
4376 case NOTE:
4377 insn = as_a <rtx_insn *> (x);
4378 while (insn)
4380 rtx_insn *next = NEXT_INSN (insn);
4381 add_insn_before (insn, before, bb);
4382 last = insn;
4383 insn = next;
4385 break;
4387 #ifdef ENABLE_RTL_CHECKING
4388 case SEQUENCE:
4389 gcc_unreachable ();
4390 break;
4391 #endif
4393 default:
4394 last = (*make_raw) (x);
4395 add_insn_before (last, before, bb);
4396 break;
4399 return safe_as_a <rtx_insn *> (last);
4402 /* Make X be output before the instruction BEFORE. */
4404 rtx_insn *
4405 emit_insn_before_noloc (rtx x, rtx_insn *before, basic_block bb)
4407 return emit_pattern_before_noloc (x, before, before, bb, make_insn_raw);
4410 /* Make an instruction with body X and code JUMP_INSN
4411 and output it before the instruction BEFORE. */
4413 rtx_jump_insn *
4414 emit_jump_insn_before_noloc (rtx x, rtx_insn *before)
4416 return as_a <rtx_jump_insn *> (
4417 emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4418 make_jump_insn_raw));
4421 /* Make an instruction with body X and code CALL_INSN
4422 and output it before the instruction BEFORE. */
4424 rtx_insn *
4425 emit_call_insn_before_noloc (rtx x, rtx_insn *before)
4427 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4428 make_call_insn_raw);
4431 /* Make an instruction with body X and code DEBUG_INSN
4432 and output it before the instruction BEFORE. */
4434 rtx_insn *
4435 emit_debug_insn_before_noloc (rtx x, rtx before)
4437 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4438 make_debug_insn_raw);
4441 /* Make an insn of code BARRIER
4442 and output it before the insn BEFORE. */
4444 rtx_barrier *
4445 emit_barrier_before (rtx before)
4447 rtx_barrier *insn = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
4449 INSN_UID (insn) = cur_insn_uid++;
4451 add_insn_before (insn, before, NULL);
4452 return insn;
4455 /* Emit the label LABEL before the insn BEFORE. */
4457 rtx_code_label *
4458 emit_label_before (rtx label, rtx_insn *before)
4460 gcc_checking_assert (INSN_UID (label) == 0);
4461 INSN_UID (label) = cur_insn_uid++;
4462 add_insn_before (label, before, NULL);
4463 return as_a <rtx_code_label *> (label);
4466 /* Helper for emit_insn_after, handles lists of instructions
4467 efficiently. */
4469 static rtx_insn *
4470 emit_insn_after_1 (rtx_insn *first, rtx uncast_after, basic_block bb)
4472 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4473 rtx_insn *last;
4474 rtx_insn *after_after;
4475 if (!bb && !BARRIER_P (after))
4476 bb = BLOCK_FOR_INSN (after);
4478 if (bb)
4480 df_set_bb_dirty (bb);
4481 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4482 if (!BARRIER_P (last))
4484 set_block_for_insn (last, bb);
4485 df_insn_rescan (last);
4487 if (!BARRIER_P (last))
4489 set_block_for_insn (last, bb);
4490 df_insn_rescan (last);
4492 if (BB_END (bb) == after)
4493 BB_END (bb) = last;
4495 else
4496 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4497 continue;
4499 after_after = NEXT_INSN (after);
4501 SET_NEXT_INSN (after) = first;
4502 SET_PREV_INSN (first) = after;
4503 SET_NEXT_INSN (last) = after_after;
4504 if (after_after)
4505 SET_PREV_INSN (after_after) = last;
4507 if (after == get_last_insn ())
4508 set_last_insn (last);
4510 return last;
4513 static rtx_insn *
4514 emit_pattern_after_noloc (rtx x, rtx uncast_after, basic_block bb,
4515 rtx_insn *(*make_raw)(rtx))
4517 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4518 rtx_insn *last = after;
4520 gcc_assert (after);
4522 if (x == NULL_RTX)
4523 return last;
4525 switch (GET_CODE (x))
4527 case DEBUG_INSN:
4528 case INSN:
4529 case JUMP_INSN:
4530 case CALL_INSN:
4531 case CODE_LABEL:
4532 case BARRIER:
4533 case NOTE:
4534 last = emit_insn_after_1 (as_a <rtx_insn *> (x), after, bb);
4535 break;
4537 #ifdef ENABLE_RTL_CHECKING
4538 case SEQUENCE:
4539 gcc_unreachable ();
4540 break;
4541 #endif
4543 default:
4544 last = (*make_raw) (x);
4545 add_insn_after (last, after, bb);
4546 break;
4549 return last;
4552 /* Make X be output after the insn AFTER and set the BB of insn. If
4553 BB is NULL, an attempt is made to infer the BB from AFTER. */
4555 rtx_insn *
4556 emit_insn_after_noloc (rtx x, rtx after, basic_block bb)
4558 return emit_pattern_after_noloc (x, after, bb, make_insn_raw);
4562 /* Make an insn of code JUMP_INSN with body X
4563 and output it after the insn AFTER. */
4565 rtx_jump_insn *
4566 emit_jump_insn_after_noloc (rtx x, rtx after)
4568 return as_a <rtx_jump_insn *> (
4569 emit_pattern_after_noloc (x, after, NULL, make_jump_insn_raw));
4572 /* Make an instruction with body X and code CALL_INSN
4573 and output it after the instruction AFTER. */
4575 rtx_insn *
4576 emit_call_insn_after_noloc (rtx x, rtx after)
4578 return emit_pattern_after_noloc (x, after, NULL, make_call_insn_raw);
4581 /* Make an instruction with body X and code CALL_INSN
4582 and output it after the instruction AFTER. */
4584 rtx_insn *
4585 emit_debug_insn_after_noloc (rtx x, rtx after)
4587 return emit_pattern_after_noloc (x, after, NULL, make_debug_insn_raw);
4590 /* Make an insn of code BARRIER
4591 and output it after the insn AFTER. */
4593 rtx_barrier *
4594 emit_barrier_after (rtx after)
4596 rtx_barrier *insn = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
4598 INSN_UID (insn) = cur_insn_uid++;
4600 add_insn_after (insn, after, NULL);
4601 return insn;
4604 /* Emit the label LABEL after the insn AFTER. */
4606 rtx_insn *
4607 emit_label_after (rtx label, rtx_insn *after)
4609 gcc_checking_assert (INSN_UID (label) == 0);
4610 INSN_UID (label) = cur_insn_uid++;
4611 add_insn_after (label, after, NULL);
4612 return as_a <rtx_insn *> (label);
4615 /* Notes require a bit of special handling: Some notes need to have their
4616 BLOCK_FOR_INSN set, others should never have it set, and some should
4617 have it set or clear depending on the context. */
4619 /* Return true iff a note of kind SUBTYPE should be emitted with routines
4620 that never set BLOCK_FOR_INSN on NOTE. BB_BOUNDARY is true if the
4621 caller is asked to emit a note before BB_HEAD, or after BB_END. */
4623 static bool
4624 note_outside_basic_block_p (enum insn_note subtype, bool on_bb_boundary_p)
4626 switch (subtype)
4628 /* NOTE_INSN_SWITCH_TEXT_SECTIONS only appears between basic blocks. */
4629 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4630 return true;
4632 /* Notes for var tracking and EH region markers can appear between or
4633 inside basic blocks. If the caller is emitting on the basic block
4634 boundary, do not set BLOCK_FOR_INSN on the new note. */
4635 case NOTE_INSN_VAR_LOCATION:
4636 case NOTE_INSN_CALL_ARG_LOCATION:
4637 case NOTE_INSN_EH_REGION_BEG:
4638 case NOTE_INSN_EH_REGION_END:
4639 return on_bb_boundary_p;
4641 /* Otherwise, BLOCK_FOR_INSN must be set. */
4642 default:
4643 return false;
4647 /* Emit a note of subtype SUBTYPE after the insn AFTER. */
4649 rtx_note *
4650 emit_note_after (enum insn_note subtype, rtx_insn *after)
4652 rtx_note *note = make_note_raw (subtype);
4653 basic_block bb = BARRIER_P (after) ? NULL : BLOCK_FOR_INSN (after);
4654 bool on_bb_boundary_p = (bb != NULL && BB_END (bb) == after);
4656 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4657 add_insn_after_nobb (note, after);
4658 else
4659 add_insn_after (note, after, bb);
4660 return note;
4663 /* Emit a note of subtype SUBTYPE before the insn BEFORE. */
4665 rtx_note *
4666 emit_note_before (enum insn_note subtype, rtx_insn *before)
4668 rtx_note *note = make_note_raw (subtype);
4669 basic_block bb = BARRIER_P (before) ? NULL : BLOCK_FOR_INSN (before);
4670 bool on_bb_boundary_p = (bb != NULL && BB_HEAD (bb) == before);
4672 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4673 add_insn_before_nobb (note, before);
4674 else
4675 add_insn_before (note, before, bb);
4676 return note;
4679 /* Insert PATTERN after AFTER, setting its INSN_LOCATION to LOC.
4680 MAKE_RAW indicates how to turn PATTERN into a real insn. */
4682 static rtx_insn *
4683 emit_pattern_after_setloc (rtx pattern, rtx uncast_after, int loc,
4684 rtx_insn *(*make_raw) (rtx))
4686 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4687 rtx_insn *last = emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4689 if (pattern == NULL_RTX || !loc)
4690 return last;
4692 after = NEXT_INSN (after);
4693 while (1)
4695 if (active_insn_p (after)
4696 && !JUMP_TABLE_DATA_P (after) /* FIXME */
4697 && !INSN_LOCATION (after))
4698 INSN_LOCATION (after) = loc;
4699 if (after == last)
4700 break;
4701 after = NEXT_INSN (after);
4703 return last;
4706 /* Insert PATTERN after AFTER. MAKE_RAW indicates how to turn PATTERN
4707 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert after
4708 any DEBUG_INSNs. */
4710 static rtx_insn *
4711 emit_pattern_after (rtx pattern, rtx uncast_after, bool skip_debug_insns,
4712 rtx_insn *(*make_raw) (rtx))
4714 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4715 rtx_insn *prev = after;
4717 if (skip_debug_insns)
4718 while (DEBUG_INSN_P (prev))
4719 prev = PREV_INSN (prev);
4721 if (INSN_P (prev))
4722 return emit_pattern_after_setloc (pattern, after, INSN_LOCATION (prev),
4723 make_raw);
4724 else
4725 return emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4728 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4729 rtx_insn *
4730 emit_insn_after_setloc (rtx pattern, rtx after, int loc)
4732 return emit_pattern_after_setloc (pattern, after, loc, make_insn_raw);
4735 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4736 rtx_insn *
4737 emit_insn_after (rtx pattern, rtx after)
4739 return emit_pattern_after (pattern, after, true, make_insn_raw);
4742 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4743 rtx_jump_insn *
4744 emit_jump_insn_after_setloc (rtx pattern, rtx after, int loc)
4746 return as_a <rtx_jump_insn *> (
4747 emit_pattern_after_setloc (pattern, after, loc, make_jump_insn_raw));
4750 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4751 rtx_jump_insn *
4752 emit_jump_insn_after (rtx pattern, rtx after)
4754 return as_a <rtx_jump_insn *> (
4755 emit_pattern_after (pattern, after, true, make_jump_insn_raw));
4758 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4759 rtx_insn *
4760 emit_call_insn_after_setloc (rtx pattern, rtx after, int loc)
4762 return emit_pattern_after_setloc (pattern, after, loc, make_call_insn_raw);
4765 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4766 rtx_insn *
4767 emit_call_insn_after (rtx pattern, rtx after)
4769 return emit_pattern_after (pattern, after, true, make_call_insn_raw);
4772 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4773 rtx_insn *
4774 emit_debug_insn_after_setloc (rtx pattern, rtx after, int loc)
4776 return emit_pattern_after_setloc (pattern, after, loc, make_debug_insn_raw);
4779 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4780 rtx_insn *
4781 emit_debug_insn_after (rtx pattern, rtx after)
4783 return emit_pattern_after (pattern, after, false, make_debug_insn_raw);
4786 /* Insert PATTERN before BEFORE, setting its INSN_LOCATION to LOC.
4787 MAKE_RAW indicates how to turn PATTERN into a real insn. INSNP
4788 indicates if PATTERN is meant for an INSN as opposed to a JUMP_INSN,
4789 CALL_INSN, etc. */
4791 static rtx_insn *
4792 emit_pattern_before_setloc (rtx pattern, rtx uncast_before, int loc, bool insnp,
4793 rtx_insn *(*make_raw) (rtx))
4795 rtx_insn *before = as_a <rtx_insn *> (uncast_before);
4796 rtx_insn *first = PREV_INSN (before);
4797 rtx_insn *last = emit_pattern_before_noloc (pattern, before,
4798 insnp ? before : NULL_RTX,
4799 NULL, make_raw);
4801 if (pattern == NULL_RTX || !loc)
4802 return last;
4804 if (!first)
4805 first = get_insns ();
4806 else
4807 first = NEXT_INSN (first);
4808 while (1)
4810 if (active_insn_p (first)
4811 && !JUMP_TABLE_DATA_P (first) /* FIXME */
4812 && !INSN_LOCATION (first))
4813 INSN_LOCATION (first) = loc;
4814 if (first == last)
4815 break;
4816 first = NEXT_INSN (first);
4818 return last;
4821 /* Insert PATTERN before BEFORE. MAKE_RAW indicates how to turn PATTERN
4822 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert
4823 before any DEBUG_INSNs. INSNP indicates if PATTERN is meant for an
4824 INSN as opposed to a JUMP_INSN, CALL_INSN, etc. */
4826 static rtx_insn *
4827 emit_pattern_before (rtx pattern, rtx uncast_before, bool skip_debug_insns,
4828 bool insnp, rtx_insn *(*make_raw) (rtx))
4830 rtx_insn *before = safe_as_a <rtx_insn *> (uncast_before);
4831 rtx_insn *next = before;
4833 if (skip_debug_insns)
4834 while (DEBUG_INSN_P (next))
4835 next = PREV_INSN (next);
4837 if (INSN_P (next))
4838 return emit_pattern_before_setloc (pattern, before, INSN_LOCATION (next),
4839 insnp, make_raw);
4840 else
4841 return emit_pattern_before_noloc (pattern, before,
4842 insnp ? before : NULL_RTX,
4843 NULL, make_raw);
4846 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4847 rtx_insn *
4848 emit_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4850 return emit_pattern_before_setloc (pattern, before, loc, true,
4851 make_insn_raw);
4854 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4855 rtx_insn *
4856 emit_insn_before (rtx pattern, rtx before)
4858 return emit_pattern_before (pattern, before, true, true, make_insn_raw);
4861 /* like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4862 rtx_jump_insn *
4863 emit_jump_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4865 return as_a <rtx_jump_insn *> (
4866 emit_pattern_before_setloc (pattern, before, loc, false,
4867 make_jump_insn_raw));
4870 /* Like emit_jump_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4871 rtx_jump_insn *
4872 emit_jump_insn_before (rtx pattern, rtx before)
4874 return as_a <rtx_jump_insn *> (
4875 emit_pattern_before (pattern, before, true, false,
4876 make_jump_insn_raw));
4879 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4880 rtx_insn *
4881 emit_call_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4883 return emit_pattern_before_setloc (pattern, before, loc, false,
4884 make_call_insn_raw);
4887 /* Like emit_call_insn_before_noloc,
4888 but set insn_location according to BEFORE. */
4889 rtx_insn *
4890 emit_call_insn_before (rtx pattern, rtx_insn *before)
4892 return emit_pattern_before (pattern, before, true, false,
4893 make_call_insn_raw);
4896 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4897 rtx_insn *
4898 emit_debug_insn_before_setloc (rtx pattern, rtx before, int loc)
4900 return emit_pattern_before_setloc (pattern, before, loc, false,
4901 make_debug_insn_raw);
4904 /* Like emit_debug_insn_before_noloc,
4905 but set insn_location according to BEFORE. */
4906 rtx_insn *
4907 emit_debug_insn_before (rtx pattern, rtx_insn *before)
4909 return emit_pattern_before (pattern, before, false, false,
4910 make_debug_insn_raw);
4913 /* Take X and emit it at the end of the doubly-linked
4914 INSN list.
4916 Returns the last insn emitted. */
4918 rtx_insn *
4919 emit_insn (rtx x)
4921 rtx_insn *last = get_last_insn ();
4922 rtx_insn *insn;
4924 if (x == NULL_RTX)
4925 return last;
4927 switch (GET_CODE (x))
4929 case DEBUG_INSN:
4930 case INSN:
4931 case JUMP_INSN:
4932 case CALL_INSN:
4933 case CODE_LABEL:
4934 case BARRIER:
4935 case NOTE:
4936 insn = as_a <rtx_insn *> (x);
4937 while (insn)
4939 rtx_insn *next = NEXT_INSN (insn);
4940 add_insn (insn);
4941 last = insn;
4942 insn = next;
4944 break;
4946 #ifdef ENABLE_RTL_CHECKING
4947 case JUMP_TABLE_DATA:
4948 case SEQUENCE:
4949 gcc_unreachable ();
4950 break;
4951 #endif
4953 default:
4954 last = make_insn_raw (x);
4955 add_insn (last);
4956 break;
4959 return last;
4962 /* Make an insn of code DEBUG_INSN with pattern X
4963 and add it to the end of the doubly-linked list. */
4965 rtx_insn *
4966 emit_debug_insn (rtx x)
4968 rtx_insn *last = get_last_insn ();
4969 rtx_insn *insn;
4971 if (x == NULL_RTX)
4972 return last;
4974 switch (GET_CODE (x))
4976 case DEBUG_INSN:
4977 case INSN:
4978 case JUMP_INSN:
4979 case CALL_INSN:
4980 case CODE_LABEL:
4981 case BARRIER:
4982 case NOTE:
4983 insn = as_a <rtx_insn *> (x);
4984 while (insn)
4986 rtx_insn *next = NEXT_INSN (insn);
4987 add_insn (insn);
4988 last = insn;
4989 insn = next;
4991 break;
4993 #ifdef ENABLE_RTL_CHECKING
4994 case JUMP_TABLE_DATA:
4995 case SEQUENCE:
4996 gcc_unreachable ();
4997 break;
4998 #endif
5000 default:
5001 last = make_debug_insn_raw (x);
5002 add_insn (last);
5003 break;
5006 return last;
5009 /* Make an insn of code JUMP_INSN with pattern X
5010 and add it to the end of the doubly-linked list. */
5012 rtx_insn *
5013 emit_jump_insn (rtx x)
5015 rtx_insn *last = NULL;
5016 rtx_insn *insn;
5018 switch (GET_CODE (x))
5020 case DEBUG_INSN:
5021 case INSN:
5022 case JUMP_INSN:
5023 case CALL_INSN:
5024 case CODE_LABEL:
5025 case BARRIER:
5026 case NOTE:
5027 insn = as_a <rtx_insn *> (x);
5028 while (insn)
5030 rtx_insn *next = NEXT_INSN (insn);
5031 add_insn (insn);
5032 last = insn;
5033 insn = next;
5035 break;
5037 #ifdef ENABLE_RTL_CHECKING
5038 case JUMP_TABLE_DATA:
5039 case SEQUENCE:
5040 gcc_unreachable ();
5041 break;
5042 #endif
5044 default:
5045 last = make_jump_insn_raw (x);
5046 add_insn (last);
5047 break;
5050 return last;
5053 /* Make an insn of code CALL_INSN with pattern X
5054 and add it to the end of the doubly-linked list. */
5056 rtx_insn *
5057 emit_call_insn (rtx x)
5059 rtx_insn *insn;
5061 switch (GET_CODE (x))
5063 case DEBUG_INSN:
5064 case INSN:
5065 case JUMP_INSN:
5066 case CALL_INSN:
5067 case CODE_LABEL:
5068 case BARRIER:
5069 case NOTE:
5070 insn = emit_insn (x);
5071 break;
5073 #ifdef ENABLE_RTL_CHECKING
5074 case SEQUENCE:
5075 case JUMP_TABLE_DATA:
5076 gcc_unreachable ();
5077 break;
5078 #endif
5080 default:
5081 insn = make_call_insn_raw (x);
5082 add_insn (insn);
5083 break;
5086 return insn;
5089 /* Add the label LABEL to the end of the doubly-linked list. */
5091 rtx_code_label *
5092 emit_label (rtx uncast_label)
5094 rtx_code_label *label = as_a <rtx_code_label *> (uncast_label);
5096 gcc_checking_assert (INSN_UID (label) == 0);
5097 INSN_UID (label) = cur_insn_uid++;
5098 add_insn (label);
5099 return label;
5102 /* Make an insn of code JUMP_TABLE_DATA
5103 and add it to the end of the doubly-linked list. */
5105 rtx_jump_table_data *
5106 emit_jump_table_data (rtx table)
5108 rtx_jump_table_data *jump_table_data =
5109 as_a <rtx_jump_table_data *> (rtx_alloc (JUMP_TABLE_DATA));
5110 INSN_UID (jump_table_data) = cur_insn_uid++;
5111 PATTERN (jump_table_data) = table;
5112 BLOCK_FOR_INSN (jump_table_data) = NULL;
5113 add_insn (jump_table_data);
5114 return jump_table_data;
5117 /* Make an insn of code BARRIER
5118 and add it to the end of the doubly-linked list. */
5120 rtx_barrier *
5121 emit_barrier (void)
5123 rtx_barrier *barrier = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
5124 INSN_UID (barrier) = cur_insn_uid++;
5125 add_insn (barrier);
5126 return barrier;
5129 /* Emit a copy of note ORIG. */
5131 rtx_note *
5132 emit_note_copy (rtx_note *orig)
5134 enum insn_note kind = (enum insn_note) NOTE_KIND (orig);
5135 rtx_note *note = make_note_raw (kind);
5136 NOTE_DATA (note) = NOTE_DATA (orig);
5137 add_insn (note);
5138 return note;
5141 /* Make an insn of code NOTE or type NOTE_NO
5142 and add it to the end of the doubly-linked list. */
5144 rtx_note *
5145 emit_note (enum insn_note kind)
5147 rtx_note *note = make_note_raw (kind);
5148 add_insn (note);
5149 return note;
5152 /* Emit a clobber of lvalue X. */
5154 rtx_insn *
5155 emit_clobber (rtx x)
5157 /* CONCATs should not appear in the insn stream. */
5158 if (GET_CODE (x) == CONCAT)
5160 emit_clobber (XEXP (x, 0));
5161 return emit_clobber (XEXP (x, 1));
5163 return emit_insn (gen_rtx_CLOBBER (VOIDmode, x));
5166 /* Return a sequence of insns to clobber lvalue X. */
5168 rtx_insn *
5169 gen_clobber (rtx x)
5171 rtx_insn *seq;
5173 start_sequence ();
5174 emit_clobber (x);
5175 seq = get_insns ();
5176 end_sequence ();
5177 return seq;
5180 /* Emit a use of rvalue X. */
5182 rtx_insn *
5183 emit_use (rtx x)
5185 /* CONCATs should not appear in the insn stream. */
5186 if (GET_CODE (x) == CONCAT)
5188 emit_use (XEXP (x, 0));
5189 return emit_use (XEXP (x, 1));
5191 return emit_insn (gen_rtx_USE (VOIDmode, x));
5194 /* Return a sequence of insns to use rvalue X. */
5196 rtx_insn *
5197 gen_use (rtx x)
5199 rtx_insn *seq;
5201 start_sequence ();
5202 emit_use (x);
5203 seq = get_insns ();
5204 end_sequence ();
5205 return seq;
5208 /* Notes like REG_EQUAL and REG_EQUIV refer to a set in an instruction.
5209 Return the set in INSN that such notes describe, or NULL if the notes
5210 have no meaning for INSN. */
5213 set_for_reg_notes (rtx insn)
5215 rtx pat, reg;
5217 if (!INSN_P (insn))
5218 return NULL_RTX;
5220 pat = PATTERN (insn);
5221 if (GET_CODE (pat) == PARALLEL)
5223 /* We do not use single_set because that ignores SETs of unused
5224 registers. REG_EQUAL and REG_EQUIV notes really do require the
5225 PARALLEL to have a single SET. */
5226 if (multiple_sets (insn))
5227 return NULL_RTX;
5228 pat = XVECEXP (pat, 0, 0);
5231 if (GET_CODE (pat) != SET)
5232 return NULL_RTX;
5234 reg = SET_DEST (pat);
5236 /* Notes apply to the contents of a STRICT_LOW_PART. */
5237 if (GET_CODE (reg) == STRICT_LOW_PART
5238 || GET_CODE (reg) == ZERO_EXTRACT)
5239 reg = XEXP (reg, 0);
5241 /* Check that we have a register. */
5242 if (!(REG_P (reg) || GET_CODE (reg) == SUBREG))
5243 return NULL_RTX;
5245 return pat;
5248 /* Place a note of KIND on insn INSN with DATUM as the datum. If a
5249 note of this type already exists, remove it first. */
5252 set_unique_reg_note (rtx insn, enum reg_note kind, rtx datum)
5254 rtx note = find_reg_note (insn, kind, NULL_RTX);
5256 switch (kind)
5258 case REG_EQUAL:
5259 case REG_EQUIV:
5260 /* We need to support the REG_EQUAL on USE trick of find_reloads. */
5261 if (!set_for_reg_notes (insn) && GET_CODE (PATTERN (insn)) != USE)
5262 return NULL_RTX;
5264 /* Don't add ASM_OPERAND REG_EQUAL/REG_EQUIV notes.
5265 It serves no useful purpose and breaks eliminate_regs. */
5266 if (GET_CODE (datum) == ASM_OPERANDS)
5267 return NULL_RTX;
5269 /* Notes with side effects are dangerous. Even if the side-effect
5270 initially mirrors one in PATTERN (INSN), later optimizations
5271 might alter the way that the final register value is calculated
5272 and so move or alter the side-effect in some way. The note would
5273 then no longer be a valid substitution for SET_SRC. */
5274 if (side_effects_p (datum))
5275 return NULL_RTX;
5276 break;
5278 default:
5279 break;
5282 if (note)
5283 XEXP (note, 0) = datum;
5284 else
5286 add_reg_note (insn, kind, datum);
5287 note = REG_NOTES (insn);
5290 switch (kind)
5292 case REG_EQUAL:
5293 case REG_EQUIV:
5294 df_notes_rescan (as_a <rtx_insn *> (insn));
5295 break;
5296 default:
5297 break;
5300 return note;
5303 /* Like set_unique_reg_note, but don't do anything unless INSN sets DST. */
5305 set_dst_reg_note (rtx insn, enum reg_note kind, rtx datum, rtx dst)
5307 rtx set = set_for_reg_notes (insn);
5309 if (set && SET_DEST (set) == dst)
5310 return set_unique_reg_note (insn, kind, datum);
5311 return NULL_RTX;
5314 /* Emit the rtl pattern X as an appropriate kind of insn. Also emit a
5315 following barrier if the instruction needs one and if ALLOW_BARRIER_P
5316 is true.
5318 If X is a label, it is simply added into the insn chain. */
5320 rtx_insn *
5321 emit (rtx x, bool allow_barrier_p)
5323 enum rtx_code code = classify_insn (x);
5325 switch (code)
5327 case CODE_LABEL:
5328 return emit_label (x);
5329 case INSN:
5330 return emit_insn (x);
5331 case JUMP_INSN:
5333 rtx_insn *insn = emit_jump_insn (x);
5334 if (allow_barrier_p
5335 && (any_uncondjump_p (insn) || GET_CODE (x) == RETURN))
5336 return emit_barrier ();
5337 return insn;
5339 case CALL_INSN:
5340 return emit_call_insn (x);
5341 case DEBUG_INSN:
5342 return emit_debug_insn (x);
5343 default:
5344 gcc_unreachable ();
5348 /* Space for free sequence stack entries. */
5349 static GTY ((deletable)) struct sequence_stack *free_sequence_stack;
5351 /* Begin emitting insns to a sequence. If this sequence will contain
5352 something that might cause the compiler to pop arguments to function
5353 calls (because those pops have previously been deferred; see
5354 INHIBIT_DEFER_POP for more details), use do_pending_stack_adjust
5355 before calling this function. That will ensure that the deferred
5356 pops are not accidentally emitted in the middle of this sequence. */
5358 void
5359 start_sequence (void)
5361 struct sequence_stack *tem;
5363 if (free_sequence_stack != NULL)
5365 tem = free_sequence_stack;
5366 free_sequence_stack = tem->next;
5368 else
5369 tem = ggc_alloc<sequence_stack> ();
5371 tem->next = get_current_sequence ()->next;
5372 tem->first = get_insns ();
5373 tem->last = get_last_insn ();
5374 get_current_sequence ()->next = tem;
5376 set_first_insn (0);
5377 set_last_insn (0);
5380 /* Set up the insn chain starting with FIRST as the current sequence,
5381 saving the previously current one. See the documentation for
5382 start_sequence for more information about how to use this function. */
5384 void
5385 push_to_sequence (rtx_insn *first)
5387 rtx_insn *last;
5389 start_sequence ();
5391 for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last))
5394 set_first_insn (first);
5395 set_last_insn (last);
5398 /* Like push_to_sequence, but take the last insn as an argument to avoid
5399 looping through the list. */
5401 void
5402 push_to_sequence2 (rtx_insn *first, rtx_insn *last)
5404 start_sequence ();
5406 set_first_insn (first);
5407 set_last_insn (last);
5410 /* Set up the outer-level insn chain
5411 as the current sequence, saving the previously current one. */
5413 void
5414 push_topmost_sequence (void)
5416 struct sequence_stack *top;
5418 start_sequence ();
5420 top = get_topmost_sequence ();
5421 set_first_insn (top->first);
5422 set_last_insn (top->last);
5425 /* After emitting to the outer-level insn chain, update the outer-level
5426 insn chain, and restore the previous saved state. */
5428 void
5429 pop_topmost_sequence (void)
5431 struct sequence_stack *top;
5433 top = get_topmost_sequence ();
5434 top->first = get_insns ();
5435 top->last = get_last_insn ();
5437 end_sequence ();
5440 /* After emitting to a sequence, restore previous saved state.
5442 To get the contents of the sequence just made, you must call
5443 `get_insns' *before* calling here.
5445 If the compiler might have deferred popping arguments while
5446 generating this sequence, and this sequence will not be immediately
5447 inserted into the instruction stream, use do_pending_stack_adjust
5448 before calling get_insns. That will ensure that the deferred
5449 pops are inserted into this sequence, and not into some random
5450 location in the instruction stream. See INHIBIT_DEFER_POP for more
5451 information about deferred popping of arguments. */
5453 void
5454 end_sequence (void)
5456 struct sequence_stack *tem = get_current_sequence ()->next;
5458 set_first_insn (tem->first);
5459 set_last_insn (tem->last);
5460 get_current_sequence ()->next = tem->next;
5462 memset (tem, 0, sizeof (*tem));
5463 tem->next = free_sequence_stack;
5464 free_sequence_stack = tem;
5467 /* Return 1 if currently emitting into a sequence. */
5470 in_sequence_p (void)
5472 return get_current_sequence ()->next != 0;
5475 /* Put the various virtual registers into REGNO_REG_RTX. */
5477 static void
5478 init_virtual_regs (void)
5480 regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
5481 regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
5482 regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
5483 regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
5484 regno_reg_rtx[VIRTUAL_CFA_REGNUM] = virtual_cfa_rtx;
5485 regno_reg_rtx[VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM]
5486 = virtual_preferred_stack_boundary_rtx;
5490 /* Used by copy_insn_1 to avoid copying SCRATCHes more than once. */
5491 static rtx copy_insn_scratch_in[MAX_RECOG_OPERANDS];
5492 static rtx copy_insn_scratch_out[MAX_RECOG_OPERANDS];
5493 static int copy_insn_n_scratches;
5495 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5496 copied an ASM_OPERANDS.
5497 In that case, it is the original input-operand vector. */
5498 static rtvec orig_asm_operands_vector;
5500 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5501 copied an ASM_OPERANDS.
5502 In that case, it is the copied input-operand vector. */
5503 static rtvec copy_asm_operands_vector;
5505 /* Likewise for the constraints vector. */
5506 static rtvec orig_asm_constraints_vector;
5507 static rtvec copy_asm_constraints_vector;
5509 /* Recursively create a new copy of an rtx for copy_insn.
5510 This function differs from copy_rtx in that it handles SCRATCHes and
5511 ASM_OPERANDs properly.
5512 Normally, this function is not used directly; use copy_insn as front end.
5513 However, you could first copy an insn pattern with copy_insn and then use
5514 this function afterwards to properly copy any REG_NOTEs containing
5515 SCRATCHes. */
5518 copy_insn_1 (rtx orig)
5520 rtx copy;
5521 int i, j;
5522 RTX_CODE code;
5523 const char *format_ptr;
5525 if (orig == NULL)
5526 return NULL;
5528 code = GET_CODE (orig);
5530 switch (code)
5532 case REG:
5533 case DEBUG_EXPR:
5534 CASE_CONST_ANY:
5535 case SYMBOL_REF:
5536 case CODE_LABEL:
5537 case PC:
5538 case CC0:
5539 case RETURN:
5540 case SIMPLE_RETURN:
5541 return orig;
5542 case CLOBBER:
5543 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
5544 clobbers or clobbers of hard registers that originated as pseudos.
5545 This is needed to allow safe register renaming. */
5546 if (REG_P (XEXP (orig, 0))
5547 && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0)))
5548 && HARD_REGISTER_NUM_P (ORIGINAL_REGNO (XEXP (orig, 0))))
5549 return orig;
5550 break;
5552 case SCRATCH:
5553 for (i = 0; i < copy_insn_n_scratches; i++)
5554 if (copy_insn_scratch_in[i] == orig)
5555 return copy_insn_scratch_out[i];
5556 break;
5558 case CONST:
5559 if (shared_const_p (orig))
5560 return orig;
5561 break;
5563 /* A MEM with a constant address is not sharable. The problem is that
5564 the constant address may need to be reloaded. If the mem is shared,
5565 then reloading one copy of this mem will cause all copies to appear
5566 to have been reloaded. */
5568 default:
5569 break;
5572 /* Copy the various flags, fields, and other information. We assume
5573 that all fields need copying, and then clear the fields that should
5574 not be copied. That is the sensible default behavior, and forces
5575 us to explicitly document why we are *not* copying a flag. */
5576 copy = shallow_copy_rtx (orig);
5578 /* We do not copy JUMP, CALL, or FRAME_RELATED for INSNs. */
5579 if (INSN_P (orig))
5581 RTX_FLAG (copy, jump) = 0;
5582 RTX_FLAG (copy, call) = 0;
5583 RTX_FLAG (copy, frame_related) = 0;
5586 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
5588 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
5589 switch (*format_ptr++)
5591 case 'e':
5592 if (XEXP (orig, i) != NULL)
5593 XEXP (copy, i) = copy_insn_1 (XEXP (orig, i));
5594 break;
5596 case 'E':
5597 case 'V':
5598 if (XVEC (orig, i) == orig_asm_constraints_vector)
5599 XVEC (copy, i) = copy_asm_constraints_vector;
5600 else if (XVEC (orig, i) == orig_asm_operands_vector)
5601 XVEC (copy, i) = copy_asm_operands_vector;
5602 else if (XVEC (orig, i) != NULL)
5604 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
5605 for (j = 0; j < XVECLEN (copy, i); j++)
5606 XVECEXP (copy, i, j) = copy_insn_1 (XVECEXP (orig, i, j));
5608 break;
5610 case 't':
5611 case 'w':
5612 case 'i':
5613 case 's':
5614 case 'S':
5615 case 'u':
5616 case '0':
5617 /* These are left unchanged. */
5618 break;
5620 default:
5621 gcc_unreachable ();
5624 if (code == SCRATCH)
5626 i = copy_insn_n_scratches++;
5627 gcc_assert (i < MAX_RECOG_OPERANDS);
5628 copy_insn_scratch_in[i] = orig;
5629 copy_insn_scratch_out[i] = copy;
5631 else if (code == ASM_OPERANDS)
5633 orig_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (orig);
5634 copy_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (copy);
5635 orig_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (orig);
5636 copy_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (copy);
5639 return copy;
5642 /* Create a new copy of an rtx.
5643 This function differs from copy_rtx in that it handles SCRATCHes and
5644 ASM_OPERANDs properly.
5645 INSN doesn't really have to be a full INSN; it could be just the
5646 pattern. */
5648 copy_insn (rtx insn)
5650 copy_insn_n_scratches = 0;
5651 orig_asm_operands_vector = 0;
5652 orig_asm_constraints_vector = 0;
5653 copy_asm_operands_vector = 0;
5654 copy_asm_constraints_vector = 0;
5655 return copy_insn_1 (insn);
5658 /* Return a copy of INSN that can be used in a SEQUENCE delay slot,
5659 on that assumption that INSN itself remains in its original place. */
5661 rtx_insn *
5662 copy_delay_slot_insn (rtx_insn *insn)
5664 /* Copy INSN with its rtx_code, all its notes, location etc. */
5665 insn = as_a <rtx_insn *> (copy_rtx (insn));
5666 INSN_UID (insn) = cur_insn_uid++;
5667 return insn;
5670 /* Initialize data structures and variables in this file
5671 before generating rtl for each function. */
5673 void
5674 init_emit (void)
5676 set_first_insn (NULL);
5677 set_last_insn (NULL);
5678 if (MIN_NONDEBUG_INSN_UID)
5679 cur_insn_uid = MIN_NONDEBUG_INSN_UID;
5680 else
5681 cur_insn_uid = 1;
5682 cur_debug_insn_uid = 1;
5683 reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
5684 first_label_num = label_num;
5685 get_current_sequence ()->next = NULL;
5687 /* Init the tables that describe all the pseudo regs. */
5689 crtl->emit.regno_pointer_align_length = LAST_VIRTUAL_REGISTER + 101;
5691 crtl->emit.regno_pointer_align
5692 = XCNEWVEC (unsigned char, crtl->emit.regno_pointer_align_length);
5694 regno_reg_rtx
5695 = ggc_cleared_vec_alloc<rtx> (crtl->emit.regno_pointer_align_length);
5697 /* Put copies of all the hard registers into regno_reg_rtx. */
5698 memcpy (regno_reg_rtx,
5699 initial_regno_reg_rtx,
5700 FIRST_PSEUDO_REGISTER * sizeof (rtx));
5702 /* Put copies of all the virtual register rtx into regno_reg_rtx. */
5703 init_virtual_regs ();
5705 /* Indicate that the virtual registers and stack locations are
5706 all pointers. */
5707 REG_POINTER (stack_pointer_rtx) = 1;
5708 REG_POINTER (frame_pointer_rtx) = 1;
5709 REG_POINTER (hard_frame_pointer_rtx) = 1;
5710 REG_POINTER (arg_pointer_rtx) = 1;
5712 REG_POINTER (virtual_incoming_args_rtx) = 1;
5713 REG_POINTER (virtual_stack_vars_rtx) = 1;
5714 REG_POINTER (virtual_stack_dynamic_rtx) = 1;
5715 REG_POINTER (virtual_outgoing_args_rtx) = 1;
5716 REG_POINTER (virtual_cfa_rtx) = 1;
5718 #ifdef STACK_BOUNDARY
5719 REGNO_POINTER_ALIGN (STACK_POINTER_REGNUM) = STACK_BOUNDARY;
5720 REGNO_POINTER_ALIGN (FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5721 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5722 REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) = STACK_BOUNDARY;
5724 /* ??? These are problematic (for example, 3 out of 4 are wrong on
5725 32-bit SPARC and cannot be all fixed because of the ABI). */
5726 REGNO_POINTER_ALIGN (VIRTUAL_INCOMING_ARGS_REGNUM) = STACK_BOUNDARY;
5727 REGNO_POINTER_ALIGN (VIRTUAL_STACK_VARS_REGNUM) = STACK_BOUNDARY;
5728 REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM) = STACK_BOUNDARY;
5729 REGNO_POINTER_ALIGN (VIRTUAL_OUTGOING_ARGS_REGNUM) = STACK_BOUNDARY;
5731 REGNO_POINTER_ALIGN (VIRTUAL_CFA_REGNUM) = BITS_PER_WORD;
5732 #endif
5734 #ifdef INIT_EXPANDERS
5735 INIT_EXPANDERS;
5736 #endif
5739 /* Generate a vector constant for mode MODE and constant value CONSTANT. */
5741 static rtx
5742 gen_const_vector (machine_mode mode, int constant)
5744 rtx tem;
5745 rtvec v;
5746 int units, i;
5747 machine_mode inner;
5749 units = GET_MODE_NUNITS (mode);
5750 inner = GET_MODE_INNER (mode);
5752 gcc_assert (!DECIMAL_FLOAT_MODE_P (inner));
5754 v = rtvec_alloc (units);
5756 /* We need to call this function after we set the scalar const_tiny_rtx
5757 entries. */
5758 gcc_assert (const_tiny_rtx[constant][(int) inner]);
5760 for (i = 0; i < units; ++i)
5761 RTVEC_ELT (v, i) = const_tiny_rtx[constant][(int) inner];
5763 tem = gen_rtx_raw_CONST_VECTOR (mode, v);
5764 return tem;
5767 /* Generate a vector like gen_rtx_raw_CONST_VEC, but use the zero vector when
5768 all elements are zero, and the one vector when all elements are one. */
5770 gen_rtx_CONST_VECTOR (machine_mode mode, rtvec v)
5772 machine_mode inner = GET_MODE_INNER (mode);
5773 int nunits = GET_MODE_NUNITS (mode);
5774 rtx x;
5775 int i;
5777 /* Check to see if all of the elements have the same value. */
5778 x = RTVEC_ELT (v, nunits - 1);
5779 for (i = nunits - 2; i >= 0; i--)
5780 if (RTVEC_ELT (v, i) != x)
5781 break;
5783 /* If the values are all the same, check to see if we can use one of the
5784 standard constant vectors. */
5785 if (i == -1)
5787 if (x == CONST0_RTX (inner))
5788 return CONST0_RTX (mode);
5789 else if (x == CONST1_RTX (inner))
5790 return CONST1_RTX (mode);
5791 else if (x == CONSTM1_RTX (inner))
5792 return CONSTM1_RTX (mode);
5795 return gen_rtx_raw_CONST_VECTOR (mode, v);
5798 /* Initialise global register information required by all functions. */
5800 void
5801 init_emit_regs (void)
5803 int i;
5804 machine_mode mode;
5805 mem_attrs *attrs;
5807 /* Reset register attributes */
5808 reg_attrs_htab->empty ();
5810 /* We need reg_raw_mode, so initialize the modes now. */
5811 init_reg_modes_target ();
5813 /* Assign register numbers to the globally defined register rtx. */
5814 stack_pointer_rtx = gen_raw_REG (Pmode, STACK_POINTER_REGNUM);
5815 frame_pointer_rtx = gen_raw_REG (Pmode, FRAME_POINTER_REGNUM);
5816 hard_frame_pointer_rtx = gen_raw_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
5817 arg_pointer_rtx = gen_raw_REG (Pmode, ARG_POINTER_REGNUM);
5818 virtual_incoming_args_rtx =
5819 gen_raw_REG (Pmode, VIRTUAL_INCOMING_ARGS_REGNUM);
5820 virtual_stack_vars_rtx =
5821 gen_raw_REG (Pmode, VIRTUAL_STACK_VARS_REGNUM);
5822 virtual_stack_dynamic_rtx =
5823 gen_raw_REG (Pmode, VIRTUAL_STACK_DYNAMIC_REGNUM);
5824 virtual_outgoing_args_rtx =
5825 gen_raw_REG (Pmode, VIRTUAL_OUTGOING_ARGS_REGNUM);
5826 virtual_cfa_rtx = gen_raw_REG (Pmode, VIRTUAL_CFA_REGNUM);
5827 virtual_preferred_stack_boundary_rtx =
5828 gen_raw_REG (Pmode, VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM);
5830 /* Initialize RTL for commonly used hard registers. These are
5831 copied into regno_reg_rtx as we begin to compile each function. */
5832 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5833 initial_regno_reg_rtx[i] = gen_raw_REG (reg_raw_mode[i], i);
5835 #ifdef RETURN_ADDRESS_POINTER_REGNUM
5836 return_address_pointer_rtx
5837 = gen_raw_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM);
5838 #endif
5840 pic_offset_table_rtx = NULL_RTX;
5841 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
5842 pic_offset_table_rtx = gen_raw_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
5844 for (i = 0; i < (int) MAX_MACHINE_MODE; i++)
5846 mode = (machine_mode) i;
5847 attrs = ggc_cleared_alloc<mem_attrs> ();
5848 attrs->align = BITS_PER_UNIT;
5849 attrs->addrspace = ADDR_SPACE_GENERIC;
5850 if (mode != BLKmode)
5852 attrs->size_known_p = true;
5853 attrs->size = GET_MODE_SIZE (mode);
5854 if (STRICT_ALIGNMENT)
5855 attrs->align = GET_MODE_ALIGNMENT (mode);
5857 mode_mem_attrs[i] = attrs;
5861 /* Initialize global machine_mode variables. */
5863 void
5864 init_derived_machine_modes (void)
5866 opt_scalar_int_mode mode_iter, opt_byte_mode, opt_word_mode;
5867 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
5869 scalar_int_mode mode = mode_iter.require ();
5871 if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
5872 && !opt_byte_mode.exists ())
5873 opt_byte_mode = mode;
5875 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
5876 && !opt_word_mode.exists ())
5877 opt_word_mode = mode;
5880 byte_mode = opt_byte_mode.require ();
5881 word_mode = opt_word_mode.require ();
5882 ptr_mode = as_a <scalar_int_mode> (mode_for_size (POINTER_SIZE,
5883 MODE_INT, 0));
5886 /* Create some permanent unique rtl objects shared between all functions. */
5888 void
5889 init_emit_once (void)
5891 int i;
5892 machine_mode mode;
5893 scalar_float_mode double_mode;
5895 /* Initialize the CONST_INT, CONST_WIDE_INT, CONST_DOUBLE,
5896 CONST_FIXED, and memory attribute hash tables. */
5897 const_int_htab = hash_table<const_int_hasher>::create_ggc (37);
5899 #if TARGET_SUPPORTS_WIDE_INT
5900 const_wide_int_htab = hash_table<const_wide_int_hasher>::create_ggc (37);
5901 #endif
5902 const_double_htab = hash_table<const_double_hasher>::create_ggc (37);
5904 const_fixed_htab = hash_table<const_fixed_hasher>::create_ggc (37);
5906 reg_attrs_htab = hash_table<reg_attr_hasher>::create_ggc (37);
5908 #ifdef INIT_EXPANDERS
5909 /* This is to initialize {init|mark|free}_machine_status before the first
5910 call to push_function_context_to. This is needed by the Chill front
5911 end which calls push_function_context_to before the first call to
5912 init_function_start. */
5913 INIT_EXPANDERS;
5914 #endif
5916 /* Create the unique rtx's for certain rtx codes and operand values. */
5918 /* Process stack-limiting command-line options. */
5919 if (opt_fstack_limit_symbol_arg != NULL)
5920 stack_limit_rtx
5921 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (opt_fstack_limit_symbol_arg));
5922 if (opt_fstack_limit_register_no >= 0)
5923 stack_limit_rtx = gen_rtx_REG (Pmode, opt_fstack_limit_register_no);
5925 /* Don't use gen_rtx_CONST_INT here since gen_rtx_CONST_INT in this case
5926 tries to use these variables. */
5927 for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
5928 const_int_rtx[i + MAX_SAVED_CONST_INT] =
5929 gen_rtx_raw_CONST_INT (VOIDmode, (HOST_WIDE_INT) i);
5931 if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT
5932 && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT)
5933 const_true_rtx = const_int_rtx[STORE_FLAG_VALUE + MAX_SAVED_CONST_INT];
5934 else
5935 const_true_rtx = gen_rtx_CONST_INT (VOIDmode, STORE_FLAG_VALUE);
5937 double_mode = float_mode_for_size (DOUBLE_TYPE_SIZE).require ();
5939 real_from_integer (&dconst0, double_mode, 0, SIGNED);
5940 real_from_integer (&dconst1, double_mode, 1, SIGNED);
5941 real_from_integer (&dconst2, double_mode, 2, SIGNED);
5943 dconstm1 = dconst1;
5944 dconstm1.sign = 1;
5946 dconsthalf = dconst1;
5947 SET_REAL_EXP (&dconsthalf, REAL_EXP (&dconsthalf) - 1);
5949 for (i = 0; i < 3; i++)
5951 const REAL_VALUE_TYPE *const r =
5952 (i == 0 ? &dconst0 : i == 1 ? &dconst1 : &dconst2);
5954 FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT)
5955 const_tiny_rtx[i][(int) mode] =
5956 const_double_from_real_value (*r, mode);
5958 FOR_EACH_MODE_IN_CLASS (mode, MODE_DECIMAL_FLOAT)
5959 const_tiny_rtx[i][(int) mode] =
5960 const_double_from_real_value (*r, mode);
5962 const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
5964 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
5965 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5967 for (mode = MIN_MODE_PARTIAL_INT;
5968 mode <= MAX_MODE_PARTIAL_INT;
5969 mode = (machine_mode)((int)(mode) + 1))
5970 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5973 const_tiny_rtx[3][(int) VOIDmode] = constm1_rtx;
5975 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
5976 const_tiny_rtx[3][(int) mode] = constm1_rtx;
5978 for (mode = MIN_MODE_PARTIAL_INT;
5979 mode <= MAX_MODE_PARTIAL_INT;
5980 mode = (machine_mode)((int)(mode) + 1))
5981 const_tiny_rtx[3][(int) mode] = constm1_rtx;
5983 FOR_EACH_MODE_IN_CLASS (mode, MODE_COMPLEX_INT)
5985 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
5986 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
5989 FOR_EACH_MODE_IN_CLASS (mode, MODE_COMPLEX_FLOAT)
5991 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
5992 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
5995 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
5997 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5998 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
5999 const_tiny_rtx[3][(int) mode] = gen_const_vector (mode, 3);
6002 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_FLOAT)
6004 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6005 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6008 FOR_EACH_MODE_IN_CLASS (mode, MODE_FRACT)
6010 FCONST0 (mode).data.high = 0;
6011 FCONST0 (mode).data.low = 0;
6012 FCONST0 (mode).mode = mode;
6013 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6014 FCONST0 (mode), mode);
6017 FOR_EACH_MODE_IN_CLASS (mode, MODE_UFRACT)
6019 FCONST0 (mode).data.high = 0;
6020 FCONST0 (mode).data.low = 0;
6021 FCONST0 (mode).mode = mode;
6022 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6023 FCONST0 (mode), mode);
6026 FOR_EACH_MODE_IN_CLASS (mode, MODE_ACCUM)
6028 FCONST0 (mode).data.high = 0;
6029 FCONST0 (mode).data.low = 0;
6030 FCONST0 (mode).mode = mode;
6031 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6032 FCONST0 (mode), mode);
6034 /* We store the value 1. */
6035 FCONST1 (mode).data.high = 0;
6036 FCONST1 (mode).data.low = 0;
6037 FCONST1 (mode).mode = mode;
6038 FCONST1 (mode).data
6039 = double_int_one.lshift (GET_MODE_FBIT (mode),
6040 HOST_BITS_PER_DOUBLE_INT,
6041 SIGNED_FIXED_POINT_MODE_P (mode));
6042 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6043 FCONST1 (mode), mode);
6046 FOR_EACH_MODE_IN_CLASS (mode, MODE_UACCUM)
6048 FCONST0 (mode).data.high = 0;
6049 FCONST0 (mode).data.low = 0;
6050 FCONST0 (mode).mode = mode;
6051 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6052 FCONST0 (mode), mode);
6054 /* We store the value 1. */
6055 FCONST1 (mode).data.high = 0;
6056 FCONST1 (mode).data.low = 0;
6057 FCONST1 (mode).mode = mode;
6058 FCONST1 (mode).data
6059 = double_int_one.lshift (GET_MODE_FBIT (mode),
6060 HOST_BITS_PER_DOUBLE_INT,
6061 SIGNED_FIXED_POINT_MODE_P (mode));
6062 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6063 FCONST1 (mode), mode);
6066 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_FRACT)
6068 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6071 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_UFRACT)
6073 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6076 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_ACCUM)
6078 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6079 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6082 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_UACCUM)
6084 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6085 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6088 for (i = (int) CCmode; i < (int) MAX_MACHINE_MODE; ++i)
6089 if (GET_MODE_CLASS ((machine_mode) i) == MODE_CC)
6090 const_tiny_rtx[0][i] = const0_rtx;
6092 const_tiny_rtx[0][(int) BImode] = const0_rtx;
6093 if (STORE_FLAG_VALUE == 1)
6094 const_tiny_rtx[1][(int) BImode] = const1_rtx;
6096 FOR_EACH_MODE_IN_CLASS (mode, MODE_POINTER_BOUNDS)
6098 wide_int wi_zero = wi::zero (GET_MODE_PRECISION (mode));
6099 const_tiny_rtx[0][mode] = immed_wide_int_const (wi_zero, mode);
6102 pc_rtx = gen_rtx_fmt_ (PC, VOIDmode);
6103 ret_rtx = gen_rtx_fmt_ (RETURN, VOIDmode);
6104 simple_return_rtx = gen_rtx_fmt_ (SIMPLE_RETURN, VOIDmode);
6105 cc0_rtx = gen_rtx_fmt_ (CC0, VOIDmode);
6106 invalid_insn_rtx = gen_rtx_INSN (VOIDmode,
6107 /*prev_insn=*/NULL,
6108 /*next_insn=*/NULL,
6109 /*bb=*/NULL,
6110 /*pattern=*/NULL_RTX,
6111 /*location=*/-1,
6112 CODE_FOR_nothing,
6113 /*reg_notes=*/NULL_RTX);
6116 /* Produce exact duplicate of insn INSN after AFTER.
6117 Care updating of libcall regions if present. */
6119 rtx_insn *
6120 emit_copy_of_insn_after (rtx_insn *insn, rtx_insn *after)
6122 rtx_insn *new_rtx;
6123 rtx link;
6125 switch (GET_CODE (insn))
6127 case INSN:
6128 new_rtx = emit_insn_after (copy_insn (PATTERN (insn)), after);
6129 break;
6131 case JUMP_INSN:
6132 new_rtx = emit_jump_insn_after (copy_insn (PATTERN (insn)), after);
6133 CROSSING_JUMP_P (new_rtx) = CROSSING_JUMP_P (insn);
6134 break;
6136 case DEBUG_INSN:
6137 new_rtx = emit_debug_insn_after (copy_insn (PATTERN (insn)), after);
6138 break;
6140 case CALL_INSN:
6141 new_rtx = emit_call_insn_after (copy_insn (PATTERN (insn)), after);
6142 if (CALL_INSN_FUNCTION_USAGE (insn))
6143 CALL_INSN_FUNCTION_USAGE (new_rtx)
6144 = copy_insn (CALL_INSN_FUNCTION_USAGE (insn));
6145 SIBLING_CALL_P (new_rtx) = SIBLING_CALL_P (insn);
6146 RTL_CONST_CALL_P (new_rtx) = RTL_CONST_CALL_P (insn);
6147 RTL_PURE_CALL_P (new_rtx) = RTL_PURE_CALL_P (insn);
6148 RTL_LOOPING_CONST_OR_PURE_CALL_P (new_rtx)
6149 = RTL_LOOPING_CONST_OR_PURE_CALL_P (insn);
6150 break;
6152 default:
6153 gcc_unreachable ();
6156 /* Update LABEL_NUSES. */
6157 mark_jump_label (PATTERN (new_rtx), new_rtx, 0);
6159 INSN_LOCATION (new_rtx) = INSN_LOCATION (insn);
6161 /* If the old insn is frame related, then so is the new one. This is
6162 primarily needed for IA-64 unwind info which marks epilogue insns,
6163 which may be duplicated by the basic block reordering code. */
6164 RTX_FRAME_RELATED_P (new_rtx) = RTX_FRAME_RELATED_P (insn);
6166 /* Locate the end of existing REG_NOTES in NEW_RTX. */
6167 rtx *ptail = &REG_NOTES (new_rtx);
6168 while (*ptail != NULL_RTX)
6169 ptail = &XEXP (*ptail, 1);
6171 /* Copy all REG_NOTES except REG_LABEL_OPERAND since mark_jump_label
6172 will make them. REG_LABEL_TARGETs are created there too, but are
6173 supposed to be sticky, so we copy them. */
6174 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
6175 if (REG_NOTE_KIND (link) != REG_LABEL_OPERAND)
6177 *ptail = duplicate_reg_note (link);
6178 ptail = &XEXP (*ptail, 1);
6181 INSN_CODE (new_rtx) = INSN_CODE (insn);
6182 return new_rtx;
6185 static GTY((deletable)) rtx hard_reg_clobbers [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
6187 gen_hard_reg_clobber (machine_mode mode, unsigned int regno)
6189 if (hard_reg_clobbers[mode][regno])
6190 return hard_reg_clobbers[mode][regno];
6191 else
6192 return (hard_reg_clobbers[mode][regno] =
6193 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (mode, regno)));
6196 location_t prologue_location;
6197 location_t epilogue_location;
6199 /* Hold current location information and last location information, so the
6200 datastructures are built lazily only when some instructions in given
6201 place are needed. */
6202 static location_t curr_location;
6204 /* Allocate insn location datastructure. */
6205 void
6206 insn_locations_init (void)
6208 prologue_location = epilogue_location = 0;
6209 curr_location = UNKNOWN_LOCATION;
6212 /* At the end of emit stage, clear current location. */
6213 void
6214 insn_locations_finalize (void)
6216 epilogue_location = curr_location;
6217 curr_location = UNKNOWN_LOCATION;
6220 /* Set current location. */
6221 void
6222 set_curr_insn_location (location_t location)
6224 curr_location = location;
6227 /* Get current location. */
6228 location_t
6229 curr_insn_location (void)
6231 return curr_location;
6234 /* Return lexical scope block insn belongs to. */
6235 tree
6236 insn_scope (const rtx_insn *insn)
6238 return LOCATION_BLOCK (INSN_LOCATION (insn));
6241 /* Return line number of the statement that produced this insn. */
6243 insn_line (const rtx_insn *insn)
6245 return LOCATION_LINE (INSN_LOCATION (insn));
6248 /* Return source file of the statement that produced this insn. */
6249 const char *
6250 insn_file (const rtx_insn *insn)
6252 return LOCATION_FILE (INSN_LOCATION (insn));
6255 /* Return expanded location of the statement that produced this insn. */
6256 expanded_location
6257 insn_location (const rtx_insn *insn)
6259 return expand_location (INSN_LOCATION (insn));
6262 /* Return true if memory model MODEL requires a pre-operation (release-style)
6263 barrier or a post-operation (acquire-style) barrier. While not universal,
6264 this function matches behavior of several targets. */
6266 bool
6267 need_atomic_barrier_p (enum memmodel model, bool pre)
6269 switch (model & MEMMODEL_BASE_MASK)
6271 case MEMMODEL_RELAXED:
6272 case MEMMODEL_CONSUME:
6273 return false;
6274 case MEMMODEL_RELEASE:
6275 return pre;
6276 case MEMMODEL_ACQUIRE:
6277 return !pre;
6278 case MEMMODEL_ACQ_REL:
6279 case MEMMODEL_SEQ_CST:
6280 return true;
6281 default:
6282 gcc_unreachable ();
6286 /* Initialize fields of rtl_data related to stack alignment. */
6288 void
6289 rtl_data::init_stack_alignment ()
6291 stack_alignment_needed = STACK_BOUNDARY;
6292 max_used_stack_slot_alignment = STACK_BOUNDARY;
6293 stack_alignment_estimated = 0;
6294 preferred_stack_boundary = STACK_BOUNDARY;
6298 #include "gt-emit-rtl.h"