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