2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / emit-rtl.c
blob24e3c92151b85bbf2443c46c5a1d9aa95d0546e3
1 /* Emit RTL for the GCC expander.
2 Copyright (C) 1987-2013 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 "gimple.h"
43 #include "tm_p.h"
44 #include "flags.h"
45 #include "function.h"
46 #include "stringpool.h"
47 #include "expr.h"
48 #include "regs.h"
49 #include "hard-reg-set.h"
50 #include "hashtab.h"
51 #include "insn-config.h"
52 #include "recog.h"
53 #include "bitmap.h"
54 #include "basic-block.h"
55 #include "ggc.h"
56 #include "debug.h"
57 #include "langhooks.h"
58 #include "df.h"
59 #include "params.h"
60 #include "target.h"
61 #include "tree-eh.h"
63 struct target_rtl default_target_rtl;
64 #if SWITCHABLE_TARGET
65 struct target_rtl *this_target_rtl = &default_target_rtl;
66 #endif
68 #define initial_regno_reg_rtx (this_target_rtl->x_initial_regno_reg_rtx)
70 /* Commonly used modes. */
72 enum machine_mode byte_mode; /* Mode whose width is BITS_PER_UNIT. */
73 enum machine_mode word_mode; /* Mode whose width is BITS_PER_WORD. */
74 enum machine_mode double_mode; /* Mode whose width is DOUBLE_TYPE_SIZE. */
75 enum machine_mode ptr_mode; /* Mode whose width is POINTER_SIZE. */
77 /* Datastructures maintained for currently processed function in RTL form. */
79 struct rtl_data x_rtl;
81 /* Indexed by pseudo register number, gives the rtx for that pseudo.
82 Allocated in parallel with regno_pointer_align.
83 FIXME: We could put it into emit_status struct, but gengtype is not able to deal
84 with length attribute nested in top level structures. */
86 rtx * regno_reg_rtx;
88 /* This is *not* reset after each function. It gives each CODE_LABEL
89 in the entire compilation a unique label number. */
91 static GTY(()) int label_num = 1;
93 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
94 the values of 0, 1, and 2. For the integer entries and VOIDmode, we
95 record a copy of const[012]_rtx and constm1_rtx. CONSTM1_RTX
96 is set only for MODE_INT and MODE_VECTOR_INT modes. */
98 rtx const_tiny_rtx[4][(int) MAX_MACHINE_MODE];
100 rtx const_true_rtx;
102 REAL_VALUE_TYPE dconst0;
103 REAL_VALUE_TYPE dconst1;
104 REAL_VALUE_TYPE dconst2;
105 REAL_VALUE_TYPE dconstm1;
106 REAL_VALUE_TYPE dconsthalf;
108 /* Record fixed-point constant 0 and 1. */
109 FIXED_VALUE_TYPE fconst0[MAX_FCONST0];
110 FIXED_VALUE_TYPE fconst1[MAX_FCONST1];
112 /* We make one copy of (const_int C) where C is in
113 [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
114 to save space during the compilation and simplify comparisons of
115 integers. */
117 rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
119 /* Standard pieces of rtx, to be substituted directly into things. */
120 rtx pc_rtx;
121 rtx ret_rtx;
122 rtx simple_return_rtx;
123 rtx cc0_rtx;
125 /* A hash table storing CONST_INTs whose absolute value is greater
126 than MAX_SAVED_CONST_INT. */
128 static GTY ((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
129 htab_t const_int_htab;
131 /* A hash table storing memory attribute structures. */
132 static GTY ((if_marked ("ggc_marked_p"), param_is (struct mem_attrs)))
133 htab_t mem_attrs_htab;
135 /* A hash table storing register attribute structures. */
136 static GTY ((if_marked ("ggc_marked_p"), param_is (struct reg_attrs)))
137 htab_t reg_attrs_htab;
139 /* A hash table storing all CONST_DOUBLEs. */
140 static GTY ((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
141 htab_t const_double_htab;
143 /* A hash table storing all CONST_FIXEDs. */
144 static GTY ((if_marked ("ggc_marked_p"), param_is (struct rtx_def)))
145 htab_t const_fixed_htab;
147 #define cur_insn_uid (crtl->emit.x_cur_insn_uid)
148 #define cur_debug_insn_uid (crtl->emit.x_cur_debug_insn_uid)
149 #define first_label_num (crtl->emit.x_first_label_num)
151 static rtx change_address_1 (rtx, enum machine_mode, rtx, int);
152 static void set_used_decls (tree);
153 static void mark_label_nuses (rtx);
154 static hashval_t const_int_htab_hash (const void *);
155 static int const_int_htab_eq (const void *, const void *);
156 static hashval_t const_double_htab_hash (const void *);
157 static int const_double_htab_eq (const void *, const void *);
158 static rtx lookup_const_double (rtx);
159 static hashval_t const_fixed_htab_hash (const void *);
160 static int const_fixed_htab_eq (const void *, const void *);
161 static rtx lookup_const_fixed (rtx);
162 static hashval_t mem_attrs_htab_hash (const void *);
163 static int mem_attrs_htab_eq (const void *, const void *);
164 static hashval_t reg_attrs_htab_hash (const void *);
165 static int reg_attrs_htab_eq (const void *, const void *);
166 static reg_attrs *get_reg_attrs (tree, int);
167 static rtx gen_const_vector (enum machine_mode, int);
168 static void copy_rtx_if_shared_1 (rtx *orig);
170 /* Probability of the conditional branch currently proceeded by try_split.
171 Set to -1 otherwise. */
172 int split_branch_probability = -1;
174 /* Returns a hash code for X (which is a really a CONST_INT). */
176 static hashval_t
177 const_int_htab_hash (const void *x)
179 return (hashval_t) INTVAL ((const_rtx) x);
182 /* Returns nonzero if the value represented by X (which is really a
183 CONST_INT) is the same as that given by Y (which is really a
184 HOST_WIDE_INT *). */
186 static int
187 const_int_htab_eq (const void *x, const void *y)
189 return (INTVAL ((const_rtx) x) == *((const HOST_WIDE_INT *) y));
192 /* Returns a hash code for X (which is really a CONST_DOUBLE). */
193 static hashval_t
194 const_double_htab_hash (const void *x)
196 const_rtx const value = (const_rtx) x;
197 hashval_t h;
199 if (GET_MODE (value) == VOIDmode)
200 h = CONST_DOUBLE_LOW (value) ^ CONST_DOUBLE_HIGH (value);
201 else
203 h = real_hash (CONST_DOUBLE_REAL_VALUE (value));
204 /* MODE is used in the comparison, so it should be in the hash. */
205 h ^= GET_MODE (value);
207 return h;
210 /* Returns nonzero if the value represented by X (really a ...)
211 is the same as that represented by Y (really a ...) */
212 static int
213 const_double_htab_eq (const void *x, const void *y)
215 const_rtx const a = (const_rtx)x, b = (const_rtx)y;
217 if (GET_MODE (a) != GET_MODE (b))
218 return 0;
219 if (GET_MODE (a) == VOIDmode)
220 return (CONST_DOUBLE_LOW (a) == CONST_DOUBLE_LOW (b)
221 && CONST_DOUBLE_HIGH (a) == CONST_DOUBLE_HIGH (b));
222 else
223 return real_identical (CONST_DOUBLE_REAL_VALUE (a),
224 CONST_DOUBLE_REAL_VALUE (b));
227 /* Returns a hash code for X (which is really a CONST_FIXED). */
229 static hashval_t
230 const_fixed_htab_hash (const void *x)
232 const_rtx const value = (const_rtx) x;
233 hashval_t h;
235 h = fixed_hash (CONST_FIXED_VALUE (value));
236 /* MODE is used in the comparison, so it should be in the hash. */
237 h ^= GET_MODE (value);
238 return h;
241 /* Returns nonzero if the value represented by X (really a ...)
242 is the same as that represented by Y (really a ...). */
244 static int
245 const_fixed_htab_eq (const void *x, const void *y)
247 const_rtx const a = (const_rtx) x, b = (const_rtx) y;
249 if (GET_MODE (a) != GET_MODE (b))
250 return 0;
251 return fixed_identical (CONST_FIXED_VALUE (a), CONST_FIXED_VALUE (b));
254 /* Returns a hash code for X (which is a really a mem_attrs *). */
256 static hashval_t
257 mem_attrs_htab_hash (const void *x)
259 const mem_attrs *const p = (const mem_attrs *) x;
261 return (p->alias ^ (p->align * 1000)
262 ^ (p->addrspace * 4000)
263 ^ ((p->offset_known_p ? p->offset : 0) * 50000)
264 ^ ((p->size_known_p ? p->size : 0) * 2500000)
265 ^ (size_t) iterative_hash_expr (p->expr, 0));
268 /* Return true if the given memory attributes are equal. */
270 static bool
271 mem_attrs_eq_p (const struct mem_attrs *p, const struct mem_attrs *q)
273 return (p->alias == q->alias
274 && p->offset_known_p == q->offset_known_p
275 && (!p->offset_known_p || p->offset == q->offset)
276 && p->size_known_p == q->size_known_p
277 && (!p->size_known_p || p->size == q->size)
278 && p->align == q->align
279 && p->addrspace == q->addrspace
280 && (p->expr == q->expr
281 || (p->expr != NULL_TREE && q->expr != NULL_TREE
282 && operand_equal_p (p->expr, q->expr, 0))));
285 /* Returns nonzero if the value represented by X (which is really a
286 mem_attrs *) is the same as that given by Y (which is also really a
287 mem_attrs *). */
289 static int
290 mem_attrs_htab_eq (const void *x, const void *y)
292 return mem_attrs_eq_p ((const mem_attrs *) x, (const mem_attrs *) y);
295 /* Set MEM's memory attributes so that they are the same as ATTRS. */
297 static void
298 set_mem_attrs (rtx mem, mem_attrs *attrs)
300 void **slot;
302 /* If everything is the default, we can just clear the attributes. */
303 if (mem_attrs_eq_p (attrs, mode_mem_attrs[(int) GET_MODE (mem)]))
305 MEM_ATTRS (mem) = 0;
306 return;
309 slot = htab_find_slot (mem_attrs_htab, attrs, INSERT);
310 if (*slot == 0)
312 *slot = ggc_alloc_mem_attrs ();
313 memcpy (*slot, attrs, sizeof (mem_attrs));
316 MEM_ATTRS (mem) = (mem_attrs *) *slot;
319 /* Returns a hash code for X (which is a really a reg_attrs *). */
321 static hashval_t
322 reg_attrs_htab_hash (const void *x)
324 const reg_attrs *const p = (const reg_attrs *) x;
326 return ((p->offset * 1000) ^ (intptr_t) p->decl);
329 /* Returns nonzero if the value represented by X (which is really a
330 reg_attrs *) is the same as that given by Y (which is also really a
331 reg_attrs *). */
333 static int
334 reg_attrs_htab_eq (const void *x, const void *y)
336 const reg_attrs *const p = (const reg_attrs *) x;
337 const reg_attrs *const q = (const reg_attrs *) y;
339 return (p->decl == q->decl && p->offset == q->offset);
341 /* Allocate a new reg_attrs structure and insert it into the hash table if
342 one identical to it is not already in the table. We are doing this for
343 MEM of mode MODE. */
345 static reg_attrs *
346 get_reg_attrs (tree decl, int offset)
348 reg_attrs attrs;
349 void **slot;
351 /* If everything is the default, we can just return zero. */
352 if (decl == 0 && offset == 0)
353 return 0;
355 attrs.decl = decl;
356 attrs.offset = offset;
358 slot = htab_find_slot (reg_attrs_htab, &attrs, INSERT);
359 if (*slot == 0)
361 *slot = ggc_alloc_reg_attrs ();
362 memcpy (*slot, &attrs, sizeof (reg_attrs));
365 return (reg_attrs *) *slot;
369 #if !HAVE_blockage
370 /* Generate an empty ASM_INPUT, which is used to block attempts to schedule,
371 and to block register equivalences to be seen across this insn. */
374 gen_blockage (void)
376 rtx x = gen_rtx_ASM_INPUT (VOIDmode, "");
377 MEM_VOLATILE_P (x) = true;
378 return x;
380 #endif
383 /* Generate a new REG rtx. Make sure ORIGINAL_REGNO is set properly, and
384 don't attempt to share with the various global pieces of rtl (such as
385 frame_pointer_rtx). */
388 gen_raw_REG (enum machine_mode mode, int regno)
390 rtx x = gen_rtx_raw_REG (mode, regno);
391 ORIGINAL_REGNO (x) = regno;
392 return x;
395 /* There are some RTL codes that require special attention; the generation
396 functions do the raw handling. If you add to this list, modify
397 special_rtx in gengenrtl.c as well. */
400 gen_rtx_CONST_INT (enum machine_mode mode ATTRIBUTE_UNUSED, HOST_WIDE_INT arg)
402 void **slot;
404 if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
405 return const_int_rtx[arg + MAX_SAVED_CONST_INT];
407 #if STORE_FLAG_VALUE != 1 && STORE_FLAG_VALUE != -1
408 if (const_true_rtx && arg == STORE_FLAG_VALUE)
409 return const_true_rtx;
410 #endif
412 /* Look up the CONST_INT in the hash table. */
413 slot = htab_find_slot_with_hash (const_int_htab, &arg,
414 (hashval_t) arg, INSERT);
415 if (*slot == 0)
416 *slot = gen_rtx_raw_CONST_INT (VOIDmode, arg);
418 return (rtx) *slot;
422 gen_int_mode (HOST_WIDE_INT c, enum machine_mode mode)
424 return GEN_INT (trunc_int_for_mode (c, mode));
427 /* CONST_DOUBLEs might be created from pairs of integers, or from
428 REAL_VALUE_TYPEs. Also, their length is known only at run time,
429 so we cannot use gen_rtx_raw_CONST_DOUBLE. */
431 /* Determine whether REAL, a CONST_DOUBLE, already exists in the
432 hash table. If so, return its counterpart; otherwise add it
433 to the hash table and return it. */
434 static rtx
435 lookup_const_double (rtx real)
437 void **slot = htab_find_slot (const_double_htab, real, INSERT);
438 if (*slot == 0)
439 *slot = real;
441 return (rtx) *slot;
444 /* Return a CONST_DOUBLE rtx for a floating-point value specified by
445 VALUE in mode MODE. */
447 const_double_from_real_value (REAL_VALUE_TYPE value, enum machine_mode mode)
449 rtx real = rtx_alloc (CONST_DOUBLE);
450 PUT_MODE (real, mode);
452 real->u.rv = value;
454 return lookup_const_double (real);
457 /* Determine whether FIXED, a CONST_FIXED, already exists in the
458 hash table. If so, return its counterpart; otherwise add it
459 to the hash table and return it. */
461 static rtx
462 lookup_const_fixed (rtx fixed)
464 void **slot = htab_find_slot (const_fixed_htab, fixed, INSERT);
465 if (*slot == 0)
466 *slot = fixed;
468 return (rtx) *slot;
471 /* Return a CONST_FIXED rtx for a fixed-point value specified by
472 VALUE in mode MODE. */
475 const_fixed_from_fixed_value (FIXED_VALUE_TYPE value, enum machine_mode mode)
477 rtx fixed = rtx_alloc (CONST_FIXED);
478 PUT_MODE (fixed, mode);
480 fixed->u.fv = value;
482 return lookup_const_fixed (fixed);
485 /* Constructs double_int from rtx CST. */
487 double_int
488 rtx_to_double_int (const_rtx cst)
490 double_int r;
492 if (CONST_INT_P (cst))
493 r = double_int::from_shwi (INTVAL (cst));
494 else if (CONST_DOUBLE_AS_INT_P (cst))
496 r.low = CONST_DOUBLE_LOW (cst);
497 r.high = CONST_DOUBLE_HIGH (cst);
499 else
500 gcc_unreachable ();
502 return r;
506 /* Return a CONST_DOUBLE or CONST_INT for a value specified as
507 a double_int. */
510 immed_double_int_const (double_int i, enum machine_mode mode)
512 return immed_double_const (i.low, i.high, mode);
515 /* Return a CONST_DOUBLE or CONST_INT for a value specified as a pair
516 of ints: I0 is the low-order word and I1 is the high-order word.
517 For values that are larger than HOST_BITS_PER_DOUBLE_INT, the
518 implied upper bits are copies of the high bit of i1. The value
519 itself is neither signed nor unsigned. Do not use this routine for
520 non-integer modes; convert to REAL_VALUE_TYPE and use
521 CONST_DOUBLE_FROM_REAL_VALUE. */
524 immed_double_const (HOST_WIDE_INT i0, HOST_WIDE_INT i1, enum machine_mode mode)
526 rtx value;
527 unsigned int i;
529 /* There are the following cases (note that there are no modes with
530 HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode) < HOST_BITS_PER_DOUBLE_INT):
532 1) If GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT, then we use
533 gen_int_mode.
534 2) If the value of the integer fits into HOST_WIDE_INT anyway
535 (i.e., i1 consists only from copies of the sign bit, and sign
536 of i0 and i1 are the same), then we return a CONST_INT for i0.
537 3) Otherwise, we create a CONST_DOUBLE for i0 and i1. */
538 if (mode != VOIDmode)
540 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT
541 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT
542 /* We can get a 0 for an error mark. */
543 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
544 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT);
546 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
547 return gen_int_mode (i0, mode);
550 /* If this integer fits in one word, return a CONST_INT. */
551 if ((i1 == 0 && i0 >= 0) || (i1 == ~0 && i0 < 0))
552 return GEN_INT (i0);
554 /* We use VOIDmode for integers. */
555 value = rtx_alloc (CONST_DOUBLE);
556 PUT_MODE (value, VOIDmode);
558 CONST_DOUBLE_LOW (value) = i0;
559 CONST_DOUBLE_HIGH (value) = i1;
561 for (i = 2; i < (sizeof CONST_DOUBLE_FORMAT - 1); i++)
562 XWINT (value, i) = 0;
564 return lookup_const_double (value);
568 gen_rtx_REG (enum machine_mode mode, unsigned int regno)
570 /* In case the MD file explicitly references the frame pointer, have
571 all such references point to the same frame pointer. This is
572 used during frame pointer elimination to distinguish the explicit
573 references to these registers from pseudos that happened to be
574 assigned to them.
576 If we have eliminated the frame pointer or arg pointer, we will
577 be using it as a normal register, for example as a spill
578 register. In such cases, we might be accessing it in a mode that
579 is not Pmode and therefore cannot use the pre-allocated rtx.
581 Also don't do this when we are making new REGs in reload, since
582 we don't want to get confused with the real pointers. */
584 if (mode == Pmode && !reload_in_progress && !lra_in_progress)
586 if (regno == FRAME_POINTER_REGNUM
587 && (!reload_completed || frame_pointer_needed))
588 return frame_pointer_rtx;
589 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
590 if (regno == HARD_FRAME_POINTER_REGNUM
591 && (!reload_completed || frame_pointer_needed))
592 return hard_frame_pointer_rtx;
593 #endif
594 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM && !HARD_FRAME_POINTER_IS_ARG_POINTER
595 if (regno == ARG_POINTER_REGNUM)
596 return arg_pointer_rtx;
597 #endif
598 #ifdef RETURN_ADDRESS_POINTER_REGNUM
599 if (regno == RETURN_ADDRESS_POINTER_REGNUM)
600 return return_address_pointer_rtx;
601 #endif
602 if (regno == (unsigned) PIC_OFFSET_TABLE_REGNUM
603 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
604 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
605 return pic_offset_table_rtx;
606 if (regno == STACK_POINTER_REGNUM)
607 return stack_pointer_rtx;
610 #if 0
611 /* If the per-function register table has been set up, try to re-use
612 an existing entry in that table to avoid useless generation of RTL.
614 This code is disabled for now until we can fix the various backends
615 which depend on having non-shared hard registers in some cases. Long
616 term we want to re-enable this code as it can significantly cut down
617 on the amount of useless RTL that gets generated.
619 We'll also need to fix some code that runs after reload that wants to
620 set ORIGINAL_REGNO. */
622 if (cfun
623 && cfun->emit
624 && regno_reg_rtx
625 && regno < FIRST_PSEUDO_REGISTER
626 && reg_raw_mode[regno] == mode)
627 return regno_reg_rtx[regno];
628 #endif
630 return gen_raw_REG (mode, regno);
634 gen_rtx_MEM (enum machine_mode mode, rtx addr)
636 rtx rt = gen_rtx_raw_MEM (mode, addr);
638 /* This field is not cleared by the mere allocation of the rtx, so
639 we clear it here. */
640 MEM_ATTRS (rt) = 0;
642 return rt;
645 /* Generate a memory referring to non-trapping constant memory. */
648 gen_const_mem (enum machine_mode mode, rtx addr)
650 rtx mem = gen_rtx_MEM (mode, addr);
651 MEM_READONLY_P (mem) = 1;
652 MEM_NOTRAP_P (mem) = 1;
653 return mem;
656 /* Generate a MEM referring to fixed portions of the frame, e.g., register
657 save areas. */
660 gen_frame_mem (enum machine_mode mode, rtx addr)
662 rtx mem = gen_rtx_MEM (mode, addr);
663 MEM_NOTRAP_P (mem) = 1;
664 set_mem_alias_set (mem, get_frame_alias_set ());
665 return mem;
668 /* Generate a MEM referring to a temporary use of the stack, not part
669 of the fixed stack frame. For example, something which is pushed
670 by a target splitter. */
672 gen_tmp_stack_mem (enum machine_mode mode, rtx addr)
674 rtx mem = gen_rtx_MEM (mode, addr);
675 MEM_NOTRAP_P (mem) = 1;
676 if (!cfun->calls_alloca)
677 set_mem_alias_set (mem, get_frame_alias_set ());
678 return mem;
681 /* We want to create (subreg:OMODE (obj:IMODE) OFFSET). Return true if
682 this construct would be valid, and false otherwise. */
684 bool
685 validate_subreg (enum machine_mode omode, enum machine_mode imode,
686 const_rtx reg, unsigned int offset)
688 unsigned int isize = GET_MODE_SIZE (imode);
689 unsigned int osize = GET_MODE_SIZE (omode);
691 /* All subregs must be aligned. */
692 if (offset % osize != 0)
693 return false;
695 /* The subreg offset cannot be outside the inner object. */
696 if (offset >= isize)
697 return false;
699 /* ??? This should not be here. Temporarily continue to allow word_mode
700 subregs of anything. The most common offender is (subreg:SI (reg:DF)).
701 Generally, backends are doing something sketchy but it'll take time to
702 fix them all. */
703 if (omode == word_mode)
705 /* ??? Similarly, e.g. with (subreg:DF (reg:TI)). Though store_bit_field
706 is the culprit here, and not the backends. */
707 else if (osize >= UNITS_PER_WORD && isize >= osize)
709 /* Allow component subregs of complex and vector. Though given the below
710 extraction rules, it's not always clear what that means. */
711 else if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
712 && GET_MODE_INNER (imode) == omode)
714 /* ??? x86 sse code makes heavy use of *paradoxical* vector subregs,
715 i.e. (subreg:V4SF (reg:SF) 0). This surely isn't the cleanest way to
716 represent this. It's questionable if this ought to be represented at
717 all -- why can't this all be hidden in post-reload splitters that make
718 arbitrarily mode changes to the registers themselves. */
719 else if (VECTOR_MODE_P (omode) && GET_MODE_INNER (omode) == imode)
721 /* Subregs involving floating point modes are not allowed to
722 change size. Therefore (subreg:DI (reg:DF) 0) is fine, but
723 (subreg:SI (reg:DF) 0) isn't. */
724 else if (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))
726 if (! (isize == osize
727 /* LRA can use subreg to store a floating point value in
728 an integer mode. Although the floating point and the
729 integer modes need the same number of hard registers,
730 the size of floating point mode can be less than the
731 integer mode. LRA also uses subregs for a register
732 should be used in different mode in on insn. */
733 || lra_in_progress))
734 return false;
737 /* Paradoxical subregs must have offset zero. */
738 if (osize > isize)
739 return offset == 0;
741 /* This is a normal subreg. Verify that the offset is representable. */
743 /* For hard registers, we already have most of these rules collected in
744 subreg_offset_representable_p. */
745 if (reg && REG_P (reg) && HARD_REGISTER_P (reg))
747 unsigned int regno = REGNO (reg);
749 #ifdef CANNOT_CHANGE_MODE_CLASS
750 if ((COMPLEX_MODE_P (imode) || VECTOR_MODE_P (imode))
751 && GET_MODE_INNER (imode) == omode)
753 else if (REG_CANNOT_CHANGE_MODE_P (regno, imode, omode))
754 return false;
755 #endif
757 return subreg_offset_representable_p (regno, imode, offset, omode);
760 /* For pseudo registers, we want most of the same checks. Namely:
761 If the register no larger than a word, the subreg must be lowpart.
762 If the register is larger than a word, the subreg must be the lowpart
763 of a subword. A subreg does *not* perform arbitrary bit extraction.
764 Given that we've already checked mode/offset alignment, we only have
765 to check subword subregs here. */
766 if (osize < UNITS_PER_WORD
767 && ! (lra_in_progress && (FLOAT_MODE_P (imode) || FLOAT_MODE_P (omode))))
769 enum machine_mode wmode = isize > UNITS_PER_WORD ? word_mode : imode;
770 unsigned int low_off = subreg_lowpart_offset (omode, wmode);
771 if (offset % UNITS_PER_WORD != low_off)
772 return false;
774 return true;
778 gen_rtx_SUBREG (enum machine_mode mode, rtx reg, int offset)
780 gcc_assert (validate_subreg (mode, GET_MODE (reg), reg, offset));
781 return gen_rtx_raw_SUBREG (mode, reg, offset);
784 /* Generate a SUBREG representing the least-significant part of REG if MODE
785 is smaller than mode of REG, otherwise paradoxical SUBREG. */
788 gen_lowpart_SUBREG (enum machine_mode mode, rtx reg)
790 enum machine_mode inmode;
792 inmode = GET_MODE (reg);
793 if (inmode == VOIDmode)
794 inmode = mode;
795 return gen_rtx_SUBREG (mode, reg,
796 subreg_lowpart_offset (mode, inmode));
800 /* Create an rtvec and stores within it the RTXen passed in the arguments. */
802 rtvec
803 gen_rtvec (int n, ...)
805 int i;
806 rtvec rt_val;
807 va_list p;
809 va_start (p, n);
811 /* Don't allocate an empty rtvec... */
812 if (n == 0)
814 va_end (p);
815 return NULL_RTVEC;
818 rt_val = rtvec_alloc (n);
820 for (i = 0; i < n; i++)
821 rt_val->elem[i] = va_arg (p, rtx);
823 va_end (p);
824 return rt_val;
827 rtvec
828 gen_rtvec_v (int n, rtx *argp)
830 int i;
831 rtvec rt_val;
833 /* Don't allocate an empty rtvec... */
834 if (n == 0)
835 return NULL_RTVEC;
837 rt_val = rtvec_alloc (n);
839 for (i = 0; i < n; i++)
840 rt_val->elem[i] = *argp++;
842 return rt_val;
845 /* Return the number of bytes between the start of an OUTER_MODE
846 in-memory value and the start of an INNER_MODE in-memory value,
847 given that the former is a lowpart of the latter. It may be a
848 paradoxical lowpart, in which case the offset will be negative
849 on big-endian targets. */
852 byte_lowpart_offset (enum machine_mode outer_mode,
853 enum machine_mode inner_mode)
855 if (GET_MODE_SIZE (outer_mode) < GET_MODE_SIZE (inner_mode))
856 return subreg_lowpart_offset (outer_mode, inner_mode);
857 else
858 return -subreg_lowpart_offset (inner_mode, outer_mode);
861 /* Generate a REG rtx for a new pseudo register of mode MODE.
862 This pseudo is assigned the next sequential register number. */
865 gen_reg_rtx (enum machine_mode mode)
867 rtx val;
868 unsigned int align = GET_MODE_ALIGNMENT (mode);
870 gcc_assert (can_create_pseudo_p ());
872 /* If a virtual register with bigger mode alignment is generated,
873 increase stack alignment estimation because it might be spilled
874 to stack later. */
875 if (SUPPORTS_STACK_ALIGNMENT
876 && crtl->stack_alignment_estimated < align
877 && !crtl->stack_realign_processed)
879 unsigned int min_align = MINIMUM_ALIGNMENT (NULL, mode, align);
880 if (crtl->stack_alignment_estimated < min_align)
881 crtl->stack_alignment_estimated = min_align;
884 if (generating_concat_p
885 && (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
886 || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT))
888 /* For complex modes, don't make a single pseudo.
889 Instead, make a CONCAT of two pseudos.
890 This allows noncontiguous allocation of the real and imaginary parts,
891 which makes much better code. Besides, allocating DCmode
892 pseudos overstrains reload on some machines like the 386. */
893 rtx realpart, imagpart;
894 enum machine_mode partmode = GET_MODE_INNER (mode);
896 realpart = gen_reg_rtx (partmode);
897 imagpart = gen_reg_rtx (partmode);
898 return gen_rtx_CONCAT (mode, realpart, imagpart);
901 /* Make sure regno_pointer_align, and regno_reg_rtx are large
902 enough to have an element for this pseudo reg number. */
904 if (reg_rtx_no == crtl->emit.regno_pointer_align_length)
906 int old_size = crtl->emit.regno_pointer_align_length;
907 char *tmp;
908 rtx *new1;
910 tmp = XRESIZEVEC (char, crtl->emit.regno_pointer_align, old_size * 2);
911 memset (tmp + old_size, 0, old_size);
912 crtl->emit.regno_pointer_align = (unsigned char *) tmp;
914 new1 = GGC_RESIZEVEC (rtx, regno_reg_rtx, old_size * 2);
915 memset (new1 + old_size, 0, old_size * sizeof (rtx));
916 regno_reg_rtx = new1;
918 crtl->emit.regno_pointer_align_length = old_size * 2;
921 val = gen_raw_REG (mode, reg_rtx_no);
922 regno_reg_rtx[reg_rtx_no++] = val;
923 return val;
926 /* Return TRUE if REG is a PARM_DECL, FALSE otherwise. */
928 bool
929 reg_is_parm_p (rtx reg)
931 tree decl;
933 gcc_assert (REG_P (reg));
934 decl = REG_EXPR (reg);
935 return (decl && TREE_CODE (decl) == PARM_DECL);
938 /* Update NEW with the same attributes as REG, but with OFFSET added
939 to the REG_OFFSET. */
941 static void
942 update_reg_offset (rtx new_rtx, rtx reg, int offset)
944 REG_ATTRS (new_rtx) = get_reg_attrs (REG_EXPR (reg),
945 REG_OFFSET (reg) + offset);
948 /* Generate a register with same attributes as REG, but with OFFSET
949 added to the REG_OFFSET. */
952 gen_rtx_REG_offset (rtx reg, enum machine_mode mode, unsigned int regno,
953 int offset)
955 rtx new_rtx = gen_rtx_REG (mode, regno);
957 update_reg_offset (new_rtx, reg, offset);
958 return new_rtx;
961 /* Generate a new pseudo-register with the same attributes as REG, but
962 with OFFSET added to the REG_OFFSET. */
965 gen_reg_rtx_offset (rtx reg, enum machine_mode mode, int offset)
967 rtx new_rtx = gen_reg_rtx (mode);
969 update_reg_offset (new_rtx, reg, offset);
970 return new_rtx;
973 /* Adjust REG in-place so that it has mode MODE. It is assumed that the
974 new register is a (possibly paradoxical) lowpart of the old one. */
976 void
977 adjust_reg_mode (rtx reg, enum machine_mode mode)
979 update_reg_offset (reg, reg, byte_lowpart_offset (mode, GET_MODE (reg)));
980 PUT_MODE (reg, mode);
983 /* Copy REG's attributes from X, if X has any attributes. If REG and X
984 have different modes, REG is a (possibly paradoxical) lowpart of X. */
986 void
987 set_reg_attrs_from_value (rtx reg, rtx x)
989 int offset;
990 bool can_be_reg_pointer = true;
992 /* Don't call mark_reg_pointer for incompatible pointer sign
993 extension. */
994 while (GET_CODE (x) == SIGN_EXTEND
995 || GET_CODE (x) == ZERO_EXTEND
996 || GET_CODE (x) == TRUNCATE
997 || (GET_CODE (x) == SUBREG && subreg_lowpart_p (x)))
999 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1000 if ((GET_CODE (x) == SIGN_EXTEND && POINTERS_EXTEND_UNSIGNED)
1001 || (GET_CODE (x) != SIGN_EXTEND && ! POINTERS_EXTEND_UNSIGNED))
1002 can_be_reg_pointer = false;
1003 #endif
1004 x = XEXP (x, 0);
1007 /* Hard registers can be reused for multiple purposes within the same
1008 function, so setting REG_ATTRS, REG_POINTER and REG_POINTER_ALIGN
1009 on them is wrong. */
1010 if (HARD_REGISTER_P (reg))
1011 return;
1013 offset = byte_lowpart_offset (GET_MODE (reg), GET_MODE (x));
1014 if (MEM_P (x))
1016 if (MEM_OFFSET_KNOWN_P (x))
1017 REG_ATTRS (reg) = get_reg_attrs (MEM_EXPR (x),
1018 MEM_OFFSET (x) + offset);
1019 if (can_be_reg_pointer && MEM_POINTER (x))
1020 mark_reg_pointer (reg, 0);
1022 else if (REG_P (x))
1024 if (REG_ATTRS (x))
1025 update_reg_offset (reg, x, offset);
1026 if (can_be_reg_pointer && REG_POINTER (x))
1027 mark_reg_pointer (reg, REGNO_POINTER_ALIGN (REGNO (x)));
1031 /* Generate a REG rtx for a new pseudo register, copying the mode
1032 and attributes from X. */
1035 gen_reg_rtx_and_attrs (rtx x)
1037 rtx reg = gen_reg_rtx (GET_MODE (x));
1038 set_reg_attrs_from_value (reg, x);
1039 return reg;
1042 /* Set the register attributes for registers contained in PARM_RTX.
1043 Use needed values from memory attributes of MEM. */
1045 void
1046 set_reg_attrs_for_parm (rtx parm_rtx, rtx mem)
1048 if (REG_P (parm_rtx))
1049 set_reg_attrs_from_value (parm_rtx, mem);
1050 else if (GET_CODE (parm_rtx) == PARALLEL)
1052 /* Check for a NULL entry in the first slot, used to indicate that the
1053 parameter goes both on the stack and in registers. */
1054 int i = XEXP (XVECEXP (parm_rtx, 0, 0), 0) ? 0 : 1;
1055 for (; i < XVECLEN (parm_rtx, 0); i++)
1057 rtx x = XVECEXP (parm_rtx, 0, i);
1058 if (REG_P (XEXP (x, 0)))
1059 REG_ATTRS (XEXP (x, 0))
1060 = get_reg_attrs (MEM_EXPR (mem),
1061 INTVAL (XEXP (x, 1)));
1066 /* Set the REG_ATTRS for registers in value X, given that X represents
1067 decl T. */
1069 void
1070 set_reg_attrs_for_decl_rtl (tree t, rtx x)
1072 if (GET_CODE (x) == SUBREG)
1074 gcc_assert (subreg_lowpart_p (x));
1075 x = SUBREG_REG (x);
1077 if (REG_P (x))
1078 REG_ATTRS (x)
1079 = get_reg_attrs (t, byte_lowpart_offset (GET_MODE (x),
1080 DECL_MODE (t)));
1081 if (GET_CODE (x) == CONCAT)
1083 if (REG_P (XEXP (x, 0)))
1084 REG_ATTRS (XEXP (x, 0)) = get_reg_attrs (t, 0);
1085 if (REG_P (XEXP (x, 1)))
1086 REG_ATTRS (XEXP (x, 1))
1087 = get_reg_attrs (t, GET_MODE_UNIT_SIZE (GET_MODE (XEXP (x, 0))));
1089 if (GET_CODE (x) == PARALLEL)
1091 int i, start;
1093 /* Check for a NULL entry, used to indicate that the parameter goes
1094 both on the stack and in registers. */
1095 if (XEXP (XVECEXP (x, 0, 0), 0))
1096 start = 0;
1097 else
1098 start = 1;
1100 for (i = start; i < XVECLEN (x, 0); i++)
1102 rtx y = XVECEXP (x, 0, i);
1103 if (REG_P (XEXP (y, 0)))
1104 REG_ATTRS (XEXP (y, 0)) = get_reg_attrs (t, INTVAL (XEXP (y, 1)));
1109 /* Assign the RTX X to declaration T. */
1111 void
1112 set_decl_rtl (tree t, rtx x)
1114 DECL_WRTL_CHECK (t)->decl_with_rtl.rtl = x;
1115 if (x)
1116 set_reg_attrs_for_decl_rtl (t, x);
1119 /* Assign the RTX X to parameter declaration T. BY_REFERENCE_P is true
1120 if the ABI requires the parameter to be passed by reference. */
1122 void
1123 set_decl_incoming_rtl (tree t, rtx x, bool by_reference_p)
1125 DECL_INCOMING_RTL (t) = x;
1126 if (x && !by_reference_p)
1127 set_reg_attrs_for_decl_rtl (t, x);
1130 /* Identify REG (which may be a CONCAT) as a user register. */
1132 void
1133 mark_user_reg (rtx reg)
1135 if (GET_CODE (reg) == CONCAT)
1137 REG_USERVAR_P (XEXP (reg, 0)) = 1;
1138 REG_USERVAR_P (XEXP (reg, 1)) = 1;
1140 else
1142 gcc_assert (REG_P (reg));
1143 REG_USERVAR_P (reg) = 1;
1147 /* Identify REG as a probable pointer register and show its alignment
1148 as ALIGN, if nonzero. */
1150 void
1151 mark_reg_pointer (rtx reg, int align)
1153 if (! REG_POINTER (reg))
1155 REG_POINTER (reg) = 1;
1157 if (align)
1158 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1160 else if (align && align < REGNO_POINTER_ALIGN (REGNO (reg)))
1161 /* We can no-longer be sure just how aligned this pointer is. */
1162 REGNO_POINTER_ALIGN (REGNO (reg)) = align;
1165 /* Return 1 plus largest pseudo reg number used in the current function. */
1168 max_reg_num (void)
1170 return reg_rtx_no;
1173 /* Return 1 + the largest label number used so far in the current function. */
1176 max_label_num (void)
1178 return label_num;
1181 /* Return first label number used in this function (if any were used). */
1184 get_first_label_num (void)
1186 return first_label_num;
1189 /* If the rtx for label was created during the expansion of a nested
1190 function, then first_label_num won't include this label number.
1191 Fix this now so that array indices work later. */
1193 void
1194 maybe_set_first_label_num (rtx x)
1196 if (CODE_LABEL_NUMBER (x) < first_label_num)
1197 first_label_num = CODE_LABEL_NUMBER (x);
1200 /* Return a value representing some low-order bits of X, where the number
1201 of low-order bits is given by MODE. Note that no conversion is done
1202 between floating-point and fixed-point values, rather, the bit
1203 representation is returned.
1205 This function handles the cases in common between gen_lowpart, below,
1206 and two variants in cse.c and combine.c. These are the cases that can
1207 be safely handled at all points in the compilation.
1209 If this is not a case we can handle, return 0. */
1212 gen_lowpart_common (enum machine_mode mode, rtx x)
1214 int msize = GET_MODE_SIZE (mode);
1215 int xsize;
1216 int offset = 0;
1217 enum machine_mode innermode;
1219 /* Unfortunately, this routine doesn't take a parameter for the mode of X,
1220 so we have to make one up. Yuk. */
1221 innermode = GET_MODE (x);
1222 if (CONST_INT_P (x)
1223 && msize * BITS_PER_UNIT <= HOST_BITS_PER_WIDE_INT)
1224 innermode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1225 else if (innermode == VOIDmode)
1226 innermode = mode_for_size (HOST_BITS_PER_DOUBLE_INT, MODE_INT, 0);
1228 xsize = GET_MODE_SIZE (innermode);
1230 gcc_assert (innermode != VOIDmode && innermode != BLKmode);
1232 if (innermode == mode)
1233 return x;
1235 /* MODE must occupy no more words than the mode of X. */
1236 if ((msize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
1237 > ((xsize + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
1238 return 0;
1240 /* Don't allow generating paradoxical FLOAT_MODE subregs. */
1241 if (SCALAR_FLOAT_MODE_P (mode) && msize > xsize)
1242 return 0;
1244 offset = subreg_lowpart_offset (mode, innermode);
1246 if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
1247 && (GET_MODE_CLASS (mode) == MODE_INT
1248 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
1250 /* If we are getting the low-order part of something that has been
1251 sign- or zero-extended, we can either just use the object being
1252 extended or make a narrower extension. If we want an even smaller
1253 piece than the size of the object being extended, call ourselves
1254 recursively.
1256 This case is used mostly by combine and cse. */
1258 if (GET_MODE (XEXP (x, 0)) == mode)
1259 return XEXP (x, 0);
1260 else if (msize < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
1261 return gen_lowpart_common (mode, XEXP (x, 0));
1262 else if (msize < xsize)
1263 return gen_rtx_fmt_e (GET_CODE (x), mode, XEXP (x, 0));
1265 else if (GET_CODE (x) == SUBREG || REG_P (x)
1266 || GET_CODE (x) == CONCAT || GET_CODE (x) == CONST_VECTOR
1267 || CONST_DOUBLE_AS_FLOAT_P (x) || CONST_SCALAR_INT_P (x))
1268 return simplify_gen_subreg (mode, x, innermode, offset);
1270 /* Otherwise, we can't do this. */
1271 return 0;
1275 gen_highpart (enum machine_mode mode, rtx x)
1277 unsigned int msize = GET_MODE_SIZE (mode);
1278 rtx result;
1280 /* This case loses if X is a subreg. To catch bugs early,
1281 complain if an invalid MODE is used even in other cases. */
1282 gcc_assert (msize <= UNITS_PER_WORD
1283 || msize == (unsigned int) GET_MODE_UNIT_SIZE (GET_MODE (x)));
1285 result = simplify_gen_subreg (mode, x, GET_MODE (x),
1286 subreg_highpart_offset (mode, GET_MODE (x)));
1287 gcc_assert (result);
1289 /* simplify_gen_subreg is not guaranteed to return a valid operand for
1290 the target if we have a MEM. gen_highpart must return a valid operand,
1291 emitting code if necessary to do so. */
1292 if (MEM_P (result))
1294 result = validize_mem (result);
1295 gcc_assert (result);
1298 return result;
1301 /* Like gen_highpart, but accept mode of EXP operand in case EXP can
1302 be VOIDmode constant. */
1304 gen_highpart_mode (enum machine_mode outermode, enum machine_mode innermode, rtx exp)
1306 if (GET_MODE (exp) != VOIDmode)
1308 gcc_assert (GET_MODE (exp) == innermode);
1309 return gen_highpart (outermode, exp);
1311 return simplify_gen_subreg (outermode, exp, innermode,
1312 subreg_highpart_offset (outermode, innermode));
1315 /* Return the SUBREG_BYTE for an OUTERMODE lowpart of an INNERMODE value. */
1317 unsigned int
1318 subreg_lowpart_offset (enum machine_mode outermode, enum machine_mode innermode)
1320 unsigned int offset = 0;
1321 int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1323 if (difference > 0)
1325 if (WORDS_BIG_ENDIAN)
1326 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1327 if (BYTES_BIG_ENDIAN)
1328 offset += difference % UNITS_PER_WORD;
1331 return offset;
1334 /* Return offset in bytes to get OUTERMODE high part
1335 of the value in mode INNERMODE stored in memory in target format. */
1336 unsigned int
1337 subreg_highpart_offset (enum machine_mode outermode, enum machine_mode innermode)
1339 unsigned int offset = 0;
1340 int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
1342 gcc_assert (GET_MODE_SIZE (innermode) >= GET_MODE_SIZE (outermode));
1344 if (difference > 0)
1346 if (! WORDS_BIG_ENDIAN)
1347 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
1348 if (! BYTES_BIG_ENDIAN)
1349 offset += difference % UNITS_PER_WORD;
1352 return offset;
1355 /* Return 1 iff X, assumed to be a SUBREG,
1356 refers to the least significant part of its containing reg.
1357 If X is not a SUBREG, always return 1 (it is its own low part!). */
1360 subreg_lowpart_p (const_rtx x)
1362 if (GET_CODE (x) != SUBREG)
1363 return 1;
1364 else if (GET_MODE (SUBREG_REG (x)) == VOIDmode)
1365 return 0;
1367 return (subreg_lowpart_offset (GET_MODE (x), GET_MODE (SUBREG_REG (x)))
1368 == SUBREG_BYTE (x));
1371 /* Return true if X is a paradoxical subreg, false otherwise. */
1372 bool
1373 paradoxical_subreg_p (const_rtx x)
1375 if (GET_CODE (x) != SUBREG)
1376 return false;
1377 return (GET_MODE_PRECISION (GET_MODE (x))
1378 > GET_MODE_PRECISION (GET_MODE (SUBREG_REG (x))));
1381 /* Return subword OFFSET of operand OP.
1382 The word number, OFFSET, is interpreted as the word number starting
1383 at the low-order address. OFFSET 0 is the low-order word if not
1384 WORDS_BIG_ENDIAN, otherwise it is the high-order word.
1386 If we cannot extract the required word, we return zero. Otherwise,
1387 an rtx corresponding to the requested word will be returned.
1389 VALIDATE_ADDRESS is nonzero if the address should be validated. Before
1390 reload has completed, a valid address will always be returned. After
1391 reload, if a valid address cannot be returned, we return zero.
1393 If VALIDATE_ADDRESS is zero, we simply form the required address; validating
1394 it is the responsibility of the caller.
1396 MODE is the mode of OP in case it is a CONST_INT.
1398 ??? This is still rather broken for some cases. The problem for the
1399 moment is that all callers of this thing provide no 'goal mode' to
1400 tell us to work with. This exists because all callers were written
1401 in a word based SUBREG world.
1402 Now use of this function can be deprecated by simplify_subreg in most
1403 cases.
1407 operand_subword (rtx op, unsigned int offset, int validate_address, enum machine_mode mode)
1409 if (mode == VOIDmode)
1410 mode = GET_MODE (op);
1412 gcc_assert (mode != VOIDmode);
1414 /* If OP is narrower than a word, fail. */
1415 if (mode != BLKmode
1416 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD))
1417 return 0;
1419 /* If we want a word outside OP, return zero. */
1420 if (mode != BLKmode
1421 && (offset + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode))
1422 return const0_rtx;
1424 /* Form a new MEM at the requested address. */
1425 if (MEM_P (op))
1427 rtx new_rtx = adjust_address_nv (op, word_mode, offset * UNITS_PER_WORD);
1429 if (! validate_address)
1430 return new_rtx;
1432 else if (reload_completed)
1434 if (! strict_memory_address_addr_space_p (word_mode,
1435 XEXP (new_rtx, 0),
1436 MEM_ADDR_SPACE (op)))
1437 return 0;
1439 else
1440 return replace_equiv_address (new_rtx, XEXP (new_rtx, 0));
1443 /* Rest can be handled by simplify_subreg. */
1444 return simplify_gen_subreg (word_mode, op, mode, (offset * UNITS_PER_WORD));
1447 /* Similar to `operand_subword', but never return 0. If we can't
1448 extract the required subword, put OP into a register and try again.
1449 The second attempt must succeed. We always validate the address in
1450 this case.
1452 MODE is the mode of OP, in case it is CONST_INT. */
1455 operand_subword_force (rtx op, unsigned int offset, enum machine_mode mode)
1457 rtx result = operand_subword (op, offset, 1, mode);
1459 if (result)
1460 return result;
1462 if (mode != BLKmode && mode != VOIDmode)
1464 /* If this is a register which can not be accessed by words, copy it
1465 to a pseudo register. */
1466 if (REG_P (op))
1467 op = copy_to_reg (op);
1468 else
1469 op = force_reg (mode, op);
1472 result = operand_subword (op, offset, 1, mode);
1473 gcc_assert (result);
1475 return result;
1478 /* Returns 1 if both MEM_EXPR can be considered equal
1479 and 0 otherwise. */
1482 mem_expr_equal_p (const_tree expr1, const_tree expr2)
1484 if (expr1 == expr2)
1485 return 1;
1487 if (! expr1 || ! expr2)
1488 return 0;
1490 if (TREE_CODE (expr1) != TREE_CODE (expr2))
1491 return 0;
1493 return operand_equal_p (expr1, expr2, 0);
1496 /* Return OFFSET if XEXP (MEM, 0) - OFFSET is known to be ALIGN
1497 bits aligned for 0 <= OFFSET < ALIGN / BITS_PER_UNIT, or
1498 -1 if not known. */
1501 get_mem_align_offset (rtx mem, unsigned int align)
1503 tree expr;
1504 unsigned HOST_WIDE_INT offset;
1506 /* This function can't use
1507 if (!MEM_EXPR (mem) || !MEM_OFFSET_KNOWN_P (mem)
1508 || (MAX (MEM_ALIGN (mem),
1509 MAX (align, get_object_alignment (MEM_EXPR (mem))))
1510 < align))
1511 return -1;
1512 else
1513 return (- MEM_OFFSET (mem)) & (align / BITS_PER_UNIT - 1);
1514 for two reasons:
1515 - COMPONENT_REFs in MEM_EXPR can have NULL first operand,
1516 for <variable>. get_inner_reference doesn't handle it and
1517 even if it did, the alignment in that case needs to be determined
1518 from DECL_FIELD_CONTEXT's TYPE_ALIGN.
1519 - it would do suboptimal job for COMPONENT_REFs, even if MEM_EXPR
1520 isn't sufficiently aligned, the object it is in might be. */
1521 gcc_assert (MEM_P (mem));
1522 expr = MEM_EXPR (mem);
1523 if (expr == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
1524 return -1;
1526 offset = MEM_OFFSET (mem);
1527 if (DECL_P (expr))
1529 if (DECL_ALIGN (expr) < align)
1530 return -1;
1532 else if (INDIRECT_REF_P (expr))
1534 if (TYPE_ALIGN (TREE_TYPE (expr)) < (unsigned int) align)
1535 return -1;
1537 else if (TREE_CODE (expr) == COMPONENT_REF)
1539 while (1)
1541 tree inner = TREE_OPERAND (expr, 0);
1542 tree field = TREE_OPERAND (expr, 1);
1543 tree byte_offset = component_ref_field_offset (expr);
1544 tree bit_offset = DECL_FIELD_BIT_OFFSET (field);
1546 if (!byte_offset
1547 || !tree_fits_uhwi_p (byte_offset)
1548 || !tree_fits_uhwi_p (bit_offset))
1549 return -1;
1551 offset += tree_to_uhwi (byte_offset);
1552 offset += tree_to_uhwi (bit_offset) / BITS_PER_UNIT;
1554 if (inner == NULL_TREE)
1556 if (TYPE_ALIGN (DECL_FIELD_CONTEXT (field))
1557 < (unsigned int) align)
1558 return -1;
1559 break;
1561 else if (DECL_P (inner))
1563 if (DECL_ALIGN (inner) < align)
1564 return -1;
1565 break;
1567 else if (TREE_CODE (inner) != COMPONENT_REF)
1568 return -1;
1569 expr = inner;
1572 else
1573 return -1;
1575 return offset & ((align / BITS_PER_UNIT) - 1);
1578 /* Given REF (a MEM) and T, either the type of X or the expression
1579 corresponding to REF, set the memory attributes. OBJECTP is nonzero
1580 if we are making a new object of this type. BITPOS is nonzero if
1581 there is an offset outstanding on T that will be applied later. */
1583 void
1584 set_mem_attributes_minus_bitpos (rtx ref, tree t, int objectp,
1585 HOST_WIDE_INT bitpos)
1587 HOST_WIDE_INT apply_bitpos = 0;
1588 tree type;
1589 struct mem_attrs attrs, *defattrs, *refattrs;
1590 addr_space_t as;
1592 /* It can happen that type_for_mode was given a mode for which there
1593 is no language-level type. In which case it returns NULL, which
1594 we can see here. */
1595 if (t == NULL_TREE)
1596 return;
1598 type = TYPE_P (t) ? t : TREE_TYPE (t);
1599 if (type == error_mark_node)
1600 return;
1602 /* If we have already set DECL_RTL = ref, get_alias_set will get the
1603 wrong answer, as it assumes that DECL_RTL already has the right alias
1604 info. Callers should not set DECL_RTL until after the call to
1605 set_mem_attributes. */
1606 gcc_assert (!DECL_P (t) || ref != DECL_RTL_IF_SET (t));
1608 memset (&attrs, 0, sizeof (attrs));
1610 /* Get the alias set from the expression or type (perhaps using a
1611 front-end routine) and use it. */
1612 attrs.alias = get_alias_set (t);
1614 MEM_VOLATILE_P (ref) |= TYPE_VOLATILE (type);
1615 MEM_POINTER (ref) = POINTER_TYPE_P (type);
1617 /* Default values from pre-existing memory attributes if present. */
1618 refattrs = MEM_ATTRS (ref);
1619 if (refattrs)
1621 /* ??? Can this ever happen? Calling this routine on a MEM that
1622 already carries memory attributes should probably be invalid. */
1623 attrs.expr = refattrs->expr;
1624 attrs.offset_known_p = refattrs->offset_known_p;
1625 attrs.offset = refattrs->offset;
1626 attrs.size_known_p = refattrs->size_known_p;
1627 attrs.size = refattrs->size;
1628 attrs.align = refattrs->align;
1631 /* Otherwise, default values from the mode of the MEM reference. */
1632 else
1634 defattrs = mode_mem_attrs[(int) GET_MODE (ref)];
1635 gcc_assert (!defattrs->expr);
1636 gcc_assert (!defattrs->offset_known_p);
1638 /* Respect mode size. */
1639 attrs.size_known_p = defattrs->size_known_p;
1640 attrs.size = defattrs->size;
1641 /* ??? Is this really necessary? We probably should always get
1642 the size from the type below. */
1644 /* Respect mode alignment for STRICT_ALIGNMENT targets if T is a type;
1645 if T is an object, always compute the object alignment below. */
1646 if (TYPE_P (t))
1647 attrs.align = defattrs->align;
1648 else
1649 attrs.align = BITS_PER_UNIT;
1650 /* ??? If T is a type, respecting mode alignment may *also* be wrong
1651 e.g. if the type carries an alignment attribute. Should we be
1652 able to simply always use TYPE_ALIGN? */
1655 /* We can set the alignment from the type if we are making an object,
1656 this is an INDIRECT_REF, or if TYPE_ALIGN_OK. */
1657 if (objectp || TREE_CODE (t) == INDIRECT_REF || TYPE_ALIGN_OK (type))
1658 attrs.align = MAX (attrs.align, TYPE_ALIGN (type));
1660 /* If the size is known, we can set that. */
1661 tree new_size = TYPE_SIZE_UNIT (type);
1663 /* The address-space is that of the type. */
1664 as = TYPE_ADDR_SPACE (type);
1666 /* If T is not a type, we may be able to deduce some more information about
1667 the expression. */
1668 if (! TYPE_P (t))
1670 tree base;
1672 if (TREE_THIS_VOLATILE (t))
1673 MEM_VOLATILE_P (ref) = 1;
1675 /* Now remove any conversions: they don't change what the underlying
1676 object is. Likewise for SAVE_EXPR. */
1677 while (CONVERT_EXPR_P (t)
1678 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1679 || TREE_CODE (t) == SAVE_EXPR)
1680 t = TREE_OPERAND (t, 0);
1682 /* Note whether this expression can trap. */
1683 MEM_NOTRAP_P (ref) = !tree_could_trap_p (t);
1685 base = get_base_address (t);
1686 if (base)
1688 if (DECL_P (base)
1689 && TREE_READONLY (base)
1690 && (TREE_STATIC (base) || DECL_EXTERNAL (base))
1691 && !TREE_THIS_VOLATILE (base))
1692 MEM_READONLY_P (ref) = 1;
1694 /* Mark static const strings readonly as well. */
1695 if (TREE_CODE (base) == STRING_CST
1696 && TREE_READONLY (base)
1697 && TREE_STATIC (base))
1698 MEM_READONLY_P (ref) = 1;
1700 /* Address-space information is on the base object. */
1701 if (TREE_CODE (base) == MEM_REF
1702 || TREE_CODE (base) == TARGET_MEM_REF)
1703 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (base,
1704 0))));
1705 else
1706 as = TYPE_ADDR_SPACE (TREE_TYPE (base));
1709 /* If this expression uses it's parent's alias set, mark it such
1710 that we won't change it. */
1711 if (component_uses_parent_alias_set_from (t) != NULL_TREE)
1712 MEM_KEEP_ALIAS_SET_P (ref) = 1;
1714 /* If this is a decl, set the attributes of the MEM from it. */
1715 if (DECL_P (t))
1717 attrs.expr = t;
1718 attrs.offset_known_p = true;
1719 attrs.offset = 0;
1720 apply_bitpos = bitpos;
1721 new_size = DECL_SIZE_UNIT (t);
1724 /* ??? If we end up with a constant here do record a MEM_EXPR. */
1725 else if (CONSTANT_CLASS_P (t))
1728 /* If this is a field reference, record it. */
1729 else if (TREE_CODE (t) == COMPONENT_REF)
1731 attrs.expr = t;
1732 attrs.offset_known_p = true;
1733 attrs.offset = 0;
1734 apply_bitpos = bitpos;
1735 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
1736 new_size = DECL_SIZE_UNIT (TREE_OPERAND (t, 1));
1739 /* If this is an array reference, look for an outer field reference. */
1740 else if (TREE_CODE (t) == ARRAY_REF)
1742 tree off_tree = size_zero_node;
1743 /* We can't modify t, because we use it at the end of the
1744 function. */
1745 tree t2 = t;
1749 tree index = TREE_OPERAND (t2, 1);
1750 tree low_bound = array_ref_low_bound (t2);
1751 tree unit_size = array_ref_element_size (t2);
1753 /* We assume all arrays have sizes that are a multiple of a byte.
1754 First subtract the lower bound, if any, in the type of the
1755 index, then convert to sizetype and multiply by the size of
1756 the array element. */
1757 if (! integer_zerop (low_bound))
1758 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
1759 index, low_bound);
1761 off_tree = size_binop (PLUS_EXPR,
1762 size_binop (MULT_EXPR,
1763 fold_convert (sizetype,
1764 index),
1765 unit_size),
1766 off_tree);
1767 t2 = TREE_OPERAND (t2, 0);
1769 while (TREE_CODE (t2) == ARRAY_REF);
1771 if (DECL_P (t2)
1772 || TREE_CODE (t2) == COMPONENT_REF)
1774 attrs.expr = t2;
1775 attrs.offset_known_p = false;
1776 if (tree_fits_uhwi_p (off_tree))
1778 attrs.offset_known_p = true;
1779 attrs.offset = tree_to_uhwi (off_tree);
1780 apply_bitpos = bitpos;
1783 /* Else do not record a MEM_EXPR. */
1786 /* If this is an indirect reference, record it. */
1787 else if (TREE_CODE (t) == MEM_REF
1788 || TREE_CODE (t) == TARGET_MEM_REF)
1790 attrs.expr = t;
1791 attrs.offset_known_p = true;
1792 attrs.offset = 0;
1793 apply_bitpos = bitpos;
1796 /* Compute the alignment. */
1797 unsigned int obj_align;
1798 unsigned HOST_WIDE_INT obj_bitpos;
1799 get_object_alignment_1 (t, &obj_align, &obj_bitpos);
1800 obj_bitpos = (obj_bitpos - bitpos) & (obj_align - 1);
1801 if (obj_bitpos != 0)
1802 obj_align = (obj_bitpos & -obj_bitpos);
1803 attrs.align = MAX (attrs.align, obj_align);
1806 if (tree_fits_uhwi_p (new_size))
1808 attrs.size_known_p = true;
1809 attrs.size = tree_to_uhwi (new_size);
1812 /* If we modified OFFSET based on T, then subtract the outstanding
1813 bit position offset. Similarly, increase the size of the accessed
1814 object to contain the negative offset. */
1815 if (apply_bitpos)
1817 gcc_assert (attrs.offset_known_p);
1818 attrs.offset -= apply_bitpos / BITS_PER_UNIT;
1819 if (attrs.size_known_p)
1820 attrs.size += apply_bitpos / BITS_PER_UNIT;
1823 /* Now set the attributes we computed above. */
1824 attrs.addrspace = as;
1825 set_mem_attrs (ref, &attrs);
1828 void
1829 set_mem_attributes (rtx ref, tree t, int objectp)
1831 set_mem_attributes_minus_bitpos (ref, t, objectp, 0);
1834 /* Set the alias set of MEM to SET. */
1836 void
1837 set_mem_alias_set (rtx mem, alias_set_type set)
1839 struct mem_attrs attrs;
1841 /* If the new and old alias sets don't conflict, something is wrong. */
1842 gcc_checking_assert (alias_sets_conflict_p (set, MEM_ALIAS_SET (mem)));
1843 attrs = *get_mem_attrs (mem);
1844 attrs.alias = set;
1845 set_mem_attrs (mem, &attrs);
1848 /* Set the address space of MEM to ADDRSPACE (target-defined). */
1850 void
1851 set_mem_addr_space (rtx mem, addr_space_t addrspace)
1853 struct mem_attrs attrs;
1855 attrs = *get_mem_attrs (mem);
1856 attrs.addrspace = addrspace;
1857 set_mem_attrs (mem, &attrs);
1860 /* Set the alignment of MEM to ALIGN bits. */
1862 void
1863 set_mem_align (rtx mem, unsigned int align)
1865 struct mem_attrs attrs;
1867 attrs = *get_mem_attrs (mem);
1868 attrs.align = align;
1869 set_mem_attrs (mem, &attrs);
1872 /* Set the expr for MEM to EXPR. */
1874 void
1875 set_mem_expr (rtx mem, tree expr)
1877 struct mem_attrs attrs;
1879 attrs = *get_mem_attrs (mem);
1880 attrs.expr = expr;
1881 set_mem_attrs (mem, &attrs);
1884 /* Set the offset of MEM to OFFSET. */
1886 void
1887 set_mem_offset (rtx mem, HOST_WIDE_INT offset)
1889 struct mem_attrs attrs;
1891 attrs = *get_mem_attrs (mem);
1892 attrs.offset_known_p = true;
1893 attrs.offset = offset;
1894 set_mem_attrs (mem, &attrs);
1897 /* Clear the offset of MEM. */
1899 void
1900 clear_mem_offset (rtx mem)
1902 struct mem_attrs attrs;
1904 attrs = *get_mem_attrs (mem);
1905 attrs.offset_known_p = false;
1906 set_mem_attrs (mem, &attrs);
1909 /* Set the size of MEM to SIZE. */
1911 void
1912 set_mem_size (rtx mem, HOST_WIDE_INT size)
1914 struct mem_attrs attrs;
1916 attrs = *get_mem_attrs (mem);
1917 attrs.size_known_p = true;
1918 attrs.size = size;
1919 set_mem_attrs (mem, &attrs);
1922 /* Clear the size of MEM. */
1924 void
1925 clear_mem_size (rtx mem)
1927 struct mem_attrs attrs;
1929 attrs = *get_mem_attrs (mem);
1930 attrs.size_known_p = false;
1931 set_mem_attrs (mem, &attrs);
1934 /* Return a memory reference like MEMREF, but with its mode changed to MODE
1935 and its address changed to ADDR. (VOIDmode means don't change the mode.
1936 NULL for ADDR means don't change the address.) VALIDATE is nonzero if the
1937 returned memory location is required to be valid. The memory
1938 attributes are not changed. */
1940 static rtx
1941 change_address_1 (rtx memref, enum machine_mode mode, rtx addr, int validate)
1943 addr_space_t as;
1944 rtx new_rtx;
1946 gcc_assert (MEM_P (memref));
1947 as = MEM_ADDR_SPACE (memref);
1948 if (mode == VOIDmode)
1949 mode = GET_MODE (memref);
1950 if (addr == 0)
1951 addr = XEXP (memref, 0);
1952 if (mode == GET_MODE (memref) && addr == XEXP (memref, 0)
1953 && (!validate || memory_address_addr_space_p (mode, addr, as)))
1954 return memref;
1956 if (validate)
1958 if (reload_in_progress || reload_completed)
1959 gcc_assert (memory_address_addr_space_p (mode, addr, as));
1960 else
1961 addr = memory_address_addr_space (mode, addr, as);
1964 if (rtx_equal_p (addr, XEXP (memref, 0)) && mode == GET_MODE (memref))
1965 return memref;
1967 new_rtx = gen_rtx_MEM (mode, addr);
1968 MEM_COPY_ATTRIBUTES (new_rtx, memref);
1969 return new_rtx;
1972 /* Like change_address_1 with VALIDATE nonzero, but we are not saying in what
1973 way we are changing MEMREF, so we only preserve the alias set. */
1976 change_address (rtx memref, enum machine_mode mode, rtx addr)
1978 rtx new_rtx = change_address_1 (memref, mode, addr, 1);
1979 enum machine_mode mmode = GET_MODE (new_rtx);
1980 struct mem_attrs attrs, *defattrs;
1982 attrs = *get_mem_attrs (memref);
1983 defattrs = mode_mem_attrs[(int) mmode];
1984 attrs.expr = NULL_TREE;
1985 attrs.offset_known_p = false;
1986 attrs.size_known_p = defattrs->size_known_p;
1987 attrs.size = defattrs->size;
1988 attrs.align = defattrs->align;
1990 /* If there are no changes, just return the original memory reference. */
1991 if (new_rtx == memref)
1993 if (mem_attrs_eq_p (get_mem_attrs (memref), &attrs))
1994 return new_rtx;
1996 new_rtx = gen_rtx_MEM (mmode, XEXP (memref, 0));
1997 MEM_COPY_ATTRIBUTES (new_rtx, memref);
2000 set_mem_attrs (new_rtx, &attrs);
2001 return new_rtx;
2004 /* Return a memory reference like MEMREF, but with its mode changed
2005 to MODE and its address offset by OFFSET bytes. If VALIDATE is
2006 nonzero, the memory address is forced to be valid.
2007 If ADJUST_ADDRESS is zero, OFFSET is only used to update MEM_ATTRS
2008 and the caller is responsible for adjusting MEMREF base register.
2009 If ADJUST_OBJECT is zero, the underlying object associated with the
2010 memory reference is left unchanged and the caller is responsible for
2011 dealing with it. Otherwise, if the new memory reference is outside
2012 the underlying object, even partially, then the object is dropped.
2013 SIZE, if nonzero, is the size of an access in cases where MODE
2014 has no inherent size. */
2017 adjust_address_1 (rtx memref, enum machine_mode mode, HOST_WIDE_INT offset,
2018 int validate, int adjust_address, int adjust_object,
2019 HOST_WIDE_INT size)
2021 rtx addr = XEXP (memref, 0);
2022 rtx new_rtx;
2023 enum machine_mode address_mode;
2024 int pbits;
2025 struct mem_attrs attrs = *get_mem_attrs (memref), *defattrs;
2026 unsigned HOST_WIDE_INT max_align;
2027 #ifdef POINTERS_EXTEND_UNSIGNED
2028 enum machine_mode pointer_mode
2029 = targetm.addr_space.pointer_mode (attrs.addrspace);
2030 #endif
2032 /* VOIDmode means no mode change for change_address_1. */
2033 if (mode == VOIDmode)
2034 mode = GET_MODE (memref);
2036 /* Take the size of non-BLKmode accesses from the mode. */
2037 defattrs = mode_mem_attrs[(int) mode];
2038 if (defattrs->size_known_p)
2039 size = defattrs->size;
2041 /* If there are no changes, just return the original memory reference. */
2042 if (mode == GET_MODE (memref) && !offset
2043 && (size == 0 || (attrs.size_known_p && attrs.size == size))
2044 && (!validate || memory_address_addr_space_p (mode, addr,
2045 attrs.addrspace)))
2046 return memref;
2048 /* ??? Prefer to create garbage instead of creating shared rtl.
2049 This may happen even if offset is nonzero -- consider
2050 (plus (plus reg reg) const_int) -- so do this always. */
2051 addr = copy_rtx (addr);
2053 /* Convert a possibly large offset to a signed value within the
2054 range of the target address space. */
2055 address_mode = get_address_mode (memref);
2056 pbits = GET_MODE_BITSIZE (address_mode);
2057 if (HOST_BITS_PER_WIDE_INT > pbits)
2059 int shift = HOST_BITS_PER_WIDE_INT - pbits;
2060 offset = (((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) offset << shift))
2061 >> shift);
2064 if (adjust_address)
2066 /* If MEMREF is a LO_SUM and the offset is within the alignment of the
2067 object, we can merge it into the LO_SUM. */
2068 if (GET_MODE (memref) != BLKmode && GET_CODE (addr) == LO_SUM
2069 && offset >= 0
2070 && (unsigned HOST_WIDE_INT) offset
2071 < GET_MODE_ALIGNMENT (GET_MODE (memref)) / BITS_PER_UNIT)
2072 addr = gen_rtx_LO_SUM (address_mode, XEXP (addr, 0),
2073 plus_constant (address_mode,
2074 XEXP (addr, 1), offset));
2075 #ifdef POINTERS_EXTEND_UNSIGNED
2076 /* If MEMREF is a ZERO_EXTEND from pointer_mode and the offset is valid
2077 in that mode, we merge it into the ZERO_EXTEND. We take advantage of
2078 the fact that pointers are not allowed to overflow. */
2079 else if (POINTERS_EXTEND_UNSIGNED > 0
2080 && GET_CODE (addr) == ZERO_EXTEND
2081 && GET_MODE (XEXP (addr, 0)) == pointer_mode
2082 && trunc_int_for_mode (offset, pointer_mode) == offset)
2083 addr = gen_rtx_ZERO_EXTEND (address_mode,
2084 plus_constant (pointer_mode,
2085 XEXP (addr, 0), offset));
2086 #endif
2087 else
2088 addr = plus_constant (address_mode, addr, offset);
2091 new_rtx = change_address_1 (memref, mode, addr, validate);
2093 /* If the address is a REG, change_address_1 rightfully returns memref,
2094 but this would destroy memref's MEM_ATTRS. */
2095 if (new_rtx == memref && offset != 0)
2096 new_rtx = copy_rtx (new_rtx);
2098 /* Conservatively drop the object if we don't know where we start from. */
2099 if (adjust_object && (!attrs.offset_known_p || !attrs.size_known_p))
2101 attrs.expr = NULL_TREE;
2102 attrs.alias = 0;
2105 /* Compute the new values of the memory attributes due to this adjustment.
2106 We add the offsets and update the alignment. */
2107 if (attrs.offset_known_p)
2109 attrs.offset += offset;
2111 /* Drop the object if the new left end is not within its bounds. */
2112 if (adjust_object && attrs.offset < 0)
2114 attrs.expr = NULL_TREE;
2115 attrs.alias = 0;
2119 /* Compute the new alignment by taking the MIN of the alignment and the
2120 lowest-order set bit in OFFSET, but don't change the alignment if OFFSET
2121 if zero. */
2122 if (offset != 0)
2124 max_align = (offset & -offset) * BITS_PER_UNIT;
2125 attrs.align = MIN (attrs.align, max_align);
2128 if (size)
2130 /* Drop the object if the new right end is not within its bounds. */
2131 if (adjust_object && (offset + size) > attrs.size)
2133 attrs.expr = NULL_TREE;
2134 attrs.alias = 0;
2136 attrs.size_known_p = true;
2137 attrs.size = size;
2139 else if (attrs.size_known_p)
2141 gcc_assert (!adjust_object);
2142 attrs.size -= offset;
2143 /* ??? The store_by_pieces machinery generates negative sizes,
2144 so don't assert for that here. */
2147 set_mem_attrs (new_rtx, &attrs);
2149 return new_rtx;
2152 /* Return a memory reference like MEMREF, but with its mode changed
2153 to MODE and its address changed to ADDR, which is assumed to be
2154 MEMREF offset by OFFSET bytes. If VALIDATE is
2155 nonzero, the memory address is forced to be valid. */
2158 adjust_automodify_address_1 (rtx memref, enum machine_mode mode, rtx addr,
2159 HOST_WIDE_INT offset, int validate)
2161 memref = change_address_1 (memref, VOIDmode, addr, validate);
2162 return adjust_address_1 (memref, mode, offset, validate, 0, 0, 0);
2165 /* Return a memory reference like MEMREF, but whose address is changed by
2166 adding OFFSET, an RTX, to it. POW2 is the highest power of two factor
2167 known to be in OFFSET (possibly 1). */
2170 offset_address (rtx memref, rtx offset, unsigned HOST_WIDE_INT pow2)
2172 rtx new_rtx, addr = XEXP (memref, 0);
2173 enum machine_mode address_mode;
2174 struct mem_attrs attrs, *defattrs;
2176 attrs = *get_mem_attrs (memref);
2177 address_mode = get_address_mode (memref);
2178 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2180 /* At this point we don't know _why_ the address is invalid. It
2181 could have secondary memory references, multiplies or anything.
2183 However, if we did go and rearrange things, we can wind up not
2184 being able to recognize the magic around pic_offset_table_rtx.
2185 This stuff is fragile, and is yet another example of why it is
2186 bad to expose PIC machinery too early. */
2187 if (! memory_address_addr_space_p (GET_MODE (memref), new_rtx,
2188 attrs.addrspace)
2189 && GET_CODE (addr) == PLUS
2190 && XEXP (addr, 0) == pic_offset_table_rtx)
2192 addr = force_reg (GET_MODE (addr), addr);
2193 new_rtx = simplify_gen_binary (PLUS, address_mode, addr, offset);
2196 update_temp_slot_address (XEXP (memref, 0), new_rtx);
2197 new_rtx = change_address_1 (memref, VOIDmode, new_rtx, 1);
2199 /* If there are no changes, just return the original memory reference. */
2200 if (new_rtx == memref)
2201 return new_rtx;
2203 /* Update the alignment to reflect the offset. Reset the offset, which
2204 we don't know. */
2205 defattrs = mode_mem_attrs[(int) GET_MODE (new_rtx)];
2206 attrs.offset_known_p = false;
2207 attrs.size_known_p = defattrs->size_known_p;
2208 attrs.size = defattrs->size;
2209 attrs.align = MIN (attrs.align, pow2 * BITS_PER_UNIT);
2210 set_mem_attrs (new_rtx, &attrs);
2211 return new_rtx;
2214 /* Return a memory reference like MEMREF, but with its address changed to
2215 ADDR. The caller is asserting that the actual piece of memory pointed
2216 to is the same, just the form of the address is being changed, such as
2217 by putting something into a register. */
2220 replace_equiv_address (rtx memref, rtx addr)
2222 /* change_address_1 copies the memory attribute structure without change
2223 and that's exactly what we want here. */
2224 update_temp_slot_address (XEXP (memref, 0), addr);
2225 return change_address_1 (memref, VOIDmode, addr, 1);
2228 /* Likewise, but the reference is not required to be valid. */
2231 replace_equiv_address_nv (rtx memref, rtx addr)
2233 return change_address_1 (memref, VOIDmode, addr, 0);
2236 /* Return a memory reference like MEMREF, but with its mode widened to
2237 MODE and offset by OFFSET. This would be used by targets that e.g.
2238 cannot issue QImode memory operations and have to use SImode memory
2239 operations plus masking logic. */
2242 widen_memory_access (rtx memref, enum machine_mode mode, HOST_WIDE_INT offset)
2244 rtx new_rtx = adjust_address_1 (memref, mode, offset, 1, 1, 0, 0);
2245 struct mem_attrs attrs;
2246 unsigned int size = GET_MODE_SIZE (mode);
2248 /* If there are no changes, just return the original memory reference. */
2249 if (new_rtx == memref)
2250 return new_rtx;
2252 attrs = *get_mem_attrs (new_rtx);
2254 /* If we don't know what offset we were at within the expression, then
2255 we can't know if we've overstepped the bounds. */
2256 if (! attrs.offset_known_p)
2257 attrs.expr = NULL_TREE;
2259 while (attrs.expr)
2261 if (TREE_CODE (attrs.expr) == COMPONENT_REF)
2263 tree field = TREE_OPERAND (attrs.expr, 1);
2264 tree offset = component_ref_field_offset (attrs.expr);
2266 if (! DECL_SIZE_UNIT (field))
2268 attrs.expr = NULL_TREE;
2269 break;
2272 /* Is the field at least as large as the access? If so, ok,
2273 otherwise strip back to the containing structure. */
2274 if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
2275 && compare_tree_int (DECL_SIZE_UNIT (field), size) >= 0
2276 && attrs.offset >= 0)
2277 break;
2279 if (! tree_fits_uhwi_p (offset))
2281 attrs.expr = NULL_TREE;
2282 break;
2285 attrs.expr = TREE_OPERAND (attrs.expr, 0);
2286 attrs.offset += tree_to_uhwi (offset);
2287 attrs.offset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
2288 / BITS_PER_UNIT);
2290 /* Similarly for the decl. */
2291 else if (DECL_P (attrs.expr)
2292 && DECL_SIZE_UNIT (attrs.expr)
2293 && TREE_CODE (DECL_SIZE_UNIT (attrs.expr)) == INTEGER_CST
2294 && compare_tree_int (DECL_SIZE_UNIT (attrs.expr), size) >= 0
2295 && (! attrs.offset_known_p || attrs.offset >= 0))
2296 break;
2297 else
2299 /* The widened memory access overflows the expression, which means
2300 that it could alias another expression. Zap it. */
2301 attrs.expr = NULL_TREE;
2302 break;
2306 if (! attrs.expr)
2307 attrs.offset_known_p = false;
2309 /* The widened memory may alias other stuff, so zap the alias set. */
2310 /* ??? Maybe use get_alias_set on any remaining expression. */
2311 attrs.alias = 0;
2312 attrs.size_known_p = true;
2313 attrs.size = size;
2314 set_mem_attrs (new_rtx, &attrs);
2315 return new_rtx;
2318 /* A fake decl that is used as the MEM_EXPR of spill slots. */
2319 static GTY(()) tree spill_slot_decl;
2321 tree
2322 get_spill_slot_decl (bool force_build_p)
2324 tree d = spill_slot_decl;
2325 rtx rd;
2326 struct mem_attrs attrs;
2328 if (d || !force_build_p)
2329 return d;
2331 d = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2332 VAR_DECL, get_identifier ("%sfp"), void_type_node);
2333 DECL_ARTIFICIAL (d) = 1;
2334 DECL_IGNORED_P (d) = 1;
2335 TREE_USED (d) = 1;
2336 spill_slot_decl = d;
2338 rd = gen_rtx_MEM (BLKmode, frame_pointer_rtx);
2339 MEM_NOTRAP_P (rd) = 1;
2340 attrs = *mode_mem_attrs[(int) BLKmode];
2341 attrs.alias = new_alias_set ();
2342 attrs.expr = d;
2343 set_mem_attrs (rd, &attrs);
2344 SET_DECL_RTL (d, rd);
2346 return d;
2349 /* Given MEM, a result from assign_stack_local, fill in the memory
2350 attributes as appropriate for a register allocator spill slot.
2351 These slots are not aliasable by other memory. We arrange for
2352 them all to use a single MEM_EXPR, so that the aliasing code can
2353 work properly in the case of shared spill slots. */
2355 void
2356 set_mem_attrs_for_spill (rtx mem)
2358 struct mem_attrs attrs;
2359 rtx addr;
2361 attrs = *get_mem_attrs (mem);
2362 attrs.expr = get_spill_slot_decl (true);
2363 attrs.alias = MEM_ALIAS_SET (DECL_RTL (attrs.expr));
2364 attrs.addrspace = ADDR_SPACE_GENERIC;
2366 /* We expect the incoming memory to be of the form:
2367 (mem:MODE (plus (reg sfp) (const_int offset)))
2368 with perhaps the plus missing for offset = 0. */
2369 addr = XEXP (mem, 0);
2370 attrs.offset_known_p = true;
2371 attrs.offset = 0;
2372 if (GET_CODE (addr) == PLUS
2373 && CONST_INT_P (XEXP (addr, 1)))
2374 attrs.offset = INTVAL (XEXP (addr, 1));
2376 set_mem_attrs (mem, &attrs);
2377 MEM_NOTRAP_P (mem) = 1;
2380 /* Return a newly created CODE_LABEL rtx with a unique label number. */
2383 gen_label_rtx (void)
2385 return gen_rtx_CODE_LABEL (VOIDmode, 0, NULL_RTX, NULL_RTX,
2386 NULL, label_num++, NULL);
2389 /* For procedure integration. */
2391 /* Install new pointers to the first and last insns in the chain.
2392 Also, set cur_insn_uid to one higher than the last in use.
2393 Used for an inline-procedure after copying the insn chain. */
2395 void
2396 set_new_first_and_last_insn (rtx first, rtx last)
2398 rtx insn;
2400 set_first_insn (first);
2401 set_last_insn (last);
2402 cur_insn_uid = 0;
2404 if (MIN_NONDEBUG_INSN_UID || MAY_HAVE_DEBUG_INSNS)
2406 int debug_count = 0;
2408 cur_insn_uid = MIN_NONDEBUG_INSN_UID - 1;
2409 cur_debug_insn_uid = 0;
2411 for (insn = first; insn; insn = NEXT_INSN (insn))
2412 if (INSN_UID (insn) < MIN_NONDEBUG_INSN_UID)
2413 cur_debug_insn_uid = MAX (cur_debug_insn_uid, INSN_UID (insn));
2414 else
2416 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2417 if (DEBUG_INSN_P (insn))
2418 debug_count++;
2421 if (debug_count)
2422 cur_debug_insn_uid = MIN_NONDEBUG_INSN_UID + debug_count;
2423 else
2424 cur_debug_insn_uid++;
2426 else
2427 for (insn = first; insn; insn = NEXT_INSN (insn))
2428 cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
2430 cur_insn_uid++;
2433 /* Go through all the RTL insn bodies and copy any invalid shared
2434 structure. This routine should only be called once. */
2436 static void
2437 unshare_all_rtl_1 (rtx insn)
2439 /* Unshare just about everything else. */
2440 unshare_all_rtl_in_chain (insn);
2442 /* Make sure the addresses of stack slots found outside the insn chain
2443 (such as, in DECL_RTL of a variable) are not shared
2444 with the insn chain.
2446 This special care is necessary when the stack slot MEM does not
2447 actually appear in the insn chain. If it does appear, its address
2448 is unshared from all else at that point. */
2449 stack_slot_list = copy_rtx_if_shared (stack_slot_list);
2452 /* Go through all the RTL insn bodies and copy any invalid shared
2453 structure, again. This is a fairly expensive thing to do so it
2454 should be done sparingly. */
2456 void
2457 unshare_all_rtl_again (rtx insn)
2459 rtx p;
2460 tree decl;
2462 for (p = insn; p; p = NEXT_INSN (p))
2463 if (INSN_P (p))
2465 reset_used_flags (PATTERN (p));
2466 reset_used_flags (REG_NOTES (p));
2467 if (CALL_P (p))
2468 reset_used_flags (CALL_INSN_FUNCTION_USAGE (p));
2471 /* Make sure that virtual stack slots are not shared. */
2472 set_used_decls (DECL_INITIAL (cfun->decl));
2474 /* Make sure that virtual parameters are not shared. */
2475 for (decl = DECL_ARGUMENTS (cfun->decl); decl; decl = DECL_CHAIN (decl))
2476 set_used_flags (DECL_RTL (decl));
2478 reset_used_flags (stack_slot_list);
2480 unshare_all_rtl_1 (insn);
2483 unsigned int
2484 unshare_all_rtl (void)
2486 unshare_all_rtl_1 (get_insns ());
2487 return 0;
2491 /* Check that ORIG is not marked when it should not be and mark ORIG as in use,
2492 Recursively does the same for subexpressions. */
2494 static void
2495 verify_rtx_sharing (rtx orig, rtx insn)
2497 rtx x = orig;
2498 int i;
2499 enum rtx_code code;
2500 const char *format_ptr;
2502 if (x == 0)
2503 return;
2505 code = GET_CODE (x);
2507 /* These types may be freely shared. */
2509 switch (code)
2511 case REG:
2512 case DEBUG_EXPR:
2513 case VALUE:
2514 CASE_CONST_ANY:
2515 case SYMBOL_REF:
2516 case LABEL_REF:
2517 case CODE_LABEL:
2518 case PC:
2519 case CC0:
2520 case RETURN:
2521 case SIMPLE_RETURN:
2522 case SCRATCH:
2523 /* SCRATCH must be shared because they represent distinct values. */
2524 return;
2525 case CLOBBER:
2526 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2527 clobbers or clobbers of hard registers that originated as pseudos.
2528 This is needed to allow safe register renaming. */
2529 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2530 && ORIGINAL_REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 0)))
2531 return;
2532 break;
2534 case CONST:
2535 if (shared_const_p (orig))
2536 return;
2537 break;
2539 case MEM:
2540 /* A MEM is allowed to be shared if its address is constant. */
2541 if (CONSTANT_ADDRESS_P (XEXP (x, 0))
2542 || reload_completed || reload_in_progress)
2543 return;
2545 break;
2547 default:
2548 break;
2551 /* This rtx may not be shared. If it has already been seen,
2552 replace it with a copy of itself. */
2553 #ifdef ENABLE_CHECKING
2554 if (RTX_FLAG (x, used))
2556 error ("invalid rtl sharing found in the insn");
2557 debug_rtx (insn);
2558 error ("shared rtx");
2559 debug_rtx (x);
2560 internal_error ("internal consistency failure");
2562 #endif
2563 gcc_assert (!RTX_FLAG (x, used));
2565 RTX_FLAG (x, used) = 1;
2567 /* Now scan the subexpressions recursively. */
2569 format_ptr = GET_RTX_FORMAT (code);
2571 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2573 switch (*format_ptr++)
2575 case 'e':
2576 verify_rtx_sharing (XEXP (x, i), insn);
2577 break;
2579 case 'E':
2580 if (XVEC (x, i) != NULL)
2582 int j;
2583 int len = XVECLEN (x, i);
2585 for (j = 0; j < len; j++)
2587 /* We allow sharing of ASM_OPERANDS inside single
2588 instruction. */
2589 if (j && GET_CODE (XVECEXP (x, i, j)) == SET
2590 && (GET_CODE (SET_SRC (XVECEXP (x, i, j)))
2591 == ASM_OPERANDS))
2592 verify_rtx_sharing (SET_DEST (XVECEXP (x, i, j)), insn);
2593 else
2594 verify_rtx_sharing (XVECEXP (x, i, j), insn);
2597 break;
2600 return;
2603 /* Reset used-flags for INSN. */
2605 static void
2606 reset_insn_used_flags (rtx insn)
2608 gcc_assert (INSN_P (insn));
2609 reset_used_flags (PATTERN (insn));
2610 reset_used_flags (REG_NOTES (insn));
2611 if (CALL_P (insn))
2612 reset_used_flags (CALL_INSN_FUNCTION_USAGE (insn));
2615 /* Go through all the RTL insn bodies and clear all the USED bits. */
2617 static void
2618 reset_all_used_flags (void)
2620 rtx p;
2622 for (p = get_insns (); p; p = NEXT_INSN (p))
2623 if (INSN_P (p))
2625 rtx pat = PATTERN (p);
2626 if (GET_CODE (pat) != SEQUENCE)
2627 reset_insn_used_flags (p);
2628 else
2630 gcc_assert (REG_NOTES (p) == NULL);
2631 for (int i = 0; i < XVECLEN (pat, 0); i++)
2632 reset_insn_used_flags (XVECEXP (pat, 0, i));
2637 /* Verify sharing in INSN. */
2639 static void
2640 verify_insn_sharing (rtx insn)
2642 gcc_assert (INSN_P (insn));
2643 reset_used_flags (PATTERN (insn));
2644 reset_used_flags (REG_NOTES (insn));
2645 if (CALL_P (insn))
2646 reset_used_flags (CALL_INSN_FUNCTION_USAGE (insn));
2649 /* Go through all the RTL insn bodies and check that there is no unexpected
2650 sharing in between the subexpressions. */
2652 DEBUG_FUNCTION void
2653 verify_rtl_sharing (void)
2655 rtx p;
2657 timevar_push (TV_VERIFY_RTL_SHARING);
2659 reset_all_used_flags ();
2661 for (p = get_insns (); p; p = NEXT_INSN (p))
2662 if (INSN_P (p))
2664 rtx pat = PATTERN (p);
2665 if (GET_CODE (pat) != SEQUENCE)
2666 verify_insn_sharing (p);
2667 else
2668 for (int i = 0; i < XVECLEN (pat, 0); i++)
2669 verify_insn_sharing (XVECEXP (pat, 0, i));
2672 reset_all_used_flags ();
2674 timevar_pop (TV_VERIFY_RTL_SHARING);
2677 /* Go through all the RTL insn bodies and copy any invalid shared structure.
2678 Assumes the mark bits are cleared at entry. */
2680 void
2681 unshare_all_rtl_in_chain (rtx insn)
2683 for (; insn; insn = NEXT_INSN (insn))
2684 if (INSN_P (insn))
2686 PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
2687 REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
2688 if (CALL_P (insn))
2689 CALL_INSN_FUNCTION_USAGE (insn)
2690 = copy_rtx_if_shared (CALL_INSN_FUNCTION_USAGE (insn));
2694 /* Go through all virtual stack slots of a function and mark them as
2695 shared. We never replace the DECL_RTLs themselves with a copy,
2696 but expressions mentioned into a DECL_RTL cannot be shared with
2697 expressions in the instruction stream.
2699 Note that reload may convert pseudo registers into memories in-place.
2700 Pseudo registers are always shared, but MEMs never are. Thus if we
2701 reset the used flags on MEMs in the instruction stream, we must set
2702 them again on MEMs that appear in DECL_RTLs. */
2704 static void
2705 set_used_decls (tree blk)
2707 tree t;
2709 /* Mark decls. */
2710 for (t = BLOCK_VARS (blk); t; t = DECL_CHAIN (t))
2711 if (DECL_RTL_SET_P (t))
2712 set_used_flags (DECL_RTL (t));
2714 /* Now process sub-blocks. */
2715 for (t = BLOCK_SUBBLOCKS (blk); t; t = BLOCK_CHAIN (t))
2716 set_used_decls (t);
2719 /* Mark ORIG as in use, and return a copy of it if it was already in use.
2720 Recursively does the same for subexpressions. Uses
2721 copy_rtx_if_shared_1 to reduce stack space. */
2724 copy_rtx_if_shared (rtx orig)
2726 copy_rtx_if_shared_1 (&orig);
2727 return orig;
2730 /* Mark *ORIG1 as in use, and set it to a copy of it if it was already in
2731 use. Recursively does the same for subexpressions. */
2733 static void
2734 copy_rtx_if_shared_1 (rtx *orig1)
2736 rtx x;
2737 int i;
2738 enum rtx_code code;
2739 rtx *last_ptr;
2740 const char *format_ptr;
2741 int copied = 0;
2742 int length;
2744 /* Repeat is used to turn tail-recursion into iteration. */
2745 repeat:
2746 x = *orig1;
2748 if (x == 0)
2749 return;
2751 code = GET_CODE (x);
2753 /* These types may be freely shared. */
2755 switch (code)
2757 case REG:
2758 case DEBUG_EXPR:
2759 case VALUE:
2760 CASE_CONST_ANY:
2761 case SYMBOL_REF:
2762 case LABEL_REF:
2763 case CODE_LABEL:
2764 case PC:
2765 case CC0:
2766 case RETURN:
2767 case SIMPLE_RETURN:
2768 case SCRATCH:
2769 /* SCRATCH must be shared because they represent distinct values. */
2770 return;
2771 case CLOBBER:
2772 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
2773 clobbers or clobbers of hard registers that originated as pseudos.
2774 This is needed to allow safe register renaming. */
2775 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2776 && ORIGINAL_REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 0)))
2777 return;
2778 break;
2780 case CONST:
2781 if (shared_const_p (x))
2782 return;
2783 break;
2785 case DEBUG_INSN:
2786 case INSN:
2787 case JUMP_INSN:
2788 case CALL_INSN:
2789 case NOTE:
2790 case BARRIER:
2791 /* The chain of insns is not being copied. */
2792 return;
2794 default:
2795 break;
2798 /* This rtx may not be shared. If it has already been seen,
2799 replace it with a copy of itself. */
2801 if (RTX_FLAG (x, used))
2803 x = shallow_copy_rtx (x);
2804 copied = 1;
2806 RTX_FLAG (x, used) = 1;
2808 /* Now scan the subexpressions recursively.
2809 We can store any replaced subexpressions directly into X
2810 since we know X is not shared! Any vectors in X
2811 must be copied if X was copied. */
2813 format_ptr = GET_RTX_FORMAT (code);
2814 length = GET_RTX_LENGTH (code);
2815 last_ptr = NULL;
2817 for (i = 0; i < length; i++)
2819 switch (*format_ptr++)
2821 case 'e':
2822 if (last_ptr)
2823 copy_rtx_if_shared_1 (last_ptr);
2824 last_ptr = &XEXP (x, i);
2825 break;
2827 case 'E':
2828 if (XVEC (x, i) != NULL)
2830 int j;
2831 int len = XVECLEN (x, i);
2833 /* Copy the vector iff I copied the rtx and the length
2834 is nonzero. */
2835 if (copied && len > 0)
2836 XVEC (x, i) = gen_rtvec_v (len, XVEC (x, i)->elem);
2838 /* Call recursively on all inside the vector. */
2839 for (j = 0; j < len; j++)
2841 if (last_ptr)
2842 copy_rtx_if_shared_1 (last_ptr);
2843 last_ptr = &XVECEXP (x, i, j);
2846 break;
2849 *orig1 = x;
2850 if (last_ptr)
2852 orig1 = last_ptr;
2853 goto repeat;
2855 return;
2858 /* Set the USED bit in X and its non-shareable subparts to FLAG. */
2860 static void
2861 mark_used_flags (rtx x, int flag)
2863 int i, j;
2864 enum rtx_code code;
2865 const char *format_ptr;
2866 int length;
2868 /* Repeat is used to turn tail-recursion into iteration. */
2869 repeat:
2870 if (x == 0)
2871 return;
2873 code = GET_CODE (x);
2875 /* These types may be freely shared so we needn't do any resetting
2876 for them. */
2878 switch (code)
2880 case REG:
2881 case DEBUG_EXPR:
2882 case VALUE:
2883 CASE_CONST_ANY:
2884 case SYMBOL_REF:
2885 case CODE_LABEL:
2886 case PC:
2887 case CC0:
2888 case RETURN:
2889 case SIMPLE_RETURN:
2890 return;
2892 case DEBUG_INSN:
2893 case INSN:
2894 case JUMP_INSN:
2895 case CALL_INSN:
2896 case NOTE:
2897 case LABEL_REF:
2898 case BARRIER:
2899 /* The chain of insns is not being copied. */
2900 return;
2902 default:
2903 break;
2906 RTX_FLAG (x, used) = flag;
2908 format_ptr = GET_RTX_FORMAT (code);
2909 length = GET_RTX_LENGTH (code);
2911 for (i = 0; i < length; i++)
2913 switch (*format_ptr++)
2915 case 'e':
2916 if (i == length-1)
2918 x = XEXP (x, i);
2919 goto repeat;
2921 mark_used_flags (XEXP (x, i), flag);
2922 break;
2924 case 'E':
2925 for (j = 0; j < XVECLEN (x, i); j++)
2926 mark_used_flags (XVECEXP (x, i, j), flag);
2927 break;
2932 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
2933 to look for shared sub-parts. */
2935 void
2936 reset_used_flags (rtx x)
2938 mark_used_flags (x, 0);
2941 /* Set all the USED bits in X to allow copy_rtx_if_shared to be used
2942 to look for shared sub-parts. */
2944 void
2945 set_used_flags (rtx x)
2947 mark_used_flags (x, 1);
2950 /* Copy X if necessary so that it won't be altered by changes in OTHER.
2951 Return X or the rtx for the pseudo reg the value of X was copied into.
2952 OTHER must be valid as a SET_DEST. */
2955 make_safe_from (rtx x, rtx other)
2957 while (1)
2958 switch (GET_CODE (other))
2960 case SUBREG:
2961 other = SUBREG_REG (other);
2962 break;
2963 case STRICT_LOW_PART:
2964 case SIGN_EXTEND:
2965 case ZERO_EXTEND:
2966 other = XEXP (other, 0);
2967 break;
2968 default:
2969 goto done;
2971 done:
2972 if ((MEM_P (other)
2973 && ! CONSTANT_P (x)
2974 && !REG_P (x)
2975 && GET_CODE (x) != SUBREG)
2976 || (REG_P (other)
2977 && (REGNO (other) < FIRST_PSEUDO_REGISTER
2978 || reg_mentioned_p (other, x))))
2980 rtx temp = gen_reg_rtx (GET_MODE (x));
2981 emit_move_insn (temp, x);
2982 return temp;
2984 return x;
2987 /* Emission of insns (adding them to the doubly-linked list). */
2989 /* Return the last insn emitted, even if it is in a sequence now pushed. */
2992 get_last_insn_anywhere (void)
2994 struct sequence_stack *stack;
2995 if (get_last_insn ())
2996 return get_last_insn ();
2997 for (stack = seq_stack; stack; stack = stack->next)
2998 if (stack->last != 0)
2999 return stack->last;
3000 return 0;
3003 /* Return the first nonnote insn emitted in current sequence or current
3004 function. This routine looks inside SEQUENCEs. */
3007 get_first_nonnote_insn (void)
3009 rtx insn = get_insns ();
3011 if (insn)
3013 if (NOTE_P (insn))
3014 for (insn = next_insn (insn);
3015 insn && NOTE_P (insn);
3016 insn = next_insn (insn))
3017 continue;
3018 else
3020 if (NONJUMP_INSN_P (insn)
3021 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3022 insn = XVECEXP (PATTERN (insn), 0, 0);
3026 return insn;
3029 /* Return the last nonnote insn emitted in current sequence or current
3030 function. This routine looks inside SEQUENCEs. */
3033 get_last_nonnote_insn (void)
3035 rtx insn = get_last_insn ();
3037 if (insn)
3039 if (NOTE_P (insn))
3040 for (insn = previous_insn (insn);
3041 insn && NOTE_P (insn);
3042 insn = previous_insn (insn))
3043 continue;
3044 else
3046 if (NONJUMP_INSN_P (insn)
3047 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3048 insn = XVECEXP (PATTERN (insn), 0,
3049 XVECLEN (PATTERN (insn), 0) - 1);
3053 return insn;
3056 /* Return the number of actual (non-debug) insns emitted in this
3057 function. */
3060 get_max_insn_count (void)
3062 int n = cur_insn_uid;
3064 /* The table size must be stable across -g, to avoid codegen
3065 differences due to debug insns, and not be affected by
3066 -fmin-insn-uid, to avoid excessive table size and to simplify
3067 debugging of -fcompare-debug failures. */
3068 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3069 n -= cur_debug_insn_uid;
3070 else
3071 n -= MIN_NONDEBUG_INSN_UID;
3073 return n;
3077 /* Return the next insn. If it is a SEQUENCE, return the first insn
3078 of the sequence. */
3081 next_insn (rtx insn)
3083 if (insn)
3085 insn = NEXT_INSN (insn);
3086 if (insn && NONJUMP_INSN_P (insn)
3087 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3088 insn = XVECEXP (PATTERN (insn), 0, 0);
3091 return insn;
3094 /* Return the previous insn. If it is a SEQUENCE, return the last insn
3095 of the sequence. */
3098 previous_insn (rtx insn)
3100 if (insn)
3102 insn = PREV_INSN (insn);
3103 if (insn && NONJUMP_INSN_P (insn)
3104 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3105 insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
3108 return insn;
3111 /* Return the next insn after INSN that is not a NOTE. This routine does not
3112 look inside SEQUENCEs. */
3115 next_nonnote_insn (rtx insn)
3117 while (insn)
3119 insn = NEXT_INSN (insn);
3120 if (insn == 0 || !NOTE_P (insn))
3121 break;
3124 return insn;
3127 /* Return the next insn after INSN that is not a NOTE, but stop the
3128 search before we enter another basic block. This routine does not
3129 look inside SEQUENCEs. */
3132 next_nonnote_insn_bb (rtx insn)
3134 while (insn)
3136 insn = NEXT_INSN (insn);
3137 if (insn == 0 || !NOTE_P (insn))
3138 break;
3139 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3140 return NULL_RTX;
3143 return insn;
3146 /* Return the previous insn before INSN that is not a NOTE. This routine does
3147 not look inside SEQUENCEs. */
3150 prev_nonnote_insn (rtx insn)
3152 while (insn)
3154 insn = PREV_INSN (insn);
3155 if (insn == 0 || !NOTE_P (insn))
3156 break;
3159 return insn;
3162 /* Return the previous insn before INSN that is not a NOTE, but stop
3163 the search before we enter another basic block. This routine does
3164 not look inside SEQUENCEs. */
3167 prev_nonnote_insn_bb (rtx insn)
3169 while (insn)
3171 insn = PREV_INSN (insn);
3172 if (insn == 0 || !NOTE_P (insn))
3173 break;
3174 if (NOTE_INSN_BASIC_BLOCK_P (insn))
3175 return NULL_RTX;
3178 return insn;
3181 /* Return the next insn after INSN that is not a DEBUG_INSN. This
3182 routine does not look inside SEQUENCEs. */
3185 next_nondebug_insn (rtx insn)
3187 while (insn)
3189 insn = NEXT_INSN (insn);
3190 if (insn == 0 || !DEBUG_INSN_P (insn))
3191 break;
3194 return insn;
3197 /* Return the previous insn before INSN that is not a DEBUG_INSN.
3198 This routine does not look inside SEQUENCEs. */
3201 prev_nondebug_insn (rtx insn)
3203 while (insn)
3205 insn = PREV_INSN (insn);
3206 if (insn == 0 || !DEBUG_INSN_P (insn))
3207 break;
3210 return insn;
3213 /* Return the next insn after INSN that is not a NOTE nor DEBUG_INSN.
3214 This routine does not look inside SEQUENCEs. */
3217 next_nonnote_nondebug_insn (rtx insn)
3219 while (insn)
3221 insn = NEXT_INSN (insn);
3222 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3223 break;
3226 return insn;
3229 /* Return the previous insn before INSN that is not a NOTE nor DEBUG_INSN.
3230 This routine does not look inside SEQUENCEs. */
3233 prev_nonnote_nondebug_insn (rtx insn)
3235 while (insn)
3237 insn = PREV_INSN (insn);
3238 if (insn == 0 || (!NOTE_P (insn) && !DEBUG_INSN_P (insn)))
3239 break;
3242 return insn;
3245 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
3246 or 0, if there is none. This routine does not look inside
3247 SEQUENCEs. */
3250 next_real_insn (rtx insn)
3252 while (insn)
3254 insn = NEXT_INSN (insn);
3255 if (insn == 0 || INSN_P (insn))
3256 break;
3259 return insn;
3262 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
3263 or 0, if there is none. This routine does not look inside
3264 SEQUENCEs. */
3267 prev_real_insn (rtx insn)
3269 while (insn)
3271 insn = PREV_INSN (insn);
3272 if (insn == 0 || INSN_P (insn))
3273 break;
3276 return insn;
3279 /* Return the last CALL_INSN in the current list, or 0 if there is none.
3280 This routine does not look inside SEQUENCEs. */
3283 last_call_insn (void)
3285 rtx insn;
3287 for (insn = get_last_insn ();
3288 insn && !CALL_P (insn);
3289 insn = PREV_INSN (insn))
3292 return insn;
3295 /* Find the next insn after INSN that really does something. This routine
3296 does not look inside SEQUENCEs. After reload this also skips over
3297 standalone USE and CLOBBER insn. */
3300 active_insn_p (const_rtx insn)
3302 return (CALL_P (insn) || JUMP_P (insn)
3303 || JUMP_TABLE_DATA_P (insn) /* FIXME */
3304 || (NONJUMP_INSN_P (insn)
3305 && (! reload_completed
3306 || (GET_CODE (PATTERN (insn)) != USE
3307 && GET_CODE (PATTERN (insn)) != CLOBBER))));
3311 next_active_insn (rtx insn)
3313 while (insn)
3315 insn = NEXT_INSN (insn);
3316 if (insn == 0 || active_insn_p (insn))
3317 break;
3320 return insn;
3323 /* Find the last insn before INSN that really does something. This routine
3324 does not look inside SEQUENCEs. After reload this also skips over
3325 standalone USE and CLOBBER insn. */
3328 prev_active_insn (rtx insn)
3330 while (insn)
3332 insn = PREV_INSN (insn);
3333 if (insn == 0 || active_insn_p (insn))
3334 break;
3337 return insn;
3340 #ifdef HAVE_cc0
3341 /* Return the next insn that uses CC0 after INSN, which is assumed to
3342 set it. This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
3343 applied to the result of this function should yield INSN).
3345 Normally, this is simply the next insn. However, if a REG_CC_USER note
3346 is present, it contains the insn that uses CC0.
3348 Return 0 if we can't find the insn. */
3351 next_cc0_user (rtx insn)
3353 rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
3355 if (note)
3356 return XEXP (note, 0);
3358 insn = next_nonnote_insn (insn);
3359 if (insn && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3360 insn = XVECEXP (PATTERN (insn), 0, 0);
3362 if (insn && INSN_P (insn) && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
3363 return insn;
3365 return 0;
3368 /* Find the insn that set CC0 for INSN. Unless INSN has a REG_CC_SETTER
3369 note, it is the previous insn. */
3372 prev_cc0_setter (rtx insn)
3374 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3376 if (note)
3377 return XEXP (note, 0);
3379 insn = prev_nonnote_insn (insn);
3380 gcc_assert (sets_cc0_p (PATTERN (insn)));
3382 return insn;
3384 #endif
3386 #ifdef AUTO_INC_DEC
3387 /* Find a RTX_AUTOINC class rtx which matches DATA. */
3389 static int
3390 find_auto_inc (rtx *xp, void *data)
3392 rtx x = *xp;
3393 rtx reg = (rtx) data;
3395 if (GET_RTX_CLASS (GET_CODE (x)) != RTX_AUTOINC)
3396 return 0;
3398 switch (GET_CODE (x))
3400 case PRE_DEC:
3401 case PRE_INC:
3402 case POST_DEC:
3403 case POST_INC:
3404 case PRE_MODIFY:
3405 case POST_MODIFY:
3406 if (rtx_equal_p (reg, XEXP (x, 0)))
3407 return 1;
3408 break;
3410 default:
3411 gcc_unreachable ();
3413 return -1;
3415 #endif
3417 /* Increment the label uses for all labels present in rtx. */
3419 static void
3420 mark_label_nuses (rtx x)
3422 enum rtx_code code;
3423 int i, j;
3424 const char *fmt;
3426 code = GET_CODE (x);
3427 if (code == LABEL_REF && LABEL_P (XEXP (x, 0)))
3428 LABEL_NUSES (XEXP (x, 0))++;
3430 fmt = GET_RTX_FORMAT (code);
3431 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3433 if (fmt[i] == 'e')
3434 mark_label_nuses (XEXP (x, i));
3435 else if (fmt[i] == 'E')
3436 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3437 mark_label_nuses (XVECEXP (x, i, j));
3442 /* Try splitting insns that can be split for better scheduling.
3443 PAT is the pattern which might split.
3444 TRIAL is the insn providing PAT.
3445 LAST is nonzero if we should return the last insn of the sequence produced.
3447 If this routine succeeds in splitting, it returns the first or last
3448 replacement insn depending on the value of LAST. Otherwise, it
3449 returns TRIAL. If the insn to be returned can be split, it will be. */
3452 try_split (rtx pat, rtx trial, int last)
3454 rtx before = PREV_INSN (trial);
3455 rtx after = NEXT_INSN (trial);
3456 int has_barrier = 0;
3457 rtx note, seq, tem;
3458 int probability;
3459 rtx insn_last, insn;
3460 int njumps = 0;
3462 /* We're not good at redistributing frame information. */
3463 if (RTX_FRAME_RELATED_P (trial))
3464 return trial;
3466 if (any_condjump_p (trial)
3467 && (note = find_reg_note (trial, REG_BR_PROB, 0)))
3468 split_branch_probability = XINT (note, 0);
3469 probability = split_branch_probability;
3471 seq = split_insns (pat, trial);
3473 split_branch_probability = -1;
3475 /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
3476 We may need to handle this specially. */
3477 if (after && BARRIER_P (after))
3479 has_barrier = 1;
3480 after = NEXT_INSN (after);
3483 if (!seq)
3484 return trial;
3486 /* Avoid infinite loop if any insn of the result matches
3487 the original pattern. */
3488 insn_last = seq;
3489 while (1)
3491 if (INSN_P (insn_last)
3492 && rtx_equal_p (PATTERN (insn_last), pat))
3493 return trial;
3494 if (!NEXT_INSN (insn_last))
3495 break;
3496 insn_last = NEXT_INSN (insn_last);
3499 /* We will be adding the new sequence to the function. The splitters
3500 may have introduced invalid RTL sharing, so unshare the sequence now. */
3501 unshare_all_rtl_in_chain (seq);
3503 /* Mark labels. */
3504 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3506 if (JUMP_P (insn))
3508 mark_jump_label (PATTERN (insn), insn, 0);
3509 njumps++;
3510 if (probability != -1
3511 && any_condjump_p (insn)
3512 && !find_reg_note (insn, REG_BR_PROB, 0))
3514 /* We can preserve the REG_BR_PROB notes only if exactly
3515 one jump is created, otherwise the machine description
3516 is responsible for this step using
3517 split_branch_probability variable. */
3518 gcc_assert (njumps == 1);
3519 add_int_reg_note (insn, REG_BR_PROB, probability);
3524 /* If we are splitting a CALL_INSN, look for the CALL_INSN
3525 in SEQ and copy any additional information across. */
3526 if (CALL_P (trial))
3528 for (insn = insn_last; insn ; insn = PREV_INSN (insn))
3529 if (CALL_P (insn))
3531 rtx next, *p;
3533 /* Add the old CALL_INSN_FUNCTION_USAGE to whatever the
3534 target may have explicitly specified. */
3535 p = &CALL_INSN_FUNCTION_USAGE (insn);
3536 while (*p)
3537 p = &XEXP (*p, 1);
3538 *p = CALL_INSN_FUNCTION_USAGE (trial);
3540 /* If the old call was a sibling call, the new one must
3541 be too. */
3542 SIBLING_CALL_P (insn) = SIBLING_CALL_P (trial);
3544 /* If the new call is the last instruction in the sequence,
3545 it will effectively replace the old call in-situ. Otherwise
3546 we must move any following NOTE_INSN_CALL_ARG_LOCATION note
3547 so that it comes immediately after the new call. */
3548 if (NEXT_INSN (insn))
3549 for (next = NEXT_INSN (trial);
3550 next && NOTE_P (next);
3551 next = NEXT_INSN (next))
3552 if (NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
3554 remove_insn (next);
3555 add_insn_after (next, insn, NULL);
3556 break;
3561 /* Copy notes, particularly those related to the CFG. */
3562 for (note = REG_NOTES (trial); note; note = XEXP (note, 1))
3564 switch (REG_NOTE_KIND (note))
3566 case REG_EH_REGION:
3567 copy_reg_eh_region_note_backward (note, insn_last, NULL);
3568 break;
3570 case REG_NORETURN:
3571 case REG_SETJMP:
3572 case REG_TM:
3573 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3575 if (CALL_P (insn))
3576 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3578 break;
3580 case REG_NON_LOCAL_GOTO:
3581 case REG_CROSSING_JUMP:
3582 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3584 if (JUMP_P (insn))
3585 add_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
3587 break;
3589 #ifdef AUTO_INC_DEC
3590 case REG_INC:
3591 for (insn = insn_last; insn != NULL_RTX; insn = PREV_INSN (insn))
3593 rtx reg = XEXP (note, 0);
3594 if (!FIND_REG_INC_NOTE (insn, reg)
3595 && for_each_rtx (&PATTERN (insn), find_auto_inc, reg) > 0)
3596 add_reg_note (insn, REG_INC, reg);
3598 break;
3599 #endif
3601 case REG_ARGS_SIZE:
3602 fixup_args_size_notes (NULL_RTX, insn_last, INTVAL (XEXP (note, 0)));
3603 break;
3605 default:
3606 break;
3610 /* If there are LABELS inside the split insns increment the
3611 usage count so we don't delete the label. */
3612 if (INSN_P (trial))
3614 insn = insn_last;
3615 while (insn != NULL_RTX)
3617 /* JUMP_P insns have already been "marked" above. */
3618 if (NONJUMP_INSN_P (insn))
3619 mark_label_nuses (PATTERN (insn));
3621 insn = PREV_INSN (insn);
3625 tem = emit_insn_after_setloc (seq, trial, INSN_LOCATION (trial));
3627 delete_insn (trial);
3628 if (has_barrier)
3629 emit_barrier_after (tem);
3631 /* Recursively call try_split for each new insn created; by the
3632 time control returns here that insn will be fully split, so
3633 set LAST and continue from the insn after the one returned.
3634 We can't use next_active_insn here since AFTER may be a note.
3635 Ignore deleted insns, which can be occur if not optimizing. */
3636 for (tem = NEXT_INSN (before); tem != after; tem = NEXT_INSN (tem))
3637 if (! INSN_DELETED_P (tem) && INSN_P (tem))
3638 tem = try_split (PATTERN (tem), tem, 1);
3640 /* Return either the first or the last insn, depending on which was
3641 requested. */
3642 return last
3643 ? (after ? PREV_INSN (after) : get_last_insn ())
3644 : NEXT_INSN (before);
3647 /* Make and return an INSN rtx, initializing all its slots.
3648 Store PATTERN in the pattern slots. */
3651 make_insn_raw (rtx pattern)
3653 rtx insn;
3655 insn = rtx_alloc (INSN);
3657 INSN_UID (insn) = cur_insn_uid++;
3658 PATTERN (insn) = pattern;
3659 INSN_CODE (insn) = -1;
3660 REG_NOTES (insn) = NULL;
3661 INSN_LOCATION (insn) = curr_insn_location ();
3662 BLOCK_FOR_INSN (insn) = NULL;
3664 #ifdef ENABLE_RTL_CHECKING
3665 if (insn
3666 && INSN_P (insn)
3667 && (returnjump_p (insn)
3668 || (GET_CODE (insn) == SET
3669 && SET_DEST (insn) == pc_rtx)))
3671 warning (0, "ICE: emit_insn used where emit_jump_insn needed:\n");
3672 debug_rtx (insn);
3674 #endif
3676 return insn;
3679 /* Like `make_insn_raw' but make a DEBUG_INSN instead of an insn. */
3681 static rtx
3682 make_debug_insn_raw (rtx pattern)
3684 rtx insn;
3686 insn = rtx_alloc (DEBUG_INSN);
3687 INSN_UID (insn) = cur_debug_insn_uid++;
3688 if (cur_debug_insn_uid > MIN_NONDEBUG_INSN_UID)
3689 INSN_UID (insn) = cur_insn_uid++;
3691 PATTERN (insn) = pattern;
3692 INSN_CODE (insn) = -1;
3693 REG_NOTES (insn) = NULL;
3694 INSN_LOCATION (insn) = curr_insn_location ();
3695 BLOCK_FOR_INSN (insn) = NULL;
3697 return insn;
3700 /* Like `make_insn_raw' but make a JUMP_INSN instead of an insn. */
3702 static rtx
3703 make_jump_insn_raw (rtx pattern)
3705 rtx insn;
3707 insn = rtx_alloc (JUMP_INSN);
3708 INSN_UID (insn) = cur_insn_uid++;
3710 PATTERN (insn) = pattern;
3711 INSN_CODE (insn) = -1;
3712 REG_NOTES (insn) = NULL;
3713 JUMP_LABEL (insn) = NULL;
3714 INSN_LOCATION (insn) = curr_insn_location ();
3715 BLOCK_FOR_INSN (insn) = NULL;
3717 return insn;
3720 /* Like `make_insn_raw' but make a CALL_INSN instead of an insn. */
3722 static rtx
3723 make_call_insn_raw (rtx pattern)
3725 rtx insn;
3727 insn = rtx_alloc (CALL_INSN);
3728 INSN_UID (insn) = cur_insn_uid++;
3730 PATTERN (insn) = pattern;
3731 INSN_CODE (insn) = -1;
3732 REG_NOTES (insn) = NULL;
3733 CALL_INSN_FUNCTION_USAGE (insn) = NULL;
3734 INSN_LOCATION (insn) = curr_insn_location ();
3735 BLOCK_FOR_INSN (insn) = NULL;
3737 return insn;
3740 /* Like `make_insn_raw' but make a NOTE instead of an insn. */
3742 static rtx
3743 make_note_raw (enum insn_note subtype)
3745 /* Some notes are never created this way at all. These notes are
3746 only created by patching out insns. */
3747 gcc_assert (subtype != NOTE_INSN_DELETED_LABEL
3748 && subtype != NOTE_INSN_DELETED_DEBUG_LABEL);
3750 rtx note = rtx_alloc (NOTE);
3751 INSN_UID (note) = cur_insn_uid++;
3752 NOTE_KIND (note) = subtype;
3753 BLOCK_FOR_INSN (note) = NULL;
3754 memset (&NOTE_DATA (note), 0, sizeof (NOTE_DATA (note)));
3755 return note;
3758 /* Add INSN to the end of the doubly-linked list, between PREV and NEXT.
3759 INSN may be any object that can appear in the chain: INSN_P and NOTE_P objects,
3760 but also BARRIERs and JUMP_TABLE_DATAs. PREV and NEXT may be NULL. */
3762 static inline void
3763 link_insn_into_chain (rtx insn, rtx prev, rtx next)
3765 PREV_INSN (insn) = prev;
3766 NEXT_INSN (insn) = next;
3767 if (prev != NULL)
3769 NEXT_INSN (prev) = insn;
3770 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
3772 rtx sequence = PATTERN (prev);
3773 NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
3776 if (next != NULL)
3778 PREV_INSN (next) = insn;
3779 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
3780 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
3783 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
3785 rtx sequence = PATTERN (insn);
3786 PREV_INSN (XVECEXP (sequence, 0, 0)) = prev;
3787 NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = next;
3791 /* Add INSN to the end of the doubly-linked list.
3792 INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE. */
3794 void
3795 add_insn (rtx insn)
3797 rtx prev = get_last_insn ();
3798 link_insn_into_chain (insn, prev, NULL);
3799 if (NULL == get_insns ())
3800 set_first_insn (insn);
3801 set_last_insn (insn);
3804 /* Add INSN into the doubly-linked list after insn AFTER. */
3806 static void
3807 add_insn_after_nobb (rtx insn, rtx after)
3809 rtx next = NEXT_INSN (after);
3811 gcc_assert (!optimize || !INSN_DELETED_P (after));
3813 link_insn_into_chain (insn, after, next);
3815 if (next == NULL)
3817 if (get_last_insn () == after)
3818 set_last_insn (insn);
3819 else
3821 struct sequence_stack *stack = seq_stack;
3822 /* Scan all pending sequences too. */
3823 for (; stack; stack = stack->next)
3824 if (after == stack->last)
3826 stack->last = insn;
3827 break;
3833 /* Add INSN into the doubly-linked list before insn BEFORE. */
3835 static void
3836 add_insn_before_nobb (rtx insn, rtx before)
3838 rtx prev = PREV_INSN (before);
3840 gcc_assert (!optimize || !INSN_DELETED_P (before));
3842 link_insn_into_chain (insn, prev, before);
3844 if (prev == NULL)
3846 if (get_insns () == before)
3847 set_first_insn (insn);
3848 else
3850 struct sequence_stack *stack = seq_stack;
3851 /* Scan all pending sequences too. */
3852 for (; stack; stack = stack->next)
3853 if (before == stack->first)
3855 stack->first = insn;
3856 break;
3859 gcc_assert (stack);
3864 /* Like add_insn_after_nobb, but try to set BLOCK_FOR_INSN.
3865 If BB is NULL, an attempt is made to infer the bb from before.
3867 This and the next function should be the only functions called
3868 to insert an insn once delay slots have been filled since only
3869 they know how to update a SEQUENCE. */
3871 void
3872 add_insn_after (rtx insn, rtx after, basic_block bb)
3874 add_insn_after_nobb (insn, after);
3875 if (!BARRIER_P (after)
3876 && !BARRIER_P (insn)
3877 && (bb = BLOCK_FOR_INSN (after)))
3879 set_block_for_insn (insn, bb);
3880 if (INSN_P (insn))
3881 df_insn_rescan (insn);
3882 /* Should not happen as first in the BB is always
3883 either NOTE or LABEL. */
3884 if (BB_END (bb) == after
3885 /* Avoid clobbering of structure when creating new BB. */
3886 && !BARRIER_P (insn)
3887 && !NOTE_INSN_BASIC_BLOCK_P (insn))
3888 BB_END (bb) = insn;
3892 /* Like add_insn_before_nobb, but try to set BLOCK_FOR_INSN.
3893 If BB is NULL, an attempt is made to infer the bb from before.
3895 This and the previous function should be the only functions called
3896 to insert an insn once delay slots have been filled since only
3897 they know how to update a SEQUENCE. */
3899 void
3900 add_insn_before (rtx insn, rtx before, basic_block bb)
3902 add_insn_before_nobb (insn, before);
3904 if (!bb
3905 && !BARRIER_P (before)
3906 && !BARRIER_P (insn))
3907 bb = BLOCK_FOR_INSN (before);
3909 if (bb)
3911 set_block_for_insn (insn, bb);
3912 if (INSN_P (insn))
3913 df_insn_rescan (insn);
3914 /* Should not happen as first in the BB is always either NOTE or
3915 LABEL. */
3916 gcc_assert (BB_HEAD (bb) != insn
3917 /* Avoid clobbering of structure when creating new BB. */
3918 || BARRIER_P (insn)
3919 || NOTE_INSN_BASIC_BLOCK_P (insn));
3923 /* Replace insn with an deleted instruction note. */
3925 void
3926 set_insn_deleted (rtx insn)
3928 if (INSN_P (insn))
3929 df_insn_delete (insn);
3930 PUT_CODE (insn, NOTE);
3931 NOTE_KIND (insn) = NOTE_INSN_DELETED;
3935 /* Unlink INSN from the insn chain.
3937 This function knows how to handle sequences.
3939 This function does not invalidate data flow information associated with
3940 INSN (i.e. does not call df_insn_delete). That makes this function
3941 usable for only disconnecting an insn from the chain, and re-emit it
3942 elsewhere later.
3944 To later insert INSN elsewhere in the insn chain via add_insn and
3945 similar functions, PREV_INSN and NEXT_INSN must be nullified by
3946 the caller. Nullifying them here breaks many insn chain walks.
3948 To really delete an insn and related DF information, use delete_insn. */
3950 void
3951 remove_insn (rtx insn)
3953 rtx next = NEXT_INSN (insn);
3954 rtx prev = PREV_INSN (insn);
3955 basic_block bb;
3957 if (prev)
3959 NEXT_INSN (prev) = next;
3960 if (NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
3962 rtx sequence = PATTERN (prev);
3963 NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = next;
3966 else if (get_insns () == insn)
3968 if (next)
3969 PREV_INSN (next) = NULL;
3970 set_first_insn (next);
3972 else
3974 struct sequence_stack *stack = seq_stack;
3975 /* Scan all pending sequences too. */
3976 for (; stack; stack = stack->next)
3977 if (insn == stack->first)
3979 stack->first = next;
3980 break;
3983 gcc_assert (stack);
3986 if (next)
3988 PREV_INSN (next) = prev;
3989 if (NONJUMP_INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE)
3990 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3992 else if (get_last_insn () == insn)
3993 set_last_insn (prev);
3994 else
3996 struct sequence_stack *stack = seq_stack;
3997 /* Scan all pending sequences too. */
3998 for (; stack; stack = stack->next)
3999 if (insn == stack->last)
4001 stack->last = prev;
4002 break;
4005 gcc_assert (stack);
4008 /* Fix up basic block boundaries, if necessary. */
4009 if (!BARRIER_P (insn)
4010 && (bb = BLOCK_FOR_INSN (insn)))
4012 if (BB_HEAD (bb) == insn)
4014 /* Never ever delete the basic block note without deleting whole
4015 basic block. */
4016 gcc_assert (!NOTE_P (insn));
4017 BB_HEAD (bb) = next;
4019 if (BB_END (bb) == insn)
4020 BB_END (bb) = prev;
4024 /* Append CALL_FUSAGE to the CALL_INSN_FUNCTION_USAGE for CALL_INSN. */
4026 void
4027 add_function_usage_to (rtx call_insn, rtx call_fusage)
4029 gcc_assert (call_insn && CALL_P (call_insn));
4031 /* Put the register usage information on the CALL. If there is already
4032 some usage information, put ours at the end. */
4033 if (CALL_INSN_FUNCTION_USAGE (call_insn))
4035 rtx link;
4037 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
4038 link = XEXP (link, 1))
4041 XEXP (link, 1) = call_fusage;
4043 else
4044 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
4047 /* Delete all insns made since FROM.
4048 FROM becomes the new last instruction. */
4050 void
4051 delete_insns_since (rtx from)
4053 if (from == 0)
4054 set_first_insn (0);
4055 else
4056 NEXT_INSN (from) = 0;
4057 set_last_insn (from);
4060 /* This function is deprecated, please use sequences instead.
4062 Move a consecutive bunch of insns to a different place in the chain.
4063 The insns to be moved are those between FROM and TO.
4064 They are moved to a new position after the insn AFTER.
4065 AFTER must not be FROM or TO or any insn in between.
4067 This function does not know about SEQUENCEs and hence should not be
4068 called after delay-slot filling has been done. */
4070 void
4071 reorder_insns_nobb (rtx from, rtx to, rtx after)
4073 #ifdef ENABLE_CHECKING
4074 rtx x;
4075 for (x = from; x != to; x = NEXT_INSN (x))
4076 gcc_assert (after != x);
4077 gcc_assert (after != to);
4078 #endif
4080 /* Splice this bunch out of where it is now. */
4081 if (PREV_INSN (from))
4082 NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
4083 if (NEXT_INSN (to))
4084 PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
4085 if (get_last_insn () == to)
4086 set_last_insn (PREV_INSN (from));
4087 if (get_insns () == from)
4088 set_first_insn (NEXT_INSN (to));
4090 /* Make the new neighbors point to it and it to them. */
4091 if (NEXT_INSN (after))
4092 PREV_INSN (NEXT_INSN (after)) = to;
4094 NEXT_INSN (to) = NEXT_INSN (after);
4095 PREV_INSN (from) = after;
4096 NEXT_INSN (after) = from;
4097 if (after == get_last_insn ())
4098 set_last_insn (to);
4101 /* Same as function above, but take care to update BB boundaries. */
4102 void
4103 reorder_insns (rtx from, rtx to, rtx after)
4105 rtx prev = PREV_INSN (from);
4106 basic_block bb, bb2;
4108 reorder_insns_nobb (from, to, after);
4110 if (!BARRIER_P (after)
4111 && (bb = BLOCK_FOR_INSN (after)))
4113 rtx x;
4114 df_set_bb_dirty (bb);
4116 if (!BARRIER_P (from)
4117 && (bb2 = BLOCK_FOR_INSN (from)))
4119 if (BB_END (bb2) == to)
4120 BB_END (bb2) = prev;
4121 df_set_bb_dirty (bb2);
4124 if (BB_END (bb) == after)
4125 BB_END (bb) = to;
4127 for (x = from; x != NEXT_INSN (to); x = NEXT_INSN (x))
4128 if (!BARRIER_P (x))
4129 df_insn_change_bb (x, bb);
4134 /* Emit insn(s) of given code and pattern
4135 at a specified place within the doubly-linked list.
4137 All of the emit_foo global entry points accept an object
4138 X which is either an insn list or a PATTERN of a single
4139 instruction.
4141 There are thus a few canonical ways to generate code and
4142 emit it at a specific place in the instruction stream. For
4143 example, consider the instruction named SPOT and the fact that
4144 we would like to emit some instructions before SPOT. We might
4145 do it like this:
4147 start_sequence ();
4148 ... emit the new instructions ...
4149 insns_head = get_insns ();
4150 end_sequence ();
4152 emit_insn_before (insns_head, SPOT);
4154 It used to be common to generate SEQUENCE rtl instead, but that
4155 is a relic of the past which no longer occurs. The reason is that
4156 SEQUENCE rtl results in much fragmented RTL memory since the SEQUENCE
4157 generated would almost certainly die right after it was created. */
4159 static rtx
4160 emit_pattern_before_noloc (rtx x, rtx before, rtx last, basic_block bb,
4161 rtx (*make_raw) (rtx))
4163 rtx insn;
4165 gcc_assert (before);
4167 if (x == NULL_RTX)
4168 return last;
4170 switch (GET_CODE (x))
4172 case DEBUG_INSN:
4173 case INSN:
4174 case JUMP_INSN:
4175 case CALL_INSN:
4176 case CODE_LABEL:
4177 case BARRIER:
4178 case NOTE:
4179 insn = x;
4180 while (insn)
4182 rtx next = NEXT_INSN (insn);
4183 add_insn_before (insn, before, bb);
4184 last = insn;
4185 insn = next;
4187 break;
4189 #ifdef ENABLE_RTL_CHECKING
4190 case SEQUENCE:
4191 gcc_unreachable ();
4192 break;
4193 #endif
4195 default:
4196 last = (*make_raw) (x);
4197 add_insn_before (last, before, bb);
4198 break;
4201 return last;
4204 /* Make X be output before the instruction BEFORE. */
4207 emit_insn_before_noloc (rtx x, rtx before, basic_block bb)
4209 return emit_pattern_before_noloc (x, before, before, bb, make_insn_raw);
4212 /* Make an instruction with body X and code JUMP_INSN
4213 and output it before the instruction BEFORE. */
4216 emit_jump_insn_before_noloc (rtx x, rtx before)
4218 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4219 make_jump_insn_raw);
4222 /* Make an instruction with body X and code CALL_INSN
4223 and output it before the instruction BEFORE. */
4226 emit_call_insn_before_noloc (rtx x, rtx before)
4228 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4229 make_call_insn_raw);
4232 /* Make an instruction with body X and code DEBUG_INSN
4233 and output it before the instruction BEFORE. */
4236 emit_debug_insn_before_noloc (rtx x, rtx before)
4238 return emit_pattern_before_noloc (x, before, NULL_RTX, NULL,
4239 make_debug_insn_raw);
4242 /* Make an insn of code BARRIER
4243 and output it before the insn BEFORE. */
4246 emit_barrier_before (rtx before)
4248 rtx insn = rtx_alloc (BARRIER);
4250 INSN_UID (insn) = cur_insn_uid++;
4252 add_insn_before (insn, before, NULL);
4253 return insn;
4256 /* Emit the label LABEL before the insn BEFORE. */
4259 emit_label_before (rtx label, rtx before)
4261 gcc_checking_assert (INSN_UID (label) == 0);
4262 INSN_UID (label) = cur_insn_uid++;
4263 add_insn_before (label, before, NULL);
4264 return label;
4267 /* Helper for emit_insn_after, handles lists of instructions
4268 efficiently. */
4270 static rtx
4271 emit_insn_after_1 (rtx first, rtx after, basic_block bb)
4273 rtx last;
4274 rtx after_after;
4275 if (!bb && !BARRIER_P (after))
4276 bb = BLOCK_FOR_INSN (after);
4278 if (bb)
4280 df_set_bb_dirty (bb);
4281 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4282 if (!BARRIER_P (last))
4284 set_block_for_insn (last, bb);
4285 df_insn_rescan (last);
4287 if (!BARRIER_P (last))
4289 set_block_for_insn (last, bb);
4290 df_insn_rescan (last);
4292 if (BB_END (bb) == after)
4293 BB_END (bb) = last;
4295 else
4296 for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
4297 continue;
4299 after_after = NEXT_INSN (after);
4301 NEXT_INSN (after) = first;
4302 PREV_INSN (first) = after;
4303 NEXT_INSN (last) = after_after;
4304 if (after_after)
4305 PREV_INSN (after_after) = last;
4307 if (after == get_last_insn ())
4308 set_last_insn (last);
4310 return last;
4313 static rtx
4314 emit_pattern_after_noloc (rtx x, rtx after, basic_block bb,
4315 rtx (*make_raw)(rtx))
4317 rtx last = after;
4319 gcc_assert (after);
4321 if (x == NULL_RTX)
4322 return last;
4324 switch (GET_CODE (x))
4326 case DEBUG_INSN:
4327 case INSN:
4328 case JUMP_INSN:
4329 case CALL_INSN:
4330 case CODE_LABEL:
4331 case BARRIER:
4332 case NOTE:
4333 last = emit_insn_after_1 (x, after, bb);
4334 break;
4336 #ifdef ENABLE_RTL_CHECKING
4337 case SEQUENCE:
4338 gcc_unreachable ();
4339 break;
4340 #endif
4342 default:
4343 last = (*make_raw) (x);
4344 add_insn_after (last, after, bb);
4345 break;
4348 return last;
4351 /* Make X be output after the insn AFTER and set the BB of insn. If
4352 BB is NULL, an attempt is made to infer the BB from AFTER. */
4355 emit_insn_after_noloc (rtx x, rtx after, basic_block bb)
4357 return emit_pattern_after_noloc (x, after, bb, make_insn_raw);
4361 /* Make an insn of code JUMP_INSN with body X
4362 and output it after the insn AFTER. */
4365 emit_jump_insn_after_noloc (rtx x, rtx after)
4367 return emit_pattern_after_noloc (x, after, NULL, make_jump_insn_raw);
4370 /* Make an instruction with body X and code CALL_INSN
4371 and output it after the instruction AFTER. */
4374 emit_call_insn_after_noloc (rtx x, rtx after)
4376 return emit_pattern_after_noloc (x, after, NULL, make_call_insn_raw);
4379 /* Make an instruction with body X and code CALL_INSN
4380 and output it after the instruction AFTER. */
4383 emit_debug_insn_after_noloc (rtx x, rtx after)
4385 return emit_pattern_after_noloc (x, after, NULL, make_debug_insn_raw);
4388 /* Make an insn of code BARRIER
4389 and output it after the insn AFTER. */
4392 emit_barrier_after (rtx after)
4394 rtx insn = rtx_alloc (BARRIER);
4396 INSN_UID (insn) = cur_insn_uid++;
4398 add_insn_after (insn, after, NULL);
4399 return insn;
4402 /* Emit the label LABEL after the insn AFTER. */
4405 emit_label_after (rtx label, rtx after)
4407 gcc_checking_assert (INSN_UID (label) == 0);
4408 INSN_UID (label) = cur_insn_uid++;
4409 add_insn_after (label, after, NULL);
4410 return label;
4413 /* Notes require a bit of special handling: Some notes need to have their
4414 BLOCK_FOR_INSN set, others should never have it set, and some should
4415 have it set or clear depending on the context. */
4417 /* Return true iff a note of kind SUBTYPE should be emitted with routines
4418 that never set BLOCK_FOR_INSN on NOTE. BB_BOUNDARY is true if the
4419 caller is asked to emit a note before BB_HEAD, or after BB_END. */
4421 static bool
4422 note_outside_basic_block_p (enum insn_note subtype, bool on_bb_boundary_p)
4424 switch (subtype)
4426 /* NOTE_INSN_SWITCH_TEXT_SECTIONS only appears between basic blocks. */
4427 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4428 return true;
4430 /* Notes for var tracking and EH region markers can appear between or
4431 inside basic blocks. If the caller is emitting on the basic block
4432 boundary, do not set BLOCK_FOR_INSN on the new note. */
4433 case NOTE_INSN_VAR_LOCATION:
4434 case NOTE_INSN_CALL_ARG_LOCATION:
4435 case NOTE_INSN_EH_REGION_BEG:
4436 case NOTE_INSN_EH_REGION_END:
4437 return on_bb_boundary_p;
4439 /* Otherwise, BLOCK_FOR_INSN must be set. */
4440 default:
4441 return false;
4445 /* Emit a note of subtype SUBTYPE after the insn AFTER. */
4448 emit_note_after (enum insn_note subtype, rtx after)
4450 rtx note = make_note_raw (subtype);
4451 basic_block bb = BARRIER_P (after) ? NULL : BLOCK_FOR_INSN (after);
4452 bool on_bb_boundary_p = (bb != NULL && BB_END (bb) == after);
4454 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4455 add_insn_after_nobb (note, after);
4456 else
4457 add_insn_after (note, after, bb);
4458 return note;
4461 /* Emit a note of subtype SUBTYPE before the insn BEFORE. */
4464 emit_note_before (enum insn_note subtype, rtx before)
4466 rtx note = make_note_raw (subtype);
4467 basic_block bb = BARRIER_P (before) ? NULL : BLOCK_FOR_INSN (before);
4468 bool on_bb_boundary_p = (bb != NULL && BB_HEAD (bb) == before);
4470 if (note_outside_basic_block_p (subtype, on_bb_boundary_p))
4471 add_insn_before_nobb (note, before);
4472 else
4473 add_insn_before (note, before, bb);
4474 return note;
4477 /* Insert PATTERN after AFTER, setting its INSN_LOCATION to LOC.
4478 MAKE_RAW indicates how to turn PATTERN into a real insn. */
4480 static rtx
4481 emit_pattern_after_setloc (rtx pattern, rtx after, int loc,
4482 rtx (*make_raw) (rtx))
4484 rtx last = emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4486 if (pattern == NULL_RTX || !loc)
4487 return last;
4489 after = NEXT_INSN (after);
4490 while (1)
4492 if (active_insn_p (after) && !INSN_LOCATION (after))
4493 INSN_LOCATION (after) = loc;
4494 if (after == last)
4495 break;
4496 after = NEXT_INSN (after);
4498 return last;
4501 /* Insert PATTERN after AFTER. MAKE_RAW indicates how to turn PATTERN
4502 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert after
4503 any DEBUG_INSNs. */
4505 static rtx
4506 emit_pattern_after (rtx pattern, rtx after, bool skip_debug_insns,
4507 rtx (*make_raw) (rtx))
4509 rtx prev = after;
4511 if (skip_debug_insns)
4512 while (DEBUG_INSN_P (prev))
4513 prev = PREV_INSN (prev);
4515 if (INSN_P (prev))
4516 return emit_pattern_after_setloc (pattern, after, INSN_LOCATION (prev),
4517 make_raw);
4518 else
4519 return emit_pattern_after_noloc (pattern, after, NULL, make_raw);
4522 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4524 emit_insn_after_setloc (rtx pattern, rtx after, int loc)
4526 return emit_pattern_after_setloc (pattern, after, loc, make_insn_raw);
4529 /* Like emit_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4531 emit_insn_after (rtx pattern, rtx after)
4533 return emit_pattern_after (pattern, after, true, make_insn_raw);
4536 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4538 emit_jump_insn_after_setloc (rtx pattern, rtx after, int loc)
4540 return emit_pattern_after_setloc (pattern, after, loc, make_jump_insn_raw);
4543 /* Like emit_jump_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4545 emit_jump_insn_after (rtx pattern, rtx after)
4547 return emit_pattern_after (pattern, after, true, make_jump_insn_raw);
4550 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4552 emit_call_insn_after_setloc (rtx pattern, rtx after, int loc)
4554 return emit_pattern_after_setloc (pattern, after, loc, make_call_insn_raw);
4557 /* Like emit_call_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4559 emit_call_insn_after (rtx pattern, rtx after)
4561 return emit_pattern_after (pattern, after, true, make_call_insn_raw);
4564 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to LOC. */
4566 emit_debug_insn_after_setloc (rtx pattern, rtx after, int loc)
4568 return emit_pattern_after_setloc (pattern, after, loc, make_debug_insn_raw);
4571 /* Like emit_debug_insn_after_noloc, but set INSN_LOCATION according to AFTER. */
4573 emit_debug_insn_after (rtx pattern, rtx after)
4575 return emit_pattern_after (pattern, after, false, make_debug_insn_raw);
4578 /* Insert PATTERN before BEFORE, setting its INSN_LOCATION to LOC.
4579 MAKE_RAW indicates how to turn PATTERN into a real insn. INSNP
4580 indicates if PATTERN is meant for an INSN as opposed to a JUMP_INSN,
4581 CALL_INSN, etc. */
4583 static rtx
4584 emit_pattern_before_setloc (rtx pattern, rtx before, int loc, bool insnp,
4585 rtx (*make_raw) (rtx))
4587 rtx first = PREV_INSN (before);
4588 rtx last = emit_pattern_before_noloc (pattern, before,
4589 insnp ? before : NULL_RTX,
4590 NULL, make_raw);
4592 if (pattern == NULL_RTX || !loc)
4593 return last;
4595 if (!first)
4596 first = get_insns ();
4597 else
4598 first = NEXT_INSN (first);
4599 while (1)
4601 if (active_insn_p (first) && !INSN_LOCATION (first))
4602 INSN_LOCATION (first) = loc;
4603 if (first == last)
4604 break;
4605 first = NEXT_INSN (first);
4607 return last;
4610 /* Insert PATTERN before BEFORE. MAKE_RAW indicates how to turn PATTERN
4611 into a real insn. SKIP_DEBUG_INSNS indicates whether to insert
4612 before any DEBUG_INSNs. INSNP indicates if PATTERN is meant for an
4613 INSN as opposed to a JUMP_INSN, CALL_INSN, etc. */
4615 static rtx
4616 emit_pattern_before (rtx pattern, rtx before, bool skip_debug_insns,
4617 bool insnp, rtx (*make_raw) (rtx))
4619 rtx next = before;
4621 if (skip_debug_insns)
4622 while (DEBUG_INSN_P (next))
4623 next = PREV_INSN (next);
4625 if (INSN_P (next))
4626 return emit_pattern_before_setloc (pattern, before, INSN_LOCATION (next),
4627 insnp, make_raw);
4628 else
4629 return emit_pattern_before_noloc (pattern, before,
4630 insnp ? before : NULL_RTX,
4631 NULL, make_raw);
4634 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4636 emit_insn_before_setloc (rtx pattern, rtx before, int loc)
4638 return emit_pattern_before_setloc (pattern, before, loc, true,
4639 make_insn_raw);
4642 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4644 emit_insn_before (rtx pattern, rtx before)
4646 return emit_pattern_before (pattern, before, true, true, make_insn_raw);
4649 /* like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4651 emit_jump_insn_before_setloc (rtx pattern, rtx before, int loc)
4653 return emit_pattern_before_setloc (pattern, before, loc, false,
4654 make_jump_insn_raw);
4657 /* Like emit_jump_insn_before_noloc, but set INSN_LOCATION according to BEFORE. */
4659 emit_jump_insn_before (rtx pattern, rtx before)
4661 return emit_pattern_before (pattern, before, true, false,
4662 make_jump_insn_raw);
4665 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4667 emit_call_insn_before_setloc (rtx pattern, rtx before, int loc)
4669 return emit_pattern_before_setloc (pattern, before, loc, false,
4670 make_call_insn_raw);
4673 /* Like emit_call_insn_before_noloc,
4674 but set insn_location according to BEFORE. */
4676 emit_call_insn_before (rtx pattern, rtx before)
4678 return emit_pattern_before (pattern, before, true, false,
4679 make_call_insn_raw);
4682 /* Like emit_insn_before_noloc, but set INSN_LOCATION according to LOC. */
4684 emit_debug_insn_before_setloc (rtx pattern, rtx before, int loc)
4686 return emit_pattern_before_setloc (pattern, before, loc, false,
4687 make_debug_insn_raw);
4690 /* Like emit_debug_insn_before_noloc,
4691 but set insn_location according to BEFORE. */
4693 emit_debug_insn_before (rtx pattern, rtx before)
4695 return emit_pattern_before (pattern, before, false, false,
4696 make_debug_insn_raw);
4699 /* Take X and emit it at the end of the doubly-linked
4700 INSN list.
4702 Returns the last insn emitted. */
4705 emit_insn (rtx x)
4707 rtx last = get_last_insn ();
4708 rtx insn;
4710 if (x == NULL_RTX)
4711 return last;
4713 switch (GET_CODE (x))
4715 case DEBUG_INSN:
4716 case INSN:
4717 case JUMP_INSN:
4718 case CALL_INSN:
4719 case CODE_LABEL:
4720 case BARRIER:
4721 case NOTE:
4722 insn = x;
4723 while (insn)
4725 rtx next = NEXT_INSN (insn);
4726 add_insn (insn);
4727 last = insn;
4728 insn = next;
4730 break;
4732 #ifdef ENABLE_RTL_CHECKING
4733 case JUMP_TABLE_DATA:
4734 case SEQUENCE:
4735 gcc_unreachable ();
4736 break;
4737 #endif
4739 default:
4740 last = make_insn_raw (x);
4741 add_insn (last);
4742 break;
4745 return last;
4748 /* Make an insn of code DEBUG_INSN with pattern X
4749 and add it to the end of the doubly-linked list. */
4752 emit_debug_insn (rtx x)
4754 rtx last = get_last_insn ();
4755 rtx insn;
4757 if (x == NULL_RTX)
4758 return last;
4760 switch (GET_CODE (x))
4762 case DEBUG_INSN:
4763 case INSN:
4764 case JUMP_INSN:
4765 case CALL_INSN:
4766 case CODE_LABEL:
4767 case BARRIER:
4768 case NOTE:
4769 insn = x;
4770 while (insn)
4772 rtx next = NEXT_INSN (insn);
4773 add_insn (insn);
4774 last = insn;
4775 insn = next;
4777 break;
4779 #ifdef ENABLE_RTL_CHECKING
4780 case JUMP_TABLE_DATA:
4781 case SEQUENCE:
4782 gcc_unreachable ();
4783 break;
4784 #endif
4786 default:
4787 last = make_debug_insn_raw (x);
4788 add_insn (last);
4789 break;
4792 return last;
4795 /* Make an insn of code JUMP_INSN with pattern X
4796 and add it to the end of the doubly-linked list. */
4799 emit_jump_insn (rtx x)
4801 rtx last = NULL_RTX, insn;
4803 switch (GET_CODE (x))
4805 case DEBUG_INSN:
4806 case INSN:
4807 case JUMP_INSN:
4808 case CALL_INSN:
4809 case CODE_LABEL:
4810 case BARRIER:
4811 case NOTE:
4812 insn = x;
4813 while (insn)
4815 rtx next = NEXT_INSN (insn);
4816 add_insn (insn);
4817 last = insn;
4818 insn = next;
4820 break;
4822 #ifdef ENABLE_RTL_CHECKING
4823 case JUMP_TABLE_DATA:
4824 case SEQUENCE:
4825 gcc_unreachable ();
4826 break;
4827 #endif
4829 default:
4830 last = make_jump_insn_raw (x);
4831 add_insn (last);
4832 break;
4835 return last;
4838 /* Make an insn of code CALL_INSN with pattern X
4839 and add it to the end of the doubly-linked list. */
4842 emit_call_insn (rtx x)
4844 rtx insn;
4846 switch (GET_CODE (x))
4848 case DEBUG_INSN:
4849 case INSN:
4850 case JUMP_INSN:
4851 case CALL_INSN:
4852 case CODE_LABEL:
4853 case BARRIER:
4854 case NOTE:
4855 insn = emit_insn (x);
4856 break;
4858 #ifdef ENABLE_RTL_CHECKING
4859 case SEQUENCE:
4860 case JUMP_TABLE_DATA:
4861 gcc_unreachable ();
4862 break;
4863 #endif
4865 default:
4866 insn = make_call_insn_raw (x);
4867 add_insn (insn);
4868 break;
4871 return insn;
4874 /* Add the label LABEL to the end of the doubly-linked list. */
4877 emit_label (rtx label)
4879 gcc_checking_assert (INSN_UID (label) == 0);
4880 INSN_UID (label) = cur_insn_uid++;
4881 add_insn (label);
4882 return label;
4885 /* Make an insn of code JUMP_TABLE_DATA
4886 and add it to the end of the doubly-linked list. */
4889 emit_jump_table_data (rtx table)
4891 rtx jump_table_data = rtx_alloc (JUMP_TABLE_DATA);
4892 INSN_UID (jump_table_data) = cur_insn_uid++;
4893 PATTERN (jump_table_data) = table;
4894 BLOCK_FOR_INSN (jump_table_data) = NULL;
4895 add_insn (jump_table_data);
4896 return jump_table_data;
4899 /* Make an insn of code BARRIER
4900 and add it to the end of the doubly-linked list. */
4903 emit_barrier (void)
4905 rtx barrier = rtx_alloc (BARRIER);
4906 INSN_UID (barrier) = cur_insn_uid++;
4907 add_insn (barrier);
4908 return barrier;
4911 /* Emit a copy of note ORIG. */
4914 emit_note_copy (rtx orig)
4916 enum insn_note kind = (enum insn_note) NOTE_KIND (orig);
4917 rtx note = make_note_raw (kind);
4918 NOTE_DATA (note) = NOTE_DATA (orig);
4919 add_insn (note);
4920 return note;
4923 /* Make an insn of code NOTE or type NOTE_NO
4924 and add it to the end of the doubly-linked list. */
4927 emit_note (enum insn_note kind)
4929 rtx note = make_note_raw (kind);
4930 add_insn (note);
4931 return note;
4934 /* Emit a clobber of lvalue X. */
4937 emit_clobber (rtx x)
4939 /* CONCATs should not appear in the insn stream. */
4940 if (GET_CODE (x) == CONCAT)
4942 emit_clobber (XEXP (x, 0));
4943 return emit_clobber (XEXP (x, 1));
4945 return emit_insn (gen_rtx_CLOBBER (VOIDmode, x));
4948 /* Return a sequence of insns to clobber lvalue X. */
4951 gen_clobber (rtx x)
4953 rtx seq;
4955 start_sequence ();
4956 emit_clobber (x);
4957 seq = get_insns ();
4958 end_sequence ();
4959 return seq;
4962 /* Emit a use of rvalue X. */
4965 emit_use (rtx x)
4967 /* CONCATs should not appear in the insn stream. */
4968 if (GET_CODE (x) == CONCAT)
4970 emit_use (XEXP (x, 0));
4971 return emit_use (XEXP (x, 1));
4973 return emit_insn (gen_rtx_USE (VOIDmode, x));
4976 /* Return a sequence of insns to use rvalue X. */
4979 gen_use (rtx x)
4981 rtx seq;
4983 start_sequence ();
4984 emit_use (x);
4985 seq = get_insns ();
4986 end_sequence ();
4987 return seq;
4990 /* Place a note of KIND on insn INSN with DATUM as the datum. If a
4991 note of this type already exists, remove it first. */
4994 set_unique_reg_note (rtx insn, enum reg_note kind, rtx datum)
4996 rtx note = find_reg_note (insn, kind, NULL_RTX);
4998 switch (kind)
5000 case REG_EQUAL:
5001 case REG_EQUIV:
5002 /* Don't add REG_EQUAL/REG_EQUIV notes if the insn
5003 has multiple sets (some callers assume single_set
5004 means the insn only has one set, when in fact it
5005 means the insn only has one * useful * set). */
5006 if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
5008 gcc_assert (!note);
5009 return NULL_RTX;
5012 /* Don't add ASM_OPERAND REG_EQUAL/REG_EQUIV notes.
5013 It serves no useful purpose and breaks eliminate_regs. */
5014 if (GET_CODE (datum) == ASM_OPERANDS)
5015 return NULL_RTX;
5017 if (note)
5019 XEXP (note, 0) = datum;
5020 df_notes_rescan (insn);
5021 return note;
5023 break;
5025 default:
5026 if (note)
5028 XEXP (note, 0) = datum;
5029 return note;
5031 break;
5034 add_reg_note (insn, kind, datum);
5036 switch (kind)
5038 case REG_EQUAL:
5039 case REG_EQUIV:
5040 df_notes_rescan (insn);
5041 break;
5042 default:
5043 break;
5046 return REG_NOTES (insn);
5049 /* Like set_unique_reg_note, but don't do anything unless INSN sets DST. */
5051 set_dst_reg_note (rtx insn, enum reg_note kind, rtx datum, rtx dst)
5053 rtx set = single_set (insn);
5055 if (set && SET_DEST (set) == dst)
5056 return set_unique_reg_note (insn, kind, datum);
5057 return NULL_RTX;
5060 /* Return an indication of which type of insn should have X as a body.
5061 The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN. */
5063 static enum rtx_code
5064 classify_insn (rtx x)
5066 if (LABEL_P (x))
5067 return CODE_LABEL;
5068 if (GET_CODE (x) == CALL)
5069 return CALL_INSN;
5070 if (ANY_RETURN_P (x))
5071 return JUMP_INSN;
5072 if (GET_CODE (x) == SET)
5074 if (SET_DEST (x) == pc_rtx)
5075 return JUMP_INSN;
5076 else if (GET_CODE (SET_SRC (x)) == CALL)
5077 return CALL_INSN;
5078 else
5079 return INSN;
5081 if (GET_CODE (x) == PARALLEL)
5083 int j;
5084 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
5085 if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
5086 return CALL_INSN;
5087 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
5088 && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
5089 return JUMP_INSN;
5090 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
5091 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
5092 return CALL_INSN;
5094 return INSN;
5097 /* Emit the rtl pattern X as an appropriate kind of insn.
5098 If X is a label, it is simply added into the insn chain. */
5101 emit (rtx x)
5103 enum rtx_code code = classify_insn (x);
5105 switch (code)
5107 case CODE_LABEL:
5108 return emit_label (x);
5109 case INSN:
5110 return emit_insn (x);
5111 case JUMP_INSN:
5113 rtx insn = emit_jump_insn (x);
5114 if (any_uncondjump_p (insn) || GET_CODE (x) == RETURN)
5115 return emit_barrier ();
5116 return insn;
5118 case CALL_INSN:
5119 return emit_call_insn (x);
5120 case DEBUG_INSN:
5121 return emit_debug_insn (x);
5122 default:
5123 gcc_unreachable ();
5127 /* Space for free sequence stack entries. */
5128 static GTY ((deletable)) struct sequence_stack *free_sequence_stack;
5130 /* Begin emitting insns to a sequence. If this sequence will contain
5131 something that might cause the compiler to pop arguments to function
5132 calls (because those pops have previously been deferred; see
5133 INHIBIT_DEFER_POP for more details), use do_pending_stack_adjust
5134 before calling this function. That will ensure that the deferred
5135 pops are not accidentally emitted in the middle of this sequence. */
5137 void
5138 start_sequence (void)
5140 struct sequence_stack *tem;
5142 if (free_sequence_stack != NULL)
5144 tem = free_sequence_stack;
5145 free_sequence_stack = tem->next;
5147 else
5148 tem = ggc_alloc_sequence_stack ();
5150 tem->next = seq_stack;
5151 tem->first = get_insns ();
5152 tem->last = get_last_insn ();
5154 seq_stack = tem;
5156 set_first_insn (0);
5157 set_last_insn (0);
5160 /* Set up the insn chain starting with FIRST as the current sequence,
5161 saving the previously current one. See the documentation for
5162 start_sequence for more information about how to use this function. */
5164 void
5165 push_to_sequence (rtx first)
5167 rtx last;
5169 start_sequence ();
5171 for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last))
5174 set_first_insn (first);
5175 set_last_insn (last);
5178 /* Like push_to_sequence, but take the last insn as an argument to avoid
5179 looping through the list. */
5181 void
5182 push_to_sequence2 (rtx first, rtx last)
5184 start_sequence ();
5186 set_first_insn (first);
5187 set_last_insn (last);
5190 /* Set up the outer-level insn chain
5191 as the current sequence, saving the previously current one. */
5193 void
5194 push_topmost_sequence (void)
5196 struct sequence_stack *stack, *top = NULL;
5198 start_sequence ();
5200 for (stack = seq_stack; stack; stack = stack->next)
5201 top = stack;
5203 set_first_insn (top->first);
5204 set_last_insn (top->last);
5207 /* After emitting to the outer-level insn chain, update the outer-level
5208 insn chain, and restore the previous saved state. */
5210 void
5211 pop_topmost_sequence (void)
5213 struct sequence_stack *stack, *top = NULL;
5215 for (stack = seq_stack; stack; stack = stack->next)
5216 top = stack;
5218 top->first = get_insns ();
5219 top->last = get_last_insn ();
5221 end_sequence ();
5224 /* After emitting to a sequence, restore previous saved state.
5226 To get the contents of the sequence just made, you must call
5227 `get_insns' *before* calling here.
5229 If the compiler might have deferred popping arguments while
5230 generating this sequence, and this sequence will not be immediately
5231 inserted into the instruction stream, use do_pending_stack_adjust
5232 before calling get_insns. That will ensure that the deferred
5233 pops are inserted into this sequence, and not into some random
5234 location in the instruction stream. See INHIBIT_DEFER_POP for more
5235 information about deferred popping of arguments. */
5237 void
5238 end_sequence (void)
5240 struct sequence_stack *tem = seq_stack;
5242 set_first_insn (tem->first);
5243 set_last_insn (tem->last);
5244 seq_stack = tem->next;
5246 memset (tem, 0, sizeof (*tem));
5247 tem->next = free_sequence_stack;
5248 free_sequence_stack = tem;
5251 /* Return 1 if currently emitting into a sequence. */
5254 in_sequence_p (void)
5256 return seq_stack != 0;
5259 /* Put the various virtual registers into REGNO_REG_RTX. */
5261 static void
5262 init_virtual_regs (void)
5264 regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
5265 regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
5266 regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
5267 regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
5268 regno_reg_rtx[VIRTUAL_CFA_REGNUM] = virtual_cfa_rtx;
5269 regno_reg_rtx[VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM]
5270 = virtual_preferred_stack_boundary_rtx;
5274 /* Used by copy_insn_1 to avoid copying SCRATCHes more than once. */
5275 static rtx copy_insn_scratch_in[MAX_RECOG_OPERANDS];
5276 static rtx copy_insn_scratch_out[MAX_RECOG_OPERANDS];
5277 static int copy_insn_n_scratches;
5279 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5280 copied an ASM_OPERANDS.
5281 In that case, it is the original input-operand vector. */
5282 static rtvec orig_asm_operands_vector;
5284 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
5285 copied an ASM_OPERANDS.
5286 In that case, it is the copied input-operand vector. */
5287 static rtvec copy_asm_operands_vector;
5289 /* Likewise for the constraints vector. */
5290 static rtvec orig_asm_constraints_vector;
5291 static rtvec copy_asm_constraints_vector;
5293 /* Recursively create a new copy of an rtx for copy_insn.
5294 This function differs from copy_rtx in that it handles SCRATCHes and
5295 ASM_OPERANDs properly.
5296 Normally, this function is not used directly; use copy_insn as front end.
5297 However, you could first copy an insn pattern with copy_insn and then use
5298 this function afterwards to properly copy any REG_NOTEs containing
5299 SCRATCHes. */
5302 copy_insn_1 (rtx orig)
5304 rtx copy;
5305 int i, j;
5306 RTX_CODE code;
5307 const char *format_ptr;
5309 if (orig == NULL)
5310 return NULL;
5312 code = GET_CODE (orig);
5314 switch (code)
5316 case REG:
5317 case DEBUG_EXPR:
5318 CASE_CONST_ANY:
5319 case SYMBOL_REF:
5320 case CODE_LABEL:
5321 case PC:
5322 case CC0:
5323 case RETURN:
5324 case SIMPLE_RETURN:
5325 return orig;
5326 case CLOBBER:
5327 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
5328 clobbers or clobbers of hard registers that originated as pseudos.
5329 This is needed to allow safe register renaming. */
5330 if (REG_P (XEXP (orig, 0)) && REGNO (XEXP (orig, 0)) < FIRST_PSEUDO_REGISTER
5331 && ORIGINAL_REGNO (XEXP (orig, 0)) == REGNO (XEXP (orig, 0)))
5332 return orig;
5333 break;
5335 case SCRATCH:
5336 for (i = 0; i < copy_insn_n_scratches; i++)
5337 if (copy_insn_scratch_in[i] == orig)
5338 return copy_insn_scratch_out[i];
5339 break;
5341 case CONST:
5342 if (shared_const_p (orig))
5343 return orig;
5344 break;
5346 /* A MEM with a constant address is not sharable. The problem is that
5347 the constant address may need to be reloaded. If the mem is shared,
5348 then reloading one copy of this mem will cause all copies to appear
5349 to have been reloaded. */
5351 default:
5352 break;
5355 /* Copy the various flags, fields, and other information. We assume
5356 that all fields need copying, and then clear the fields that should
5357 not be copied. That is the sensible default behavior, and forces
5358 us to explicitly document why we are *not* copying a flag. */
5359 copy = shallow_copy_rtx (orig);
5361 /* We do not copy the USED flag, which is used as a mark bit during
5362 walks over the RTL. */
5363 RTX_FLAG (copy, used) = 0;
5365 /* We do not copy JUMP, CALL, or FRAME_RELATED for INSNs. */
5366 if (INSN_P (orig))
5368 RTX_FLAG (copy, jump) = 0;
5369 RTX_FLAG (copy, call) = 0;
5370 RTX_FLAG (copy, frame_related) = 0;
5373 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
5375 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
5376 switch (*format_ptr++)
5378 case 'e':
5379 if (XEXP (orig, i) != NULL)
5380 XEXP (copy, i) = copy_insn_1 (XEXP (orig, i));
5381 break;
5383 case 'E':
5384 case 'V':
5385 if (XVEC (orig, i) == orig_asm_constraints_vector)
5386 XVEC (copy, i) = copy_asm_constraints_vector;
5387 else if (XVEC (orig, i) == orig_asm_operands_vector)
5388 XVEC (copy, i) = copy_asm_operands_vector;
5389 else if (XVEC (orig, i) != NULL)
5391 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
5392 for (j = 0; j < XVECLEN (copy, i); j++)
5393 XVECEXP (copy, i, j) = copy_insn_1 (XVECEXP (orig, i, j));
5395 break;
5397 case 't':
5398 case 'w':
5399 case 'i':
5400 case 's':
5401 case 'S':
5402 case 'u':
5403 case '0':
5404 /* These are left unchanged. */
5405 break;
5407 default:
5408 gcc_unreachable ();
5411 if (code == SCRATCH)
5413 i = copy_insn_n_scratches++;
5414 gcc_assert (i < MAX_RECOG_OPERANDS);
5415 copy_insn_scratch_in[i] = orig;
5416 copy_insn_scratch_out[i] = copy;
5418 else if (code == ASM_OPERANDS)
5420 orig_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (orig);
5421 copy_asm_operands_vector = ASM_OPERANDS_INPUT_VEC (copy);
5422 orig_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (orig);
5423 copy_asm_constraints_vector = ASM_OPERANDS_INPUT_CONSTRAINT_VEC (copy);
5426 return copy;
5429 /* Create a new copy of an rtx.
5430 This function differs from copy_rtx in that it handles SCRATCHes and
5431 ASM_OPERANDs properly.
5432 INSN doesn't really have to be a full INSN; it could be just the
5433 pattern. */
5435 copy_insn (rtx insn)
5437 copy_insn_n_scratches = 0;
5438 orig_asm_operands_vector = 0;
5439 orig_asm_constraints_vector = 0;
5440 copy_asm_operands_vector = 0;
5441 copy_asm_constraints_vector = 0;
5442 return copy_insn_1 (insn);
5445 /* Return a copy of INSN that can be used in a SEQUENCE delay slot,
5446 on that assumption that INSN itself remains in its original place. */
5449 copy_delay_slot_insn (rtx insn)
5451 /* Copy INSN with its rtx_code, all its notes, location etc. */
5452 insn = copy_rtx (insn);
5453 INSN_UID (insn) = cur_insn_uid++;
5454 return insn;
5457 /* Initialize data structures and variables in this file
5458 before generating rtl for each function. */
5460 void
5461 init_emit (void)
5463 set_first_insn (NULL);
5464 set_last_insn (NULL);
5465 if (MIN_NONDEBUG_INSN_UID)
5466 cur_insn_uid = MIN_NONDEBUG_INSN_UID;
5467 else
5468 cur_insn_uid = 1;
5469 cur_debug_insn_uid = 1;
5470 reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
5471 first_label_num = label_num;
5472 seq_stack = NULL;
5474 /* Init the tables that describe all the pseudo regs. */
5476 crtl->emit.regno_pointer_align_length = LAST_VIRTUAL_REGISTER + 101;
5478 crtl->emit.regno_pointer_align
5479 = XCNEWVEC (unsigned char, crtl->emit.regno_pointer_align_length);
5481 regno_reg_rtx = ggc_alloc_vec_rtx (crtl->emit.regno_pointer_align_length);
5483 /* Put copies of all the hard registers into regno_reg_rtx. */
5484 memcpy (regno_reg_rtx,
5485 initial_regno_reg_rtx,
5486 FIRST_PSEUDO_REGISTER * sizeof (rtx));
5488 /* Put copies of all the virtual register rtx into regno_reg_rtx. */
5489 init_virtual_regs ();
5491 /* Indicate that the virtual registers and stack locations are
5492 all pointers. */
5493 REG_POINTER (stack_pointer_rtx) = 1;
5494 REG_POINTER (frame_pointer_rtx) = 1;
5495 REG_POINTER (hard_frame_pointer_rtx) = 1;
5496 REG_POINTER (arg_pointer_rtx) = 1;
5498 REG_POINTER (virtual_incoming_args_rtx) = 1;
5499 REG_POINTER (virtual_stack_vars_rtx) = 1;
5500 REG_POINTER (virtual_stack_dynamic_rtx) = 1;
5501 REG_POINTER (virtual_outgoing_args_rtx) = 1;
5502 REG_POINTER (virtual_cfa_rtx) = 1;
5504 #ifdef STACK_BOUNDARY
5505 REGNO_POINTER_ALIGN (STACK_POINTER_REGNUM) = STACK_BOUNDARY;
5506 REGNO_POINTER_ALIGN (FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5507 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = STACK_BOUNDARY;
5508 REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) = STACK_BOUNDARY;
5510 REGNO_POINTER_ALIGN (VIRTUAL_INCOMING_ARGS_REGNUM) = STACK_BOUNDARY;
5511 REGNO_POINTER_ALIGN (VIRTUAL_STACK_VARS_REGNUM) = STACK_BOUNDARY;
5512 REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM) = STACK_BOUNDARY;
5513 REGNO_POINTER_ALIGN (VIRTUAL_OUTGOING_ARGS_REGNUM) = STACK_BOUNDARY;
5514 REGNO_POINTER_ALIGN (VIRTUAL_CFA_REGNUM) = BITS_PER_WORD;
5515 #endif
5517 #ifdef INIT_EXPANDERS
5518 INIT_EXPANDERS;
5519 #endif
5522 /* Generate a vector constant for mode MODE and constant value CONSTANT. */
5524 static rtx
5525 gen_const_vector (enum machine_mode mode, int constant)
5527 rtx tem;
5528 rtvec v;
5529 int units, i;
5530 enum machine_mode inner;
5532 units = GET_MODE_NUNITS (mode);
5533 inner = GET_MODE_INNER (mode);
5535 gcc_assert (!DECIMAL_FLOAT_MODE_P (inner));
5537 v = rtvec_alloc (units);
5539 /* We need to call this function after we set the scalar const_tiny_rtx
5540 entries. */
5541 gcc_assert (const_tiny_rtx[constant][(int) inner]);
5543 for (i = 0; i < units; ++i)
5544 RTVEC_ELT (v, i) = const_tiny_rtx[constant][(int) inner];
5546 tem = gen_rtx_raw_CONST_VECTOR (mode, v);
5547 return tem;
5550 /* Generate a vector like gen_rtx_raw_CONST_VEC, but use the zero vector when
5551 all elements are zero, and the one vector when all elements are one. */
5553 gen_rtx_CONST_VECTOR (enum machine_mode mode, rtvec v)
5555 enum machine_mode inner = GET_MODE_INNER (mode);
5556 int nunits = GET_MODE_NUNITS (mode);
5557 rtx x;
5558 int i;
5560 /* Check to see if all of the elements have the same value. */
5561 x = RTVEC_ELT (v, nunits - 1);
5562 for (i = nunits - 2; i >= 0; i--)
5563 if (RTVEC_ELT (v, i) != x)
5564 break;
5566 /* If the values are all the same, check to see if we can use one of the
5567 standard constant vectors. */
5568 if (i == -1)
5570 if (x == CONST0_RTX (inner))
5571 return CONST0_RTX (mode);
5572 else if (x == CONST1_RTX (inner))
5573 return CONST1_RTX (mode);
5574 else if (x == CONSTM1_RTX (inner))
5575 return CONSTM1_RTX (mode);
5578 return gen_rtx_raw_CONST_VECTOR (mode, v);
5581 /* Initialise global register information required by all functions. */
5583 void
5584 init_emit_regs (void)
5586 int i;
5587 enum machine_mode mode;
5588 mem_attrs *attrs;
5590 /* Reset register attributes */
5591 htab_empty (reg_attrs_htab);
5593 /* We need reg_raw_mode, so initialize the modes now. */
5594 init_reg_modes_target ();
5596 /* Assign register numbers to the globally defined register rtx. */
5597 stack_pointer_rtx = gen_raw_REG (Pmode, STACK_POINTER_REGNUM);
5598 frame_pointer_rtx = gen_raw_REG (Pmode, FRAME_POINTER_REGNUM);
5599 hard_frame_pointer_rtx = gen_raw_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
5600 arg_pointer_rtx = gen_raw_REG (Pmode, ARG_POINTER_REGNUM);
5601 virtual_incoming_args_rtx =
5602 gen_raw_REG (Pmode, VIRTUAL_INCOMING_ARGS_REGNUM);
5603 virtual_stack_vars_rtx =
5604 gen_raw_REG (Pmode, VIRTUAL_STACK_VARS_REGNUM);
5605 virtual_stack_dynamic_rtx =
5606 gen_raw_REG (Pmode, VIRTUAL_STACK_DYNAMIC_REGNUM);
5607 virtual_outgoing_args_rtx =
5608 gen_raw_REG (Pmode, VIRTUAL_OUTGOING_ARGS_REGNUM);
5609 virtual_cfa_rtx = gen_raw_REG (Pmode, VIRTUAL_CFA_REGNUM);
5610 virtual_preferred_stack_boundary_rtx =
5611 gen_raw_REG (Pmode, VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM);
5613 /* Initialize RTL for commonly used hard registers. These are
5614 copied into regno_reg_rtx as we begin to compile each function. */
5615 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5616 initial_regno_reg_rtx[i] = gen_raw_REG (reg_raw_mode[i], i);
5618 #ifdef RETURN_ADDRESS_POINTER_REGNUM
5619 return_address_pointer_rtx
5620 = gen_raw_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM);
5621 #endif
5623 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
5624 pic_offset_table_rtx = gen_raw_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
5625 else
5626 pic_offset_table_rtx = NULL_RTX;
5628 for (i = 0; i < (int) MAX_MACHINE_MODE; i++)
5630 mode = (enum machine_mode) i;
5631 attrs = ggc_alloc_cleared_mem_attrs ();
5632 attrs->align = BITS_PER_UNIT;
5633 attrs->addrspace = ADDR_SPACE_GENERIC;
5634 if (mode != BLKmode)
5636 attrs->size_known_p = true;
5637 attrs->size = GET_MODE_SIZE (mode);
5638 if (STRICT_ALIGNMENT)
5639 attrs->align = GET_MODE_ALIGNMENT (mode);
5641 mode_mem_attrs[i] = attrs;
5645 /* Create some permanent unique rtl objects shared between all functions. */
5647 void
5648 init_emit_once (void)
5650 int i;
5651 enum machine_mode mode;
5652 enum machine_mode double_mode;
5654 /* Initialize the CONST_INT, CONST_DOUBLE, CONST_FIXED, and memory attribute
5655 hash tables. */
5656 const_int_htab = htab_create_ggc (37, const_int_htab_hash,
5657 const_int_htab_eq, NULL);
5659 const_double_htab = htab_create_ggc (37, const_double_htab_hash,
5660 const_double_htab_eq, NULL);
5662 const_fixed_htab = htab_create_ggc (37, const_fixed_htab_hash,
5663 const_fixed_htab_eq, NULL);
5665 mem_attrs_htab = htab_create_ggc (37, mem_attrs_htab_hash,
5666 mem_attrs_htab_eq, NULL);
5667 reg_attrs_htab = htab_create_ggc (37, reg_attrs_htab_hash,
5668 reg_attrs_htab_eq, NULL);
5670 /* Compute the word and byte modes. */
5672 byte_mode = VOIDmode;
5673 word_mode = VOIDmode;
5674 double_mode = VOIDmode;
5676 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5677 mode != VOIDmode;
5678 mode = GET_MODE_WIDER_MODE (mode))
5680 if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
5681 && byte_mode == VOIDmode)
5682 byte_mode = mode;
5684 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
5685 && word_mode == VOIDmode)
5686 word_mode = mode;
5689 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
5690 mode != VOIDmode;
5691 mode = GET_MODE_WIDER_MODE (mode))
5693 if (GET_MODE_BITSIZE (mode) == DOUBLE_TYPE_SIZE
5694 && double_mode == VOIDmode)
5695 double_mode = mode;
5698 ptr_mode = mode_for_size (POINTER_SIZE, GET_MODE_CLASS (Pmode), 0);
5700 #ifdef INIT_EXPANDERS
5701 /* This is to initialize {init|mark|free}_machine_status before the first
5702 call to push_function_context_to. This is needed by the Chill front
5703 end which calls push_function_context_to before the first call to
5704 init_function_start. */
5705 INIT_EXPANDERS;
5706 #endif
5708 /* Create the unique rtx's for certain rtx codes and operand values. */
5710 /* Don't use gen_rtx_CONST_INT here since gen_rtx_CONST_INT in this case
5711 tries to use these variables. */
5712 for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
5713 const_int_rtx[i + MAX_SAVED_CONST_INT] =
5714 gen_rtx_raw_CONST_INT (VOIDmode, (HOST_WIDE_INT) i);
5716 if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT
5717 && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT)
5718 const_true_rtx = const_int_rtx[STORE_FLAG_VALUE + MAX_SAVED_CONST_INT];
5719 else
5720 const_true_rtx = gen_rtx_CONST_INT (VOIDmode, STORE_FLAG_VALUE);
5722 REAL_VALUE_FROM_INT (dconst0, 0, 0, double_mode);
5723 REAL_VALUE_FROM_INT (dconst1, 1, 0, double_mode);
5724 REAL_VALUE_FROM_INT (dconst2, 2, 0, double_mode);
5726 dconstm1 = dconst1;
5727 dconstm1.sign = 1;
5729 dconsthalf = dconst1;
5730 SET_REAL_EXP (&dconsthalf, REAL_EXP (&dconsthalf) - 1);
5732 for (i = 0; i < 3; i++)
5734 const REAL_VALUE_TYPE *const r =
5735 (i == 0 ? &dconst0 : i == 1 ? &dconst1 : &dconst2);
5737 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
5738 mode != VOIDmode;
5739 mode = GET_MODE_WIDER_MODE (mode))
5740 const_tiny_rtx[i][(int) mode] =
5741 CONST_DOUBLE_FROM_REAL_VALUE (*r, mode);
5743 for (mode = GET_CLASS_NARROWEST_MODE (MODE_DECIMAL_FLOAT);
5744 mode != VOIDmode;
5745 mode = GET_MODE_WIDER_MODE (mode))
5746 const_tiny_rtx[i][(int) mode] =
5747 CONST_DOUBLE_FROM_REAL_VALUE (*r, mode);
5749 const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
5751 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5752 mode != VOIDmode;
5753 mode = GET_MODE_WIDER_MODE (mode))
5754 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5756 for (mode = MIN_MODE_PARTIAL_INT;
5757 mode <= MAX_MODE_PARTIAL_INT;
5758 mode = (enum machine_mode)((int)(mode) + 1))
5759 const_tiny_rtx[i][(int) mode] = GEN_INT (i);
5762 const_tiny_rtx[3][(int) VOIDmode] = constm1_rtx;
5764 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5765 mode != VOIDmode;
5766 mode = GET_MODE_WIDER_MODE (mode))
5767 const_tiny_rtx[3][(int) mode] = constm1_rtx;
5769 for (mode = MIN_MODE_PARTIAL_INT;
5770 mode <= MAX_MODE_PARTIAL_INT;
5771 mode = (enum machine_mode)((int)(mode) + 1))
5772 const_tiny_rtx[3][(int) mode] = constm1_rtx;
5774 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_INT);
5775 mode != VOIDmode;
5776 mode = GET_MODE_WIDER_MODE (mode))
5778 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
5779 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
5782 for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
5783 mode != VOIDmode;
5784 mode = GET_MODE_WIDER_MODE (mode))
5786 rtx inner = const_tiny_rtx[0][(int)GET_MODE_INNER (mode)];
5787 const_tiny_rtx[0][(int) mode] = gen_rtx_CONCAT (mode, inner, inner);
5790 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
5791 mode != VOIDmode;
5792 mode = GET_MODE_WIDER_MODE (mode))
5794 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5795 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
5796 const_tiny_rtx[3][(int) mode] = gen_const_vector (mode, 3);
5799 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
5800 mode != VOIDmode;
5801 mode = GET_MODE_WIDER_MODE (mode))
5803 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5804 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
5807 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FRACT);
5808 mode != VOIDmode;
5809 mode = GET_MODE_WIDER_MODE (mode))
5811 FCONST0 (mode).data.high = 0;
5812 FCONST0 (mode).data.low = 0;
5813 FCONST0 (mode).mode = mode;
5814 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5815 FCONST0 (mode), mode);
5818 for (mode = GET_CLASS_NARROWEST_MODE (MODE_UFRACT);
5819 mode != VOIDmode;
5820 mode = GET_MODE_WIDER_MODE (mode))
5822 FCONST0 (mode).data.high = 0;
5823 FCONST0 (mode).data.low = 0;
5824 FCONST0 (mode).mode = mode;
5825 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5826 FCONST0 (mode), mode);
5829 for (mode = GET_CLASS_NARROWEST_MODE (MODE_ACCUM);
5830 mode != VOIDmode;
5831 mode = GET_MODE_WIDER_MODE (mode))
5833 FCONST0 (mode).data.high = 0;
5834 FCONST0 (mode).data.low = 0;
5835 FCONST0 (mode).mode = mode;
5836 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5837 FCONST0 (mode), mode);
5839 /* We store the value 1. */
5840 FCONST1 (mode).data.high = 0;
5841 FCONST1 (mode).data.low = 0;
5842 FCONST1 (mode).mode = mode;
5843 FCONST1 (mode).data
5844 = double_int_one.lshift (GET_MODE_FBIT (mode),
5845 HOST_BITS_PER_DOUBLE_INT,
5846 SIGNED_FIXED_POINT_MODE_P (mode));
5847 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5848 FCONST1 (mode), mode);
5851 for (mode = GET_CLASS_NARROWEST_MODE (MODE_UACCUM);
5852 mode != VOIDmode;
5853 mode = GET_MODE_WIDER_MODE (mode))
5855 FCONST0 (mode).data.high = 0;
5856 FCONST0 (mode).data.low = 0;
5857 FCONST0 (mode).mode = mode;
5858 const_tiny_rtx[0][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5859 FCONST0 (mode), mode);
5861 /* We store the value 1. */
5862 FCONST1 (mode).data.high = 0;
5863 FCONST1 (mode).data.low = 0;
5864 FCONST1 (mode).mode = mode;
5865 FCONST1 (mode).data
5866 = double_int_one.lshift (GET_MODE_FBIT (mode),
5867 HOST_BITS_PER_DOUBLE_INT,
5868 SIGNED_FIXED_POINT_MODE_P (mode));
5869 const_tiny_rtx[1][(int) mode] = CONST_FIXED_FROM_FIXED_VALUE (
5870 FCONST1 (mode), mode);
5873 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FRACT);
5874 mode != VOIDmode;
5875 mode = GET_MODE_WIDER_MODE (mode))
5877 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5880 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_UFRACT);
5881 mode != VOIDmode;
5882 mode = GET_MODE_WIDER_MODE (mode))
5884 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5887 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_ACCUM);
5888 mode != VOIDmode;
5889 mode = GET_MODE_WIDER_MODE (mode))
5891 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5892 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
5895 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_UACCUM);
5896 mode != VOIDmode;
5897 mode = GET_MODE_WIDER_MODE (mode))
5899 const_tiny_rtx[0][(int) mode] = gen_const_vector (mode, 0);
5900 const_tiny_rtx[1][(int) mode] = gen_const_vector (mode, 1);
5903 for (i = (int) CCmode; i < (int) MAX_MACHINE_MODE; ++i)
5904 if (GET_MODE_CLASS ((enum machine_mode) i) == MODE_CC)
5905 const_tiny_rtx[0][i] = const0_rtx;
5907 const_tiny_rtx[0][(int) BImode] = const0_rtx;
5908 if (STORE_FLAG_VALUE == 1)
5909 const_tiny_rtx[1][(int) BImode] = const1_rtx;
5911 pc_rtx = gen_rtx_fmt_ (PC, VOIDmode);
5912 ret_rtx = gen_rtx_fmt_ (RETURN, VOIDmode);
5913 simple_return_rtx = gen_rtx_fmt_ (SIMPLE_RETURN, VOIDmode);
5914 cc0_rtx = gen_rtx_fmt_ (CC0, VOIDmode);
5917 /* Produce exact duplicate of insn INSN after AFTER.
5918 Care updating of libcall regions if present. */
5921 emit_copy_of_insn_after (rtx insn, rtx after)
5923 rtx new_rtx, link;
5925 switch (GET_CODE (insn))
5927 case INSN:
5928 new_rtx = emit_insn_after (copy_insn (PATTERN (insn)), after);
5929 break;
5931 case JUMP_INSN:
5932 new_rtx = emit_jump_insn_after (copy_insn (PATTERN (insn)), after);
5933 break;
5935 case DEBUG_INSN:
5936 new_rtx = emit_debug_insn_after (copy_insn (PATTERN (insn)), after);
5937 break;
5939 case CALL_INSN:
5940 new_rtx = emit_call_insn_after (copy_insn (PATTERN (insn)), after);
5941 if (CALL_INSN_FUNCTION_USAGE (insn))
5942 CALL_INSN_FUNCTION_USAGE (new_rtx)
5943 = copy_insn (CALL_INSN_FUNCTION_USAGE (insn));
5944 SIBLING_CALL_P (new_rtx) = SIBLING_CALL_P (insn);
5945 RTL_CONST_CALL_P (new_rtx) = RTL_CONST_CALL_P (insn);
5946 RTL_PURE_CALL_P (new_rtx) = RTL_PURE_CALL_P (insn);
5947 RTL_LOOPING_CONST_OR_PURE_CALL_P (new_rtx)
5948 = RTL_LOOPING_CONST_OR_PURE_CALL_P (insn);
5949 break;
5951 default:
5952 gcc_unreachable ();
5955 /* Update LABEL_NUSES. */
5956 mark_jump_label (PATTERN (new_rtx), new_rtx, 0);
5958 INSN_LOCATION (new_rtx) = INSN_LOCATION (insn);
5960 /* If the old insn is frame related, then so is the new one. This is
5961 primarily needed for IA-64 unwind info which marks epilogue insns,
5962 which may be duplicated by the basic block reordering code. */
5963 RTX_FRAME_RELATED_P (new_rtx) = RTX_FRAME_RELATED_P (insn);
5965 /* Copy all REG_NOTES except REG_LABEL_OPERAND since mark_jump_label
5966 will make them. REG_LABEL_TARGETs are created there too, but are
5967 supposed to be sticky, so we copy them. */
5968 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
5969 if (REG_NOTE_KIND (link) != REG_LABEL_OPERAND)
5971 if (GET_CODE (link) == EXPR_LIST)
5972 add_reg_note (new_rtx, REG_NOTE_KIND (link),
5973 copy_insn_1 (XEXP (link, 0)));
5974 else
5975 add_shallow_copy_of_reg_note (new_rtx, link);
5978 INSN_CODE (new_rtx) = INSN_CODE (insn);
5979 return new_rtx;
5982 static GTY((deletable)) rtx hard_reg_clobbers [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
5984 gen_hard_reg_clobber (enum machine_mode mode, unsigned int regno)
5986 if (hard_reg_clobbers[mode][regno])
5987 return hard_reg_clobbers[mode][regno];
5988 else
5989 return (hard_reg_clobbers[mode][regno] =
5990 gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (mode, regno)));
5993 location_t prologue_location;
5994 location_t epilogue_location;
5996 /* Hold current location information and last location information, so the
5997 datastructures are built lazily only when some instructions in given
5998 place are needed. */
5999 static location_t curr_location;
6001 /* Allocate insn location datastructure. */
6002 void
6003 insn_locations_init (void)
6005 prologue_location = epilogue_location = 0;
6006 curr_location = UNKNOWN_LOCATION;
6009 /* At the end of emit stage, clear current location. */
6010 void
6011 insn_locations_finalize (void)
6013 epilogue_location = curr_location;
6014 curr_location = UNKNOWN_LOCATION;
6017 /* Set current location. */
6018 void
6019 set_curr_insn_location (location_t location)
6021 curr_location = location;
6024 /* Get current location. */
6025 location_t
6026 curr_insn_location (void)
6028 return curr_location;
6031 /* Return lexical scope block insn belongs to. */
6032 tree
6033 insn_scope (const_rtx insn)
6035 return LOCATION_BLOCK (INSN_LOCATION (insn));
6038 /* Return line number of the statement that produced this insn. */
6040 insn_line (const_rtx insn)
6042 return LOCATION_LINE (INSN_LOCATION (insn));
6045 /* Return source file of the statement that produced this insn. */
6046 const char *
6047 insn_file (const_rtx insn)
6049 return LOCATION_FILE (INSN_LOCATION (insn));
6052 /* Return true if memory model MODEL requires a pre-operation (release-style)
6053 barrier or a post-operation (acquire-style) barrier. While not universal,
6054 this function matches behavior of several targets. */
6056 bool
6057 need_atomic_barrier_p (enum memmodel model, bool pre)
6059 switch (model & MEMMODEL_MASK)
6061 case MEMMODEL_RELAXED:
6062 case MEMMODEL_CONSUME:
6063 return false;
6064 case MEMMODEL_RELEASE:
6065 return pre;
6066 case MEMMODEL_ACQUIRE:
6067 return !pre;
6068 case MEMMODEL_ACQ_REL:
6069 case MEMMODEL_SEQ_CST:
6070 return true;
6071 default:
6072 gcc_unreachable ();
6076 #include "gt-emit-rtl.h"