1/* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
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
8Software Foundation; either version 3, or (at your option) any later
9version.
10
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.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20/* This file handles the generation of rtl code from tree structure
21 at the level of the function as a whole.
22 It creates the rtl expressions for parameters and auto variables
23 and has full responsibility for allocating stack slots.
24
25 `expand_function_start' is called at the beginning of a function,
26 before the function body is parsed, and `expand_function_end' is
27 called after parsing the body.
28
29 Call `assign_stack_local' to allocate a stack slot for a local variable.
30 This is usually done during the RTL generation for the function body,
31 but it can also be done in the reload pass when a pseudo-register does
32 not get a hard register. */
33
34#include "config.h"
35#include "system.h"
36#include "coretypes.h"
37#include "backend.h"
38#include "target.h"
39#include "rtl.h"
40#include "tree.h"
41#include "gimple-expr.h"
42#include "cfghooks.h"
43#include "df.h"
44#include "memmodel.h"
45#include "tm_p.h"
46#include "stringpool.h"
47#include "expmed.h"
48#include "optabs.h"
49#include "opts.h"
50#include "regs.h"
51#include "emit-rtl.h"
52#include "recog.h"
53#include "rtl-error.h"
54#include "hard-reg-set.h"
55#include "alias.h"
56#include "fold-const.h"
57#include "stor-layout.h"
58#include "varasm.h"
59#include "except.h"
60#include "dojump.h"
61#include "explow.h"
62#include "calls.h"
63#include "expr.h"
64#include "optabs-tree.h"
65#include "output.h"
66#include "langhooks.h"
67#include "common/common-target.h"
68#include "gimplify.h"
69#include "tree-pass.h"
70#include "cfgrtl.h"
71#include "cfganal.h"
72#include "cfgbuild.h"
73#include "cfgcleanup.h"
74#include "cfgexpand.h"
75#include "shrink-wrap.h"
76#include "toplev.h"
77#include "rtl-iter.h"
78#include "tree-dfa.h"
79#include "tree-ssa.h"
80#include "stringpool.h"
81#include "attribs.h"
82#include "gimple.h"
83#include "options.h"
84#include "function-abi.h"
85#include "value-range.h"
86#include "gimple-range.h"
87#include "insn-attr.h"
88
89/* So we can assign to cfun in this file. */
90#undef cfun
91
92#ifndef STACK_ALIGNMENT_NEEDED
93#define STACK_ALIGNMENT_NEEDED 1
94#endif
95
96#define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
97
98/* Round a value to the lowest integer less than it that is a multiple of
99 the required alignment. Avoid using division in case the value is
100 negative. Assume the alignment is a power of two. */
101#define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
102
103/* Similar, but round to the next highest integer that meets the
104 alignment. */
105#define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
106
107/* Nonzero once virtual register instantiation has been done.
108 assign_stack_local uses frame_pointer_rtx when this is nonzero.
109 calls.cc:emit_library_call_value_1 uses it to set up
110 post-instantiation libcalls. */
111int virtuals_instantiated;
112
113/* Assign unique numbers to labels generated for profiling, debugging, etc. */
114static GTY(()) int funcdef_no;
115
116/* These variables hold pointers to functions to create and destroy
117 target specific, per-function data structures. */
118struct machine_function * (*init_machine_status) (void);
119
120/* The currently compiled function. */
121struct function *cfun = 0;
122
123/* These hashes record the prologue and epilogue insns. */
124
125struct insn_cache_hasher : ggc_cache_ptr_hash<rtx_def>
126{
127 static hashval_t hash (rtx x) { return htab_hash_pointer (x); }
128 static bool equal (rtx a, rtx b) { return a == b; }
129};
130
131static GTY((cache))
132 hash_table<insn_cache_hasher> *prologue_insn_hash;
133static GTY((cache))
134 hash_table<insn_cache_hasher> *epilogue_insn_hash;
135
136
137hash_table<used_type_hasher> *types_used_by_vars_hash = NULL;
138vec<tree, va_gc> *types_used_by_cur_var_decl;
139
140/* Forward declarations. */
141
142static class temp_slot *find_temp_slot_from_address (rtx);
143static void pad_to_arg_alignment (struct args_size *, int, struct args_size *);
144static void pad_below (struct args_size *, machine_mode, tree);
145static void reorder_blocks_1 (rtx_insn *, tree, vec<tree> *);
146static int all_blocks (tree, tree *);
147static tree *get_block_vector (tree, int *);
148extern tree debug_find_var_in_block_tree (tree, tree);
149/* We always define `record_insns' even if it's not used so that we
150 can always export `prologue_epilogue_contains'. */
151static void record_insns (rtx_insn *, rtx, hash_table<insn_cache_hasher> **)
152 ATTRIBUTE_UNUSED;
153static bool contains (const rtx_insn *, hash_table<insn_cache_hasher> *);
154static void prepare_function_start (void);
155static void do_clobber_return_reg (rtx, void *);
156static void do_use_return_reg (rtx, void *);
157
158
159/* Stack of nested functions. */
160/* Keep track of the cfun stack. */
161
162static vec<function *> function_context_stack;
163
164/* Save the current context for compilation of a nested function.
165 This is called from language-specific code. */
166
167void
168push_function_context (void)
169{
170 if (cfun == 0)
171 allocate_struct_function (NULL, false);
172
173 function_context_stack.safe_push (obj: cfun);
174 set_cfun (NULL);
175}
176
177/* Restore the last saved context, at the end of a nested function.
178 This function is called from language-specific code. */
179
180void
181pop_function_context (void)
182{
183 struct function *p = function_context_stack.pop ();
184 set_cfun (new_cfun: p);
185 current_function_decl = p->decl;
186
187 /* Reset variables that have known state during rtx generation. */
188 virtuals_instantiated = 0;
189 generating_concat_p = 1;
190}
191
192/* Clear out all parts of the state in F that can safely be discarded
193 after the function has been parsed, but not compiled, to let
194 garbage collection reclaim the memory. */
195
196void
197free_after_parsing (struct function *f)
198{
199 f->language = 0;
200}
201
202/* Clear out all parts of the state in F that can safely be discarded
203 after the function has been compiled, to let garbage collection
204 reclaim the memory. */
205
206void
207free_after_compilation (struct function *f)
208{
209 prologue_insn_hash = NULL;
210 epilogue_insn_hash = NULL;
211
212 free (crtl->emit.regno_pointer_align);
213
214 memset (crtl, c: 0, n: sizeof (struct rtl_data));
215 f->eh = NULL;
216 f->machine = NULL;
217 f->cfg = NULL;
218 f->curr_properties &= ~PROP_cfg;
219 delete f->cond_uids;
220
221 regno_reg_rtx = NULL;
222}
223
224/* Return size needed for stack frame based on slots so far allocated.
225 This size counts from zero. It is not rounded to PREFERRED_STACK_BOUNDARY;
226 the caller may have to do that. */
227
228poly_int64
229get_frame_size (void)
230{
231 if (FRAME_GROWS_DOWNWARD)
232 return -frame_offset;
233 else
234 return frame_offset;
235}
236
237/* Issue an error message and return TRUE if frame OFFSET overflows in
238 the signed target pointer arithmetics for function FUNC. Otherwise
239 return FALSE. */
240
241bool
242frame_offset_overflow (poly_int64 offset, tree func)
243{
244 poly_uint64 size = FRAME_GROWS_DOWNWARD ? -offset : offset;
245 unsigned HOST_WIDE_INT limit
246 = ((HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (Pmode) - 1))
247 /* Leave room for the fixed part of the frame. */
248 - 64 * UNITS_PER_WORD);
249
250 if (!coeffs_in_range_p (a: size, b: 0U, c: limit))
251 {
252 unsigned HOST_WIDE_INT hwisize;
253 if (size.is_constant (const_value: &hwisize))
254 error_at (DECL_SOURCE_LOCATION (func),
255 "total size of local objects %wu exceeds maximum %wu",
256 hwisize, limit);
257 else
258 error_at (DECL_SOURCE_LOCATION (func),
259 "total size of local objects exceeds maximum %wu",
260 limit);
261 return true;
262 }
263
264 return false;
265}
266
267/* Return the minimum spill slot alignment for a register of mode MODE. */
268
269unsigned int
270spill_slot_alignment (machine_mode mode ATTRIBUTE_UNUSED)
271{
272 return STACK_SLOT_ALIGNMENT (NULL_TREE, mode, GET_MODE_ALIGNMENT (mode));
273}
274
275/* Return stack slot alignment in bits for TYPE and MODE. */
276
277static unsigned int
278get_stack_local_alignment (tree type, machine_mode mode)
279{
280 unsigned int alignment;
281
282 if (mode == BLKmode)
283 alignment = BIGGEST_ALIGNMENT;
284 else
285 alignment = GET_MODE_ALIGNMENT (mode);
286
287 /* Allow the frond-end to (possibly) increase the alignment of this
288 stack slot. */
289 if (! type)
290 type = lang_hooks.types.type_for_mode (mode, 0);
291
292 return STACK_SLOT_ALIGNMENT (type, mode, alignment);
293}
294
295/* Determine whether it is possible to fit a stack slot of size SIZE and
296 alignment ALIGNMENT into an area in the stack frame that starts at
297 frame offset START and has a length of LENGTH. If so, store the frame
298 offset to be used for the stack slot in *POFFSET and return true;
299 return false otherwise. This function will extend the frame size when
300 given a start/length pair that lies at the end of the frame. */
301
302static bool
303try_fit_stack_local (poly_int64 start, poly_int64 length,
304 poly_int64 size, unsigned int alignment,
305 poly_int64 *poffset)
306{
307 poly_int64 this_frame_offset;
308 int frame_off, frame_alignment, frame_phase;
309
310 /* Calculate how many bytes the start of local variables is off from
311 stack alignment. */
312 frame_alignment = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
313 frame_off = targetm.starting_frame_offset () % frame_alignment;
314 frame_phase = frame_off ? frame_alignment - frame_off : 0;
315
316 /* Round the frame offset to the specified alignment. */
317
318 if (FRAME_GROWS_DOWNWARD)
319 this_frame_offset
320 = (aligned_lower_bound (value: start + length - size - frame_phase, align: alignment)
321 + frame_phase);
322 else
323 this_frame_offset
324 = aligned_upper_bound (value: start - frame_phase, align: alignment) + frame_phase;
325
326 /* See if it fits. If this space is at the edge of the frame,
327 consider extending the frame to make it fit. Our caller relies on
328 this when allocating a new slot. */
329 if (maybe_lt (a: this_frame_offset, b: start))
330 {
331 if (known_eq (frame_offset, start))
332 frame_offset = this_frame_offset;
333 else
334 return false;
335 }
336 else if (maybe_gt (this_frame_offset + size, start + length))
337 {
338 if (known_eq (frame_offset, start + length))
339 frame_offset = this_frame_offset + size;
340 else
341 return false;
342 }
343
344 *poffset = this_frame_offset;
345 return true;
346}
347
348/* Create a new frame_space structure describing free space in the stack
349 frame beginning at START and ending at END, and chain it into the
350 function's frame_space_list. */
351
352static void
353add_frame_space (poly_int64 start, poly_int64 end)
354{
355 class frame_space *space = ggc_alloc<frame_space> ();
356 space->next = crtl->frame_space_list;
357 crtl->frame_space_list = space;
358 space->start = start;
359 space->length = end - start;
360}
361
362/* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
363 with machine mode MODE.
364
365 ALIGN controls the amount of alignment for the address of the slot:
366 0 means according to MODE,
367 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
368 -2 means use BITS_PER_UNIT,
369 positive specifies alignment boundary in bits.
370
371 KIND has ASLK_REDUCE_ALIGN bit set if it is OK to reduce
372 alignment and ASLK_RECORD_PAD bit set if we should remember
373 extra space we allocated for alignment purposes. When we are
374 called from assign_stack_temp_for_type, it is not set so we don't
375 track the same stack slot in two independent lists.
376
377 We do not round to stack_boundary here. */
378
379rtx
380assign_stack_local_1 (machine_mode mode, poly_int64 size,
381 int align, int kind)
382{
383 rtx x, addr;
384 poly_int64 bigend_correction = 0;
385 poly_int64 slot_offset = 0, old_frame_offset;
386 unsigned int alignment, alignment_in_bits;
387
388 if (align == 0)
389 {
390 alignment = get_stack_local_alignment (NULL, mode);
391 alignment /= BITS_PER_UNIT;
392 }
393 else if (align == -1)
394 {
395 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
396 size = aligned_upper_bound (value: size, align: alignment);
397 }
398 else if (align == -2)
399 alignment = 1; /* BITS_PER_UNIT / BITS_PER_UNIT */
400 else
401 alignment = align / BITS_PER_UNIT;
402
403 alignment_in_bits = alignment * BITS_PER_UNIT;
404
405 /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
406 if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
407 {
408 alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
409 alignment = MAX_SUPPORTED_STACK_ALIGNMENT / BITS_PER_UNIT;
410 }
411
412 if (SUPPORTS_STACK_ALIGNMENT)
413 {
414 if (crtl->stack_alignment_estimated < alignment_in_bits)
415 {
416 if (!crtl->stack_realign_processed)
417 crtl->stack_alignment_estimated = alignment_in_bits;
418 else
419 {
420 /* If stack is realigned and stack alignment value
421 hasn't been finalized, it is OK not to increase
422 stack_alignment_estimated. The bigger alignment
423 requirement is recorded in stack_alignment_needed
424 below. */
425 gcc_assert (!crtl->stack_realign_finalized);
426 if (!crtl->stack_realign_needed)
427 {
428 /* It is OK to reduce the alignment as long as the
429 requested size is 0 or the estimated stack
430 alignment >= mode alignment. */
431 gcc_assert ((kind & ASLK_REDUCE_ALIGN)
432 || known_eq (size, 0)
433 || (crtl->stack_alignment_estimated
434 >= GET_MODE_ALIGNMENT (mode)));
435 alignment_in_bits = crtl->stack_alignment_estimated;
436 alignment = alignment_in_bits / BITS_PER_UNIT;
437 }
438 }
439 }
440 }
441
442 if (crtl->stack_alignment_needed < alignment_in_bits)
443 crtl->stack_alignment_needed = alignment_in_bits;
444 if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
445 crtl->max_used_stack_slot_alignment = alignment_in_bits;
446
447 if (mode != BLKmode || maybe_ne (a: size, b: 0))
448 {
449 if (kind & ASLK_RECORD_PAD)
450 {
451 class frame_space **psp;
452
453 for (psp = &crtl->frame_space_list; *psp; psp = &(*psp)->next)
454 {
455 class frame_space *space = *psp;
456 if (!try_fit_stack_local (start: space->start, length: space->length, size,
457 alignment, poffset: &slot_offset))
458 continue;
459 *psp = space->next;
460 if (known_gt (slot_offset, space->start))
461 add_frame_space (start: space->start, end: slot_offset);
462 if (known_lt (slot_offset + size, space->start + space->length))
463 add_frame_space (start: slot_offset + size,
464 end: space->start + space->length);
465 goto found_space;
466 }
467 }
468 }
469 else if (!STACK_ALIGNMENT_NEEDED)
470 {
471 slot_offset = frame_offset;
472 goto found_space;
473 }
474
475 old_frame_offset = frame_offset;
476
477 if (FRAME_GROWS_DOWNWARD)
478 {
479 frame_offset -= size;
480 try_fit_stack_local (frame_offset, length: size, size, alignment, poffset: &slot_offset);
481
482 if (kind & ASLK_RECORD_PAD)
483 {
484 if (known_gt (slot_offset, frame_offset))
485 add_frame_space (frame_offset, end: slot_offset);
486 if (known_lt (slot_offset + size, old_frame_offset))
487 add_frame_space (start: slot_offset + size, end: old_frame_offset);
488 }
489 }
490 else
491 {
492 frame_offset += size;
493 try_fit_stack_local (start: old_frame_offset, length: size, size, alignment, poffset: &slot_offset);
494
495 if (kind & ASLK_RECORD_PAD)
496 {
497 if (known_gt (slot_offset, old_frame_offset))
498 add_frame_space (start: old_frame_offset, end: slot_offset);
499 if (known_lt (slot_offset + size, frame_offset))
500 add_frame_space (start: slot_offset + size, frame_offset);
501 }
502 }
503
504 found_space:
505 /* On a big-endian machine, if we are allocating more space than we will use,
506 use the least significant bytes of those that are allocated. */
507 if (mode != BLKmode)
508 {
509 /* The slot size can sometimes be smaller than the mode size;
510 e.g. the rs6000 port allocates slots with a vector mode
511 that have the size of only one element. However, the slot
512 size must always be ordered wrt to the mode size, in the
513 same way as for a subreg. */
514 gcc_checking_assert (ordered_p (GET_MODE_SIZE (mode), size));
515 if (BYTES_BIG_ENDIAN && maybe_lt (a: GET_MODE_SIZE (mode), b: size))
516 bigend_correction = size - GET_MODE_SIZE (mode);
517 }
518
519 /* If we have already instantiated virtual registers, return the actual
520 address relative to the frame pointer. */
521 if (virtuals_instantiated)
522 addr = plus_constant (Pmode, frame_pointer_rtx,
523 trunc_int_for_mode
524 (slot_offset + bigend_correction
525 + targetm.starting_frame_offset (), Pmode));
526 else
527 addr = plus_constant (Pmode, virtual_stack_vars_rtx,
528 trunc_int_for_mode
529 (slot_offset + bigend_correction,
530 Pmode));
531
532 x = gen_rtx_MEM (mode, addr);
533 set_mem_align (x, alignment_in_bits);
534 MEM_NOTRAP_P (x) = 1;
535
536 vec_safe_push (stack_slot_list, obj: x);
537
538 if (frame_offset_overflow (frame_offset, func: current_function_decl))
539 frame_offset = 0;
540
541 return x;
542}
543
544/* Wrap up assign_stack_local_1 with last parameter as false. */
545
546rtx
547assign_stack_local (machine_mode mode, poly_int64 size, int align)
548{
549 return assign_stack_local_1 (mode, size, align, ASLK_RECORD_PAD);
550}
551
552/* In order to evaluate some expressions, such as function calls returning
553 structures in memory, we need to temporarily allocate stack locations.
554 We record each allocated temporary in the following structure.
555
556 Associated with each temporary slot is a nesting level. When we pop up
557 one level, all temporaries associated with the previous level are freed.
558 Normally, all temporaries are freed after the execution of the statement
559 in which they were created. However, if we are inside a ({...}) grouping,
560 the result may be in a temporary and hence must be preserved. If the
561 result could be in a temporary, we preserve it if we can determine which
562 one it is in. If we cannot determine which temporary may contain the
563 result, all temporaries are preserved. A temporary is preserved by
564 pretending it was allocated at the previous nesting level. */
565
566class GTY(()) temp_slot {
567public:
568 /* Points to next temporary slot. */
569 class temp_slot *next;
570 /* Points to previous temporary slot. */
571 class temp_slot *prev;
572 /* The rtx to used to reference the slot. */
573 rtx slot;
574 /* The size, in units, of the slot. */
575 poly_int64 size;
576 /* The type of the object in the slot, or zero if it doesn't correspond
577 to a type. We use this to determine whether a slot can be reused.
578 It can be reused if objects of the type of the new slot will always
579 conflict with objects of the type of the old slot. */
580 tree type;
581 /* The alignment (in bits) of the slot. */
582 unsigned int align;
583 /* True if this temporary is currently in use. */
584 bool in_use;
585 /* Nesting level at which this slot is being used. */
586 int level;
587 /* The offset of the slot from the frame_pointer, including extra space
588 for alignment. This info is for combine_temp_slots. */
589 poly_int64 base_offset;
590 /* The size of the slot, including extra space for alignment. This
591 info is for combine_temp_slots. */
592 poly_int64 full_size;
593};
594
595/* Entry for the below hash table. */
596struct GTY((for_user)) temp_slot_address_entry {
597 hashval_t hash;
598 rtx address;
599 class temp_slot *temp_slot;
600};
601
602struct temp_address_hasher : ggc_ptr_hash<temp_slot_address_entry>
603{
604 static hashval_t hash (temp_slot_address_entry *);
605 static bool equal (temp_slot_address_entry *, temp_slot_address_entry *);
606};
607
608/* A table of addresses that represent a stack slot. The table is a mapping
609 from address RTXen to a temp slot. */
610static GTY(()) hash_table<temp_address_hasher> *temp_slot_address_table;
611static size_t n_temp_slots_in_use;
612
613/* Removes temporary slot TEMP from LIST. */
614
615static void
616cut_slot_from_list (class temp_slot *temp, class temp_slot **list)
617{
618 if (temp->next)
619 temp->next->prev = temp->prev;
620 if (temp->prev)
621 temp->prev->next = temp->next;
622 else
623 *list = temp->next;
624
625 temp->prev = temp->next = NULL;
626}
627
628/* Inserts temporary slot TEMP to LIST. */
629
630static void
631insert_slot_to_list (class temp_slot *temp, class temp_slot **list)
632{
633 temp->next = *list;
634 if (*list)
635 (*list)->prev = temp;
636 temp->prev = NULL;
637 *list = temp;
638}
639
640/* Returns the list of used temp slots at LEVEL. */
641
642static class temp_slot **
643temp_slots_at_level (int level)
644{
645 if (level >= (int) vec_safe_length (used_temp_slots))
646 vec_safe_grow_cleared (used_temp_slots, len: level + 1, exact: true);
647
648 return &(*used_temp_slots)[level];
649}
650
651/* Returns the maximal temporary slot level. */
652
653static int
654max_slot_level (void)
655{
656 if (!used_temp_slots)
657 return -1;
658
659 return used_temp_slots->length () - 1;
660}
661
662/* Moves temporary slot TEMP to LEVEL. */
663
664static void
665move_slot_to_level (class temp_slot *temp, int level)
666{
667 cut_slot_from_list (temp, list: temp_slots_at_level (level: temp->level));
668 insert_slot_to_list (temp, list: temp_slots_at_level (level));
669 temp->level = level;
670}
671
672/* Make temporary slot TEMP available. */
673
674static void
675make_slot_available (class temp_slot *temp)
676{
677 cut_slot_from_list (temp, list: temp_slots_at_level (level: temp->level));
678 insert_slot_to_list (temp, list: &avail_temp_slots);
679 temp->in_use = false;
680 temp->level = -1;
681 n_temp_slots_in_use--;
682}
683
684/* Compute the hash value for an address -> temp slot mapping.
685 The value is cached on the mapping entry. */
686static hashval_t
687temp_slot_address_compute_hash (struct temp_slot_address_entry *t)
688{
689 int do_not_record = 0;
690 return hash_rtx (t->address, GET_MODE (t->address),
691 &do_not_record, NULL, false);
692}
693
694/* Return the hash value for an address -> temp slot mapping. */
695hashval_t
696temp_address_hasher::hash (temp_slot_address_entry *t)
697{
698 return t->hash;
699}
700
701/* Compare two address -> temp slot mapping entries. */
702bool
703temp_address_hasher::equal (temp_slot_address_entry *t1,
704 temp_slot_address_entry *t2)
705{
706 return exp_equiv_p (t1->address, t2->address, 0, true);
707}
708
709/* Add ADDRESS as an alias of TEMP_SLOT to the addess -> temp slot mapping. */
710static void
711insert_temp_slot_address (rtx address, class temp_slot *temp_slot)
712{
713 struct temp_slot_address_entry *t = ggc_alloc<temp_slot_address_entry> ();
714 t->address = copy_rtx (address);
715 t->temp_slot = temp_slot;
716 t->hash = temp_slot_address_compute_hash (t);
717 *temp_slot_address_table->find_slot_with_hash (comparable: t, hash: t->hash, insert: INSERT) = t;
718}
719
720/* Remove an address -> temp slot mapping entry if the temp slot is
721 not in use anymore. Callback for remove_unused_temp_slot_addresses. */
722int
723remove_unused_temp_slot_addresses_1 (temp_slot_address_entry **slot, void *)
724{
725 const struct temp_slot_address_entry *t = *slot;
726 if (! t->temp_slot->in_use)
727 temp_slot_address_table->clear_slot (slot);
728 return 1;
729}
730
731/* Remove all mappings of addresses to unused temp slots. */
732static void
733remove_unused_temp_slot_addresses (void)
734{
735 /* Use quicker clearing if there aren't any active temp slots. */
736 if (n_temp_slots_in_use)
737 temp_slot_address_table->traverse
738 <void *, remove_unused_temp_slot_addresses_1> (NULL);
739 else
740 temp_slot_address_table->empty ();
741}
742
743/* Find the temp slot corresponding to the object at address X. */
744
745static class temp_slot *
746find_temp_slot_from_address (rtx x)
747{
748 class temp_slot *p;
749 struct temp_slot_address_entry tmp, *t;
750
751 /* First try the easy way:
752 See if X exists in the address -> temp slot mapping. */
753 tmp.address = x;
754 tmp.temp_slot = NULL;
755 tmp.hash = temp_slot_address_compute_hash (t: &tmp);
756 t = temp_slot_address_table->find_with_hash (comparable: &tmp, hash: tmp.hash);
757 if (t)
758 return t->temp_slot;
759
760 /* If we have a sum involving a register, see if it points to a temp
761 slot. */
762 if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
763 && (p = find_temp_slot_from_address (XEXP (x, 0))) != 0)
764 return p;
765 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 1))
766 && (p = find_temp_slot_from_address (XEXP (x, 1))) != 0)
767 return p;
768
769 /* Last resort: Address is a virtual stack var address. */
770 poly_int64 offset;
771 if (strip_offset (x, &offset) == virtual_stack_vars_rtx)
772 {
773 int i;
774 for (i = max_slot_level (); i >= 0; i--)
775 for (p = *temp_slots_at_level (level: i); p; p = p->next)
776 if (known_in_range_p (val: offset, pos: p->base_offset, size: p->full_size))
777 return p;
778 }
779
780 return NULL;
781}
782
783/* Allocate a temporary stack slot and record it for possible later
784 reuse.
785
786 MODE is the machine mode to be given to the returned rtx.
787
788 SIZE is the size in units of the space required. We do no rounding here
789 since assign_stack_local will do any required rounding.
790
791 TYPE is the type that will be used for the stack slot. */
792
793rtx
794assign_stack_temp_for_type (machine_mode mode, poly_int64 size, tree type)
795{
796 unsigned int align;
797 class temp_slot *p, *best_p = 0, *selected = NULL, **pp;
798 rtx slot;
799
800 gcc_assert (known_size_p (size));
801
802 align = get_stack_local_alignment (type, mode);
803
804 /* Try to find an available, already-allocated temporary of the proper
805 mode which meets the size and alignment requirements. Choose the
806 smallest one with the closest alignment.
807
808 If assign_stack_temp is called outside of the tree->rtl expansion,
809 we cannot reuse the stack slots (that may still refer to
810 VIRTUAL_STACK_VARS_REGNUM). */
811 if (!virtuals_instantiated)
812 {
813 for (p = avail_temp_slots; p; p = p->next)
814 {
815 if (p->align >= align
816 && known_ge (p->size, size)
817 && GET_MODE (p->slot) == mode
818 && objects_must_conflict_p (p->type, type)
819 && (best_p == 0
820 || (known_eq (best_p->size, p->size)
821 ? best_p->align > p->align
822 : known_ge (best_p->size, p->size))))
823 {
824 if (p->align == align && known_eq (p->size, size))
825 {
826 selected = p;
827 cut_slot_from_list (temp: selected, list: &avail_temp_slots);
828 best_p = 0;
829 break;
830 }
831 best_p = p;
832 }
833 }
834 }
835
836 /* Make our best, if any, the one to use. */
837 if (best_p)
838 {
839 selected = best_p;
840 cut_slot_from_list (temp: selected, list: &avail_temp_slots);
841
842 /* If there are enough aligned bytes left over, make them into a new
843 temp_slot so that the extra bytes don't get wasted. Do this only
844 for BLKmode slots, so that we can be sure of the alignment. */
845 if (GET_MODE (best_p->slot) == BLKmode)
846 {
847 int alignment = best_p->align / BITS_PER_UNIT;
848 poly_int64 rounded_size = aligned_upper_bound (value: size, align: alignment);
849
850 if (known_ge (best_p->size - rounded_size, alignment))
851 {
852 p = ggc_alloc<temp_slot> ();
853 p->in_use = false;
854 p->size = best_p->size - rounded_size;
855 p->base_offset = best_p->base_offset + rounded_size;
856 p->full_size = best_p->full_size - rounded_size;
857 p->slot = adjust_address_nv (best_p->slot, BLKmode, rounded_size);
858 p->align = best_p->align;
859 p->type = best_p->type;
860 insert_slot_to_list (temp: p, list: &avail_temp_slots);
861
862 vec_safe_push (stack_slot_list, obj: p->slot);
863
864 best_p->size = rounded_size;
865 best_p->full_size = rounded_size;
866 }
867 }
868 }
869
870 /* If we still didn't find one, make a new temporary. */
871 if (selected == 0)
872 {
873 poly_int64 frame_offset_old = frame_offset;
874
875 p = ggc_alloc<temp_slot> ();
876
877 /* We are passing an explicit alignment request to assign_stack_local.
878 One side effect of that is assign_stack_local will not round SIZE
879 to ensure the frame offset remains suitably aligned.
880
881 So for requests which depended on the rounding of SIZE, we go ahead
882 and round it now. We also make sure ALIGNMENT is at least
883 BIGGEST_ALIGNMENT. */
884 gcc_assert (mode != BLKmode || align == BIGGEST_ALIGNMENT);
885 p->slot = assign_stack_local_1 (mode,
886 size: (mode == BLKmode
887 ? aligned_upper_bound (value: size,
888 align: (int) align
889 / BITS_PER_UNIT)
890 : size),
891 align, kind: 0);
892
893 p->align = align;
894
895 /* The following slot size computation is necessary because we don't
896 know the actual size of the temporary slot until assign_stack_local
897 has performed all the frame alignment and size rounding for the
898 requested temporary. Note that extra space added for alignment
899 can be either above or below this stack slot depending on which
900 way the frame grows. We include the extra space if and only if it
901 is above this slot. */
902 if (FRAME_GROWS_DOWNWARD)
903 p->size = frame_offset_old - frame_offset;
904 else
905 p->size = size;
906
907 /* Now define the fields used by combine_temp_slots. */
908 if (FRAME_GROWS_DOWNWARD)
909 {
910 p->base_offset = frame_offset;
911 p->full_size = frame_offset_old - frame_offset;
912 }
913 else
914 {
915 p->base_offset = frame_offset_old;
916 p->full_size = frame_offset - frame_offset_old;
917 }
918
919 selected = p;
920 }
921
922 p = selected;
923 p->in_use = true;
924 p->type = type;
925 p->level = temp_slot_level;
926 n_temp_slots_in_use++;
927
928 pp = temp_slots_at_level (level: p->level);
929 insert_slot_to_list (temp: p, list: pp);
930 insert_temp_slot_address (XEXP (p->slot, 0), temp_slot: p);
931
932 /* Create a new MEM rtx to avoid clobbering MEM flags of old slots. */
933 slot = gen_rtx_MEM (mode, XEXP (p->slot, 0));
934 vec_safe_push (stack_slot_list, obj: slot);
935
936 /* If we know the alias set for the memory that will be used, use
937 it. If there's no TYPE, then we don't know anything about the
938 alias set for the memory. */
939 set_mem_alias_set (slot, type ? get_alias_set (type) : 0);
940 set_mem_align (slot, align);
941
942 /* If a type is specified, set the relevant flags. */
943 if (type != 0)
944 MEM_VOLATILE_P (slot) = TYPE_VOLATILE (type);
945 MEM_NOTRAP_P (slot) = 1;
946
947 return slot;
948}
949
950/* Allocate a temporary stack slot and record it for possible later
951 reuse. First two arguments are same as in preceding function. */
952
953rtx
954assign_stack_temp (machine_mode mode, poly_int64 size)
955{
956 return assign_stack_temp_for_type (mode, size, NULL_TREE);
957}
958
959/* Assign a temporary.
960 If TYPE_OR_DECL is a decl, then we are doing it on behalf of the decl
961 and so that should be used in error messages. In either case, we
962 allocate of the given type.
963 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
964 it is 0 if a register is OK.
965 DONT_PROMOTE is 1 if we should not promote values in register
966 to wider modes. */
967
968rtx
969assign_temp (tree type_or_decl, int memory_required,
970 int dont_promote ATTRIBUTE_UNUSED)
971{
972 tree type, decl;
973 machine_mode mode;
974#ifdef PROMOTE_MODE
975 int unsignedp;
976#endif
977
978 if (DECL_P (type_or_decl))
979 decl = type_or_decl, type = TREE_TYPE (decl);
980 else
981 decl = NULL, type = type_or_decl;
982
983 mode = TYPE_MODE (type);
984#ifdef PROMOTE_MODE
985 unsignedp = TYPE_UNSIGNED (type);
986#endif
987
988 /* Allocating temporaries of TREE_ADDRESSABLE type must be done in the front
989 end. See also create_tmp_var for the gimplification-time check. */
990 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
991
992 if (mode == BLKmode || memory_required)
993 {
994 poly_int64 size;
995 rtx tmp;
996
997 /* Unfortunately, we don't yet know how to allocate variable-sized
998 temporaries. However, sometimes we can find a fixed upper limit on
999 the size, so try that instead. */
1000 if (!poly_int_tree_p (TYPE_SIZE_UNIT (type), value: &size))
1001 size = max_int_size_in_bytes (type);
1002
1003 /* Zero sized arrays are a GNU C extension. Set size to 1 to avoid
1004 problems with allocating the stack space. */
1005 if (known_eq (size, 0))
1006 size = 1;
1007
1008 /* The size of the temporary may be too large to fit into an integer. */
1009 /* ??? Not sure this should happen except for user silliness, so limit
1010 this to things that aren't compiler-generated temporaries. The
1011 rest of the time we'll die in assign_stack_temp_for_type. */
1012 if (decl
1013 && !known_size_p (a: size)
1014 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST)
1015 {
1016 error ("size of variable %q+D is too large", decl);
1017 size = 1;
1018 }
1019
1020 tmp = assign_stack_temp_for_type (mode, size, type);
1021 return tmp;
1022 }
1023
1024#ifdef PROMOTE_MODE
1025 if (! dont_promote)
1026 mode = promote_mode (type, mode, &unsignedp);
1027#endif
1028
1029 return gen_reg_rtx (mode);
1030}
1031
1032/* Combine temporary stack slots which are adjacent on the stack.
1033
1034 This allows for better use of already allocated stack space. This is only
1035 done for BLKmode slots because we can be sure that we won't have alignment
1036 problems in this case. */
1037
1038static void
1039combine_temp_slots (void)
1040{
1041 class temp_slot *p, *q, *next, *next_q;
1042 int num_slots;
1043
1044 /* We can't combine slots, because the information about which slot
1045 is in which alias set will be lost. */
1046 if (flag_strict_aliasing)
1047 return;
1048
1049 /* If there are a lot of temp slots, don't do anything unless
1050 high levels of optimization. */
1051 if (! flag_expensive_optimizations)
1052 for (p = avail_temp_slots, num_slots = 0; p; p = p->next, num_slots++)
1053 if (num_slots > 100 || (num_slots > 10 && optimize == 0))
1054 return;
1055
1056 for (p = avail_temp_slots; p; p = next)
1057 {
1058 int delete_p = 0;
1059
1060 next = p->next;
1061
1062 if (GET_MODE (p->slot) != BLKmode)
1063 continue;
1064
1065 for (q = p->next; q; q = next_q)
1066 {
1067 int delete_q = 0;
1068
1069 next_q = q->next;
1070
1071 if (GET_MODE (q->slot) != BLKmode)
1072 continue;
1073
1074 if (known_eq (p->base_offset + p->full_size, q->base_offset))
1075 {
1076 /* Q comes after P; combine Q into P. */
1077 p->size += q->size;
1078 p->full_size += q->full_size;
1079 delete_q = 1;
1080 }
1081 else if (known_eq (q->base_offset + q->full_size, p->base_offset))
1082 {
1083 /* P comes after Q; combine P into Q. */
1084 q->size += p->size;
1085 q->full_size += p->full_size;
1086 delete_p = 1;
1087 break;
1088 }
1089 if (delete_q)
1090 cut_slot_from_list (temp: q, list: &avail_temp_slots);
1091 }
1092
1093 /* Either delete P or advance past it. */
1094 if (delete_p)
1095 cut_slot_from_list (temp: p, list: &avail_temp_slots);
1096 }
1097}
1098
1099/* Indicate that NEW_RTX is an alternate way of referring to the temp
1100 slot that previously was known by OLD_RTX. */
1101
1102void
1103update_temp_slot_address (rtx old_rtx, rtx new_rtx)
1104{
1105 class temp_slot *p;
1106
1107 if (rtx_equal_p (old_rtx, new_rtx))
1108 return;
1109
1110 p = find_temp_slot_from_address (x: old_rtx);
1111
1112 /* If we didn't find one, see if both OLD_RTX is a PLUS. If so, and
1113 NEW_RTX is a register, see if one operand of the PLUS is a
1114 temporary location. If so, NEW_RTX points into it. Otherwise,
1115 if both OLD_RTX and NEW_RTX are a PLUS and if there is a register
1116 in common between them. If so, try a recursive call on those
1117 values. */
1118 if (p == 0)
1119 {
1120 if (GET_CODE (old_rtx) != PLUS)
1121 return;
1122
1123 if (REG_P (new_rtx))
1124 {
1125 update_temp_slot_address (XEXP (old_rtx, 0), new_rtx);
1126 update_temp_slot_address (XEXP (old_rtx, 1), new_rtx);
1127 return;
1128 }
1129 else if (GET_CODE (new_rtx) != PLUS)
1130 return;
1131
1132 if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 0)))
1133 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 1));
1134 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 0)))
1135 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 1));
1136 else if (rtx_equal_p (XEXP (old_rtx, 0), XEXP (new_rtx, 1)))
1137 update_temp_slot_address (XEXP (old_rtx, 1), XEXP (new_rtx, 0));
1138 else if (rtx_equal_p (XEXP (old_rtx, 1), XEXP (new_rtx, 1)))
1139 update_temp_slot_address (XEXP (old_rtx, 0), XEXP (new_rtx, 0));
1140
1141 return;
1142 }
1143
1144 /* Otherwise add an alias for the temp's address. */
1145 insert_temp_slot_address (address: new_rtx, temp_slot: p);
1146}
1147
1148/* If X could be a reference to a temporary slot, mark that slot as
1149 belonging to the to one level higher than the current level. If X
1150 matched one of our slots, just mark that one. Otherwise, we can't
1151 easily predict which it is, so upgrade all of them.
1152
1153 This is called when an ({...}) construct occurs and a statement
1154 returns a value in memory. */
1155
1156void
1157preserve_temp_slots (rtx x)
1158{
1159 class temp_slot *p = 0, *next;
1160
1161 if (x == 0)
1162 return;
1163
1164 /* If X is a register that is being used as a pointer, see if we have
1165 a temporary slot we know it points to. */
1166 if (REG_P (x) && REG_POINTER (x))
1167 p = find_temp_slot_from_address (x);
1168
1169 /* If X is not in memory or is at a constant address, it cannot be in
1170 a temporary slot. */
1171 if (p == 0 && (!MEM_P (x) || CONSTANT_P (XEXP (x, 0))))
1172 return;
1173
1174 /* First see if we can find a match. */
1175 if (p == 0)
1176 p = find_temp_slot_from_address (XEXP (x, 0));
1177
1178 if (p != 0)
1179 {
1180 if (p->level == temp_slot_level)
1181 move_slot_to_level (temp: p, temp_slot_level - 1);
1182 return;
1183 }
1184
1185 /* Otherwise, preserve all non-kept slots at this level. */
1186 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1187 {
1188 next = p->next;
1189 move_slot_to_level (temp: p, temp_slot_level - 1);
1190 }
1191}
1192
1193/* Free all temporaries used so far. This is normally called at the
1194 end of generating code for a statement. */
1195
1196void
1197free_temp_slots (void)
1198{
1199 class temp_slot *p, *next;
1200 bool some_available = false;
1201
1202 for (p = *temp_slots_at_level (temp_slot_level); p; p = next)
1203 {
1204 next = p->next;
1205 make_slot_available (temp: p);
1206 some_available = true;
1207 }
1208
1209 if (some_available)
1210 {
1211 remove_unused_temp_slot_addresses ();
1212 combine_temp_slots ();
1213 }
1214}
1215
1216/* Push deeper into the nesting level for stack temporaries. */
1217
1218void
1219push_temp_slots (void)
1220{
1221 temp_slot_level++;
1222}
1223
1224/* Pop a temporary nesting level. All slots in use in the current level
1225 are freed. */
1226
1227void
1228pop_temp_slots (void)
1229{
1230 free_temp_slots ();
1231 temp_slot_level--;
1232}
1233
1234/* Initialize temporary slots. */
1235
1236void
1237init_temp_slots (void)
1238{
1239 /* We have not allocated any temporaries yet. */
1240 avail_temp_slots = 0;
1241 vec_alloc (used_temp_slots, nelems: 0);
1242 temp_slot_level = 0;
1243 n_temp_slots_in_use = 0;
1244
1245 /* Set up the table to map addresses to temp slots. */
1246 if (! temp_slot_address_table)
1247 temp_slot_address_table = hash_table<temp_address_hasher>::create_ggc (n: 32);
1248 else
1249 temp_slot_address_table->empty ();
1250}
1251
1252/* Functions and data structures to keep track of the values hard regs
1253 had at the start of the function. */
1254
1255/* Private type used by get_hard_reg_initial_reg, get_hard_reg_initial_val,
1256 and has_hard_reg_initial_val.. */
1257struct GTY(()) initial_value_pair {
1258 rtx hard_reg;
1259 rtx pseudo;
1260};
1261/* ??? This could be a VEC but there is currently no way to define an
1262 opaque VEC type. This could be worked around by defining struct
1263 initial_value_pair in function.h. */
1264struct GTY(()) initial_value_struct {
1265 int num_entries;
1266 int max_entries;
1267 initial_value_pair * GTY ((length ("%h.num_entries"))) entries;
1268};
1269
1270/* If a pseudo represents an initial hard reg (or expression), return
1271 it, else return NULL_RTX. */
1272
1273rtx
1274get_hard_reg_initial_reg (rtx reg)
1275{
1276 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1277 int i;
1278
1279 if (ivs == 0)
1280 return NULL_RTX;
1281
1282 for (i = 0; i < ivs->num_entries; i++)
1283 if (rtx_equal_p (ivs->entries[i].pseudo, reg))
1284 return ivs->entries[i].hard_reg;
1285
1286 return NULL_RTX;
1287}
1288
1289/* Make sure that there's a pseudo register of mode MODE that stores the
1290 initial value of hard register REGNO. Return an rtx for such a pseudo. */
1291
1292rtx
1293get_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1294{
1295 struct initial_value_struct *ivs;
1296 rtx rv;
1297
1298 rv = has_hard_reg_initial_val (mode, regno);
1299 if (rv)
1300 return rv;
1301
1302 ivs = crtl->hard_reg_initial_vals;
1303 if (ivs == 0)
1304 {
1305 ivs = ggc_alloc<initial_value_struct> ();
1306 ivs->num_entries = 0;
1307 ivs->max_entries = 5;
1308 ivs->entries = ggc_vec_alloc<initial_value_pair> (c: 5);
1309 crtl->hard_reg_initial_vals = ivs;
1310 }
1311
1312 if (ivs->num_entries >= ivs->max_entries)
1313 {
1314 ivs->max_entries += 5;
1315 ivs->entries = GGC_RESIZEVEC (initial_value_pair, ivs->entries,
1316 ivs->max_entries);
1317 }
1318
1319 ivs->entries[ivs->num_entries].hard_reg = gen_rtx_REG (mode, regno);
1320 ivs->entries[ivs->num_entries].pseudo = gen_reg_rtx (mode);
1321
1322 return ivs->entries[ivs->num_entries++].pseudo;
1323}
1324
1325/* See if get_hard_reg_initial_val has been used to create a pseudo
1326 for the initial value of hard register REGNO in mode MODE. Return
1327 the associated pseudo if so, otherwise return NULL. */
1328
1329rtx
1330has_hard_reg_initial_val (machine_mode mode, unsigned int regno)
1331{
1332 struct initial_value_struct *ivs;
1333 int i;
1334
1335 ivs = crtl->hard_reg_initial_vals;
1336 if (ivs != 0)
1337 for (i = 0; i < ivs->num_entries; i++)
1338 if (GET_MODE (ivs->entries[i].hard_reg) == mode
1339 && REGNO (ivs->entries[i].hard_reg) == regno)
1340 return ivs->entries[i].pseudo;
1341
1342 return NULL_RTX;
1343}
1344
1345void
1346emit_initial_value_sets (void)
1347{
1348 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1349 int i;
1350 rtx_insn *seq;
1351
1352 if (ivs == 0)
1353 return;
1354
1355 start_sequence ();
1356 for (i = 0; i < ivs->num_entries; i++)
1357 emit_move_insn (ivs->entries[i].pseudo, ivs->entries[i].hard_reg);
1358 seq = end_sequence ();
1359
1360 emit_insn_at_entry (seq);
1361}
1362
1363/* Return the hardreg-pseudoreg initial values pair entry I and
1364 TRUE if I is a valid entry, or FALSE if I is not a valid entry. */
1365bool
1366initial_value_entry (int i, rtx *hreg, rtx *preg)
1367{
1368 struct initial_value_struct *ivs = crtl->hard_reg_initial_vals;
1369 if (!ivs || i >= ivs->num_entries)
1370 return false;
1371
1372 *hreg = ivs->entries[i].hard_reg;
1373 *preg = ivs->entries[i].pseudo;
1374 return true;
1375}
1376
1377/* These routines are responsible for converting virtual register references
1378 to the actual hard register references once RTL generation is complete.
1379
1380 The following four variables are used for communication between the
1381 routines. They contain the offsets of the virtual registers from their
1382 respective hard registers. */
1383
1384static poly_int64 in_arg_offset;
1385static poly_int64 var_offset;
1386static poly_int64 dynamic_offset;
1387static poly_int64 out_arg_offset;
1388static poly_int64 cfa_offset;
1389
1390/* In most machines, the stack pointer register is equivalent to the bottom
1391 of the stack. */
1392
1393#ifndef STACK_POINTER_OFFSET
1394#define STACK_POINTER_OFFSET 0
1395#endif
1396
1397#if defined (REG_PARM_STACK_SPACE) && !defined (INCOMING_REG_PARM_STACK_SPACE)
1398#define INCOMING_REG_PARM_STACK_SPACE REG_PARM_STACK_SPACE
1399#endif
1400
1401/* If not defined, pick an appropriate default for the offset of dynamically
1402 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
1403 INCOMING_REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
1404
1405#ifndef STACK_DYNAMIC_OFFSET
1406
1407/* The bottom of the stack points to the actual arguments. If
1408 REG_PARM_STACK_SPACE is defined, this includes the space for the register
1409 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
1410 stack space for register parameters is not pushed by the caller, but
1411 rather part of the fixed stack areas and hence not included in
1412 `crtl->outgoing_args_size'. Nevertheless, we must allow
1413 for it when allocating stack dynamic objects. */
1414
1415#ifdef INCOMING_REG_PARM_STACK_SPACE
1416#define STACK_DYNAMIC_OFFSET(FNDECL) \
1417((ACCUMULATE_OUTGOING_ARGS \
1418 ? (crtl->outgoing_args_size \
1419 + (OUTGOING_REG_PARM_STACK_SPACE ((!(FNDECL) ? NULL_TREE : TREE_TYPE (FNDECL))) ? 0 \
1420 : INCOMING_REG_PARM_STACK_SPACE (FNDECL))) \
1421 : 0) + (STACK_POINTER_OFFSET))
1422#else
1423#define STACK_DYNAMIC_OFFSET(FNDECL) \
1424 ((ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : poly_int64 (0)) \
1425 + (STACK_POINTER_OFFSET))
1426#endif
1427#endif
1428
1429
1430/* Given a piece of RTX and a pointer to a HOST_WIDE_INT, if the RTX
1431 is a virtual register, return the equivalent hard register and set the
1432 offset indirectly through the pointer. Otherwise, return 0. */
1433
1434static rtx
1435instantiate_new_reg (rtx x, poly_int64 *poffset)
1436{
1437 rtx new_rtx;
1438 poly_int64 offset;
1439
1440 if (x == virtual_incoming_args_rtx)
1441 {
1442 if (stack_realign_drap)
1443 {
1444 /* Replace virtual_incoming_args_rtx with internal arg
1445 pointer if DRAP is used to realign stack. */
1446 new_rtx = crtl->args.internal_arg_pointer;
1447 offset = 0;
1448 }
1449 else
1450 new_rtx = arg_pointer_rtx, offset = in_arg_offset;
1451 }
1452 else if (x == virtual_stack_vars_rtx)
1453 new_rtx = frame_pointer_rtx, offset = var_offset;
1454 else if (x == virtual_stack_dynamic_rtx)
1455 new_rtx = stack_pointer_rtx, offset = dynamic_offset;
1456 else if (x == virtual_outgoing_args_rtx)
1457 new_rtx = stack_pointer_rtx, offset = out_arg_offset;
1458 else if (x == virtual_cfa_rtx)
1459 {
1460#ifdef FRAME_POINTER_CFA_OFFSET
1461 new_rtx = frame_pointer_rtx;
1462#else
1463 new_rtx = arg_pointer_rtx;
1464#endif
1465 offset = cfa_offset;
1466 }
1467 else if (x == virtual_preferred_stack_boundary_rtx)
1468 {
1469 new_rtx = GEN_INT (crtl->preferred_stack_boundary / BITS_PER_UNIT);
1470 offset = 0;
1471 }
1472 else
1473 return NULL_RTX;
1474
1475 *poffset = offset;
1476 return new_rtx;
1477}
1478
1479/* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1480 registers present inside of *LOC. The expression is simplified,
1481 as much as possible, but is not to be considered "valid" in any sense
1482 implied by the target. Return true if any change is made. */
1483
1484static bool
1485instantiate_virtual_regs_in_rtx (rtx *loc)
1486{
1487 if (!*loc)
1488 return false;
1489 bool changed = false;
1490 subrtx_ptr_iterator::array_type array;
1491 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
1492 {
1493 rtx *loc = *iter;
1494 if (rtx x = *loc)
1495 {
1496 rtx new_rtx;
1497 poly_int64 offset;
1498 switch (GET_CODE (x))
1499 {
1500 case REG:
1501 new_rtx = instantiate_new_reg (x, poffset: &offset);
1502 if (new_rtx)
1503 {
1504 *loc = plus_constant (GET_MODE (x), new_rtx, offset);
1505 changed = true;
1506 }
1507 iter.skip_subrtxes ();
1508 break;
1509
1510 case PLUS:
1511 new_rtx = instantiate_new_reg (XEXP (x, 0), poffset: &offset);
1512 if (new_rtx)
1513 {
1514 XEXP (x, 0) = new_rtx;
1515 *loc = plus_constant (GET_MODE (x), x, offset, true);
1516 changed = true;
1517 iter.skip_subrtxes ();
1518 break;
1519 }
1520
1521 /* FIXME -- from old code */
1522 /* If we have (plus (subreg (virtual-reg)) (const_int)), we know
1523 we can commute the PLUS and SUBREG because pointers into the
1524 frame are well-behaved. */
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 }
1531 }
1532 return changed;
1533}
1534
1535/* A subroutine of instantiate_virtual_regs_in_insn. Return true if X
1536 matches the predicate for insn CODE operand OPERAND. */
1537
1538static bool
1539safe_insn_predicate (int code, int operand, rtx x)
1540{
1541 return code < 0 || insn_operand_matches (icode: (enum insn_code) code, opno: operand, operand: x);
1542}
1543
1544/* A subroutine of instantiate_virtual_regs. Instantiate any virtual
1545 registers present inside of insn. The result will be a valid insn. */
1546
1547static void
1548instantiate_virtual_regs_in_insn (rtx_insn *insn)
1549{
1550 poly_int64 offset;
1551 int insn_code, i;
1552 bool any_change = false;
1553 rtx set, new_rtx, x;
1554 rtx_insn *seq;
1555
1556 /* There are some special cases to be handled first. */
1557 set = single_set (insn);
1558 if (set)
1559 {
1560 /* We're allowed to assign to a virtual register. This is interpreted
1561 to mean that the underlying register gets assigned the inverse
1562 transformation. This is used, for example, in the handling of
1563 non-local gotos. */
1564 new_rtx = instantiate_new_reg (SET_DEST (set), poffset: &offset);
1565 if (new_rtx)
1566 {
1567 start_sequence ();
1568
1569 instantiate_virtual_regs_in_rtx (loc: &SET_SRC (set));
1570 x = simplify_gen_binary (code: PLUS, GET_MODE (new_rtx), SET_SRC (set),
1571 op1: gen_int_mode (-offset, GET_MODE (new_rtx)));
1572 x = force_operand (x, new_rtx);
1573 if (x != new_rtx)
1574 emit_move_insn (new_rtx, x);
1575
1576 seq = end_sequence ();
1577
1578 emit_insn_before (seq, insn);
1579 delete_insn (insn);
1580 return;
1581 }
1582
1583 /* Handle a straight copy from a virtual register by generating a
1584 new add insn. The difference between this and falling through
1585 to the generic case is avoiding a new pseudo and eliminating a
1586 move insn in the initial rtl stream. */
1587 new_rtx = instantiate_new_reg (SET_SRC (set), poffset: &offset);
1588 if (new_rtx
1589 && maybe_ne (a: offset, b: 0)
1590 && REG_P (SET_DEST (set))
1591 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1592 {
1593 start_sequence ();
1594
1595 x = expand_simple_binop (GET_MODE (SET_DEST (set)), PLUS, new_rtx,
1596 gen_int_mode (offset,
1597 GET_MODE (SET_DEST (set))),
1598 SET_DEST (set), 1, OPTAB_LIB_WIDEN);
1599 if (x != SET_DEST (set))
1600 emit_move_insn (SET_DEST (set), x);
1601
1602 seq = end_sequence ();
1603
1604 emit_insn_before (seq, insn);
1605 delete_insn (insn);
1606 return;
1607 }
1608
1609 extract_insn (insn);
1610 insn_code = INSN_CODE (insn);
1611
1612 /* Handle a plus involving a virtual register by determining if the
1613 operands remain valid if they're modified in place. */
1614 poly_int64 delta;
1615 if (GET_CODE (SET_SRC (set)) == PLUS
1616 && recog_data.n_operands >= 3
1617 && recog_data.operand_loc[1] == &XEXP (SET_SRC (set), 0)
1618 && recog_data.operand_loc[2] == &XEXP (SET_SRC (set), 1)
1619 && poly_int_rtx_p (x: recog_data.operand[2], res: &delta)
1620 && (new_rtx = instantiate_new_reg (x: recog_data.operand[1], poffset: &offset)))
1621 {
1622 offset += delta;
1623
1624 /* If the sum is zero, then replace with a plain move. */
1625 if (known_eq (offset, 0)
1626 && REG_P (SET_DEST (set))
1627 && REGNO (SET_DEST (set)) > LAST_VIRTUAL_REGISTER)
1628 {
1629 start_sequence ();
1630 emit_move_insn (SET_DEST (set), new_rtx);
1631 seq = end_sequence ();
1632
1633 emit_insn_before (seq, insn);
1634 delete_insn (insn);
1635 return;
1636 }
1637
1638 x = gen_int_mode (offset, recog_data.operand_mode[2]);
1639
1640 /* Using validate_change and apply_change_group here leaves
1641 recog_data in an invalid state. Since we know exactly what
1642 we want to check, do those two by hand. */
1643 if (safe_insn_predicate (code: insn_code, operand: 1, x: new_rtx)
1644 && safe_insn_predicate (code: insn_code, operand: 2, x))
1645 {
1646 *recog_data.operand_loc[1] = recog_data.operand[1] = new_rtx;
1647 *recog_data.operand_loc[2] = recog_data.operand[2] = x;
1648 any_change = true;
1649
1650 /* Fall through into the regular operand fixup loop in
1651 order to take care of operands other than 1 and 2. */
1652 }
1653 }
1654 }
1655 else
1656 {
1657 extract_insn (insn);
1658 insn_code = INSN_CODE (insn);
1659 }
1660
1661 /* In the general case, we expect virtual registers to appear only in
1662 operands, and then only as either bare registers or inside memories. */
1663 for (i = 0; i < recog_data.n_operands; ++i)
1664 {
1665 x = recog_data.operand[i];
1666 switch (GET_CODE (x))
1667 {
1668 case MEM:
1669 {
1670 rtx addr = XEXP (x, 0);
1671
1672 if (!instantiate_virtual_regs_in_rtx (loc: &addr))
1673 continue;
1674
1675 start_sequence ();
1676 x = replace_equiv_address (x, addr, true);
1677 /* It may happen that the address with the virtual reg
1678 was valid (e.g. based on the virtual stack reg, which might
1679 be acceptable to the predicates with all offsets), whereas
1680 the address now isn't anymore, for instance when the address
1681 is still offsetted, but the base reg isn't virtual-stack-reg
1682 anymore. Below we would do a force_reg on the whole operand,
1683 but this insn might actually only accept memory. Hence,
1684 before doing that last resort, try to reload the address into
1685 a register, so this operand stays a MEM. */
1686 if (!safe_insn_predicate (code: insn_code, operand: i, x))
1687 {
1688 addr = force_reg (GET_MODE (addr), addr);
1689 x = replace_equiv_address (x, addr, true);
1690 }
1691 seq = end_sequence ();
1692 if (seq)
1693 emit_insn_before (seq, insn);
1694 }
1695 break;
1696
1697 case REG:
1698 new_rtx = instantiate_new_reg (x, poffset: &offset);
1699 if (new_rtx == NULL)
1700 continue;
1701 if (known_eq (offset, 0))
1702 x = new_rtx;
1703 else
1704 {
1705 start_sequence ();
1706
1707 /* Careful, special mode predicates may have stuff in
1708 insn_data[insn_code].operand[i].mode that isn't useful
1709 to us for computing a new value. */
1710 /* ??? Recognize address_operand and/or "p" constraints
1711 to see if (plus new offset) is a valid before we put
1712 this through expand_simple_binop. */
1713 x = expand_simple_binop (GET_MODE (x), PLUS, new_rtx,
1714 gen_int_mode (offset, GET_MODE (x)),
1715 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1716 seq = end_sequence ();
1717 emit_insn_before (seq, insn);
1718 }
1719 break;
1720
1721 case SUBREG:
1722 new_rtx = instantiate_new_reg (SUBREG_REG (x), poffset: &offset);
1723 if (new_rtx == NULL)
1724 continue;
1725 if (maybe_ne (a: offset, b: 0))
1726 {
1727 start_sequence ();
1728 new_rtx = expand_simple_binop
1729 (GET_MODE (new_rtx), PLUS, new_rtx,
1730 gen_int_mode (offset, GET_MODE (new_rtx)),
1731 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1732 seq = end_sequence ();
1733 emit_insn_before (seq, insn);
1734 }
1735 x = simplify_gen_subreg (outermode: recog_data.operand_mode[i], op: new_rtx,
1736 GET_MODE (new_rtx), SUBREG_BYTE (x));
1737 gcc_assert (x);
1738 break;
1739
1740 default:
1741 continue;
1742 }
1743
1744 /* At this point, X contains the new value for the operand.
1745 Validate the new value vs the insn predicate. Note that
1746 asm insns will have insn_code -1 here. */
1747 if (!safe_insn_predicate (code: insn_code, operand: i, x))
1748 {
1749 start_sequence ();
1750 if (REG_P (x))
1751 {
1752 gcc_assert (REGNO (x) <= LAST_VIRTUAL_REGISTER);
1753 x = copy_to_reg (x);
1754 }
1755 else
1756 x = force_reg (insn_data[insn_code].operand[i].mode, x);
1757 seq = end_sequence ();
1758 if (seq)
1759 emit_insn_before (seq, insn);
1760 }
1761
1762 *recog_data.operand_loc[i] = recog_data.operand[i] = x;
1763 any_change = true;
1764 }
1765
1766 if (any_change)
1767 {
1768 /* Propagate operand changes into the duplicates. */
1769 for (i = 0; i < recog_data.n_dups; ++i)
1770 *recog_data.dup_loc[i]
1771 = copy_rtx (recog_data.operand[(unsigned)recog_data.dup_num[i]]);
1772
1773 /* Force re-recognition of the instruction for validation. */
1774 INSN_CODE (insn) = -1;
1775 }
1776
1777 if (asm_noperands (PATTERN (insn)) >= 0)
1778 {
1779 if (!check_asm_operands (PATTERN (insn)))
1780 {
1781 error_for_asm (insn, "impossible constraint in %<asm%>");
1782 /* For asm goto, instead of fixing up all the edges
1783 just clear the template and clear input and output operands
1784 and strip away clobbers. */
1785 if (JUMP_P (insn))
1786 {
1787 rtx asm_op = extract_asm_operands (PATTERN (insn));
1788 PATTERN (insn) = asm_op;
1789 PUT_MODE (x: asm_op, VOIDmode);
1790 ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
1791 ASM_OPERANDS_OUTPUT_CONSTRAINT (asm_op) = "";
1792 ASM_OPERANDS_OUTPUT_IDX (asm_op) = 0;
1793 ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
1794 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
1795 }
1796 else
1797 delete_insn (insn);
1798 }
1799 }
1800 else
1801 {
1802 if (recog_memoized (insn) < 0)
1803 fatal_insn_not_found (insn);
1804 }
1805}
1806
1807/* Subroutine of instantiate_decls. Given RTL representing a decl,
1808 do any instantiation required. */
1809
1810void
1811instantiate_decl_rtl (rtx x)
1812{
1813 rtx addr;
1814
1815 if (x == 0)
1816 return;
1817
1818 /* If this is a CONCAT, recurse for the pieces. */
1819 if (GET_CODE (x) == CONCAT)
1820 {
1821 instantiate_decl_rtl (XEXP (x, 0));
1822 instantiate_decl_rtl (XEXP (x, 1));
1823 return;
1824 }
1825
1826 /* If this is not a MEM, no need to do anything. Similarly if the
1827 address is a constant or a register that is not a virtual register. */
1828 if (!MEM_P (x))
1829 return;
1830
1831 addr = XEXP (x, 0);
1832 if (CONSTANT_P (addr)
1833 || (REG_P (addr)
1834 && !VIRTUAL_REGISTER_P (addr)))
1835 return;
1836
1837 instantiate_virtual_regs_in_rtx (loc: &XEXP (x, 0));
1838}
1839
1840/* Helper for instantiate_decls called via walk_tree: Process all decls
1841 in the given DECL_VALUE_EXPR. */
1842
1843static tree
1844instantiate_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1845{
1846 tree t = *tp;
1847 if (! EXPR_P (t))
1848 {
1849 *walk_subtrees = 0;
1850 if (DECL_P (t))
1851 {
1852 if (DECL_RTL_SET_P (t))
1853 instantiate_decl_rtl (DECL_RTL (t));
1854 if (TREE_CODE (t) == PARM_DECL && DECL_NAMELESS (t)
1855 && DECL_INCOMING_RTL (t))
1856 instantiate_decl_rtl (DECL_INCOMING_RTL (t));
1857 if ((VAR_P (t) || TREE_CODE (t) == RESULT_DECL)
1858 && DECL_HAS_VALUE_EXPR_P (t))
1859 {
1860 tree v = DECL_VALUE_EXPR (t);
1861 walk_tree (&v, instantiate_expr, NULL, NULL);
1862 }
1863 }
1864 }
1865 return NULL;
1866}
1867
1868/* Subroutine of instantiate_decls: Process all decls in the given
1869 BLOCK node and all its subblocks. */
1870
1871static void
1872instantiate_decls_1 (tree let)
1873{
1874 tree t;
1875
1876 for (t = BLOCK_VARS (let); t; t = DECL_CHAIN (t))
1877 {
1878 if (DECL_RTL_SET_P (t))
1879 instantiate_decl_rtl (DECL_RTL (t));
1880 if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
1881 {
1882 tree v = DECL_VALUE_EXPR (t);
1883 walk_tree (&v, instantiate_expr, NULL, NULL);
1884 }
1885 }
1886
1887 /* Process all subblocks. */
1888 for (t = BLOCK_SUBBLOCKS (let); t; t = BLOCK_CHAIN (t))
1889 instantiate_decls_1 (let: t);
1890}
1891
1892/* Scan all decls in FNDECL (both variables and parameters) and instantiate
1893 all virtual registers in their DECL_RTL's. */
1894
1895static void
1896instantiate_decls (tree fndecl)
1897{
1898 tree decl;
1899 unsigned ix;
1900
1901 /* Process all parameters of the function. */
1902 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = DECL_CHAIN (decl))
1903 {
1904 instantiate_decl_rtl (DECL_RTL (decl));
1905 instantiate_decl_rtl (DECL_INCOMING_RTL (decl));
1906 if (DECL_HAS_VALUE_EXPR_P (decl))
1907 {
1908 tree v = DECL_VALUE_EXPR (decl);
1909 walk_tree (&v, instantiate_expr, NULL, NULL);
1910 }
1911 }
1912
1913 if ((decl = DECL_RESULT (fndecl))
1914 && TREE_CODE (decl) == RESULT_DECL)
1915 {
1916 if (DECL_RTL_SET_P (decl))
1917 instantiate_decl_rtl (DECL_RTL (decl));
1918 if (DECL_HAS_VALUE_EXPR_P (decl))
1919 {
1920 tree v = DECL_VALUE_EXPR (decl);
1921 walk_tree (&v, instantiate_expr, NULL, NULL);
1922 }
1923 }
1924
1925 /* Process the saved static chain if it exists. */
1926 decl = DECL_STRUCT_FUNCTION (fndecl)->static_chain_decl;
1927 if (decl && DECL_HAS_VALUE_EXPR_P (decl))
1928 instantiate_decl_rtl (DECL_RTL (DECL_VALUE_EXPR (decl)));
1929
1930 /* Now process all variables defined in the function or its subblocks. */
1931 if (DECL_INITIAL (fndecl))
1932 instantiate_decls_1 (DECL_INITIAL (fndecl));
1933
1934 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
1935 if (DECL_RTL_SET_P (decl))
1936 instantiate_decl_rtl (DECL_RTL (decl));
1937 vec_free (v&: cfun->local_decls);
1938}
1939
1940/* Return the value of STACK_DYNAMIC_OFFSET for the current function.
1941 This is done through a function wrapper so that the macro sees a
1942 predictable set of included files. */
1943
1944poly_int64
1945get_stack_dynamic_offset ()
1946{
1947 return STACK_DYNAMIC_OFFSET (current_function_decl);
1948}
1949
1950/* Pass through the INSNS of function FNDECL and convert virtual register
1951 references to hard register references. */
1952
1953static void
1954instantiate_virtual_regs (void)
1955{
1956 rtx_insn *insn;
1957
1958 /* Compute the offsets to use for this function. */
1959 in_arg_offset = FIRST_PARM_OFFSET (current_function_decl);
1960 var_offset = targetm.starting_frame_offset ();
1961 dynamic_offset = get_stack_dynamic_offset ();
1962 out_arg_offset = STACK_POINTER_OFFSET;
1963#ifdef FRAME_POINTER_CFA_OFFSET
1964 cfa_offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
1965#else
1966 cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
1967#endif
1968
1969 /* Initialize recognition, indicating that volatile is OK. */
1970 init_recog ();
1971
1972 /* Scan through all the insns, instantiating every virtual register still
1973 present. */
1974 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1975 if (INSN_P (insn))
1976 {
1977 /* These patterns in the instruction stream can never be recognized.
1978 Fortunately, they shouldn't contain virtual registers either. */
1979 if (GET_CODE (PATTERN (insn)) == USE
1980 || GET_CODE (PATTERN (insn)) == CLOBBER
1981 || GET_CODE (PATTERN (insn)) == ASM_INPUT
1982 || DEBUG_MARKER_INSN_P (insn))
1983 continue;
1984 else if (DEBUG_BIND_INSN_P (insn))
1985 instantiate_virtual_regs_in_rtx (INSN_VAR_LOCATION_PTR (insn));
1986 else
1987 instantiate_virtual_regs_in_insn (insn);
1988
1989 if (insn->deleted ())
1990 continue;
1991
1992 instantiate_virtual_regs_in_rtx (loc: &REG_NOTES (insn));
1993
1994 /* Instantiate any virtual registers in CALL_INSN_FUNCTION_USAGE. */
1995 if (CALL_P (insn))
1996 instantiate_virtual_regs_in_rtx (loc: &CALL_INSN_FUNCTION_USAGE (insn));
1997 }
1998
1999 /* Instantiate the virtual registers in the DECLs for debugging purposes. */
2000 instantiate_decls (fndecl: current_function_decl);
2001
2002 targetm.instantiate_decls ();
2003
2004 /* Indicate that, from now on, assign_stack_local should use
2005 frame_pointer_rtx. */
2006 virtuals_instantiated = 1;
2007}
2008
2009namespace {
2010
2011const pass_data pass_data_instantiate_virtual_regs =
2012{
2013 .type: RTL_PASS, /* type */
2014 .name: "vregs", /* name */
2015 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
2016 .tv_id: TV_NONE, /* tv_id */
2017 .properties_required: 0, /* properties_required */
2018 .properties_provided: 0, /* properties_provided */
2019 .properties_destroyed: 0, /* properties_destroyed */
2020 .todo_flags_start: 0, /* todo_flags_start */
2021 .todo_flags_finish: 0, /* todo_flags_finish */
2022};
2023
2024class pass_instantiate_virtual_regs : public rtl_opt_pass
2025{
2026public:
2027 pass_instantiate_virtual_regs (gcc::context *ctxt)
2028 : rtl_opt_pass (pass_data_instantiate_virtual_regs, ctxt)
2029 {}
2030
2031 /* opt_pass methods: */
2032 unsigned int execute (function *) final override
2033 {
2034 instantiate_virtual_regs ();
2035 return 0;
2036 }
2037
2038}; // class pass_instantiate_virtual_regs
2039
2040} // anon namespace
2041
2042rtl_opt_pass *
2043make_pass_instantiate_virtual_regs (gcc::context *ctxt)
2044{
2045 return new pass_instantiate_virtual_regs (ctxt);
2046}
2047
2048
2049/* Return true if EXP is an aggregate type (or a value with aggregate type).
2050 This means a type for which function calls must pass an address to the
2051 function or get an address back from the function.
2052 EXP may be a type node or an expression (whose type is tested). */
2053
2054bool
2055aggregate_value_p (const_tree exp, const_tree fntype)
2056{
2057 const_tree type = (TYPE_P (exp)) ? exp : TREE_TYPE (exp);
2058 int i, regno, nregs;
2059 rtx reg;
2060
2061 if (fntype)
2062 switch (TREE_CODE (fntype))
2063 {
2064 case CALL_EXPR:
2065 {
2066 tree fndecl = get_callee_fndecl (fntype);
2067 if (fndecl)
2068 fntype = TREE_TYPE (fndecl);
2069 else if (CALL_EXPR_FN (fntype))
2070 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (fntype)));
2071 else
2072 /* For internal functions, assume nothing needs to be
2073 returned in memory. */
2074 return false;
2075 }
2076 break;
2077 case FUNCTION_DECL:
2078 fntype = TREE_TYPE (fntype);
2079 break;
2080 case FUNCTION_TYPE:
2081 case METHOD_TYPE:
2082 break;
2083 case IDENTIFIER_NODE:
2084 fntype = NULL_TREE;
2085 break;
2086 default:
2087 /* We don't expect other tree types here. */
2088 gcc_unreachable ();
2089 }
2090
2091 if (VOID_TYPE_P (type))
2092 return false;
2093
2094 if (error_operand_p (t: fntype))
2095 return false;
2096
2097 /* If a record should be passed the same as its first (and only) member
2098 don't pass it as an aggregate. */
2099 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2100 return aggregate_value_p (exp: first_field (type), fntype);
2101
2102 /* If the front end has decided that this needs to be passed by
2103 reference, do so. */
2104 if ((TREE_CODE (exp) == PARM_DECL || TREE_CODE (exp) == RESULT_DECL)
2105 && DECL_BY_REFERENCE (exp))
2106 return true;
2107
2108 /* Function types that are TREE_ADDRESSABLE force return in memory. */
2109 if (fntype && TREE_ADDRESSABLE (fntype))
2110 return true;
2111
2112 /* Types that are TREE_ADDRESSABLE must be constructed in memory,
2113 and thus can't be returned in registers. */
2114 if (TREE_ADDRESSABLE (type))
2115 return true;
2116
2117 if (TYPE_EMPTY_P (type))
2118 return false;
2119
2120 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
2121 return true;
2122
2123 if (targetm.calls.return_in_memory (type, fntype))
2124 return true;
2125
2126 /* Make sure we have suitable call-clobbered regs to return
2127 the value in; if not, we must return it in memory. */
2128 reg = hard_function_value (type, 0, fntype, 0);
2129
2130 /* If we have something other than a REG (e.g. a PARALLEL), then assume
2131 it is OK. */
2132 if (!REG_P (reg))
2133 return false;
2134
2135 /* Use the default ABI if the type of the function isn't known.
2136 The scheme for handling interoperability between different ABIs
2137 requires us to be able to tell when we're calling a function with
2138 a nondefault ABI. */
2139 const predefined_function_abi &abi = (fntype
2140 ? fntype_abi (fntype)
2141 : default_function_abi);
2142 regno = REGNO (reg);
2143 nregs = hard_regno_nregs (regno, TYPE_MODE (type));
2144 for (i = 0; i < nregs; i++)
2145 if (!fixed_regs[regno + i] && !abi.clobbers_full_reg_p (regno: regno + i))
2146 return true;
2147
2148 return false;
2149}
2150
2151/* Return true if we should assign DECL a pseudo register; false if it
2152 should live on the local stack. */
2153
2154bool
2155use_register_for_decl (const_tree decl)
2156{
2157 if (TREE_CODE (decl) == SSA_NAME)
2158 {
2159 /* We often try to use the SSA_NAME, instead of its underlying
2160 decl, to get type information and guide decisions, to avoid
2161 differences of behavior between anonymous and named
2162 variables, but in this one case we have to go for the actual
2163 variable if there is one. The main reason is that, at least
2164 at -O0, we want to place user variables on the stack, but we
2165 don't mind using pseudos for anonymous or ignored temps.
2166 Should we take the SSA_NAME, we'd conclude all SSA_NAMEs
2167 should go in pseudos, whereas their corresponding variables
2168 might have to go on the stack. So, disregarding the decl
2169 here would negatively impact debug info at -O0, enable
2170 coalescing between SSA_NAMEs that ought to get different
2171 stack/pseudo assignments, and get the incoming argument
2172 processing thoroughly confused by PARM_DECLs expected to live
2173 in stack slots but assigned to pseudos. */
2174 if (!SSA_NAME_VAR (decl))
2175 return TYPE_MODE (TREE_TYPE (decl)) != BLKmode
2176 && !(flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)));
2177
2178 decl = SSA_NAME_VAR (decl);
2179 }
2180
2181 /* Honor volatile. */
2182 if (TREE_SIDE_EFFECTS (decl))
2183 return false;
2184
2185 /* Honor addressability. */
2186 if (TREE_ADDRESSABLE (decl))
2187 return false;
2188
2189 /* RESULT_DECLs are a bit special in that they're assigned without
2190 regard to use_register_for_decl, but we generally only store in
2191 them. If we coalesce their SSA NAMEs, we'd better return a
2192 result that matches the assignment in expand_function_start. */
2193 if (TREE_CODE (decl) == RESULT_DECL)
2194 {
2195 /* If it's not an aggregate, we're going to use a REG or a
2196 PARALLEL containing a REG. */
2197 if (!aggregate_value_p (exp: decl, fntype: current_function_decl))
2198 return true;
2199
2200 /* If expand_function_start determines the return value, we'll
2201 use MEM if it's not by reference. */
2202 if (cfun->returns_pcc_struct
2203 || (targetm.calls.struct_value_rtx
2204 (TREE_TYPE (current_function_decl), 1)))
2205 return DECL_BY_REFERENCE (decl);
2206
2207 /* Otherwise, we're taking an extra all.function_result_decl
2208 argument. It's set up in assign_parms_augmented_arg_list,
2209 under the (negated) conditions above, and then it's used to
2210 set up the RESULT_DECL rtl in assign_params, after looping
2211 over all parameters. Now, if the RESULT_DECL is not by
2212 reference, we'll use a MEM either way. */
2213 if (!DECL_BY_REFERENCE (decl))
2214 return false;
2215
2216 /* Otherwise, if RESULT_DECL is DECL_BY_REFERENCE, it will take
2217 the function_result_decl's assignment. Since it's a pointer,
2218 we can short-circuit a number of the tests below, and we must
2219 duplicate them because we don't have the function_result_decl
2220 to test. */
2221 if (!targetm.calls.allocate_stack_slots_for_args ())
2222 return true;
2223 /* We don't set DECL_IGNORED_P for the function_result_decl. */
2224 if (optimize)
2225 return true;
2226 /* Needed for [[musttail]] which can operate even at -O0 */
2227 if (cfun->tail_call_marked)
2228 return true;
2229 /* We don't set DECL_REGISTER for the function_result_decl. */
2230 return false;
2231 }
2232
2233 /* Only register-like things go in registers. */
2234 if (DECL_MODE (decl) == BLKmode)
2235 return false;
2236
2237 /* If -ffloat-store specified, don't put explicit float variables
2238 into registers. */
2239 /* ??? This should be checked after DECL_ARTIFICIAL, but tree-ssa
2240 propagates values across these stores, and it probably shouldn't. */
2241 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (decl)))
2242 return false;
2243
2244 if (!targetm.calls.allocate_stack_slots_for_args ())
2245 return true;
2246
2247 /* If we're not interested in tracking debugging information for
2248 this decl, then we can certainly put it in a register. */
2249 if (DECL_IGNORED_P (decl))
2250 return true;
2251
2252 if (optimize)
2253 return true;
2254
2255 /* Thunks force a tail call even at -O0 so we need to avoid creating a
2256 dangling reference in case the parameter is passed by reference. */
2257 if (TREE_CODE (decl) == PARM_DECL && cfun->tail_call_marked)
2258 return true;
2259
2260 if (!DECL_REGISTER (decl))
2261 return false;
2262
2263 /* When not optimizing, disregard register keyword for types that
2264 could have methods, otherwise the methods won't be callable from
2265 the debugger. */
2266 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
2267 return false;
2268
2269 return true;
2270}
2271
2272/* Structures to communicate between the subroutines of assign_parms.
2273 The first holds data persistent across all parameters, the second
2274 is cleared out for each parameter. */
2275
2276struct assign_parm_data_all
2277{
2278 /* When INIT_CUMULATIVE_ARGS gets revamped, allocating CUMULATIVE_ARGS
2279 should become a job of the target or otherwise encapsulated. */
2280 CUMULATIVE_ARGS args_so_far_v;
2281 cumulative_args_t args_so_far;
2282 struct args_size stack_args_size;
2283 tree function_result_decl;
2284 tree orig_fnargs;
2285 rtx_insn *first_conversion_insn;
2286 rtx_insn *last_conversion_insn;
2287 HOST_WIDE_INT pretend_args_size;
2288 HOST_WIDE_INT extra_pretend_bytes;
2289 int reg_parm_stack_space;
2290};
2291
2292struct assign_parm_data_one
2293{
2294 tree nominal_type;
2295 function_arg_info arg;
2296 rtx entry_parm;
2297 rtx stack_parm;
2298 machine_mode nominal_mode;
2299 machine_mode passed_mode;
2300 struct locate_and_pad_arg_data locate;
2301 int partial;
2302};
2303
2304/* A subroutine of assign_parms. Initialize ALL. */
2305
2306static void
2307assign_parms_initialize_all (struct assign_parm_data_all *all)
2308{
2309 tree fntype ATTRIBUTE_UNUSED;
2310
2311 memset (s: all, c: 0, n: sizeof (*all));
2312
2313 fntype = TREE_TYPE (current_function_decl);
2314
2315#ifdef INIT_CUMULATIVE_INCOMING_ARGS
2316 INIT_CUMULATIVE_INCOMING_ARGS (all->args_so_far_v, fntype, NULL_RTX);
2317#else
2318 INIT_CUMULATIVE_ARGS (all->args_so_far_v, fntype, NULL_RTX,
2319 current_function_decl, -1);
2320#endif
2321 all->args_so_far = pack_cumulative_args (arg: &all->args_so_far_v);
2322
2323#ifdef INCOMING_REG_PARM_STACK_SPACE
2324 all->reg_parm_stack_space
2325 = INCOMING_REG_PARM_STACK_SPACE (current_function_decl);
2326#endif
2327}
2328
2329/* If ARGS contains entries with complex types, split the entry into two
2330 entries of the component type. Return a new list of substitutions are
2331 needed, else the old list. */
2332
2333static void
2334split_complex_args (vec<tree> *args)
2335{
2336 unsigned i;
2337 tree p;
2338
2339 FOR_EACH_VEC_ELT (*args, i, p)
2340 {
2341 tree type = TREE_TYPE (p);
2342 if (TREE_CODE (type) == COMPLEX_TYPE
2343 && targetm.calls.split_complex_arg (type))
2344 {
2345 tree decl;
2346 tree subtype = TREE_TYPE (type);
2347 bool addressable = TREE_ADDRESSABLE (p);
2348
2349 /* Rewrite the PARM_DECL's type with its component. */
2350 p = copy_node (p);
2351 TREE_TYPE (p) = subtype;
2352 DECL_ARG_TYPE (p) = TREE_TYPE (DECL_ARG_TYPE (p));
2353 SET_DECL_MODE (p, VOIDmode);
2354 DECL_SIZE (p) = NULL;
2355 DECL_SIZE_UNIT (p) = NULL;
2356 /* If this arg must go in memory, put it in a pseudo here.
2357 We can't allow it to go in memory as per normal parms,
2358 because the usual place might not have the imag part
2359 adjacent to the real part. */
2360 DECL_ARTIFICIAL (p) = addressable;
2361 DECL_IGNORED_P (p) = addressable;
2362 TREE_ADDRESSABLE (p) = 0;
2363 layout_decl (p, 0);
2364 (*args)[i] = p;
2365
2366 /* Build a second synthetic decl. */
2367 decl = build_decl (EXPR_LOCATION (p),
2368 PARM_DECL, NULL_TREE, subtype);
2369 DECL_ARG_TYPE (decl) = DECL_ARG_TYPE (p);
2370 DECL_ARTIFICIAL (decl) = addressable;
2371 DECL_IGNORED_P (decl) = addressable;
2372 layout_decl (decl, 0);
2373 args->safe_insert (ix: ++i, obj: decl);
2374 }
2375 }
2376}
2377
2378/* A subroutine of assign_parms. Adjust the parameter list to incorporate
2379 the hidden struct return argument, and (abi willing) complex args.
2380 Return the new parameter list. */
2381
2382static vec<tree>
2383assign_parms_augmented_arg_list (struct assign_parm_data_all *all)
2384{
2385 tree fndecl = current_function_decl;
2386 tree fntype = TREE_TYPE (fndecl);
2387 vec<tree> fnargs = vNULL;
2388 tree arg;
2389
2390 for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
2391 fnargs.safe_push (obj: arg);
2392
2393 all->orig_fnargs = DECL_ARGUMENTS (fndecl);
2394
2395 /* If struct value address is treated as the first argument, make it so. */
2396 if (aggregate_value_p (DECL_RESULT (fndecl), fntype: fndecl)
2397 && ! cfun->returns_pcc_struct
2398 && targetm.calls.struct_value_rtx (TREE_TYPE (fndecl), 1) == 0)
2399 {
2400 tree type = build_pointer_type (TREE_TYPE (fntype));
2401 tree decl;
2402
2403 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
2404 PARM_DECL, get_identifier (".result_ptr"), type);
2405 DECL_ARG_TYPE (decl) = type;
2406 DECL_ARTIFICIAL (decl) = 1;
2407 DECL_NAMELESS (decl) = 1;
2408 TREE_CONSTANT (decl) = 1;
2409 /* We don't set DECL_IGNORED_P or DECL_REGISTER here. If this
2410 changes, the end of the RESULT_DECL handling block in
2411 use_register_for_decl must be adjusted to match. */
2412
2413 DECL_CHAIN (decl) = all->orig_fnargs;
2414 all->orig_fnargs = decl;
2415 fnargs.safe_insert (ix: 0, obj: decl);
2416
2417 all->function_result_decl = decl;
2418 }
2419
2420 /* If the target wants to split complex arguments into scalars, do so. */
2421 if (targetm.calls.split_complex_arg)
2422 split_complex_args (args: &fnargs);
2423
2424 return fnargs;
2425}
2426
2427/* A subroutine of assign_parms. Examine PARM and pull out type and mode
2428 data for the parameter. Incorporate ABI specifics such as pass-by-
2429 reference and type promotion. */
2430
2431static void
2432assign_parm_find_data_types (struct assign_parm_data_all *all, tree parm,
2433 struct assign_parm_data_one *data)
2434{
2435 int unsignedp;
2436
2437 *data = assign_parm_data_one ();
2438
2439 /* NAMED_ARG is a misnomer. We really mean 'non-variadic'. */
2440 if (!cfun->stdarg)
2441 data->arg.named = 1; /* No variadic parms. */
2442 else if (DECL_CHAIN (parm))
2443 data->arg.named = 1; /* Not the last non-variadic parm. */
2444 else if (targetm.calls.strict_argument_naming (all->args_so_far))
2445 data->arg.named = 1; /* Only variadic ones are unnamed. */
2446 else
2447 data->arg.named = 0; /* Treat as variadic. */
2448
2449 data->nominal_type = TREE_TYPE (parm);
2450 data->arg.type = DECL_ARG_TYPE (parm);
2451
2452 /* Look out for errors propagating this far. Also, if the parameter's
2453 type is void then its value doesn't matter. */
2454 if (TREE_TYPE (parm) == error_mark_node
2455 /* This can happen after weird syntax errors
2456 or if an enum type is defined among the parms. */
2457 || TREE_CODE (parm) != PARM_DECL
2458 || data->arg.type == NULL
2459 || VOID_TYPE_P (data->nominal_type))
2460 {
2461 data->nominal_type = data->arg.type = void_type_node;
2462 data->nominal_mode = data->passed_mode = data->arg.mode = VOIDmode;
2463 return;
2464 }
2465
2466 /* Find mode of arg as it is passed, and mode of arg as it should be
2467 during execution of this function. */
2468 data->passed_mode = data->arg.mode = TYPE_MODE (data->arg.type);
2469 data->nominal_mode = TYPE_MODE (data->nominal_type);
2470
2471 /* If the parm is to be passed as a transparent union or record, use the
2472 type of the first field for the tests below. We have already verified
2473 that the modes are the same. */
2474 if (RECORD_OR_UNION_TYPE_P (data->arg.type)
2475 && TYPE_TRANSPARENT_AGGR (data->arg.type))
2476 data->arg.type = TREE_TYPE (first_field (data->arg.type));
2477
2478 /* See if this arg was passed by invisible reference. */
2479 if (apply_pass_by_reference_rules (&all->args_so_far_v, data->arg))
2480 {
2481 data->nominal_type = data->arg.type;
2482 data->passed_mode = data->nominal_mode = data->arg.mode;
2483 }
2484
2485 /* Find mode as it is passed by the ABI. */
2486 unsignedp = TYPE_UNSIGNED (data->arg.type);
2487 data->arg.mode
2488 = promote_function_mode (data->arg.type, data->arg.mode, &unsignedp,
2489 TREE_TYPE (current_function_decl), 0);
2490}
2491
2492/* A subroutine of assign_parms. Invoke setup_incoming_varargs. */
2493
2494static void
2495assign_parms_setup_varargs (struct assign_parm_data_all *all,
2496 struct assign_parm_data_one *data, bool no_rtl)
2497{
2498 int varargs_pretend_bytes = 0;
2499
2500 function_arg_info last_named_arg = data->arg;
2501 last_named_arg.named = true;
2502 targetm.calls.setup_incoming_varargs (all->args_so_far, last_named_arg,
2503 &varargs_pretend_bytes, no_rtl);
2504
2505 /* If the back-end has requested extra stack space, record how much is
2506 needed. Do not change pretend_args_size otherwise since it may be
2507 nonzero from an earlier partial argument. */
2508 if (varargs_pretend_bytes > 0)
2509 all->pretend_args_size = varargs_pretend_bytes;
2510}
2511
2512/* A subroutine of assign_parms. Set DATA->ENTRY_PARM corresponding to
2513 the incoming location of the current parameter. */
2514
2515static void
2516assign_parm_find_entry_rtl (struct assign_parm_data_all *all,
2517 struct assign_parm_data_one *data)
2518{
2519 HOST_WIDE_INT pretend_bytes = 0;
2520 rtx entry_parm;
2521 bool in_regs;
2522
2523 if (data->arg.mode == VOIDmode)
2524 {
2525 data->entry_parm = data->stack_parm = const0_rtx;
2526 return;
2527 }
2528
2529 targetm.calls.warn_parameter_passing_abi (all->args_so_far,
2530 data->arg.type);
2531
2532 entry_parm = targetm.calls.function_incoming_arg (all->args_so_far,
2533 data->arg);
2534 if (entry_parm == 0)
2535 data->arg.mode = data->passed_mode;
2536
2537 /* Determine parm's home in the stack, in case it arrives in the stack
2538 or we should pretend it did. Compute the stack position and rtx where
2539 the argument arrives and its size.
2540
2541 There is one complexity here: If this was a parameter that would
2542 have been passed in registers, but wasn't only because it is
2543 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
2544 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
2545 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of 0
2546 as it was the previous time. */
2547 in_regs = (entry_parm != 0);
2548#ifdef STACK_PARMS_IN_REG_PARM_AREA
2549 in_regs = true;
2550#endif
2551 if (!in_regs && !data->arg.named)
2552 {
2553 if (targetm.calls.pretend_outgoing_varargs_named (all->args_so_far))
2554 {
2555 rtx tem;
2556 function_arg_info named_arg = data->arg;
2557 named_arg.named = true;
2558 tem = targetm.calls.function_incoming_arg (all->args_so_far,
2559 named_arg);
2560 in_regs = tem != NULL;
2561 }
2562 }
2563
2564 /* If this parameter was passed both in registers and in the stack, use
2565 the copy on the stack. */
2566 if (targetm.calls.must_pass_in_stack (data->arg))
2567 entry_parm = 0;
2568
2569 if (entry_parm)
2570 {
2571 int partial;
2572
2573 partial = targetm.calls.arg_partial_bytes (all->args_so_far, data->arg);
2574 data->partial = partial;
2575
2576 /* The caller might already have allocated stack space for the
2577 register parameters. */
2578 if (partial != 0 && all->reg_parm_stack_space == 0)
2579 {
2580 /* Part of this argument is passed in registers and part
2581 is passed on the stack. Ask the prologue code to extend
2582 the stack part so that we can recreate the full value.
2583
2584 PRETEND_BYTES is the size of the registers we need to store.
2585 CURRENT_FUNCTION_PRETEND_ARGS_SIZE is the amount of extra
2586 stack space that the prologue should allocate.
2587
2588 Internally, gcc assumes that the argument pointer is aligned
2589 to STACK_BOUNDARY bits. This is used both for alignment
2590 optimizations (see init_emit) and to locate arguments that are
2591 aligned to more than PARM_BOUNDARY bits. We must preserve this
2592 invariant by rounding CURRENT_FUNCTION_PRETEND_ARGS_SIZE up to
2593 a stack boundary. */
2594
2595 /* We assume at most one partial arg, and it must be the first
2596 argument on the stack. */
2597 gcc_assert (!all->extra_pretend_bytes && !all->pretend_args_size);
2598
2599 pretend_bytes = partial;
2600 all->pretend_args_size = CEIL_ROUND (pretend_bytes, STACK_BYTES);
2601
2602 /* We want to align relative to the actual stack pointer, so
2603 don't include this in the stack size until later. */
2604 all->extra_pretend_bytes = all->pretend_args_size;
2605 }
2606 }
2607
2608 locate_and_pad_parm (data->arg.mode, data->arg.type, in_regs,
2609 all->reg_parm_stack_space,
2610 entry_parm ? data->partial : 0, current_function_decl,
2611 &all->stack_args_size, &data->locate);
2612
2613 /* Update parm_stack_boundary if this parameter is passed in the
2614 stack. */
2615 if (!in_regs && crtl->parm_stack_boundary < data->locate.boundary)
2616 crtl->parm_stack_boundary = data->locate.boundary;
2617
2618 /* Adjust offsets to include the pretend args. */
2619 pretend_bytes = all->extra_pretend_bytes - pretend_bytes;
2620 data->locate.slot_offset.constant += pretend_bytes;
2621 data->locate.offset.constant += pretend_bytes;
2622
2623 data->entry_parm = entry_parm;
2624}
2625
2626/* A subroutine of assign_parms. If there is actually space on the stack
2627 for this parm, count it in stack_args_size and return true. */
2628
2629static bool
2630assign_parm_is_stack_parm (struct assign_parm_data_all *all,
2631 struct assign_parm_data_one *data)
2632{
2633 /* Trivially true if we've no incoming register. */
2634 if (data->entry_parm == NULL)
2635 ;
2636 /* Also true if we're partially in registers and partially not,
2637 since we've arranged to drop the entire argument on the stack. */
2638 else if (data->partial != 0)
2639 ;
2640 /* Also true if the target says that it's passed in both registers
2641 and on the stack. */
2642 else if (GET_CODE (data->entry_parm) == PARALLEL
2643 && XEXP (XVECEXP (data->entry_parm, 0, 0), 0) == NULL_RTX)
2644 ;
2645 /* Also true if the target says that there's stack allocated for
2646 all register parameters. */
2647 else if (all->reg_parm_stack_space > 0)
2648 ;
2649 /* Otherwise, no, this parameter has no ABI defined stack slot. */
2650 else
2651 return false;
2652
2653 all->stack_args_size.constant += data->locate.size.constant;
2654 if (data->locate.size.var)
2655 ADD_PARM_SIZE (all->stack_args_size, data->locate.size.var);
2656
2657 return true;
2658}
2659
2660/* A subroutine of assign_parms. Given that this parameter is allocated
2661 stack space by the ABI, find it. */
2662
2663static void
2664assign_parm_find_stack_rtl (tree parm, struct assign_parm_data_one *data)
2665{
2666 rtx offset_rtx, stack_parm;
2667 unsigned int align, boundary;
2668
2669 /* If we're passing this arg using a reg, make its stack home the
2670 aligned stack slot. */
2671 if (data->entry_parm)
2672 offset_rtx = ARGS_SIZE_RTX (data->locate.slot_offset);
2673 else
2674 offset_rtx = ARGS_SIZE_RTX (data->locate.offset);
2675
2676 stack_parm = crtl->args.internal_arg_pointer;
2677 if (offset_rtx != const0_rtx)
2678 stack_parm = gen_rtx_PLUS (Pmode, stack_parm, offset_rtx);
2679 stack_parm = gen_rtx_MEM (data->arg.mode, stack_parm);
2680
2681 if (!data->arg.pass_by_reference)
2682 {
2683 set_mem_attributes (stack_parm, parm, 1);
2684 /* set_mem_attributes could set MEM_SIZE to the passed mode's size,
2685 while promoted mode's size is needed. */
2686 if (data->arg.mode != BLKmode
2687 && data->arg.mode != DECL_MODE (parm))
2688 {
2689 set_mem_size (stack_parm, GET_MODE_SIZE (mode: data->arg.mode));
2690 if (MEM_EXPR (stack_parm) && MEM_OFFSET_KNOWN_P (stack_parm))
2691 {
2692 poly_int64 offset = subreg_lowpart_offset (DECL_MODE (parm),
2693 innermode: data->arg.mode);
2694 if (maybe_ne (a: offset, b: 0))
2695 set_mem_offset (stack_parm, MEM_OFFSET (stack_parm) - offset);
2696 }
2697 }
2698 }
2699
2700 boundary = data->locate.boundary;
2701 align = BITS_PER_UNIT;
2702
2703 /* If we're padding upward, we know that the alignment of the slot
2704 is TARGET_FUNCTION_ARG_BOUNDARY. If we're using slot_offset, we're
2705 intentionally forcing upward padding. Otherwise we have to come
2706 up with a guess at the alignment based on OFFSET_RTX. */
2707 poly_int64 offset;
2708 if (data->locate.where_pad == PAD_NONE || data->entry_parm)
2709 align = boundary;
2710 else if (data->locate.where_pad == PAD_UPWARD)
2711 {
2712 align = boundary;
2713 /* If the argument offset is actually more aligned than the nominal
2714 stack slot boundary, take advantage of that excess alignment.
2715 Don't make any assumptions if STACK_POINTER_OFFSET is in use. */
2716 if (poly_int_rtx_p (x: offset_rtx, res: &offset)
2717 && known_eq (STACK_POINTER_OFFSET, 0))
2718 {
2719 unsigned int offset_align = known_alignment (a: offset) * BITS_PER_UNIT;
2720 if (offset_align == 0 || offset_align > STACK_BOUNDARY)
2721 offset_align = STACK_BOUNDARY;
2722 align = MAX (align, offset_align);
2723 }
2724 }
2725 else if (poly_int_rtx_p (x: offset_rtx, res: &offset))
2726 {
2727 align = least_bit_hwi (x: boundary);
2728 unsigned int offset_align = known_alignment (a: offset) * BITS_PER_UNIT;
2729 if (offset_align != 0)
2730 align = MIN (align, offset_align);
2731 }
2732 set_mem_align (stack_parm, align);
2733
2734 if (data->entry_parm)
2735 set_reg_attrs_for_parm (data->entry_parm, stack_parm);
2736
2737 data->stack_parm = stack_parm;
2738}
2739
2740/* A subroutine of assign_parms. Adjust DATA->ENTRY_RTL such that it's
2741 always valid and contiguous. */
2742
2743static void
2744assign_parm_adjust_entry_rtl (struct assign_parm_data_one *data)
2745{
2746 rtx entry_parm = data->entry_parm;
2747 rtx stack_parm = data->stack_parm;
2748
2749 /* If this parm was passed part in regs and part in memory, pretend it
2750 arrived entirely in memory by pushing the register-part onto the stack.
2751 In the special case of a DImode or DFmode that is split, we could put
2752 it together in a pseudoreg directly, but for now that's not worth
2753 bothering with. */
2754 if (data->partial != 0)
2755 {
2756 /* Handle calls that pass values in multiple non-contiguous
2757 locations. The Irix 6 ABI has examples of this. */
2758 if (GET_CODE (entry_parm) == PARALLEL)
2759 emit_group_store (validize_mem (copy_rtx (stack_parm)), entry_parm,
2760 data->arg.type, int_size_in_bytes (data->arg.type));
2761 else
2762 {
2763 gcc_assert (data->partial % UNITS_PER_WORD == 0);
2764 move_block_from_reg (REGNO (entry_parm),
2765 validize_mem (copy_rtx (stack_parm)),
2766 data->partial / UNITS_PER_WORD);
2767 }
2768
2769 entry_parm = stack_parm;
2770 }
2771
2772 /* If we didn't decide this parm came in a register, by default it came
2773 on the stack. */
2774 else if (entry_parm == NULL)
2775 entry_parm = stack_parm;
2776
2777 /* When an argument is passed in multiple locations, we can't make use
2778 of this information, but we can save some copying if the whole argument
2779 is passed in a single register. */
2780 else if (GET_CODE (entry_parm) == PARALLEL
2781 && data->nominal_mode != BLKmode
2782 && data->passed_mode != BLKmode)
2783 {
2784 size_t i, len = XVECLEN (entry_parm, 0);
2785
2786 for (i = 0; i < len; i++)
2787 if (XEXP (XVECEXP (entry_parm, 0, i), 0) != NULL_RTX
2788 && REG_P (XEXP (XVECEXP (entry_parm, 0, i), 0))
2789 && (GET_MODE (XEXP (XVECEXP (entry_parm, 0, i), 0))
2790 == data->passed_mode)
2791 && INTVAL (XEXP (XVECEXP (entry_parm, 0, i), 1)) == 0)
2792 {
2793 entry_parm = XEXP (XVECEXP (entry_parm, 0, i), 0);
2794 break;
2795 }
2796 }
2797
2798 data->entry_parm = entry_parm;
2799}
2800
2801/* A subroutine of assign_parms. Reconstitute any values which were
2802 passed in multiple registers and would fit in a single register. */
2803
2804static void
2805assign_parm_remove_parallels (struct assign_parm_data_one *data)
2806{
2807 rtx entry_parm = data->entry_parm;
2808
2809 /* Convert the PARALLEL to a REG of the same mode as the parallel.
2810 This can be done with register operations rather than on the
2811 stack, even if we will store the reconstituted parameter on the
2812 stack later. */
2813 if (GET_CODE (entry_parm) == PARALLEL && GET_MODE (entry_parm) != BLKmode)
2814 {
2815 rtx parmreg = gen_reg_rtx (GET_MODE (entry_parm));
2816 emit_group_store (parmreg, entry_parm, data->arg.type,
2817 GET_MODE_SIZE (GET_MODE (entry_parm)));
2818 entry_parm = parmreg;
2819 }
2820
2821 data->entry_parm = entry_parm;
2822}
2823
2824/* A subroutine of assign_parms. Adjust DATA->STACK_RTL such that it's
2825 always valid and properly aligned. */
2826
2827static void
2828assign_parm_adjust_stack_rtl (struct assign_parm_data_one *data)
2829{
2830 rtx stack_parm = data->stack_parm;
2831
2832 /* If we can't trust the parm stack slot to be aligned enough for its
2833 ultimate type, don't use that slot after entry. We'll make another
2834 stack slot, if we need one. */
2835 if (stack_parm
2836 && ((GET_MODE_ALIGNMENT (data->nominal_mode) > MEM_ALIGN (stack_parm)
2837 && ((optab_handler (op: movmisalign_optab, mode: data->nominal_mode)
2838 != CODE_FOR_nothing)
2839 || targetm.slow_unaligned_access (data->nominal_mode,
2840 MEM_ALIGN (stack_parm))))
2841 || (data->nominal_type
2842 && TYPE_ALIGN (data->nominal_type) > MEM_ALIGN (stack_parm)
2843 && MEM_ALIGN (stack_parm) < PREFERRED_STACK_BOUNDARY)))
2844 stack_parm = NULL;
2845
2846 /* If parm was passed in memory, and we need to convert it on entry,
2847 don't store it back in that same slot. */
2848 else if (data->entry_parm == stack_parm
2849 && data->nominal_mode != BLKmode
2850 && data->nominal_mode != data->passed_mode)
2851 stack_parm = NULL;
2852
2853 /* If stack protection is in effect for this function, don't leave any
2854 pointers in their passed stack slots. */
2855 else if (crtl->stack_protect_guard
2856 && (flag_stack_protect == SPCT_FLAG_ALL
2857 || data->arg.pass_by_reference
2858 || POINTER_TYPE_P (data->nominal_type)))
2859 stack_parm = NULL;
2860
2861 data->stack_parm = stack_parm;
2862}
2863
2864/* A subroutine of assign_parms. Return true if the current parameter
2865 should be stored as a BLKmode in the current frame. */
2866
2867static bool
2868assign_parm_setup_block_p (struct assign_parm_data_one *data)
2869{
2870 if (data->nominal_mode == BLKmode)
2871 return true;
2872 if (GET_MODE (data->entry_parm) == BLKmode)
2873 return true;
2874
2875#ifdef BLOCK_REG_PADDING
2876 /* Only assign_parm_setup_block knows how to deal with register arguments
2877 that are padded at the least significant end. */
2878 if (REG_P (data->entry_parm)
2879 && known_lt (GET_MODE_SIZE (data->arg.mode), UNITS_PER_WORD)
2880 && (BLOCK_REG_PADDING (data->passed_mode, data->arg.type, 1)
2881 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
2882 return true;
2883#endif
2884
2885 return false;
2886}
2887
2888/* A subroutine of assign_parms. Arrange for the parameter to be
2889 present and valid in DATA->STACK_RTL. */
2890
2891static void
2892assign_parm_setup_block (struct assign_parm_data_all *all,
2893 tree parm, struct assign_parm_data_one *data)
2894{
2895 rtx entry_parm = data->entry_parm;
2896 rtx stack_parm = data->stack_parm;
2897 rtx target_reg = NULL_RTX;
2898 bool in_conversion_seq = false;
2899 HOST_WIDE_INT size;
2900 HOST_WIDE_INT size_stored;
2901
2902 if (GET_CODE (entry_parm) == PARALLEL)
2903 entry_parm = emit_group_move_into_temps (entry_parm);
2904
2905 /* If we want the parameter in a pseudo, don't use a stack slot. */
2906 if (is_gimple_reg (parm) && use_register_for_decl (decl: parm))
2907 {
2908 tree def = ssa_default_def (cfun, parm);
2909 gcc_assert (def);
2910 machine_mode mode = promote_ssa_mode (def, NULL);
2911 rtx reg = gen_reg_rtx (mode);
2912 if (GET_CODE (reg) != CONCAT)
2913 stack_parm = reg;
2914 else
2915 {
2916 target_reg = reg;
2917 /* Avoid allocating a stack slot, if there isn't one
2918 preallocated by the ABI. It might seem like we should
2919 always prefer a pseudo, but converting between
2920 floating-point and integer modes goes through the stack
2921 on various machines, so it's better to use the reserved
2922 stack slot than to risk wasting it and allocating more
2923 for the conversion. */
2924 if (stack_parm == NULL_RTX)
2925 {
2926 int save = generating_concat_p;
2927 generating_concat_p = 0;
2928 stack_parm = gen_reg_rtx (mode);
2929 generating_concat_p = save;
2930 }
2931 }
2932 data->stack_parm = NULL;
2933 }
2934
2935 size = int_size_in_bytes (data->arg.type);
2936 size_stored = CEIL_ROUND (size, UNITS_PER_WORD);
2937 if (stack_parm == 0)
2938 {
2939 HOST_WIDE_INT parm_align
2940 = (STRICT_ALIGNMENT
2941 ? MAX (DECL_ALIGN (parm), BITS_PER_WORD) : DECL_ALIGN (parm));
2942
2943 SET_DECL_ALIGN (parm, parm_align);
2944 if (DECL_ALIGN (parm) > MAX_SUPPORTED_STACK_ALIGNMENT)
2945 {
2946 rtx allocsize = gen_int_mode (size_stored, Pmode);
2947 get_dynamic_stack_size (&allocsize, 0, DECL_ALIGN (parm), NULL);
2948 stack_parm = assign_stack_local (BLKmode, UINTVAL (allocsize),
2949 MAX_SUPPORTED_STACK_ALIGNMENT);
2950 rtx addr = align_dynamic_address (XEXP (stack_parm, 0),
2951 DECL_ALIGN (parm));
2952 mark_reg_pointer (addr, DECL_ALIGN (parm));
2953 stack_parm = gen_rtx_MEM (GET_MODE (stack_parm), addr);
2954 MEM_NOTRAP_P (stack_parm) = 1;
2955 }
2956 else
2957 stack_parm = assign_stack_local (BLKmode, size: size_stored,
2958 DECL_ALIGN (parm));
2959 if (known_eq (GET_MODE_SIZE (GET_MODE (entry_parm)), size))
2960 PUT_MODE (x: stack_parm, GET_MODE (entry_parm));
2961 set_mem_attributes (stack_parm, parm, 1);
2962 }
2963
2964 /* If a BLKmode arrives in registers, copy it to a stack slot. Handle
2965 calls that pass values in multiple non-contiguous locations. */
2966 if (REG_P (entry_parm) || GET_CODE (entry_parm) == PARALLEL)
2967 {
2968 rtx mem;
2969
2970 /* Note that we will be storing an integral number of words.
2971 So we have to be careful to ensure that we allocate an
2972 integral number of words. We do this above when we call
2973 assign_stack_local if space was not allocated in the argument
2974 list. If it was, this will not work if PARM_BOUNDARY is not
2975 a multiple of BITS_PER_WORD. It isn't clear how to fix this
2976 if it becomes a problem. Exception is when BLKmode arrives
2977 with arguments not conforming to word_mode. */
2978
2979 if (data->stack_parm == 0)
2980 ;
2981 else if (GET_CODE (entry_parm) == PARALLEL)
2982 ;
2983 else
2984 gcc_assert (!size || !(PARM_BOUNDARY % BITS_PER_WORD));
2985
2986 mem = validize_mem (copy_rtx (stack_parm));
2987
2988 /* Handle values in multiple non-contiguous locations. */
2989 if (GET_CODE (entry_parm) == PARALLEL && !MEM_P (mem))
2990 emit_group_store (mem, entry_parm, data->arg.type, size);
2991 else if (GET_CODE (entry_parm) == PARALLEL)
2992 {
2993 push_to_sequence2 (all->first_conversion_insn,
2994 all->last_conversion_insn);
2995 emit_group_store (mem, entry_parm, data->arg.type, size);
2996 all->first_conversion_insn = get_insns ();
2997 all->last_conversion_insn = get_last_insn ();
2998 end_sequence ();
2999 in_conversion_seq = true;
3000 }
3001
3002 else if (size == 0)
3003 ;
3004
3005 /* If SIZE is that of a mode no bigger than a word, just use
3006 that mode's store operation. */
3007 else if (size <= UNITS_PER_WORD)
3008 {
3009 unsigned int bits = size * BITS_PER_UNIT;
3010 machine_mode mode = int_mode_for_size (size: bits, limit: 0).else_blk ();
3011
3012 if (mode != BLKmode
3013#ifdef BLOCK_REG_PADDING
3014 && (size == UNITS_PER_WORD
3015 || (BLOCK_REG_PADDING (mode, data->arg.type, 1)
3016 != (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
3017#endif
3018 )
3019 {
3020 rtx reg;
3021
3022 /* We are really truncating a word_mode value containing
3023 SIZE bytes into a value of mode MODE. If such an
3024 operation requires no actual instructions, we can refer
3025 to the value directly in mode MODE, otherwise we must
3026 start with the register in word_mode and explicitly
3027 convert it. */
3028 if (mode == word_mode
3029 || TRULY_NOOP_TRUNCATION_MODES_P (mode, word_mode))
3030 reg = gen_rtx_REG (mode, REGNO (entry_parm));
3031 else
3032 {
3033 reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3034 reg = convert_to_mode (mode, copy_to_reg (reg), 1);
3035 }
3036
3037 /* We use adjust_address to get a new MEM with the mode
3038 changed. adjust_address is better than change_address
3039 for this purpose because adjust_address does not lose
3040 the MEM_EXPR associated with the MEM.
3041
3042 If the MEM_EXPR is lost, then optimizations like DSE
3043 assume the MEM escapes and thus is not subject to DSE. */
3044 emit_move_insn (adjust_address (mem, mode, 0), reg);
3045 }
3046
3047#ifdef BLOCK_REG_PADDING
3048 /* Storing the register in memory as a full word, as
3049 move_block_from_reg below would do, and then using the
3050 MEM in a smaller mode, has the effect of shifting right
3051 if BYTES_BIG_ENDIAN. If we're bypassing memory, the
3052 shifting must be explicit. */
3053 else if (!MEM_P (mem))
3054 {
3055 rtx x;
3056
3057 /* If the assert below fails, we should have taken the
3058 mode != BLKmode path above, unless we have downward
3059 padding of smaller-than-word arguments on a machine
3060 with little-endian bytes, which would likely require
3061 additional changes to work correctly. */
3062 gcc_checking_assert (BYTES_BIG_ENDIAN
3063 && (BLOCK_REG_PADDING (mode,
3064 data->arg.type, 1)
3065 == PAD_UPWARD));
3066
3067 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3068
3069 x = gen_rtx_REG (word_mode, REGNO (entry_parm));
3070 x = expand_shift (RSHIFT_EXPR, word_mode, x, by,
3071 NULL_RTX, 1);
3072 x = force_reg (word_mode, x);
3073 x = gen_lowpart_SUBREG (GET_MODE (mem), x);
3074
3075 emit_move_insn (mem, x);
3076 }
3077#endif
3078
3079 /* Blocks smaller than a word on a BYTES_BIG_ENDIAN
3080 machine must be aligned to the left before storing
3081 to memory. Note that the previous test doesn't
3082 handle all cases (e.g. SIZE == 3). */
3083 else if (size != UNITS_PER_WORD
3084#ifdef BLOCK_REG_PADDING
3085 && (BLOCK_REG_PADDING (mode, data->arg.type, 1)
3086 == PAD_DOWNWARD)
3087#else
3088 && BYTES_BIG_ENDIAN
3089#endif
3090 )
3091 {
3092 rtx tem, x;
3093 int by = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
3094 rtx reg = gen_rtx_REG (word_mode, REGNO (entry_parm));
3095
3096 x = expand_shift (LSHIFT_EXPR, word_mode, reg, by, NULL_RTX, 1);
3097 tem = change_address (mem, word_mode, 0);
3098 emit_move_insn (tem, x);
3099 }
3100 else
3101 move_block_from_reg (REGNO (entry_parm), mem,
3102 size_stored / UNITS_PER_WORD);
3103 }
3104 else if (!MEM_P (mem))
3105 {
3106 gcc_checking_assert (size > UNITS_PER_WORD);
3107#ifdef BLOCK_REG_PADDING
3108 gcc_checking_assert (BLOCK_REG_PADDING (GET_MODE (mem),
3109 data->arg.type, 0)
3110 == PAD_UPWARD);
3111#endif
3112 emit_move_insn (mem, entry_parm);
3113 }
3114 else
3115 move_block_from_reg (REGNO (entry_parm), mem,
3116 size_stored / UNITS_PER_WORD);
3117 }
3118 else if (data->stack_parm == 0 && !TYPE_EMPTY_P (data->arg.type))
3119 {
3120 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3121 emit_block_move (stack_parm, data->entry_parm, GEN_INT (size),
3122 BLOCK_OP_NORMAL);
3123 all->first_conversion_insn = get_insns ();
3124 all->last_conversion_insn = get_last_insn ();
3125 end_sequence ();
3126 in_conversion_seq = true;
3127 }
3128
3129 if (target_reg)
3130 {
3131 if (!in_conversion_seq)
3132 emit_move_insn (target_reg, stack_parm);
3133 else
3134 {
3135 push_to_sequence2 (all->first_conversion_insn,
3136 all->last_conversion_insn);
3137 emit_move_insn (target_reg, stack_parm);
3138 all->first_conversion_insn = get_insns ();
3139 all->last_conversion_insn = get_last_insn ();
3140 end_sequence ();
3141 }
3142 stack_parm = target_reg;
3143 }
3144
3145 data->stack_parm = stack_parm;
3146 set_parm_rtl (parm, stack_parm);
3147}
3148
3149/* A subroutine of assign_parms. Allocate a pseudo to hold the current
3150 parameter. Get it there. Perform all ABI specified conversions. */
3151
3152static void
3153assign_parm_setup_reg (struct assign_parm_data_all *all, tree parm,
3154 struct assign_parm_data_one *data)
3155{
3156 rtx parmreg, validated_mem;
3157 rtx equiv_stack_parm;
3158 machine_mode promoted_nominal_mode;
3159 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (parm));
3160 bool did_conversion = false;
3161 bool need_conversion, moved;
3162 enum insn_code icode;
3163 rtx rtl;
3164
3165 /* Store the parm in a pseudoregister during the function, but we may
3166 need to do it in a wider mode. Using 2 here makes the result
3167 consistent with promote_decl_mode and thus expand_expr_real_1. */
3168 promoted_nominal_mode
3169 = promote_function_mode (data->nominal_type, data->nominal_mode, &unsignedp,
3170 TREE_TYPE (current_function_decl), 2);
3171
3172 parmreg = gen_reg_rtx (promoted_nominal_mode);
3173 if (!DECL_ARTIFICIAL (parm))
3174 mark_user_reg (parmreg);
3175
3176 /* If this was an item that we received a pointer to,
3177 set rtl appropriately. */
3178 if (data->arg.pass_by_reference)
3179 {
3180 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data->arg.type)), parmreg);
3181 set_mem_attributes (rtl, parm, 1);
3182 }
3183 else
3184 rtl = parmreg;
3185
3186 assign_parm_remove_parallels (data);
3187
3188 /* Copy the value into the register, thus bridging between
3189 assign_parm_find_data_types and expand_expr_real_1. */
3190
3191 equiv_stack_parm = data->stack_parm;
3192 validated_mem = validize_mem (copy_rtx (data->entry_parm));
3193
3194 need_conversion = (data->nominal_mode != data->passed_mode
3195 || promoted_nominal_mode != data->arg.mode);
3196 moved = false;
3197
3198 if (need_conversion
3199 && GET_MODE_CLASS (data->nominal_mode) == MODE_INT
3200 && data->nominal_mode == data->passed_mode
3201 && data->nominal_mode == GET_MODE (data->entry_parm))
3202 {
3203 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3204 mode, by the caller. We now have to convert it to
3205 NOMINAL_MODE, if different. However, PARMREG may be in
3206 a different mode than NOMINAL_MODE if it is being stored
3207 promoted.
3208
3209 If ENTRY_PARM is a hard register, it might be in a register
3210 not valid for operating in its mode (e.g., an odd-numbered
3211 register for a DFmode). In that case, moves are the only
3212 thing valid, so we can't do a convert from there. This
3213 occurs when the calling sequence allow such misaligned
3214 usages.
3215
3216 In addition, the conversion may involve a call, which could
3217 clobber parameters which haven't been copied to pseudo
3218 registers yet.
3219
3220 First, we try to emit an insn which performs the necessary
3221 conversion. We verify that this insn does not clobber any
3222 hard registers. */
3223
3224 rtx op0, op1;
3225
3226 icode = can_extend_p (promoted_nominal_mode, data->passed_mode,
3227 unsignedp);
3228
3229 op0 = parmreg;
3230 op1 = validated_mem;
3231 if (icode != CODE_FOR_nothing
3232 && insn_operand_matches (icode, opno: 0, operand: op0)
3233 && insn_operand_matches (icode, opno: 1, operand: op1))
3234 {
3235 enum rtx_code code = unsignedp ? ZERO_EXTEND : SIGN_EXTEND;
3236 rtx_insn *insn, *insns;
3237 rtx t = op1;
3238 HARD_REG_SET hardregs;
3239
3240 start_sequence ();
3241 /* If op1 is a hard register that is likely spilled, first
3242 force it into a pseudo, otherwise combiner might extend
3243 its lifetime too much. */
3244 if (GET_CODE (t) == SUBREG)
3245 t = SUBREG_REG (t);
3246 if (REG_P (t)
3247 && HARD_REGISTER_P (t)
3248 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (t))
3249 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (t))))
3250 {
3251 t = gen_reg_rtx (GET_MODE (op1));
3252 emit_move_insn (t, op1);
3253 }
3254 else
3255 t = op1;
3256 rtx_insn *pat = gen_extend_insn (op0, t, promoted_nominal_mode,
3257 data->passed_mode, unsignedp);
3258 emit_insn (pat);
3259 insns = get_insns ();
3260
3261 moved = true;
3262 CLEAR_HARD_REG_SET (set&: hardregs);
3263 for (insn = insns; insn && moved; insn = NEXT_INSN (insn))
3264 {
3265 if (INSN_P (insn))
3266 note_stores (insn, record_hard_reg_sets, &hardregs);
3267 if (!hard_reg_set_empty_p (x: hardregs))
3268 moved = false;
3269 }
3270
3271 end_sequence ();
3272
3273 if (moved)
3274 {
3275 emit_insn (insns);
3276 if (equiv_stack_parm != NULL_RTX)
3277 equiv_stack_parm = gen_rtx_fmt_e (code, GET_MODE (parmreg),
3278 equiv_stack_parm);
3279 }
3280 }
3281 }
3282
3283 if (moved)
3284 /* Nothing to do. */
3285 ;
3286 else if (need_conversion)
3287 {
3288 /* We did not have an insn to convert directly, or the sequence
3289 generated appeared unsafe. We must first copy the parm to a
3290 pseudo reg, and save the conversion until after all
3291 parameters have been moved. */
3292
3293 int save_tree_used;
3294 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3295
3296 emit_move_insn (tempreg, validated_mem);
3297
3298 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3299 tempreg = convert_to_mode (data->nominal_mode, tempreg, unsignedp);
3300
3301 if (partial_subreg_p (x: tempreg)
3302 && GET_MODE (tempreg) == data->nominal_mode
3303 && REG_P (SUBREG_REG (tempreg))
3304 && data->nominal_mode == data->passed_mode
3305 && GET_MODE (SUBREG_REG (tempreg)) == GET_MODE (data->entry_parm))
3306 {
3307 /* The argument is already sign/zero extended, so note it
3308 into the subreg. */
3309 SUBREG_PROMOTED_VAR_P (tempreg) = 1;
3310 SUBREG_PROMOTED_SET (tempreg, unsignedp);
3311 }
3312
3313 /* TREE_USED gets set erroneously during expand_assignment. */
3314 save_tree_used = TREE_USED (parm);
3315 SET_DECL_RTL (parm, rtl);
3316 expand_assignment (parm, make_tree (data->nominal_type, tempreg), false);
3317 SET_DECL_RTL (parm, NULL_RTX);
3318 TREE_USED (parm) = save_tree_used;
3319 all->first_conversion_insn = get_insns ();
3320 all->last_conversion_insn = get_last_insn ();
3321 end_sequence ();
3322
3323 did_conversion = true;
3324 }
3325 else if (MEM_P (data->entry_parm)
3326 && GET_MODE_ALIGNMENT (promoted_nominal_mode)
3327 > MEM_ALIGN (data->entry_parm)
3328 && (((icode = optab_handler (op: movmisalign_optab,
3329 mode: promoted_nominal_mode))
3330 != CODE_FOR_nothing)
3331 || targetm.slow_unaligned_access (promoted_nominal_mode,
3332 MEM_ALIGN (data->entry_parm))))
3333 {
3334 if (icode != CODE_FOR_nothing)
3335 emit_insn (GEN_FCN (icode) (parmreg, validated_mem));
3336 else
3337 rtl = parmreg = extract_bit_field (validated_mem,
3338 GET_MODE_BITSIZE (mode: promoted_nominal_mode), 0,
3339 unsignedp, parmreg,
3340 promoted_nominal_mode, VOIDmode, false, NULL);
3341 }
3342 else
3343 emit_move_insn (parmreg, validated_mem);
3344
3345 /* If we were passed a pointer but the actual value can live in a register,
3346 retrieve it and use it directly. Note that we cannot use nominal_mode,
3347 because it will have been set to Pmode above, we must use the actual mode
3348 of the parameter instead. */
3349 if (data->arg.pass_by_reference && TYPE_MODE (TREE_TYPE (parm)) != BLKmode)
3350 {
3351 /* Use a stack slot for debugging purposes if possible. */
3352 if (use_register_for_decl (decl: parm))
3353 {
3354 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3355 mark_user_reg (parmreg);
3356 }
3357 else
3358 {
3359 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3360 TYPE_MODE (TREE_TYPE (parm)),
3361 TYPE_ALIGN (TREE_TYPE (parm)));
3362 parmreg
3363 = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)),
3364 size: GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))),
3365 align);
3366 set_mem_attributes (parmreg, parm, 1);
3367 }
3368
3369 /* We need to preserve an address based on VIRTUAL_STACK_VARS_REGNUM for
3370 the debug info in case it is not legitimate. */
3371 if (GET_MODE (parmreg) != GET_MODE (rtl))
3372 {
3373 rtx tempreg = gen_reg_rtx (GET_MODE (rtl));
3374 int unsigned_p = TYPE_UNSIGNED (TREE_TYPE (parm));
3375
3376 push_to_sequence2 (all->first_conversion_insn,
3377 all->last_conversion_insn);
3378 emit_move_insn (tempreg, rtl);
3379 tempreg = convert_to_mode (GET_MODE (parmreg), tempreg, unsigned_p);
3380 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg,
3381 tempreg);
3382 all->first_conversion_insn = get_insns ();
3383 all->last_conversion_insn = get_last_insn ();
3384 end_sequence ();
3385
3386 did_conversion = true;
3387 }
3388 else
3389 emit_move_insn (MEM_P (parmreg) ? copy_rtx (parmreg) : parmreg, rtl);
3390
3391 rtl = parmreg;
3392
3393 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3394 now the parm. */
3395 data->stack_parm = NULL;
3396 }
3397
3398 set_parm_rtl (parm, rtl);
3399
3400 /* Mark the register as eliminable if we did no conversion and it was
3401 copied from memory at a fixed offset, and the arg pointer was not
3402 copied to a pseudo-reg. If the arg pointer is a pseudo reg or the
3403 offset formed an invalid address, such memory-equivalences as we
3404 make here would screw up life analysis for it. */
3405 if (data->nominal_mode == data->passed_mode
3406 && !did_conversion
3407 && data->stack_parm != 0
3408 && MEM_P (data->stack_parm)
3409 && data->locate.offset.var == 0
3410 && reg_mentioned_p (virtual_incoming_args_rtx,
3411 XEXP (data->stack_parm, 0)))
3412 {
3413 rtx_insn *linsn = get_last_insn ();
3414 rtx_insn *sinsn;
3415 rtx set;
3416
3417 /* Mark complex types separately. */
3418 if (GET_CODE (parmreg) == CONCAT)
3419 {
3420 scalar_mode submode = GET_MODE_INNER (GET_MODE (parmreg));
3421 int regnor = REGNO (XEXP (parmreg, 0));
3422 int regnoi = REGNO (XEXP (parmreg, 1));
3423 rtx stackr = adjust_address_nv (data->stack_parm, submode, 0);
3424 rtx stacki = adjust_address_nv (data->stack_parm, submode,
3425 GET_MODE_SIZE (submode));
3426
3427 /* Scan backwards for the set of the real and
3428 imaginary parts. */
3429 for (sinsn = linsn; sinsn != 0;
3430 sinsn = prev_nonnote_insn (sinsn))
3431 {
3432 set = single_set (insn: sinsn);
3433 if (set == 0)
3434 continue;
3435
3436 if (SET_DEST (set) == regno_reg_rtx [regnoi])
3437 set_unique_reg_note (sinsn, REG_EQUIV, stacki);
3438 else if (SET_DEST (set) == regno_reg_rtx [regnor])
3439 set_unique_reg_note (sinsn, REG_EQUIV, stackr);
3440 }
3441 }
3442 else
3443 set_dst_reg_note (linsn, REG_EQUIV, equiv_stack_parm, parmreg);
3444 }
3445
3446 /* For pointer data type, suggest pointer register. */
3447 if (POINTER_TYPE_P (TREE_TYPE (parm)))
3448 mark_reg_pointer (parmreg,
3449 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
3450}
3451
3452/* A subroutine of assign_parms. Allocate stack space to hold the current
3453 parameter. Get it there. Perform all ABI specified conversions. */
3454
3455static void
3456assign_parm_setup_stack (struct assign_parm_data_all *all, tree parm,
3457 struct assign_parm_data_one *data)
3458{
3459 /* Value must be stored in the stack slot STACK_PARM during function
3460 execution. */
3461 bool to_conversion = false;
3462
3463 assign_parm_remove_parallels (data);
3464
3465 if (data->arg.mode != data->nominal_mode)
3466 {
3467 /* Conversion is required. */
3468 rtx tempreg = gen_reg_rtx (GET_MODE (data->entry_parm));
3469
3470 emit_move_insn (tempreg, validize_mem (copy_rtx (data->entry_parm)));
3471
3472 /* Some ABIs require scalar floating point modes to be passed
3473 in a wider scalar integer mode. We need to explicitly
3474 truncate to an integer mode of the correct precision before
3475 using a SUBREG to reinterpret as a floating point value. */
3476 if (SCALAR_FLOAT_MODE_P (data->nominal_mode)
3477 && SCALAR_INT_MODE_P (data->arg.mode)
3478 && known_lt (GET_MODE_SIZE (data->nominal_mode),
3479 GET_MODE_SIZE (data->arg.mode)))
3480 tempreg = convert_wider_int_to_float (mode: data->nominal_mode,
3481 imode: data->arg.mode, x: tempreg);
3482
3483 push_to_sequence2 (all->first_conversion_insn, all->last_conversion_insn);
3484 to_conversion = true;
3485
3486 data->entry_parm = convert_to_mode (data->nominal_mode, tempreg,
3487 TYPE_UNSIGNED (TREE_TYPE (parm)));
3488
3489 if (data->stack_parm)
3490 {
3491 poly_int64 offset
3492 = subreg_lowpart_offset (outermode: data->nominal_mode,
3493 GET_MODE (data->stack_parm));
3494 /* ??? This may need a big-endian conversion on sparc64. */
3495 data->stack_parm
3496 = adjust_address (data->stack_parm, data->nominal_mode, 0);
3497 if (maybe_ne (a: offset, b: 0) && MEM_OFFSET_KNOWN_P (data->stack_parm))
3498 set_mem_offset (data->stack_parm,
3499 MEM_OFFSET (data->stack_parm) + offset);
3500 }
3501 }
3502
3503 if (data->entry_parm != data->stack_parm)
3504 {
3505 rtx src, dest;
3506
3507 if (data->stack_parm == 0)
3508 {
3509 int align = STACK_SLOT_ALIGNMENT (data->arg.type,
3510 GET_MODE (data->entry_parm),
3511 TYPE_ALIGN (data->arg.type));
3512 if (align < (int)GET_MODE_ALIGNMENT (GET_MODE (data->entry_parm))
3513 && ((optab_handler (op: movmisalign_optab,
3514 GET_MODE (data->entry_parm))
3515 != CODE_FOR_nothing)
3516 || targetm.slow_unaligned_access (GET_MODE (data->entry_parm),
3517 align)))
3518 align = GET_MODE_ALIGNMENT (GET_MODE (data->entry_parm));
3519 data->stack_parm
3520 = assign_stack_local (GET_MODE (data->entry_parm),
3521 size: GET_MODE_SIZE (GET_MODE (data->entry_parm)),
3522 align);
3523 align = MEM_ALIGN (data->stack_parm);
3524 set_mem_attributes (data->stack_parm, parm, 1);
3525 set_mem_align (data->stack_parm, align);
3526 }
3527
3528 dest = validize_mem (copy_rtx (data->stack_parm));
3529 src = validize_mem (copy_rtx (data->entry_parm));
3530
3531 if (TYPE_EMPTY_P (data->arg.type))
3532 /* Empty types don't really need to be copied. */;
3533 else if (MEM_P (src))
3534 {
3535 /* Use a block move to handle potentially misaligned entry_parm. */
3536 if (!to_conversion)
3537 push_to_sequence2 (all->first_conversion_insn,
3538 all->last_conversion_insn);
3539 to_conversion = true;
3540
3541 emit_block_move (dest, src,
3542 GEN_INT (int_size_in_bytes (data->arg.type)),
3543 BLOCK_OP_NORMAL);
3544 }
3545 else
3546 {
3547 if (!REG_P (src))
3548 src = force_reg (GET_MODE (src), src);
3549 emit_move_insn (dest, src);
3550 }
3551 }
3552
3553 if (to_conversion)
3554 {
3555 all->first_conversion_insn = get_insns ();
3556 all->last_conversion_insn = get_last_insn ();
3557 end_sequence ();
3558 }
3559
3560 set_parm_rtl (parm, data->stack_parm);
3561}
3562
3563/* A subroutine of assign_parms. If the ABI splits complex arguments, then
3564 undo the frobbing that we did in assign_parms_augmented_arg_list. */
3565
3566static void
3567assign_parms_unsplit_complex (struct assign_parm_data_all *all,
3568 vec<tree> fnargs)
3569{
3570 tree parm;
3571 tree orig_fnargs = all->orig_fnargs;
3572 unsigned i = 0;
3573
3574 for (parm = orig_fnargs; parm; parm = TREE_CHAIN (parm), ++i)
3575 {
3576 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
3577 && targetm.calls.split_complex_arg (TREE_TYPE (parm)))
3578 {
3579 rtx tmp, real, imag;
3580 scalar_mode inner = GET_MODE_INNER (DECL_MODE (parm));
3581
3582 real = DECL_RTL (fnargs[i]);
3583 imag = DECL_RTL (fnargs[i + 1]);
3584 if (inner != GET_MODE (real))
3585 {
3586 real = gen_lowpart_SUBREG (inner, real);
3587 imag = gen_lowpart_SUBREG (inner, imag);
3588 }
3589
3590 if (TREE_ADDRESSABLE (parm))
3591 {
3592 rtx rmem, imem;
3593 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (parm));
3594 int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm),
3595 DECL_MODE (parm),
3596 TYPE_ALIGN (TREE_TYPE (parm)));
3597
3598 /* split_complex_arg put the real and imag parts in
3599 pseudos. Move them to memory. */
3600 tmp = assign_stack_local (DECL_MODE (parm), size, align);
3601 set_mem_attributes (tmp, parm, 1);
3602 rmem = adjust_address_nv (tmp, inner, 0);
3603 imem = adjust_address_nv (tmp, inner, GET_MODE_SIZE (inner));
3604 push_to_sequence2 (all->first_conversion_insn,
3605 all->last_conversion_insn);
3606 emit_move_insn (rmem, real);
3607 emit_move_insn (imem, imag);
3608 all->first_conversion_insn = get_insns ();
3609 all->last_conversion_insn = get_last_insn ();
3610 end_sequence ();
3611 }
3612 else
3613 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3614 set_parm_rtl (parm, tmp);
3615
3616 real = DECL_INCOMING_RTL (fnargs[i]);
3617 imag = DECL_INCOMING_RTL (fnargs[i + 1]);
3618 if (inner != GET_MODE (real))
3619 {
3620 real = gen_lowpart_SUBREG (inner, real);
3621 imag = gen_lowpart_SUBREG (inner, imag);
3622 }
3623 tmp = gen_rtx_CONCAT (DECL_MODE (parm), real, imag);
3624 set_decl_incoming_rtl (parm, tmp, false);
3625 i++;
3626 }
3627 }
3628}
3629
3630/* Assign RTL expressions to the function's parameters. This may involve
3631 copying them into registers and using those registers as the DECL_RTL. */
3632
3633static void
3634assign_parms (tree fndecl)
3635{
3636 struct assign_parm_data_all all;
3637 tree parm;
3638 vec<tree> fnargs;
3639 unsigned i;
3640
3641 crtl->args.internal_arg_pointer
3642 = targetm.calls.internal_arg_pointer ();
3643
3644 assign_parms_initialize_all (all: &all);
3645 fnargs = assign_parms_augmented_arg_list (all: &all);
3646
3647 if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (fndecl))
3648 && fnargs.is_empty ())
3649 {
3650 struct assign_parm_data_one data = {};
3651 assign_parms_setup_varargs (all: &all, data: &data, no_rtl: false);
3652 }
3653
3654 FOR_EACH_VEC_ELT (fnargs, i, parm)
3655 {
3656 struct assign_parm_data_one data;
3657
3658 /* Extract the type of PARM; adjust it according to ABI. */
3659 assign_parm_find_data_types (all: &all, parm, data: &data);
3660
3661 /* Early out for errors and void parameters. */
3662 if (data.passed_mode == VOIDmode)
3663 {
3664 SET_DECL_RTL (parm, const0_rtx);
3665 DECL_INCOMING_RTL (parm) = DECL_RTL (parm);
3666 continue;
3667 }
3668
3669 /* Estimate stack alignment from parameter alignment. */
3670 if (SUPPORTS_STACK_ALIGNMENT)
3671 {
3672 unsigned int align
3673 = targetm.calls.function_arg_boundary (data.arg.mode,
3674 data.arg.type);
3675 align = MINIMUM_ALIGNMENT (data.arg.type, data.arg.mode, align);
3676 if (TYPE_ALIGN (data.nominal_type) > align)
3677 align = MINIMUM_ALIGNMENT (data.nominal_type,
3678 TYPE_MODE (data.nominal_type),
3679 TYPE_ALIGN (data.nominal_type));
3680 if (crtl->stack_alignment_estimated < align)
3681 {
3682 gcc_assert (!crtl->stack_realign_processed);
3683 crtl->stack_alignment_estimated = align;
3684 }
3685 }
3686
3687 /* Find out where the parameter arrives in this function. */
3688 assign_parm_find_entry_rtl (all: &all, data: &data);
3689
3690 /* Find out where stack space for this parameter might be. */
3691 if (assign_parm_is_stack_parm (all: &all, data: &data))
3692 {
3693 assign_parm_find_stack_rtl (parm, data: &data);
3694 assign_parm_adjust_entry_rtl (data: &data);
3695 /* For arguments that occupy no space in the parameter
3696 passing area, have non-zero size and have address taken,
3697 force creation of a stack slot so that they have distinct
3698 address from other parameters. */
3699 if (TYPE_EMPTY_P (data.arg.type)
3700 && TREE_ADDRESSABLE (parm)
3701 && data.entry_parm == data.stack_parm
3702 && MEM_P (data.entry_parm)
3703 && int_size_in_bytes (data.arg.type))
3704 data.stack_parm = NULL_RTX;
3705 }
3706 /* Record permanently how this parm was passed. */
3707 if (data.arg.pass_by_reference)
3708 {
3709 rtx incoming_rtl
3710 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (data.arg.type)),
3711 data.entry_parm);
3712 set_decl_incoming_rtl (parm, incoming_rtl, true);
3713 }
3714 else
3715 set_decl_incoming_rtl (parm, data.entry_parm, false);
3716
3717 assign_parm_adjust_stack_rtl (data: &data);
3718
3719 if (assign_parm_setup_block_p (data: &data))
3720 assign_parm_setup_block (all: &all, parm, data: &data);
3721 else if (data.arg.pass_by_reference || use_register_for_decl (decl: parm))
3722 assign_parm_setup_reg (all: &all, parm, data: &data);
3723 else
3724 assign_parm_setup_stack (all: &all, parm, data: &data);
3725
3726 if (cfun->stdarg && !DECL_CHAIN (parm))
3727 assign_parms_setup_varargs (all: &all, data: &data, no_rtl: false);
3728
3729 /* Update info on where next arg arrives in registers. */
3730 targetm.calls.function_arg_advance (all.args_so_far, data.arg);
3731 }
3732
3733 if (targetm.calls.split_complex_arg)
3734 assign_parms_unsplit_complex (all: &all, fnargs);
3735
3736 fnargs.release ();
3737
3738 /* Output all parameter conversion instructions (possibly including calls)
3739 now that all parameters have been copied out of hard registers. */
3740 emit_insn (all.first_conversion_insn);
3741
3742 do_pending_stack_adjust ();
3743
3744 /* Estimate reload stack alignment from scalar return mode. */
3745 if (SUPPORTS_STACK_ALIGNMENT)
3746 {
3747 if (DECL_RESULT (fndecl))
3748 {
3749 tree type = TREE_TYPE (DECL_RESULT (fndecl));
3750 machine_mode mode = TYPE_MODE (type);
3751
3752 if (mode != BLKmode
3753 && mode != VOIDmode
3754 && !AGGREGATE_TYPE_P (type))
3755 {
3756 unsigned int align = GET_MODE_ALIGNMENT (mode);
3757 if (crtl->stack_alignment_estimated < align)
3758 {
3759 gcc_assert (!crtl->stack_realign_processed);
3760 crtl->stack_alignment_estimated = align;
3761 }
3762 }
3763 }
3764 }
3765
3766 /* If we are receiving a struct value address as the first argument, set up
3767 the RTL for the function result. As this might require code to convert
3768 the transmitted address to Pmode, we do this here to ensure that possible
3769 preliminary conversions of the address have been emitted already. */
3770 if (all.function_result_decl)
3771 {
3772 tree result = DECL_RESULT (current_function_decl);
3773 rtx addr = DECL_RTL (all.function_result_decl);
3774 rtx x;
3775
3776 if (DECL_BY_REFERENCE (result))
3777 {
3778 SET_DECL_VALUE_EXPR (result, all.function_result_decl);
3779 x = addr;
3780 }
3781 else
3782 {
3783 SET_DECL_VALUE_EXPR (result,
3784 build1 (INDIRECT_REF, TREE_TYPE (result),
3785 all.function_result_decl));
3786 addr = convert_memory_address (Pmode, addr);
3787 x = gen_rtx_MEM (DECL_MODE (result), addr);
3788 set_mem_attributes (x, result, 1);
3789 }
3790
3791 DECL_HAS_VALUE_EXPR_P (result) = 1;
3792
3793 set_parm_rtl (result, x);
3794 }
3795
3796 /* We have aligned all the args, so add space for the pretend args. */
3797 crtl->args.pretend_args_size = all.pretend_args_size;
3798 all.stack_args_size.constant += all.extra_pretend_bytes;
3799 crtl->args.size = all.stack_args_size.constant;
3800
3801 /* Adjust function incoming argument size for alignment and
3802 minimum length. */
3803
3804 crtl->args.size = upper_bound (crtl->args.size, b: all.reg_parm_stack_space);
3805 crtl->args.size = aligned_upper_bound (crtl->args.size,
3806 PARM_BOUNDARY / BITS_PER_UNIT);
3807
3808 if (ARGS_GROW_DOWNWARD)
3809 {
3810 crtl->args.arg_offset_rtx
3811 = (all.stack_args_size.var == 0
3812 ? gen_int_mode (-all.stack_args_size.constant, Pmode)
3813 : expand_expr (size_diffop (all.stack_args_size.var,
3814 size_int (-all.stack_args_size.constant)),
3815 NULL_RTX, VOIDmode, modifier: EXPAND_NORMAL));
3816 }
3817 else
3818 crtl->args.arg_offset_rtx = ARGS_SIZE_RTX (all.stack_args_size);
3819
3820 /* See how many bytes, if any, of its args a function should try to pop
3821 on return. */
3822
3823 crtl->args.pops_args = targetm.calls.return_pops_args (fndecl,
3824 TREE_TYPE (fndecl),
3825 crtl->args.size);
3826
3827 /* For stdarg.h function, save info about
3828 regs and stack space used by the named args. */
3829
3830 crtl->args.info = all.args_so_far_v;
3831
3832 /* Set the rtx used for the function return value. Put this in its
3833 own variable so any optimizers that need this information don't have
3834 to include tree.h. Do this here so it gets done when an inlined
3835 function gets output. */
3836
3837 crtl->return_rtx
3838 = (DECL_RTL_SET_P (DECL_RESULT (fndecl))
3839 ? DECL_RTL (DECL_RESULT (fndecl)) : NULL_RTX);
3840
3841 /* If scalar return value was computed in a pseudo-reg, or was a named
3842 return value that got dumped to the stack, copy that to the hard
3843 return register. */
3844 if (DECL_RTL_SET_P (DECL_RESULT (fndecl)))
3845 {
3846 tree decl_result = DECL_RESULT (fndecl);
3847 rtx decl_rtl = DECL_RTL (decl_result);
3848
3849 if (REG_P (decl_rtl)
3850 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
3851 : DECL_REGISTER (decl_result))
3852 {
3853 rtx real_decl_rtl;
3854
3855 /* Unless the psABI says not to. */
3856 if (TYPE_EMPTY_P (TREE_TYPE (decl_result)))
3857 real_decl_rtl = NULL_RTX;
3858 else
3859 {
3860 real_decl_rtl
3861 = targetm.calls.function_value (TREE_TYPE (decl_result),
3862 fndecl, true);
3863 REG_FUNCTION_VALUE_P (real_decl_rtl) = 1;
3864 }
3865 /* The delay slot scheduler assumes that crtl->return_rtx
3866 holds the hard register containing the return value, not a
3867 temporary pseudo. */
3868 crtl->return_rtx = real_decl_rtl;
3869 }
3870 }
3871}
3872
3873/* Gimplify the parameter list for current_function_decl. This involves
3874 evaluating SAVE_EXPRs of variable sized parameters and generating code
3875 to implement callee-copies reference parameters. Returns a sequence of
3876 statements to add to the beginning of the function. */
3877
3878gimple_seq
3879gimplify_parameters (gimple_seq *cleanup)
3880{
3881 struct assign_parm_data_all all;
3882 tree parm;
3883 gimple_seq stmts = NULL;
3884 vec<tree> fnargs;
3885 unsigned i;
3886
3887 assign_parms_initialize_all (all: &all);
3888 fnargs = assign_parms_augmented_arg_list (all: &all);
3889
3890 FOR_EACH_VEC_ELT (fnargs, i, parm)
3891 {
3892 struct assign_parm_data_one data;
3893
3894 /* Extract the type of PARM; adjust it according to ABI. */
3895 assign_parm_find_data_types (all: &all, parm, data: &data);
3896
3897 /* Early out for errors and void parameters. */
3898 if (data.passed_mode == VOIDmode || DECL_SIZE (parm) == NULL)
3899 continue;
3900
3901 /* Update info on where next arg arrives in registers. */
3902 targetm.calls.function_arg_advance (all.args_so_far, data.arg);
3903
3904 /* ??? Once upon a time variable_size stuffed parameter list
3905 SAVE_EXPRs (amongst others) onto a pending sizes list. This
3906 turned out to be less than manageable in the gimple world.
3907 Now we have to hunt them down ourselves. */
3908 gimplify_type_sizes (TREE_TYPE (parm), &stmts);
3909
3910 if (TREE_CODE (DECL_SIZE_UNIT (parm)) != INTEGER_CST)
3911 {
3912 gimplify_one_sizepos (&DECL_SIZE (parm), &stmts);
3913 gimplify_one_sizepos (&DECL_SIZE_UNIT (parm), &stmts);
3914 }
3915
3916 if (data.arg.pass_by_reference)
3917 {
3918 tree type = TREE_TYPE (data.arg.type);
3919 function_arg_info orig_arg (type, data.arg.named);
3920 if (reference_callee_copied (&all.args_so_far_v, orig_arg))
3921 {
3922 tree local, t;
3923
3924 /* For constant-sized objects, this is trivial; for
3925 variable-sized objects, we have to play games. */
3926 if (TREE_CODE (DECL_SIZE_UNIT (parm)) == INTEGER_CST
3927 && !(flag_stack_check == GENERIC_STACK_CHECK
3928 && compare_tree_int (DECL_SIZE_UNIT (parm),
3929 STACK_CHECK_MAX_VAR_SIZE) > 0))
3930 {
3931 local = create_tmp_var (type, get_name (parm));
3932 DECL_IGNORED_P (local) = 0;
3933 /* If PARM was addressable, move that flag over
3934 to the local copy, as its address will be taken,
3935 not the PARMs. Keep the parms address taken
3936 as we'll query that flag during gimplification. */
3937 if (TREE_ADDRESSABLE (parm))
3938 TREE_ADDRESSABLE (local) = 1;
3939 if (DECL_NOT_GIMPLE_REG_P (parm))
3940 DECL_NOT_GIMPLE_REG_P (local) = 1;
3941
3942 if (!is_gimple_reg (local)
3943 && flag_stack_reuse != SR_NONE)
3944 {
3945 tree clobber = build_clobber (type);
3946 gimple *clobber_stmt;
3947 clobber_stmt = gimple_build_assign (local, clobber);
3948 gimple_seq_add_stmt (cleanup, clobber_stmt);
3949 }
3950 }
3951 else
3952 {
3953 tree ptr_type, addr;
3954
3955 ptr_type = build_pointer_type (type);
3956 addr = create_tmp_reg (ptr_type, get_name (parm));
3957 DECL_IGNORED_P (addr) = 0;
3958 local = build_fold_indirect_ref (addr);
3959
3960 t = build_alloca_call_expr (DECL_SIZE_UNIT (parm),
3961 DECL_ALIGN (parm),
3962 max_int_size_in_bytes (type));
3963 /* The call has been built for a variable-sized object. */
3964 CALL_ALLOCA_FOR_VAR_P (t) = 1;
3965 t = fold_convert (ptr_type, t);
3966 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
3967 gimplify_and_add (t, &stmts);
3968 }
3969
3970 gimplify_assign (local, parm, &stmts);
3971
3972 SET_DECL_VALUE_EXPR (parm, local);
3973 DECL_HAS_VALUE_EXPR_P (parm) = 1;
3974 }
3975 }
3976 }
3977
3978 fnargs.release ();
3979
3980 return stmts;
3981}
3982
3983/* Compute the size and offset from the start of the stacked arguments for a
3984 parm passed in mode PASSED_MODE and with type TYPE.
3985
3986 INITIAL_OFFSET_PTR points to the current offset into the stacked
3987 arguments.
3988
3989 The starting offset and size for this parm are returned in
3990 LOCATE->OFFSET and LOCATE->SIZE, respectively. When IN_REGS is
3991 nonzero, the offset is that of stack slot, which is returned in
3992 LOCATE->SLOT_OFFSET. LOCATE->ALIGNMENT_PAD is the amount of
3993 padding required from the initial offset ptr to the stack slot.
3994
3995 IN_REGS is nonzero if the argument will be passed in registers. It will
3996 never be set if REG_PARM_STACK_SPACE is not defined.
3997
3998 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
3999 for arguments which are passed in registers.
4000
4001 FNDECL is the function in which the argument was defined.
4002
4003 There are two types of rounding that are done. The first, controlled by
4004 TARGET_FUNCTION_ARG_BOUNDARY, forces the offset from the start of the
4005 argument list to be aligned to the specific boundary (in bits). This
4006 rounding affects the initial and starting offsets, but not the argument
4007 size.
4008
4009 The second, controlled by TARGET_FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4010 optionally rounds the size of the parm to PARM_BOUNDARY. The
4011 initial offset is not affected by this rounding, while the size always
4012 is and the starting offset may be. */
4013
4014/* LOCATE->OFFSET will be negative for ARGS_GROW_DOWNWARD case;
4015 INITIAL_OFFSET_PTR is positive because locate_and_pad_parm's
4016 callers pass in the total size of args so far as
4017 INITIAL_OFFSET_PTR. LOCATE->SIZE is always positive. */
4018
4019void
4020locate_and_pad_parm (machine_mode passed_mode, tree type, int in_regs,
4021 int reg_parm_stack_space, int partial,
4022 tree fndecl ATTRIBUTE_UNUSED,
4023 struct args_size *initial_offset_ptr,
4024 struct locate_and_pad_arg_data *locate)
4025{
4026 tree sizetree;
4027 pad_direction where_pad;
4028 unsigned int boundary, round_boundary;
4029 int part_size_in_regs;
4030
4031 /* If we have found a stack parm before we reach the end of the
4032 area reserved for registers, skip that area. */
4033 if (! in_regs)
4034 {
4035 if (reg_parm_stack_space > 0)
4036 {
4037 if (initial_offset_ptr->var
4038 || !ordered_p (a: initial_offset_ptr->constant,
4039 b: reg_parm_stack_space))
4040 {
4041 initial_offset_ptr->var
4042 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4043 ssize_int (reg_parm_stack_space));
4044 initial_offset_ptr->constant = 0;
4045 }
4046 else
4047 initial_offset_ptr->constant
4048 = ordered_max (a: initial_offset_ptr->constant,
4049 b: reg_parm_stack_space);
4050 }
4051 }
4052
4053 part_size_in_regs = (reg_parm_stack_space == 0 ? partial : 0);
4054
4055 sizetree = (type
4056 ? arg_size_in_bytes (type)
4057 : size_int (GET_MODE_SIZE (passed_mode)));
4058 where_pad = targetm.calls.function_arg_padding (passed_mode, type);
4059 boundary = targetm.calls.function_arg_boundary (passed_mode, type);
4060 round_boundary = targetm.calls.function_arg_round_boundary (passed_mode,
4061 type);
4062 locate->where_pad = where_pad;
4063
4064 /* Alignment can't exceed MAX_SUPPORTED_STACK_ALIGNMENT. */
4065 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4066 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4067
4068 locate->boundary = boundary;
4069
4070 if (SUPPORTS_STACK_ALIGNMENT)
4071 {
4072 /* stack_alignment_estimated can't change after stack has been
4073 realigned. */
4074 if (crtl->stack_alignment_estimated < boundary)
4075 {
4076 if (!crtl->stack_realign_processed)
4077 crtl->stack_alignment_estimated = boundary;
4078 else
4079 {
4080 /* If stack is realigned and stack alignment value
4081 hasn't been finalized, it is OK not to increase
4082 stack_alignment_estimated. The bigger alignment
4083 requirement is recorded in stack_alignment_needed
4084 below. */
4085 gcc_assert (!crtl->stack_realign_finalized
4086 && crtl->stack_realign_needed);
4087 }
4088 }
4089 }
4090
4091 if (ARGS_GROW_DOWNWARD)
4092 {
4093 locate->slot_offset.constant = -initial_offset_ptr->constant;
4094 if (initial_offset_ptr->var)
4095 locate->slot_offset.var = size_binop (MINUS_EXPR, ssize_int (0),
4096 initial_offset_ptr->var);
4097
4098 {
4099 tree s2 = sizetree;
4100 if (where_pad != PAD_NONE
4101 && (!tree_fits_uhwi_p (sizetree)
4102 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4103 s2 = round_up (s2, round_boundary / BITS_PER_UNIT);
4104 SUB_PARM_SIZE (locate->slot_offset, s2);
4105 }
4106
4107 locate->slot_offset.constant += part_size_in_regs;
4108
4109 if (!in_regs || reg_parm_stack_space > 0)
4110 pad_to_arg_alignment (&locate->slot_offset, boundary,
4111 &locate->alignment_pad);
4112
4113 locate->size.constant = (-initial_offset_ptr->constant
4114 - locate->slot_offset.constant);
4115 if (initial_offset_ptr->var)
4116 locate->size.var = size_binop (MINUS_EXPR,
4117 size_binop (MINUS_EXPR,
4118 ssize_int (0),
4119 initial_offset_ptr->var),
4120 locate->slot_offset.var);
4121
4122 /* Pad_below needs the pre-rounded size to know how much to pad
4123 below. */
4124 locate->offset = locate->slot_offset;
4125 if (where_pad == PAD_DOWNWARD)
4126 pad_below (&locate->offset, passed_mode, sizetree);
4127
4128 }
4129 else
4130 {
4131 if (!in_regs || reg_parm_stack_space > 0)
4132 pad_to_arg_alignment (initial_offset_ptr, boundary,
4133 &locate->alignment_pad);
4134 locate->slot_offset = *initial_offset_ptr;
4135
4136#ifdef PUSH_ROUNDING
4137 if (passed_mode != BLKmode)
4138 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4139#endif
4140
4141 /* Pad_below needs the pre-rounded size to know how much to pad below
4142 so this must be done before rounding up. */
4143 locate->offset = locate->slot_offset;
4144 if (where_pad == PAD_DOWNWARD)
4145 pad_below (&locate->offset, passed_mode, sizetree);
4146
4147 if (where_pad != PAD_NONE
4148 && (!tree_fits_uhwi_p (sizetree)
4149 || (tree_to_uhwi (sizetree) * BITS_PER_UNIT) % round_boundary))
4150 sizetree = round_up (sizetree, round_boundary / BITS_PER_UNIT);
4151
4152 ADD_PARM_SIZE (locate->size, sizetree);
4153
4154 locate->size.constant -= part_size_in_regs;
4155 }
4156
4157 locate->offset.constant
4158 += targetm.calls.function_arg_offset (passed_mode, type);
4159}
4160
4161/* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4162 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4163
4164static void
4165pad_to_arg_alignment (struct args_size *offset_ptr, int boundary,
4166 struct args_size *alignment_pad)
4167{
4168 tree save_var = NULL_TREE;
4169 poly_int64 save_constant = 0;
4170 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4171 poly_int64 sp_offset = STACK_POINTER_OFFSET;
4172
4173#ifdef SPARC_STACK_BOUNDARY_HACK
4174 /* ??? The SPARC port may claim a STACK_BOUNDARY higher than
4175 the real alignment of %sp. However, when it does this, the
4176 alignment of %sp+STACK_POINTER_OFFSET is STACK_BOUNDARY. */
4177 if (SPARC_STACK_BOUNDARY_HACK)
4178 sp_offset = 0;
4179#endif
4180
4181 if (boundary > PARM_BOUNDARY)
4182 {
4183 save_var = offset_ptr->var;
4184 save_constant = offset_ptr->constant;
4185 }
4186
4187 alignment_pad->var = NULL_TREE;
4188 alignment_pad->constant = 0;
4189
4190 if (boundary > BITS_PER_UNIT)
4191 {
4192 int misalign;
4193 if (offset_ptr->var
4194 || !known_misalignment (value: offset_ptr->constant + sp_offset,
4195 align: boundary_in_bytes, misalign: &misalign))
4196 {
4197 tree sp_offset_tree = ssize_int (sp_offset);
4198 tree offset = size_binop (PLUS_EXPR,
4199 ARGS_SIZE_TREE (*offset_ptr),
4200 sp_offset_tree);
4201 tree rounded;
4202 if (ARGS_GROW_DOWNWARD)
4203 rounded = round_down (offset, boundary / BITS_PER_UNIT);
4204 else
4205 rounded = round_up (offset, boundary / BITS_PER_UNIT);
4206
4207 offset_ptr->var = size_binop (MINUS_EXPR, rounded, sp_offset_tree);
4208 /* ARGS_SIZE_TREE includes constant term. */
4209 offset_ptr->constant = 0;
4210 if (boundary > PARM_BOUNDARY)
4211 alignment_pad->var = size_binop (MINUS_EXPR, offset_ptr->var,
4212 save_var);
4213 }
4214 else
4215 {
4216 if (ARGS_GROW_DOWNWARD)
4217 offset_ptr->constant -= misalign;
4218 else
4219 offset_ptr->constant += -misalign & (boundary_in_bytes - 1);
4220
4221 if (boundary > PARM_BOUNDARY)
4222 alignment_pad->constant = offset_ptr->constant - save_constant;
4223 }
4224 }
4225}
4226
4227static void
4228pad_below (struct args_size *offset_ptr, machine_mode passed_mode, tree sizetree)
4229{
4230 unsigned int align = PARM_BOUNDARY / BITS_PER_UNIT;
4231 int misalign;
4232 if (passed_mode != BLKmode
4233 && known_misalignment (value: GET_MODE_SIZE (mode: passed_mode), align, misalign: &misalign))
4234 offset_ptr->constant += -misalign & (align - 1);
4235 else
4236 {
4237 if (TREE_CODE (sizetree) != INTEGER_CST
4238 || (TREE_INT_CST_LOW (sizetree) & (align - 1)) != 0)
4239 {
4240 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4241 tree s2 = round_up (sizetree, align);
4242 /* Add it in. */
4243 ADD_PARM_SIZE (*offset_ptr, s2);
4244 SUB_PARM_SIZE (*offset_ptr, sizetree);
4245 }
4246 }
4247}
4248
4249
4250/* True if register REGNO was alive at a place where `setjmp' was
4251 called and was set more than once or is an argument. Such regs may
4252 be clobbered by `longjmp'. */
4253
4254static bool
4255regno_clobbered_at_setjmp (bitmap setjmp_crosses, int regno)
4256{
4257 /* There appear to be cases where some local vars never reach the
4258 backend but have bogus regnos. */
4259 if (regno >= max_reg_num ())
4260 return false;
4261
4262 return ((REG_N_SETS (regno) > 1
4263 || REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
4264 regno))
4265 && REGNO_REG_SET_P (setjmp_crosses, regno));
4266}
4267
4268/* Walk the tree of blocks describing the binding levels within a
4269 function and warn about variables the might be killed by setjmp or
4270 vfork. This is done after calling flow_analysis before register
4271 allocation since that will clobber the pseudo-regs to hard
4272 regs. */
4273
4274static void
4275setjmp_vars_warning (bitmap setjmp_crosses, tree block)
4276{
4277 tree decl, sub;
4278
4279 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
4280 {
4281 if (VAR_P (decl)
4282 && DECL_RTL_SET_P (decl)
4283 && REG_P (DECL_RTL (decl))
4284 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4285 warning (OPT_Wclobbered, "variable %q+D might be clobbered by"
4286 " %<longjmp%> or %<vfork%>", decl);
4287 }
4288
4289 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
4290 setjmp_vars_warning (setjmp_crosses, block: sub);
4291}
4292
4293/* Do the appropriate part of setjmp_vars_warning
4294 but for arguments instead of local variables. */
4295
4296static void
4297setjmp_args_warning (bitmap setjmp_crosses)
4298{
4299 tree decl;
4300 for (decl = DECL_ARGUMENTS (current_function_decl);
4301 decl; decl = DECL_CHAIN (decl))
4302 if (DECL_RTL (decl) != 0
4303 && REG_P (DECL_RTL (decl))
4304 && regno_clobbered_at_setjmp (setjmp_crosses, REGNO (DECL_RTL (decl))))
4305 warning (OPT_Wclobbered,
4306 "argument %q+D might be clobbered by %<longjmp%> or %<vfork%>",
4307 decl);
4308}
4309
4310/* Generate warning messages for variables live across setjmp. */
4311
4312void
4313generate_setjmp_warnings (void)
4314{
4315 bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
4316
4317 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS
4318 || bitmap_empty_p (map: setjmp_crosses))
4319 return;
4320
4321 setjmp_vars_warning (setjmp_crosses, DECL_INITIAL (current_function_decl));
4322 setjmp_args_warning (setjmp_crosses);
4323}
4324
4325
4326/* Reverse the order of elements in the fragment chain T of blocks,
4327 and return the new head of the chain (old last element).
4328 In addition to that clear BLOCK_SAME_RANGE flags when needed
4329 and adjust BLOCK_SUPERCONTEXT from the super fragment to
4330 its super fragment origin. */
4331
4332static tree
4333block_fragments_nreverse (tree t)
4334{
4335 tree prev = 0, block, next, prev_super = 0;
4336 tree super = BLOCK_SUPERCONTEXT (t);
4337 if (BLOCK_FRAGMENT_ORIGIN (super))
4338 super = BLOCK_FRAGMENT_ORIGIN (super);
4339 for (block = t; block; block = next)
4340 {
4341 next = BLOCK_FRAGMENT_CHAIN (block);
4342 BLOCK_FRAGMENT_CHAIN (block) = prev;
4343 if ((prev && !BLOCK_SAME_RANGE (prev))
4344 || (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (block))
4345 != prev_super))
4346 BLOCK_SAME_RANGE (block) = 0;
4347 prev_super = BLOCK_SUPERCONTEXT (block);
4348 BLOCK_SUPERCONTEXT (block) = super;
4349 prev = block;
4350 }
4351 t = BLOCK_FRAGMENT_ORIGIN (t);
4352 if (BLOCK_FRAGMENT_CHAIN (BLOCK_SUPERCONTEXT (t))
4353 != prev_super)
4354 BLOCK_SAME_RANGE (t) = 0;
4355 BLOCK_SUPERCONTEXT (t) = super;
4356 return prev;
4357}
4358
4359/* Reverse the order of elements in the chain T of blocks,
4360 and return the new head of the chain (old last element).
4361 Also do the same on subblocks and reverse the order of elements
4362 in BLOCK_FRAGMENT_CHAIN as well. */
4363
4364static tree
4365blocks_nreverse_all (tree t)
4366{
4367 tree prev = 0, block, next;
4368 for (block = t; block; block = next)
4369 {
4370 next = BLOCK_CHAIN (block);
4371 BLOCK_CHAIN (block) = prev;
4372 if (BLOCK_FRAGMENT_CHAIN (block)
4373 && BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE)
4374 {
4375 BLOCK_FRAGMENT_CHAIN (block)
4376 = block_fragments_nreverse (BLOCK_FRAGMENT_CHAIN (block));
4377 if (!BLOCK_SAME_RANGE (BLOCK_FRAGMENT_CHAIN (block)))
4378 BLOCK_SAME_RANGE (block) = 0;
4379 }
4380 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4381 prev = block;
4382 }
4383 return prev;
4384}
4385
4386
4387/* Identify BLOCKs referenced by more than one NOTE_INSN_BLOCK_{BEG,END},
4388 and create duplicate blocks. */
4389/* ??? Need an option to either create block fragments or to create
4390 abstract origin duplicates of a source block. It really depends
4391 on what optimization has been performed. */
4392
4393void
4394reorder_blocks (void)
4395{
4396 tree block = DECL_INITIAL (current_function_decl);
4397
4398 if (block == NULL_TREE)
4399 return;
4400
4401 auto_vec<tree, 10> block_stack;
4402
4403 /* Reset the TREE_ASM_WRITTEN bit for all blocks. */
4404 clear_block_marks (block);
4405
4406 /* Prune the old trees away, so that they don't get in the way. */
4407 BLOCK_SUBBLOCKS (block) = NULL_TREE;
4408 BLOCK_CHAIN (block) = NULL_TREE;
4409
4410 /* Recreate the block tree from the note nesting. */
4411 reorder_blocks_1 (get_insns (), block, &block_stack);
4412 BLOCK_SUBBLOCKS (block) = blocks_nreverse_all (BLOCK_SUBBLOCKS (block));
4413}
4414
4415/* Helper function for reorder_blocks. Reset TREE_ASM_WRITTEN. */
4416
4417void
4418clear_block_marks (tree block)
4419{
4420 while (block)
4421 {
4422 TREE_ASM_WRITTEN (block) = 0;
4423 clear_block_marks (BLOCK_SUBBLOCKS (block));
4424 block = BLOCK_CHAIN (block);
4425 }
4426}
4427
4428static void
4429reorder_blocks_1 (rtx_insn *insns, tree current_block,
4430 vec<tree> *p_block_stack)
4431{
4432 rtx_insn *insn;
4433 tree prev_beg = NULL_TREE, prev_end = NULL_TREE;
4434
4435 for (insn = insns; insn; insn = NEXT_INSN (insn))
4436 {
4437 if (NOTE_P (insn))
4438 {
4439 if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_BEG)
4440 {
4441 tree block = NOTE_BLOCK (insn);
4442 tree origin;
4443
4444 gcc_assert (BLOCK_FRAGMENT_ORIGIN (block) == NULL_TREE);
4445 origin = block;
4446
4447 if (prev_end)
4448 BLOCK_SAME_RANGE (prev_end) = 0;
4449 prev_end = NULL_TREE;
4450
4451 /* If we have seen this block before, that means it now
4452 spans multiple address regions. Create a new fragment. */
4453 if (TREE_ASM_WRITTEN (block))
4454 {
4455 tree new_block = copy_node (block);
4456
4457 BLOCK_SAME_RANGE (new_block) = 0;
4458 BLOCK_FRAGMENT_ORIGIN (new_block) = origin;
4459 BLOCK_FRAGMENT_CHAIN (new_block)
4460 = BLOCK_FRAGMENT_CHAIN (origin);
4461 BLOCK_FRAGMENT_CHAIN (origin) = new_block;
4462
4463 NOTE_BLOCK (insn) = new_block;
4464 block = new_block;
4465 }
4466
4467 if (prev_beg == current_block && prev_beg)
4468 BLOCK_SAME_RANGE (block) = 1;
4469
4470 prev_beg = origin;
4471
4472 BLOCK_SUBBLOCKS (block) = 0;
4473 TREE_ASM_WRITTEN (block) = 1;
4474 /* When there's only one block for the entire function,
4475 current_block == block and we mustn't do this, it
4476 will cause infinite recursion. */
4477 if (block != current_block)
4478 {
4479 tree super;
4480 if (block != origin)
4481 gcc_assert (BLOCK_SUPERCONTEXT (origin) == current_block
4482 || BLOCK_FRAGMENT_ORIGIN (BLOCK_SUPERCONTEXT
4483 (origin))
4484 == current_block);
4485 if (p_block_stack->is_empty ())
4486 super = current_block;
4487 else
4488 {
4489 super = p_block_stack->last ();
4490 gcc_assert (super == current_block
4491 || BLOCK_FRAGMENT_ORIGIN (super)
4492 == current_block);
4493 }
4494 BLOCK_SUPERCONTEXT (block) = super;
4495 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4496 BLOCK_SUBBLOCKS (current_block) = block;
4497 current_block = origin;
4498 }
4499 p_block_stack->safe_push (obj: block);
4500 }
4501 else if (NOTE_KIND (insn) == NOTE_INSN_BLOCK_END)
4502 {
4503 NOTE_BLOCK (insn) = p_block_stack->pop ();
4504 current_block = BLOCK_SUPERCONTEXT (current_block);
4505 if (BLOCK_FRAGMENT_ORIGIN (current_block))
4506 current_block = BLOCK_FRAGMENT_ORIGIN (current_block);
4507 prev_beg = NULL_TREE;
4508 prev_end = BLOCK_SAME_RANGE (NOTE_BLOCK (insn))
4509 ? NOTE_BLOCK (insn) : NULL_TREE;
4510 }
4511 }
4512 else
4513 {
4514 prev_beg = NULL_TREE;
4515 if (prev_end)
4516 BLOCK_SAME_RANGE (prev_end) = 0;
4517 prev_end = NULL_TREE;
4518 }
4519 }
4520}
4521
4522/* Reverse the order of elements in the chain T of blocks,
4523 and return the new head of the chain (old last element). */
4524
4525tree
4526blocks_nreverse (tree t)
4527{
4528 tree prev = 0, block, next;
4529 for (block = t; block; block = next)
4530 {
4531 next = BLOCK_CHAIN (block);
4532 BLOCK_CHAIN (block) = prev;
4533 prev = block;
4534 }
4535 return prev;
4536}
4537
4538/* Concatenate two chains of blocks (chained through BLOCK_CHAIN)
4539 by modifying the last node in chain 1 to point to chain 2. */
4540
4541tree
4542block_chainon (tree op1, tree op2)
4543{
4544 tree t1;
4545
4546 if (!op1)
4547 return op2;
4548 if (!op2)
4549 return op1;
4550
4551 for (t1 = op1; BLOCK_CHAIN (t1); t1 = BLOCK_CHAIN (t1))
4552 continue;
4553 BLOCK_CHAIN (t1) = op2;
4554
4555#ifdef ENABLE_TREE_CHECKING
4556 {
4557 tree t2;
4558 for (t2 = op2; t2; t2 = BLOCK_CHAIN (t2))
4559 gcc_assert (t2 != t1);
4560 }
4561#endif
4562
4563 return op1;
4564}
4565
4566/* Count the subblocks of the list starting with BLOCK. If VECTOR is
4567 non-NULL, list them all into VECTOR, in a depth-first preorder
4568 traversal of the block tree. Also clear TREE_ASM_WRITTEN in all
4569 blocks. */
4570
4571static int
4572all_blocks (tree block, tree *vector)
4573{
4574 int n_blocks = 0;
4575
4576 while (block)
4577 {
4578 TREE_ASM_WRITTEN (block) = 0;
4579
4580 /* Record this block. */
4581 if (vector)
4582 vector[n_blocks] = block;
4583
4584 ++n_blocks;
4585
4586 /* Record the subblocks, and their subblocks... */
4587 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4588 vector: vector ? vector + n_blocks : 0);
4589 block = BLOCK_CHAIN (block);
4590 }
4591
4592 return n_blocks;
4593}
4594
4595/* Return a vector containing all the blocks rooted at BLOCK. The
4596 number of elements in the vector is stored in N_BLOCKS_P. The
4597 vector is dynamically allocated; it is the caller's responsibility
4598 to call `free' on the pointer returned. */
4599
4600static tree *
4601get_block_vector (tree block, int *n_blocks_p)
4602{
4603 tree *block_vector;
4604
4605 *n_blocks_p = all_blocks (block, NULL);
4606 block_vector = XNEWVEC (tree, *n_blocks_p);
4607 all_blocks (block, vector: block_vector);
4608
4609 return block_vector;
4610}
4611
4612static GTY(()) int next_block_index = 2;
4613
4614/* Set BLOCK_NUMBER for all the blocks in FN. */
4615
4616void
4617number_blocks (tree fn)
4618{
4619 int i;
4620 int n_blocks;
4621 tree *block_vector;
4622
4623 block_vector = get_block_vector (DECL_INITIAL (fn), n_blocks_p: &n_blocks);
4624
4625 /* The top-level BLOCK isn't numbered at all. */
4626 for (i = 1; i < n_blocks; ++i)
4627 /* We number the blocks from two. */
4628 BLOCK_NUMBER (block_vector[i]) = next_block_index++;
4629
4630 free (ptr: block_vector);
4631
4632 return;
4633}
4634
4635/* If VAR is present in a subblock of BLOCK, return the subblock. */
4636
4637DEBUG_FUNCTION tree
4638debug_find_var_in_block_tree (tree var, tree block)
4639{
4640 tree t;
4641
4642 for (t = BLOCK_VARS (block); t; t = TREE_CHAIN (t))
4643 if (t == var)
4644 return block;
4645
4646 for (t = BLOCK_SUBBLOCKS (block); t; t = TREE_CHAIN (t))
4647 {
4648 tree ret = debug_find_var_in_block_tree (var, block: t);
4649 if (ret)
4650 return ret;
4651 }
4652
4653 return NULL_TREE;
4654}
4655
4656/* Keep track of whether we're in a dummy function context. If we are,
4657 we don't want to invoke the set_current_function hook, because we'll
4658 get into trouble if the hook calls target_reinit () recursively or
4659 when the initial initialization is not yet complete. */
4660
4661static bool in_dummy_function;
4662
4663/* Invoke the target hook when setting cfun. Update the optimization options
4664 if the function uses different options than the default. */
4665
4666static void
4667invoke_set_current_function_hook (tree fndecl)
4668{
4669 if (!in_dummy_function)
4670 {
4671 tree opts = ((fndecl)
4672 ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
4673 : optimization_default_node);
4674
4675 if (!opts)
4676 opts = optimization_default_node;
4677
4678 /* Change optimization options if needed. */
4679 if (optimization_current_node != opts)
4680 {
4681 optimization_current_node = opts;
4682 cl_optimization_restore (&global_options, &global_options_set,
4683 TREE_OPTIMIZATION (opts));
4684 }
4685
4686 targetm.set_current_function (fndecl);
4687 this_fn_optabs = this_target_optabs;
4688
4689 /* Initialize global alignment variables after op. */
4690 parse_alignment_opts ();
4691
4692 if (opts != optimization_default_node)
4693 {
4694 init_tree_optimization_optabs (opts);
4695 if (TREE_OPTIMIZATION_OPTABS (opts))
4696 this_fn_optabs = (struct target_optabs *)
4697 TREE_OPTIMIZATION_OPTABS (opts);
4698 }
4699 }
4700}
4701
4702/* Set cfun to NEW_CFUN and switch to the optimization and target options
4703 associated with NEW_FNDECL.
4704
4705 FORCE says whether we should do the switch even if NEW_CFUN is the current
4706 function, e.g. because there has been a change in optimization or target
4707 options. */
4708
4709static void
4710set_function_decl (function *new_cfun, tree new_fndecl, bool force)
4711{
4712 if (cfun != new_cfun || force)
4713 {
4714 cfun = new_cfun;
4715 invoke_set_current_function_hook (fndecl: new_fndecl);
4716 redirect_edge_var_map_empty ();
4717 }
4718}
4719
4720/* cfun should never be set directly; use this function. */
4721
4722void
4723set_cfun (struct function *new_cfun, bool force)
4724{
4725 set_function_decl (new_cfun, new_fndecl: new_cfun ? new_cfun->decl : NULL_TREE, force);
4726}
4727
4728/* Initialized with NOGC, making this poisonous to the garbage collector. */
4729
4730static vec<function *> cfun_stack;
4731
4732/* Push the current cfun onto the stack, then switch to function NEW_CFUN
4733 and FUNCTION_DECL NEW_FNDECL. FORCE is as for set_function_decl. */
4734
4735static void
4736push_function_decl (function *new_cfun, tree new_fndecl, bool force)
4737{
4738 gcc_assert ((!cfun && !current_function_decl)
4739 || (cfun && current_function_decl == cfun->decl));
4740 cfun_stack.safe_push (obj: cfun);
4741 current_function_decl = new_fndecl;
4742 set_function_decl (new_cfun, new_fndecl, force);
4743}
4744
4745/* Push the current cfun onto the stack and switch to function declaration
4746 NEW_FNDECL, which might or might not have a function body. FORCE is as for
4747 set_function_decl. */
4748
4749void
4750push_function_decl (tree new_fndecl, bool force)
4751{
4752 force |= current_function_decl != new_fndecl;
4753 push_function_decl (DECL_STRUCT_FUNCTION (new_fndecl), new_fndecl, force);
4754}
4755
4756/* Push the current cfun onto the stack, and set cfun to new_cfun. Also set
4757 current_function_decl accordingly. */
4758
4759void
4760push_cfun (struct function *new_cfun)
4761{
4762 push_function_decl (new_cfun, new_fndecl: new_cfun ? new_cfun->decl : NULL_TREE, force: false);
4763}
4764
4765/* A common subroutine for pop_cfun and pop_function_decl. FORCE is as
4766 for set_function_decl. */
4767
4768static void
4769pop_cfun_1 (bool force)
4770{
4771 struct function *new_cfun = cfun_stack.pop ();
4772 /* When in_dummy_function, we do have a cfun but current_function_decl is
4773 NULL. We also allow pushing NULL cfun and subsequently changing
4774 current_function_decl to something else and have both restored by
4775 pop_cfun. */
4776 gcc_checking_assert (in_dummy_function
4777 || !cfun
4778 || current_function_decl == cfun->decl);
4779 set_cfun (new_cfun, force);
4780 current_function_decl = new_cfun ? new_cfun->decl : NULL_TREE;
4781}
4782
4783/* Pop cfun from the stack. Also set current_function_decl accordingly. */
4784
4785void
4786pop_cfun (void)
4787{
4788 pop_cfun_1 (force: false);
4789}
4790
4791/* Undo push_function_decl. */
4792
4793void
4794pop_function_decl (void)
4795{
4796 /* If the previous cfun was null, the options should be reset to the
4797 global set. Checking the current cfun against the new (popped) cfun
4798 wouldn't catch this if the current function decl has no function
4799 struct. */
4800 pop_cfun_1 (force: !cfun_stack.last ());
4801}
4802
4803/* Return value of funcdef and increase it. */
4804int
4805get_next_funcdef_no (void)
4806{
4807 return funcdef_no++;
4808}
4809
4810/* Return value of funcdef. */
4811int
4812get_last_funcdef_no (void)
4813{
4814 return funcdef_no;
4815}
4816
4817/* Allocate and initialize the stack usage info data structure for the
4818 current function. */
4819static void
4820allocate_stack_usage_info (void)
4821{
4822 gcc_assert (!cfun->su);
4823 cfun->su = ggc_cleared_alloc<stack_usage> ();
4824 cfun->su->static_stack_size = -1;
4825}
4826
4827/* Allocate a function structure for FNDECL and set its contents
4828 to the defaults. Set cfun to the newly-allocated object.
4829 Some of the helper functions invoked during initialization assume
4830 that cfun has already been set. Therefore, assign the new object
4831 directly into cfun and invoke the back end hook explicitly at the
4832 very end, rather than initializing a temporary and calling set_cfun
4833 on it.
4834
4835 ABSTRACT_P is true if this is a function that will never be seen by
4836 the middle-end. Such functions are front-end concepts (like C++
4837 function templates) that do not correspond directly to functions
4838 placed in object files. */
4839
4840void
4841allocate_struct_function (tree fndecl, bool abstract_p)
4842{
4843 tree fntype = fndecl ? TREE_TYPE (fndecl) : NULL_TREE;
4844
4845 cfun = ggc_cleared_alloc<function> ();
4846
4847 init_eh_for_function ();
4848
4849 if (init_machine_status)
4850 cfun->machine = (*init_machine_status) ();
4851
4852#ifdef OVERRIDE_ABI_FORMAT
4853 OVERRIDE_ABI_FORMAT (fndecl);
4854#endif
4855
4856 if (fndecl != NULL_TREE)
4857 {
4858 DECL_STRUCT_FUNCTION (fndecl) = cfun;
4859 cfun->decl = fndecl;
4860 current_function_funcdef_no = get_next_funcdef_no ();
4861 }
4862
4863 invoke_set_current_function_hook (fndecl);
4864
4865 if (fndecl != NULL_TREE)
4866 {
4867 tree result = DECL_RESULT (fndecl);
4868
4869 if (!abstract_p)
4870 {
4871 /* Now that we have activated any function-specific attributes
4872 that might affect layout, particularly vector modes, relayout
4873 each of the parameters and the result. */
4874 relayout_decl (result);
4875 for (tree parm = DECL_ARGUMENTS (fndecl); parm;
4876 parm = DECL_CHAIN (parm))
4877 relayout_decl (parm);
4878
4879 /* Similarly relayout the function decl. */
4880 targetm.target_option.relayout_function (fndecl);
4881 }
4882
4883 if (!abstract_p && aggregate_value_p (exp: result, fntype: fndecl))
4884 {
4885#ifdef PCC_STATIC_STRUCT_RETURN
4886 cfun->returns_pcc_struct = 1;
4887#endif
4888 cfun->returns_struct = 1;
4889 }
4890
4891 cfun->stdarg = stdarg_p (fntype);
4892
4893 /* Assume all registers in stdarg functions need to be saved. */
4894 cfun->va_list_gpr_size = VA_LIST_MAX_GPR_SIZE;
4895 cfun->va_list_fpr_size = VA_LIST_MAX_FPR_SIZE;
4896
4897 /* ??? This could be set on a per-function basis by the front-end
4898 but is this worth the hassle? */
4899 cfun->can_throw_non_call_exceptions = flag_non_call_exceptions;
4900 cfun->can_delete_dead_exceptions = flag_delete_dead_exceptions;
4901
4902 if (!profile_flag && !flag_instrument_function_entry_exit)
4903 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) = 1;
4904
4905 if (flag_callgraph_info)
4906 allocate_stack_usage_info ();
4907 }
4908
4909 /* Don't enable begin stmt markers if var-tracking at assignments is
4910 disabled. The markers make little sense without the variable
4911 binding annotations among them. */
4912 cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
4913 && MAY_HAVE_DEBUG_MARKER_STMTS;
4914}
4915
4916/* This is like allocate_struct_function, but pushes a new cfun for FNDECL
4917 instead of just setting it. */
4918
4919void
4920push_struct_function (tree fndecl, bool abstract_p)
4921{
4922 /* When in_dummy_function we might be in the middle of a pop_cfun and
4923 current_function_decl and cfun may not match. */
4924 gcc_assert (in_dummy_function
4925 || (!cfun && !current_function_decl)
4926 || (cfun && current_function_decl == cfun->decl));
4927 cfun_stack.safe_push (obj: cfun);
4928 current_function_decl = fndecl;
4929 allocate_struct_function (fndecl, abstract_p);
4930}
4931
4932/* Reset crtl and other non-struct-function variables to defaults as
4933 appropriate for emitting rtl at the start of a function. */
4934
4935static void
4936prepare_function_start (void)
4937{
4938 gcc_assert (!get_last_insn ());
4939
4940 if (in_dummy_function)
4941 crtl->abi = &default_function_abi;
4942 else
4943 crtl->abi = &fndecl_abi (cfun->decl).base_abi ();
4944
4945 init_temp_slots ();
4946 init_emit ();
4947 init_varasm_status ();
4948 init_expr ();
4949 default_rtl_profile ();
4950
4951 if (flag_stack_usage_info && !flag_callgraph_info)
4952 allocate_stack_usage_info ();
4953
4954 cse_not_expected = ! optimize;
4955
4956 /* Caller save not needed yet. */
4957 caller_save_needed = 0;
4958
4959 /* We haven't done register allocation yet. */
4960 reg_renumber = 0;
4961
4962 /* Indicate that we have not instantiated virtual registers yet. */
4963 virtuals_instantiated = 0;
4964
4965 /* Indicate that we want CONCATs now. */
4966 generating_concat_p = 1;
4967
4968 /* Indicate we have no need of a frame pointer yet. */
4969 frame_pointer_needed = 0;
4970}
4971
4972void
4973push_dummy_function (bool with_decl)
4974{
4975 tree fn_decl, fn_type, fn_result_decl;
4976
4977 gcc_assert (!in_dummy_function);
4978 in_dummy_function = true;
4979
4980 if (with_decl)
4981 {
4982 fn_type = build_function_type_list (void_type_node, NULL_TREE);
4983 fn_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
4984 fn_type);
4985 fn_result_decl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
4986 NULL_TREE, void_type_node);
4987 DECL_RESULT (fn_decl) = fn_result_decl;
4988 DECL_ARTIFICIAL (fn_decl) = 1;
4989 tree fn_name = get_identifier (" ");
4990 SET_DECL_ASSEMBLER_NAME (fn_decl, fn_name);
4991 }
4992 else
4993 fn_decl = NULL_TREE;
4994
4995 push_struct_function (fndecl: fn_decl);
4996}
4997
4998/* Initialize the rtl expansion mechanism so that we can do simple things
4999 like generate sequences. This is used to provide a context during global
5000 initialization of some passes. You must call expand_dummy_function_end
5001 to exit this context. */
5002
5003void
5004init_dummy_function_start (void)
5005{
5006 push_dummy_function (with_decl: false);
5007 prepare_function_start ();
5008}
5009
5010/* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
5011 and initialize static variables for generating RTL for the statements
5012 of the function. */
5013
5014void
5015init_function_start (tree subr)
5016{
5017 /* Initialize backend, if needed. */
5018 initialize_rtl ();
5019
5020 prepare_function_start ();
5021 decide_function_section (subr);
5022
5023 /* Warn if this value is an aggregate type,
5024 regardless of which calling convention we are using for it. */
5025 if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
5026 warning_at (DECL_SOURCE_LOCATION (DECL_RESULT (subr)),
5027 OPT_Waggregate_return, "function returns an aggregate");
5028}
5029
5030/* Expand code to verify the stack_protect_guard. This is invoked at
5031 the end of a function to be protected. */
5032
5033void
5034stack_protect_epilogue (void)
5035{
5036 tree guard_decl = crtl->stack_protect_guard_decl;
5037 rtx_code_label *label = gen_label_rtx ();
5038 rtx x, y;
5039 rtx_insn *seq = NULL;
5040
5041 x = expand_normal (crtl->stack_protect_guard);
5042
5043 if (targetm.have_stack_protect_combined_test () && guard_decl)
5044 {
5045 gcc_assert (DECL_P (guard_decl));
5046 y = DECL_RTL (guard_decl);
5047 /* Allow the target to compute address of Y and compare it with X without
5048 leaking Y into a register. This combined address + compare pattern
5049 allows the target to prevent spilling of any intermediate results by
5050 splitting it after register allocator. */
5051 seq = targetm.gen_stack_protect_combined_test (x, y, label);
5052 }
5053 else
5054 {
5055 if (guard_decl)
5056 y = expand_normal (exp: guard_decl);
5057 else
5058 y = const0_rtx;
5059
5060 /* Allow the target to compare Y with X without leaking either into
5061 a register. */
5062 if (targetm.have_stack_protect_test ())
5063 seq = targetm.gen_stack_protect_test (x, y, label);
5064 }
5065
5066 if (seq)
5067 emit_insn (seq);
5068 else
5069 emit_cmp_and_jump_insns (x, y, EQ, NULL_RTX, ptr_mode, 1, label);
5070
5071 /* The noreturn predictor has been moved to the tree level. The rtl-level
5072 predictors estimate this branch about 20%, which isn't enough to get
5073 things moved out of line. Since this is the only extant case of adding
5074 a noreturn function at the rtl level, it doesn't seem worth doing ought
5075 except adding the prediction by hand. */
5076 rtx_insn *tmp = get_last_insn ();
5077 if (JUMP_P (tmp))
5078 predict_insn_def (tmp, PRED_NORETURN, TAKEN);
5079
5080 expand_call (targetm.stack_protect_fail (), NULL_RTX, /*ignore=*/true);
5081 free_temp_slots ();
5082 emit_label (label);
5083}
5084
5085/* Start the RTL for a new function, and set variables used for
5086 emitting RTL.
5087 SUBR is the FUNCTION_DECL node.
5088 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5089 the function's parameters, which must be run at any return statement. */
5090
5091bool currently_expanding_function_start;
5092void
5093expand_function_start (tree subr)
5094{
5095 currently_expanding_function_start = true;
5096
5097 /* Make sure volatile mem refs aren't considered
5098 valid operands of arithmetic insns. */
5099 init_recog_no_volatile ();
5100
5101 crtl->profile
5102 = (profile_flag
5103 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr));
5104
5105 crtl->limit_stack
5106 = (stack_limit_rtx != NULL_RTX && ! DECL_NO_LIMIT_STACK (subr));
5107
5108 /* Make the label for return statements to jump to. Do not special
5109 case machines with special return instructions -- they will be
5110 handled later during jump, ifcvt, or epilogue creation. */
5111 return_label = gen_label_rtx ();
5112
5113 /* Initialize rtx used to return the value. */
5114 /* Do this before assign_parms so that we copy the struct value address
5115 before any library calls that assign parms might generate. */
5116
5117 /* Decide whether to return the value in memory or in a register. */
5118 tree res = DECL_RESULT (subr);
5119 if (aggregate_value_p (exp: res, fntype: subr))
5120 {
5121 /* Returning something that won't go in a register. */
5122 rtx value_address = 0;
5123
5124#ifdef PCC_STATIC_STRUCT_RETURN
5125 if (cfun->returns_pcc_struct)
5126 {
5127 int size = int_size_in_bytes (TREE_TYPE (res));
5128 value_address = assemble_static_space (size);
5129 }
5130 else
5131#endif
5132 {
5133 rtx sv = targetm.calls.struct_value_rtx (TREE_TYPE (subr), 2);
5134 /* Expect to be passed the address of a place to store the value.
5135 If it is passed as an argument, assign_parms will take care of
5136 it. */
5137 if (sv)
5138 {
5139 value_address = gen_reg_rtx (Pmode);
5140 emit_move_insn (value_address, sv);
5141 }
5142 }
5143 if (value_address)
5144 {
5145 rtx x = value_address;
5146 if (!DECL_BY_REFERENCE (res))
5147 {
5148 x = gen_rtx_MEM (DECL_MODE (res), x);
5149 set_mem_attributes (x, res, 1);
5150 }
5151 set_parm_rtl (res, x);
5152 }
5153 }
5154 else if (DECL_MODE (res) == VOIDmode)
5155 /* If return mode is void, this decl rtl should not be used. */
5156 set_parm_rtl (res, NULL_RTX);
5157 else
5158 {
5159 /* Compute the return values into a pseudo reg, which we will copy
5160 into the true return register after the cleanups are done. */
5161 tree return_type = TREE_TYPE (res);
5162
5163 /* If we may coalesce this result, make sure it has the expected mode
5164 in case it was promoted. But we need not bother about BLKmode. */
5165 machine_mode promoted_mode
5166 = flag_tree_coalesce_vars && is_gimple_reg (res)
5167 ? promote_ssa_mode (ssa_default_def (cfun, res), NULL)
5168 : BLKmode;
5169
5170 if (promoted_mode != BLKmode)
5171 set_parm_rtl (res, gen_reg_rtx (promoted_mode));
5172 else if (TYPE_MODE (return_type) != BLKmode
5173 && targetm.calls.return_in_msb (return_type))
5174 /* expand_function_end will insert the appropriate padding in
5175 this case. Use the return value's natural (unpadded) mode
5176 within the function proper. */
5177 set_parm_rtl (res, gen_reg_rtx (TYPE_MODE (return_type)));
5178 else
5179 {
5180 /* In order to figure out what mode to use for the pseudo, we
5181 figure out what the mode of the eventual return register will
5182 actually be, and use that. */
5183 rtx hard_reg = hard_function_value (return_type, subr, 0, 1);
5184
5185 /* Structures that are returned in registers are not
5186 aggregate_value_p, so we may see a PARALLEL or a REG. */
5187 if (REG_P (hard_reg))
5188 set_parm_rtl (res, gen_reg_rtx (GET_MODE (hard_reg)));
5189 else
5190 {
5191 gcc_assert (GET_CODE (hard_reg) == PARALLEL);
5192 set_parm_rtl (res, gen_group_rtx (hard_reg));
5193 }
5194 }
5195
5196 /* Set DECL_REGISTER flag so that expand_function_end will copy the
5197 result to the real return register(s). */
5198 DECL_REGISTER (res) = 1;
5199 }
5200
5201 /* Initialize rtx for parameters and local variables.
5202 In some cases this requires emitting insns. */
5203 assign_parms (fndecl: subr);
5204
5205 /* If function gets a static chain arg, store it. */
5206 if (cfun->static_chain_decl)
5207 {
5208 tree parm = cfun->static_chain_decl;
5209 rtx local, chain;
5210 rtx_insn *insn;
5211 int unsignedp;
5212
5213 local = gen_reg_rtx (promote_decl_mode (parm, &unsignedp));
5214 chain = targetm.calls.static_chain (current_function_decl, true);
5215
5216 set_decl_incoming_rtl (parm, chain, false);
5217 set_parm_rtl (parm, local);
5218 mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
5219
5220 if (GET_MODE (local) != GET_MODE (chain))
5221 {
5222 convert_move (local, chain, unsignedp);
5223 insn = get_last_insn ();
5224 }
5225 else
5226 insn = emit_move_insn (local, chain);
5227
5228 /* Mark the register as eliminable, similar to parameters. */
5229 if (MEM_P (chain)
5230 && reg_mentioned_p (arg_pointer_rtx, XEXP (chain, 0)))
5231 set_dst_reg_note (insn, REG_EQUIV, chain, local);
5232
5233 /* If we aren't optimizing, save the static chain onto the stack. */
5234 if (!optimize)
5235 {
5236 tree saved_static_chain_decl
5237 = build_decl (DECL_SOURCE_LOCATION (parm), VAR_DECL,
5238 DECL_NAME (parm), TREE_TYPE (parm));
5239 rtx saved_static_chain_rtx
5240 = assign_stack_local (Pmode, size: GET_MODE_SIZE (Pmode), align: 0);
5241 SET_DECL_RTL (saved_static_chain_decl, saved_static_chain_rtx);
5242 emit_move_insn (saved_static_chain_rtx, chain);
5243 SET_DECL_VALUE_EXPR (parm, saved_static_chain_decl);
5244 DECL_HAS_VALUE_EXPR_P (parm) = 1;
5245 }
5246 }
5247
5248 /* The following was moved from init_function_start.
5249 The move was supposed to make sdb output more accurate. */
5250 /* Indicate the beginning of the function body,
5251 as opposed to parm setup. */
5252 emit_note (NOTE_INSN_FUNCTION_BEG);
5253
5254 gcc_assert (NOTE_P (get_last_insn ()));
5255
5256 function_beg_insn = parm_birth_insn = get_last_insn ();
5257
5258 /* If the function receives a non-local goto, then store the
5259 bits we need to restore the frame pointer. */
5260 if (cfun->nonlocal_goto_save_area)
5261 {
5262 tree t_save;
5263 rtx r_save;
5264
5265 tree var = TREE_OPERAND (cfun->nonlocal_goto_save_area, 0);
5266 gcc_assert (DECL_RTL_SET_P (var));
5267
5268 t_save = build4 (ARRAY_REF,
5269 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
5270 cfun->nonlocal_goto_save_area,
5271 integer_zero_node, NULL_TREE, NULL_TREE);
5272 r_save = expand_expr (exp: t_save, NULL_RTX, VOIDmode, modifier: EXPAND_WRITE);
5273 gcc_assert (GET_MODE (r_save) == Pmode);
5274
5275 emit_move_insn (r_save, hard_frame_pointer_rtx);
5276 update_nonlocal_goto_save_area ();
5277 }
5278
5279 if (crtl->profile)
5280 {
5281#ifdef PROFILE_HOOK
5282 PROFILE_HOOK (current_function_funcdef_no);
5283#endif
5284 }
5285
5286 /* If we are doing generic stack checking, the probe should go here. */
5287 if (flag_stack_check == GENERIC_STACK_CHECK)
5288 stack_check_probe_note = emit_note (NOTE_INSN_DELETED);
5289
5290 currently_expanding_function_start = false;
5291}
5292
5293void
5294pop_dummy_function (void)
5295{
5296 pop_cfun ();
5297 in_dummy_function = false;
5298}
5299
5300/* Undo the effects of init_dummy_function_start. */
5301void
5302expand_dummy_function_end (void)
5303{
5304 gcc_assert (in_dummy_function);
5305
5306 /* End any sequences that failed to be closed due to syntax errors. */
5307 while (in_sequence_p ())
5308 end_sequence ();
5309
5310 /* Outside function body, can't compute type's actual size
5311 until next function's body starts. */
5312
5313 free_after_parsing (f: cfun);
5314 free_after_compilation (f: cfun);
5315 pop_dummy_function ();
5316}
5317
5318/* Helper for diddle_return_value. */
5319
5320void
5321diddle_return_value_1 (void (*doit) (rtx, void *), void *arg, rtx outgoing)
5322{
5323 if (! outgoing)
5324 return;
5325
5326 if (REG_P (outgoing))
5327 (*doit) (outgoing, arg);
5328 else if (GET_CODE (outgoing) == PARALLEL)
5329 {
5330 int i;
5331
5332 for (i = 0; i < XVECLEN (outgoing, 0); i++)
5333 {
5334 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
5335
5336 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
5337 (*doit) (x, arg);
5338 }
5339 }
5340}
5341
5342/* Call DOIT for each hard register used as a return value from
5343 the current function. */
5344
5345void
5346diddle_return_value (void (*doit) (rtx, void *), void *arg)
5347{
5348 diddle_return_value_1 (doit, arg, crtl->return_rtx);
5349}
5350
5351static void
5352do_clobber_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5353{
5354 emit_clobber (reg);
5355}
5356
5357void
5358clobber_return_register (void)
5359{
5360 diddle_return_value (doit: do_clobber_return_reg, NULL);
5361
5362 /* In case we do use pseudo to return value, clobber it too. */
5363 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5364 {
5365 tree decl_result = DECL_RESULT (current_function_decl);
5366 rtx decl_rtl = DECL_RTL (decl_result);
5367 if (REG_P (decl_rtl) && REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER)
5368 {
5369 do_clobber_return_reg (reg: decl_rtl, NULL);
5370 }
5371 }
5372}
5373
5374static void
5375do_use_return_reg (rtx reg, void *arg ATTRIBUTE_UNUSED)
5376{
5377 emit_use (reg);
5378}
5379
5380static void
5381use_return_register (void)
5382{
5383 diddle_return_value (doit: do_use_return_reg, NULL);
5384}
5385
5386/* Generate RTL for the end of the current function. */
5387
5388void
5389expand_function_end (void)
5390{
5391 /* If arg_pointer_save_area was referenced only from a nested
5392 function, we will not have initialized it yet. Do that now. */
5393 if (arg_pointer_save_area && ! crtl->arg_pointer_save_area_init)
5394 get_arg_pointer_save_area ();
5395
5396 /* If we are doing generic stack checking and this function makes calls,
5397 do a stack probe at the start of the function to ensure we have enough
5398 space for another stack frame. */
5399 if (flag_stack_check == GENERIC_STACK_CHECK)
5400 {
5401 rtx_insn *insn, *seq;
5402
5403 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5404 if (CALL_P (insn))
5405 {
5406 rtx max_frame_size = GEN_INT (STACK_CHECK_MAX_FRAME_SIZE);
5407 start_sequence ();
5408 if (STACK_CHECK_MOVING_SP)
5409 anti_adjust_stack_and_probe (max_frame_size, true);
5410 else
5411 probe_stack_range (STACK_OLD_CHECK_PROTECT, max_frame_size);
5412 seq = end_sequence ();
5413 set_insn_locations (seq, prologue_location);
5414 emit_insn_before (seq, stack_check_probe_note);
5415 break;
5416 }
5417 }
5418
5419 /* End any sequences that failed to be closed due to syntax errors. */
5420 while (in_sequence_p ())
5421 end_sequence ();
5422
5423 clear_pending_stack_adjust ();
5424 do_pending_stack_adjust ();
5425
5426 /* Output a linenumber for the end of the function.
5427 SDB depended on this. */
5428 set_curr_insn_location (input_location);
5429
5430 /* Before the return label (if any), clobber the return
5431 registers so that they are not propagated live to the rest of
5432 the function. This can only happen with functions that drop
5433 through; if there had been a return statement, there would
5434 have either been a return rtx, or a jump to the return label.
5435
5436 We delay actual code generation after the current_function_value_rtx
5437 is computed. */
5438 rtx_insn *clobber_after = get_last_insn ();
5439
5440 /* Output the label for the actual return from the function. */
5441 emit_label (return_label);
5442
5443 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
5444 {
5445 /* Let except.cc know where it should emit the call to unregister
5446 the function context for sjlj exceptions. */
5447 if (flag_exceptions)
5448 sjlj_emit_function_exit_after (get_last_insn ());
5449 }
5450
5451 /* If this is an implementation of throw, do what's necessary to
5452 communicate between __builtin_eh_return and the epilogue. */
5453 expand_eh_return ();
5454
5455 /* If stack protection is enabled for this function, check the guard. */
5456 if (crtl->stack_protect_guard
5457 && targetm.stack_protect_runtime_enabled_p ()
5458 && naked_return_label == NULL_RTX)
5459 stack_protect_epilogue ();
5460
5461 /* If scalar return value was computed in a pseudo-reg, or was a named
5462 return value that got dumped to the stack, copy that to the hard
5463 return register. */
5464 if (DECL_RTL_SET_P (DECL_RESULT (current_function_decl)))
5465 {
5466 tree decl_result = DECL_RESULT (current_function_decl);
5467 rtx decl_rtl = DECL_RTL (decl_result);
5468
5469 if ((REG_P (decl_rtl)
5470 ? REGNO (decl_rtl) >= FIRST_PSEUDO_REGISTER
5471 : DECL_REGISTER (decl_result))
5472 /* Unless the psABI says not to. */
5473 && !TYPE_EMPTY_P (TREE_TYPE (decl_result)))
5474 {
5475 rtx real_decl_rtl = crtl->return_rtx;
5476 complex_mode cmode;
5477
5478 /* This should be set in assign_parms. */
5479 gcc_assert (REG_FUNCTION_VALUE_P (real_decl_rtl));
5480
5481 /* If this is a BLKmode structure being returned in registers,
5482 then use the mode computed in expand_return. Note that if
5483 decl_rtl is memory, then its mode may have been changed,
5484 but that crtl->return_rtx has not. */
5485 if (GET_MODE (real_decl_rtl) == BLKmode)
5486 PUT_MODE (x: real_decl_rtl, GET_MODE (decl_rtl));
5487
5488 /* If a non-BLKmode return value should be padded at the least
5489 significant end of the register, shift it left by the appropriate
5490 amount. BLKmode results are handled using the group load/store
5491 machinery. */
5492 if (TYPE_MODE (TREE_TYPE (decl_result)) != BLKmode
5493 && REG_P (real_decl_rtl)
5494 && targetm.calls.return_in_msb (TREE_TYPE (decl_result)))
5495 {
5496 emit_move_insn (gen_rtx_REG (GET_MODE (decl_rtl),
5497 REGNO (real_decl_rtl)),
5498 decl_rtl);
5499 shift_return_value (GET_MODE (decl_rtl), true, real_decl_rtl);
5500 }
5501 else if (GET_CODE (real_decl_rtl) == PARALLEL)
5502 {
5503 /* If expand_function_start has created a PARALLEL for decl_rtl,
5504 move the result to the real return registers. Otherwise, do
5505 a group load from decl_rtl for a named return. */
5506 if (GET_CODE (decl_rtl) == PARALLEL)
5507 emit_group_move (real_decl_rtl, decl_rtl);
5508 else
5509 emit_group_load (real_decl_rtl, decl_rtl,
5510 TREE_TYPE (decl_result),
5511 int_size_in_bytes (TREE_TYPE (decl_result)));
5512 }
5513 /* In the case of complex integer modes smaller than a word, we'll
5514 need to generate some non-trivial bitfield insertions. Do that
5515 on a pseudo and not the hard register. */
5516 else if (GET_CODE (decl_rtl) == CONCAT
5517 && is_complex_int_mode (GET_MODE (decl_rtl), cmode: &cmode)
5518 && GET_MODE_BITSIZE (mode: cmode) <= BITS_PER_WORD)
5519 {
5520 int old_generating_concat_p;
5521 rtx tmp;
5522
5523 old_generating_concat_p = generating_concat_p;
5524 generating_concat_p = 0;
5525 tmp = gen_reg_rtx (GET_MODE (decl_rtl));
5526 generating_concat_p = old_generating_concat_p;
5527
5528 emit_move_insn (tmp, decl_rtl);
5529 emit_move_insn (real_decl_rtl, tmp);
5530 }
5531 /* If a named return value dumped decl_return to memory, then
5532 we may need to re-do the PROMOTE_MODE signed/unsigned
5533 extension. */
5534 else if (GET_MODE (real_decl_rtl) != GET_MODE (decl_rtl))
5535 {
5536 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (decl_result));
5537 promote_function_mode (TREE_TYPE (decl_result),
5538 GET_MODE (decl_rtl), &unsignedp,
5539 TREE_TYPE (current_function_decl), 1);
5540
5541 convert_move (real_decl_rtl, decl_rtl, unsignedp);
5542 }
5543 else
5544 emit_move_insn (real_decl_rtl, decl_rtl);
5545 }
5546 }
5547
5548 /* If returning a structure, arrange to return the address of the value
5549 in a place where debuggers expect to find it.
5550
5551 If returning a structure PCC style,
5552 the caller also depends on this value.
5553 And cfun->returns_pcc_struct is not necessarily set. */
5554 if ((cfun->returns_struct || cfun->returns_pcc_struct)
5555 && !targetm.calls.omit_struct_return_reg)
5556 {
5557 rtx value_address = DECL_RTL (DECL_RESULT (current_function_decl));
5558 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5559 rtx outgoing;
5560
5561 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
5562 type = TREE_TYPE (type);
5563 else
5564 value_address = XEXP (value_address, 0);
5565
5566 outgoing = targetm.calls.function_value (build_pointer_type (type),
5567 current_function_decl, true);
5568
5569 /* Mark this as a function return value so integrate will delete the
5570 assignment and USE below when inlining this function. */
5571 REG_FUNCTION_VALUE_P (outgoing) = 1;
5572
5573 /* The address may be ptr_mode and OUTGOING may be Pmode. */
5574 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (outgoing));
5575 value_address = convert_memory_address (mode, value_address);
5576
5577 emit_move_insn (outgoing, value_address);
5578
5579 /* Show return register used to hold result (in this case the address
5580 of the result. */
5581 crtl->return_rtx = outgoing;
5582 }
5583
5584 /* Emit the actual code to clobber return register. Don't emit
5585 it if clobber_after is a barrier, then the previous basic block
5586 certainly doesn't fall thru into the exit block. */
5587 if (!BARRIER_P (clobber_after))
5588 {
5589 start_sequence ();
5590 clobber_return_register ();
5591 rtx_insn *seq = end_sequence ();
5592
5593 emit_insn_after (seq, clobber_after);
5594 }
5595
5596 /* Output the label for the naked return from the function. */
5597 if (naked_return_label)
5598 emit_label (naked_return_label);
5599
5600 /* @@@ This is a kludge. We want to ensure that instructions that
5601 may trap are not moved into the epilogue by scheduling, because
5602 we don't always emit unwind information for the epilogue. */
5603 if (cfun->can_throw_non_call_exceptions
5604 && targetm_common.except_unwind_info (&global_options) != UI_SJLJ)
5605 emit_insn (gen_blockage ());
5606
5607 /* If stack protection is enabled for this function, check the guard. */
5608 if (crtl->stack_protect_guard
5609 && targetm.stack_protect_runtime_enabled_p ()
5610 && naked_return_label)
5611 stack_protect_epilogue ();
5612
5613 /* If we had calls to alloca, and this machine needs
5614 an accurate stack pointer to exit the function,
5615 insert some code to save and restore the stack pointer. */
5616 if (! EXIT_IGNORE_STACK
5617 && cfun->calls_alloca)
5618 {
5619 rtx tem = 0;
5620
5621 start_sequence ();
5622 emit_stack_save (SAVE_FUNCTION, &tem);
5623 rtx_insn *seq = end_sequence ();
5624 emit_insn_before (seq, parm_birth_insn);
5625
5626 emit_stack_restore (SAVE_FUNCTION, tem);
5627 }
5628
5629 /* ??? This should no longer be necessary since stupid is no longer with
5630 us, but there are some parts of the compiler (eg reload_combine, and
5631 sh mach_dep_reorg) that still try and compute their own lifetime info
5632 instead of using the general framework. */
5633 use_return_register ();
5634}
5635
5636rtx
5637get_arg_pointer_save_area (void)
5638{
5639 rtx ret = arg_pointer_save_area;
5640
5641 if (! ret)
5642 {
5643 ret = assign_stack_local (Pmode, size: GET_MODE_SIZE (Pmode), align: 0);
5644 arg_pointer_save_area = ret;
5645 }
5646
5647 if (! crtl->arg_pointer_save_area_init)
5648 {
5649 /* Save the arg pointer at the beginning of the function. The
5650 generated stack slot may not be a valid memory address, so we
5651 have to check it and fix it if necessary. */
5652 start_sequence ();
5653 emit_move_insn (validize_mem (copy_rtx (ret)),
5654 crtl->args.internal_arg_pointer);
5655 rtx_insn *seq = end_sequence ();
5656
5657 push_topmost_sequence ();
5658 emit_insn_after (seq, entry_of_function ());
5659 pop_topmost_sequence ();
5660
5661 crtl->arg_pointer_save_area_init = true;
5662 }
5663
5664 return ret;
5665}
5666
5667
5668/* If debugging dumps are requested, dump information about how the
5669 target handled -fstack-check=clash for the prologue.
5670
5671 PROBES describes what if any probes were emitted.
5672
5673 RESIDUALS indicates if the prologue had any residual allocation
5674 (i.e. total allocation was not a multiple of PROBE_INTERVAL). */
5675
5676void
5677dump_stack_clash_frame_info (enum stack_clash_probes probes, bool residuals)
5678{
5679 if (!dump_file)
5680 return;
5681
5682 switch (probes)
5683 {
5684 case NO_PROBE_NO_FRAME:
5685 fprintf (stream: dump_file,
5686 format: "Stack clash no probe no stack adjustment in prologue.\n");
5687 break;
5688 case NO_PROBE_SMALL_FRAME:
5689 fprintf (stream: dump_file,
5690 format: "Stack clash no probe small stack adjustment in prologue.\n");
5691 break;
5692 case PROBE_INLINE:
5693 fprintf (stream: dump_file, format: "Stack clash inline probes in prologue.\n");
5694 break;
5695 case PROBE_LOOP:
5696 fprintf (stream: dump_file, format: "Stack clash probe loop in prologue.\n");
5697 break;
5698 }
5699
5700 if (residuals)
5701 fprintf (stream: dump_file, format: "Stack clash residual allocation in prologue.\n");
5702 else
5703 fprintf (stream: dump_file, format: "Stack clash no residual allocation in prologue.\n");
5704
5705 if (frame_pointer_needed)
5706 fprintf (stream: dump_file, format: "Stack clash frame pointer needed.\n");
5707 else
5708 fprintf (stream: dump_file, format: "Stack clash no frame pointer needed.\n");
5709
5710 if (TREE_THIS_VOLATILE (cfun->decl))
5711 fprintf (stream: dump_file,
5712 format: "Stack clash noreturn prologue, assuming no implicit"
5713 " probes in caller.\n");
5714 else
5715 fprintf (stream: dump_file,
5716 format: "Stack clash not noreturn prologue.\n");
5717}
5718
5719/* Add a list of INSNS to the hash HASHP, possibly allocating HASHP
5720 for the first time. */
5721
5722static void
5723record_insns (rtx_insn *insns, rtx end, hash_table<insn_cache_hasher> **hashp)
5724{
5725 rtx_insn *tmp;
5726 hash_table<insn_cache_hasher> *hash = *hashp;
5727
5728 if (hash == NULL)
5729 *hashp = hash = hash_table<insn_cache_hasher>::create_ggc (n: 17);
5730
5731 for (tmp = insns; tmp != end; tmp = NEXT_INSN (insn: tmp))
5732 {
5733 rtx *slot = hash->find_slot (value: tmp, insert: INSERT);
5734 gcc_assert (*slot == NULL);
5735 *slot = tmp;
5736 }
5737}
5738
5739/* INSN has been duplicated or replaced by as COPY, perhaps by duplicating a
5740 basic block, splitting or peepholes. If INSN is a prologue or epilogue
5741 insn, then record COPY as well. */
5742
5743void
5744maybe_copy_prologue_epilogue_insn (rtx insn, rtx copy)
5745{
5746 hash_table<insn_cache_hasher> *hash;
5747 rtx *slot;
5748
5749 hash = epilogue_insn_hash;
5750 if (!hash || !hash->find (value: insn))
5751 {
5752 hash = prologue_insn_hash;
5753 if (!hash || !hash->find (value: insn))
5754 return;
5755 }
5756
5757 slot = hash->find_slot (value: copy, insert: INSERT);
5758 gcc_assert (*slot == NULL);
5759 *slot = copy;
5760}
5761
5762/* Determine if any INSNs in HASH are, or are part of, INSN. Because
5763 we can be running after reorg, SEQUENCE rtl is possible. */
5764
5765static bool
5766contains (const rtx_insn *insn, hash_table<insn_cache_hasher> *hash)
5767{
5768 if (hash == NULL)
5769 return false;
5770
5771 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
5772 {
5773 rtx_sequence *seq = as_a <rtx_sequence *> (p: PATTERN (insn));
5774 int i;
5775 for (i = seq->len () - 1; i >= 0; i--)
5776 if (hash->find (value: seq->element (index: i)))
5777 return true;
5778 return false;
5779 }
5780
5781 return hash->find (value: const_cast<rtx_insn *> (insn)) != NULL;
5782}
5783
5784bool
5785prologue_contains (const rtx_insn *insn)
5786{
5787 return contains (insn, hash: prologue_insn_hash);
5788}
5789
5790bool
5791epilogue_contains (const rtx_insn *insn)
5792{
5793 return contains (insn, hash: epilogue_insn_hash);
5794}
5795
5796bool
5797prologue_epilogue_contains (const rtx_insn *insn)
5798{
5799 if (contains (insn, hash: prologue_insn_hash))
5800 return true;
5801 if (contains (insn, hash: epilogue_insn_hash))
5802 return true;
5803 return false;
5804}
5805
5806void
5807record_prologue_seq (rtx_insn *seq)
5808{
5809 record_insns (insns: seq, NULL, hashp: &prologue_insn_hash);
5810}
5811
5812void
5813record_epilogue_seq (rtx_insn *seq)
5814{
5815 record_insns (insns: seq, NULL, hashp: &epilogue_insn_hash);
5816}
5817
5818/* Set JUMP_LABEL for a return insn. */
5819
5820void
5821set_return_jump_label (rtx_insn *returnjump)
5822{
5823 rtx pat = PATTERN (insn: returnjump);
5824 if (GET_CODE (pat) == PARALLEL)
5825 pat = XVECEXP (pat, 0, 0);
5826 if (ANY_RETURN_P (pat))
5827 JUMP_LABEL (returnjump) = pat;
5828 else
5829 JUMP_LABEL (returnjump) = ret_rtx;
5830}
5831
5832/* Return a sequence to be used as the split prologue for the current
5833 function, or NULL. */
5834
5835static rtx_insn *
5836make_split_prologue_seq (void)
5837{
5838 if (!flag_split_stack
5839 || lookup_attribute (attr_name: "no_split_stack", DECL_ATTRIBUTES (cfun->decl)))
5840 return NULL;
5841
5842 start_sequence ();
5843 emit_insn (targetm.gen_split_stack_prologue ());
5844 rtx_insn *seq = end_sequence ();
5845
5846 record_insns (insns: seq, NULL, hashp: &prologue_insn_hash);
5847 set_insn_locations (seq, prologue_location);
5848
5849 return seq;
5850}
5851
5852/* Return a sequence to be used as the prologue for the current function,
5853 or NULL. */
5854
5855static rtx_insn *
5856make_prologue_seq (void)
5857{
5858 if (!targetm.have_prologue ())
5859 return NULL;
5860
5861 start_sequence ();
5862 rtx_insn *seq = targetm.gen_prologue ();
5863 emit_insn (seq);
5864
5865 /* Insert an explicit USE for the frame pointer
5866 if the profiling is on and the frame pointer is required. */
5867 if (crtl->profile && frame_pointer_needed)
5868 emit_use (hard_frame_pointer_rtx);
5869
5870 /* Retain a map of the prologue insns. */
5871 record_insns (insns: seq, NULL, hashp: &prologue_insn_hash);
5872 emit_note (NOTE_INSN_PROLOGUE_END);
5873
5874 /* Ensure that instructions are not moved into the prologue when
5875 profiling is on. The call to the profiling routine can be
5876 emitted within the live range of a call-clobbered register. */
5877 if (!targetm.profile_before_prologue () && crtl->profile)
5878 emit_insn (gen_blockage ());
5879
5880 seq = end_sequence ();
5881 set_insn_locations (seq, prologue_location);
5882
5883 return seq;
5884}
5885
5886/* Emit a sequence of insns to zero the call-used registers before RET
5887 according to ZERO_REGS_TYPE. */
5888
5889static void
5890gen_call_used_regs_seq (rtx_insn *ret, unsigned int zero_regs_type)
5891{
5892 bool only_gpr = true;
5893 bool only_used = true;
5894 bool only_arg = true;
5895
5896 /* No need to zero call-used-regs in main (). */
5897 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
5898 return;
5899
5900 /* No need to zero call-used-regs if __builtin_eh_return is called
5901 since it isn't a normal function return. */
5902 if (crtl->calls_eh_return)
5903 return;
5904
5905 /* If only_gpr is true, only zero call-used registers that are
5906 general-purpose registers; if only_used is true, only zero
5907 call-used registers that are used in the current function;
5908 if only_arg is true, only zero call-used registers that pass
5909 parameters defined by the flatform's calling conversion. */
5910
5911 using namespace zero_regs_flags;
5912
5913 only_gpr = zero_regs_type & ONLY_GPR;
5914 only_used = zero_regs_type & ONLY_USED;
5915 only_arg = zero_regs_type & ONLY_ARG;
5916
5917 if ((zero_regs_type & LEAFY_MODE) && leaf_function_p ())
5918 only_used = true;
5919
5920 /* For each of the hard registers, we should zero it if:
5921 1. it is a call-used register;
5922 and 2. it is not a fixed register;
5923 and 3. it is not live at the return of the routine;
5924 and 4. it is general registor if only_gpr is true;
5925 and 5. it is used in the routine if only_used is true;
5926 and 6. it is a register that passes parameter if only_arg is true. */
5927
5928 /* First, prepare the data flow information. */
5929 basic_block bb = BLOCK_FOR_INSN (insn: ret);
5930 auto_bitmap live_out;
5931 bitmap_copy (live_out, df_get_live_out (bb));
5932 df_simulate_initialize_backwards (bb, live_out);
5933 df_simulate_one_insn_backwards (bb, ret, live_out);
5934
5935 HARD_REG_SET selected_hardregs;
5936 HARD_REG_SET all_call_used_regs;
5937 CLEAR_HARD_REG_SET (set&: selected_hardregs);
5938 CLEAR_HARD_REG_SET (set&: all_call_used_regs);
5939 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5940 {
5941 if (!crtl->abi->clobbers_full_reg_p (regno))
5942 continue;
5943 if (fixed_regs[regno])
5944 continue;
5945 if (REGNO_REG_SET_P (live_out, regno))
5946 continue;
5947#ifdef LEAF_REG_REMAP
5948 if (crtl->uses_only_leaf_regs && LEAF_REG_REMAP (regno) < 0)
5949 continue;
5950#endif
5951 /* This is a call used register that is dead at return. */
5952 SET_HARD_REG_BIT (set&: all_call_used_regs, bit: regno);
5953
5954 if (only_gpr
5955 && !TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], bit: regno))
5956 continue;
5957 if (only_used && !df_regs_ever_live_p (regno))
5958 continue;
5959 if (only_arg && !FUNCTION_ARG_REGNO_P (regno))
5960 continue;
5961
5962 /* Now this is a register that we might want to zero. */
5963 SET_HARD_REG_BIT (set&: selected_hardregs, bit: regno);
5964 }
5965
5966 if (hard_reg_set_empty_p (x: selected_hardregs))
5967 return;
5968
5969 /* Now that we have a hard register set that needs to be zeroed, pass it to
5970 target to generate zeroing sequence. */
5971 HARD_REG_SET zeroed_hardregs;
5972 start_sequence ();
5973 zeroed_hardregs = targetm.calls.zero_call_used_regs (selected_hardregs);
5974
5975 /* For most targets, the returned set of registers is a subset of
5976 selected_hardregs, however, for some of the targets (for example MIPS),
5977 clearing some registers that are in selected_hardregs requires clearing
5978 other call used registers that are not in the selected_hardregs, under
5979 such situation, the returned set of registers must be a subset of
5980 all call used registers. */
5981 gcc_assert (hard_reg_set_subset_p (zeroed_hardregs, all_call_used_regs));
5982
5983 rtx_insn *seq = end_sequence ();
5984 if (seq)
5985 {
5986 /* Emit the memory blockage and register clobber asm volatile before
5987 the whole sequence. */
5988 start_sequence ();
5989 expand_asm_reg_clobber_mem_blockage (zeroed_hardregs);
5990 rtx_insn *seq_barrier = end_sequence ();
5991
5992 emit_insn_before (seq_barrier, ret);
5993 emit_insn_before (seq, ret);
5994
5995 /* Update the data flow information. */
5996 crtl->must_be_zero_on_return |= zeroed_hardregs;
5997 df_update_exit_block_uses ();
5998 }
5999}
6000
6001
6002/* Return a sequence to be used as the epilogue for the current function,
6003 or NULL. */
6004
6005static rtx_insn *
6006make_epilogue_seq (void)
6007{
6008 if (!targetm.have_epilogue ())
6009 return NULL;
6010
6011 start_sequence ();
6012 emit_note (NOTE_INSN_EPILOGUE_BEG);
6013 rtx_insn *seq = targetm.gen_epilogue ();
6014 if (seq)
6015 emit_jump_insn (seq);
6016
6017 /* Retain a map of the epilogue insns. */
6018 record_insns (insns: seq, NULL, hashp: &epilogue_insn_hash);
6019 set_insn_locations (seq, epilogue_location);
6020
6021 seq = get_insns ();
6022 rtx_insn *returnjump = get_last_insn ();
6023 end_sequence ();
6024
6025 if (JUMP_P (returnjump))
6026 set_return_jump_label (returnjump);
6027
6028 return seq;
6029}
6030
6031
6032/* Generate the prologue and epilogue RTL if the machine supports it. Thread
6033 this into place with notes indicating where the prologue ends and where
6034 the epilogue begins. Update the basic block information when possible.
6035
6036 Notes on epilogue placement:
6037 There are several kinds of edges to the exit block:
6038 * a single fallthru edge from LAST_BB
6039 * possibly, edges from blocks containing sibcalls
6040 * possibly, fake edges from infinite loops
6041
6042 The epilogue is always emitted on the fallthru edge from the last basic
6043 block in the function, LAST_BB, into the exit block.
6044
6045 If LAST_BB is empty except for a label, it is the target of every
6046 other basic block in the function that ends in a return. If a
6047 target has a return or simple_return pattern (possibly with
6048 conditional variants), these basic blocks can be changed so that a
6049 return insn is emitted into them, and their target is adjusted to
6050 the real exit block.
6051
6052 Notes on shrink wrapping: We implement a fairly conservative
6053 version of shrink-wrapping rather than the textbook one. We only
6054 generate a single prologue and a single epilogue. This is
6055 sufficient to catch a number of interesting cases involving early
6056 exits.
6057
6058 First, we identify the blocks that require the prologue to occur before
6059 them. These are the ones that modify a call-saved register, or reference
6060 any of the stack or frame pointer registers. To simplify things, we then
6061 mark everything reachable from these blocks as also requiring a prologue.
6062 This takes care of loops automatically, and avoids the need to examine
6063 whether MEMs reference the frame, since it is sufficient to check for
6064 occurrences of the stack or frame pointer.
6065
6066 We then compute the set of blocks for which the need for a prologue
6067 is anticipatable (borrowing terminology from the shrink-wrapping
6068 description in Muchnick's book). These are the blocks which either
6069 require a prologue themselves, or those that have only successors
6070 where the prologue is anticipatable. The prologue needs to be
6071 inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
6072 is not. For the moment, we ensure that only one such edge exists.
6073
6074 The epilogue is placed as described above, but we make a
6075 distinction between inserting return and simple_return patterns
6076 when modifying other blocks that end in a return. Blocks that end
6077 in a sibcall omit the sibcall_epilogue if the block is not in
6078 ANTIC. */
6079
6080void
6081thread_prologue_and_epilogue_insns (void)
6082{
6083 df_analyze ();
6084
6085 /* Can't deal with multiple successors of the entry block at the
6086 moment. Function should always have at least one entry
6087 point. */
6088 gcc_assert (single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
6089
6090 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6091 edge orig_entry_edge = entry_edge;
6092
6093 rtx_insn *split_prologue_seq = make_split_prologue_seq ();
6094 rtx_insn *prologue_seq = make_prologue_seq ();
6095 rtx_insn *epilogue_seq = make_epilogue_seq ();
6096
6097 /* Try to perform a kind of shrink-wrapping, making sure the
6098 prologue/epilogue is emitted only around those parts of the
6099 function that require it. */
6100 try_shrink_wrapping (entry_edge: &entry_edge, prologue_seq);
6101
6102 /* If the target can handle splitting the prologue/epilogue into separate
6103 components, try to shrink-wrap these components separately. */
6104 try_shrink_wrapping_separate (first_bb: entry_edge->dest);
6105
6106 /* If that did anything for any component we now need the generate the
6107 "main" prologue again. Because some targets require some of these
6108 to be called in a specific order (i386 requires the split prologue
6109 to be first, for example), we create all three sequences again here.
6110 If this does not work for some target, that target should not enable
6111 separate shrink-wrapping. */
6112 if (crtl->shrink_wrapped_separate)
6113 {
6114 split_prologue_seq = make_split_prologue_seq ();
6115 prologue_seq = make_prologue_seq ();
6116 epilogue_seq = make_epilogue_seq ();
6117 }
6118
6119 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
6120
6121 /* A small fib -- epilogue is not yet completed, but we wish to re-use
6122 this marker for the splits of EH_RETURN patterns, and nothing else
6123 uses the flag in the meantime. */
6124 epilogue_completed = 1;
6125
6126 /* Find non-fallthru edges that end with EH_RETURN instructions. On
6127 some targets, these get split to a special version of the epilogue
6128 code. In order to be able to properly annotate these with unwind
6129 info, try to split them now. If we get a valid split, drop an
6130 EPILOGUE_BEG note and mark the insns as epilogue insns. */
6131 edge e;
6132 edge_iterator ei;
6133 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6134 {
6135 rtx_insn *prev, *last, *trial;
6136
6137 if (e->flags & EDGE_FALLTHRU)
6138 continue;
6139 last = BB_END (e->src);
6140 if (!eh_returnjump_p (last))
6141 continue;
6142
6143 prev = PREV_INSN (insn: last);
6144 trial = try_split (PATTERN (insn: last), last, 1);
6145 if (trial == last)
6146 continue;
6147
6148 record_insns (insns: NEXT_INSN (insn: prev), end: NEXT_INSN (insn: trial), hashp: &epilogue_insn_hash);
6149 emit_note_after (NOTE_INSN_EPILOGUE_BEG, prev);
6150 }
6151
6152 edge exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6153
6154 if (exit_fallthru_edge)
6155 {
6156 if (epilogue_seq)
6157 {
6158 insert_insn_on_edge (epilogue_seq, exit_fallthru_edge);
6159 commit_edge_insertions ();
6160
6161 /* The epilogue insns we inserted may cause the exit edge to no longer
6162 be fallthru. */
6163 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6164 {
6165 if (((e->flags & EDGE_FALLTHRU) != 0)
6166 && returnjump_p (BB_END (e->src)))
6167 e->flags &= ~EDGE_FALLTHRU;
6168 }
6169
6170 find_sub_basic_blocks (BLOCK_FOR_INSN (insn: epilogue_seq));
6171 }
6172 else if (next_active_insn (BB_END (exit_fallthru_edge->src)))
6173 {
6174 /* We have a fall-through edge to the exit block, the source is not
6175 at the end of the function, and there will be an assembler epilogue
6176 at the end of the function.
6177 We can't use force_nonfallthru here, because that would try to
6178 use return. Inserting a jump 'by hand' is extremely messy, so
6179 we take advantage of cfg_layout_finalize using
6180 fixup_fallthru_exit_predecessor. */
6181 cfg_layout_initialize (0);
6182 basic_block cur_bb;
6183 FOR_EACH_BB_FN (cur_bb, cfun)
6184 if (cur_bb->index >= NUM_FIXED_BLOCKS
6185 && cur_bb->next_bb->index >= NUM_FIXED_BLOCKS)
6186 cur_bb->aux = cur_bb->next_bb;
6187 cfg_layout_finalize ();
6188 }
6189 }
6190
6191 /* Insert the prologue. */
6192
6193 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6194
6195 if (split_prologue_seq || prologue_seq)
6196 {
6197 rtx_insn *split_prologue_insn = split_prologue_seq;
6198 if (split_prologue_seq)
6199 {
6200 while (split_prologue_insn && !NONDEBUG_INSN_P (split_prologue_insn))
6201 split_prologue_insn = NEXT_INSN (insn: split_prologue_insn);
6202 insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
6203 }
6204
6205 rtx_insn *prologue_insn = prologue_seq;
6206 if (prologue_seq)
6207 {
6208 while (prologue_insn && !NONDEBUG_INSN_P (prologue_insn))
6209 prologue_insn = NEXT_INSN (insn: prologue_insn);
6210 insert_insn_on_edge (prologue_seq, entry_edge);
6211 }
6212
6213 commit_edge_insertions ();
6214
6215 /* Look for basic blocks within the prologue insns. */
6216 if (split_prologue_insn
6217 && BLOCK_FOR_INSN (insn: split_prologue_insn) == NULL)
6218 split_prologue_insn = NULL;
6219 if (prologue_insn
6220 && BLOCK_FOR_INSN (insn: prologue_insn) == NULL)
6221 prologue_insn = NULL;
6222 if (split_prologue_insn || prologue_insn)
6223 {
6224 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
6225 bitmap_clear (blocks);
6226 if (split_prologue_insn)
6227 bitmap_set_bit (map: blocks,
6228 bitno: BLOCK_FOR_INSN (insn: split_prologue_insn)->index);
6229 if (prologue_insn)
6230 bitmap_set_bit (map: blocks, bitno: BLOCK_FOR_INSN (insn: prologue_insn)->index);
6231 find_many_sub_basic_blocks (blocks);
6232 }
6233 }
6234
6235 default_rtl_profile ();
6236
6237 /* Emit sibling epilogues before any sibling call sites. */
6238 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
6239 (e = ei_safe_edge (i: ei));
6240 ei_next (i: &ei))
6241 {
6242 /* Skip those already handled, the ones that run without prologue. */
6243 if (e->flags & EDGE_IGNORE)
6244 {
6245 e->flags &= ~EDGE_IGNORE;
6246 continue;
6247 }
6248
6249 rtx_insn *insn = BB_END (e->src);
6250
6251 if (!(CALL_P (insn) && SIBLING_CALL_P (insn)))
6252 continue;
6253
6254 rtx_insn *ep_seq;
6255 if (targetm.emit_epilogue_for_sibcall)
6256 {
6257 start_sequence ();
6258 targetm.emit_epilogue_for_sibcall (as_a<rtx_call_insn *> (p: insn));
6259 ep_seq = end_sequence ();
6260 }
6261 else
6262 ep_seq = targetm.gen_sibcall_epilogue ();
6263 if (ep_seq)
6264 {
6265 start_sequence ();
6266 emit_note (NOTE_INSN_EPILOGUE_BEG);
6267 emit_insn (ep_seq);
6268 rtx_insn *seq = end_sequence ();
6269
6270 /* Retain a map of the epilogue insns. Used in life analysis to
6271 avoid getting rid of sibcall epilogue insns. Do this before we
6272 actually emit the sequence. */
6273 record_insns (insns: seq, NULL, hashp: &epilogue_insn_hash);
6274 set_insn_locations (seq, epilogue_location);
6275
6276 emit_insn_before (seq, insn);
6277
6278 find_sub_basic_blocks (BLOCK_FOR_INSN (insn));
6279 }
6280 }
6281
6282 if (epilogue_seq)
6283 {
6284 rtx_insn *insn, *next;
6285
6286 /* Similarly, move any line notes that appear after the epilogue.
6287 There is no need, however, to be quite so anal about the existence
6288 of such a note. Also possibly move
6289 NOTE_INSN_FUNCTION_BEG notes, as those can be relevant for debug
6290 info generation. */
6291 for (insn = epilogue_seq; insn; insn = next)
6292 {
6293 next = NEXT_INSN (insn);
6294 if (NOTE_P (insn)
6295 && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG))
6296 reorder_insns (insn, insn, PREV_INSN (insn: epilogue_seq));
6297 }
6298 }
6299
6300 /* Threading the prologue and epilogue changes the artificial refs in the
6301 entry and exit blocks, and may invalidate DF info for tail calls.
6302 This is also needed for [[musttail]] conversion even when not
6303 optimizing. */
6304 if (optimize
6305 || cfun->tail_call_marked
6306 || flag_optimize_sibling_calls
6307 || flag_ipa_icf_functions
6308 || in_lto_p)
6309 df_update_entry_exit_and_calls ();
6310 else
6311 {
6312 df_update_entry_block_defs ();
6313 df_update_exit_block_uses ();
6314 }
6315}
6316
6317/* Reposition the prologue-end and epilogue-begin notes after
6318 instruction scheduling. */
6319
6320void
6321reposition_prologue_and_epilogue_notes (void)
6322{
6323 if (!targetm.have_prologue ()
6324 && !targetm.have_epilogue ()
6325 && !targetm.have_sibcall_epilogue ()
6326 && !targetm.emit_epilogue_for_sibcall)
6327 return;
6328
6329 /* Since the hash table is created on demand, the fact that it is
6330 non-null is a signal that it is non-empty. */
6331 if (prologue_insn_hash != NULL)
6332 {
6333 size_t len = prologue_insn_hash->elements ();
6334 rtx_insn *insn, *last = NULL, *note = NULL;
6335
6336 /* Scan from the beginning until we reach the last prologue insn. */
6337 /* ??? While we do have the CFG intact, there are two problems:
6338 (1) The prologue can contain loops (typically probing the stack),
6339 which means that the end of the prologue isn't in the first bb.
6340 (2) Sometimes the PROLOGUE_END note gets pushed into the next bb. */
6341 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6342 {
6343 if (NOTE_P (insn))
6344 {
6345 if (NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
6346 note = insn;
6347 }
6348 else if (contains (insn, hash: prologue_insn_hash))
6349 {
6350 last = insn;
6351 if (--len == 0)
6352 break;
6353 }
6354 }
6355
6356 if (last)
6357 {
6358 if (note == NULL)
6359 {
6360 /* Scan forward looking for the PROLOGUE_END note. It should
6361 be right at the beginning of the block, possibly with other
6362 insn notes that got moved there. */
6363 for (note = NEXT_INSN (insn: last); ; note = NEXT_INSN (insn: note))
6364 {
6365 if (NOTE_P (note)
6366 && NOTE_KIND (note) == NOTE_INSN_PROLOGUE_END)
6367 break;
6368 }
6369 }
6370
6371 /* Avoid placing note between CODE_LABEL and BASIC_BLOCK note. */
6372 if (LABEL_P (last))
6373 last = NEXT_INSN (insn: last);
6374 reorder_insns (note, note, last);
6375 }
6376 }
6377
6378 if (epilogue_insn_hash != NULL)
6379 {
6380 edge_iterator ei;
6381 edge e;
6382
6383 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6384 {
6385 rtx_insn *insn, *first = NULL, *note = NULL;
6386 basic_block bb = e->src;
6387
6388 /* Scan from the beginning until we reach the first epilogue insn. */
6389 FOR_BB_INSNS (bb, insn)
6390 {
6391 if (NOTE_P (insn))
6392 {
6393 if (NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
6394 {
6395 note = insn;
6396 if (first != NULL)
6397 break;
6398 }
6399 }
6400 else if (first == NULL && contains (insn, hash: epilogue_insn_hash))
6401 {
6402 first = insn;
6403 if (note != NULL)
6404 break;
6405 }
6406 }
6407
6408 if (note)
6409 {
6410 /* If the function has a single basic block, and no real
6411 epilogue insns (e.g. sibcall with no cleanup), the
6412 epilogue note can get scheduled before the prologue
6413 note. If we have frame related prologue insns, having
6414 them scanned during the epilogue will result in a crash.
6415 In this case re-order the epilogue note to just before
6416 the last insn in the block. */
6417 if (first == NULL)
6418 first = BB_END (bb);
6419
6420 if (PREV_INSN (insn: first) != note)
6421 reorder_insns (note, note, PREV_INSN (insn: first));
6422 }
6423 }
6424 }
6425}
6426
6427/* Returns the name of function declared by FNDECL. */
6428const char *
6429fndecl_name (tree fndecl)
6430{
6431 if (fndecl == NULL)
6432 return "(nofn)";
6433 return lang_hooks.decl_printable_name (fndecl, 1);
6434}
6435
6436/* Returns the name of function FN. */
6437const char *
6438function_name (const function *fn)
6439{
6440 tree fndecl = (fn == NULL) ? NULL : fn->decl;
6441 return fndecl_name (fndecl);
6442}
6443
6444/* Returns the name of the current function. */
6445const char *
6446current_function_name (void)
6447{
6448 return function_name (fn: cfun);
6449}
6450
6451
6452static void
6453rest_of_handle_check_leaf_regs (void)
6454{
6455#ifdef LEAF_REGISTERS
6456 crtl->uses_only_leaf_regs
6457 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
6458#endif
6459}
6460
6461/* Insert a TYPE into the used types hash table of CFUN. */
6462
6463static void
6464used_types_insert_helper (tree type, struct function *func)
6465{
6466 if (type != NULL && func != NULL)
6467 {
6468 if (func->used_types_hash == NULL)
6469 func->used_types_hash = hash_set<tree>::create_ggc (n: 37);
6470
6471 func->used_types_hash->add (k: type);
6472 }
6473}
6474
6475/* Given a type, insert it into the used hash table in cfun. */
6476void
6477used_types_insert (tree t)
6478{
6479 while (POINTER_TYPE_P (t) || TREE_CODE (t) == ARRAY_TYPE)
6480 if (TYPE_NAME (t))
6481 break;
6482 else
6483 t = TREE_TYPE (t);
6484 if (TREE_CODE (t) == ERROR_MARK)
6485 return;
6486 if (TYPE_NAME (t) == NULL_TREE
6487 || TYPE_NAME (t) == TYPE_NAME (TYPE_MAIN_VARIANT (t)))
6488 t = TYPE_MAIN_VARIANT (t);
6489 if (debug_info_level > DINFO_LEVEL_NONE)
6490 {
6491 if (cfun)
6492 used_types_insert_helper (type: t, func: cfun);
6493 else
6494 {
6495 /* So this might be a type referenced by a global variable.
6496 Record that type so that we can later decide to emit its
6497 debug information. */
6498 vec_safe_push (v&: types_used_by_cur_var_decl, obj: t);
6499 }
6500 }
6501}
6502
6503/* Helper to Hash a struct types_used_by_vars_entry. */
6504
6505static hashval_t
6506hash_types_used_by_vars_entry (const struct types_used_by_vars_entry *entry)
6507{
6508 gcc_assert (entry && entry->var_decl && entry->type);
6509
6510 return iterative_hash_object (entry->type,
6511 iterative_hash_object (entry->var_decl, 0));
6512}
6513
6514/* Hash function of the types_used_by_vars_entry hash table. */
6515
6516hashval_t
6517used_type_hasher::hash (types_used_by_vars_entry *entry)
6518{
6519 return hash_types_used_by_vars_entry (entry);
6520}
6521
6522/*Equality function of the types_used_by_vars_entry hash table. */
6523
6524bool
6525used_type_hasher::equal (types_used_by_vars_entry *e1,
6526 types_used_by_vars_entry *e2)
6527{
6528 return (e1->var_decl == e2->var_decl && e1->type == e2->type);
6529}
6530
6531/* Inserts an entry into the types_used_by_vars_hash hash table. */
6532
6533void
6534types_used_by_var_decl_insert (tree type, tree var_decl)
6535{
6536 if (type != NULL && var_decl != NULL)
6537 {
6538 types_used_by_vars_entry **slot;
6539 struct types_used_by_vars_entry e;
6540 e.var_decl = var_decl;
6541 e.type = type;
6542 if (types_used_by_vars_hash == NULL)
6543 types_used_by_vars_hash
6544 = hash_table<used_type_hasher>::create_ggc (n: 37);
6545
6546 slot = types_used_by_vars_hash->find_slot (value: &e, insert: INSERT);
6547 if (*slot == NULL)
6548 {
6549 struct types_used_by_vars_entry *entry;
6550 entry = ggc_alloc<types_used_by_vars_entry> ();
6551 entry->type = type;
6552 entry->var_decl = var_decl;
6553 *slot = entry;
6554 }
6555 }
6556}
6557
6558namespace {
6559
6560const pass_data pass_data_leaf_regs =
6561{
6562 .type: RTL_PASS, /* type */
6563 .name: "*leaf_regs", /* name */
6564 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
6565 .tv_id: TV_NONE, /* tv_id */
6566 .properties_required: 0, /* properties_required */
6567 .properties_provided: 0, /* properties_provided */
6568 .properties_destroyed: 0, /* properties_destroyed */
6569 .todo_flags_start: 0, /* todo_flags_start */
6570 .todo_flags_finish: 0, /* todo_flags_finish */
6571};
6572
6573class pass_leaf_regs : public rtl_opt_pass
6574{
6575public:
6576 pass_leaf_regs (gcc::context *ctxt)
6577 : rtl_opt_pass (pass_data_leaf_regs, ctxt)
6578 {}
6579
6580 /* opt_pass methods: */
6581 unsigned int execute (function *) final override
6582 {
6583 rest_of_handle_check_leaf_regs ();
6584 return 0;
6585 }
6586
6587}; // class pass_leaf_regs
6588
6589} // anon namespace
6590
6591rtl_opt_pass *
6592make_pass_leaf_regs (gcc::context *ctxt)
6593{
6594 return new pass_leaf_regs (ctxt);
6595}
6596
6597static void
6598rest_of_handle_thread_prologue_and_epilogue (function *fun)
6599{
6600 /* prepare_shrink_wrap is sensitive to the block structure of the control
6601 flow graph, so clean it up first. */
6602 if (cfun->tail_call_marked || optimize)
6603 cleanup_cfg (0);
6604
6605 /* On some machines, the prologue and epilogue code, or parts thereof,
6606 can be represented as RTL. Doing so lets us schedule insns between
6607 it and the rest of the code and also allows delayed branch
6608 scheduling to operate in the epilogue. */
6609 thread_prologue_and_epilogue_insns ();
6610
6611 /* Some non-cold blocks may now be only reachable from cold blocks.
6612 Fix that up. */
6613 fixup_partitions ();
6614
6615 /* After prologue and epilogue generation, the judgement on whether
6616 one memory access onto stack frame may trap or not could change,
6617 since we get more exact stack information by now. So try to
6618 remove any EH edges here, see PR90259. */
6619 if (fun->can_throw_non_call_exceptions)
6620 purge_all_dead_edges ();
6621
6622 /* Shrink-wrapping can result in unreachable edges in the epilogue,
6623 see PR57320. */
6624 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
6625
6626 /* The stack usage info is finalized during prologue expansion. */
6627 if (flag_stack_usage_info || flag_callgraph_info)
6628 output_stack_usage ();
6629}
6630
6631/* Record a final call to CALLEE at LOCATION. */
6632
6633void
6634record_final_call (tree callee, location_t location)
6635{
6636 struct callinfo_callee datum = { .location: location, .decl: callee };
6637 vec_safe_push (v&: cfun->su->callees, obj: datum);
6638}
6639
6640/* Record a dynamic allocation made for DECL_OR_EXP. */
6641
6642void
6643record_dynamic_alloc (tree decl_or_exp)
6644{
6645 struct callinfo_dalloc datum;
6646
6647 if (DECL_P (decl_or_exp))
6648 {
6649 datum.location = DECL_SOURCE_LOCATION (decl_or_exp);
6650 const char *name = lang_hooks.decl_printable_name (decl_or_exp, 2);
6651 const char *dot = strrchr (s: name, c: '.');
6652 if (dot)
6653 name = dot + 1;
6654 datum.name = ggc_strdup (name);
6655 }
6656 else
6657 {
6658 datum.location = EXPR_LOCATION (decl_or_exp);
6659 datum.name = NULL;
6660 }
6661
6662 vec_safe_push (v&: cfun->su->dallocs, obj: datum);
6663}
6664
6665namespace {
6666
6667const pass_data pass_data_thread_prologue_and_epilogue =
6668{
6669 .type: RTL_PASS, /* type */
6670 .name: "pro_and_epilogue", /* name */
6671 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
6672 .tv_id: TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6673 .properties_required: 0, /* properties_required */
6674 .properties_provided: 0, /* properties_provided */
6675 .properties_destroyed: 0, /* properties_destroyed */
6676 .todo_flags_start: 0, /* todo_flags_start */
6677 .todo_flags_finish: ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6678};
6679
6680class pass_thread_prologue_and_epilogue : public rtl_opt_pass
6681{
6682public:
6683 pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6684 : rtl_opt_pass (pass_data_thread_prologue_and_epilogue, ctxt)
6685 {}
6686
6687 /* opt_pass methods: */
6688 bool gate (function *) final override
6689 {
6690 return !targetm.use_late_prologue_epilogue ();
6691 }
6692
6693 unsigned int execute (function * fun) final override
6694 {
6695 rest_of_handle_thread_prologue_and_epilogue (fun);
6696 return 0;
6697 }
6698
6699}; // class pass_thread_prologue_and_epilogue
6700
6701const pass_data pass_data_late_thread_prologue_and_epilogue =
6702{
6703 .type: RTL_PASS, /* type */
6704 .name: "late_pro_and_epilogue", /* name */
6705 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
6706 .tv_id: TV_THREAD_PROLOGUE_AND_EPILOGUE, /* tv_id */
6707 .properties_required: 0, /* properties_required */
6708 .properties_provided: 0, /* properties_provided */
6709 .properties_destroyed: 0, /* properties_destroyed */
6710 .todo_flags_start: 0, /* todo_flags_start */
6711 .todo_flags_finish: ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
6712};
6713
6714class pass_late_thread_prologue_and_epilogue : public rtl_opt_pass
6715{
6716public:
6717 pass_late_thread_prologue_and_epilogue (gcc::context *ctxt)
6718 : rtl_opt_pass (pass_data_late_thread_prologue_and_epilogue, ctxt)
6719 {}
6720
6721 /* opt_pass methods: */
6722 bool gate (function *) final override
6723 {
6724 return targetm.use_late_prologue_epilogue ();
6725 }
6726
6727 unsigned int execute (function *fn) final override
6728 {
6729 /* It's not currently possible to have both delay slots and
6730 late prologue/epilogue, since the latter has to run before
6731 the former, and the former won't honor whatever restrictions
6732 the latter is trying to enforce. */
6733 gcc_assert (!DELAY_SLOTS);
6734 rest_of_handle_thread_prologue_and_epilogue (fun: fn);
6735 return 0;
6736 }
6737}; // class pass_late_thread_prologue_and_epilogue
6738
6739} // anon namespace
6740
6741rtl_opt_pass *
6742make_pass_thread_prologue_and_epilogue (gcc::context *ctxt)
6743{
6744 return new pass_thread_prologue_and_epilogue (ctxt);
6745}
6746
6747rtl_opt_pass *
6748make_pass_late_thread_prologue_and_epilogue (gcc::context *ctxt)
6749{
6750 return new pass_late_thread_prologue_and_epilogue (ctxt);
6751}
6752
6753namespace {
6754
6755const pass_data pass_data_zero_call_used_regs =
6756{
6757 .type: RTL_PASS, /* type */
6758 .name: "zero_call_used_regs", /* name */
6759 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
6760 .tv_id: TV_NONE, /* tv_id */
6761 .properties_required: 0, /* properties_required */
6762 .properties_provided: 0, /* properties_provided */
6763 .properties_destroyed: 0, /* properties_destroyed */
6764 .todo_flags_start: 0, /* todo_flags_start */
6765 .todo_flags_finish: 0, /* todo_flags_finish */
6766};
6767
6768class pass_zero_call_used_regs: public rtl_opt_pass
6769{
6770public:
6771 pass_zero_call_used_regs (gcc::context *ctxt)
6772 : rtl_opt_pass (pass_data_zero_call_used_regs, ctxt)
6773 {}
6774
6775 /* opt_pass methods: */
6776 unsigned int execute (function *) final override;
6777
6778}; // class pass_zero_call_used_regs
6779
6780unsigned int
6781pass_zero_call_used_regs::execute (function *fun)
6782{
6783 using namespace zero_regs_flags;
6784 unsigned int zero_regs_type = UNSET;
6785
6786 tree attr_zero_regs = lookup_attribute (attr_name: "zero_call_used_regs",
6787 DECL_ATTRIBUTES (fun->decl));
6788
6789 /* Get the type of zero_call_used_regs from function attribute.
6790 We have filtered out invalid attribute values already at this point. */
6791 if (attr_zero_regs)
6792 {
6793 /* The TREE_VALUE of an attribute is a TREE_LIST whose TREE_VALUE
6794 is the attribute argument's value. */
6795 attr_zero_regs = TREE_VALUE (attr_zero_regs);
6796 gcc_assert (TREE_CODE (attr_zero_regs) == TREE_LIST);
6797 attr_zero_regs = TREE_VALUE (attr_zero_regs);
6798 gcc_assert (TREE_CODE (attr_zero_regs) == STRING_CST);
6799
6800 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
6801 if (strcmp (TREE_STRING_POINTER (attr_zero_regs),
6802 s2: zero_call_used_regs_opts[i].name) == 0)
6803 {
6804 zero_regs_type = zero_call_used_regs_opts[i].flag;
6805 break;
6806 }
6807 }
6808
6809 if (!zero_regs_type)
6810 zero_regs_type = flag_zero_call_used_regs;
6811
6812 /* No need to zero call-used-regs when no user request is present. */
6813 if (!(zero_regs_type & ENABLED))
6814 return 0;
6815
6816 edge_iterator ei;
6817 edge e;
6818
6819 /* This pass needs data flow information. */
6820 df_analyze ();
6821
6822 /* Iterate over the function's return instructions and insert any
6823 register zeroing required by the -fzero-call-used-regs command-line
6824 option or the "zero_call_used_regs" function attribute. */
6825 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
6826 {
6827 rtx_insn *insn = BB_END (e->src);
6828 if (JUMP_P (insn) && ANY_RETURN_P (JUMP_LABEL (insn)))
6829 gen_call_used_regs_seq (ret: insn, zero_regs_type);
6830 }
6831
6832 return 0;
6833}
6834
6835} // anon namespace
6836
6837rtl_opt_pass *
6838make_pass_zero_call_used_regs (gcc::context *ctxt)
6839{
6840 return new pass_zero_call_used_regs (ctxt);
6841}
6842
6843/* If CONSTRAINT is a matching constraint, then return its number.
6844 Otherwise, return -1. */
6845
6846static int
6847matching_constraint_num (const char *constraint)
6848{
6849 if (*constraint == '%')
6850 constraint++;
6851
6852 if (IN_RANGE (*constraint, '0', '9'))
6853 return strtoul (nptr: constraint, NULL, base: 10);
6854
6855 return -1;
6856}
6857
6858/* This mini-pass fixes fall-out from SSA in asm statements that have
6859 in-out constraints. Say you start with
6860
6861 orig = inout;
6862 asm ("": "+mr" (inout));
6863 use (orig);
6864
6865 which is transformed very early to use explicit output and match operands:
6866
6867 orig = inout;
6868 asm ("": "=mr" (inout) : "0" (inout));
6869 use (orig);
6870
6871 Or, after SSA and copyprop,
6872
6873 asm ("": "=mr" (inout_2) : "0" (inout_1));
6874 use (inout_1);
6875
6876 Clearly inout_2 and inout_1 can't be coalesced easily anymore, as
6877 they represent two separate values, so they will get different pseudo
6878 registers during expansion. Then, since the two operands need to match
6879 per the constraints, but use different pseudo registers, reload can
6880 only register a reload for these operands. But reloads can only be
6881 satisfied by hardregs, not by memory, so we need a register for this
6882 reload, just because we are presented with non-matching operands.
6883 So, even though we allow memory for this operand, no memory can be
6884 used for it, just because the two operands don't match. This can
6885 cause reload failures on register-starved targets.
6886
6887 So it's a symptom of reload not being able to use memory for reloads
6888 or, alternatively it's also a symptom of both operands not coming into
6889 reload as matching (in which case the pseudo could go to memory just
6890 fine, as the alternative allows it, and no reload would be necessary).
6891 We fix the latter problem here, by transforming
6892
6893 asm ("": "=mr" (inout_2) : "0" (inout_1));
6894
6895 back to
6896
6897 inout_2 = inout_1;
6898 asm ("": "=mr" (inout_2) : "0" (inout_2)); */
6899
6900static void
6901match_asm_constraints_1 (rtx_insn *insn, rtx *p_sets, int noutputs)
6902{
6903 int i;
6904 bool changed = false;
6905 rtx op = SET_SRC (p_sets[0]);
6906 int ninputs = ASM_OPERANDS_INPUT_LENGTH (op);
6907 rtvec inputs = ASM_OPERANDS_INPUT_VEC (op);
6908 bool *output_matched = XALLOCAVEC (bool, noutputs);
6909
6910 memset (s: output_matched, c: 0, n: noutputs * sizeof (bool));
6911 for (i = 0; i < ninputs; i++)
6912 {
6913 rtx input, output;
6914 rtx_insn *insns;
6915 const char *constraint = ASM_OPERANDS_INPUT_CONSTRAINT (op, i);
6916 int match, j;
6917
6918 match = matching_constraint_num (constraint);
6919 if (match < 0)
6920 continue;
6921
6922 gcc_assert (match < noutputs);
6923 output = SET_DEST (p_sets[match]);
6924 input = RTVEC_ELT (inputs, i);
6925 /* Only do the transformation for pseudos. */
6926 if (! REG_P (output)
6927 || rtx_equal_p (output, input)
6928 || !(REG_P (input) || SUBREG_P (input)
6929 || MEM_P (input) || CONSTANT_P (input))
6930 || !general_operand (input, GET_MODE (output)))
6931 continue;
6932
6933 /* We can't do anything if the output is also used as input,
6934 as we're going to overwrite it. */
6935 for (j = 0; j < ninputs; j++)
6936 if (reg_overlap_mentioned_p (output, RTVEC_ELT (inputs, j)))
6937 break;
6938 if (j != ninputs)
6939 continue;
6940
6941 /* Avoid changing the same input several times. For
6942 asm ("" : "=mr" (out1), "=mr" (out2) : "0" (in), "1" (in));
6943 only change it once (to out1), rather than changing it
6944 first to out1 and afterwards to out2. */
6945 if (i > 0)
6946 {
6947 for (j = 0; j < noutputs; j++)
6948 if (output_matched[j] && input == SET_DEST (p_sets[j]))
6949 break;
6950 if (j != noutputs)
6951 continue;
6952 }
6953 output_matched[match] = true;
6954
6955 start_sequence ();
6956 emit_move_insn (output, copy_rtx (input));
6957 insns = end_sequence ();
6958 emit_insn_before (insns, insn);
6959
6960 constraint = ASM_OPERANDS_OUTPUT_CONSTRAINT(SET_SRC(p_sets[match]));
6961 bool early_clobber_p = strchr (s: constraint, c: '&') != NULL;
6962
6963 /* Now replace all mentions of the input with output. We can't
6964 just replace the occurrence in inputs[i], as the register might
6965 also be used in some other input (or even in an address of an
6966 output), which would mean possibly increasing the number of
6967 inputs by one (namely 'output' in addition), which might pose
6968 a too complicated problem for reload to solve. E.g. this situation:
6969
6970 asm ("" : "=r" (output), "=m" (input) : "0" (input))
6971
6972 Here 'input' is used in two occurrences as input (once for the
6973 input operand, once for the address in the second output operand).
6974 If we would replace only the occurrence of the input operand (to
6975 make the matching) we would be left with this:
6976
6977 output = input
6978 asm ("" : "=r" (output), "=m" (input) : "0" (output))
6979
6980 Now we suddenly have two different input values (containing the same
6981 value, but different pseudos) where we formerly had only one.
6982 With more complicated asms this might lead to reload failures
6983 which wouldn't have happen without this pass. So, iterate over
6984 all operands and replace all occurrences of the register used.
6985
6986 However, if one or more of the 'input' uses have a non-matching
6987 constraint and the matched output operand is an early clobber
6988 operand, then do not replace the input operand, since by definition
6989 it conflicts with the output operand and cannot share the same
6990 register. See PR89313 for details. */
6991
6992 for (j = 0; j < noutputs; j++)
6993 if (!rtx_equal_p (SET_DEST (p_sets[j]), input)
6994 && reg_overlap_mentioned_p (input, SET_DEST (p_sets[j])))
6995 SET_DEST (p_sets[j]) = replace_rtx (SET_DEST (p_sets[j]),
6996 input, output);
6997 for (j = 0; j < ninputs; j++)
6998 if (reg_overlap_mentioned_p (input, RTVEC_ELT (inputs, j)))
6999 {
7000 if (!early_clobber_p
7001 || match == matching_constraint_num
7002 (ASM_OPERANDS_INPUT_CONSTRAINT (op, j)))
7003 RTVEC_ELT (inputs, j) = replace_rtx (RTVEC_ELT (inputs, j),
7004 input, output);
7005 }
7006
7007 changed = true;
7008 }
7009
7010 if (changed)
7011 df_insn_rescan (insn);
7012}
7013
7014/* Add the decl D to the local_decls list of FUN. */
7015
7016void
7017add_local_decl (struct function *fun, tree d)
7018{
7019 gcc_assert (VAR_P (d));
7020 vec_safe_push (v&: fun->local_decls, obj: d);
7021}
7022
7023namespace {
7024
7025const pass_data pass_data_match_asm_constraints =
7026{
7027 .type: RTL_PASS, /* type */
7028 .name: "asmcons", /* name */
7029 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
7030 .tv_id: TV_NONE, /* tv_id */
7031 .properties_required: 0, /* properties_required */
7032 .properties_provided: 0, /* properties_provided */
7033 .properties_destroyed: 0, /* properties_destroyed */
7034 .todo_flags_start: 0, /* todo_flags_start */
7035 .todo_flags_finish: 0, /* todo_flags_finish */
7036};
7037
7038class pass_match_asm_constraints : public rtl_opt_pass
7039{
7040public:
7041 pass_match_asm_constraints (gcc::context *ctxt)
7042 : rtl_opt_pass (pass_data_match_asm_constraints, ctxt)
7043 {}
7044
7045 /* opt_pass methods: */
7046 unsigned int execute (function *) final override;
7047
7048}; // class pass_match_asm_constraints
7049
7050unsigned
7051pass_match_asm_constraints::execute (function *fun)
7052{
7053 basic_block bb;
7054 rtx_insn *insn;
7055 rtx pat, *p_sets;
7056 int noutputs;
7057
7058 if (!crtl->has_asm_statement)
7059 return 0;
7060
7061 df_set_flags (DF_DEFER_INSN_RESCAN);
7062 FOR_EACH_BB_FN (bb, fun)
7063 {
7064 FOR_BB_INSNS (bb, insn)
7065 {
7066 if (!INSN_P (insn))
7067 continue;
7068
7069 pat = PATTERN (insn);
7070 if (GET_CODE (pat) == PARALLEL)
7071 p_sets = &XVECEXP (pat, 0, 0), noutputs = XVECLEN (pat, 0);
7072 else if (GET_CODE (pat) == SET)
7073 p_sets = &PATTERN (insn), noutputs = 1;
7074 else
7075 continue;
7076
7077 if (GET_CODE (*p_sets) == SET
7078 && GET_CODE (SET_SRC (*p_sets)) == ASM_OPERANDS)
7079 match_asm_constraints_1 (insn, p_sets, noutputs);
7080 }
7081 }
7082
7083 return TODO_df_finish;
7084}
7085
7086} // anon namespace
7087
7088rtl_opt_pass *
7089make_pass_match_asm_constraints (gcc::context *ctxt)
7090{
7091 return new pass_match_asm_constraints (ctxt);
7092}
7093
7094
7095#include "gt-function.h"
7096

source code of gcc/function.cc