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