2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>
[official-gcc.git] / gcc / emit-rtl.c
blobfa5e41beb463ae04c707ae4dadd2c0199828de84
1 /* Emit RTL for the GCC expander.
2 Copyright (C) 1987-2014 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 "tm.h"
38 #include "diagnostic-core.h"
39 #include "rtl.h"
40 #include "tree.h"
41 #include "varasm.h"
42 #include "predict.h"
43 #include "vec.h"
44 #include "hashtab.h"
45 #include "hash-set.h"
46 #include "machmode.h"
47 #include "hard-reg-set.h"
48 #include "input.h"
49 #include "function.h"
50 #include "cfgrtl.h"
51 #include "basic-block.h"
52 #include "tree-eh.h"
53 #include "tm_p.h"
54 #include "flags.h"
55 #include "stringpool.h"
56 #include "expr.h"
57 #include "regs.h"
58 #include "insn-config.h"
59 #include "recog.h"
60 #include "bitmap.h"
61 #include "debug.h"
62 #include "langhooks.h"
63 #include "df.h"
64 #include "params.h"
65 #include "target.h"
66 #include "builtins.h"
67 #include "rtl-iter.h"
69 struct target_rtl default_target_rtl;
70 #if SWITCHABLE_TARGET
71 struct target_rtl *this_target_rtl = &default_target_rtl;
72 #endif
74 #define initial_regno_reg_rtx (this_target_rtl->x_initial_regno_reg_rtx)
76 /* Commonly used modes. */
78 machine_mode byte_mode; /* Mode whose width is BITS_PER_UNIT. */
79 machine_mode word_mode; /* Mode whose width is BITS_PER_WORD. */
80 machine_mode double_mode; /* Mode whose width is DOUBLE_TYPE_SIZE. */
81 machine_mode ptr_mode; /* Mode whose width is POINTER_SIZE. */
83 /* Datastructures maintained for currently processed function in RTL form. */
85 struct rtl_data x_rtl;
87 /* Indexed by pseudo register number, gives the rtx for that pseudo.
88 Allocated in parallel with regno_pointer_align.
89 FIXME: We could put it into emit_status struct, but gengtype is not able to deal
90 with length attribute nested in top level structures. */
92 rtx * regno_reg_rtx;
94 /* This is *not* reset after each function. It gives each CODE_LABEL
95 in the entire compilation a unique label number. */
97 static GTY(()) int label_num = 1;
99 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
100 the values of 0, 1, and 2. For the integer entries and VOIDmode, we
101 record a copy of const[012]_rtx and constm1_rtx. CONSTM1_RTX
102 is set only for MODE_INT and MODE_VECTOR_INT modes. */
104 rtx const_tiny_rtx[4][(int) MAX_MACHINE_MODE];
106 rtx const_true_rtx;
108 REAL_VALUE_TYPE dconst0;
109 REAL_VALUE_TYPE dconst1;
110 REAL_VALUE_TYPE dconst2;
111 REAL_VALUE_TYPE dconstm1;
112 REAL_VALUE_TYPE dconsthalf;
114 /* Record fixed-point constant 0 and 1. */
115 FIXED_VALUE_TYPE fconst0[MAX_FCONST0];
116 FIXED_VALUE_TYPE fconst1[MAX_FCONST1];
118 /* We make one copy of (const_int C) where C is in
119 [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
120 to save space during the compilation and simplify comparisons of
121 integers. */
123 rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
125 /* Standard pieces of rtx, to be substituted directly into things. */
126 rtx pc_rtx;
127 rtx ret_rtx;
128 rtx simple_return_rtx;
129 rtx cc0_rtx;
131 /* A hash table storing CONST_INTs whose absolute value is greater
132 than MAX_SAVED_CONST_INT. */
134 struct const_int_hasher : ggc_cache_hasher<rtx>
136 typedef HOST_WIDE_INT compare_type;
138 static hashval_t hash (rtx i);
139 static bool equal (rtx i, HOST_WIDE_INT h);
142 static GTY ((cache)) hash_table<const_int_hasher> *const_int_htab;
144 struct const_wide_int_hasher : ggc_cache_hasher<rtx>
146 static hashval_t hash (rtx x);
147 static bool equal (rtx x, rtx y);
150 static GTY ((cache)) hash_table<const_wide_int_hasher> *const_wide_int_htab;
152 /* A hash table storing register attribute structures. */
153 struct reg_attr_hasher : ggc_cache_hasher<reg_attrs *>
155 static hashval_t hash (reg_attrs *x);
156 static bool equal (reg_attrs *a, reg_attrs *b);
159 static GTY ((cache)) hash_table<reg_attr_hasher> *reg_attrs_htab;
161 /* A hash table storing all CONST_DOUBLEs. */
162 struct const_double_hasher : ggc_cache_hasher<rtx>
164 static hashval_t hash (rtx x);
165 static bool equal (rtx x, rtx y);
168 static GTY ((cache)) hash_table<const_double_hasher> *const_double_htab;
170 /* A hash table storing all CONST_FIXEDs. */
171 struct const_fixed_hasher : ggc_cache_hasher<rtx>
173 static hashval_t hash (rtx x);
174 static bool equal (rtx x, rtx y);
177 static GTY ((cache)) hash_table<const_fixed_hasher> *const_fixed_htab;
179 #define cur_insn_uid (crtl->emit.x_cur_insn_uid)
180 #define cur_debug_insn_uid (crtl->emit.x_cur_debug_insn_uid)
181 #define first_label_num (crtl->emit.x_first_label_num)
183 static void set_used_decls (tree);
184 static void mark_label_nuses (rtx);
185 #if TARGET_SUPPORTS_WIDE_INT
186 static rtx lookup_const_wide_int (rtx);
187 #endif
188 static rtx lookup_const_double (rtx);
189 static rtx lookup_const_fixed (rtx);
190 static reg_attrs *get_reg_attrs (tree, int);
191 static rtx gen_const_vector (machine_mode, int);
192 static void copy_rtx_if_shared_1 (rtx *orig);
194 /* Probability of the conditional branch currently proceeded by try_split.
195 Set to -1 otherwise. */
196 int split_branch_probability = -1;
198 /* Returns a hash code for X (which is a really a CONST_INT). */
200 hashval_t
201 const_int_hasher::hash (rtx x)
203 return (hashval_t) INTVAL (x);
206 /* Returns nonzero if the value represented by X (which is really a
207 CONST_INT) is the same as that given by Y (which is really a
208 HOST_WIDE_INT *). */
210 bool
211 const_int_hasher::equal (rtx x, HOST_WIDE_INT y)
213 return (INTVAL (x) == y);
216 #if TARGET_SUPPORTS_WIDE_INT
217 /* Returns a hash code for X (which is a really a CONST_WIDE_INT). */
219 hashval_t
220 const_wide_int_hasher::hash (rtx x)
222 int i;
223 unsigned HOST_WIDE_INT hash = 0;
224 const_rtx xr = x;
226 for (i = 0; i < CONST_WIDE_INT_NUNITS (xr); i++)
227 hash += CONST_WIDE_INT_ELT (xr, i);
229 return (hashval_t) hash;
232 /* Returns nonzero if the value represented by X (which is really a
233 CONST_WIDE_INT) is the same as that given by Y (which is really a
234 CONST_WIDE_INT). */
236 bool
237 const_wide_int_hasher::equal (rtx x, rtx y)
239 int i;
240 const_rtx xr = x;
241 const_rtx yr = y;
242 if (CONST_WIDE_INT_NUNITS (xr) != CONST_WIDE_INT_NUNITS (yr))
243 return false;
245 for (i = 0; i < CONST_WIDE_INT_NUNITS (xr); i++)
246 if (CONST_WIDE_INT_ELT (xr, i) != CONST_WIDE_INT_ELT (yr, i))
247 return false;
249 return true;
251 #endif
253 /* Returns a hash code for X (which is really a CONST_DOUBLE). */
254 hashval_t
255 const_double_hasher::hash (rtx x)
257 const_rtx const value = x;
258 hashval_t h;
260 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (value) == VOIDmode)
261 h = CONST_DOUBLE_LOW (value) ^ CONST_DOUBLE_HIGH (value);
262 else
264 h = real_hash (CONST_DOUBLE_REAL_VALUE (value));
265 /* MODE is used in the comparison, so it should be in the hash. */
266 h ^= GET_MODE (value);
268 return h;
271 /* Returns nonzero if the value represented by X (really a ...)
272 is the same as that represented by Y (really a ...) */
273 bool
274 const_double_hasher::equal (rtx x, rtx y)
276 const_rtx const a = x, b = y;
278 if (GET_MODE (a) != GET_MODE (b))
279 return 0;
280 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (a) == VOIDmode)
281 return (CONST_DOUBLE_LOW (a) == CONST_DOUBLE_LOW (b)
282 && CONST_DOUBLE_HIGH (a) == CONST_DOUBLE_HIGH (b));
283 else
284 return real_identical (CONST_DOUBLE_REAL_VALUE (a),
285 CONST_DOUBLE_REAL_VALUE (b));
288 /* Returns a hash code for X (which is really a CONST_FIXED). */
290 hashval_t
291 const_fixed_hasher::hash (rtx x)
293 const_rtx const value = x;
294 hashval_t h;
296 h = fixed_hash (CONST_FIXED_VALUE (value));
297 /* MODE is used in the comparison, so it should be in the hash. */
298 h ^= GET_MODE (value);
299 return h;
302 /* Returns nonzero if the value represented by X is the same as that
303 represented by Y. */
305 bool
306 const_fixed_hasher::equal (rtx x, rtx y)
308 const_rtx const a = x, b = y;
310 if (GET_MODE (a) != GET_MODE (b))
311 return 0;
312 return fixed_identical (CONST_FIXED_VALUE (a), CONST_FIXED_VALUE (b));
315 /* Return true if the given memory attributes are equal. */
317 bool
318 mem_attrs_eq_p (const struct mem_attrs *p, const struct mem_attrs *q)
320 if (p == q)
321 return true;
322 if (!p || !q)
323 return false;
324 return (p->alias == q->alias
325 && p->offset_known_p == q->offset_known_p
326 && (!p->offset_known_p || p->offset == q->offset)
327 && p->size_known_p == q->size_known_p
328 && (!p->size_known_p || p->size == q->size)
329 && p->align == q->align
330 && p->addrspace == q->addrspace
331 && (p->expr == q->expr
332 || (p->expr != NULL_TREE && q->expr != NULL_TREE
333 && operand_equal_p (p->expr, q->expr, 0))));
336 /* Set MEM's memory attributes so that they are the same as ATTRS. */
338 static void
339 set_mem_attrs (rtx mem, mem_attrs *attrs)
341 /* If everything is the default, we can just clear the attributes. */
342 if (mem_attrs_eq_p (attrs, mode_mem_attrs[(int) GET_MODE (mem)]))
344 MEM_ATTRS (mem) = 0;
345 return;
348 if (!MEM_ATTRS (mem)
349 || !mem_attrs_eq_p (attrs, MEM_ATTRS (mem)))
351 MEM_ATTRS (mem) = ggc_alloc<mem_attrs> ();
352 memcpy (MEM_ATTRS (mem), attrs, sizeof (mem_attrs));
356 /* Returns a hash code for X (which is a really a reg_attrs *). */
358 hashval_t
359 reg_attr_hasher::hash (reg_attrs *x)
361 const reg_attrs *const p = x;
363 return ((p->offset * 1000) ^ (intptr_t) p->decl);
366 /* Returns nonzero if the value represented by X is the same as that given by
367 Y. */
369 bool
370 reg_attr_hasher::equal (reg_attrs *x, reg_attrs *y)
372 const reg_attrs *const p = x;
373 const reg_attrs *const q = y;
375 return (p->decl == q->decl && p->offset == q->offset);
377 /* Allocate a new reg_attrs structure and insert it into the hash table if
378 one identical to it is not already in the table. We are doing this for
379 MEM of mode MODE. */
381 static reg_attrs *
382 get_reg_attrs (tree decl, int offset)
384 reg_attrs attrs;
386 /* If everything is the default, we can just return zero. */
387 if (decl == 0 && offset == 0)
388 return 0;
390 attrs.decl = decl;
391 attrs.offset = offset;
393 reg_attrs **slot = reg_attrs_htab->find_slot (&attrs, INSERT);
394 if (*slot == 0)
396 *slot = ggc_alloc<reg_attrs> ();
397 memcpy (*slot, &attrs, sizeof (reg_attrs));
400 return *slot;
404 #if !HAVE_blockage
405 /* Generate an empty ASM_INPUT, which is used to block attempts to schedule,
406 and to block register equivalences to be seen across this insn. */
409 gen_blockage (void)
411 rtx x = gen_rtx_ASM_INPUT (VOIDmode, "");
412 MEM_VOLATILE_P (x) = true;
413 return x;
415 #endif
418 /* Generate a new REG rtx. Make sure ORIGINAL_REGNO is set properly, and
419 don't attempt to share with the various global pieces of rtl (such as
420 frame_pointer_rtx). */
423 gen_raw_REG (machine_mode mode, int regno)
425 rtx x = gen_rtx_raw_REG (mode, regno);
426 ORIGINAL_REGNO (x) = regno;
427 return x;
430 /* There are some RTL codes that require special attention; the generation
431 functions do the raw handling. If you add to this list, modify
432 special_rtx in gengenrtl.c as well. */
434 rtx_expr_list *
435 gen_rtx_EXPR_LIST (machine_mode mode, rtx expr, rtx expr_list)
437 return as_a <rtx_expr_list *> (gen_rtx_fmt_ee (EXPR_LIST, mode, expr,
438 expr_list));
441 rtx_insn_list *
442 gen_rtx_INSN_LIST (machine_mode mode, rtx insn, rtx insn_list)
444 return as_a <rtx_insn_list *> (gen_rtx_fmt_ue (INSN_LIST, mode, insn,
445 insn_list));
448 rtx_insn *
449 gen_rtx_INSN (machine_mode mode, rtx_insn *prev_insn, rtx_insn *next_insn,
450 basic_block bb, rtx pattern, int location, int code,
451 rtx reg_notes)
453 return as_a <rtx_insn *> (gen_rtx_fmt_uuBeiie (INSN, mode,
454 prev_insn, next_insn,
455 bb, pattern, location, code,
456 reg_notes));
460 gen_rtx_CONST_INT (machine_mode mode ATTRIBUTE_UNUSED, HOST_WIDE_INT arg)
462 if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
463 return const_int_rtx[arg + MAX_SAVED_CONST_INT];
465 #if STORE_FLAG_VALUE != 1 && STORE_FLAG_VALUE != -1
466 if (const_true_rtx && arg == STORE_FLAG_VALUE)
467 return const_true_rtx;
468 #endif
470 /* Look up the CONST_INT in the hash table. */
471 rtx *slot = const_int_htab->find_slot_with_hash (arg, (hashval_t) arg,
472 INSERT);
473 if (*slot == 0)
474 *slot = gen_rtx_raw_CONST_INT (VOIDmode, arg);
476 return *slot;
480 gen_int_mode (HOST_WIDE_INT c, machine_mode mode)
482 return GEN_INT (trunc_int_for_mode (c, mode));
485 /* CONST_DOUBLEs might be created from pairs of integers, or from
486 REAL_VALUE_TYPEs. Also, their length is known only at run time,
487 so we cannot use gen_rtx_raw_CONST_DOUBLE. */
489 /* Determine whether REAL, a CONST_DOUBLE, already exists in the
490 hash table. If so, return its counterpart; otherwise add it
491 to the hash table and return it. */
492 static rtx
493 lookup_const_double (rtx real)
495 rtx *slot = const_double_htab->find_slot (real, INSERT);
496 if (*slot == 0)
497 *slot = real;
499 return *slot;
502 /* Return a CONST_DOUBLE rtx for a floating-point value specified by
503 VALUE in mode MODE. */
505 const_double_from_real_value (REAL_VALUE_TYPE value, machine_mode mode)
507 rtx real = rtx_alloc (CONST_DOUBLE);
508 PUT_MODE (real, mode);
510 real->u.rv = value;
512 return lookup_const_double (real);
515 /* Determine whether FIXED, a CONST_FIXED, already exists in the
516 hash table. If so, return its counterpart; otherwise add it
517 to the hash table and return it. */
519 static rtx
520 lookup_const_fixed (rtx fixed)
522 rtx *slot = const_fixed_htab->find_slot (fixed, INSERT);
523 if (*slot == 0)
524 *slot = fixed;
526 return *slot;
529 /* Return a CONST_FIXED rtx for a fixed-point value specified by
530 VALUE in mode MODE. */
533 const_fixed_from_fixed_value (FIXED_VALUE_TYPE value, machine_mode mode)
535 rtx fixed = rtx_alloc (CONST_FIXED);
536 PUT_MODE (fixed, mode);
538 fixed->u.fv = value;
540 return lookup_const_fixed (fixed);
543 #if TARGET_SUPPORTS_WIDE_INT == 0
544 /* Constructs double_int from rtx CST. */
546 double_int
547 rtx_to_double_int (const_rtx cst)
549 double_int r;
551 if (CONST_INT_P (cst))
552 r = double_int::from_shwi (INTVAL (cst));
553 else if (CONST_DOUBLE_AS_INT_P (cst))
555 r.low = CONST_DOUBLE_LOW (cst);
556 r.high = CONST_DOUBLE_HIGH (cst);
558 else
559 gcc_unreachable ();
561 return r;
563 #endif
565 #if TARGET_SUPPORTS_WIDE_INT
566 /* Determine whether CONST_WIDE_INT WINT already exists in the hash table.
567 If so, return its counterpart; otherwise add it to the hash table and
568 return it. */
570 static rtx
571 lookup_const_wide_int (rtx wint)
573 rtx *slot = const_wide_int_htab->find_slot (wint, INSERT);
574 if (*slot == 0)
575 *slot = wint;
577 return *slot;
579 #endif
581 /* Return an rtx constant for V, given that the constant has mode MODE.
582 The returned rtx will be a CONST_INT if V fits, otherwise it will be
583 a CONST_DOUBLE (if !TARGET_SUPPORTS_WIDE_INT) or a CONST_WIDE_INT
584 (if TARGET_SUPPORTS_WIDE_INT). */
587 immed_wide_int_const (const wide_int_ref &v, machine_mode mode)
589 unsigned int len = v.get_len ();
590 unsigned int prec = GET_MODE_PRECISION (mode);
592 /* Allow truncation but not extension since we do not know if the
593 number is signed or unsigned. */
594 gcc_assert (prec <= v.get_precision ());
596 if (len < 2 || prec <= HOST_BITS_PER_WIDE_INT)
597 return gen_int_mode (v.elt (0), mode);
599 #if TARGET_SUPPORTS_WIDE_INT
601 unsigned int i;
602 rtx value;
603 unsigned int blocks_needed
604 = (prec + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
606 if (len > blocks_needed)
607 len = blocks_needed;
609 value = const_wide_int_alloc (len);
611 /* It is so tempting to just put the mode in here. Must control
612 myself ... */
613 PUT_MODE (value, VOIDmode);
614 CWI_PUT_NUM_ELEM (value, len);
616 for (i = 0; i < len; i++)
617 CONST_WIDE_INT_ELT (value, i) = v.elt (i);
619 return lookup_const_wide_int (value);
621 #else
622 return immed_double_const (v.elt (0), v.elt (1), mode);
623 #endif
626 #if TARGET_SUPPORTS_WIDE_INT == 0
627 /* Return a CONST_DOUBLE or CONST_INT for a value specified as a pair
628 of ints: I0 is the low-order word and I1 is the high-order word.
629 For values that are larger than HOST_BITS_PER_DOUBLE_INT, the
630 implied upper bits are copies of the high bit of i1. The value
631 itself is neither signed nor unsigned. Do not use this routine for
632 non-integer modes; convert to REAL_VALUE_TYPE and use
633 CONST_DOUBLE_FROM_REAL_VALUE. */
636 immed_double_const (HOST_WIDE_INT i0, HOST_WIDE_INT i1, machine_mode mode)
638 rtx value;
639 unsigned int i;
641 /* There are the following cases (note that there are no modes with
642 HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode) < HOST_BITS_PER_DOUBLE_INT):
644 1) If GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT, then we use
645 gen_int_mode.
646 2) If the value of the integer fits into HOST_WIDE_INT anyway
647 (i.e., i1 consists only from copies of the sign bit, and sign
648 of i0 and i1 are the same), then we return a CONST_INT for i0.
649 3) Otherwise, we create a CONST_DOUBLE for i0 and i1. */
650 if (mode != VOIDmode)
652 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
653 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT
654 /* We can get a 0 for an error mark. */
655 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
656 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
657 || GET_MODE_CLASS (mode) == MODE_POINTER_BOUNDS);
659 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
660 return gen_int_mode (i0, mode);
663 /* If this integer fits in one word, return a CONST_INT. */
664 if ((i1 == 0 && i0 >= 0) || (i1 == ~0 && i0 < 0))
665 return GEN_INT (i0);
667 /* We use VOIDmode for integers. */
668 value = rtx_alloc (CONST_DOUBLE);
669 PUT_MODE (value, VOIDmode);
671 CONST_DOUBLE_LOW (value) = i0;
672 CONST_DOUBLE_HIGH (value) = i1;
674 for (i = 2; i < (sizeof CONST_DOUBLE_FORMAT - 1); i++)
675 XWINT (value, i) = 0;
677 return lookup_const_double (value);
679 #endif
682 gen_rtx_REG (machine_mode mode, unsigned int regno)
684 /* In case the MD file explicitly references the frame pointer, have
685 all such references point to the same frame pointer. This is
686 used during frame pointer elimination to distinguish the explicit
687 references to these registers from pseudos that happened to be
688 assigned to them.
690 If we have eliminated the frame pointer or arg pointer, we will
691 be using it as a normal register, for example as a spill
692 register. In such cases, we might be accessing it in a mode that
693 is not Pmode and therefore cannot use the pre-allocated rtx.
695 Also don't do this when we are making new REGs in reload, since
696 we don't want to get confused with the real pointers. */
698 if (mode == Pmode && !reload_in_progress && !lra_in_progress)
700 if (regno == FRAME_POINTER_REGNUM
701 && (!reload_completed || frame_pointer_needed))
702 return frame_pointer_rtx;
703 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
704 if (regno == HARD_FRAME_POINTER_REGNUM
705 && (!reload_completed || frame_pointer_needed))
706 return hard_frame_pointer_rtx;
707 #endif
708 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM && !HARD_FRAME_POINTER_IS_ARG_POINTER
709 if (regno == ARG_POINTER_REGNUM)
710 return arg_pointer_rtx;
711 #endif
712 #ifdef RETURN_ADDRESS_POINTER_REGNUM
713 if (regno == RETURN_ADDRESS_POINTER_REGNUM)
714 return return_address_pointer_rtx;
715 #endif
716 if (regno == (unsigned) PIC_OFFSET_TABLE_REGNUM
717 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
718 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
719 return pic_offset_table_rtx;
720 if (regno == STACK_POINTER_REGNUM)
721 return stack_pointer_rtx;
724 #if 0
725 /* If the per-function register table has been set up, try to re-use
726 an existing entry in that table to avoid useless generation of RTL.
728 This code is disabled for now until we can fix the various backends
729 which depend on having non-shared hard registers in some cases. Long
730 term we want to re-enable this code as it can significantly cut down
731 on the amount of useless RTL that gets generated.
733 We'll also need to fix some code that runs after reload that wants to
734 set ORIGINAL_REGNO. */
736 if (cfun
737 && cfun->emit
738 && regno_reg_rtx
739 && regno < FIRST_PSEUDO_REGISTER
740 && reg_raw_mode[regno] == mode)
741 return regno_reg_rtx[regno];
742 #endif
744 return gen_raw_REG (mode, regno);
748 gen_rtx_MEM (machine_mode mode, rtx addr)
750 rtx rt = gen_rtx_raw_MEM (mode, addr);
752 /* This field is not cleared by the mere allocation of the rtx, so
753 we clear it here. */
754 MEM_ATTRS (rt) = 0;
756 return rt;
759 /* Generate a memory referring to non-trapping constant memory. */
762 gen_const_mem (machine_mode mode, rtx addr)
764 rtx mem = gen_rtx_MEM (mode, addr);
765 MEM_READONLY_P (mem) = 1;
766 MEM_NOTRAP_P (mem) = 1;
767 return mem;
770 /* Generate a MEM referring to fixed portions of the frame, e.g., register
771 save areas. */
774 gen_frame_mem (machine_mode mode, rtx addr)
776 rtx mem = gen_rtx_MEM (mode, addr);
777 MEM_NOTRAP_P (mem) = 1;
778 set_mem_alias_set (mem, get_frame_alias_set ());
779 return mem;
782 /* Generate a MEM referring to a temporary use of the stack, not part
783 of the fixed stack frame. For example, something which is pushed
784 by a target splitter. */
786 gen_tmp_stack_mem (machine_mode mode, rtx addr)
788 rtx mem = gen_rtx_MEM (mode, addr);
789 MEM_NOTRAP_P (mem) = 1;
790 if (!cfun->calls_alloca)
791 set_mem_alias_set (mem, get_frame_alias_set ());
792 return mem;
795 /* We want to create (subreg:OMODE (obj:IMODE) OFFSET). Return true if
796 this construct would be valid, and false otherwise. */
798 bool
799 validate_subreg (machine_mode omode, machine_mode imode,
800 const_rtx reg, unsigned int offset)
802 unsigned int isize = GET_MODE_SIZE (imode);
803 unsigned int osize = GET_MODE_SIZE (omode);
805 /* All subregs must be aligned. */
806 if (offset % osize != 0)
807 return false;
809 /* The subreg offset cannot be outside the inner object. */
810 if (offset >= isize)
811 return false;
813 /* ??? This should not be here. Temporarily continue to allow word_mode
814 subregs of anything. The most common offender is (subreg:SI (reg:DF)).
815 Generally, backends are doing something sketchy but it'll take time to
816 fix them all. */
817 if (omode == word_mode)
819 /* ??? Similarly, e.g. with (subreg:DF (reg:TI)). Though store_bit_field
820 is the culprit here, and not the backends. */
821 else if (osize >= UNITS_PER_WORD && isize >= osize)
823 /* Allow component subregs of complex and vector. Though given the below
824 extraction rules, it's not always clear what that means. */
825 else if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
826 && GET_MODE_INNER (imode) == omode)
828 /* ??? x86 sse code makes heavy use of *paradoxical* vector subregs,
829 i.e. (subreg:V4SF (reg:SF) 0). This surely isn't the cleanest way to
830 represent this. It's questionable if this ought to be represented at
831 all -- why can't this all be hidden in post-reload splitters that make
832 arbitrarily mode changes to the registers themselves. */
833 else if (VECTOR_MODE_P (omode) && GET_MODE_INNER (omode) == imode)
835 /* Subregs involving floating point modes are not allowed to
836 change size. Therefore (subreg:DI (reg:DF) 0) is fine, but
837 (subreg:SI (reg:DF) 0) isn't. */
838 else if (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))
840 if (! (isize == osize
841 /* LRA can use subreg to store a floating point value in
842 an integer mode. Although the floating point and the
843 integer modes need the same number of hard registers,
844 the size of floating point mode can be less than the
845 integer mode. LRA also uses subregs for a register
846 should be used in different mode in on insn. */
847 || lra_in_progress))
848 return false;
851 /* Paradoxical subregs must have offset zero. */
852 if (osize > isize)
853 return offset == 0;
855 /* This is a normal subreg. Verify that the offset is representable. */
857 /* For hard registers, we already have most of these rules collected in
858 subreg_offset_representable_p. */
859 if (reg && REG_P (reg) && HARD_REGISTER_P (reg))
861 unsigned int regno = REGNO (reg);
863 #ifdef CANNOT_CHANGE_MODE_CLASS
864 if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
865 && GET_MODE_INNER (imode) == omode)
867 else if (REG_CANNOT_CHANGE_MODE_P (regno, imode, omode))
868 return false;
869 #endif
871 return subreg_offset_representable_p (regno, imode, offset, omode);
874 /* For pseudo registers, we want most of the same checks. Namely:
875 If the register no larger than a word, the subreg must be lowpart.
876 If the register is larger than a word, the subreg must be the lowpart
877 of a subword. A subreg does *not* perform arbitrary bit extraction.
878 Given that we've already checked mode/offset alignment, we only have
879 to check subword subregs here. */
880 if (osize < UNITS_PER_WORD
881 && ! (lra_in_progress && (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))))
883 machine_mode wmode = isize > UNITS_PER_WORD ? word_mode : imode;
884 unsigned int low_off = subreg_lowpart_offset (omode, wmode);
885 if (offset % UNITS_PER_WORD != low_off)
886 return false;
888 return true;
892 gen_rtx_SUBREG (machine_mode mode, rtx reg, int offset)
894 gcc_assert (validate_subreg (mode, GET_MODE (reg), reg, offset));
895 return gen_rtx_raw_SUBREG (mode, reg, offset);
898 /* Generate a SUBREG representing the least-significant part of REG if MODE
899 is smaller than mode of REG, otherwise paradoxical SUBREG. */
902 gen_lowpart_SUBREG (machine_mode mode, rtx reg)
904 machine_mode inmode;
906 inmode = GET_MODE (reg);
907 if (inmode == VOIDmode)
908 inmode = mode;
909 return gen_rtx_SUBREG (mode, reg,
910 subreg_lowpart_offset (mode, inmode));
914 gen_rtx_VAR_LOCATION (machine_mode mode, tree decl, rtx loc,
915 enum var_init_status status)
917 rtx x = gen_rtx_fmt_te (VAR_LOCATION, mode, decl, loc);
918 PAT_VAR_LOCATION_STATUS (x) = status;
919 return x;
923 /* Create an rtvec and stores within it the RTXen passed in the arguments. */
925 rtvec
926 gen_rtvec (int n, ...)
928 int i;
929 rtvec rt_val;
930 va_list p;
932 va_start (p, n);
934 /* Don't allocate an empty rtvec... */
935 if (n == 0)
937 va_end (p);
938 return NULL_RTVEC;
941 rt_val = rtvec_alloc (n);
943 for (i = 0; i < n; i++)
944 rt_val->elem[i] = va_arg (p, rtx);
946 va_end (p);
947 return rt_val;
950 rtvec
951 gen_rtvec_v (int n, rtx *argp)
953 int i;
954 rtvec rt_val;
956 /* Don't allocate an empty rtvec... */
957 if (n == 0)
958 return NULL_RTVEC;
960 rt_val = rtvec_alloc (n);
962 for (i = 0; i < n; i++)
963 rt_val->elem[i] = *argp++;
965 return rt_val;
968 rtvec
969 gen_rtvec_v (int n, rtx_insn **argp)
971 int i;
972 rtvec rt_val;
974 /* Don't allocate an empty rtvec... */
975 if (n == 0)
976 return NULL_RTVEC;
978 rt_val = rtvec_alloc (n);
980 for (i = 0; i < n; i++)
981 rt_val->elem[i] = *argp++;
983 return rt_val;
987 /* Return the number of bytes between the start of an OUTER_MODE
988 in-memory value and the start of an INNER_MODE in-memory value,
989 given that the former is a lowpart of the latter. It may be a
990 paradoxical lowpart, in which case the offset will be negative
991 on big-endian targets. */
994 byte_lowpart_offset (machine_mode outer_mode,
995 machine_mode inner_mode)
997 if (GET_MODE_SIZE (outer_mode) < GET_MODE_SIZE (inner_mode))
998 return subreg_lowpart_offset (outer_mode, inner_mode);
999 else
1000 return -subreg_lowpart_offset (inner_mode, outer_mode);
1003 /* Generate a REG rtx for a new pseudo register of mode MODE.
1004 This pseudo is assigned the next sequential register number. */
1007 gen_reg_rtx (machine_mode mode)
1009 rtx val;
1010 unsigned int align = GET_MODE_ALIGNMENT (mode);
1012 gcc_assert (can_create_pseudo_p ());
1014 /* If a virtual register with bigger mode alignment is generated,
1015 increase stack alignment estimation because it might be spilled
1016 to stack later. */
1017 if (SUPPORTS_STACK_ALIGNMENT
1018 && crtl->stack_alignment_estimated < align
1019 && !crtl->stack_realign_processed)
1021 unsigned int min_align = MINIMUM_ALIGNMENT (NULL, mode, align);
1022 if (crtl->stack_alignment_estimated < min_align)
1023 crtl->stack_alignment_estimated = min_align;
1026 if (generating_concat_p
1027 && (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
1028 || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT))
1030 /* For complex modes, don't make a single pseudo.
1031 Instead, make a CONCAT of two pseudos.
1032 This allows noncontiguous allocation of the real and imaginary parts,
1033 which makes much better code. Besides, allocating DCmode
1034 pseudos overstrains reload on some machines like the 386. */
1035 rtx realpart, imagpart;
1036 machine_mode partmode = GET_MODE_INNER (mode);
1038 realpart = gen_reg_rtx (partmode);
1039 imagpart = gen_reg_rtx (partmode);
1040 return gen_rtx_CONCAT (mode, realpart, imagpart);
1043 /* Do not call gen_reg_rtx with uninitialized crtl. */
1044 gcc_assert (crtl->emit.regno_pointer_align_length);
1046 /* Make sure regno_pointer_align, and regno_reg_rtx are large
1047 enough to have an element for this pseudo reg number. */
1049 if (reg_rtx_no == crtl->emit.regno_pointer_align_length)
1051 int old_size = crtl->emit.regno_pointer_align_length;
1052 char *tmp;
1053 rtx *new1;
1055 tmp = XRESIZEVEC (char, crtl->emit.regno_pointer_align, old_size * 2);
1056 memset (tmp + old_size, 0, old_size);
1057 crtl->emit.regno_pointer_align = (unsigned char *) tmp;
1059 new1 = GGC_RESIZEVEC (rtx, regno_reg_rtx, old_size * 2);
1060 memset (new1 + old_size, 0, old_size * sizeof (rtx));
1061 regno_reg_rtx = new1;
1063 crtl->emit.regno_pointer_align_length = old_size * 2;
1066 val = gen_raw_REG (mode, reg_rtx_no);
1067 regno_reg_rtx[reg_rtx_no++] = val;
1068 return val;
1071 /* Return TRUE if REG is a PARM_DECL, FALSE otherwise. */
1073 bool
1074 reg_is_parm_p (rtx reg)
1076 tree decl;
1078 gcc_assert (REG_P (reg));
1079 decl = REG_EXPR (reg);
1080 return (decl && TREE_CODE (decl) == PARM_DECL);
1083 /* Update NEW with the same attributes as REG, but with OFFSET added
1084 to the REG_OFFSET. */
1086 static void
1087 update_reg_offset (rtx new_rtx, rtx reg, int offset)
1089 REG_ATTRS (new_rtx) = get_reg_attrs (REG_EXPR (reg),
1090 REG_OFFSET (reg) + offset);
1093 /* Generate a register with same attributes as REG, but with OFFSET
1094 added to the REG_OFFSET. */
1097 gen_rtx_REG_offset (rtx reg, machine_mode mode, unsigned int regno,
1098 int offset)
1100 rtx new_rtx = gen_rtx_REG (mode, regno);
1102 update_reg_offset (new_rtx, reg, offset);
1103 return new_rtx;
1106 /* Generate a new pseudo-register with the same attributes as REG, but
1107 with OFFSET added to the REG_OFFSET. */
1110 gen_reg_rtx_offset (rtx reg, machine_mode mode, int offset)
1112 rtx new_rtx = gen_reg_rtx (mode);
1114 update_reg_offset (new_rtx, reg, offset);
1115 return new_rtx;
1118 /* Adjust REG in-place so that it has mode MODE. It is assumed that the
1119 new register is a (possibly paradoxical) lowpart of the old one. */
1121 void
1122 adjust_reg_mode (rtx reg, machine_mode mode)
1124 update_reg_offset (reg, reg, byte_lowpart_offset (mode, GET_MODE (reg)));
1125 PUT_MODE (reg, mode);
1128 /* Copy REG's attributes from X, if X has any attributes. If REG and X
1129 have different modes, REG is a (possibly paradoxical) lowpart of X. */
1131 void
1132 set_reg_attrs_from_value (rtx reg, rtx x)
1134 int offset;
1135 bool can_be_reg_pointer = true;
1137 /* Don't call mark_reg_pointer for incompatible pointer sign
1138 extension. */
1139 while (GET_CODE (x) == SIGN_EXTEND
1140 || GET_CODE (x) == ZERO_EXTEND
1141 || GET_CODE (x) == TRUNCATE
1142 || (GET_CODE (x) == SUBREG && subreg_lowpart_p (x)))
1144 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1145 if ((GET_CODE (x) == SIGN_EXTEND && POINTERS_EXTEND_UNSIGNED)
1146 || (GET_CODE (x) != SIGN_EXTEND && ! POINTERS_EXTEND_UNSIGNED))
1147 can_be_reg_pointer = false;
1148 #endif
1149 x = XEXP (x, 0);
1152 /* Hard registers can be reused for multiple purposes within the same
1153 function, so setting REG_ATTRS, REG_POINTER and REG_POINTER_ALIGN
1154 on them is wrong. */
1155 if (HARD_REGISTER_P (reg))
1156 return;
1158 offset = byte_lowpart_offset (GET_MODE (reg), GET_MODE (x));
1159 if (MEM_P (x))
1161 if (MEM_OFFSET_KNOWN_P (x))
1162 REG_ATTRS (reg) = get_reg_attrs (MEM_EXPR (x),
1163 MEM_OFFSET (x) + offset);
1164 if (can_be_reg_pointer && MEM_POINTER (x))
1165 mark_reg_pointer (reg, 0);
1167 else if (REG_P (x))
1169 if (REG_ATTRS (x))
1170 update_reg_offset (reg, x, offset);
1171 if (can_be_reg_pointer && REG_POINTER (x))
1172 mark_reg_pointer (reg, REGNO_POINTER_ALIGN (REGNO (x)));
1176 /* Generate a REG rtx for a new pseudo register, copying the mode
1177 and attributes from X. */
1180 gen_reg_rtx_and_attrs (rtx x)
1182 rtx reg = gen_reg_rtx (GET_MODE (x));
1183 set_reg_attrs_from_value (reg, x);
1184 return reg;
1187 /* Set the register attributes for registers contained in PARM_RTX.
1188 Use needed values from memory attributes of MEM. */
1190 void
1191 set_reg_attrs_for_parm (rtx parm_rtx, rtx mem)
1193 if (REG_P (parm_rtx))
1194 set_reg_attrs_from_value (parm_rtx, mem);
1195 else if (GET_CODE (parm_rtx) == PARALLEL)
1197 /* Check for a NULL entry in the first slot, used to indicate that the
1198 parameter goes both on the stack and in registers. */
1199 int i = XEXP (XVECEXP (parm_rtx, 0, 0), 0) ? 0 : 1;
1200 for (; i < XVECLEN (parm_rtx, 0); i++)
1202 rtx x = XVECEXP (parm_rtx, 0, i);
1203 if (REG_P (XEXP (x, 0)))
1204 REG_ATTRS (XEXP (x, 0))
1205 = get_reg_attrs (MEM_EXPR (mem),
1206 INTVAL (XEXP (x, 1)));
1211 /* Set the REG_ATTRS for registers in value X, given that X represents
1212 decl T. */
1214 void
1215 set_reg_attrs_for_decl_rtl (tree t, rtx x)
1217 if (GET_CODE (x) == SUBREG)
1219 gcc_assert (subreg_lowpart_p (x));
1220 x = SUBREG_REG (x);
1222 if (REG_P (x))
1223 REG_ATTRS (x)
1224 = get_reg_attrs (t, byte_lowpart_offset (GET_MODE (x),
1225 DECL_MODE (t)));
1226 if (GET_CODE (x) == CONCAT)
1228 if (REG_P (XEXP (x, 0)))
1229 REG_ATTRS (XEXP (x, 0)) = get_reg_attrs (t, 0);
1230 if (REG_P (XEXP (x, 1)))
1231 REG_ATTRS (XEXP (x, 1))
1232 = get_reg_attrs (t, GET_MODE_UNIT_SIZE (GET_MODE (XEXP (x, 0))));
1234 if (GET_CODE (x) == PARALLEL)
1236 int i, start;
1238 /* Check for a NULL entry, used to indicate that the parameter goes
1239 both on the stack and in registers. */
1240 if (XEXP (XVECEXP (x, 0, 0), 0))
1241 start = 0;
1242 else
1243 start = 1;
1245 for (i = start; i < XVECLEN (x, 0); i++)
1247 rtx y = XVECEXP (x, 0, i);
1248 if (REG_P (XEXP (y, 0)))
1249 REG_ATTRS (XEXP (y, 0)) = get_reg_attrs (t, INTVAL (XEXP (y, 1)));
1254 /* Assign the RTX X to declaration T. */
1256 void
1257 set_decl_rtl (tree t, rtx x)
1259 DECL_WRTL_CHECK (t)->decl_with_rtl.rtl = x;
1260 if (x)
1261 set_reg_attrs_for_decl_rtl (t, x);
1264 /* Assign the RTX X to parameter declaration T. BY_REFERENCE_P is true
1265 if the ABI requires the parameter to be passed by reference. */
1267 void
1268 set_decl_incoming_rtl (tree t, rtx x, bool by_reference_p)
1270 DECL_INCOMING_RTL (t) = x;
1271 if (x && !by_reference_p)
1272 set_reg_attrs_for_decl_rtl (t, x);
1275 /* Identify REG (which may be a CONCAT) as a user register. */
1277 void
1278 mark_user_reg (rtx reg)
1280 if (GET_CODE (reg) == CONCAT)
1282 REG_USERVAR_P (XEXP (reg, 0)) = 1;
1283 REG_USERVAR_P (XEXP (reg, 1)) = 1;
1285 else
1287 gcc_assert (REG_P (reg));
1288 REG_USERVAR_P (reg) = 1;
1292 /* Identify REG as a probable pointer register and show its alignment
1293 as ALIGN, if nonzero. */
1295 void
1296 mark_reg_pointer (rtx reg, int align)
1298 if (! REG_POINTER (reg))
1300 REG_POINTER (reg) = 1;
1302 if (align)
1303 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1305 else if (align && align < REGNO_POINTER_ALIGN (REGNO (reg)))
1306 /* We can no-longer be sure just how aligned this pointer is. */
1307 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1310 /* Return 1 plus largest pseudo reg number used in the current function. */
1313 max_reg_num (void)
1315 return reg_rtx_no;
1318 /* Return 1 + the largest label number used so far in the current function. */
1321 max_label_num (void)
1323 return label_num;
1326 /* Return first label number used in this function (if any were used). */
1329 get_first_label_num (void)
1331 return first_label_num;
1334 /* If the rtx for label was created during the expansion of a nested
1335 function, then first_label_num won't include this label number.
1336 Fix this now so that array indices work later. */
1338 void
1339 maybe_set_first_label_num (rtx x)
1341 if (CODE_LABEL_NUMBER (x) < first_label_num)
1342 first_label_num = CODE_LABEL_NUMBER (x);
1345 /* Return a value representing some low-order bits of X, where the number
1346 of low-order bits is given by MODE. Note that no conversion is done
1347 between floating-point and fixed-point values, rather, the bit
1348 representation is returned.
1350 This function handles the cases in common between gen_lowpart, below,
1351 and two variants in cse.c and combine.c. These are the cases that can
1352 be safely handled at all points in the compilation.
1354 If this is not a case we can handle, return 0. */
1357 gen_lowpart_common (machine_mode mode, rtx x)
1359 int msize = GET_MODE_SIZE (mode);
1360 int xsize;
1361 int offset = 0;
1362 machine_mode innermode;
1364 /* Unfortunately, this routine doesn't take a parameter for the mode of X,
1365 so we have to make one up. Yuk. */
1366 innermode = GET_MODE (x);
1367 if (CONST_INT_P (x)
1368 && msize * BITS_PER_UNIT <= HOST_BITS_PER_WIDE_INT)
1369 innermode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1370 else if (innermode == VOIDmode)
1371 innermode = mode_for_size (HOST_BITS_PER_DOUBLE_INT, MODE_INT, 0);
1373 xsize = GET_MODE_SIZE (innermode);
1375 gcc_assert (innermode != VOIDmode && innermode != BLKmode);
1377 if (innermode == mode)
1378 return x;
1380 /* MODE must occupy no more words than the mode of X. */
1381 if ((msize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
1382 > ((xsize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
1383 return 0;
1385 /* Don't allow generating paradoxical FLOAT_MODE subregs. */
1386 if (SCALAR_FLOAT_MODE_P (mode) && msize > xsize)
1387 return 0;
1389 offset = subreg_lowpart_offset (mode, innermode);
1391 if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
1392 && (GET_MODE_CLASS (mode) == MODE_INT
1393 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
1395 /* If we are getting the low-order part of something that has been
1396 sign- or zero-extended, we can either just use the object being
1397 extended or make a narrower extension. If we want an even smaller
1398 piece than the size of the object being extended, call ourselves
1399 recursively.
1401 This case is used mostly by combine and cse. */
1403 if (GET_MODE (XEXP (x, 0)) == mode)
1404 return XEXP (x, 0);
1405 else if (msize < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
1406 return gen_lowpart_common (mode, XEXP (x, 0));
1407 else if (msize < xsize)
1408 return gen_rtx_fmt_e (GET_CODE (x), mode, XEXP (x, 0));
1410 else if (GET_CODE (x) == SUBREG || REG_P (x)
1411 || GET_CODE (x) == CONCAT || GET_CODE (x) == CONST_VECTOR
1412 || CONST_DOUBLE_AS_FLOAT_P (x) || CONST_SCALAR_INT_P (x))
1413 return simplify_gen_subreg (mode, x, innermode, offset);
1415 /* Otherwise, we can't do this. */
1416 return 0;
1420 gen_highpart (machine_mode mode, rtx x)
1422 unsigned int msize = GET_MODE_SIZE (mode);
1423 rtx result;
1425 /* This case loses if X is a subreg. To catch bugs early,
1426 complain if an invalid MODE is used even in other cases. */
1427 gcc_assert (msize <= UNITS_PER_WORD
1428 || msize == (unsigned int) GET_MODE_UNIT_SIZE (GET_MODE (x)));
1430 result = simplify_gen_subreg (mode, x, GET_MODE (x),
1431 subreg_highpart_offset (mode, GET_MODE (x)));
1432 gcc_assert (result);
1434 /* simplify_gen_subreg is not guaranteed to return a valid operand for
1435 the target if we have a MEM. gen_highpart must return a valid operand,
1436 emitting code if necessary to do so. */
1437 if (MEM_P (result))
1439 result = validize_mem (result);
1440 gcc_assert (result);
1443 return result;
1446 /* Like gen_highpart, but accept mode of EXP operand in case EXP can
1447 be VOIDmode constant. */
1449 gen_highpart_mode (machine_mode outermode, machine_mode innermode, rtx exp)
1451 if (GET_MODE (exp) != VOIDmode)
1453 gcc_assert (GET_MODE (exp) == innermode);
1454 return gen_highpart (outermode, exp);
1456 return simplify_gen_subreg (outermode, exp, innermode,
1457 subreg_highpart_offset (outermode, innermode));
1460 /* Return the SUBREG_BYTE for an OUTERMODE lowpart of an INNERMODE value. */
1462 unsigned int
1463 subreg_lowpart_offset (machine_mode outermode, machine_mode innermode)
1465 unsigned int offset = 0;
1466 int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1468 if (difference > 0)
1470 if (WORDS_BIG_ENDIAN)
1471 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1472 if (BYTES_BIG_ENDIAN)
1473 offset += difference % UNITS_PER_WORD;
1476 return offset;
1479 /* Return offset in bytes to get OUTERMODE high part
1480 of the value in mode INNERMODE stored in memory in target format. */
1481 unsigned int
1482 subreg_highpart_offset (machine_mode outermode, machine_mode innermode)
1484 unsigned int offset = 0;
1485 int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1487 gcc_assert (GET_MODE_SIZE (innermode) >= GET_MODE_SIZE (outermode));
1489 if (difference > 0)
1491 if (! WORDS_BIG_ENDIAN)
1492 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1493 if (! BYTES_BIG_ENDIAN)
1494 offset += difference % UNITS_PER_WORD;
1497 return offset;
1500 /* Return 1 iff X, assumed to be a SUBREG,
1501 refers to the least significant part of its containing reg.
1502 If X is not a SUBREG, always return 1 (it is its own low part!). */
1505 subreg_lowpart_p (const_rtx x)
1507 if (GET_CODE (x) != SUBREG)
1508 return 1;
1509 else if (GET_MODE (SUBREG_REG (x)) == VOIDmode)
1510 return 0;
1512 return (subreg_lowpart_offset (GET_MODE (x), GET_MODE (SUBREG_REG (x)))
1513 == SUBREG_BYTE (x));
1516 /* Return true if X is a paradoxical subreg, false otherwise. */
1517 bool
1518 paradoxical_subreg_p (const_rtx x)
1520 if (GET_CODE (x) != SUBREG)
1521 return false;
1522 return (GET_MODE_PRECISION (GET_MODE (x))
1523 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (x))));
1526 /* Return subword OFFSET of operand OP.
1527 The word number, OFFSET, is interpreted as the word number starting
1528 at the low-order address. OFFSET 0 is the low-order word if not
1529 WORDS_BIG_ENDIAN, otherwise it is the high-order word.
1531 If we cannot extract the required word, we return zero. Otherwise,
1532 an rtx corresponding to the requested word will be returned.
1534 VALIDATE_ADDRESS is nonzero if the address should be validated. Before
1535 reload has completed, a valid address will always be returned. After
1536 reload, if a valid address cannot be returned, we return zero.
1538 If VALIDATE_ADDRESS is zero, we simply form the required address; validating
1539 it is the responsibility of the caller.
1541 MODE is the mode of OP in case it is a CONST_INT.
1543 ??? This is still rather broken for some cases. The problem for the
1544 moment is that all callers of this thing provide no 'goal mode' to
1545 tell us to work with. This exists because all callers were written
1546 in a word based SUBREG world.
1547 Now use of this function can be deprecated by simplify_subreg in most
1548 cases.
1552 operand_subword (rtx op, unsigned int offset, int validate_address, machine_mode mode)
1554 if (mode == VOIDmode)
1555 mode = GET_MODE (op);
1557 gcc_assert (mode != VOIDmode);
1559 /* If OP is narrower than a word, fail. */
1560 if (mode != BLKmode
1561 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD))
1562 return 0;
1564 /* If we want a word outside OP, return zero. */
1565 if (mode != BLKmode
1566 && (offset + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode))
1567 return const0_rtx;
1569 /* Form a new MEM at the requested address. */
1570 if (MEM_P (op))
1572 rtx new_rtx = adjust_address_nv (op, word_mode, offset * UNITS_PER_WORD);
1574 if (! validate_address)
1575 return new_rtx;
1577 else if (reload_completed)
1579 if (! strict_memory_address_addr_space_p (word_mode,
1580 XEXP (new_rtx, 0),
1581 MEM_ADDR_SPACE (op)))
1582 return 0;
1584 else
1585 return replace_equiv_address (new_rtx, XEXP (new_rtx, 0));
1588 /* Rest can be handled by simplify_subreg. */
1589 return simplify_gen_subreg (word_mode, op, mode, (offset * UNITS_PER_WORD));
1592 /* Similar to `operand_subword', but never return 0. If we can't
1593 extract the required subword, put OP into a register and try again.
1594 The second attempt must succeed. We always validate the address in
1595 this case.
1597 MODE is the mode of OP, in case it is CONST_INT. */
1600 operand_subword_force (rtx op, unsigned int offset, machine_mode mode)
1602 rtx result = operand_subword (op, offset, 1, mode);
1604 if (result)
1605 return result;
1607 if (mode != BLKmode && mode != VOIDmode)
1609 /* If this is a register which can not be accessed by words, copy it
1610 to a pseudo register. */
1611 if (REG_P (op))
1612 op = copy_to_reg (op);
1613 else
1614 op = force_reg (mode, op);
1617 result = operand_subword (op, offset, 1, mode);
1618 gcc_assert (result);
1620 return result;
1623 /* Returns 1 if both MEM_EXPR can be considered equal
1624 and 0 otherwise. */
1627 mem_expr_equal_p (const_tree expr1, const_tree expr2)
1629 if (expr1 == expr2)
1630 return 1;
1632 if (! expr1 || ! expr2)
1633 return 0;
1635 if (TREE_CODE (expr1) != TREE_CODE (expr2))
1636 return 0;
1638 return operand_equal_p (expr1, expr2, 0);
1641 /* Return OFFSET if XEXP (MEM, 0) - OFFSET is known to be ALIGN
1642 bits aligned for 0 <= OFFSET < ALIGN / BITS_PER_UNIT, or
1643 -1 if not known. */
1646 get_mem_align_offset (rtx mem, unsigned int align)
1648 tree expr;
1649 unsigned HOST_WIDE_INT offset;
1651 /* This function can't use
1652 if (!MEM_EXPR (mem) || !MEM_OFFSET_KNOWN_P (mem)
1653 || (MAX (MEM_ALIGN (mem),
1654 MAX (align, get_object_alignment (MEM_EXPR (mem))))
1655 < align))
1656 return -1;
1657 else
1658 return (- MEM_OFFSET (mem)) & (align / BITS_PER_UNIT - 1);
1659 for two reasons:
1660 - COMPONENT_REFs in MEM_EXPR can have NULL first operand,
1661 for <variable>. get_inner_reference doesn't handle it and
1662 even if it did, the alignment in that case needs to be determined
1663 from DECL_FIELD_CONTEXT's TYPE_ALIGN.
1664 - it would do suboptimal job for COMPONENT_REFs, even if MEM_EXPR
1665 isn't sufficiently aligned, the object it is in might be. */
1666 gcc_assert (MEM_P (mem));
1667 expr = MEM_EXPR (mem);
1668 if (expr == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
1669 return -1;
1671 offset = MEM_OFFSET (mem);
1672 if (DECL_P (expr))
1674 if (DECL_ALIGN (expr) < align)
1675 return -1;
1677 else if (INDIRECT_REF_P (expr))
1679 if (TYPE_ALIGN (TREE_TYPE (expr)) < (unsigned int) align)
1680 return -1;
1682 else if (TREE_CODE (expr) == COMPONENT_REF)
1684 while (1)
1686 tree inner = TREE_OPERAND (expr, 0);
1687 tree field = TREE_OPERAND (expr, 1);
1688 tree byte_offset = component_ref_field_offset (expr);
1689 tree bit_offset = DECL_FIELD_BIT_OFFSET (field);
1691 if (!byte_offset
1692 || !tree_fits_uhwi_p (byte_offset)
1693 || !tree_fits_uhwi_p (bit_offset))
1694 return -1;
1696 offset += tree_to_uhwi (byte_offset);
1697 offset += tree_to_uhwi (bit_offset) / BITS_PER_UNIT;
1699 if (inner == NULL_TREE)
1701 if (TYPE_ALIGN (DECL_FIELD_CONTEXT (field))
1702 < (unsigned int) align)
1703 return -1;
1704 break;
1706 else if (DECL_P (inner))
1708 if (DECL_ALIGN (inner) < align)
1709 return -1;
1710 break;
1712 else if (TREE_CODE (inner) != COMPONENT_REF)
1713 return -1;
1714 expr = inner;
1717 else
1718 return -1;
1720 return offset & ((align / BITS_PER_UNIT) - 1);
1723 /* Given REF (a MEM) and T, either the type of X or the expression
1724 corresponding to REF, set the memory attributes. OBJECTP is nonzero
1725 if we are making a new object of this type. BITPOS is nonzero if
1726 there is an offset outstanding on T that will be applied later. */
1728 void
1729 set_mem_attributes_minus_bitpos (rtx ref, tree t, int objectp,
1730 HOST_WIDE_INT bitpos)
1732 HOST_WIDE_INT apply_bitpos = 0;
1733 tree type;
1734 struct mem_attrs attrs, *defattrs, *refattrs;
1735 addr_space_t as;
1737 /* It can happen that type_for_mode was given a mode for which there
1738 is no language-level type. In which case it returns NULL, which
1739 we can see here. */
1740 if (t == NULL_TREE)
1741 return;
1743 type = TYPE_P (t) ? t : TREE_TYPE (t);
1744 if (type == error_mark_node)
1745 return;
1747 /* If we have already set DECL_RTL = ref, get_alias_set will get the
1748 wrong answer, as it assumes that DECL_RTL already has the right alias
1749 info. Callers should not set DECL_RTL until after the call to
1750 set_mem_attributes. */
1751 gcc_assert (!DECL_P (t) || ref != DECL_RTL_IF_SET (t));
1753 memset (&attrs, 0, sizeof (attrs));
1755 /* Get the alias set from the expression or type (perhaps using a
1756 front-end routine) and use it. */
1757 attrs.alias = get_alias_set (t);
1759 MEM_VOLATILE_P (ref) |= TYPE_VOLATILE (type);
1760 MEM_POINTER (ref) = POINTER_TYPE_P (type);
1762 /* Default values from pre-existing memory attributes if present. */
1763 refattrs = MEM_ATTRS (ref);
1764 if (refattrs)
1766 /* ??? Can this ever happen? Calling this routine on a MEM that
1767 already carries memory attributes should probably be invalid. */
1768 attrs.expr = refattrs->expr;
1769 attrs.offset_known_p = refattrs->offset_known_p;
1770 attrs.offset = refattrs->offset;
1771 attrs.size_known_p = refattrs->size_known_p;
1772 attrs.size = refattrs->size;
1773 attrs.align = refattrs->align;
1776 /* Otherwise, default values from the mode of the MEM reference. */
1777 else
1779 defattrs = mode_mem_attrs[(int) GET_MODE (ref)];
1780 gcc_assert (!defattrs->expr);
1781 gcc_assert (!defattrs->offset_known_p);
1783 /* Respect mode size. */
1784 attrs.size_known_p = defattrs->size_known_p;
1785 attrs.size = defattrs->size;
1786 /* ??? Is this really necessary? We probably should always get
1787 the size from the type below. */
1789 /* Respect mode alignment for STRICT_ALIGNMENT targets if T is a type;
1790 if T is an object, always compute the object alignment below. */
1791 if (TYPE_P (t))
1792 attrs.align = defattrs->align;
1793 else
1794 attrs.align = BITS_PER_UNIT;
1795 /* ??? If T is a type, respecting mode alignment may *also* be wrong
1796 e.g. if the type carries an alignment attribute. Should we be
1797 able to simply always use TYPE_ALIGN? */
1800 /* We can set the alignment from the type if we are making an object,
1801 this is an INDIRECT_REF, or if TYPE_ALIGN_OK. */
1802 if (objectp || TREE_CODE (t) == INDIRECT_REF || TYPE_ALIGN_OK (type))
1803 attrs.align = MAX (attrs.align, TYPE_ALIGN (type));
1805 /* If the size is known, we can set that. */
1806 tree new_size = TYPE_SIZE_UNIT (type);
1808 /* The address-space is that of the type. */
1809 as = TYPE_ADDR_SPACE (type);
1811 /* If T is not a type, we may be able to deduce some more information about
1812 the expression. */
1813 if (! TYPE_P (t))
1815 tree base;
1817 if (TREE_THIS_VOLATILE (t))
1818 MEM_VOLATILE_P (ref) = 1;
1820 /* Now remove any conversions: they don't change what the underlying
1821 object is. Likewise for SAVE_EXPR. */
1822 while (CONVERT_EXPR_P (t)
1823 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1824 || TREE_CODE (t) == SAVE_EXPR)
1825 t = TREE_OPERAND (t, 0);
1827 /* Note whether this expression can trap. */
1828 MEM_NOTRAP_P (ref) = !tree_could_trap_p (t);
1830 base = get_base_address (t);
1831 if (base)
1833 if (DECL_P (base)
1834 && TREE_READONLY (base)
1835 && (TREE_STATIC (base) || DECL_EXTERNAL (base))
1836 && !TREE_THIS_VOLATILE (base))
1837 MEM_READONLY_P (ref) = 1;
1839 /* Mark static const strings readonly as well. */
1840 if (TREE_CODE (base) == STRING_CST
1841 && TREE_READONLY (base)
1842 && TREE_STATIC (base))
1843 MEM_READONLY_P (ref) = 1;
1845 /* Address-space information is on the base object. */
1846 if (TREE_CODE (base) == MEM_REF
1847 || TREE_CODE (base) == TARGET_MEM_REF)
1848 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (base,
1849 0))));
1850 else
1851 as = TYPE_ADDR_SPACE (TREE_TYPE (base));
1854 /* If this expression uses it's parent's alias set, mark it such
1855 that we won't change it. */
1856 if (component_uses_parent_alias_set_from (t) != NULL_TREE)
1857 MEM_KEEP_ALIAS_SET_P (ref) = 1;
1859 /* If this is a decl, set the attributes of the MEM from it. */
1860 if (DECL_P (t))
1862 attrs.expr = t;
1863 attrs.offset_known_p = true;
1864 attrs.offset = 0;
1865 apply_bitpos = bitpos;
1866 new_size = DECL_SIZE_UNIT (t);
1869 /* ??? If we end up with a constant here do record a MEM_EXPR. */
1870 else if (CONSTANT_CLASS_P (t))
1873 /* If this is a field reference, record it. */
1874 else if (TREE_CODE (t) == COMPONENT_REF)
1876 attrs.expr = t;
1877 attrs.offset_known_p = true;
1878 attrs.offset = 0;
1879 apply_bitpos = bitpos;
1880 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
1881 new_size = DECL_SIZE_UNIT (TREE_OPERAND (t, 1));
1884 /* If this is an array reference, look for an outer field reference. */
1885 else if (TREE_CODE (t) == ARRAY_REF)
1887 tree off_tree = size_zero_node;
1888 /* We can't modify t, because we use it at the end of the
1889 function. */
1890 tree t2 = t;
1894 tree index = TREE_OPERAND (t2, 1);
1895 tree low_bound = array_ref_low_bound (t2);
1896 tree unit_size = array_ref_element_size (t2);
1898 /* We assume all arrays have sizes that are a multiple of a byte.
1899 First subtract the lower bound, if any, in the type of the
1900 index, then convert to sizetype and multiply by the size of
1901 the array element. */
1902 if (! integer_zerop (low_bound))
1903 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
1904 index, low_bound);
1906 off_tree = size_binop (PLUS_EXPR,
1907 size_binop (MULT_EXPR,
1908 fold_convert (sizetype,
1909 index),
1910 unit_size),
1911 off_tree);
1912 t2 = TREE_OPERAND (t2, 0);
1914 while (TREE_CODE (t2) == ARRAY_REF);
1916 if (DECL_P (t2)
1917 || TREE_CODE (t2) == COMPONENT_REF)
1919 attrs.expr = t2;
1920 attrs.offset_known_p = false;
1921 if (tree_fits_uhwi_p (off_tree))
1923 attrs.offset_known_p = true;
1924 attrs.offset = tree_to_uhwi (off_tree);
1925 apply_bitpos = bitpos;
1928 /* Else do not record a MEM_EXPR. */
1931 /* If this is an indirect reference, record it. */
1932 else if (TREE_CODE (t) == MEM_REF
1933 || TREE_CODE (t) == TARGET_MEM_REF)
1935 attrs.expr = t;
1936 attrs.offset_known_p = true;
1937 attrs.offset = 0;
1938 apply_bitpos = bitpos;
1941 /* Compute the alignment. */
1942 unsigned int obj_align;
1943 unsigned HOST_WIDE_INT obj_bitpos;
1944 get_object_alignment_1 (t, &obj_align, &obj_bitpos);
1945 obj_bitpos = (obj_bitpos - bitpos) & (obj_align - 1);
1946 if (obj_bitpos != 0)
1947 obj_align = (obj_bitpos & -obj_bitpos);
1948 attrs.align = MAX (attrs.align, obj_align);
1951 if (tree_fits_uhwi_p (new_size))
1953 attrs.size_known_p = true;
1954 attrs.size = tree_to_uhwi (new_size);
1957 /* If we modified OFFSET based on T, then subtract the outstanding
1958 bit position offset. Similarly, increase the size of the accessed
1959 object to contain the negative offset. */
1960 if (apply_bitpos)
1962 gcc_assert (attrs.offset_known_p);
1963 attrs.offset -= apply_bitpos / BITS_PER_UNIT;
1964 if (attrs.size_known_p)
1965 attrs.size += apply_bitpos / BITS_PER_UNIT;
1968 /* Now set the attributes we computed above. */
1969 attrs.addrspace = as;
1970 set_mem_attrs (ref, &attrs);
1973 void
1974 set_mem_attributes (rtx ref, tree t, int objectp)
1976 set_mem_attributes_minus_bitpos (ref, t, objectp, 0);
1979 /* Set the alias set of MEM to SET. */
1981 void
1982 set_mem_alias_set (rtx mem, alias_set_type set)
1984 struct mem_attrs attrs;
1986 /* If the new and old alias sets don't conflict, something is wrong. */
1987 gcc_checking_assert (alias_sets_conflict_p (set, MEM_ALIAS_SET (mem)));
1988 attrs = *get_mem_attrs (mem);
1989 attrs.alias = set;
1990 set_mem_attrs (mem, &attrs);
1993 /* Set the address space of MEM to ADDRSPACE (target-defined). */
1995 void
1996 set_mem_addr_space (rtx mem, addr_space_t addrspace)
1998 struct mem_attrs attrs;
2000 attrs = *get_mem_attrs (mem);
2001 attrs.addrspace = addrspace;
2002 set_mem_attrs (mem, &attrs);
2005 /* Set the alignment of MEM to ALIGN bits. */
2007 void
2008 set_mem_align (rtx mem, unsigned int align)
2010 struct mem_attrs attrs;
2012 attrs = *get_mem_attrs (mem);
2013 attrs.align = align;
2014 set_mem_attrs (mem, &attrs);
2017 /* Set the expr for MEM to EXPR. */
2019 void
2020 set_mem_expr (rtx mem, tree expr)
2022 struct mem_attrs attrs;
2024 attrs = *get_mem_attrs (mem);
2025 attrs.expr = expr;
2026 set_mem_attrs (mem, &attrs);
2029 /* Set the offset of MEM to OFFSET. */
2031 void
2032 set_mem_offset (rtx mem, HOST_WIDE_INT offset)
2034 struct mem_attrs attrs;
2036 attrs = *get_mem_attrs (mem);
2037 attrs.offset_known_p = true;
2038 attrs.offset = offset;
2039 set_mem_attrs (mem, &attrs);
2042 /* Clear the offset of MEM. */
2044 void
2045 clear_mem_offset (rtx mem)
2047 struct mem_attrs attrs;
2049 attrs = *get_mem_attrs (mem);
2050 attrs.offset_known_p = false;
2051 set_mem_attrs (mem, &attrs);
2054 /* Set the size of MEM to SIZE. */
2056 void
2057 set_mem_size (rtx mem, HOST_WIDE_INT size)
2059 struct mem_attrs attrs;
2061 attrs = *get_mem_attrs (mem);
2062 attrs.size_known_p = true;
2063 attrs.size = size;
2064 set_mem_attrs (mem, &attrs);
2067 /* Clear the size of MEM. */
2069 void
2070 clear_mem_size (rtx mem)
2072 struct mem_attrs attrs;
2074 attrs = *get_mem_attrs (mem);
2075 attrs.size_known_p = false;
2076 set_mem_attrs (mem, &attrs);
2079 /* Return a memory reference like MEMREF, but with its mode changed to MODE
2080 and its address changed to ADDR. (VOIDmode means don't change the mode.
2081 NULL for ADDR means don't change the address.) VALIDATE is nonzero if the
2082 returned memory location is required to be valid. INPLACE is true if any
2083 changes can be made directly to MEMREF or false if MEMREF must be treated
2084 as immutable.
2086 The memory attributes are not changed. */
2088 static rtx
2089 change_address_1 (rtx memref, machine_mode mode, rtx addr, int validate,
2090 bool inplace)
2092 addr_space_t as;
2093 rtx new_rtx;
2095 gcc_assert (MEM_P (memref));
2096 as = MEM_ADDR_SPACE (memref);
2097 if (mode == VOIDmode)
2098 mode = GET_MODE (memref);
2099 if (addr == 0)
2100 addr = XEXP (memref, 0);
2101 if (mode == GET_MODE (memref) && addr == XEXP (memref, 0)
2102 && (!validate || memory_address_addr_space_p (mode, addr, as)))
2103 return memref;
2105 /* Don't validate address for LRA. LRA can make the address valid
2106 by itself in most efficient way. */
2107 if (validate && !lra_in_progress)
2109 if (reload_in_progress || reload_completed)
2110 gcc_assert (memory_address_addr_space_p (mode, addr, as));
2111 else
2112 addr = memory_address_addr_space (mode, addr, as);
2115 if (rtx_equal_p (addr, XEXP (memref, 0)) && mode == GET_MODE (memref))
2116 return memref;
2118 if (inplace)
2120 XEXP (memref, 0) = addr;
2121 return memref;
2124 new_rtx = gen_rtx_MEM (mode, addr);
2125 MEM_COPY_ATTRIBUTES (new_rtx, memref);
2126 return new_rtx;
2129 /* Like change_address_1 with VALIDATE nonzero, but we are not saying in what
2130 way we are changing MEMREF, so we only preserve the alias set. */
2133 change_address (rtx memref, machine_mode mode, rtx addr)
2135 rtx new_rtx = change_address_1 (memref, mode, addr, 1, false);
2136 machine_mode mmode = GET_MODE (new_rtx);
2137 struct mem_attrs attrs, *defattrs;
2139 attrs = *get_mem_attrs (memref);
2140 defattrs = mode_mem_attrs[(int) mmode];
2141 attrs.expr = NULL_TREE;
2142 attrs.offset_known_p = false;
2143 attrs.size_known_p = defattrs->size_known_p;
2144 attrs.size = defattrs->size;
2145 attrs.align = defattrs->align;
2147 /* If there are no changes, just return the original memory reference. */
2148 if (new_rtx == memref)
2150 if (mem_attrs_eq_p (get_mem_attrs (memref), &attrs))
2151 return new_rtx;
2153 new_rtx = gen_rtx_MEM (mmode, XEXP (memref, 0));
2154 MEM_COPY_ATTRIBUTES (new_rtx, memref);
2157 set_mem_attrs (new_rtx, &attrs);
2158 return new_rtx;
2161 /* Return a memory reference like MEMREF, but with its mode changed
2162 to MODE and its address offset by OFFSET bytes. If VALIDATE is
2163 nonzero, the memory address is forced to be valid.
2164 If ADJUST_ADDRESS is zero, OFFSET is only used to update MEM_ATTRS
2165 and the caller is responsible for adjusting MEMREF base register.
2166 If ADJUST_OBJECT is zero, the underlying object associated with the
2167 memory reference is left unchanged and the caller is responsible for
2168 dealing with it. Otherwise, if the new memory reference is outside
2169 the underlying object, even partially, then the object is dropped.
2170 SIZE, if nonzero, is the size of an access in cases where MODE
2171 has no inherent size. */
2174 adjust_address_1 (rtx memref, machine_mode mode, HOST_WIDE_INT offset,
2175 int validate, int adjust_address, int adjust_object,
2176 HOST_WIDE_INT size)
2178 rtx addr = XEXP (memref, 0);
2179 rtx new_rtx;
2180 machine_mode address_mode;
2181 int pbits;
2182 struct mem_attrs attrs = *get_mem_attrs (memref), *defattrs;
2183 unsigned HOST_WIDE_INT max_align;
2184 #ifdef POINTERS_EXTEND_UNSIGNED
2185 machine_mode pointer_mode
2186 = targetm.addr_space.pointer_mode (attrs.addrspace);
2187 #endif
2189 /* VOIDmode means no mode change for change_address_1. */
2190 if (mode == VOIDmode)
2191 mode = GET_MODE (memref);
2193 /* Take the size of non-BLKmode accesses from the mode. */
2194 defattrs = mode_mem_attrs[(int) mode];
2195 if (defattrs->size_known_p)
2196 size = defattrs->size;
2198 /* If there are no changes, just return the original memory reference. */
2199 if (mode == GET_MODE (memref) && !offset
2200 && (size == 0 || (attrs.size_known_p && attrs.size == size))
2201 && (!validate || memory_address_addr_space_p (mode, addr,
2202 attrs.addrspace)))
2203 return memref;
2205 /* ??? Prefer to create garbage instead of creating shared rtl.
2206 This may happen even if offset is nonzero -- consider
2207 (plus (plus reg reg) const_int) -- so do this always. */
2208 addr = copy_rtx (addr);
2210 /* Convert a possibly large offset to a signed value within the
2211 range of the target address space. */
2212 address_mode = get_address_mode (memref);
2213 pbits = GET_MODE_BITSIZE (address_mode);
2214 if (HOST_BITS_PER_WIDE_INT > pbits)
2216 int shift = HOST_BITS_PER_WIDE_INT - pbits;
2217 offset = (((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) offset << shift))
2218 >> shift);
2221 if (adjust_address)
2223 /* If MEMREF is a LO_SUM and the offset is within the alignment of the
2224 object, we can merge it into the LO_SUM. */
2225 if (GET_MODE (memref) != BLKmode && GET_CODE (addr) == LO_SUM
2226 && offset >= 0
2227 && (unsigned HOST_WIDE_INT) offset
2228 < GET_MODE_ALIGNMENT (GET_MODE (memref)) / BITS_PER_UNIT)
2229 addr = gen_rtx_LO_SUM (address_mode, XEXP (addr, 0),
2230 plus_constant (address_mode,
2231 XEXP (addr, 1), offset));
2232 #ifdef POINTERS_EXTEND_UNSIGNED
2233 /* If MEMREF is a ZERO_EXTEND from pointer_mode and the offset is valid
2234 in that mode, we merge it into the ZERO_EXTEND. We take advantage of
2235 the fact that pointers are not allowed to overflow. */
2236 else if (POINTERS_EXTEND_UNSIGNED > 0
2237 && GET_CODE (addr) == ZERO_EXTEND
2238 && GET_MODE (XEXP (addr, 0)) == pointer_mode
2239 && trunc_int_for_mode (offset, pointer_mode) == offset)
2240 addr = gen_rtx_ZERO_EXTEND (address_mode,
2241 plus_constant (pointer_mode,
2242 XEXP (addr, 0), offset));
2243 #endif
2244 else
2245 addr = plus_constant (address_mode, addr, offset);
2248 new_rtx = change_address_1 (memref, mode, addr, validate, false);
2250 /* If the address is a REG, change_address_1 rightfully returns memref,
2251 but this would destroy memref's MEM_ATTRS. */
2252 if (new_rtx == memref && offset != 0)
2253 new_rtx = copy_rtx (new_rtx);
2255 /* Conservatively drop the object if we don't know where we start from. */
2256 if (adjust_object && (!attrs.offset_known_p || !attrs.size_known_p))
2258 attrs.expr = NULL_TREE;
2259 attrs.alias = 0;
2262 /* Compute the new values of the memory attributes due to this adjustment.
2263 We add the offsets and update the alignment. */
2264 if (attrs.offset_known_p)
2266 attrs.offset += offset;
2268 /* Drop the object if the new left end is not within its bounds. */
2269 if (adjust_object && attrs.offset < 0)
2271 attrs.expr = NULL_TREE;
2272 attrs.alias = 0;
2276 /* Compute the new alignment by taking the MIN of the alignment and the
2277 lowest-order set bit in OFFSET, but don't change the alignment if OFFSET
2278 if zero. */
2279 if (offset != 0)
2281 max_align = (offset & -offset) * BITS_PER_UNIT;
2282 attrs.align = MIN (attrs.align, max_align);
2285 if (size)
2287 /* Drop the object if the new right end is not within its bounds. */
2288 if (adjust_object && (offset + size) > attrs.size)
2290 attrs.expr = NULL_TREE;
2291 attrs.alias = 0;
2293 attrs.size_known_p = true;
2294 attrs.size = size;
2296 else if (attrs.size_known_p)
2298 gcc_assert (!adjust_object);
2299 attrs.size -= offset;
2300 /* ??? The store_by_pieces machinery generates negative sizes,
2301 so don't assert for that here. */
2304 set_mem_attrs (new_rtx, &attrs);
2306 return new_rtx;
2309 /* Return a memory reference like MEMREF, but with its mode changed
2310 to MODE and its address changed to ADDR, which is assumed to be
2311 MEMREF offset by OFFSET bytes. If VALIDATE is
2312 nonzero, the memory address is forced to be valid. */
2315 adjust_automodify_address_1 (rtx memref, machine_mode mode, rtx addr,
2316 HOST_WIDE_INT offset, int validate)
2318 memref = change_address_1 (memref, VOIDmode, addr, validate, false);
2319 return adjust_address_1 (memref, mode, offset, validate, 0, 0, 0);
2322 /* Return a memory reference like MEMREF, but whose address is changed by
2323 adding OFFSET, an RTX, to it. POW2 is the highest power of two factor
2324 known to be in OFFSET (possibly 1). */
2327 offset_address (rtx memref, rtx offset, unsigned HOST_WIDE_INT pow2)
2329 rtx new_rtx, addr = XEXP (memref, 0);
2330 machine_mode address_mode;
2331 struct mem_attrs attrs, *defattrs;
2333 attrs = *get_mem_attrs (memref);
2334 address_mode = get_address_mode (memref);
2335 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2337 /* At this point we don't know _why_ the address is invalid. It
2338 could have secondary memory references, multiplies or anything.
2340 However, if we did go and rearrange things, we can wind up not
2341 being able to recognize the magic around pic_offset_table_rtx.
2342 This stuff is fragile, and is yet another example of why it is
2343 bad to expose PIC machinery too early. */
2344 if (! memory_address_addr_space_p (GET_MODE (memref), new_rtx,
2345 attrs.addrspace)
2346 && GET_CODE (addr) == PLUS
2347 && XEXP (addr, 0) == pic_offset_table_rtx)
2349 addr = force_reg (GET_MODE (addr), addr);
2350 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2353 update_temp_slot_address (XEXP (memref, 0), new_rtx);
2354 new_rtx = change_address_1 (memref, VOIDmode, new_rtx, 1, false);
2356 /* If there are no changes, just return the original memory reference. */
2357 if (new_rtx == memref)
2358 return new_rtx;
2360 /* Update the alignment to reflect the offset. Reset the offset, which
2361 we don't know. */
2362 defattrs = mode_mem_attrs[(int) GET_MODE (new_rtx)];
2363 attrs.offset_known_p = false;
2364 attrs.size_known_p = defattrs->size_known_p;
2365 attrs.size = defattrs->size;
2366 attrs.align = MIN (attrs.align, pow2 * BITS_PER_UNIT);
2367 set_mem_attrs (new_rtx, &attrs);
2368 return new_rtx;
2371 /* Return a memory reference like MEMREF, but with its address changed to
2372 ADDR. The caller is asserting that the actual piece of memory pointed
2373 to is the same, just the form of the address is being changed, such as
2374 by putting something into a register. INPLACE is true if any changes
2375 can be made directly to MEMREF or false if MEMREF must be treated as
2376 immutable. */
2379 replace_equiv_address (rtx memref, rtx addr, bool inplace)
2381 /* change_address_1 copies the memory attribute structure without change
2382 and that's exactly what we want here. */
2383 update_temp_slot_address (XEXP (memref, 0), addr);
2384 return change_address_1 (memref, VOIDmode, addr, 1, inplace);
2387 /* Likewise, but the reference is not required to be valid. */
2390 replace_equiv_address_nv (rtx memref, rtx addr, bool inplace)
2392 return change_address_1 (memref, VOIDmode, addr, 0, inplace);
2395 /* Return a memory reference like MEMREF, but with its mode widened to
2396 MODE and offset by OFFSET. This would be used by targets that e.g.
2397 cannot issue QImode memory operations and have to use SImode memory
2398 operations plus masking logic. */
2401 widen_memory_access (rtx memref, machine_mode mode, HOST_WIDE_INT offset)
2403 rtx new_rtx = adjust_address_1 (memref, mode, offset, 1, 1, 0, 0);
2404 struct mem_attrs attrs;
2405 unsigned int size = GET_MODE_SIZE (mode);
2407 /* If there are no changes, just return the original memory reference. */
2408 if (new_rtx == memref)
2409 return new_rtx;
2411 attrs = *get_mem_attrs (new_rtx);
2413 /* If we don't know what offset we were at within the expression, then
2414 we can't know if we've overstepped the bounds. */
2415 if (! attrs.offset_known_p)
2416 attrs.expr = NULL_TREE;
2418 while (attrs.expr)
2420 if (TREE_CODE (attrs.expr) == COMPONENT_REF)
2422 tree field = TREE_OPERAND (attrs.expr, 1);
2423 tree offset = component_ref_field_offset (attrs.expr);
2425 if (! DECL_SIZE_UNIT (field))
2427 attrs.expr = NULL_TREE;
2428 break;
2431 /* Is the field at least as large as the access? If so, ok,
2432 otherwise strip back to the containing structure. */
2433 if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
2434 && compare_tree_int (DECL_SIZE_UNIT (field), size) >= 0
2435 && attrs.offset >= 0)
2436 break;
2438 if (! tree_fits_uhwi_p (offset))
2440 attrs.expr = NULL_TREE;
2441 break;
2444 attrs.expr = TREE_OPERAND (attrs.expr, 0);
2445 attrs.offset += tree_to_uhwi (offset);
2446 attrs.offset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
2447 / BITS_PER_UNIT);
2449 /* Similarly for the decl. */
2450 else if (DECL_P (attrs.expr)
2451 && DECL_SIZE_UNIT (attrs.expr)
2452 && TREE_CODE (DECL_SIZE_UNIT (attrs.expr)) == INTEGER_CST
2453 && compare_tree_int (DECL_SIZE_UNIT (attrs.expr), size) >= 0
2454 && (! attrs.offset_known_p || attrs.offset >= 0))
2455 break;
2456 else
2458 /* The widened memory access overflows the expression, which means
2459 that it could alias another expression. Zap it. */
2460 attrs.expr = NULL_TREE;
2461 break;
2465 if (! attrs.expr)
2466 attrs.offset_known_p = false;
2468 /* The widened memory may alias other stuff, so zap the alias set. */
2469 /* ??? Maybe use get_alias_set on any remaining expression. */
2470 attrs.alias = 0;
2471 attrs.size_known_p = true;
2472 attrs.size = size;
2473 set_mem_attrs (new_rtx, &attrs);
2474 return new_rtx;
2477 /* A fake decl that is used as the MEM_EXPR of spill slots. */
2478 static GTY(()) tree spill_slot_decl;
2480 tree
2481 get_spill_slot_decl (bool force_build_p)
2483 tree d = spill_slot_decl;
2484 rtx rd;
2485 struct mem_attrs attrs;
2487 if (d || !force_build_p)
2488 return d;
2490 d = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2491 VAR_DECL, get_identifier ("%sfp"), void_type_node);
2492 DECL_ARTIFICIAL (d) = 1;
2493 DECL_IGNORED_P (d) = 1;
2494 TREE_USED (d) = 1;
2495 spill_slot_decl = d;
2497 rd = gen_rtx_MEM (BLKmode, frame_pointer_rtx);
2498 MEM_NOTRAP_P (rd) = 1;
2499 attrs = *mode_mem_attrs[(int) BLKmode];
2500 attrs.alias = new_alias_set ();
2501 attrs.expr = d;
2502 set_mem_attrs (rd, &attrs);
2503 SET_DECL_RTL (d, rd);
2505 return d;
2508 /* Given MEM, a result from assign_stack_local, fill in the memory
2509 attributes as appropriate for a register allocator spill slot.
2510 These slots are not aliasable by other memory. We arrange for
2511 them all to use a single MEM_EXPR, so that the aliasing code can
2512 work properly in the case of shared spill slots. */
2514 void
2515 set_mem_attrs_for_spill (rtx mem)
2517 struct mem_attrs attrs;
2518 rtx addr;
2520 attrs = *get_mem_attrs (mem);
2521 attrs.expr = get_spill_slot_decl (true);
2522 attrs.alias = MEM_ALIAS_SET (DECL_RTL (attrs.expr));
2523 attrs.addrspace = ADDR_SPACE_GENERIC;
2525 /* We expect the incoming memory to be of the form:
2526 (mem:MODE (plus (reg sfp) (const_int offset)))
2527 with perhaps the plus missing for offset = 0. */
2528 addr = XEXP (mem, 0);
2529 attrs.offset_known_p = true;
2530 attrs.offset = 0;
2531 if (GET_CODE (addr) == PLUS
2532 && CONST_INT_P (XEXP (addr, 1)))
2533 attrs.offset = INTVAL (XEXP (addr, 1));
2535 set_mem_attrs (mem, &attrs);
2536 MEM_NOTRAP_P (mem) = 1;
2539 /* Return a newly created CODE_LABEL rtx with a unique label number. */
2541 rtx_code_label *
2542 gen_label_rtx (void)
2544 return as_a <rtx_code_label *> (
2545 gen_rtx_CODE_LABEL (VOIDmode, NULL_RTX, NULL_RTX,
2546 NULL, label_num++, NULL));
2549 /* For procedure integration. */
2551 /* Install new pointers to the first and last insns in the chain.
2552 Also, set cur_insn_uid to one higher than the last in use.
2553 Used for an inline-procedure after copying the insn chain. */
2555 void
2556 set_new_first_and_last_insn (rtx_insn *first, rtx_insn *last)
2558 rtx_insn *insn;
2560 set_first_insn (first);
2561 set_last_insn (last);
2562 cur_insn_uid = 0;
2564 if (MIN_NONDEBUG_INSN_UID || MAY_HAVE_DEBUG_INSNS)
2566 int debug_count = 0;
2568 cur_insn_uid = MIN_NONDEBUG_INSN_UID - 1;
2569 cur_debug_insn_uid = 0;
2571 for (insn = first; insn; insn = NEXT_INSN (insn))
2572 if (INSN_UID (insn) < MIN_NONDEBUG_INSN_UID)
2573 cur_debug_insn_uid = MAX (cur_debug_insn_uid, INSN_UID (insn));
2574 else
2576 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2577 if (DEBUG_INSN_P (insn))
2578 debug_count++;
2581 if (debug_count)
2582 cur_debug_insn_uid = MIN_NONDEBUG_INSN_UID + debug_count;
2583 else
2584 cur_debug_insn_uid++;
2586 else
2587 for (insn = first; insn; insn = NEXT_INSN (insn))
2588 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2590 cur_insn_uid++;
2593 /* Go through all the RTL insn bodies and copy any invalid shared
2594 structure. This routine should only be called once. */
2596 static void
2597 unshare_all_rtl_1 (rtx_insn *insn)
2599 /* Unshare just about everything else. */
2600 unshare_all_rtl_in_chain (insn);
2602 /* Make sure the addresses of stack slots found outside the insn chain
2603 (such as, in DECL_RTL of a variable) are not shared
2604 with the insn chain.
2606 This special care is necessary when the stack slot MEM does not
2607 actually appear in the insn chain. If it does appear, its address
2608 is unshared from all else at that point. */
2609 stack_slot_list = safe_as_a <rtx_expr_list *> (
2610 copy_rtx_if_shared (stack_slot_list));
2613 /* Go through all the RTL insn bodies and copy any invalid shared
2614 structure, again. This is a fairly expensive thing to do so it
2615 should be done sparingly. */
2617 void
2618 unshare_all_rtl_again (rtx_insn *insn)
2620 rtx_insn *p;
2621 tree decl;
2623 for (p = insn; p; p = NEXT_INSN (p))
2624 if (INSN_P (p))
2626 reset_used_flags (PATTERN (p));
2627 reset_used_flags (REG_NOTES (p));
2628 if (CALL_P (p))
2629 reset_used_flags (CALL_INSN_FUNCTION_USAGE (p));
2632 /* Make sure that virtual stack slots are not shared. */
2633 set_used_decls (DECL_INITIAL (cfun->decl));
2635 /* Make sure that virtual parameters are not shared. */
2636 for (decl = DECL_ARGUMENTS (cfun->decl); decl; decl = DECL_CHAIN (decl))
2637 set_used_flags (DECL_RTL (decl));
2639 reset_used_flags (stack_slot_list);
2641 unshare_all_rtl_1 (insn);
2644 unsigned int
2645 unshare_all_rtl (void)
2647 unshare_all_rtl_1 (get_insns ());
2648 return 0;
2652 /* Check that ORIG is not marked when it should not be and mark ORIG as in use,
2653 Recursively does the same for subexpressions. */
2655 static void
2656 verify_rtx_sharing (rtx orig, rtx insn)
2658 rtx x = orig;
2659 int i;
2660 enum rtx_code code;
2661 const char *format_ptr;
2663 if (x == 0)
2664 return;
2666 code = GET_CODE (x);
2668 /* These types may be freely shared. */
2670 switch (code)
2672 case REG:
2673 case DEBUG_EXPR:
2674 case VALUE:
2675 CASE_CONST_ANY:
2676 case SYMBOL_REF:
2677 case LABEL_REF:
2678 case CODE_LABEL:
2679 case PC:
2680 case CC0:
2681 case RETURN:
2682 case SIMPLE_RETURN:
2683 case SCRATCH:
2684 /* SCRATCH must be shared because they represent distinct values. */
2685 return;
2686 case CLOBBER:
2687 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2688 clobbers or clobbers of hard registers that originated as pseudos.
2689 This is needed to allow safe register renaming. */
2690 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2691 && ORIGINAL_REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 0)))
2692 return;
2693 break;
2695 case CONST:
2696 if (shared_const_p (orig))
2697 return;
2698 break;
2700 case MEM:
2701 /* A MEM is allowed to be shared if its address is constant. */
2702 if (CONSTANT_ADDRESS_P (XEXP (x, 0))
2703 || reload_completed || reload_in_progress)
2704 return;
2706 break;
2708 default:
2709 break;
2712 /* This rtx may not be shared. If it has already been seen,
2713 replace it with a copy of itself. */
2714 #ifdef ENABLE_CHECKING
2715 if (RTX_FLAG (x, used))
2717 error ("invalid rtl sharing found in the insn");
2718 debug_rtx (insn);
2719 error ("shared rtx");
2720 debug_rtx (x);
2721 internal_error ("internal consistency failure");
2723 #endif
2724 gcc_assert (!RTX_FLAG (x, used));
2726 RTX_FLAG (x, used) = 1;
2728 /* Now scan the subexpressions recursively. */
2730 format_ptr = GET_RTX_FORMAT (code);
2732 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2734 switch (*format_ptr++)
2736 case 'e':
2737 verify_rtx_sharing (XEXP (x, i), insn);
2738 break;
2740 case 'E':
2741 if (XVEC (x, i) != NULL)
2743 int j;
2744 int len = XVECLEN (x, i);
2746 for (j = 0; j < len; j++)
2748 /* We allow sharing of ASM_OPERANDS inside single
2749 instruction. */
2750 if (j && GET_CODE (XVECEXP (x, i, j)) == SET
2751 && (GET_CODE (SET_SRC (XVECEXP (x, i, j)))
2752 == ASM_OPERANDS))
2753 verify_rtx_sharing (SET_DEST (XVECEXP (x, i, j)), insn);
2754 else
2755 verify_rtx_sharing (XVECEXP (x, i, j), insn);
2758 break;
2761 return;
2764 /* Reset used-flags for INSN. */
2766 static void
2767 reset_insn_used_flags (rtx insn)
2769 gcc_assert (INSN_P (insn));
2770 reset_used_flags (PATTERN (insn));
2771 reset_used_flags (REG_NOTES (insn));
2772 if (CALL_P (insn))
2773 reset_used_flags (CALL_INSN_FUNCTION_USAGE (insn));
2776 /* Go through all the RTL insn bodies and clear all the USED bits. */
2778 static void
2779 reset_all_used_flags (void)
2781 rtx_insn *p;
2783 for (p = get_insns (); p; p = NEXT_INSN (p))
2784 if (INSN_P (p))
2786 rtx pat = PATTERN (p);
2787 if (GET_CODE (pat) != SEQUENCE)
2788 reset_insn_used_flags (p);
2789 else
2791 gcc_assert (REG_NOTES (p) == NULL);
2792 for (int i = 0; i < XVECLEN (pat, 0); i++)
2794 rtx insn = XVECEXP (pat, 0, i);
2795 if (INSN_P (insn))
2796 reset_insn_used_flags (insn);
2802 /* Verify sharing in INSN. */
2804 static void
2805 verify_insn_sharing (rtx insn)
2807 gcc_assert (INSN_P (insn));
2808 reset_used_flags (PATTERN (insn));
2809 reset_used_flags (REG_NOTES (insn));
2810 if (CALL_P (insn))
2811 reset_used_flags (CALL_INSN_FUNCTION_USAGE (insn));
2814 /* Go through all the RTL insn bodies and check that there is no unexpected
2815 sharing in between the subexpressions. */
2817 DEBUG_FUNCTION void
2818 verify_rtl_sharing (void)
2820 rtx_insn *p;
2822 timevar_push (TV_VERIFY_RTL_SHARING);
2824 reset_all_used_flags ();
2826 for (p = get_insns (); p; p = NEXT_INSN (p))
2827 if (INSN_P (p))
2829 rtx pat = PATTERN (p);
2830 if (GET_CODE (pat) != SEQUENCE)
2831 verify_insn_sharing (p);
2832 else
2833 for (int i = 0; i < XVECLEN (pat, 0); i++)
2835 rtx insn = XVECEXP (pat, 0, i);
2836 if (INSN_P (insn))
2837 verify_insn_sharing (insn);
2841 reset_all_used_flags ();
2843 timevar_pop (TV_VERIFY_RTL_SHARING);
2846 /* Go through all the RTL insn bodies and copy any invalid shared structure.
2847 Assumes the mark bits are cleared at entry. */
2849 void
2850 unshare_all_rtl_in_chain (rtx_insn *insn)
2852 for (; insn; insn = NEXT_INSN (insn))
2853 if (INSN_P (insn))
2855 PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
2856 REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
2857 if (CALL_P (insn))
2858 CALL_INSN_FUNCTION_USAGE (insn)
2859 = copy_rtx_if_shared (CALL_INSN_FUNCTION_USAGE (insn));
2863 /* Go through all virtual stack slots of a function and mark them as
2864 shared. We never replace the DECL_RTLs themselves with a copy,
2865 but expressions mentioned into a DECL_RTL cannot be shared with
2866 expressions in the instruction stream.
2868 Note that reload may convert pseudo registers into memories in-place.
2869 Pseudo registers are always shared, but MEMs never are. Thus if we
2870 reset the used flags on MEMs in the instruction stream, we must set
2871 them again on MEMs that appear in DECL_RTLs. */
2873 static void
2874 set_used_decls (tree blk)
2876 tree t;
2878 /* Mark decls. */
2879 for (t = BLOCK_VARS (blk); t; t = DECL_CHAIN (t))
2880 if (DECL_RTL_SET_P (t))
2881 set_used_flags (DECL_RTL (t));
2883 /* Now process sub-blocks. */
2884 for (t = BLOCK_SUBBLOCKS (blk); t; t = BLOCK_CHAIN (t))
2885 set_used_decls (t);
2888 /* Mark ORIG as in use, and return a copy of it if it was already in use.
2889 Recursively does the same for subexpressions. Uses
2890 copy_rtx_if_shared_1 to reduce stack space. */
2893 copy_rtx_if_shared (rtx orig)
2895 copy_rtx_if_shared_1 (&orig);
2896 return orig;
2899 /* Mark *ORIG1 as in use, and set it to a copy of it if it was already in
2900 use. Recursively does the same for subexpressions. */
2902 static void
2903 copy_rtx_if_shared_1 (rtx *orig1)
2905 rtx x;
2906 int i;
2907 enum rtx_code code;
2908 rtx *last_ptr;
2909 const char *format_ptr;
2910 int copied = 0;
2911 int length;
2913 /* Repeat is used to turn tail-recursion into iteration. */
2914 repeat:
2915 x = *orig1;
2917 if (x == 0)
2918 return;
2920 code = GET_CODE (x);
2922 /* These types may be freely shared. */
2924 switch (code)
2926 case REG:
2927 case DEBUG_EXPR:
2928 case VALUE:
2929 CASE_CONST_ANY:
2930 case SYMBOL_REF:
2931 case LABEL_REF:
2932 case CODE_LABEL:
2933 case PC:
2934 case CC0:
2935 case RETURN:
2936 case SIMPLE_RETURN:
2937 case SCRATCH:
2938 /* SCRATCH must be shared because they represent distinct values. */
2939 return;
2940 case CLOBBER:
2941 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2942 clobbers or clobbers of hard registers that originated as pseudos.
2943 This is needed to allow safe register renaming. */
2944 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2945 && ORIGINAL_REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 0)))
2946 return;
2947 break;
2949 case CONST:
2950 if (shared_const_p (x))
2951 return;
2952 break;
2954 case DEBUG_INSN:
2955 case INSN:
2956 case JUMP_INSN:
2957 case CALL_INSN:
2958 case NOTE:
2959 case BARRIER:
2960 /* The chain of insns is not being copied. */
2961 return;
2963 default:
2964 break;
2967 /* This rtx may not be shared. If it has already been seen,
2968 replace it with a copy of itself. */
2970 if (RTX_FLAG (x, used))
2972 x = shallow_copy_rtx (x);
2973 copied = 1;
2975 RTX_FLAG (x, used) = 1;
2977 /* Now scan the subexpressions recursively.
2978 We can store any replaced subexpressions directly into X
2979 since we know X is not shared! Any vectors in X
2980 must be copied if X was copied. */
2982 format_ptr = GET_RTX_FORMAT (code);
2983 length = GET_RTX_LENGTH (code);
2984 last_ptr = NULL;
2986 for (i = 0; i < length; i++)
2988 switch (*format_ptr++)
2990 case 'e':
2991 if (last_ptr)
2992 copy_rtx_if_shared_1 (last_ptr);
2993 last_ptr = &XEXP (x, i);
2994 break;
2996 case 'E':
2997 if (XVEC (x, i) != NULL)
2999 int j;
3000 int len = XVECLEN (x, i);
3002 /* Copy the vector iff I copied the rtx and the length
3003 is nonzero. */
3004 if (copied && len > 0)
3005 XVEC (x, i) = gen_rtvec_v (len, XVEC (x, i)->elem);
3007 /* Call recursively on all inside the vector. */
3008 for (j = 0; j < len; j++)
3010 if (last_ptr)
3011 copy_rtx_if_shared_1 (last_ptr);
3012 last_ptr = &XVECEXP (x, i, j);
3015 break;
3018 *orig1 = x;
3019 if (last_ptr)
3021 orig1 = last_ptr;
3022 goto repeat;
3024 return;
3027 /* Set the USED bit in X and its non-shareable subparts to FLAG. */
3029 static void
3030 mark_used_flags (rtx x, int flag)
3032 int i, j;
3033 enum rtx_code code;
3034 const char *format_ptr;
3035 int length;
3037 /* Repeat is used to turn tail-recursion into iteration. */
3038 repeat:
3039 if (x == 0)
3040 return;
3042 code = GET_CODE (x);
3044 /* These types may be freely shared so we needn't do any resetting
3045 for them. */
3047 switch (code)
3049 case REG:
3050 case DEBUG_EXPR:
3051 case VALUE:
3052 CASE_CONST_ANY:
3053 case SYMBOL_REF:
3054 case CODE_LABEL:
3055 case PC:
3056 case CC0:
3057 case RETURN:
3058 case SIMPLE_RETURN:
3059 return;
3061 case DEBUG_INSN:
3062 case INSN:
3063 case JUMP_INSN:
3064 case CALL_INSN:
3065 case NOTE:
3066 case LABEL_REF:
3067 case BARRIER:
3068 /* The chain of insns is not being copied. */
3069 return;
3071 default:
3072 break;
3075 RTX_FLAG (x, used) = flag;
3077 format_ptr = GET_RTX_FORMAT (code);
3078 length = GET_RTX_LENGTH (code);
3080 for (i = 0; i < length; i++)
3082 switch (*format_ptr++)
3084 case 'e':
3085 if (i == length-1)
3087 x = XEXP (x, i);
3088 goto repeat;
3090 mark_used_flags (XEXP (x, i), flag);
3091 break;
3093 case 'E':
3094 for (j = 0; j < XVECLEN (x, i); j++)
3095 mark_used_flags (XVECEXP (x, i, j), flag);
3096 break;
3101 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
3102 to look for shared sub-parts. */
3104 void
3105 reset_used_flags (rtx x)
3107 mark_used_flags (x, 0);
3110 /* Set all the USED bits in X to allow copy_rtx_if_shared to be used
3111 to look for shared sub-parts. */
3113 void
3114 set_used_flags (rtx x)
3116 mark_used_flags (x, 1);
3119 /* Copy X if necessary so that it won't be altered by changes in OTHER.
3120 Return X or the rtx for the pseudo reg the value of X was copied into.
3121 OTHER must be valid as a SET_DEST. */
3124 make_safe_from (rtx x, rtx other)
3126 while (1)
3127 switch (GET_CODE (other))
3129 case SUBREG:
3130 other = SUBREG_REG (other);
3131 break;
3132 case STRICT_LOW_PART:
3133 case SIGN_EXTEND:
3134 case ZERO_EXTEND:
3135 other = XEXP (other, 0);
3136 break;
3137 default:
3138 goto done;
3140 done:
3141 if ((MEM_P (other)
3142 && ! CONSTANT_P (x)
3143 && !REG_P (x)
3144 && GET_CODE (x) != SUBREG)
3145 || (REG_P (other)
3146 && (REGNO (other) < FIRST_PSEUDO_REGISTER
3147 || reg_mentioned_p (other, x))))
3149 rtx temp = gen_reg_rtx (GET_MODE (x));
3150 emit_move_insn (temp, x);
3151 return temp;
3153 return x;
3156 /* Emission of insns (adding them to the doubly-linked list). */
3158 /* Return the last insn emitted, even if it is in a sequence now pushed. */
3160 rtx_insn *
3161 get_last_insn_anywhere (void)
3163 struct sequence_stack *stack;
3164 if (get_last_insn ())
3165 return get_last_insn ();
3166 for (stack = seq_stack; stack; stack = stack->next)
3167 if (stack->last != 0)
3168 return stack->last;
3169 return 0;
3172 /* Return the first nonnote insn emitted in current sequence or current
3173 function. This routine looks inside SEQUENCEs. */
3175 rtx_insn *
3176 get_first_nonnote_insn (void)
3178 rtx_insn *insn = get_insns ();
3180 if (insn)
3182 if (NOTE_P (insn))
3183 for (insn = next_insn (insn);
3184 insn && NOTE_P (insn);
3185 insn = next_insn (insn))
3186 continue;
3187 else
3189 if (NONJUMP_INSN_P (insn)
3190 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3191 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3195 return insn;
3198 /* Return the last nonnote insn emitted in current sequence or current
3199 function. This routine looks inside SEQUENCEs. */
3201 rtx_insn *
3202 get_last_nonnote_insn (void)
3204 rtx_insn *insn = get_last_insn ();
3206 if (insn)
3208 if (NOTE_P (insn))
3209 for (insn = previous_insn (insn);
3210 insn && NOTE_P (insn);
3211 insn = previous_insn (insn))
3212 continue;
3213 else
3215 if (NONJUMP_INSN_P (insn))
3216 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3217 insn = seq->insn (seq->len () - 1);
3221 return insn;
3224 /* Return the number of actual (non-debug) insns emitted in this
3225 function. */
3228 get_max_insn_count (void)
3230 int n = cur_insn_uid;
3232 /* The table size must be stable across -g, to avoid codegen
3233 differences due to debug insns, and not be affected by
3234 -fmin-insn-uid, to avoid excessive table size and to simplify
3235 debugging of -fcompare-debug failures. */
3236 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3237 n -= cur_debug_insn_uid;
3238 else
3239 n -= MIN_NONDEBUG_INSN_UID;
3241 return n;
3245 /* Return the next insn. If it is a SEQUENCE, return the first insn
3246 of the sequence. */
3248 rtx_insn *
3249 next_insn (rtx_insn *insn)
3251 if (insn)
3253 insn = NEXT_INSN (insn);
3254 if (insn && NONJUMP_INSN_P (insn)
3255 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3256 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3259 return insn;
3262 /* Return the previous insn. If it is a SEQUENCE, return the last insn
3263 of the sequence. */
3265 rtx_insn *
3266 previous_insn (rtx_insn *insn)
3268 if (insn)
3270 insn = PREV_INSN (insn);
3271 if (insn && NONJUMP_INSN_P (insn))
3272 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3273 insn = seq->insn (seq->len () - 1);
3276 return insn;
3279 /* Return the next insn after INSN that is not a NOTE. This routine does not
3280 look inside SEQUENCEs. */
3282 rtx_insn *
3283 next_nonnote_insn (rtx uncast_insn)
3285 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3286 while (insn)
3288 insn = NEXT_INSN (insn);
3289 if (insn == 0 || !NOTE_P (insn))
3290 break;
3293 return insn;
3296 /* Return the next insn after INSN that is not a NOTE, but stop the
3297 search before we enter another basic block. This routine does not
3298 look inside SEQUENCEs. */
3300 rtx_insn *
3301 next_nonnote_insn_bb (rtx_insn *insn)
3303 while (insn)
3305 insn = NEXT_INSN (insn);
3306 if (insn == 0 || !NOTE_P (insn))
3307 break;
3308 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3309 return NULL;
3312 return insn;
3315 /* Return the previous insn before INSN that is not a NOTE. This routine does
3316 not look inside SEQUENCEs. */
3318 rtx_insn *
3319 prev_nonnote_insn (rtx uncast_insn)
3321 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3323 while (insn)
3325 insn = PREV_INSN (insn);
3326 if (insn == 0 || !NOTE_P (insn))
3327 break;
3330 return insn;
3333 /* Return the previous insn before INSN that is not a NOTE, but stop
3334 the search before we enter another basic block. This routine does
3335 not look inside SEQUENCEs. */
3337 rtx_insn *
3338 prev_nonnote_insn_bb (rtx uncast_insn)
3340 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3342 while (insn)
3344 insn = PREV_INSN (insn);
3345 if (insn == 0 || !NOTE_P (insn))
3346 break;
3347 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3348 return NULL;
3351 return insn;
3354 /* Return the next insn after INSN that is not a DEBUG_INSN. This
3355 routine does not look inside SEQUENCEs. */
3357 rtx_insn *
3358 next_nondebug_insn (rtx uncast_insn)
3360 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3362 while (insn)
3364 insn = NEXT_INSN (insn);
3365 if (insn == 0 || !DEBUG_INSN_P (insn))
3366 break;
3369 return insn;
3372 /* Return the previous insn before INSN that is not a DEBUG_INSN.
3373 This routine does not look inside SEQUENCEs. */
3375 rtx_insn *
3376 prev_nondebug_insn (rtx uncast_insn)
3378 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3380 while (insn)
3382 insn = PREV_INSN (insn);
3383 if (insn == 0 || !DEBUG_INSN_P (insn))
3384 break;
3387 return insn;
3390 /* Return the next insn after INSN that is not a NOTE nor DEBUG_INSN.
3391 This routine does not look inside SEQUENCEs. */
3393 rtx_insn *
3394 next_nonnote_nondebug_insn (rtx uncast_insn)
3396 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3398 while (insn)
3400 insn = NEXT_INSN (insn);
3401 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3402 break;
3405 return insn;
3408 /* Return the previous insn before INSN that is not a NOTE nor DEBUG_INSN.
3409 This routine does not look inside SEQUENCEs. */
3411 rtx_insn *
3412 prev_nonnote_nondebug_insn (rtx uncast_insn)
3414 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3416 while (insn)
3418 insn = PREV_INSN (insn);
3419 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3420 break;
3423 return insn;
3426 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
3427 or 0, if there is none. This routine does not look inside
3428 SEQUENCEs. */
3430 rtx_insn *
3431 next_real_insn (rtx uncast_insn)
3433 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3435 while (insn)
3437 insn = NEXT_INSN (insn);
3438 if (insn == 0 || INSN_P (insn))
3439 break;
3442 return insn;
3445 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
3446 or 0, if there is none. This routine does not look inside
3447 SEQUENCEs. */
3449 rtx_insn *
3450 prev_real_insn (rtx uncast_insn)
3452 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3454 while (insn)
3456 insn = PREV_INSN (insn);
3457 if (insn == 0 || INSN_P (insn))
3458 break;
3461 return insn;
3464 /* Return the last CALL_INSN in the current list, or 0 if there is none.
3465 This routine does not look inside SEQUENCEs. */
3467 rtx_call_insn *
3468 last_call_insn (void)
3470 rtx_insn *insn;
3472 for (insn = get_last_insn ();
3473 insn && !CALL_P (insn);
3474 insn = PREV_INSN (insn))
3477 return safe_as_a <rtx_call_insn *> (insn);
3480 /* Find the next insn after INSN that really does something. This routine
3481 does not look inside SEQUENCEs. After reload this also skips over
3482 standalone USE and CLOBBER insn. */
3485 active_insn_p (const_rtx insn)
3487 return (CALL_P (insn) || JUMP_P (insn)
3488 || JUMP_TABLE_DATA_P (insn) /* FIXME */
3489 || (NONJUMP_INSN_P (insn)
3490 && (! reload_completed
3491 || (GET_CODE (PATTERN (insn)) != USE
3492 && GET_CODE (PATTERN (insn)) != CLOBBER))));
3495 rtx_insn *
3496 next_active_insn (rtx uncast_insn)
3498 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3500 while (insn)
3502 insn = NEXT_INSN (insn);
3503 if (insn == 0 || active_insn_p (insn))
3504 break;
3507 return insn;
3510 /* Find the last insn before INSN that really does something. This routine
3511 does not look inside SEQUENCEs. After reload this also skips over
3512 standalone USE and CLOBBER insn. */
3514 rtx_insn *
3515 prev_active_insn (rtx uncast_insn)
3517 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3519 while (insn)
3521 insn = PREV_INSN (insn);
3522 if (insn == 0 || active_insn_p (insn))
3523 break;
3526 return insn;
3529 #ifdef HAVE_cc0
3530 /* Return the next insn that uses CC0 after INSN, which is assumed to
3531 set it. This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
3532 applied to the result of this function should yield INSN).
3534 Normally, this is simply the next insn. However, if a REG_CC_USER note
3535 is present, it contains the insn that uses CC0.
3537 Return 0 if we can't find the insn. */
3539 rtx_insn *
3540 next_cc0_user (rtx uncast_insn)
3542 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3544 rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
3546 if (note)
3547 return safe_as_a <rtx_insn *> (XEXP (note, 0));
3549 insn = next_nonnote_insn (insn);
3550 if (insn && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3551 insn = as_a <rtx_sequence *> (PATTERN (insn))->insn (0);
3553 if (insn && INSN_P (insn) && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
3554 return insn;
3556 return 0;
3559 /* Find the insn that set CC0 for INSN. Unless INSN has a REG_CC_SETTER
3560 note, it is the previous insn. */
3562 rtx_insn *
3563 prev_cc0_setter (rtx uncast_insn)
3565 rtx_insn *insn = safe_as_a <rtx_insn *> (uncast_insn);
3567 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3569 if (note)
3570 return safe_as_a <rtx_insn *> (XEXP (note, 0));
3572 insn = prev_nonnote_insn (insn);
3573 gcc_assert (sets_cc0_p (PATTERN (insn)));
3575 return insn;
3577 #endif
3579 #ifdef AUTO_INC_DEC
3580 /* Find a RTX_AUTOINC class rtx which matches DATA. */
3582 static int
3583 find_auto_inc (const_rtx x, const_rtx reg)
3585 subrtx_iterator::array_type array;
3586 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
3588 const_rtx x = *iter;
3589 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC
3590 && rtx_equal_p (reg, XEXP (x, 0)))
3591 return true;
3593 return false;
3595 #endif
3597 /* Increment the label uses for all labels present in rtx. */
3599 static void
3600 mark_label_nuses (rtx x)
3602 enum rtx_code code;
3603 int i, j;
3604 const char *fmt;
3606 code = GET_CODE (x);
3607 if (code == LABEL_REF && LABEL_P (LABEL_REF_LABEL (x)))
3608 LABEL_NUSES (LABEL_REF_LABEL (x))++;
3610 fmt = GET_RTX_FORMAT (code);
3611 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3613 if (fmt[i] == 'e')
3614 mark_label_nuses (XEXP (x, i));
3615 else if (fmt[i] == 'E')
3616 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3617 mark_label_nuses (XVECEXP (x, i, j));
3622 /* Try splitting insns that can be split for better scheduling.
3623 PAT is the pattern which might split.
3624 TRIAL is the insn providing PAT.
3625 LAST is nonzero if we should return the last insn of the sequence produced.
3627 If this routine succeeds in splitting, it returns the first or last
3628 replacement insn depending on the value of LAST. Otherwise, it
3629 returns TRIAL. If the insn to be returned can be split, it will be. */
3631 rtx_insn *
3632 try_split (rtx pat, rtx uncast_trial, int last)
3634 rtx_insn *trial = as_a <rtx_insn *> (uncast_trial);
3635 rtx_insn *before = PREV_INSN (trial);
3636 rtx_insn *after = NEXT_INSN (trial);
3637 rtx note;
3638 rtx_insn *seq, *tem;
3639 int probability;
3640 rtx_insn *insn_last, *insn;
3641 int njumps = 0;
3642 rtx call_insn = NULL_RTX;
3644 /* We're not good at redistributing frame information. */
3645 if (RTX_FRAME_RELATED_P (trial))
3646 return trial;
3648 if (any_condjump_p (trial)
3649 && (note = find_reg_note (trial, REG_BR_PROB, 0)))
3650 split_branch_probability = XINT (note, 0);
3651 probability = split_branch_probability;
3653 seq = safe_as_a <rtx_insn *> (split_insns (pat, trial));
3655 split_branch_probability = -1;
3657 if (!seq)
3658 return trial;
3660 /* Avoid infinite loop if any insn of the result matches
3661 the original pattern. */
3662 insn_last = seq;
3663 while (1)
3665 if (INSN_P (insn_last)
3666 && rtx_equal_p (PATTERN (insn_last), pat))
3667 return trial;
3668 if (!NEXT_INSN (insn_last))
3669 break;
3670 insn_last = NEXT_INSN (insn_last);
3673 /* We will be adding the new sequence to the function. The splitters
3674 may have introduced invalid RTL sharing, so unshare the sequence now. */
3675 unshare_all_rtl_in_chain (seq);
3677 /* Mark labels and copy flags. */
3678 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3680 if (JUMP_P (insn))
3682 if (JUMP_P (trial))
3683 CROSSING_JUMP_P (insn) = CROSSING_JUMP_P (trial);
3684 mark_jump_label (PATTERN (insn), insn, 0);
3685 njumps++;
3686 if (probability != -1
3687 && any_condjump_p (insn)
3688 && !find_reg_note (insn, REG_BR_PROB, 0))
3690 /* We can preserve the REG_BR_PROB notes only if exactly
3691 one jump is created, otherwise the machine description
3692 is responsible for this step using
3693 split_branch_probability variable. */
3694 gcc_assert (njumps == 1);
3695 add_int_reg_note (insn, REG_BR_PROB, probability);
3700 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3701 in SEQ and copy any additional information across. */
3702 if (CALL_P (trial))
3704 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3705 if (CALL_P (insn))
3707 rtx_insn *next;
3708 rtx *p;
3710 gcc_assert (call_insn == NULL_RTX);
3711 call_insn = insn;
3713 /* Add the old CALL_INSN_FUNCTION_USAGE to whatever the
3714 target may have explicitly specified. */
3715 p = &CALL_INSN_FUNCTION_USAGE (insn);
3716 while (*p)
3717 p = &XEXP (*p, 1);
3718 *p = CALL_INSN_FUNCTION_USAGE (trial);
3720 /* If the old call was a sibling call, the new one must
3721 be too. */
3722 SIBLING_CALL_P (insn) = SIBLING_CALL_P (trial);
3724 /* If the new call is the last instruction in the sequence,
3725 it will effectively replace the old call in-situ. Otherwise
3726 we must move any following NOTE_INSN_CALL_ARG_LOCATION note
3727 so that it comes immediately after the new call. */
3728 if (NEXT_INSN (insn))
3729 for (next = NEXT_INSN (trial);
3730 next && NOTE_P (next);
3731 next = NEXT_INSN (next))
3732 if (NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
3734 remove_insn (next);
3735 add_insn_after (next, insn, NULL);
3736 break;
3741 /* Copy notes, particularly those related to the CFG. */
3742 for (note = REG_NOTES (trial); note; note = XEXP (note, 1))
3744 switch (REG_NOTE_KIND (note))
3746 case REG_EH_REGION:
3747 copy_reg_eh_region_note_backward (note, insn_last, NULL);
3748 break;
3750 case REG_NORETURN:
3751 case REG_SETJMP:
3752 case REG_TM:
3753 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3755 if (CALL_P (insn))
3756 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3758 break;
3760 case REG_NON_LOCAL_GOTO:
3761 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3763 if (JUMP_P (insn))
3764 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3766 break;
3768 #ifdef AUTO_INC_DEC
3769 case REG_INC:
3770 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3772 rtx reg = XEXP (note, 0);
3773 if (!FIND_REG_INC_NOTE (insn, reg)
3774 && find_auto_inc (PATTERN (insn), reg))
3775 add_reg_note (insn, REG_INC, reg);
3777 break;
3778 #endif
3780 case REG_ARGS_SIZE:
3781 fixup_args_size_notes (NULL, insn_last, INTVAL (XEXP (note, 0)));
3782 break;
3784 case REG_CALL_DECL:
3785 gcc_assert (call_insn != NULL_RTX);
3786 add_reg_note (call_insn, REG_NOTE_KIND (note), XEXP (note, 0));
3787 break;
3789 default:
3790 break;
3794 /* If there are LABELS inside the split insns increment the
3795 usage count so we don't delete the label. */
3796 if (INSN_P (trial))
3798 insn = insn_last;
3799 while (insn != NULL_RTX)
3801 /* JUMP_P insns have already been "marked" above. */
3802 if (NONJUMP_INSN_P (insn))
3803 mark_label_nuses (PATTERN (insn));
3805 insn = PREV_INSN (insn);
3809 tem = emit_insn_after_setloc (seq, trial, INSN_LOCATION (trial));
3811 delete_insn (trial);
3813 /* Recursively call try_split for each new insn created; by the
3814 time control returns here that insn will be fully split, so
3815 set LAST and continue from the insn after the one returned.
3816 We can't use next_active_insn here since AFTER may be a note.
3817 Ignore deleted insns, which can be occur if not optimizing. */
3818 for (tem = NEXT_INSN (before); tem != after; tem = NEXT_INSN (tem))
3819 if (! tem->deleted () && INSN_P (tem))
3820 tem = try_split (PATTERN (tem), tem, 1);
3822 /* Return either the first or the last insn, depending on which was
3823 requested. */
3824 return last
3825 ? (after ? PREV_INSN (after) : get_last_insn ())
3826 : NEXT_INSN (before);
3829 /* Make and return an INSN rtx, initializing all its slots.
3830 Store PATTERN in the pattern slots. */
3832 rtx_insn *
3833 make_insn_raw (rtx pattern)
3835 rtx_insn *insn;
3837 insn = as_a <rtx_insn *> (rtx_alloc (INSN));
3839 INSN_UID (insn) = cur_insn_uid++;
3840 PATTERN (insn) = pattern;
3841 INSN_CODE (insn) = -1;
3842 REG_NOTES (insn) = NULL;
3843 INSN_LOCATION (insn) = curr_insn_location ();
3844 BLOCK_FOR_INSN (insn) = NULL;
3846 #ifdef ENABLE_RTL_CHECKING
3847 if (insn
3848 && INSN_P (insn)
3849 && (returnjump_p (insn)
3850 || (GET_CODE (insn) == SET
3851 && SET_DEST (insn) == pc_rtx)))
3853 warning (0, "ICE: emit_insn used where emit_jump_insn needed:\n");
3854 debug_rtx (insn);
3856 #endif
3858 return insn;
3861 /* Like `make_insn_raw' but make a DEBUG_INSN instead of an insn. */
3863 static rtx_insn *
3864 make_debug_insn_raw (rtx pattern)
3866 rtx_debug_insn *insn;
3868 insn = as_a <rtx_debug_insn *> (rtx_alloc (DEBUG_INSN));
3869 INSN_UID (insn) = cur_debug_insn_uid++;
3870 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3871 INSN_UID (insn) = cur_insn_uid++;
3873 PATTERN (insn) = pattern;
3874 INSN_CODE (insn) = -1;
3875 REG_NOTES (insn) = NULL;
3876 INSN_LOCATION (insn) = curr_insn_location ();
3877 BLOCK_FOR_INSN (insn) = NULL;
3879 return insn;
3882 /* Like `make_insn_raw' but make a JUMP_INSN instead of an insn. */
3884 static rtx_insn *
3885 make_jump_insn_raw (rtx pattern)
3887 rtx_jump_insn *insn;
3889 insn = as_a <rtx_jump_insn *> (rtx_alloc (JUMP_INSN));
3890 INSN_UID (insn) = cur_insn_uid++;
3892 PATTERN (insn) = pattern;
3893 INSN_CODE (insn) = -1;
3894 REG_NOTES (insn) = NULL;
3895 JUMP_LABEL (insn) = NULL;
3896 INSN_LOCATION (insn) = curr_insn_location ();
3897 BLOCK_FOR_INSN (insn) = NULL;
3899 return insn;
3902 /* Like `make_insn_raw' but make a CALL_INSN instead of an insn. */
3904 static rtx_insn *
3905 make_call_insn_raw (rtx pattern)
3907 rtx_call_insn *insn;
3909 insn = as_a <rtx_call_insn *> (rtx_alloc (CALL_INSN));
3910 INSN_UID (insn) = cur_insn_uid++;
3912 PATTERN (insn) = pattern;
3913 INSN_CODE (insn) = -1;
3914 REG_NOTES (insn) = NULL;
3915 CALL_INSN_FUNCTION_USAGE (insn) = NULL;
3916 INSN_LOCATION (insn) = curr_insn_location ();
3917 BLOCK_FOR_INSN (insn) = NULL;
3919 return insn;
3922 /* Like `make_insn_raw' but make a NOTE instead of an insn. */
3924 static rtx_note *
3925 make_note_raw (enum insn_note subtype)
3927 /* Some notes are never created this way at all. These notes are
3928 only created by patching out insns. */
3929 gcc_assert (subtype != NOTE_INSN_DELETED_LABEL
3930 && subtype != NOTE_INSN_DELETED_DEBUG_LABEL);
3932 rtx_note *note = as_a <rtx_note *> (rtx_alloc (NOTE));
3933 INSN_UID (note) = cur_insn_uid++;
3934 NOTE_KIND (note) = subtype;
3935 BLOCK_FOR_INSN (note) = NULL;
3936 memset (&NOTE_DATA (note), 0, sizeof (NOTE_DATA (note)));
3937 return note;
3940 /* Add INSN to the end of the doubly-linked list, between PREV and NEXT.
3941 INSN may be any object that can appear in the chain: INSN_P and NOTE_P objects,
3942 but also BARRIERs and JUMP_TABLE_DATAs. PREV and NEXT may be NULL. */
3944 static inline void
3945 link_insn_into_chain (rtx_insn *insn, rtx_insn *prev, rtx_insn *next)
3947 SET_PREV_INSN (insn) = prev;
3948 SET_NEXT_INSN (insn) = next;
3949 if (prev != NULL)
3951 SET_NEXT_INSN (prev) = insn;
3952 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
3954 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (prev));
3955 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = insn;
3958 if (next != NULL)
3960 SET_PREV_INSN (next) = insn;
3961 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
3963 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (next));
3964 SET_PREV_INSN (sequence->insn (0)) = insn;
3968 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3970 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (insn));
3971 SET_PREV_INSN (sequence->insn (0)) = prev;
3972 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = next;
3976 /* Add INSN to the end of the doubly-linked list.
3977 INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE. */
3979 void
3980 add_insn (rtx_insn *insn)
3982 rtx_insn *prev = get_last_insn ();
3983 link_insn_into_chain (insn, prev, NULL);
3984 if (NULL == get_insns ())
3985 set_first_insn (insn);
3986 set_last_insn (insn);
3989 /* Add INSN into the doubly-linked list after insn AFTER. */
3991 static void
3992 add_insn_after_nobb (rtx_insn *insn, rtx_insn *after)
3994 rtx_insn *next = NEXT_INSN (after);
3996 gcc_assert (!optimize || !after->deleted ());
3998 link_insn_into_chain (insn, after, next);
4000 if (next == NULL)
4002 if (get_last_insn () == after)
4003 set_last_insn (insn);
4004 else
4006 struct sequence_stack *stack = seq_stack;
4007 /* Scan all pending sequences too. */
4008 for (; stack; stack = stack->next)
4009 if (after == stack->last)
4011 stack->last = insn;
4012 break;
4018 /* Add INSN into the doubly-linked list before insn BEFORE. */
4020 static void
4021 add_insn_before_nobb (rtx_insn *insn, rtx_insn *before)
4023 rtx_insn *prev = PREV_INSN (before);
4025 gcc_assert (!optimize || !before->deleted ());
4027 link_insn_into_chain (insn, prev, before);
4029 if (prev == NULL)
4031 if (get_insns () == before)
4032 set_first_insn (insn);
4033 else
4035 struct sequence_stack *stack = seq_stack;
4036 /* Scan all pending sequences too. */
4037 for (; stack; stack = stack->next)
4038 if (before == stack->first)
4040 stack->first = insn;
4041 break;
4044 gcc_assert (stack);
4049 /* Like add_insn_after_nobb, but try to set BLOCK_FOR_INSN.
4050 If BB is NULL, an attempt is made to infer the bb from before.
4052 This and the next function should be the only functions called
4053 to insert an insn once delay slots have been filled since only
4054 they know how to update a SEQUENCE. */
4056 void
4057 add_insn_after (rtx uncast_insn, rtx uncast_after, basic_block bb)
4059 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4060 rtx_insn *after = as_a <rtx_insn *> (uncast_after);
4061 add_insn_after_nobb (insn, after);
4062 if (!BARRIER_P (after)
4063 && !BARRIER_P (insn)
4064 && (bb = BLOCK_FOR_INSN (after)))
4066 set_block_for_insn (insn, bb);
4067 if (INSN_P (insn))
4068 df_insn_rescan (insn);
4069 /* Should not happen as first in the BB is always
4070 either NOTE or LABEL. */
4071 if (BB_END (bb) == after
4072 /* Avoid clobbering of structure when creating new BB. */
4073 && !BARRIER_P (insn)
4074 && !NOTE_INSN_BASIC_BLOCK_P (insn))
4075 BB_END (bb) = insn;
4079 /* Like add_insn_before_nobb, but try to set BLOCK_FOR_INSN.
4080 If BB is NULL, an attempt is made to infer the bb from before.
4082 This and the previous function should be the only functions called
4083 to insert an insn once delay slots have been filled since only
4084 they know how to update a SEQUENCE. */
4086 void
4087 add_insn_before (rtx uncast_insn, rtx uncast_before, basic_block bb)
4089 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4090 rtx_insn *before = as_a <rtx_insn *> (uncast_before);
4091 add_insn_before_nobb (insn, before);
4093 if (!bb
4094 && !BARRIER_P (before)
4095 && !BARRIER_P (insn))
4096 bb = BLOCK_FOR_INSN (before);
4098 if (bb)
4100 set_block_for_insn (insn, bb);
4101 if (INSN_P (insn))
4102 df_insn_rescan (insn);
4103 /* Should not happen as first in the BB is always either NOTE or
4104 LABEL. */
4105 gcc_assert (BB_HEAD (bb) != insn
4106 /* Avoid clobbering of structure when creating new BB. */
4107 || BARRIER_P (insn)
4108 || NOTE_INSN_BASIC_BLOCK_P (insn));
4112 /* Replace insn with an deleted instruction note. */
4114 void
4115 set_insn_deleted (rtx insn)
4117 if (INSN_P (insn))
4118 df_insn_delete (as_a <rtx_insn *> (insn));
4119 PUT_CODE (insn, NOTE);
4120 NOTE_KIND (insn) = NOTE_INSN_DELETED;
4124 /* Unlink INSN from the insn chain.
4126 This function knows how to handle sequences.
4128 This function does not invalidate data flow information associated with
4129 INSN (i.e. does not call df_insn_delete). That makes this function
4130 usable for only disconnecting an insn from the chain, and re-emit it
4131 elsewhere later.
4133 To later insert INSN elsewhere in the insn chain via add_insn and
4134 similar functions, PREV_INSN and NEXT_INSN must be nullified by
4135 the caller. Nullifying them here breaks many insn chain walks.
4137 To really delete an insn and related DF information, use delete_insn. */
4139 void
4140 remove_insn (rtx uncast_insn)
4142 rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
4143 rtx_insn *next = NEXT_INSN (insn);
4144 rtx_insn *prev = PREV_INSN (insn);
4145 basic_block bb;
4147 if (prev)
4149 SET_NEXT_INSN (prev) = next;
4150 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
4152 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (prev));
4153 SET_NEXT_INSN (sequence->insn (sequence->len () - 1)) = next;
4156 else if (get_insns () == insn)
4158 if (next)
4159 SET_PREV_INSN (next) = NULL;
4160 set_first_insn (next);
4162 else
4164 struct sequence_stack *stack = seq_stack;
4165 /* Scan all pending sequences too. */
4166 for (; stack; stack = stack->next)
4167 if (insn == stack->first)
4169 stack->first = next;
4170 break;
4173 gcc_assert (stack);
4176 if (next)
4178 SET_PREV_INSN (next) = prev;
4179 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
4181 rtx_sequence *sequence = as_a <rtx_sequence *> (PATTERN (next));
4182 SET_PREV_INSN (sequence->insn (0)) = prev;
4185 else if (get_last_insn () == insn)
4186 set_last_insn (prev);
4187 else
4189 struct sequence_stack *stack = seq_stack;
4190 /* Scan all pending sequences too. */
4191 for (; stack; stack = stack->next)
4192 if (insn == stack->last)
4194 stack->last = prev;
4195 break;
4198 gcc_assert (stack);
4201 /* Fix up basic block boundaries, if necessary. */
4202 if (!BARRIER_P (insn)
4203 && (bb = BLOCK_FOR_INSN (insn)))
4205 if (BB_HEAD (bb) == insn)
4207 /* Never ever delete the basic block note without deleting whole
4208 basic block. */
4209 gcc_assert (!NOTE_P (insn));
4210 BB_HEAD (bb) = next;
4212 if (BB_END (bb) == insn)
4213 BB_END (bb) = prev;
4217 /* Append CALL_FUSAGE to the CALL_INSN_FUNCTION_USAGE for CALL_INSN. */
4219 void
4220 add_function_usage_to (rtx call_insn, rtx call_fusage)
4222 gcc_assert (call_insn && CALL_P (call_insn));
4224 /* Put the register usage information on the CALL. If there is already
4225 some usage information, put ours at the end. */
4226 if (CALL_INSN_FUNCTION_USAGE (call_insn))
4228 rtx link;
4230 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
4231 link = XEXP (link, 1))
4234 XEXP (link, 1) = call_fusage;
4236 else
4237 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
4240 /* Delete all insns made since FROM.
4241 FROM becomes the new last instruction. */
4243 void
4244 delete_insns_since (rtx_insn *from)
4246 if (from == 0)
4247 set_first_insn (0);
4248 else
4249 SET_NEXT_INSN (from) = 0;
4250 set_last_insn (from);
4253 /* This function is deprecated, please use sequences instead.
4255 Move a consecutive bunch of insns to a different place in the chain.
4256 The insns to be moved are those between FROM and TO.
4257 They are moved to a new position after the insn AFTER.
4258 AFTER must not be FROM or TO or any insn in between.
4260 This function does not know about SEQUENCEs and hence should not be
4261 called after delay-slot filling has been done. */
4263 void
4264 reorder_insns_nobb (rtx_insn *from, rtx_insn *to, rtx_insn *after)
4266 #ifdef ENABLE_CHECKING
4267 rtx_insn *x;
4268 for (x = from; x != to; x = NEXT_INSN (x))
4269 gcc_assert (after != x);
4270 gcc_assert (after != to);
4271 #endif
4273 /* Splice this bunch out of where it is now. */
4274 if (PREV_INSN (from))
4275 SET_NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
4276 if (NEXT_INSN (to))
4277 SET_PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
4278 if (get_last_insn () == to)
4279 set_last_insn (PREV_INSN (from));
4280 if (get_insns () == from)
4281 set_first_insn (NEXT_INSN (to));
4283 /* Make the new neighbors point to it and it to them. */
4284 if (NEXT_INSN (after))
4285 SET_PREV_INSN (NEXT_INSN (after)) = to;
4287 SET_NEXT_INSN (to) = NEXT_INSN (after);
4288 SET_PREV_INSN (from) = after;
4289 SET_NEXT_INSN (after) = from;
4290 if (after == get_last_insn ())
4291 set_last_insn (to);
4294 /* Same as function above, but take care to update BB boundaries. */
4295 void
4296 reorder_insns (rtx_insn *from, rtx_insn *to, rtx_insn *after)
4298 rtx_insn *prev = PREV_INSN (from);
4299 basic_block bb, bb2;
4301 reorder_insns_nobb (from, to, after);
4303 if (!BARRIER_P (after)
4304 && (bb = BLOCK_FOR_INSN (after)))
4306 rtx_insn *x;
4307 df_set_bb_dirty (bb);
4309 if (!BARRIER_P (from)
4310 && (bb2 = BLOCK_FOR_INSN (from)))
4312 if (BB_END (bb2) == to)
4313 BB_END (bb2) = prev;
4314 df_set_bb_dirty (bb2);
4317 if (BB_END (bb) == after)
4318 BB_END (bb) = to;
4320 for (x = from; x != NEXT_INSN (to); x = NEXT_INSN (x))
4321 if (!BARRIER_P (x))
4322 df_insn_change_bb (x, bb);
4327 /* Emit insn(s) of given code and pattern
4328 at a specified place within the doubly-linked list.
4330 All of the emit_foo global entry points accept an object
4331 X which is either an insn list or a PATTERN of a single
4332 instruction.
4334 There are thus a few canonical ways to generate code and
4335 emit it at a specific place in the instruction stream. For
4336 example, consider the instruction named SPOT and the fact that
4337 we would like to emit some instructions before SPOT. We might
4338 do it like this:
4340 start_sequence ();
4341 ... emit the new instructions ...
4342 insns_head = get_insns ();
4343 end_sequence ();
4345 emit_insn_before (insns_head, SPOT);
4347 It used to be common to generate SEQUENCE rtl instead, but that
4348 is a relic of the past which no longer occurs. The reason is that
4349 SEQUENCE rtl results in much fragmented RTL memory since the SEQUENCE
4350 generated would almost certainly die right after it was created. */
4352 static rtx_insn *
4353 emit_pattern_before_noloc (rtx x, rtx before, rtx last, basic_block bb,
4354 rtx_insn *(*make_raw) (rtx))
4356 rtx_insn *insn;
4358 gcc_assert (before);
4360 if (x == NULL_RTX)
4361 return safe_as_a <rtx_insn *> (last);
4363 switch (GET_CODE (x))
4365 case DEBUG_INSN:
4366 case INSN:
4367 case JUMP_INSN:
4368 case CALL_INSN:
4369 case CODE_LABEL:
4370 case BARRIER:
4371 case NOTE:
4372 insn = as_a <rtx_insn *> (x);
4373 while (insn)
4375 rtx_insn *next = NEXT_INSN (insn);
4376 add_insn_before (insn, before, bb);
4377 last = insn;
4378 insn = next;
4380 break;
4382 #ifdef ENABLE_RTL_CHECKING
4383 case SEQUENCE:
4384 gcc_unreachable ();
4385 break;
4386 #endif
4388 default:
4389 last = (*make_raw) (x);
4390 add_insn_before (last, before, bb);
4391 break;
4394 return safe_as_a <rtx_insn *> (last);
4397 /* Make X be output before the instruction BEFORE. */
4399 rtx_insn *
4400 emit_insn_before_noloc (rtx x, rtx_insn *before, basic_block bb)
4402 return emit_pattern_before_noloc (x, before, before, bb, make_insn_raw);
4405 /* Make an instruction with body X and code JUMP_INSN
4406 and output it before the instruction BEFORE. */
4408 rtx_insn *
4409 emit_jump_insn_before_noloc (rtx x, rtx_insn *before)
4411 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4412 make_jump_insn_raw);
4415 /* Make an instruction with body X and code CALL_INSN
4416 and output it before the instruction BEFORE. */
4418 rtx_insn *
4419 emit_call_insn_before_noloc (rtx x, rtx_insn *before)
4421 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4422 make_call_insn_raw);
4425 /* Make an instruction with body X and code DEBUG_INSN
4426 and output it before the instruction BEFORE. */
4428 rtx_insn *
4429 emit_debug_insn_before_noloc (rtx x, rtx before)
4431 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4432 make_debug_insn_raw);
4435 /* Make an insn of code BARRIER
4436 and output it before the insn BEFORE. */
4438 rtx_barrier *
4439 emit_barrier_before (rtx before)
4441 rtx_barrier *insn = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
4443 INSN_UID (insn) = cur_insn_uid++;
4445 add_insn_before (insn, before, NULL);
4446 return insn;
4449 /* Emit the label LABEL before the insn BEFORE. */
4451 rtx_insn *
4452 emit_label_before (rtx label, rtx_insn *before)
4454 gcc_checking_assert (INSN_UID (label) == 0);
4455 INSN_UID (label) = cur_insn_uid++;
4456 add_insn_before (label, before, NULL);
4457 return as_a <rtx_insn *> (label);
4460 /* Helper for emit_insn_after, handles lists of instructions
4461 efficiently. */
4463 static rtx_insn *
4464 emit_insn_after_1 (rtx_insn *first, rtx uncast_after, basic_block bb)
4466 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4467 rtx_insn *last;
4468 rtx_insn *after_after;
4469 if (!bb && !BARRIER_P (after))
4470 bb = BLOCK_FOR_INSN (after);
4472 if (bb)
4474 df_set_bb_dirty (bb);
4475 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4476 if (!BARRIER_P (last))
4478 set_block_for_insn (last, bb);
4479 df_insn_rescan (last);
4481 if (!BARRIER_P (last))
4483 set_block_for_insn (last, bb);
4484 df_insn_rescan (last);
4486 if (BB_END (bb) == after)
4487 BB_END (bb) = last;
4489 else
4490 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4491 continue;
4493 after_after = NEXT_INSN (after);
4495 SET_NEXT_INSN (after) = first;
4496 SET_PREV_INSN (first) = after;
4497 SET_NEXT_INSN (last) = after_after;
4498 if (after_after)
4499 SET_PREV_INSN (after_after) = last;
4501 if (after == get_last_insn ())
4502 set_last_insn (last);
4504 return last;
4507 static rtx_insn *
4508 emit_pattern_after_noloc (rtx x, rtx uncast_after, basic_block bb,
4509 rtx_insn *(*make_raw)(rtx))
4511 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4512 rtx_insn *last = after;
4514 gcc_assert (after);
4516 if (x == NULL_RTX)
4517 return last;
4519 switch (GET_CODE (x))
4521 case DEBUG_INSN:
4522 case INSN:
4523 case JUMP_INSN:
4524 case CALL_INSN:
4525 case CODE_LABEL:
4526 case BARRIER:
4527 case NOTE:
4528 last = emit_insn_after_1 (as_a <rtx_insn *> (x), after, bb);
4529 break;
4531 #ifdef ENABLE_RTL_CHECKING
4532 case SEQUENCE:
4533 gcc_unreachable ();
4534 break;
4535 #endif
4537 default:
4538 last = (*make_raw) (x);
4539 add_insn_after (last, after, bb);
4540 break;
4543 return last;
4546 /* Make X be output after the insn AFTER and set the BB of insn. If
4547 BB is NULL, an attempt is made to infer the BB from AFTER. */
4549 rtx_insn *
4550 emit_insn_after_noloc (rtx x, rtx after, basic_block bb)
4552 return emit_pattern_after_noloc (x, after, bb, make_insn_raw);
4556 /* Make an insn of code JUMP_INSN with body X
4557 and output it after the insn AFTER. */
4559 rtx_insn *
4560 emit_jump_insn_after_noloc (rtx x, rtx after)
4562 return emit_pattern_after_noloc (x, after, NULL, make_jump_insn_raw);
4565 /* Make an instruction with body X and code CALL_INSN
4566 and output it after the instruction AFTER. */
4568 rtx_insn *
4569 emit_call_insn_after_noloc (rtx x, rtx after)
4571 return emit_pattern_after_noloc (x, after, NULL, make_call_insn_raw);
4574 /* Make an instruction with body X and code CALL_INSN
4575 and output it after the instruction AFTER. */
4577 rtx_insn *
4578 emit_debug_insn_after_noloc (rtx x, rtx after)
4580 return emit_pattern_after_noloc (x, after, NULL, make_debug_insn_raw);
4583 /* Make an insn of code BARRIER
4584 and output it after the insn AFTER. */
4586 rtx_barrier *
4587 emit_barrier_after (rtx after)
4589 rtx_barrier *insn = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
4591 INSN_UID (insn) = cur_insn_uid++;
4593 add_insn_after (insn, after, NULL);
4594 return insn;
4597 /* Emit the label LABEL after the insn AFTER. */
4599 rtx_insn *
4600 emit_label_after (rtx label, rtx_insn *after)
4602 gcc_checking_assert (INSN_UID (label) == 0);
4603 INSN_UID (label) = cur_insn_uid++;
4604 add_insn_after (label, after, NULL);
4605 return as_a <rtx_insn *> (label);
4608 /* Notes require a bit of special handling: Some notes need to have their
4609 BLOCK_FOR_INSN set, others should never have it set, and some should
4610 have it set or clear depending on the context. */
4612 /* Return true iff a note of kind SUBTYPE should be emitted with routines
4613 that never set BLOCK_FOR_INSN on NOTE. BB_BOUNDARY is true if the
4614 caller is asked to emit a note before BB_HEAD, or after BB_END. */
4616 static bool
4617 note_outside_basic_block_p (enum insn_note subtype, bool on_bb_boundary_p)
4619 switch (subtype)
4621 /* NOTE_INSN_SWITCH_TEXT_SECTIONS only appears between basic blocks. */
4622 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4623 return true;
4625 /* Notes for var tracking and EH region markers can appear between or
4626 inside basic blocks. If the caller is emitting on the basic block
4627 boundary, do not set BLOCK_FOR_INSN on the new note. */
4628 case NOTE_INSN_VAR_LOCATION:
4629 case NOTE_INSN_CALL_ARG_LOCATION:
4630 case NOTE_INSN_EH_REGION_BEG:
4631 case NOTE_INSN_EH_REGION_END:
4632 return on_bb_boundary_p;
4634 /* Otherwise, BLOCK_FOR_INSN must be set. */
4635 default:
4636 return false;
4640 /* Emit a note of subtype SUBTYPE after the insn AFTER. */
4642 rtx_note *
4643 emit_note_after (enum insn_note subtype, rtx uncast_after)
4645 rtx_insn *after = as_a <rtx_insn *> (uncast_after);
4646 rtx_note *note = make_note_raw (subtype);
4647 basic_block bb = BARRIER_P (after) ? NULL : BLOCK_FOR_INSN (after);
4648 bool on_bb_boundary_p = (bb != NULL && BB_END (bb) == after);
4650 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4651 add_insn_after_nobb (note, after);
4652 else
4653 add_insn_after (note, after, bb);
4654 return note;
4657 /* Emit a note of subtype SUBTYPE before the insn BEFORE. */
4659 rtx_note *
4660 emit_note_before (enum insn_note subtype, rtx uncast_before)
4662 rtx_insn *before = as_a <rtx_insn *> (uncast_before);
4663 rtx_note *note = make_note_raw (subtype);
4664 basic_block bb = BARRIER_P (before) ? NULL : BLOCK_FOR_INSN (before);
4665 bool on_bb_boundary_p = (bb != NULL && BB_HEAD (bb) == before);
4667 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4668 add_insn_before_nobb (note, before);
4669 else
4670 add_insn_before (note, before, bb);
4671 return note;
4674 /* Insert PATTERN after AFTER, setting its INSN_LOCATION to LOC.
4675 MAKE_RAW indicates how to turn PATTERN into a real insn. */
4677 static rtx_insn *
4678 emit_pattern_after_setloc (rtx pattern, rtx uncast_after, int loc,
4679 rtx_insn *(*make_raw) (rtx))
4681 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4682 rtx last = emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4684 if (pattern == NULL_RTX || !loc)
4685 return safe_as_a <rtx_insn *> (last);
4687 after = NEXT_INSN (after);
4688 while (1)
4690 if (active_insn_p (after) && !INSN_LOCATION (after))
4691 INSN_LOCATION (after) = loc;
4692 if (after == last)
4693 break;
4694 after = NEXT_INSN (after);
4696 return safe_as_a <rtx_insn *> (last);
4699 /* Insert PATTERN after AFTER. MAKE_RAW indicates how to turn PATTERN
4700 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert after
4701 any DEBUG_INSNs. */
4703 static rtx_insn *
4704 emit_pattern_after (rtx pattern, rtx uncast_after, bool skip_debug_insns,
4705 rtx_insn *(*make_raw) (rtx))
4707 rtx_insn *after = safe_as_a <rtx_insn *> (uncast_after);
4708 rtx_insn *prev = after;
4710 if (skip_debug_insns)
4711 while (DEBUG_INSN_P (prev))
4712 prev = PREV_INSN (prev);
4714 if (INSN_P (prev))
4715 return emit_pattern_after_setloc (pattern, after, INSN_LOCATION (prev),
4716 make_raw);
4717 else
4718 return emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4721 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4722 rtx_insn *
4723 emit_insn_after_setloc (rtx pattern, rtx after, int loc)
4725 return emit_pattern_after_setloc (pattern, after, loc, make_insn_raw);
4728 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4729 rtx_insn *
4730 emit_insn_after (rtx pattern, rtx after)
4732 return emit_pattern_after (pattern, after, true, make_insn_raw);
4735 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4736 rtx_insn *
4737 emit_jump_insn_after_setloc (rtx pattern, rtx after, int loc)
4739 return emit_pattern_after_setloc (pattern, after, loc, make_jump_insn_raw);
4742 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4743 rtx_insn *
4744 emit_jump_insn_after (rtx pattern, rtx after)
4746 return emit_pattern_after (pattern, after, true, make_jump_insn_raw);
4749 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4750 rtx_insn *
4751 emit_call_insn_after_setloc (rtx pattern, rtx after, int loc)
4753 return emit_pattern_after_setloc (pattern, after, loc, make_call_insn_raw);
4756 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4757 rtx_insn *
4758 emit_call_insn_after (rtx pattern, rtx after)
4760 return emit_pattern_after (pattern, after, true, make_call_insn_raw);
4763 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4764 rtx_insn *
4765 emit_debug_insn_after_setloc (rtx pattern, rtx after, int loc)
4767 return emit_pattern_after_setloc (pattern, after, loc, make_debug_insn_raw);
4770 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4771 rtx_insn *
4772 emit_debug_insn_after (rtx pattern, rtx after)
4774 return emit_pattern_after (pattern, after, false, make_debug_insn_raw);
4777 /* Insert PATTERN before BEFORE, setting its INSN_LOCATION to LOC.
4778 MAKE_RAW indicates how to turn PATTERN into a real insn. INSNP
4779 indicates if PATTERN is meant for an INSN as opposed to a JUMP_INSN,
4780 CALL_INSN, etc. */
4782 static rtx_insn *
4783 emit_pattern_before_setloc (rtx pattern, rtx uncast_before, int loc, bool insnp,
4784 rtx_insn *(*make_raw) (rtx))
4786 rtx_insn *before = as_a <rtx_insn *> (uncast_before);
4787 rtx_insn *first = PREV_INSN (before);
4788 rtx_insn *last = emit_pattern_before_noloc (pattern, before,
4789 insnp ? before : NULL_RTX,
4790 NULL, make_raw);
4792 if (pattern == NULL_RTX || !loc)
4793 return last;
4795 if (!first)
4796 first = get_insns ();
4797 else
4798 first = NEXT_INSN (first);
4799 while (1)
4801 if (active_insn_p (first) && !INSN_LOCATION (first))
4802 INSN_LOCATION (first) = loc;
4803 if (first == last)
4804 break;
4805 first = NEXT_INSN (first);
4807 return last;
4810 /* Insert PATTERN before BEFORE. MAKE_RAW indicates how to turn PATTERN
4811 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert
4812 before any DEBUG_INSNs. INSNP indicates if PATTERN is meant for an
4813 INSN as opposed to a JUMP_INSN, CALL_INSN, etc. */
4815 static rtx_insn *
4816 emit_pattern_before (rtx pattern, rtx uncast_before, bool skip_debug_insns,
4817 bool insnp, rtx_insn *(*make_raw) (rtx))
4819 rtx_insn *before = safe_as_a <rtx_insn *> (uncast_before);
4820 rtx_insn *next = before;
4822 if (skip_debug_insns)
4823 while (DEBUG_INSN_P (next))
4824 next = PREV_INSN (next);
4826 if (INSN_P (next))
4827 return emit_pattern_before_setloc (pattern, before, INSN_LOCATION (next),
4828 insnp, make_raw);
4829 else
4830 return emit_pattern_before_noloc (pattern, before,
4831 insnp ? before : NULL_RTX,
4832 NULL, make_raw);
4835 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4836 rtx_insn *
4837 emit_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4839 return emit_pattern_before_setloc (pattern, before, loc, true,
4840 make_insn_raw);
4843 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4844 rtx_insn *
4845 emit_insn_before (rtx pattern, rtx before)
4847 return emit_pattern_before (pattern, before, true, true, make_insn_raw);
4850 /* like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4851 rtx_insn *
4852 emit_jump_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4854 return emit_pattern_before_setloc (pattern, before, loc, false,
4855 make_jump_insn_raw);
4858 /* Like emit_jump_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4859 rtx_insn *
4860 emit_jump_insn_before (rtx pattern, rtx before)
4862 return emit_pattern_before (pattern, before, true, false,
4863 make_jump_insn_raw);
4866 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4867 rtx_insn *
4868 emit_call_insn_before_setloc (rtx pattern, rtx_insn *before, int loc)
4870 return emit_pattern_before_setloc (pattern, before, loc, false,
4871 make_call_insn_raw);
4874 /* Like emit_call_insn_before_noloc,
4875 but set insn_location according to BEFORE. */
4876 rtx_insn *
4877 emit_call_insn_before (rtx pattern, rtx_insn *before)
4879 return emit_pattern_before (pattern, before, true, false,
4880 make_call_insn_raw);
4883 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4884 rtx_insn *
4885 emit_debug_insn_before_setloc (rtx pattern, rtx before, int loc)
4887 return emit_pattern_before_setloc (pattern, before, loc, false,
4888 make_debug_insn_raw);
4891 /* Like emit_debug_insn_before_noloc,
4892 but set insn_location according to BEFORE. */
4893 rtx_insn *
4894 emit_debug_insn_before (rtx pattern, rtx before)
4896 return emit_pattern_before (pattern, before, false, false,
4897 make_debug_insn_raw);
4900 /* Take X and emit it at the end of the doubly-linked
4901 INSN list.
4903 Returns the last insn emitted. */
4905 rtx_insn *
4906 emit_insn (rtx x)
4908 rtx_insn *last = get_last_insn ();
4909 rtx_insn *insn;
4911 if (x == NULL_RTX)
4912 return last;
4914 switch (GET_CODE (x))
4916 case DEBUG_INSN:
4917 case INSN:
4918 case JUMP_INSN:
4919 case CALL_INSN:
4920 case CODE_LABEL:
4921 case BARRIER:
4922 case NOTE:
4923 insn = as_a <rtx_insn *> (x);
4924 while (insn)
4926 rtx_insn *next = NEXT_INSN (insn);
4927 add_insn (insn);
4928 last = insn;
4929 insn = next;
4931 break;
4933 #ifdef ENABLE_RTL_CHECKING
4934 case JUMP_TABLE_DATA:
4935 case SEQUENCE:
4936 gcc_unreachable ();
4937 break;
4938 #endif
4940 default:
4941 last = make_insn_raw (x);
4942 add_insn (last);
4943 break;
4946 return last;
4949 /* Make an insn of code DEBUG_INSN with pattern X
4950 and add it to the end of the doubly-linked list. */
4952 rtx_insn *
4953 emit_debug_insn (rtx x)
4955 rtx_insn *last = get_last_insn ();
4956 rtx_insn *insn;
4958 if (x == NULL_RTX)
4959 return last;
4961 switch (GET_CODE (x))
4963 case DEBUG_INSN:
4964 case INSN:
4965 case JUMP_INSN:
4966 case CALL_INSN:
4967 case CODE_LABEL:
4968 case BARRIER:
4969 case NOTE:
4970 insn = as_a <rtx_insn *> (x);
4971 while (insn)
4973 rtx_insn *next = NEXT_INSN (insn);
4974 add_insn (insn);
4975 last = insn;
4976 insn = next;
4978 break;
4980 #ifdef ENABLE_RTL_CHECKING
4981 case JUMP_TABLE_DATA:
4982 case SEQUENCE:
4983 gcc_unreachable ();
4984 break;
4985 #endif
4987 default:
4988 last = make_debug_insn_raw (x);
4989 add_insn (last);
4990 break;
4993 return last;
4996 /* Make an insn of code JUMP_INSN with pattern X
4997 and add it to the end of the doubly-linked list. */
4999 rtx_insn *
5000 emit_jump_insn (rtx x)
5002 rtx_insn *last = NULL;
5003 rtx_insn *insn;
5005 switch (GET_CODE (x))
5007 case DEBUG_INSN:
5008 case INSN:
5009 case JUMP_INSN:
5010 case CALL_INSN:
5011 case CODE_LABEL:
5012 case BARRIER:
5013 case NOTE:
5014 insn = as_a <rtx_insn *> (x);
5015 while (insn)
5017 rtx_insn *next = NEXT_INSN (insn);
5018 add_insn (insn);
5019 last = insn;
5020 insn = next;
5022 break;
5024 #ifdef ENABLE_RTL_CHECKING
5025 case JUMP_TABLE_DATA:
5026 case SEQUENCE:
5027 gcc_unreachable ();
5028 break;
5029 #endif
5031 default:
5032 last = make_jump_insn_raw (x);
5033 add_insn (last);
5034 break;
5037 return last;
5040 /* Make an insn of code CALL_INSN with pattern X
5041 and add it to the end of the doubly-linked list. */
5043 rtx_insn *
5044 emit_call_insn (rtx x)
5046 rtx_insn *insn;
5048 switch (GET_CODE (x))
5050 case DEBUG_INSN:
5051 case INSN:
5052 case JUMP_INSN:
5053 case CALL_INSN:
5054 case CODE_LABEL:
5055 case BARRIER:
5056 case NOTE:
5057 insn = emit_insn (x);
5058 break;
5060 #ifdef ENABLE_RTL_CHECKING
5061 case SEQUENCE:
5062 case JUMP_TABLE_DATA:
5063 gcc_unreachable ();
5064 break;
5065 #endif
5067 default:
5068 insn = make_call_insn_raw (x);
5069 add_insn (insn);
5070 break;
5073 return insn;
5076 /* Add the label LABEL to the end of the doubly-linked list. */
5078 rtx_insn *
5079 emit_label (rtx label)
5081 gcc_checking_assert (INSN_UID (label) == 0);
5082 INSN_UID (label) = cur_insn_uid++;
5083 add_insn (as_a <rtx_insn *> (label));
5084 return as_a <rtx_insn *> (label);
5087 /* Make an insn of code JUMP_TABLE_DATA
5088 and add it to the end of the doubly-linked list. */
5090 rtx_jump_table_data *
5091 emit_jump_table_data (rtx table)
5093 rtx_jump_table_data *jump_table_data =
5094 as_a <rtx_jump_table_data *> (rtx_alloc (JUMP_TABLE_DATA));
5095 INSN_UID (jump_table_data) = cur_insn_uid++;
5096 PATTERN (jump_table_data) = table;
5097 BLOCK_FOR_INSN (jump_table_data) = NULL;
5098 add_insn (jump_table_data);
5099 return jump_table_data;
5102 /* Make an insn of code BARRIER
5103 and add it to the end of the doubly-linked list. */
5105 rtx_barrier *
5106 emit_barrier (void)
5108 rtx_barrier *barrier = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
5109 INSN_UID (barrier) = cur_insn_uid++;
5110 add_insn (barrier);
5111 return barrier;
5114 /* Emit a copy of note ORIG. */
5116 rtx_note *
5117 emit_note_copy (rtx_note *orig)
5119 enum insn_note kind = (enum insn_note) NOTE_KIND (orig);
5120 rtx_note *note = make_note_raw (kind);
5121 NOTE_DATA (note) = NOTE_DATA (orig);
5122 add_insn (note);
5123 return note;
5126 /* Make an insn of code NOTE or type NOTE_NO
5127 and add it to the end of the doubly-linked list. */
5129 rtx_note *
5130 emit_note (enum insn_note kind)
5132 rtx_note *note = make_note_raw (kind);
5133 add_insn (note);
5134 return note;
5137 /* Emit a clobber of lvalue X. */
5139 rtx_insn *
5140 emit_clobber (rtx x)
5142 /* CONCATs should not appear in the insn stream. */
5143 if (GET_CODE (x) == CONCAT)
5145 emit_clobber (XEXP (x, 0));
5146 return emit_clobber (XEXP (x, 1));
5148 return emit_insn (gen_rtx_CLOBBER (VOIDmode, x));
5151 /* Return a sequence of insns to clobber lvalue X. */
5153 rtx_insn *
5154 gen_clobber (rtx x)
5156 rtx_insn *seq;
5158 start_sequence ();
5159 emit_clobber (x);
5160 seq = get_insns ();
5161 end_sequence ();
5162 return seq;
5165 /* Emit a use of rvalue X. */
5167 rtx_insn *
5168 emit_use (rtx x)
5170 /* CONCATs should not appear in the insn stream. */
5171 if (GET_CODE (x) == CONCAT)
5173 emit_use (XEXP (x, 0));
5174 return emit_use (XEXP (x, 1));
5176 return emit_insn (gen_rtx_USE (VOIDmode, x));
5179 /* Return a sequence of insns to use rvalue X. */
5181 rtx_insn *
5182 gen_use (rtx x)
5184 rtx_insn *seq;
5186 start_sequence ();
5187 emit_use (x);
5188 seq = get_insns ();
5189 end_sequence ();
5190 return seq;
5193 /* Notes like REG_EQUAL and REG_EQUIV refer to a set in an instruction.
5194 Return the set in INSN that such notes describe, or NULL if the notes
5195 have no meaning for INSN. */
5198 set_for_reg_notes (rtx insn)
5200 rtx pat, reg;
5202 if (!INSN_P (insn))
5203 return NULL_RTX;
5205 pat = PATTERN (insn);
5206 if (GET_CODE (pat) == PARALLEL)
5208 /* We do not use single_set because that ignores SETs of unused
5209 registers. REG_EQUAL and REG_EQUIV notes really do require the
5210 PARALLEL to have a single SET. */
5211 if (multiple_sets (insn))
5212 return NULL_RTX;
5213 pat = XVECEXP (pat, 0, 0);
5216 if (GET_CODE (pat) != SET)
5217 return NULL_RTX;
5219 reg = SET_DEST (pat);
5221 /* Notes apply to the contents of a STRICT_LOW_PART. */
5222 if (GET_CODE (reg) == STRICT_LOW_PART)
5223 reg = XEXP (reg, 0);
5225 /* Check that we have a register. */
5226 if (!(REG_P (reg) || GET_CODE (reg) == SUBREG))
5227 return NULL_RTX;
5229 return pat;
5232 /* Place a note of KIND on insn INSN with DATUM as the datum. If a
5233 note of this type already exists, remove it first. */
5236 set_unique_reg_note (rtx insn, enum reg_note kind, rtx datum)
5238 rtx note = find_reg_note (insn, kind, NULL_RTX);
5240 switch (kind)
5242 case REG_EQUAL:
5243 case REG_EQUIV:
5244 if (!set_for_reg_notes (insn))
5245 return NULL_RTX;
5247 /* Don't add ASM_OPERAND REG_EQUAL/REG_EQUIV notes.
5248 It serves no useful purpose and breaks eliminate_regs. */
5249 if (GET_CODE (datum) == ASM_OPERANDS)
5250 return NULL_RTX;
5252 /* Notes with side effects are dangerous. Even if the side-effect
5253 initially mirrors one in PATTERN (INSN), later optimizations
5254 might alter the way that the final register value is calculated
5255 and so move or alter the side-effect in some way. The note would
5256 then no longer be a valid substitution for SET_SRC. */
5257 if (side_effects_p (datum))
5258 return NULL_RTX;
5259 break;
5261 default:
5262 break;
5265 if (note)
5266 XEXP (note, 0) = datum;
5267 else
5269 add_reg_note (insn, kind, datum);
5270 note = REG_NOTES (insn);
5273 switch (kind)
5275 case REG_EQUAL:
5276 case REG_EQUIV:
5277 df_notes_rescan (as_a <rtx_insn *> (insn));
5278 break;
5279 default:
5280 break;
5283 return note;
5286 /* Like set_unique_reg_note, but don't do anything unless INSN sets DST. */
5288 set_dst_reg_note (rtx insn, enum reg_note kind, rtx datum, rtx dst)
5290 rtx set = set_for_reg_notes (insn);
5292 if (set && SET_DEST (set) == dst)
5293 return set_unique_reg_note (insn, kind, datum);
5294 return NULL_RTX;
5297 /* Return an indication of which type of insn should have X as a body.
5298 The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN. */
5300 static enum rtx_code
5301 classify_insn (rtx x)
5303 if (LABEL_P (x))
5304 return CODE_LABEL;
5305 if (GET_CODE (x) == CALL)
5306 return CALL_INSN;
5307 if (ANY_RETURN_P (x))
5308 return JUMP_INSN;
5309 if (GET_CODE (x) == SET)
5311 if (SET_DEST (x) == pc_rtx)
5312 return JUMP_INSN;
5313 else if (GET_CODE (SET_SRC (x)) == CALL)
5314 return CALL_INSN;
5315 else
5316 return INSN;
5318 if (GET_CODE (x) == PARALLEL)
5320 int j;
5321 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
5322 if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
5323 return CALL_INSN;
5324 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
5325 && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
5326 return JUMP_INSN;
5327 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
5328 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
5329 return CALL_INSN;
5331 return INSN;
5334 /* Emit the rtl pattern X as an appropriate kind of insn.
5335 If X is a label, it is simply added into the insn chain. */
5337 rtx_insn *
5338 emit (rtx x)
5340 enum rtx_code code = classify_insn (x);
5342 switch (code)
5344 case CODE_LABEL:
5345 return emit_label (x);
5346 case INSN:
5347 return emit_insn (x);
5348 case JUMP_INSN:
5350 rtx_insn *insn = emit_jump_insn (x);
5351 if (any_uncondjump_p (insn) || GET_CODE (x) == RETURN)
5352 return emit_barrier ();
5353 return insn;
5355 case CALL_INSN:
5356 return emit_call_insn (x);
5357 case DEBUG_INSN:
5358 return emit_debug_insn (x);
5359 default:
5360 gcc_unreachable ();
5364 /* Space for free sequence stack entries. */
5365 static GTY ((deletable)) struct sequence_stack *free_sequence_stack;
5367 /* Begin emitting insns to a sequence. If this sequence will contain
5368 something that might cause the compiler to pop arguments to function
5369 calls (because those pops have previously been deferred; see
5370 INHIBIT_DEFER_POP for more details), use do_pending_stack_adjust
5371 before calling this function. That will ensure that the deferred
5372 pops are not accidentally emitted in the middle of this sequence. */
5374 void
5375 start_sequence (void)
5377 struct sequence_stack *tem;
5379 if (free_sequence_stack != NULL)
5381 tem = free_sequence_stack;
5382 free_sequence_stack = tem->next;
5384 else
5385 tem = ggc_alloc<sequence_stack> ();
5387 tem->next = seq_stack;
5388 tem->first = get_insns ();
5389 tem->last = get_last_insn ();
5391 seq_stack = tem;
5393 set_first_insn (0);
5394 set_last_insn (0);
5397 /* Set up the insn chain starting with FIRST as the current sequence,
5398 saving the previously current one. See the documentation for
5399 start_sequence for more information about how to use this function. */
5401 void
5402 push_to_sequence (rtx_insn *first)
5404 rtx_insn *last;
5406 start_sequence ();
5408 for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last))
5411 set_first_insn (first);
5412 set_last_insn (last);
5415 /* Like push_to_sequence, but take the last insn as an argument to avoid
5416 looping through the list. */
5418 void
5419 push_to_sequence2 (rtx_insn *first, rtx_insn *last)
5421 start_sequence ();
5423 set_first_insn (first);
5424 set_last_insn (last);
5427 /* Set up the outer-level insn chain
5428 as the current sequence, saving the previously current one. */
5430 void
5431 push_topmost_sequence (void)
5433 struct sequence_stack *stack, *top = NULL;
5435 start_sequence ();
5437 for (stack = seq_stack; stack; stack = stack->next)
5438 top = stack;
5440 set_first_insn (top->first);
5441 set_last_insn (top->last);
5444 /* After emitting to the outer-level insn chain, update the outer-level
5445 insn chain, and restore the previous saved state. */
5447 void
5448 pop_topmost_sequence (void)
5450 struct sequence_stack *stack, *top = NULL;
5452 for (stack = seq_stack; stack; stack = stack->next)
5453 top = stack;
5455 top->first = get_insns ();
5456 top->last = get_last_insn ();
5458 end_sequence ();
5461 /* After emitting to a sequence, restore previous saved state.
5463 To get the contents of the sequence just made, you must call
5464 `get_insns' *before* calling here.
5466 If the compiler might have deferred popping arguments while
5467 generating this sequence, and this sequence will not be immediately
5468 inserted into the instruction stream, use do_pending_stack_adjust
5469 before calling get_insns. That will ensure that the deferred
5470 pops are inserted into this sequence, and not into some random
5471 location in the instruction stream. See INHIBIT_DEFER_POP for more
5472 information about deferred popping of arguments. */
5474 void
5475 end_sequence (void)
5477 struct sequence_stack *tem = seq_stack;
5479 set_first_insn (tem->first);
5480 set_last_insn (tem->last);
5481 seq_stack = tem->next;
5483 memset (tem, 0, sizeof (*tem));
5484 tem->next = free_sequence_stack;
5485 free_sequence_stack = tem;
5488 /* Return 1 if currently emitting into a sequence. */
5491 in_sequence_p (void)
5493 return seq_stack != 0;
5496 /* Put the various virtual registers into REGNO_REG_RTX. */
5498 static void
5499 init_virtual_regs (void)
5501 regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
5502 regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
5503 regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
5504 regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
5505 regno_reg_rtx[VIRTUAL_CFA_REGNUM] = virtual_cfa_rtx;
5506 regno_reg_rtx[VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM]
5507 = virtual_preferred_stack_boundary_rtx;
5511 /* Used by copy_insn_1 to avoid copying SCRATCHes more than once. */
5512 static rtx copy_insn_scratch_in[MAX_RECOG_OPERANDS];
5513 static rtx copy_insn_scratch_out[MAX_RECOG_OPERANDS];
5514 static int copy_insn_n_scratches;
5516 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5517 copied an ASM_OPERANDS.
5518 In that case, it is the original input-operand vector. */
5519 static rtvec orig_asm_operands_vector;
5521 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5522 copied an ASM_OPERANDS.
5523 In that case, it is the copied input-operand vector. */
5524 static rtvec copy_asm_operands_vector;
5526 /* Likewise for the constraints vector. */
5527 static rtvec orig_asm_constraints_vector;
5528 static rtvec copy_asm_constraints_vector;
5530 /* Recursively create a new copy of an rtx for copy_insn.
5531 This function differs from copy_rtx in that it handles SCRATCHes and
5532 ASM_OPERANDs properly.
5533 Normally, this function is not used directly; use copy_insn as front end.
5534 However, you could first copy an insn pattern with copy_insn and then use
5535 this function afterwards to properly copy any REG_NOTEs containing
5536 SCRATCHes. */
5539 copy_insn_1 (rtx orig)
5541 rtx copy;
5542 int i, j;
5543 RTX_CODE code;
5544 const char *format_ptr;
5546 if (orig == NULL)
5547 return NULL;
5549 code = GET_CODE (orig);
5551 switch (code)
5553 case REG:
5554 case DEBUG_EXPR:
5555 CASE_CONST_ANY:
5556 case SYMBOL_REF:
5557 case CODE_LABEL:
5558 case PC:
5559 case CC0:
5560 case RETURN:
5561 case SIMPLE_RETURN:
5562 return orig;
5563 case CLOBBER:
5564 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
5565 clobbers or clobbers of hard registers that originated as pseudos.
5566 This is needed to allow safe register renaming. */
5567 if (REG_P (XEXP (orig, 0)) && REGNO (XEXP (orig, 0)) < FIRST_PSEUDO_REGISTER
5568 && ORIGINAL_REGNO (XEXP (orig, 0)) == REGNO (XEXP (orig, 0)))
5569 return orig;
5570 break;
5572 case SCRATCH:
5573 for (i = 0; i < copy_insn_n_scratches; i++)
5574 if (copy_insn_scratch_in[i] == orig)
5575 return copy_insn_scratch_out[i];
5576 break;
5578 case CONST:
5579 if (shared_const_p (orig))
5580 return orig;
5581 break;
5583 /* A MEM with a constant address is not sharable. The problem is that
5584 the constant address may need to be reloaded. If the mem is shared,
5585 then reloading one copy of this mem will cause all copies to appear
5586 to have been reloaded. */
5588 default:
5589 break;
5592 /* Copy the various flags, fields, and other information. We assume
5593 that all fields need copying, and then clear the fields that should
5594 not be copied. That is the sensible default behavior, and forces
5595 us to explicitly document why we are *not* copying a flag. */
5596 copy = shallow_copy_rtx (orig);
5598 /* We do not copy the USED flag, which is used as a mark bit during
5599 walks over the RTL. */
5600 RTX_FLAG (copy, used) = 0;
5602 /* We do not copy JUMP, CALL, or FRAME_RELATED for INSNs. */
5603 if (INSN_P (orig))
5605 RTX_FLAG (copy, jump) = 0;
5606 RTX_FLAG (copy, call) = 0;
5607 RTX_FLAG (copy, frame_related) = 0;
5610 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
5612 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
5613 switch (*format_ptr++)
5615 case 'e':
5616 if (XEXP (orig, i) != NULL)
5617 XEXP (copy, i) = copy_insn_1 (XEXP (orig, i));
5618 break;
5620 case 'E':
5621 case 'V':
5622 if (XVEC (orig, i) == orig_asm_constraints_vector)
5623 XVEC (copy, i) = copy_asm_constraints_vector;
5624 else if (XVEC (orig, i) == orig_asm_operands_vector)
5625 XVEC (copy, i) = copy_asm_operands_vector;
5626 else if (XVEC (orig, i) != NULL)
5628 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
5629 for (j = 0; j < XVECLEN (copy, i); j++)
5630 XVECEXP (copy, i, j) = copy_insn_1 (XVECEXP (orig, i, j));
5632 break;
5634 case 't':
5635 case 'w':
5636 case 'i':
5637 case 's':
5638 case 'S':
5639 case 'u':
5640 case '0':
5641 /* These are left unchanged. */
5642 break;
5644 default:
5645 gcc_unreachable ();
5648 if (code == SCRATCH)
5650 i = copy_insn_n_scratches++;
5651 gcc_assert (i < MAX_RECOG_OPERANDS);
5652 copy_insn_scratch_in[i] = orig;
5653 copy_insn_scratch_out[i] = copy;
5655 else if (code == ASM_OPERANDS)
5657 orig_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (orig);
5658 copy_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (copy);
5659 orig_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (orig);
5660 copy_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (copy);
5663 return copy;
5666 /* Create a new copy of an rtx.
5667 This function differs from copy_rtx in that it handles SCRATCHes and
5668 ASM_OPERANDs properly.
5669 INSN doesn't really have to be a full INSN; it could be just the
5670 pattern. */
5672 copy_insn (rtx insn)
5674 copy_insn_n_scratches = 0;
5675 orig_asm_operands_vector = 0;
5676 orig_asm_constraints_vector = 0;
5677 copy_asm_operands_vector = 0;
5678 copy_asm_constraints_vector = 0;
5679 return copy_insn_1 (insn);
5682 /* Return a copy of INSN that can be used in a SEQUENCE delay slot,
5683 on that assumption that INSN itself remains in its original place. */
5685 rtx_insn *
5686 copy_delay_slot_insn (rtx_insn *insn)
5688 /* Copy INSN with its rtx_code, all its notes, location etc. */
5689 insn = as_a <rtx_insn *> (copy_rtx (insn));
5690 INSN_UID (insn) = cur_insn_uid++;
5691 return insn;
5694 /* Initialize data structures and variables in this file
5695 before generating rtl for each function. */
5697 void
5698 init_emit (void)
5700 set_first_insn (NULL);
5701 set_last_insn (NULL);
5702 if (MIN_NONDEBUG_INSN_UID)
5703 cur_insn_uid = MIN_NONDEBUG_INSN_UID;
5704 else
5705 cur_insn_uid = 1;
5706 cur_debug_insn_uid = 1;
5707 reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
5708 first_label_num = label_num;
5709 seq_stack = NULL;
5711 /* Init the tables that describe all the pseudo regs. */
5713 crtl->emit.regno_pointer_align_length = LAST_VIRTUAL_REGISTER + 101;
5715 crtl->emit.regno_pointer_align
5716 = XCNEWVEC (unsigned char, crtl->emit.regno_pointer_align_length);
5718 regno_reg_rtx = ggc_vec_alloc<rtx> (crtl->emit.regno_pointer_align_length);
5720 /* Put copies of all the hard registers into regno_reg_rtx. */
5721 memcpy (regno_reg_rtx,
5722 initial_regno_reg_rtx,
5723 FIRST_PSEUDO_REGISTER * sizeof (rtx));
5725 /* Put copies of all the virtual register rtx into regno_reg_rtx. */
5726 init_virtual_regs ();
5728 /* Indicate that the virtual registers and stack locations are
5729 all pointers. */
5730 REG_POINTER (stack_pointer_rtx) = 1;
5731 REG_POINTER (frame_pointer_rtx) = 1;
5732 REG_POINTER (hard_frame_pointer_rtx) = 1;
5733 REG_POINTER (arg_pointer_rtx) = 1;
5735 REG_POINTER (virtual_incoming_args_rtx) = 1;
5736 REG_POINTER (virtual_stack_vars_rtx) = 1;
5737 REG_POINTER (virtual_stack_dynamic_rtx) = 1;
5738 REG_POINTER (virtual_outgoing_args_rtx) = 1;
5739 REG_POINTER (virtual_cfa_rtx) = 1;
5741 #ifdef STACK_BOUNDARY
5742 REGNO_POINTER_ALIGN (STACK_POINTER_REGNUM) = STACK_BOUNDARY;
5743 REGNO_POINTER_ALIGN (FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5744 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5745 REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) = STACK_BOUNDARY;
5747 REGNO_POINTER_ALIGN (VIRTUAL_INCOMING_ARGS_REGNUM) = STACK_BOUNDARY;
5748 REGNO_POINTER_ALIGN (VIRTUAL_STACK_VARS_REGNUM) = STACK_BOUNDARY;
5749 REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM) = STACK_BOUNDARY;
5750 REGNO_POINTER_ALIGN (VIRTUAL_OUTGOING_ARGS_REGNUM) = STACK_BOUNDARY;
5751 REGNO_POINTER_ALIGN (VIRTUAL_CFA_REGNUM) = BITS_PER_WORD;
5752 #endif
5754 #ifdef INIT_EXPANDERS
5755 INIT_EXPANDERS;
5756 #endif
5759 /* Generate a vector constant for mode MODE and constant value CONSTANT. */
5761 static rtx
5762 gen_const_vector (machine_mode mode, int constant)
5764 rtx tem;
5765 rtvec v;
5766 int units, i;
5767 machine_mode inner;
5769 units = GET_MODE_NUNITS (mode);
5770 inner = GET_MODE_INNER (mode);
5772 gcc_assert (!DECIMAL_FLOAT_MODE_P (inner));
5774 v = rtvec_alloc (units);
5776 /* We need to call this function after we set the scalar const_tiny_rtx
5777 entries. */
5778 gcc_assert (const_tiny_rtx[constant][(int) inner]);
5780 for (i = 0; i < units; ++i)
5781 RTVEC_ELT (v, i) = const_tiny_rtx[constant][(int) inner];
5783 tem = gen_rtx_raw_CONST_VECTOR (mode, v);
5784 return tem;
5787 /* Generate a vector like gen_rtx_raw_CONST_VEC, but use the zero vector when
5788 all elements are zero, and the one vector when all elements are one. */
5790 gen_rtx_CONST_VECTOR (machine_mode mode, rtvec v)
5792 machine_mode inner = GET_MODE_INNER (mode);
5793 int nunits = GET_MODE_NUNITS (mode);
5794 rtx x;
5795 int i;
5797 /* Check to see if all of the elements have the same value. */
5798 x = RTVEC_ELT (v, nunits - 1);
5799 for (i = nunits - 2; i >= 0; i--)
5800 if (RTVEC_ELT (v, i) != x)
5801 break;
5803 /* If the values are all the same, check to see if we can use one of the
5804 standard constant vectors. */
5805 if (i == -1)
5807 if (x == CONST0_RTX (inner))
5808 return CONST0_RTX (mode);
5809 else if (x == CONST1_RTX (inner))
5810 return CONST1_RTX (mode);
5811 else if (x == CONSTM1_RTX (inner))
5812 return CONSTM1_RTX (mode);
5815 return gen_rtx_raw_CONST_VECTOR (mode, v);
5818 /* Initialise global register information required by all functions. */
5820 void
5821 init_emit_regs (void)
5823 int i;
5824 machine_mode mode;
5825 mem_attrs *attrs;
5827 /* Reset register attributes */
5828 reg_attrs_htab->empty ();
5830 /* We need reg_raw_mode, so initialize the modes now. */
5831 init_reg_modes_target ();
5833 /* Assign register numbers to the globally defined register rtx. */
5834 stack_pointer_rtx = gen_raw_REG (Pmode, STACK_POINTER_REGNUM);
5835 frame_pointer_rtx = gen_raw_REG (Pmode, FRAME_POINTER_REGNUM);
5836 hard_frame_pointer_rtx = gen_raw_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
5837 arg_pointer_rtx = gen_raw_REG (Pmode, ARG_POINTER_REGNUM);
5838 virtual_incoming_args_rtx =
5839 gen_raw_REG (Pmode, VIRTUAL_INCOMING_ARGS_REGNUM);
5840 virtual_stack_vars_rtx =
5841 gen_raw_REG (Pmode, VIRTUAL_STACK_VARS_REGNUM);
5842 virtual_stack_dynamic_rtx =
5843 gen_raw_REG (Pmode, VIRTUAL_STACK_DYNAMIC_REGNUM);
5844 virtual_outgoing_args_rtx =
5845 gen_raw_REG (Pmode, VIRTUAL_OUTGOING_ARGS_REGNUM);
5846 virtual_cfa_rtx = gen_raw_REG (Pmode, VIRTUAL_CFA_REGNUM);
5847 virtual_preferred_stack_boundary_rtx =
5848 gen_raw_REG (Pmode, VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM);
5850 /* Initialize RTL for commonly used hard registers. These are
5851 copied into regno_reg_rtx as we begin to compile each function. */
5852 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5853 initial_regno_reg_rtx[i] = gen_raw_REG (reg_raw_mode[i], i);
5855 #ifdef RETURN_ADDRESS_POINTER_REGNUM
5856 return_address_pointer_rtx
5857 = gen_raw_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM);
5858 #endif
5860 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
5861 pic_offset_table_rtx = gen_raw_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
5862 else
5863 pic_offset_table_rtx = NULL_RTX;
5865 for (i = 0; i < (int) MAX_MACHINE_MODE; i++)
5867 mode = (machine_mode) i;
5868 attrs = ggc_cleared_alloc<mem_attrs> ();
5869 attrs->align = BITS_PER_UNIT;
5870 attrs->addrspace = ADDR_SPACE_GENERIC;
5871 if (mode != BLKmode)
5873 attrs->size_known_p = true;
5874 attrs->size = GET_MODE_SIZE (mode);
5875 if (STRICT_ALIGNMENT)
5876 attrs->align = GET_MODE_ALIGNMENT (mode);
5878 mode_mem_attrs[i] = attrs;
5882 /* Initialize global machine_mode variables. */
5884 void
5885 init_derived_machine_modes (void)
5887 byte_mode = VOIDmode;
5888 word_mode = VOIDmode;
5890 for (machine_mode mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5891 mode != VOIDmode;
5892 mode = GET_MODE_WIDER_MODE (mode))
5894 if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
5895 && byte_mode == VOIDmode)
5896 byte_mode = mode;
5898 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
5899 && word_mode == VOIDmode)
5900 word_mode = mode;
5903 ptr_mode = mode_for_size (POINTER_SIZE, GET_MODE_CLASS (Pmode), 0);
5906 /* Create some permanent unique rtl objects shared between all functions. */
5908 void
5909 init_emit_once (void)
5911 int i;
5912 machine_mode mode;
5913 machine_mode double_mode;
5915 /* Initialize the CONST_INT, CONST_WIDE_INT, CONST_DOUBLE,
5916 CONST_FIXED, and memory attribute hash tables. */
5917 const_int_htab = hash_table<const_int_hasher>::create_ggc (37);
5919 #if TARGET_SUPPORTS_WIDE_INT
5920 const_wide_int_htab = hash_table<const_wide_int_hasher>::create_ggc (37);
5921 #endif
5922 const_double_htab = hash_table<const_double_hasher>::create_ggc (37);
5924 const_fixed_htab = hash_table<const_fixed_hasher>::create_ggc (37);
5926 reg_attrs_htab = hash_table<reg_attr_hasher>::create_ggc (37);
5928 #ifdef INIT_EXPANDERS
5929 /* This is to initialize {init|mark|free}_machine_status before the first
5930 call to push_function_context_to. This is needed by the Chill front
5931 end which calls push_function_context_to before the first call to
5932 init_function_start. */
5933 INIT_EXPANDERS;
5934 #endif
5936 /* Create the unique rtx's for certain rtx codes and operand values. */
5938 /* Don't use gen_rtx_CONST_INT here since gen_rtx_CONST_INT in this case
5939 tries to use these variables. */
5940 for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
5941 const_int_rtx[i + MAX_SAVED_CONST_INT] =
5942 gen_rtx_raw_CONST_INT (VOIDmode, (HOST_WIDE_INT) i);
5944 if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT
5945 && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT)
5946 const_true_rtx = const_int_rtx[STORE_FLAG_VALUE + MAX_SAVED_CONST_INT];
5947 else
5948 const_true_rtx = gen_rtx_CONST_INT (VOIDmode, STORE_FLAG_VALUE);
5950 double_mode = mode_for_size (DOUBLE_TYPE_SIZE, MODE_FLOAT, 0);
5952 real_from_integer (&dconst0, double_mode, 0, SIGNED);
5953 real_from_integer (&dconst1, double_mode, 1, SIGNED);
5954 real_from_integer (&dconst2, double_mode, 2, SIGNED);
5956 dconstm1 = dconst1;
5957 dconstm1.sign = 1;
5959 dconsthalf = dconst1;
5960 SET_REAL_EXP (&dconsthalf, REAL_EXP (&dconsthalf) - 1);
5962 for (i = 0; i < 3; i++)
5964 const REAL_VALUE_TYPE *const r =
5965 (i == 0 ? &dconst0 : i == 1 ? &dconst1 : &dconst2);
5967 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
5968 mode != VOIDmode;
5969 mode = GET_MODE_WIDER_MODE (mode))
5970 const_tiny_rtx[i][(int) mode] =
5971 CONST_DOUBLE_FROM_REAL_VALUE (*r, mode);
5973 for (mode = GET_CLASS_NARROWEST_MODE (MODE_DECIMAL_FLOAT);
5974 mode != VOIDmode;
5975 mode = GET_MODE_WIDER_MODE (mode))
5976 const_tiny_rtx[i][(int) mode] =
5977 CONST_DOUBLE_FROM_REAL_VALUE (*r, mode);
5979 const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
5981 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5982 mode != VOIDmode;
5983 mode = GET_MODE_WIDER_MODE (mode))
5984 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5986 for (mode = MIN_MODE_PARTIAL_INT;
5987 mode <= MAX_MODE_PARTIAL_INT;
5988 mode = (machine_mode)((int)(mode) + 1))
5989 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5992 const_tiny_rtx[3][(int) VOIDmode] = constm1_rtx;
5994 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5995 mode != VOIDmode;
5996 mode = GET_MODE_WIDER_MODE (mode))
5997 const_tiny_rtx[3][(int) mode] = constm1_rtx;
5999 for (mode = MIN_MODE_PARTIAL_INT;
6000 mode <= MAX_MODE_PARTIAL_INT;
6001 mode = (machine_mode)((int)(mode) + 1))
6002 const_tiny_rtx[3][(int) mode] = constm1_rtx;
6004 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_INT);
6005 mode != VOIDmode;
6006 mode = GET_MODE_WIDER_MODE (mode))
6008 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
6009 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
6012 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
6013 mode != VOIDmode;
6014 mode = GET_MODE_WIDER_MODE (mode))
6016 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
6017 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
6020 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
6021 mode != VOIDmode;
6022 mode = GET_MODE_WIDER_MODE (mode))
6024 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6025 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6026 const_tiny_rtx[3][(int) mode] = gen_const_vector (mode, 3);
6029 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
6030 mode != VOIDmode;
6031 mode = GET_MODE_WIDER_MODE (mode))
6033 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6034 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6037 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FRACT);
6038 mode != VOIDmode;
6039 mode = GET_MODE_WIDER_MODE (mode))
6041 FCONST0 (mode).data.high = 0;
6042 FCONST0 (mode).data.low = 0;
6043 FCONST0 (mode).mode = mode;
6044 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6045 FCONST0 (mode), mode);
6048 for (mode = GET_CLASS_NARROWEST_MODE (MODE_UFRACT);
6049 mode != VOIDmode;
6050 mode = GET_MODE_WIDER_MODE (mode))
6052 FCONST0 (mode).data.high = 0;
6053 FCONST0 (mode).data.low = 0;
6054 FCONST0 (mode).mode = mode;
6055 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6056 FCONST0 (mode), mode);
6059 for (mode = GET_CLASS_NARROWEST_MODE (MODE_ACCUM);
6060 mode != VOIDmode;
6061 mode = GET_MODE_WIDER_MODE (mode))
6063 FCONST0 (mode).data.high = 0;
6064 FCONST0 (mode).data.low = 0;
6065 FCONST0 (mode).mode = mode;
6066 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6067 FCONST0 (mode), mode);
6069 /* We store the value 1. */
6070 FCONST1 (mode).data.high = 0;
6071 FCONST1 (mode).data.low = 0;
6072 FCONST1 (mode).mode = mode;
6073 FCONST1 (mode).data
6074 = double_int_one.lshift (GET_MODE_FBIT (mode),
6075 HOST_BITS_PER_DOUBLE_INT,
6076 SIGNED_FIXED_POINT_MODE_P (mode));
6077 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6078 FCONST1 (mode), mode);
6081 for (mode = GET_CLASS_NARROWEST_MODE (MODE_UACCUM);
6082 mode != VOIDmode;
6083 mode = GET_MODE_WIDER_MODE (mode))
6085 FCONST0 (mode).data.high = 0;
6086 FCONST0 (mode).data.low = 0;
6087 FCONST0 (mode).mode = mode;
6088 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6089 FCONST0 (mode), mode);
6091 /* We store the value 1. */
6092 FCONST1 (mode).data.high = 0;
6093 FCONST1 (mode).data.low = 0;
6094 FCONST1 (mode).mode = mode;
6095 FCONST1 (mode).data
6096 = double_int_one.lshift (GET_MODE_FBIT (mode),
6097 HOST_BITS_PER_DOUBLE_INT,
6098 SIGNED_FIXED_POINT_MODE_P (mode));
6099 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
6100 FCONST1 (mode), mode);
6103 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FRACT);
6104 mode != VOIDmode;
6105 mode = GET_MODE_WIDER_MODE (mode))
6107 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6110 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_UFRACT);
6111 mode != VOIDmode;
6112 mode = GET_MODE_WIDER_MODE (mode))
6114 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6117 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_ACCUM);
6118 mode != VOIDmode;
6119 mode = GET_MODE_WIDER_MODE (mode))
6121 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6122 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6125 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_UACCUM);
6126 mode != VOIDmode;
6127 mode = GET_MODE_WIDER_MODE (mode))
6129 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
6130 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
6133 for (i = (int) CCmode; i < (int) MAX_MACHINE_MODE; ++i)
6134 if (GET_MODE_CLASS ((machine_mode) i) == MODE_CC)
6135 const_tiny_rtx[0][i] = const0_rtx;
6137 const_tiny_rtx[0][(int) BImode] = const0_rtx;
6138 if (STORE_FLAG_VALUE == 1)
6139 const_tiny_rtx[1][(int) BImode] = const1_rtx;
6141 for (mode = GET_CLASS_NARROWEST_MODE (MODE_POINTER_BOUNDS);
6142 mode != VOIDmode;
6143 mode = GET_MODE_WIDER_MODE (mode))
6145 wide_int wi_zero = wi::zero (GET_MODE_PRECISION (mode));
6146 const_tiny_rtx[0][mode] = immed_wide_int_const (wi_zero, mode);
6149 pc_rtx = gen_rtx_fmt_ (PC, VOIDmode);
6150 ret_rtx = gen_rtx_fmt_ (RETURN, VOIDmode);
6151 simple_return_rtx = gen_rtx_fmt_ (SIMPLE_RETURN, VOIDmode);
6152 cc0_rtx = gen_rtx_fmt_ (CC0, VOIDmode);
6155 /* Produce exact duplicate of insn INSN after AFTER.
6156 Care updating of libcall regions if present. */
6158 rtx_insn *
6159 emit_copy_of_insn_after (rtx_insn *insn, rtx_insn *after)
6161 rtx_insn *new_rtx;
6162 rtx link;
6164 switch (GET_CODE (insn))
6166 case INSN:
6167 new_rtx = emit_insn_after (copy_insn (PATTERN (insn)), after);
6168 break;
6170 case JUMP_INSN:
6171 new_rtx = emit_jump_insn_after (copy_insn (PATTERN (insn)), after);
6172 CROSSING_JUMP_P (new_rtx) = CROSSING_JUMP_P (insn);
6173 break;
6175 case DEBUG_INSN:
6176 new_rtx = emit_debug_insn_after (copy_insn (PATTERN (insn)), after);
6177 break;
6179 case CALL_INSN:
6180 new_rtx = emit_call_insn_after (copy_insn (PATTERN (insn)), after);
6181 if (CALL_INSN_FUNCTION_USAGE (insn))
6182 CALL_INSN_FUNCTION_USAGE (new_rtx)
6183 = copy_insn (CALL_INSN_FUNCTION_USAGE (insn));
6184 SIBLING_CALL_P (new_rtx) = SIBLING_CALL_P (insn);
6185 RTL_CONST_CALL_P (new_rtx) = RTL_CONST_CALL_P (insn);
6186 RTL_PURE_CALL_P (new_rtx) = RTL_PURE_CALL_P (insn);
6187 RTL_LOOPING_CONST_OR_PURE_CALL_P (new_rtx)
6188 = RTL_LOOPING_CONST_OR_PURE_CALL_P (insn);
6189 break;
6191 default:
6192 gcc_unreachable ();
6195 /* Update LABEL_NUSES. */
6196 mark_jump_label (PATTERN (new_rtx), new_rtx, 0);
6198 INSN_LOCATION (new_rtx) = INSN_LOCATION (insn);
6200 /* If the old insn is frame related, then so is the new one. This is
6201 primarily needed for IA-64 unwind info which marks epilogue insns,
6202 which may be duplicated by the basic block reordering code. */
6203 RTX_FRAME_RELATED_P (new_rtx) = RTX_FRAME_RELATED_P (insn);
6205 /* Copy all REG_NOTES except REG_LABEL_OPERAND since mark_jump_label
6206 will make them. REG_LABEL_TARGETs are created there too, but are
6207 supposed to be sticky, so we copy them. */
6208 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
6209 if (REG_NOTE_KIND (link) != REG_LABEL_OPERAND)
6211 if (GET_CODE (link) == EXPR_LIST)
6212 add_reg_note (new_rtx, REG_NOTE_KIND (link),
6213 copy_insn_1 (XEXP (link, 0)));
6214 else
6215 add_shallow_copy_of_reg_note (new_rtx, link);
6218 INSN_CODE (new_rtx) = INSN_CODE (insn);
6219 return new_rtx;
6222 static GTY((deletable)) rtx hard_reg_clobbers [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
6224 gen_hard_reg_clobber (machine_mode mode, unsigned int regno)
6226 if (hard_reg_clobbers[mode][regno])
6227 return hard_reg_clobbers[mode][regno];
6228 else
6229 return (hard_reg_clobbers[mode][regno] =
6230 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (mode, regno)));
6233 location_t prologue_location;
6234 location_t epilogue_location;
6236 /* Hold current location information and last location information, so the
6237 datastructures are built lazily only when some instructions in given
6238 place are needed. */
6239 static location_t curr_location;
6241 /* Allocate insn location datastructure. */
6242 void
6243 insn_locations_init (void)
6245 prologue_location = epilogue_location = 0;
6246 curr_location = UNKNOWN_LOCATION;
6249 /* At the end of emit stage, clear current location. */
6250 void
6251 insn_locations_finalize (void)
6253 epilogue_location = curr_location;
6254 curr_location = UNKNOWN_LOCATION;
6257 /* Set current location. */
6258 void
6259 set_curr_insn_location (location_t location)
6261 curr_location = location;
6264 /* Get current location. */
6265 location_t
6266 curr_insn_location (void)
6268 return curr_location;
6271 /* Return lexical scope block insn belongs to. */
6272 tree
6273 insn_scope (const rtx_insn *insn)
6275 return LOCATION_BLOCK (INSN_LOCATION (insn));
6278 /* Return line number of the statement that produced this insn. */
6280 insn_line (const rtx_insn *insn)
6282 return LOCATION_LINE (INSN_LOCATION (insn));
6285 /* Return source file of the statement that produced this insn. */
6286 const char *
6287 insn_file (const rtx_insn *insn)
6289 return LOCATION_FILE (INSN_LOCATION (insn));
6292 /* Return expanded location of the statement that produced this insn. */
6293 expanded_location
6294 insn_location (const rtx_insn *insn)
6296 return expand_location (INSN_LOCATION (insn));
6299 /* Return true if memory model MODEL requires a pre-operation (release-style)
6300 barrier or a post-operation (acquire-style) barrier. While not universal,
6301 this function matches behavior of several targets. */
6303 bool
6304 need_atomic_barrier_p (enum memmodel model, bool pre)
6306 switch (model & MEMMODEL_MASK)
6308 case MEMMODEL_RELAXED:
6309 case MEMMODEL_CONSUME:
6310 return false;
6311 case MEMMODEL_RELEASE:
6312 return pre;
6313 case MEMMODEL_ACQUIRE:
6314 return !pre;
6315 case MEMMODEL_ACQ_REL:
6316 case MEMMODEL_SEQ_CST:
6317 return true;
6318 default:
6319 gcc_unreachable ();
6323 #include "gt-emit-rtl.h"