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