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