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